diff --git a/bundle.json b/bundle.json index c28db6880b55fa6c53a5cb81c430b53eff014a5c..8e6c15411a20103d34d9f44c863de858ba06082e 100644 --- a/bundle.json +++ b/bundle.json @@ -26,7 +26,8 @@ "base_group": [], "fwk_group": [ "//foundation/filemanagement/app_file_service:libremotefileshare", - "//foundation/filemanagement/app_file_service/interfaces/kits/js:fileshare" + "//foundation/filemanagement/app_file_service/interfaces/kits/js:fileshare", + "//foundation/filemanagement/app_file_service/interfaces/kits/js:fileuri" ], "service_group": [] }, diff --git a/interfaces/innerkits/remote_file_share/native/remote_file_share_log.h b/interfaces/innerkits/remote_file_share/native/remote_file_share_log.h index 4388cd4246c65135c0450071757dd5675b30a0c1..510e9c7511ba28b832274b7058269b8df0cd90e3 100644 --- a/interfaces/innerkits/remote_file_share/native/remote_file_share_log.h +++ b/interfaces/innerkits/remote_file_share/native/remote_file_share_log.h @@ -18,17 +18,18 @@ #include "hilog/log.h" namespace OHOS { +namespace AppFileService { static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_DOMAIN, REMOTE_SHARE_FILE_LOG_TAG}; #define PRINT_LOG(Level, fmt, ...) \ - OHOS::HiviewDFX::HiLog::Level(OHOS::LOG_LABEL, "[%{public}s:%{public}d] " fmt, \ + OHOS::HiviewDFX::HiLog::Level(OHOS::AppFileService::LOG_LABEL, "[%{public}s:%{public}d] " fmt, \ __FUNCTION__, __LINE__, ##__VA_ARGS__) -#define LOGD(fmt, ...) PRINT_LOG(Debug, fmt, ##__VA_ARGS__) #define LOGI(fmt, ...) PRINT_LOG(Info, fmt, ##__VA_ARGS__) #define LOGW(fmt, ...) PRINT_LOG(Warn, fmt, ##__VA_ARGS__) #define LOGE(fmt, ...) PRINT_LOG(Error, fmt, ##__VA_ARGS__) #define LOGF(fmt, ...) PRINT_LOG(Fatal, fmt, ##__VA_ARGS__) -} // OHOS +} // namespace AppFileService +} // namespace OHOS #endif // REMOTE_FILE_SHARE_LOG_H \ No newline at end of file diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 033e7d0da7831fc94faa46d33321766f94fb2de6..c14bbd22d20ec5352b11d1dc9c669df69a106b4e 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -55,23 +55,41 @@ ohos_shared_library("fileshare") { external_deps = [ "ability_base:want", "ability_base:zuri", - "ability_runtime:ability_manager", "ability_runtime:abilitykit_native", - "ability_runtime:napi_base_context", - "ability_runtime:static_subscriber_extension", "c_utils:utils", "data_share:datashare_common", "data_share:datashare_consumer", + "file_api:remote_uri_native", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", ] - defines = [ - "FILE_SHARE_LOG_TAG=\"FileShare\"", - "LOG_DOMAIN=0xD004313", + relative_install_dir = "module" + + part_name = "app_file_service" + subsystem_name = "filemanagement" +} + +ohos_shared_library("fileuri") { + include_dirs = [ + "//foundation/filemanagement/app_file_service/interfaces/kits/js", + "//foundation/filemanagement/file_api/utils/filemgmt_libn/include", + "//foundation/filemanagement/app_file_service/interfaces/kits/js/common", ] - relative_install_dir = "module" + sources = [ + "//foundation/filemanagement/app_file_service/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp", + "//foundation/filemanagement/app_file_service/interfaces/kits/js/file_uri/get_uri_from_path.cpp", + ] + + deps = [ + "//foundation/bundlemanager/bundle_framework/interfaces/kits/native/bundle:bundle_ndk", + "//foundation/filemanagement/file_api/utils/filemgmt_libn:filemgmt_libn", + ] + + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + + relative_install_dir = "module/file" part_name = "app_file_service" subsystem_name = "filemanagement" diff --git a/interfaces/kits/js/common/file_share_log.h b/interfaces/kits/js/common/log.h similarity index 75% rename from interfaces/kits/js/common/file_share_log.h rename to interfaces/kits/js/common/log.h index 0b52f7fceebcf657bae600fa87da5ce29596d17a..683e30355b6f37702fa7d52e656377e6e0ab67df 100644 --- a/interfaces/kits/js/common/file_share_log.h +++ b/interfaces/kits/js/common/log.h @@ -12,23 +12,25 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef FILE_SHARE_LOG_H -#define FILE_SHARE_LOG_H - +#ifndef LOG_H +#define LOG_H #include "hilog/log.h" namespace OHOS { -static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_DOMAIN, FILE_SHARE_LOG_TAG}; +namespace AppFileService { +const unsigned int APP_LOG_DOMAIN = 0xD004313; +const char APP_LOG_TAG[] = "AppFileService"; +static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, APP_LOG_DOMAIN, APP_LOG_TAG}; #define PRINT_LOG(Level, fmt, ...) \ - OHOS::HiviewDFX::HiLog::Level(OHOS::LOG_LABEL, "[%{public}s:%{public}d] " fmt, \ + OHOS::HiviewDFX::HiLog::Level(OHOS::AppFileService::LOG_LABEL, "[%{public}s:%{public}d] " fmt, \ __FUNCTION__, __LINE__, ##__VA_ARGS__) -#define LOGD(fmt, ...) PRINT_LOG(Debug, fmt, ##__VA_ARGS__) #define LOGI(fmt, ...) PRINT_LOG(Info, fmt, ##__VA_ARGS__) #define LOGW(fmt, ...) PRINT_LOG(Warn, fmt, ##__VA_ARGS__) #define LOGE(fmt, ...) PRINT_LOG(Error, fmt, ##__VA_ARGS__) #define LOGF(fmt, ...) PRINT_LOG(Fatal, fmt, ##__VA_ARGS__) -} // OHOS +} // namespace AppFileService +} // namespace OHOS -#endif // FILE_SHARE_LOG_H \ No newline at end of file +#endif // LOG_H \ No newline at end of file diff --git a/interfaces/kits/js/file_share/grant_uri_permission.cpp b/interfaces/kits/js/file_share/grant_uri_permission.cpp index a725932379c1de295da3d23f3fd53e7d518655b7..0dad0642b96cc8b8526eb558951938be0b85d91d 100644 --- a/interfaces/kits/js/file_share/grant_uri_permission.cpp +++ b/interfaces/kits/js/file_share/grant_uri_permission.cpp @@ -13,11 +13,14 @@ * limitations under the License. */ #include "grant_uri_permission.h" -#include "file_share_log.h" + +#include "ability.h" #include "datashare_helper.h" #include "datashare_values_bucket.h" -#include "napi_base_context.h" -#include "ability.h" +#include "ipc_skeleton.h" +#include "log.h" +#include "remote_uri.h" +#include "want.h" using namespace OHOS::DataShare; using namespace OHOS::FileManagement::LibN; @@ -49,6 +52,18 @@ namespace ModuleFileShare { return rowNum; } + static string GetModeFromFlag(int flag) + { + string mode = ""; + if (flag & OHOS::AAFwk::Want::FLAG_AUTH_READ_URI_PERMISSION) { + mode += "r"; + } + if (flag & OHOS::AAFwk::Want::FLAG_AUTH_WRITE_URI_PERMISSION) { + mode += "w"; + } + return mode; + } + static napi_value GetJSArgs(napi_env env, const NFuncArg &funcArg, DataShareValuesBucket &valuesBucket) { @@ -60,6 +75,12 @@ namespace ModuleFileShare { return nullptr; } + if (!DistributedFS::ModuleRemoteUri::RemoteUri::IsMediaUri(path.get())) { + LOGE("FileShare::GetJSArgs path parameter format error!"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto [succBundleName, bundleName, lenBundleName] = NVal(env, funcArg[NARG_POS::SECOND]).ToUTF8String(); if (!succBundleName) { LOGE("FileShare::GetJSArgs get bundleName parameter failed!"); @@ -67,9 +88,15 @@ namespace ModuleFileShare { return nullptr; } - auto [succMode, mode, lenMode] = NVal(env, funcArg[NARG_POS::THIRD]).ToUTF8String(); - if (!succMode) { - LOGE("FileShare::GetJSArgs get mode parameter failed!"); + string mode; + if (NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_number)) { + auto [succFlag, flag] = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(); + mode = GetModeFromFlag(flag); + } else if (NVal(env, funcArg[NARG_POS::THIRD]).TypeIs(napi_string)) { + auto [succFlag, flag, lenFlag] = NVal(env, funcArg[NARG_POS::THIRD]).ToUTF8String(); + mode = string(flag.get()); + } else { + LOGE("FileShare::GetJSArgs get flag parameter failed!"); NError(EINVAL).ThrowErr(env); return nullptr; } @@ -84,7 +111,7 @@ namespace ModuleFileShare { int32_t fileId = stoi(idStr); valuesBucket.Put(PERMISSION_FILE_ID, fileId); valuesBucket.Put(PERMISSION_BUNDLE_NAME, string(bundleName.get())); - valuesBucket.Put(PERMISSION_MODE, string(mode.get())); + valuesBucket.Put(PERMISSION_MODE, mode); napi_get_boolean(env, true, &result); return result; @@ -112,9 +139,9 @@ namespace ModuleFileShare { napi_value GrantUriPermission::Async(napi_env env, napi_callback_info info) { - LOGI("GrantUriPermission Begin!"); NFuncArg funcArg(env, info); if (!funcArg.InitArgs(NARG_CNT::THREE, NARG_CNT::FOUR)) { + LOGE("FileShare::GrantUriPermission GetJSArgsForGrantUriPermission Number of arguments unmatched!"); NError(EINVAL).ThrowErr(env); return nullptr; } @@ -129,7 +156,6 @@ namespace ModuleFileShare { auto cbExec = [valuesBucket, env]() -> NError { int ret = InsertByDatashare(env, valuesBucket); - LOGI("FileShare::GrantUriPermission InsertByDatashare return value is %{public}d", ret); if (ret < 0) { LOGE("FileShare::GrantUriPermission InsertByDatashare failed!"); return NError(-ret); @@ -141,7 +167,6 @@ namespace ModuleFileShare { if (err) { return { env, err.GetNapiErr(env) }; } - LOGI("GrantUriPermission Success!"); return NVal::CreateUndefined(env); }; diff --git a/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp b/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eed41abaffc6d0ecf8d782b2a7058cc777dc159a --- /dev/null +++ b/interfaces/kits/js/file_uri/file_uri_n_exporter.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "file_uri_n_exporter.h" +#include "get_uri_from_path.h" + +namespace OHOS { +namespace AppFileService { +namespace ModuleFileUri { +/*********************************************** + * Module export and register + ***********************************************/ +napi_value FileUriExport(napi_env env, napi_value exports) +{ + static napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("getUriFromPath", GetUriFromPath::Sync), + }; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} + +NAPI_MODULE(fileuri, FileUriExport) +} // namespace ModuleFileUri +} // namespace AppFileService +} // namespace OHOS diff --git a/interfaces/kits/js/file_uri/file_uri_n_exporter.h b/interfaces/kits/js/file_uri/file_uri_n_exporter.h new file mode 100644 index 0000000000000000000000000000000000000000..b37a3d9487c88434e4adab1de874c38dd02b5ff5 --- /dev/null +++ b/interfaces/kits/js/file_uri/file_uri_n_exporter.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef FILE_URI_N_EXPOTER_H +#define FILE_URI_N_EXPOTER_H + +#include "filemgmt_libn.h" + +namespace OHOS { +namespace AppFileService { +namespace ModuleFileUri { + napi_value FileUriExport(napi_env env, napi_value exports); +} // namespace ModuleFileUri +} // namespace AppFileService +} // namespace OHOS +#endif // FILE_URI_N_EXPOTER_H diff --git a/interfaces/kits/js/file_uri/get_uri_from_path.cpp b/interfaces/kits/js/file_uri/get_uri_from_path.cpp new file mode 100644 index 0000000000000000000000000000000000000000..abba57d3296eed99cbfca08bdf264463fabc124d --- /dev/null +++ b/interfaces/kits/js/file_uri/get_uri_from_path.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "get_uri_from_path.h" +#include "log.h" +#include "native_interface_bundle.h" + +namespace OHOS { +namespace AppFileService { +namespace ModuleFileUri { +using namespace OHOS::FileManagement::LibN; + +static string GetBundleName() +{ + OH_NativeBundle_ApplicationInfo nativeApplicationInfo = OH_NativeBundle_GetCurrentApplicationInfo(); + return nativeApplicationInfo.bundleName; +} + +napi_value GetUriFromPath::Sync(napi_env env, napi_callback_info info) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE)) { + LOGE("GetUriFromPath::Sync Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto [succPath, path, ignore] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succPath) { + LOGE("GetUriFromPath::Sync get path parameter failed!"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + string packageName = GetBundleName(); + string uri = SCHEME + SCHEME_SEPARATOR + PATH_SYMBOLS + packageName + path.get(); + return NVal::CreateUTF8String(env, uri).val_; +} + +} // namespace ModuleFileUri +} // namespace AppFileService +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/js/file_uri/get_uri_from_path.h b/interfaces/kits/js/file_uri/get_uri_from_path.h new file mode 100644 index 0000000000000000000000000000000000000000..7dbea01cd50d4fb90e09facdaa349d028eb6fb8f --- /dev/null +++ b/interfaces/kits/js/file_uri/get_uri_from_path.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef GET_URI_FROM_PATH_H +#define GET_URI_FROM_PATH_H + +#include +#include "filemgmt_libn.h" + +namespace OHOS { +namespace AppFileService { +namespace ModuleFileUri { +using namespace std; + +const string SCHEME = "file"; +const char SCHEME_SEPARATOR = ':'; +const string PATH_SYMBOLS = "//"; +const string FRAGMENT_SYMBOLS = "#"; + +class GetUriFromPath final { +public: + static napi_value Sync(napi_env env, napi_callback_info info); +}; +} // namespace ModuleFileUri +} // namespace AppFileService +} // namespace OHOS + +#endif // GET_URI_FROM_PATH_H \ No newline at end of file