diff --git a/bundle.json b/bundle.json index dab8e4353e7c78b8dd8e570bf17f599df6c98afe..c28db6880b55fa6c53a5cb81c430b53eff014a5c 100644 --- a/bundle.json +++ b/bundle.json @@ -25,7 +25,8 @@ "group_type": { "base_group": [], "fwk_group": [ - "//foundation/filemanagement/app_file_service:libremotefileshare" + "//foundation/filemanagement/app_file_service:libremotefileshare", + "//foundation/filemanagement/app_file_service/interfaces/kits/js:fileshare" ], "service_group": [] }, diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 436148513a1727f7a0697b6e87535a16cdf9ace1..033e7d0da7831fc94faa46d33321766f94fb2de6 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -17,6 +17,7 @@ ohos_shared_library("remotefileshare") { include_dirs = [ "//foundation/arkui/napi/interfaces/kits", "//third_party/bounds_checking_function/include", + "//foundation/filemanagement/file_api/utils/filemgmt_libn/include", ] sources = [ @@ -25,7 +26,7 @@ ohos_shared_library("remotefileshare") { ] deps = [ - "//foundation/distributeddatamgr/distributedfile/utils/filemgmt_libn", + "//foundation/filemanagement/file_api/utils/filemgmt_libn:filemgmt_libn", "//third_party/bounds_checking_function:libsec_shared", ] @@ -34,3 +35,44 @@ ohos_shared_library("remotefileshare") { part_name = "app_file_service" subsystem_name = "filemanagement" } + +ohos_shared_library("fileshare") { + 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", + ] + + sources = [ + "//foundation/filemanagement/app_file_service/interfaces/kits/js/file_share/fileshare_n_exporter.cpp", + "//foundation/filemanagement/app_file_service/interfaces/kits/js/file_share/grant_uri_permission.cpp", + ] + + deps = [ + "//foundation/filemanagement/file_api/utils/filemgmt_libn:filemgmt_libn", + ] + + 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", + "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" +} diff --git a/interfaces/kits/js/common/file_share_log.h b/interfaces/kits/js/common/file_share_log.h new file mode 100644 index 0000000000000000000000000000000000000000..0b52f7fceebcf657bae600fa87da5ce29596d17a --- /dev/null +++ b/interfaces/kits/js/common/file_share_log.h @@ -0,0 +1,34 @@ +/* + * 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_SHARE_LOG_H +#define FILE_SHARE_LOG_H + +#include "hilog/log.h" + +namespace OHOS { +static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_DOMAIN, FILE_SHARE_LOG_TAG}; + +#define PRINT_LOG(Level, fmt, ...) \ + OHOS::HiviewDFX::HiLog::Level(OHOS::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 + +#endif // FILE_SHARE_LOG_H \ No newline at end of file diff --git a/interfaces/kits/js/file_share/fileshare_n_exporter.cpp b/interfaces/kits/js/file_share/fileshare_n_exporter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a293e305735f287d0b805d6dabd9d802aa1b7654 --- /dev/null +++ b/interfaces/kits/js/file_share/fileshare_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 "fileshare_n_exporter.h" +#include "grant_uri_permission.h" + +namespace OHOS { +namespace AppFileService { +namespace ModuleFileShare { +/*********************************************** + * Module export and register + ***********************************************/ +napi_value FileShareExport(napi_env env, napi_value exports) +{ + static napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("grantUriPermission", GrantUriPermission::Async), + }; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; +} + +NAPI_MODULE(fileshare, FileShareExport) +} // namespace ModuleFileShare +} // namespace AppFileService +} // namespace OHOS diff --git a/interfaces/kits/js/file_share/fileshare_n_exporter.h b/interfaces/kits/js/file_share/fileshare_n_exporter.h new file mode 100644 index 0000000000000000000000000000000000000000..556f62a8ad198250938e575b3630e2a30a6e3b8b --- /dev/null +++ b/interfaces/kits/js/file_share/fileshare_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 FILESHARE_N_EXPOTER_H +#define FILESHARE_N_EXPOTER_H + +#include "filemgmt_libn.h" + +namespace OHOS { +namespace AppFileService { +namespace ModuleFileShare { + napi_value FileShareExport(napi_env env, napi_value exports); +} // namespace ModuleFileShare +} // namespace AppFileService +} // namespace OHOS +#endif // FILESHARE_N_EXPOTER_H diff --git a/interfaces/kits/js/file_share/grant_uri_permission.cpp b/interfaces/kits/js/file_share/grant_uri_permission.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a725932379c1de295da3d23f3fd53e7d518655b7 --- /dev/null +++ b/interfaces/kits/js/file_share/grant_uri_permission.cpp @@ -0,0 +1,158 @@ +/* + * 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 "grant_uri_permission.h" +#include "file_share_log.h" +#include "datashare_helper.h" +#include "datashare_values_bucket.h" +#include "napi_base_context.h" +#include "ability.h" + +using namespace OHOS::DataShare; +using namespace OHOS::FileManagement::LibN; + +namespace OHOS { +namespace AppFileService { +namespace ModuleFileShare { + + static bool IsAllDigits(string idStr) + { + for (size_t i = 0; i < idStr.size(); i++) { + if (!isdigit(idStr[i])) { + return false; + } + } + return true; + } + + static string GetIdFromUri(string uri) + { + string rowNum = ""; + size_t pos = uri.rfind('/'); + if (pos != string::npos) { + rowNum = uri.substr(pos + 1); + if (!IsAllDigits(rowNum)) { + rowNum = ""; + } + } + return rowNum; + } + + static napi_value GetJSArgs(napi_env env, const NFuncArg &funcArg, + DataShareValuesBucket &valuesBucket) + { + napi_value result = nullptr; + auto [succPath, path, lenPath] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!succPath) { + LOGE("FileShare::GetJSArgs get path parameter failed!"); + 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!"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + auto [succMode, mode, lenMode] = NVal(env, funcArg[NARG_POS::THIRD]).ToUTF8String(); + if (!succMode) { + LOGE("FileShare::GetJSArgs get mode parameter failed!"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + string idStr = GetIdFromUri(string(path.get())); + if (idStr == "") { + LOGE("FileShare::GetJSArgs get fileId parameter failed!"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + 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())); + + napi_get_boolean(env, true, &result); + return result; + } + + static int InsertByDatashare(napi_env env, const DataShareValuesBucket &valuesBucket) + { + int ret = -1; + std::shared_ptr dataShareHelper = nullptr; + sptr remote = new IRemoteStub(); + if (remote == nullptr) { + LOGE("FileShare::InsertByDatashare get remoteObject failed!"); + return ret; + } + + dataShareHelper = DataShare::DataShareHelper::Creator(remote->AsObject(), MEDIALIBRARY_DATA_URI); + Uri uri(MEDIA_GRANT_URI_PERMISSION); + ret = dataShareHelper->Insert(uri, valuesBucket); + if (ret < 0) { + LOGE("FileShare::InsertByDatashare insert failed with error code %{public}d!", ret); + return ret; + } + return ret; + } + + 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)) { + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + OHOS::DataShare::DataShareValuesBucket valuesBucket; + bool result = GetJSArgs(env, funcArg, valuesBucket); + if (!result) { + LOGE("FileShare::GrantUriPermission GetJSArgsForGrantUriPermission failed!"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + 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); + } + return NError(ERRNO_NOERR); + }; + + auto cbCompl = [](napi_env env, NError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } + LOGI("GrantUriPermission Success!"); + return NVal::CreateUndefined(env); + }; + + NVal thisVar(env, funcArg.GetThisVar()); + if (funcArg.GetArgc() == NARG_CNT::THREE) { + return NAsyncWorkPromise(env, thisVar).Schedule(GRANT_URI_NAME, cbExec, cbCompl).val_; + } else { + NVal cb(env, funcArg[NARG_POS::FOURTH]); + return NAsyncWorkCallback(env, thisVar, cb).Schedule(GRANT_URI_NAME, cbExec, cbCompl).val_; + } + } +} // namespace ModuleFileShare +} // namespace AppFileService +} // namespace OHOS diff --git a/interfaces/kits/js/file_share/grant_uri_permission.h b/interfaces/kits/js/file_share/grant_uri_permission.h new file mode 100644 index 0000000000000000000000000000000000000000..65f9890dca8f0c6866a939c10a9eae4a7d95b7fe --- /dev/null +++ b/interfaces/kits/js/file_share/grant_uri_permission.h @@ -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. + */ + +#ifndef GRANT_URI_PERMISSION_H +#define GRANT_URI_PERMISSION_H + +#include "filemgmt_libn.h" +#include "iremote_broker.h" + +namespace OHOS { +namespace AppFileService { +namespace ModuleFileShare { + +using namespace std; + +const string MEDIA_GRANT_URI_PERMISSION = "datashare:///media/file_operation/bundle_permission_insert_operation"; +const string MEDIALIBRARY_DATA_URI = "datashare:///media"; +const string MEDIA_FILEMODE_READONLY = "r"; +const string GRANT_URI_NAME = "file_share_grant_uri_permission"; +const string PERMISSION_BUNDLE_NAME = "bundle_name"; +const string PERMISSION_FILE_ID = "file_id"; +const string PERMISSION_MODE = "mode"; + +class FileShareGrantToken : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.fileshare.grantUriPermission"); + + FileShareGrantToken() = default; + virtual ~FileShareGrantToken() noexcept = default; +}; + +class GrantUriPermission final { +public: + static napi_value Async(napi_env env, napi_callback_info info); +}; +} // namespace ModuleFileShare +} // namespace AppFileService +} // namespace OHOS +#endif // GRANT_URI_PERMISSION_H