From 528ddcbd870a790731d61ff83379eecb47766015 Mon Sep 17 00:00:00 2001 From: caochuan Date: Fri, 7 Jul 2023 14:00:18 +0800 Subject: [PATCH] add notify base1 Signed-off-by: caochuan --- bundle.json | 1 + .../js/napi/file_access_module/BUILD.gn | 7 +- .../napi_fileaccess_helper.cpp | 9 +- .../napi_observer_callback.cpp | 6 +- .../napi_observer_callback.h | 10 ++- interfaces/inner_api/file_access/BUILD.gn | 3 + .../include/file_access_ext_ability.h | 1 - .../file_access/include/file_access_helper.h | 2 +- .../include/file_access_observer_common.h | 2 +- .../include/js_file_access_ext_ability.h | 1 + .../src/file_access_ext_ability.cpp | 7 +- .../file_access/src/file_access_helper.cpp | 57 +++++++++++- .../src/js_file_access_ext_ability.cpp | 38 ++++++-- services/5010.json | 12 +++ services/5010.xml | 24 ----- services/BUILD.gn | 79 ++++++++++++++++- services/file_access_service.cfg | 2 +- services/libfile_access_service.map | 26 ++++++ .../include/file_access_service.h | 2 +- .../include/file_access_service_proxy.h | 2 +- .../include/ifile_access_service_base.h | 2 +- .../include/iobserver_callback.h | 2 +- .../include/observer_callback_proxy.h | 2 +- .../include/observer_callback_stub.h | 10 ++- .../src/file_access_service.cpp | 45 +++++----- .../src/file_access_service_proxy.cpp | 6 +- .../src/file_access_service_stub.cpp | 17 ++-- .../src/observer_callback_proxy.cpp | 68 +++++++++++++++ .../src/observer_callback_stub.cpp | 87 +++++++++++++++++++ utils/file_access_framework_errno.h | 3 +- 30 files changed, 436 insertions(+), 97 deletions(-) create mode 100644 services/5010.json delete mode 100755 services/5010.xml create mode 100644 services/libfile_access_service.map create mode 100644 services/native/file_access_service/src/observer_callback_proxy.cpp create mode 100644 services/native/file_access_service/src/observer_callback_stub.cpp diff --git a/bundle.json b/bundle.json index 83790db9..484d26fc 100644 --- a/bundle.json +++ b/bundle.json @@ -26,6 +26,7 @@ "ability_runtime", "ipc", "samgr", + "safwk", "napi", "file_api", "bundle_framework", diff --git a/frameworks/js/napi/file_access_module/BUILD.gn b/frameworks/js/napi/file_access_module/BUILD.gn index 8251bfb0..5b6d7062 100644 --- a/frameworks/js/napi/file_access_module/BUILD.gn +++ b/frameworks/js/napi/file_access_module/BUILD.gn @@ -35,13 +35,17 @@ ohos_shared_library("fileaccess") { "file_info/napi_file_info_exporter.cpp", "file_info/napi_file_iterator_exporter.cpp", "napi_fileaccess_helper.cpp", + "napi_observer_callback.cpp", "napi_utils.cpp", "native_fileaccess_module.cpp", "root_info/napi_root_info_exporter.cpp", "root_info/napi_root_iterator_exporter.cpp", ] - deps = [ "${user_file_service_path}/interfaces/inner_api/file_access:file_access_extension_ability_kit" ] + deps = [ + "${user_file_service_path}/interfaces/inner_api/file_access:file_access_extension_ability_kit", + "${user_file_service_path}/services:file_access_service", + ] external_deps = [ "ability_base:want", @@ -53,6 +57,7 @@ ohos_shared_library("fileaccess") { "common_event_service:cesfwk_innerkits", "file_api:filemgmt_libn", "hilog:libhilog", + "ipc:ipc_core", "multimedia_image_framework:image", ] } diff --git a/frameworks/js/napi/file_access_module/napi_fileaccess_helper.cpp b/frameworks/js/napi/file_access_module/napi_fileaccess_helper.cpp index 3097ee4a..02f54618 100644 --- a/frameworks/js/napi/file_access_module/napi_fileaccess_helper.cpp +++ b/frameworks/js/napi/file_access_module/napi_fileaccess_helper.cpp @@ -1216,6 +1216,8 @@ static bool parseRegisterObserverArgs(napi_env env, NFuncArg &funcArg, std::stri NError(EINVAL).ThrowErr(env); return false; } + + uri = uriPtr.get(); std::tie(succ, notifyForDescendants) = NVal(env, funcArg[NARG_POS::SECOND]).ToBool(); if (!succ) { NError(EINVAL).ThrowErr(env); @@ -1246,7 +1248,9 @@ napi_value NAPI_RegisterObserver(napi_env env, napi_callback_info info) return nullptr; } - sptr callback; + napi_value napiCallback = funcArg[NARG_POS::THIRD]; + std::shared_ptr observer = std::make_shared(env, napiCallback); + sptr callback = new (std::nothrow) NapiObserverCallback(observer); if (callback == nullptr) { NError(EINVAL).ThrowErr(env); return nullptr; @@ -1260,7 +1264,6 @@ napi_value NAPI_RegisterObserver(napi_env env, napi_callback_info info) }; std::unique_ptr observerWrapper = std::make_unique(); observerWrapper->callback = callback; - napi_value napiCallback = funcArg[NARG_POS::THIRD]; if (napi_wrap(env, napiCallback, observerWrapper.get(), finalize, nullptr, nullptr) != napi_ok) { NError(EINVAL).ThrowErr(env); return nullptr; @@ -1272,7 +1275,7 @@ napi_value NAPI_RegisterObserver(napi_env env, napi_callback_info info) return nullptr; } OHOS::Uri uri(uriString); - auto retCode = fileAccessHelper->RegisterNotify(uri, callback, notifyForDescendants); + auto retCode = fileAccessHelper->RegisterNotify(uri, notifyForDescendants, callback); if (retCode != ERR_OK) { NError(retCode).ThrowErr(env); return nullptr; diff --git a/frameworks/js/napi/file_access_module/napi_observer_callback.cpp b/frameworks/js/napi/file_access_module/napi_observer_callback.cpp index 26709762..cc0fa8ce 100644 --- a/frameworks/js/napi/file_access_module/napi_observer_callback.cpp +++ b/frameworks/js/napi/file_access_module/napi_observer_callback.cpp @@ -107,9 +107,9 @@ void NapiObserver::OnChange(NotifyMessage ¬ifyMessage) napi_value callback = nullptr; napi_value args[ARGS_ONE] = {napiNotifyMessage.val_}; - napi_status status = napi_get_reference_value(param->napiObserver->env_, param->napiObserver->cbOnRef_, + napi_status ret = napi_get_reference_value(param->napiObserver->env_, param->napiObserver->cbOnRef_, &callback); - if (status != napi_ok) { + if (ret != napi_ok) { HILOG_ERROR("Notify get reference failed, status:%{public}d.", status); napi_close_handle_scope(param->napiObserver->env_, scope); return; @@ -117,7 +117,7 @@ void NapiObserver::OnChange(NotifyMessage ¬ifyMessage) napi_value global = nullptr; napi_get_global(param->napiObserver->env_, &global); napi_value result = nullptr; - napi_status ret = napi_call_function(param->napiObserver->env_, global, callback, ARGS_ONE, args, &result); + ret = napi_call_function(param->napiObserver->env_, global, callback, ARGS_ONE, args, &result); if (ret != napi_ok) { HILOG_ERROR("Notify failed, status:%{public}d.", ret); napi_close_handle_scope(param->napiObserver->env_, scope); diff --git a/frameworks/js/napi/file_access_module/napi_observer_callback.h b/frameworks/js/napi/file_access_module/napi_observer_callback.h index a3977a62..9d56088f 100644 --- a/frameworks/js/napi/file_access_module/napi_observer_callback.h +++ b/frameworks/js/napi/file_access_module/napi_observer_callback.h @@ -18,10 +18,12 @@ #include "file_access_framework_errno.h" #include "hilog_wrapper.h" +#include "observer_callback_stub.h" #include "napi/native_common.h" #include "napi/native_node_api.h" #include "napi/native_api.h" #include "uri.h" +#include "uv.h" namespace OHOS { namespace FileAccessFwk { @@ -42,11 +44,11 @@ private: std::unique_ptr work_ = nullptr; }; -class NapiObserverCallback { +class NapiObserverCallback : public ObserverCallbackStub{ public: - explicit NapiObserverCallback(std::shared_ptr observer): observer_(observer) {} - virtual ~NapiObserverCallback() {} - void OnChange(NotifyMessage ¬ifyMessage) + explicit NapiObserverCallback(std::shared_ptr observer): ObserverCallbackStub(), observer_(observer){} + virtual ~NapiObserverCallback() = default; + void OnChange(NotifyMessage ¬ifyMessage) override { observer_->OnChange(notifyMessage); } diff --git a/interfaces/inner_api/file_access/BUILD.gn b/interfaces/inner_api/file_access/BUILD.gn index 8b057e7f..95552601 100644 --- a/interfaces/inner_api/file_access/BUILD.gn +++ b/interfaces/inner_api/file_access/BUILD.gn @@ -51,9 +51,11 @@ ohos_shared_library("file_access_extension_ability_kit") { include_dirs = [ "${ability_runtime_path}/interfaces/kits/native/appkit/ability_runtime/context", "${file_api_path}/utils/filemgmt_libn/include", + "${user_file_service_path}/services/native/file_access_service/include", ] sources = [ + "${user_file_service_path}/services/native/file_access_service/src/file_access_service_proxy.cpp", "src/file_access_ext_ability.cpp", "src/file_access_ext_connection.cpp", "src/file_access_ext_proxy.cpp", @@ -71,6 +73,7 @@ ohos_shared_library("file_access_extension_ability_kit") { "${ability_runtime_path}/frameworks/native/ability:ability_context_public_config", ] + external_deps = [ "ability_base:want", "ability_base:zuri", diff --git a/interfaces/inner_api/file_access/include/file_access_ext_ability.h b/interfaces/inner_api/file_access/include/file_access_ext_ability.h index 81c306db..644d1662 100644 --- a/interfaces/inner_api/file_access/include/file_access_ext_ability.h +++ b/interfaces/inner_api/file_access/include/file_access_ext_ability.h @@ -63,7 +63,6 @@ public: static void SetCreator(const CreatorFunc& creator); virtual int StartWatcher(const Uri &uri); virtual int StopWatcher(const Uri &uri); - virtual int Notify(Uri &uri, NotifyType notifyType); private: static CreatorFunc creator_; }; diff --git a/interfaces/inner_api/file_access/include/file_access_helper.h b/interfaces/inner_api/file_access/include/file_access_helper.h index 80fe58d6..8c6e2701 100644 --- a/interfaces/inner_api/file_access/include/file_access_helper.h +++ b/interfaces/inner_api/file_access/include/file_access_helper.h @@ -85,7 +85,7 @@ public: int GetFileInfoFromUri(Uri &selectFile, FileInfo &fileInfo); int GetFileInfoFromRelativePath(std::string &selectFile, FileInfo &fileInfo); int GetRoots(std::vector &rootInfoVec); - int RegisterNotify(Uri uri, sptr &observer, bool notifyForDescendants); + int RegisterNotify(Uri uri, bool notifyForDescendants, sptr &observer); int UnregisterNotify(Uri uri, sptr &observer); private: int StartWatcher(Uri &uri); diff --git a/interfaces/inner_api/file_access/include/file_access_observer_common.h b/interfaces/inner_api/file_access/include/file_access_observer_common.h index 14b373b2..7e454704 100644 --- a/interfaces/inner_api/file_access/include/file_access_observer_common.h +++ b/interfaces/inner_api/file_access/include/file_access_observer_common.h @@ -60,7 +60,7 @@ public: HILOG_ERROR("NotifyMessage Unmarshalling error:write notifyType fail."); return false; } - for (int i = 0; i < uris_.size(); i++) { + for (size_t i = 0; i < uris_.size(); i++) { if (!parcel.WriteString(uris_[i])) { HILOG_ERROR("NotifyMessage Unmarshalling error:write srcUri fail."); return false; diff --git a/interfaces/inner_api/file_access/include/js_file_access_ext_ability.h b/interfaces/inner_api/file_access/include/js_file_access_ext_ability.h index 79e0eb1e..e85bdd5d 100644 --- a/interfaces/inner_api/file_access/include/js_file_access_ext_ability.h +++ b/interfaces/inner_api/file_access/include/js_file_access_ext_ability.h @@ -82,6 +82,7 @@ private: int CallJsMethod(const std::string &funcName, JsRuntime &jsRuntime, NativeReference *jsObj, InputArgsParser argParser, ResultValueParser retParser); void GetSrcPath(std::string &srcPath); + static int Notify(Uri &uri, NotifyType notifyType); static NativeValue* FuncCallback(NativeEngine *engine, NativeCallbackInfo *info); JsRuntime &jsRuntime_; std::shared_ptr jsObj_; diff --git a/interfaces/inner_api/file_access/src/file_access_ext_ability.cpp b/interfaces/inner_api/file_access/src/file_access_ext_ability.cpp index c56c4fd6..3bfc33a0 100644 --- a/interfaces/inner_api/file_access/src/file_access_ext_ability.cpp +++ b/interfaces/inner_api/file_access/src/file_access_ext_ability.cpp @@ -20,6 +20,7 @@ #include "file_access_framework_errno.h" #include "hilog_wrapper.h" #include "hitrace_meter.h" +#include "if_system_ability_manager.h" #include "js_file_access_ext_ability.h" #include "runtime.h" @@ -164,11 +165,5 @@ int FileAccessExtAbility::StopWatcher(const Uri &uri) HILOG_ERROR("FileAccessExtAbility::StopWatcher Undefined operation"); return EPERM; } - -int FileAccessExtAbility::Notify(Uri &uri, NotifyType notifyType) -{ - HILOG_ERROR("FileAccessExtAbility::Notify Undefined operation"); - return EPERM; -} } // namespace FileAccessFwk } // namespace OHOS diff --git a/interfaces/inner_api/file_access/src/file_access_helper.cpp b/interfaces/inner_api/file_access/src/file_access_helper.cpp index df45fb58..117b5508 100644 --- a/interfaces/inner_api/file_access/src/file_access_helper.cpp +++ b/interfaces/inner_api/file_access/src/file_access_helper.cpp @@ -20,6 +20,7 @@ #include "bundle_mgr_proxy.h" #include "file_access_framework_errno.h" #include "file_access_extension_info.h" +#include "file_access_service_proxy.h" #include "hilog_wrapper.h" #include "hitrace_meter.h" #include "if_system_ability_manager.h" @@ -1215,16 +1216,66 @@ int FileAccessHelper::StopWatcher(Uri &uri) return ERR_OK; } -int FileAccessHelper::RegisterNotify(Uri uri, sptr &observer, bool notifyForDescendants) +int FileAccessHelper::RegisterNotify(Uri uri, bool notifyForDescendants, sptr &observer) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "RegisterNotify"); - return ERR_OK; + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (!systemAbilityManager) { + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_LOAD_SA; + } + + sptr remoteObject = systemAbilityManager->GetSystemAbility(5010); + if (!remoteObject) { + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_LOAD_SA; + } + auto proxy = iface_cast(remoteObject); + int ret = proxy->RegisterNotify(uri, notifyForDescendants, observer); + if (ret != ERR_OK) { + HILOG_ERROR("RegisterNotify error ret = %{public}d", ret); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ret; + } + + ret = StartWatcher(uri); + if (ret != ERR_OK) { + HILOG_ERROR("StartWatcher error ret = %{public}d", ret); + } + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ret; } int FileAccessHelper::UnregisterNotify(Uri uri, sptr &observer) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "UnregisterNotify"); - return ERR_OK; + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (!systemAbilityManager) { + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_LOAD_SA; + } + + sptr remoteObject = systemAbilityManager->GetSystemAbility(5010); + if (!remoteObject) { + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_LOAD_SA; + } + auto proxy = iface_cast(remoteObject); + int ret = proxy->UnregisterNotify(uri, observer); + if (ret != ERR_OK) { + HILOG_ERROR("UnregisterNotify error ret = %{public}d", ret); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ret; + } + + ret = StopWatcher(uri); + if (ret != ERR_OK) { + HILOG_ERROR("StopWatcher error ret = %{public}d", ret); + } + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ret; } void FileAccessDeathRecipient::OnRemoteDied(const wptr &remote) diff --git a/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp b/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp index 694d86b0..221fe00e 100644 --- a/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp +++ b/interfaces/inner_api/file_access/src/js_file_access_ext_ability.cpp @@ -18,13 +18,16 @@ #include "ability_info.h" #include "accesstoken_kit.h" #include "extension_context.h" +#include "file_access_service_proxy.h" #include "file_access_ext_stub_impl.h" #include "file_access_observer_common.h" #include "file_access_extension_info.h" #include "file_access_framework_errno.h" #include "hilog_wrapper.h" #include "hitrace_meter.h" +#include "if_system_ability_manager.h" #include "ipc_skeleton.h" +#include "iservice_registry.h" #include "js_runtime.h" #include "js_runtime_utils.h" #include "napi/native_api.h" @@ -1303,16 +1306,9 @@ NativeValue* JsFileAccessExtAbility::FuncCallback(NativeEngine* engine, NativeCa int32_t event = UnwrapInt32FromJS(reinterpret_cast(engine), reinterpret_cast(info->argv[ARGC_ONE])); - JsFileAccessExtAbility* jsExtension = static_cast(info->functionInfo->data); - if (jsExtension == nullptr) { - HILOG_ERROR("invalid JsFileAccessExtAbility."); - FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return engine->CreateUndefined(); - } - Uri uri(uriString); NotifyType notifyType = static_cast(event); - auto ret = jsExtension->Notify(uri, notifyType); + auto ret = Notify(uri, notifyType); if (ret != ERR_OK) { HILOG_ERROR("JsFileAccessExtAbility notify error, ret:%{public}d", ret); } @@ -1415,5 +1411,31 @@ int JsFileAccessExtAbility::StopWatcher(const Uri &uri) FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return ERR_OK; } + +int JsFileAccessExtAbility::Notify(Uri &uri, NotifyType notifyType) +{ + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "Notify"); + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (!systemAbilityManager) { + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_LOAD_SA; + } + + sptr remoteObject = systemAbilityManager->GetSystemAbility(5010); + if (!remoteObject) { + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_LOAD_SA; + } + auto proxy = iface_cast(remoteObject); + auto ret = proxy->OnChange(uri, notifyType); + if (ret != ERR_OK) { + HILOG_ERROR("notify error, ret:%{public}d", ret); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ret; + } + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ERR_OK; +} } // namespace FileAccessFwk } // namespace OHOS diff --git a/services/5010.json b/services/5010.json new file mode 100644 index 00000000..8f40348d --- /dev/null +++ b/services/5010.json @@ -0,0 +1,12 @@ +{ + "process": "file_access_service", + "systemability": [ + { + "name": 5010, + "libpath": "libfile_access_service.z.so", + "run-on-create": false, + "distributed": false, + "dump_level": 1 + } + ] +} \ No newline at end of file diff --git a/services/5010.xml b/services/5010.xml deleted file mode 100755 index eb3849d6..00000000 --- a/services/5010.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - file_access_service - - 5010 - libfile_access_service.z.so - false - false - 1 - - \ No newline at end of file diff --git a/services/BUILD.gn b/services/BUILD.gn index 9f2c88e9..5308ed4f 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -15,7 +15,50 @@ import("//build/ohos.gni") import("//foundation/filemanagement/user_file_service/filemanagement_aafwk.gni") group("user_file_managers") { - deps = [ ":external_file_manager_hap" ] + deps = [ + ":external_file_manager_hap", + ":file_access_service", + ] +} + +ohos_sa_profile("file_access_service_profile") { + sources = [ "5010.json" ] + part_name = "user_file_service" +} + +ohos_prebuilt_etc("file_access_service.cfg") { + source = "file_access_service.cfg" + relative_install_dir = "init" + subsystem_name = "filemanagement" + part_name = "user_file_service" +} + +config("ability_config") { + visibility = [ ":*" ] + include_dirs = [ + "native/file_access_service/include", + "${user_file_service_path}/utils", + "${user_file_service_path}/interfaces/inner_api/file_access/include", + "${user_file_service_path}/frameworks/js/napi/file_access_module", + "${ability_runtime_services_path}/common/include", + "${ability_runtime_kits_path}/ability/native/include/continuation/distributed", + "${ability_runtime_kits_path}/ability/native/include/continuation/kits", + "${ability_runtime_kits_path}/appkit/native/app/include", + "${ability_runtime_napi_path}/inner/napi_common", + "${access_token_path}/interfaces/innerkits/accesstoken/include", + ] +} + +config("ability_public_config") { + visibility = [ ":*" ] + include_dirs = [ + "native/file_access_service/include", + "${user_file_service_path}/utils", + "${ability_runtime_kits_path}/appkit/native/ability_runtime/app", + "${ability_runtime_kits_path}/appkit/native/app/include", + "${ability_runtime_kits_path}/appkit/native/ability_runtime/context", + "${user_file_service_path}/interfaces/kits/js/src/common", + ] } ohos_hap("external_file_manager_hap") { @@ -31,6 +74,40 @@ ohos_hap("external_file_manager_hap") { module_install_dir = "app/com.ohos.UserFile.ExternalFileManager" } +ohos_shared_library("file_access_service") { + include_dirs = [ + "${user_file_service_path}/services/native/file_access_service/include", + "${ability_runtime_path}/interfaces/kits/native/appkit/ability_runtime/context", + ] + + sources = [ + "native/file_access_service/src/file_access_service.cpp", + "native/file_access_service/src/file_access_service_proxy.cpp", + "native/file_access_service/src/file_access_service_stub.cpp", + "native/file_access_service/src/observer_callback_proxy.cpp", + "native/file_access_service/src/observer_callback_stub.cpp", + ] + configs = [ ":ability_config" ] + version_script = "libfile_access_service.map" + public_configs = [ ":ability_public_config" ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "access_token:libaccesstoken_sdk", + "access_token:libtokenid_sdk", + "c_utils:utils", + "hitrace:hitrace_meter", + "hilog:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + subsystem_name = "filemanagement" + part_name = "user_file_service" +} + ohos_js_assets("external_file_manager_js_assets") { hap_profile = "file_extension_hap/entry/src/main/module.json" ets2abc = true diff --git a/services/file_access_service.cfg b/services/file_access_service.cfg index 8b3e76a2..21d66c59 100644 --- a/services/file_access_service.cfg +++ b/services/file_access_service.cfg @@ -1,7 +1,7 @@ { "services" : [{ "name" : "file_access_service", - "path" : ["/system/bin/sa_main", "/system/profile/file_access_service.xml"], + "path" : ["/system/bin/sa_main", "/system/profile/file_access_service.json"], "uid" : "file_manager", "secon" : "u:r:file_access_service:s0" } diff --git a/services/libfile_access_service.map b/services/libfile_access_service.map new file mode 100644 index 00000000..e7160ffa --- /dev/null +++ b/services/libfile_access_service.map @@ -0,0 +1,26 @@ +# Copyright (c) 2023 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. + +1.0 { + global: + extern "C++" { + *FileAccessServiceProxy*; + *FileAccessService*; + *FileAccessServiceStub*; + *ObserverCallbackStub*; + *ObserverCallbackProxy*; + **; + }; + local: + *; +}; \ No newline at end of file diff --git a/services/native/file_access_service/include/file_access_service.h b/services/native/file_access_service/include/file_access_service.h index afc984ec..b27ec5d1 100644 --- a/services/native/file_access_service/include/file_access_service.h +++ b/services/native/file_access_service/include/file_access_service.h @@ -90,7 +90,7 @@ public: int32_t Dump(int32_t fd, const std::vector &args) override; public: - int32_t RegisterNotify(Uri uri, const sptr &observer, bool notifyForDescendants) override; + int32_t RegisterNotify(Uri uri, bool notifyForDescendants, const sptr &observer) override; int32_t UnregisterNotify(Uri uri, const sptr &observer) override; int32_t OnChange(Uri uri, NotifyType notifyType) override; diff --git a/services/native/file_access_service/include/file_access_service_proxy.h b/services/native/file_access_service/include/file_access_service_proxy.h index cd9bf2dd..9a91e511 100644 --- a/services/native/file_access_service/include/file_access_service_proxy.h +++ b/services/native/file_access_service/include/file_access_service_proxy.h @@ -26,7 +26,7 @@ public: explicit FileAccessServiceProxy(const sptr &impl) : IRemoteProxy(impl) {} ~FileAccessServiceProxy() = default; int32_t OnChange(Uri uri, NotifyType notifyType) override; - int32_t RegisterNotify(Uri uri, const sptr &observer, bool notifyForDescendants) override; + int32_t RegisterNotify(Uri uri, bool notifyForDescendants, const sptr &observer) override; int32_t UnregisterNotify(Uri uri, const sptr &observer) override; private: diff --git a/services/native/file_access_service/include/ifile_access_service_base.h b/services/native/file_access_service/include/ifile_access_service_base.h index 9f276b6d..ab90ebbc 100644 --- a/services/native/file_access_service/include/ifile_access_service_base.h +++ b/services/native/file_access_service/include/ifile_access_service_base.h @@ -36,7 +36,7 @@ public: }; virtual int32_t OnChange(Uri uri, NotifyType notifyType) = 0; - virtual int32_t RegisterNotify(Uri uri, const sptr &observer, bool notifyForDescendants) = 0; + virtual int32_t RegisterNotify(Uri uri, bool notifyForDescendants, const sptr &observer) = 0; virtual int32_t UnregisterNotify(Uri uri, const sptr &observer) = 0; }; } // namespace FileAccessFwk diff --git a/services/native/file_access_service/include/iobserver_callback.h b/services/native/file_access_service/include/iobserver_callback.h index bf8c327f..ea47939e 100644 --- a/services/native/file_access_service/include/iobserver_callback.h +++ b/services/native/file_access_service/include/iobserver_callback.h @@ -28,7 +28,7 @@ public: public: enum { - CMD_ONCHANGE = 0 + CMD_CALLBACK = 0 }; virtual void OnChange(NotifyMessage ¬ifyMessage) = 0; }; diff --git a/services/native/file_access_service/include/observer_callback_proxy.h b/services/native/file_access_service/include/observer_callback_proxy.h index 20a4b060..fc390ba6 100644 --- a/services/native/file_access_service/include/observer_callback_proxy.h +++ b/services/native/file_access_service/include/observer_callback_proxy.h @@ -27,7 +27,7 @@ class ObserverCallbackProxy : public IRemoteProxy { public: explicit ObserverCallbackProxy(const sptr& remote) : IRemoteProxy(remote) {} ~ObserverCallbackProxy() = default; - void OnChange(NotifyMessage ¬ifyMessage) {} override; + void OnChange(NotifyMessage ¬ifyMessage) override; private: static inline BrokerDelegator delegator_; diff --git a/services/native/file_access_service/include/observer_callback_stub.h b/services/native/file_access_service/include/observer_callback_stub.h index 8ebbc8e5..229ba642 100644 --- a/services/native/file_access_service/include/observer_callback_stub.h +++ b/services/native/file_access_service/include/observer_callback_stub.h @@ -18,6 +18,7 @@ #include #include +#include #include "iobserver_callback.h" #include "iremote_stub.h" @@ -30,13 +31,14 @@ namespace FileAccessFwk { class ObserverCallbackStub : public IRemoteStub { public: ObserverCallbackStub() = default; - virtual ~ObserverCallbackStub() = default; - int OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override; - void OnChange(NotifyMessage ¬ifyMessage) override {} + virtual ~ObserverCallbackStub(); + int32_t OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override; private: int32_t OnChangeStub(MessageParcel &data, MessageParcel &reply); - int32_t ChooseCodeStub(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option); + void InitStubFuncMap(); + using RequestFuncType = int (ObserverCallbackStub::*)(MessageParcel &data, MessageParcel &reply); + std::map stubFuncMap_ = {}; }; } // namespace FileAccessFwk } // namespace OHOS diff --git a/services/native/file_access_service/src/file_access_service.cpp b/services/native/file_access_service/src/file_access_service.cpp index e1ccd19f..c8f9f538 100644 --- a/services/native/file_access_service/src/file_access_service.cpp +++ b/services/native/file_access_service/src/file_access_service.cpp @@ -24,6 +24,10 @@ namespace OHOS { namespace FileAccessFwk { +namespace { + auto pms = FileAccessService::GetInstance(); + const bool G_REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(pms.GetRefPtr()); +} sptr FileAccessService::instance_; std::mutex FileAccessService::mutex_; sptr FileAccessService::GetInstance() @@ -43,7 +47,7 @@ sptr FileAccessService::GetInstance() return instance_; } -FileAccessService::FileAccessService() : SystemAbility(FILE_ACCESS_SERVICE_ID, false) {} +FileAccessService::FileAccessService() : SystemAbility(5010, false) {} void FileAccessService::OnStart() { @@ -80,14 +84,14 @@ bool FileAccessService::IsServiceReady() const return ready_; } -static bool IsParentUri(const string &comparedUriStr, string &srcUriStr) +static bool IsParentUri(const std::string &comparedUriStr, std::string &srcUriStr) { if ((comparedUriStr.empty()) || (srcUriStr.empty())) { HILOG_ERROR("Uri is empty"); return false; } size_t slashIndex = srcUriStr.rfind("/"); - if (slashIndex != string::npos) { + if (slashIndex != std::string::npos) { if (comparedUriStr.compare(srcUriStr.substr(0, slashIndex)) == 0) { return true; } @@ -95,14 +99,14 @@ static bool IsParentUri(const string &comparedUriStr, string &srcUriStr) return false; } -static bool IsChildUri(const string &comparedUriStr, string &srcUriStr) +static bool IsChildUri(const std::string &comparedUriStr, std::string &srcUriStr) { if ((comparedUriStr.empty()) || (srcUriStr.empty())) { HILOG_ERROR("Uri is empty"); return false; } size_t slashIndex = comparedUriStr.rfind("/"); - if (slashIndex != string::npos) { + if (slashIndex != std::string::npos) { if (srcUriStr.compare(comparedUriStr.substr(0, slashIndex)) == 0) { return true; } @@ -110,13 +114,12 @@ static bool IsChildUri(const string &comparedUriStr, string &srcUriStr) return false; } -int32_t FileAccessService::RegisterNotify(Uri uri, const sptr &observer, - bool notifyForDescendants) +int32_t FileAccessService::RegisterNotify(Uri uri, bool notifyForDescendants, const sptr &observer) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "RegisterNotify"); - shared_ptr obsContext = make_shared(observer); + std::shared_ptr obsContext = std::make_shared(observer); // find if obsManager_ has this callback. - uint32_t code = obsManager_.getId([obsContext](const shared_ptr &afterContext) { + uint32_t code = obsManager_.getId([obsContext](const std::shared_ptr &afterContext) { return obsContext->EqualTo(afterContext); }); if (code == HolderManager>::CODE_CAN_NOT_FIND) { @@ -127,10 +130,10 @@ int32_t FileAccessService::RegisterNotify(Uri uri, const sptrRef(); } - string uriStr = uri.ToString(); + std::string uriStr = uri.ToString(); std::lock_guard lock(nodeMutex_); auto iter = relationshipMap_.find(uriStr); - shared_ptr obsNode; + std::shared_ptr obsNode; if (iter != relationshipMap_.end()) { obsNode = iter->second; // this node has this callback or not, if has this, unref manager. @@ -153,7 +156,7 @@ int32_t FileAccessService::RegisterNotify(Uri uri, const sptr(notifyForDescendants); + obsNode = std::make_shared(notifyForDescendants); // add new node relations. for (auto &[comUri, node] : relationshipMap_) { if (IsParentUri(comUri, uriStr)) { @@ -172,7 +175,7 @@ int32_t FileAccessService::RegisterNotify(Uri uri, const sptr obsNode) +void FileAccessService::RemoveRelations(std::string &uriStr, std::shared_ptr obsNode) { std::lock_guard lock(nodeMutex_); for (auto &[comUri, node] : relationshipMap_) { @@ -188,7 +191,7 @@ void FileAccessService::RemoveRelations(string &uriStr, shared_ptr relationshipMap_.erase(uriStr); } -int FileAccessService::FindUri(const string &uriStr, shared_ptr &outObsNode) +int FileAccessService::FindUri(const std::string &uriStr, std::shared_ptr &outObsNode) { std::lock_guard lock(nodeMutex_); auto iter = relationshipMap_.find(uriStr); @@ -208,16 +211,16 @@ int32_t FileAccessService::UnregisterNotify(Uri uri, const sptr obsNode; + std::string uriStr = uri.ToString(); + std::shared_ptr obsNode; if (FindUri(uriStr, obsNode) != ERR_OK) { HILOG_ERROR("Can not find unregisterNotify uri"); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return E_CAN_NOT_FIND_URI; } // find if obsManager_ has this callback. - shared_ptr obsContext = make_shared(observer); - uint32_t code = obsManager_.getId([obsContext](const shared_ptr &afterContext) { + std::shared_ptr obsContext = std::make_shared(observer); + uint32_t code = obsManager_.getId([obsContext](const std::shared_ptr &afterContext) { return obsContext->EqualTo(afterContext); }); if (code == HolderManager>::CODE_CAN_NOT_FIND) { @@ -250,7 +253,7 @@ int32_t FileAccessService::UnregisterNotify(Uri uri, const sptr list, NotifyMessage ¬ifyMessage) +void FileAccessService::SendListNotify(const std::vector list, NotifyMessage ¬ifyMessage) { for (uint32_t code : list) { if (obsManager_.get(code) == nullptr) { @@ -264,8 +267,8 @@ void FileAccessService::SendListNotify(const vector list, NotifyMessag int32_t FileAccessService::OnChange(Uri uri, NotifyType notifyType) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "OnChange"); - string uriStr = uri.ToString(); - shared_ptr node; + std::string uriStr = uri.ToString(); + std::shared_ptr node; if (FindUri(uriStr, node) != ERR_OK) { FinishTrace(HITRACE_TAG_FILEMANAGEMENT); HILOG_ERROR("Can not find onChange uri"); diff --git a/services/native/file_access_service/src/file_access_service_proxy.cpp b/services/native/file_access_service/src/file_access_service_proxy.cpp index 1f9b976d..08a30f52 100644 --- a/services/native/file_access_service/src/file_access_service_proxy.cpp +++ b/services/native/file_access_service/src/file_access_service_proxy.cpp @@ -25,7 +25,7 @@ int32_t FileAccessServiceProxy::OnChange(Uri uri, NotifyType notifyType) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "OnChange"); MessageParcel data; - if (!data.WriteInterfaceToken(FileAccessObserverProxy::GetDescriptor())) { + if (!data.WriteInterfaceToken(FileAccessServiceProxy::GetDescriptor())) { HILOG_ERROR("WriteInterfaceToken failed"); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); return E_IPCS; @@ -63,8 +63,8 @@ int32_t FileAccessServiceProxy::OnChange(Uri uri, NotifyType notifyType) return ERR_OK; } -int32_t FileAccessServiceProxy::RegisterNotify(Uri uri, const sptr &observer, - bool notifyForDescendants) +int32_t FileAccessServiceProxy::RegisterNotify(Uri uri, bool notifyForDescendants, + const sptr &observer) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "RegisterNotify"); MessageParcel data; diff --git a/services/native/file_access_service/src/file_access_service_stub.cpp b/services/native/file_access_service/src/file_access_service_stub.cpp index 5afcde70..3d39f887 100644 --- a/services/native/file_access_service/src/file_access_service_stub.cpp +++ b/services/native/file_access_service/src/file_access_service_stub.cpp @@ -50,11 +50,6 @@ int32_t FileAccessServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &dat MessageOption &option) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "OnRemoteRequest"); - if (!CheckCallingPermission(FILE_ACCESS_PERMISSION)) { - HILOG_ERROR("permission error"); - FinishTrace(HITRACE_TAG_FILEMANAGEMENT); - return E_PERMISSION; - } std::u16string descriptor = FileAccessServiceStub::GetDescriptor(); std::u16string remoteDescriptor = data.ReadInterfaceToken(); @@ -98,6 +93,11 @@ ErrCode FileAccessServiceStub::CmdOnChange(MessageParcel &data, MessageParcel &r ErrCode FileAccessServiceStub::CmdRegisterNotify(MessageParcel &data, MessageParcel &reply) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "CmdRegisterNotify"); + if (!CheckCallingPermission(FILE_ACCESS_PERMISSION)) { + HILOG_ERROR("permission error"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_PERMISSION; + } std::shared_ptr uri(data.ReadParcelable()); if (uri == nullptr) { HILOG_ERROR("RegisterNotify uri is nullptr"); @@ -120,7 +120,7 @@ ErrCode FileAccessServiceStub::CmdRegisterNotify(MessageParcel &data, MessagePar } bool notifyForDescendants = data.ReadBool(); - int ret = RegisterNotify(*uri, observer, notifyForDescendants); + int ret = RegisterNotify(*uri, notifyForDescendants, observer); if (!reply.WriteInt32(ret)) { HILOG_ERROR("Parameter RegisterNotify fail to WriteInt32 ret"); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); @@ -133,6 +133,11 @@ ErrCode FileAccessServiceStub::CmdRegisterNotify(MessageParcel &data, MessagePar ErrCode FileAccessServiceStub::CmdUnregisterNotify(MessageParcel &data, MessageParcel &reply) { StartTrace(HITRACE_TAG_FILEMANAGEMENT, "CmdUnregisterNotify"); + if (!CheckCallingPermission(FILE_ACCESS_PERMISSION)) { + HILOG_ERROR("permission error"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_PERMISSION; + } std::shared_ptr uri(data.ReadParcelable()); if (uri == nullptr) { HILOG_ERROR("UnregisterNotify uri is nullptr"); diff --git a/services/native/file_access_service/src/observer_callback_proxy.cpp b/services/native/file_access_service/src/observer_callback_proxy.cpp new file mode 100644 index 00000000..8d5678dd --- /dev/null +++ b/services/native/file_access_service/src/observer_callback_proxy.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2023 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 "observer_callback_proxy.h" + +#include "file_access_framework_errno.h" +#include "hilog_wrapper.h" +#include "hitrace_meter.h" +#include "ipc_types.h" +#include "message_option.h" +#include "message_parcel.h" + +namespace OHOS { +namespace FileAccessFwk { +void ObserverCallbackProxy::OnChange(NotifyMessage ¬ifyMessage) +{ + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "OnChange"); + MessageParcel data; + if (!data.WriteInterfaceToken(ObserverCallbackProxy::GetDescriptor())) { + HILOG_ERROR("WriteInterfaceToken failed"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return; + } + + if (!data.WriteParcelable(¬ifyMessage)) { + HILOG_ERROR("fail to WriteParcelable notifyMessage"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return; + } + + MessageParcel reply; + MessageOption option; + int err = Remote()->SendRequest(CMD_CALLBACK, data, reply, option); + if (err != ERR_OK) { + HILOG_ERROR("fail to SendRequest. err: %{public}d", err); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return; + } + + int ret = E_IPCS; + if (!reply.ReadInt32(ret)) { + HILOG_ERROR("fail to ReadInt32 ret"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return; + } + + if (ret != ERR_OK) { + HILOG_ERROR("OnChange operation failed ret : %{public}d", ret); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return; + } + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return; +} +} // namespace FileAccessFwk +} // namespace OHOS diff --git a/services/native/file_access_service/src/observer_callback_stub.cpp b/services/native/file_access_service/src/observer_callback_stub.cpp new file mode 100644 index 00000000..e2f6bda9 --- /dev/null +++ b/services/native/file_access_service/src/observer_callback_stub.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2023 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 "observer_callback_stub.h" + +#include +#include + +#include "accesstoken_kit.h" +#include "file_access_framework_errno.h" +#include "hilog_wrapper.h" +#include "hitrace_meter.h" +#include "ipc_object_stub.h" +#include "ipc_skeleton.h" +#include "ipc_types.h" +#include "message_parcel.h" + +namespace OHOS { +namespace FileAccessFwk { +ObserverCallbackStub::~ObserverCallbackStub() +{ + stubFuncMap_.clear(); +} + +void ObserverCallbackStub::InitStubFuncMap() +{ + if (stubFuncMap_.size() == 0) { + stubFuncMap_[CMD_CALLBACK] = &ObserverCallbackStub::OnChangeStub; + } +} + +int32_t ObserverCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, + MessageOption& option) +{ + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "OnRemoteRequest"); + InitStubFuncMap(); + std::u16string descriptor = ObserverCallbackStub::GetDescriptor(); + std::u16string remoteDescriptor = data.ReadInterfaceToken(); + if (descriptor != remoteDescriptor) { + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ERR_INVALID_STATE; + } + + const auto &itFunc = stubFuncMap_.find(code); + if (itFunc != stubFuncMap_.end()) { + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return (this->*(itFunc->second))(data, reply); + } + + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); +} + +int32_t ObserverCallbackStub::OnChangeStub(MessageParcel &data, MessageParcel &reply) +{ + StartTrace(HITRACE_TAG_FILEMANAGEMENT, "OnChangeStub"); + std::shared_ptr notifyMessage(data.ReadParcelable()); + if (notifyMessage == nullptr) { + HILOG_ERROR("OnChange uri is nullptr"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_URIS; + } + + OnChange(*notifyMessage); + int ret = 0; + if (!reply.WriteInt32(ret)) { + HILOG_ERROR("Parameter OnChangeStub fail to WriteInt32 ret"); + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return E_IPCS; + } + FinishTrace(HITRACE_TAG_FILEMANAGEMENT); + return ERR_OK; +} +} +} \ No newline at end of file diff --git a/utils/file_access_framework_errno.h b/utils/file_access_framework_errno.h index 3ee98347..c78189b7 100644 --- a/utils/file_access_framework_errno.h +++ b/utils/file_access_framework_errno.h @@ -41,7 +41,8 @@ enum { E_CAN_NOT_FIND_URI, // Can not find registered uri E_PERMISSION = 201, // Permission verification failed E_PERMISSION_SYS, // is not system app - E_COUNT + E_COUNT, + E_LOAD_SA // load SA failed }; } // namespace FileAccessFwk } // namespace OHOS -- Gitee