From 6f0936da0b66dae1746471dbd4aef183acc6b50a Mon Sep 17 00:00:00 2001 From: linjun9528 Date: Wed, 15 Jun 2022 15:06:13 +0800 Subject: [PATCH 1/2] add file access framework Signed-off-by: linjun9528 --- bundle.json | 4 +- frameworks/innerkits/BUILD.gn | 21 +++ frameworks/innerkits/file_access/BUILD.gn | 111 +++++++++++++ .../file_access/include/file_ext_ability.h | 46 ++++++ .../include/file_ext_ability_module_loader.h | 32 ++++ .../file_access/include/js_file_ext_ability.h | 54 ++++++ .../file_access/src/file_ext_ability.cpp | 67 ++++++++ .../src/file_ext_ability_module_loader.cpp | 41 +++++ .../file_access/src/js_file_ext_ability.cpp | 155 ++++++++++++++++++ .../kits/napi/file_ext_ability/BUILD.gn | 50 ++++++ .../napi/file_ext_ability/file_ext_ability.js | 19 +++ .../file_ext_ability_module.cpp | 57 +++++++ utils/hilog_wrapper.h | 83 ++++++++++ 13 files changed, 739 insertions(+), 1 deletion(-) create mode 100644 frameworks/innerkits/BUILD.gn create mode 100644 frameworks/innerkits/file_access/BUILD.gn create mode 100644 frameworks/innerkits/file_access/include/file_ext_ability.h create mode 100644 frameworks/innerkits/file_access/include/file_ext_ability_module_loader.h create mode 100644 frameworks/innerkits/file_access/include/js_file_ext_ability.h create mode 100644 frameworks/innerkits/file_access/src/file_ext_ability.cpp create mode 100644 frameworks/innerkits/file_access/src/file_ext_ability_module_loader.cpp create mode 100644 frameworks/innerkits/file_access/src/js_file_ext_ability.cpp create mode 100644 interfaces/kits/napi/file_ext_ability/BUILD.gn create mode 100644 interfaces/kits/napi/file_ext_ability/file_ext_ability.js create mode 100644 interfaces/kits/napi/file_ext_ability/file_ext_ability_module.cpp create mode 100644 utils/hilog_wrapper.h diff --git a/bundle.json b/bundle.json index 1b615582..c8dc5f1a 100644 --- a/bundle.json +++ b/bundle.json @@ -36,7 +36,9 @@ "sub_component": [ "//foundation/filemanagement/user_file_service/services:fms", "//foundation/filemanagement/user_file_service/services/sa_profile:filemanager_service_sa_profile", - "//foundation/filemanagement/user_file_service/interfaces/kits/js:filemanager" + "//foundation/filemanagement/user_file_service/interfaces/kits/js:filemanager", + "//foundation/filemanagement/user_file_service/frameworks/innerkits:frameworks_innerkits", + "//foundation/filemanagement/user_file_service/interfaces/kits/napi/file_ext_ability:fileextensionability_napi" ], "test": [ "//foundation/filemanagement/user_file_service/services/test:user_file_manager_test" diff --git a/frameworks/innerkits/BUILD.gn b/frameworks/innerkits/BUILD.gn new file mode 100644 index 00000000..f7f183ec --- /dev/null +++ b/frameworks/innerkits/BUILD.gn @@ -0,0 +1,21 @@ +# 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. + +import("//build/ohos.gni") + +group("frameworks_innerkits") { + deps = [ + "file_access:file_access_ability_kit", + "file_access:file_access_ability_module", + ] +} \ No newline at end of file diff --git a/frameworks/innerkits/file_access/BUILD.gn b/frameworks/innerkits/file_access/BUILD.gn new file mode 100644 index 00000000..be069752 --- /dev/null +++ b/frameworks/innerkits/file_access/BUILD.gn @@ -0,0 +1,111 @@ +# 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. + +import("//build/ohos.gni") +import("//foundation/filemanagement/user_file_service/filemanagement_aafwk.gni") + +BASE_DIR = "//foundation/filemanagement/user_file_service" + +config("ability_config") { + visibility = [ ":*" ] + include_dirs = [ + "include", + "${BASE_DIR}/utils", + "${aafwk_services_path}/common/include", + "${aafwk_kits_path}/ability/native/include/continuation/distributed", + "${aafwk_kits_path}/ability/native/include/continuation/kits", + "${aafwk_kits_path}/appkit/native/app/include", + "${aafwk_path}/frameworks/js/napi/aafwk/inner/napi_common", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base/include", + ] + + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } +} + +config("ability_public_config") { + visibility = [ ":*" ] + include_dirs = [ + "include", + "${BASE_DIR}/utils", + "${aafwk_kits_path}/appkit/native/ability_runtime/app", + "${aafwk_kits_path}/appkit/native/app/include", + "${aafwk_kits_path}/appkit/native/ability_runtime/context", + ] +} + +ohos_shared_library("file_access_ability_kit") { + include_dirs = [] + + sources = [ + "src/file_ext_ability.cpp", + "src/js_file_ext_ability.cpp", + ] + configs = [ ":ability_config" ] + public_configs = [ + ":ability_public_config", + "${aafwk_kits_path}/ability/ability_runtime:ability_context_public_config", + ] + + deps = [ + "${aafwk_kits_path}/ability/native:abilitykit_native", + "${aafwk_kits_path}/appkit:app_context", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:ability_context_native", + "ability_runtime:ability_manager", + "ability_runtime:app_manager", + "ability_runtime:runtime", + "ability_runtime:wantagent_innerkits", + "access_token:libaccesstoken_sdk", + "ipc:ipc_core", + "ipc_js:rpc", + "permission_standard:libpermissionsdk_standard", + "utils_base:utils", + ] + + public_deps = [ + "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog", + "//foundation/arkui/napi:ace_napi", + ] + + subsystem_name = "filemanagement" + part_name = "user_file_service" +} + +ohos_shared_library("file_access_ability_module") { + sources = [ "src/file_ext_ability_module_loader.cpp" ] + + configs = [ ":ability_config" ] + public_configs = [ ":ability_public_config" ] + + deps = [ + ":file_access_ability_kit", + "${aafwk_kits_path}/ability/native:abilitykit_native", + ] + + external_deps = [ + "ability_base:want", + "ability_runtime:runtime", + "hiviewdfx_hilog_native:libhilog", + "utils_base:utils", + ] + + subsystem_name = "filemanagement" + part_name = "user_file_service" +} diff --git a/frameworks/innerkits/file_access/include/file_ext_ability.h b/frameworks/innerkits/file_access/include/file_ext_ability.h new file mode 100644 index 00000000..b099e5b5 --- /dev/null +++ b/frameworks/innerkits/file_access/include/file_ext_ability.h @@ -0,0 +1,46 @@ +/* + * 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_EXT_ABILITY_H +#define FILE_EXT_ABILITY_H + +#include "extension_base.h" + +namespace OHOS { +namespace AbilityRuntime { +class Runtime; +} +namespace FileAccessFwk { +using namespace AbilityRuntime; +class FileExtAbility; +using CreatorFunc = std::function& runtime)>; +class FileExtAbility : public ExtensionBase<> { +public: + FileExtAbility() = default; + virtual ~FileExtAbility() = default; + + virtual void Init(const std::shared_ptr &record, + const std::shared_ptr &application, + std::shared_ptr &handler, + const sptr &token) override; + + static FileExtAbility* Create(const std::unique_ptr& runtime); + static void SetCreator(const CreatorFunc& creator); +private: + static CreatorFunc creator_; +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // FILE_EXT_ABILITY_H \ No newline at end of file diff --git a/frameworks/innerkits/file_access/include/file_ext_ability_module_loader.h b/frameworks/innerkits/file_access/include/file_ext_ability_module_loader.h new file mode 100644 index 00000000..aa6f570c --- /dev/null +++ b/frameworks/innerkits/file_access/include/file_ext_ability_module_loader.h @@ -0,0 +1,32 @@ +/* + * 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_EXT_ABILITY_MODULE_LOADER_H +#define FILE_EXT_ABILITY_MODULE_LOADER_H + +#include "extension_module_loader.h" +namespace OHOS { +namespace FileAccessFwk { +using namespace AbilityRuntime; +class FileExtAbilityModuleLoader : public ExtensionModuleLoader, + public Singleton { + DECLARE_SINGLETON(FileExtAbilityModuleLoader); + +public: + virtual Extension *Create(const std::unique_ptr& runtime) const override; +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // FILE_EXT_ABILITY_MODULE_LOADER_H \ No newline at end of file diff --git a/frameworks/innerkits/file_access/include/js_file_ext_ability.h b/frameworks/innerkits/file_access/include/js_file_ext_ability.h new file mode 100644 index 00000000..d26ed381 --- /dev/null +++ b/frameworks/innerkits/file_access/include/js_file_ext_ability.h @@ -0,0 +1,54 @@ +/* + * 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 JS_FILE_EXT_ABILITY_H +#define JS_FILE_EXT_ABILITY_H + +#include "file_ext_ability.h" + +#include "js_runtime.h" +#include "native_engine/native_reference.h" +#include "native_engine/native_value.h" + +namespace OHOS { +namespace FileAccessFwk { +using namespace AbilityRuntime; +class JsFileExtAbility : public FileExtAbility { +public: + JsFileExtAbility(JsRuntime& jsRuntime); + virtual ~JsFileExtAbility() override; + + static JsFileExtAbility* Create(const std::unique_ptr& runtime); + + void Init(const std::shared_ptr &record, + const std::shared_ptr &application, + std::shared_ptr &handler, + const sptr &token) override; + + + void OnStart(const AAFwk::Want &want) override; + + sptr OnConnect(const AAFwk::Want &want) override; + +private: + NativeValue* CallObjectMethod(const char* name, NativeValue* const* argv = nullptr, size_t argc = 0); + void GetSrcPath(std::string &srcPath); + + JsRuntime& jsRuntime_; + std::unique_ptr jsObj_; +}; +} // namespace FileAccessFwk +} // namespace OHOS +#endif // JS_FILE_EXT_ABILITY_H \ No newline at end of file diff --git a/frameworks/innerkits/file_access/src/file_ext_ability.cpp b/frameworks/innerkits/file_access/src/file_ext_ability.cpp new file mode 100644 index 00000000..8c60f608 --- /dev/null +++ b/frameworks/innerkits/file_access/src/file_ext_ability.cpp @@ -0,0 +1,67 @@ +/* + * 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_ext_ability.h" + +#include "ability_loader.h" +#include "connection_manager.h" +#include "extension_context.h" +#include "hilog_wrapper.h" +#include "js_file_ext_ability.h" +#include "runtime.h" + +namespace OHOS { +namespace FileAccessFwk { +using namespace OHOS::AppExecFwk; + +CreatorFunc FileExtAbility::creator_ = nullptr; +void FileExtAbility::SetCreator(const CreatorFunc& creator) +{ + creator_ = creator; +} + +FileExtAbility* FileExtAbility::Create(const std::unique_ptr& runtime) +{ + HILOG_INFO("tag dsa %{public}s begin.", __func__); + if (!runtime) { + return new FileExtAbility(); + } + if (creator_) { + return creator_(runtime); + } + HILOG_INFO("tag dsa FileExtAbility::Create runtime"); + switch (runtime->GetLanguage()) { + case Runtime::Language::JS: + HILOG_INFO("tag dsa Runtime::Language::JS --> JsFileExtAbility"); + return JsFileExtAbility::Create(runtime); + + default: + HILOG_INFO("tag dsa default --> FileExtAbility"); + return new FileExtAbility(); + } + HILOG_INFO("tag dsa %{public}s begin.", __func__); +} + +void FileExtAbility::Init(const std::shared_ptr &record, + const std::shared_ptr &application, + std::shared_ptr &handler, + const sptr &token) +{ + HILOG_INFO("tag dsa %{public}s begin.", __func__); + ExtensionBase<>::Init(record, application, handler, token); + HILOG_INFO("tag dsa %{public}s end.", __func__); +} +} // namespace FileAccessFwk +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/innerkits/file_access/src/file_ext_ability_module_loader.cpp b/frameworks/innerkits/file_access/src/file_ext_ability_module_loader.cpp new file mode 100644 index 00000000..ee0867dc --- /dev/null +++ b/frameworks/innerkits/file_access/src/file_ext_ability_module_loader.cpp @@ -0,0 +1,41 @@ +/* + * 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_ext_ability_module_loader.h" +#include "file_ext_ability.h" + +namespace OHOS{ +namespace FileAccessFwk{ +FileExtAbilityModuleLoader::FileExtAbilityModuleLoader() = default; +FileExtAbilityModuleLoader::~FileExtAbilityModuleLoader() = default; + +Extension *FileExtAbilityModuleLoader::Create(const std::unique_ptr& runtime) const +{ + HILOG_INFO("tag dsa %{public}s begin.", __func__); + return FileExtAbility::Create(runtime); +} + +extern "C" __attribute__((visibility("default"))) void* OHOS_EXTENSION_GetExtensionModule() +{ + HILOG_INFO("tag dsa %{public}s begin.", __func__); + return &FileExtAbilityModuleLoader::GetInstance(); +} + +extern "C" __attribute__((visibility("default"))) void SetCreator(const CreatorFunc& creator) +{ + return FileExtAbility::SetCreator(creator); +} +} // namespace FileAccessFwk +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/innerkits/file_access/src/js_file_ext_ability.cpp b/frameworks/innerkits/file_access/src/js_file_ext_ability.cpp new file mode 100644 index 00000000..691ee284 --- /dev/null +++ b/frameworks/innerkits/file_access/src/js_file_ext_ability.cpp @@ -0,0 +1,155 @@ +/* + * 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 "js_file_ext_ability.h" + +#include "extension_context.h" +#include "ability_info.h" +#include "accesstoken_kit.h" +#include "hilog_wrapper.h" +#include "ipc_skeleton.h" +#include "js_runtime.h" +#include "js_runtime_utils.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "napi_common_util.h" +#include "napi_common_want.h" +#include "napi_remote_object.h" + +namespace OHOS { +namespace FileAccessFwk { +namespace { +constexpr size_t ARGC_ONE = 1; +} + +using namespace OHOS::AppExecFwk; +using namespace OHOS::AbilityRuntime; +using OHOS::Security::AccessToken::AccessTokenKit; + +JsFileExtAbility* JsFileExtAbility::Create(const std::unique_ptr& runtime) +{ + HILOG_INFO("tag dsa %{public}s begin.", __func__); + return new JsFileExtAbility(static_cast(*runtime)); +} + +JsFileExtAbility::JsFileExtAbility(JsRuntime& jsRuntime) : jsRuntime_(jsRuntime) {} +JsFileExtAbility::~JsFileExtAbility() = default; + +void JsFileExtAbility::Init(const std::shared_ptr &record, + const std::shared_ptr &application, std::shared_ptr &handler, + const sptr &token) +{ + HILOG_INFO("tag dsa %{public}s begin.", __func__); + FileExtAbility::Init(record, application, handler, token); + std::string srcPath = ""; + GetSrcPath(srcPath); + if (srcPath.empty()) { + HILOG_ERROR("tag dsa Failed to get srcPath"); + return; + } + + std::string moduleName(Extension::abilityInfo_->moduleName); + moduleName.append("::").append(abilityInfo_->name); + HILOG_INFO("tag dsa %{public}s module:%{public}s, srcPath:%{public}s.", __func__, moduleName.c_str(), srcPath.c_str()); + HandleScope handleScope(jsRuntime_); + + jsObj_ = jsRuntime_.LoadModule(moduleName, srcPath); + if (jsObj_ == nullptr) { + HILOG_ERROR("tag dsa Failed to get jsObj_"); + return; + } + HILOG_INFO("tag dsa JsFileExtAbility::Init ConvertNativeValueTo."); + NativeObject* obj = ConvertNativeValueTo(jsObj_->Get()); + if (obj == nullptr) { + HILOG_ERROR("tag dsa Failed to get JsFileExtAbility object"); + return; + } + HILOG_INFO("tag dsa JsFileExtAbility::Init end."); +} + +void JsFileExtAbility::OnStart(const AAFwk::Want &want) +{ + HILOG_INFO("tag dsa %{public}s begin.", __func__); + Extension::OnStart(want); + HandleScope handleScope(jsRuntime_); + napi_env env = reinterpret_cast(&jsRuntime_.GetNativeEngine()); + napi_value napiWant = OHOS::AppExecFwk::WrapWant(env, want); + NativeValue* nativeWant = reinterpret_cast(napiWant); + NativeValue* argv[] = {nativeWant}; + CallObjectMethod("onCreate", argv, ARGC_ONE); + HILOG_INFO("tag dsa %{public}s end.", __func__); +} + +sptr JsFileExtAbility::OnConnect(const AAFwk::Want &want) +{ + HILOG_INFO("tag dsa %{public}s begin.", __func__); + Extension::OnConnect(want); + HILOG_INFO("tag dsa %{public}s end. ", __func__); + return nullptr; +} + +NativeValue* JsFileExtAbility::CallObjectMethod(const char* name, NativeValue* const* argv, size_t argc) +{ + HILOG_INFO("tag dsa JsFileExtAbility::CallObjectMethod(%{public}s), begin", name); + + if (!jsObj_) { + HILOG_WARN("Not found FileExtAbility.js"); + return nullptr; + } + + HandleScope handleScope(jsRuntime_); + auto& nativeEngine = jsRuntime_.GetNativeEngine(); + + NativeValue* value = jsObj_->Get(); + NativeObject* obj = ConvertNativeValueTo(value); + if (obj == nullptr) { + HILOG_ERROR("tag dsa Failed to get FileExtAbility object"); + return nullptr; + } + + NativeValue* method = obj->GetProperty(name); + if (method == nullptr) { + HILOG_ERROR("tag dsa Failed to get '%{public}s' from FileExtAbility object", name); + return nullptr; + } + HILOG_INFO("tag dsa JsFileExtAbility::CallFunction(%{public}s), success", name); + return handleScope.Escape(nativeEngine.CallFunction(value, method, argv, argc)); +} + +void JsFileExtAbility::GetSrcPath(std::string &srcPath) +{ + HILOG_INFO("tag dsa %{public}s begin.", __func__); + if (!Extension::abilityInfo_->isStageBasedModel) { + /* temporary compatibility api8 + config.json */ + srcPath.append(Extension::abilityInfo_->package); + srcPath.append("/assets/js/"); + if (!Extension::abilityInfo_->srcPath.empty()) { + srcPath.append(Extension::abilityInfo_->srcPath); + } + srcPath.append("/").append(Extension::abilityInfo_->name).append(".abc"); + HILOG_INFO("tag dsa %{public}s end1, srcPath:%{public}s", __func__, srcPath.c_str()); + return; + } + + if (!Extension::abilityInfo_->srcEntrance.empty()) { + srcPath.append(Extension::abilityInfo_->moduleName + "/"); + srcPath.append(Extension::abilityInfo_->srcEntrance); + srcPath.erase(srcPath.rfind('.')); + srcPath.append(".abc"); + } + HILOG_INFO("tag dsa %{public}s end2, srcPath:%{public}s", __func__, srcPath.c_str()); +} +} // namespace FileAccessFwk +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/file_ext_ability/BUILD.gn b/interfaces/kits/napi/file_ext_ability/BUILD.gn new file mode 100644 index 00000000..b0e794e8 --- /dev/null +++ b/interfaces/kits/napi/file_ext_ability/BUILD.gn @@ -0,0 +1,50 @@ +# 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. + +import("//ark/ts2abc/ts2panda/ts2abc_config.gni") +import("//build/ohos.gni") + +ts2abc_gen_abc("gen_file_ext_ability_abc") { + src_js = rebase_path("file_ext_ability.js") + dst_file = rebase_path(target_out_dir + "/file_ext_ability.abc") + in_puts = [ "file_ext_ability.js" ] + out_puts = [ target_out_dir + "/file_ext_ability.abc" ] + extra_args = [ "--module" ] +} + +gen_js_obj("file_ext_ability_js") { + input = "file_ext_ability.js" + output = target_out_dir + "/file_ext_ability.o" +} + +gen_js_obj("file_ext_ability_abc") { + input = get_label_info(":gen_file_ext_ability_abc", "target_out_dir") + + "/file_ext_ability.abc" + output = target_out_dir + "/file_ext_ability_abc.o" + dep = ":gen_file_ext_ability_abc" +} + +ohos_shared_library("fileextensionability_napi") { + sources = [ "file_ext_ability_module.cpp" ] + + deps = [ + ":file_ext_ability_abc", + ":file_ext_ability_js", + ] + + external_deps = [ "napi:ace_napi" ] + + relative_install_dir = "module/application" + subsystem_name = "filemanagement" + part_name = "user_file_service" +} diff --git a/interfaces/kits/napi/file_ext_ability/file_ext_ability.js b/interfaces/kits/napi/file_ext_ability/file_ext_ability.js new file mode 100644 index 00000000..f59b1bd4 --- /dev/null +++ b/interfaces/kits/napi/file_ext_ability/file_ext_ability.js @@ -0,0 +1,19 @@ +/* + * 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. + */ + +class FileExtensionAbility { +} + +export default FileExtensionAbility \ No newline at end of file diff --git a/interfaces/kits/napi/file_ext_ability/file_ext_ability_module.cpp b/interfaces/kits/napi/file_ext_ability/file_ext_ability_module.cpp new file mode 100644 index 00000000..6dca96b5 --- /dev/null +++ b/interfaces/kits/napi/file_ext_ability/file_ext_ability_module.cpp @@ -0,0 +1,57 @@ +/* + * 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 "native_engine/native_engine.h" + +extern const char _binary_file_ext_ability_js_start[]; +extern const char _binary_file_ext_ability_js_end[]; +extern const char _binary_file_ext_ability_abc_start[]; +extern const char _binary_file_ext_ability_abc_end[]; + +extern "C" __attribute__((constructor)) +void NAPI_application_FileExtensionAbility_AutoRegister() +{ + auto moduleManager = NativeModuleManager::GetInstance(); + NativeModule newModuleInfo = { + .name = "application.FileExtensionAbility", + .fileName = "application/libfileextensionability_napi.so/FileExtensionAbility.js", + }; + + moduleManager->Register(&newModuleInfo); +} + +extern "C" __attribute__((visibility("default"))) +void NAPI_application_FileExtensionAbility_GetJSCode(const char **buf, int *bufLen) +{ + if (buf != nullptr) { + *buf = _binary_file_ext_ability_js_start; + } + + if (bufLen != nullptr) { + *bufLen = _binary_file_ext_ability_js_end - _binary_file_ext_ability_js_start; + } +} + +// file extension ability JS register +extern "C" __attribute__((visibility("default"))) +void NAPI_application_FileExtensionAbility_GetABCCode(const char **buf, int *buflen) +{ + if (buf != nullptr) { + *buf = _binary_file_ext_ability_abc_start; + } + if (buflen != nullptr) { + *buflen = _binary_file_ext_ability_abc_end - _binary_file_ext_ability_abc_start; + } +} \ No newline at end of file diff --git a/utils/hilog_wrapper.h b/utils/hilog_wrapper.h new file mode 100644 index 00000000..1fde8457 --- /dev/null +++ b/utils/hilog_wrapper.h @@ -0,0 +1,83 @@ +/* + * 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 HILOG_WRAPPER_H +#define HILOG_WRAPPER_H + +#define CONFIG_HILOG +#ifdef CONFIG_HILOG +#include "hilog/log.h" + +#ifdef HILOG_FATAL +#undef HILOG_FATAL +#endif + +#ifdef HILOG_ERROR +#undef HILOG_ERROR +#endif + +#ifdef HILOG_WARN +#undef HILOG_WARN +#endif + +#ifdef HILOG_INFO +#undef HILOG_INFO +#endif + +#ifdef HILOG_DEBUG +#undef HILOG_DEBUG +#endif + +#ifndef FAF_LOG_DOMAIN +#define FAF_LOG_DOMAIN 0xD00430A +#endif + +#ifndef FAF_LOG_TAG +#define FAF_LOG_TAG "FileAccessFwk" +#endif + +#ifdef LOG_LABEL +#undef LOG_LABEL +#endif + +static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = {LOG_CORE, FAF_LOG_DOMAIN, FAF_LOG_TAG}; + +#define __FILENAME__ (__builtin_strrchr(__FILE__, '/') ? __builtin_strrchr(__FILE__, '/') + 1 : __FILE__) + +#define HILOG_FATAL(fmt, ...) \ + (void)OHOS::HiviewDFX::HiLog::Fatal( \ + LOG_LABEL, "[%{public}s(%{public}s:%{public}d)]" fmt, __FILENAME__, __FUNCTION__, __LINE__, ##__VA_ARGS__) +#define HILOG_ERROR(fmt, ...) \ + (void)OHOS::HiviewDFX::HiLog::Error( \ + LOG_LABEL, "[%{public}s(%{public}s:%{public}d)]" fmt, __FILENAME__, __FUNCTION__, __LINE__, ##__VA_ARGS__) +#define HILOG_WARN(fmt, ...) \ + (void)OHOS::HiviewDFX::HiLog::Warn( \ + LOG_LABEL, "[%{public}s(%{public}s:%{public}d)]" fmt, __FILENAME__, __FUNCTION__, __LINE__, ##__VA_ARGS__) +#define HILOG_INFO(fmt, ...) \ + (void)OHOS::HiviewDFX::HiLog::Info( \ + LOG_LABEL, "[%{public}s(%{public}s:%{public}d)]" fmt, __FILENAME__, __FUNCTION__, __LINE__, ##__VA_ARGS__) +#define HILOG_DEBUG(fmt, ...) \ + (void)OHOS::HiviewDFX::HiLog::Debug( \ + LOG_LABEL, "[%{public}s(%{public}s:%{public}d)]" fmt, __FILENAME__, __FUNCTION__, __LINE__, ##__VA_ARGS__) +#else + +#define HILOG_FATAL(...) +#define HILOG_ERROR(...) +#define HILOG_WARN(...) +#define HILOG_INFO(...) +#define HILOG_DEBUG(...) +#endif // CONFIG_HILOG + +#endif // HILOG_WRAPPER_H \ No newline at end of file -- Gitee From ad62f49b8b1e362101f81d67beb3d4ba5293417c Mon Sep 17 00:00:00 2001 From: linjun9528 Date: Wed, 15 Jun 2022 15:39:51 +0800 Subject: [PATCH 2/2] add file access framework Signed-off-by: linjun9528 --- .../file_access/include/js_file_ext_ability.h | 3 +-- .../src/file_ext_ability_module_loader.cpp | 4 ++-- .../innerkits/file_access/src/js_file_ext_ability.cpp | 3 ++- .../napi/file_ext_ability/file_ext_ability_module.cpp | 11 +++++------ 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/frameworks/innerkits/file_access/include/js_file_ext_ability.h b/frameworks/innerkits/file_access/include/js_file_ext_ability.h index d26ed381..662a7f08 100644 --- a/frameworks/innerkits/file_access/include/js_file_ext_ability.h +++ b/frameworks/innerkits/file_access/include/js_file_ext_ability.h @@ -37,13 +37,12 @@ public: std::shared_ptr &handler, const sptr &token) override; - void OnStart(const AAFwk::Want &want) override; sptr OnConnect(const AAFwk::Want &want) override; private: - NativeValue* CallObjectMethod(const char* name, NativeValue* const* argv = nullptr, size_t argc = 0); + NativeValue* CallObjectMethod(const char *name, NativeValue * const *argv = nullptr, size_t argc = 0); void GetSrcPath(std::string &srcPath); JsRuntime& jsRuntime_; diff --git a/frameworks/innerkits/file_access/src/file_ext_ability_module_loader.cpp b/frameworks/innerkits/file_access/src/file_ext_ability_module_loader.cpp index ee0867dc..ab1f2726 100644 --- a/frameworks/innerkits/file_access/src/file_ext_ability_module_loader.cpp +++ b/frameworks/innerkits/file_access/src/file_ext_ability_module_loader.cpp @@ -16,8 +16,8 @@ #include "file_ext_ability_module_loader.h" #include "file_ext_ability.h" -namespace OHOS{ -namespace FileAccessFwk{ +namespace OHOS { +namespace FileAccessFwk { FileExtAbilityModuleLoader::FileExtAbilityModuleLoader() = default; FileExtAbilityModuleLoader::~FileExtAbilityModuleLoader() = default; diff --git a/frameworks/innerkits/file_access/src/js_file_ext_ability.cpp b/frameworks/innerkits/file_access/src/js_file_ext_ability.cpp index 691ee284..9a3efd55 100644 --- a/frameworks/innerkits/file_access/src/js_file_ext_ability.cpp +++ b/frameworks/innerkits/file_access/src/js_file_ext_ability.cpp @@ -62,7 +62,8 @@ void JsFileExtAbility::Init(const std::shared_ptr &record, std::string moduleName(Extension::abilityInfo_->moduleName); moduleName.append("::").append(abilityInfo_->name); - HILOG_INFO("tag dsa %{public}s module:%{public}s, srcPath:%{public}s.", __func__, moduleName.c_str(), srcPath.c_str()); + HILOG_INFO("tag dsa %{public}s module:%{public}s, srcPath:%{public}s.", + __func__, moduleName.c_str(), srcPath.c_str()); HandleScope handleScope(jsRuntime_); jsObj_ = jsRuntime_.LoadModule(moduleName, srcPath); diff --git a/interfaces/kits/napi/file_ext_ability/file_ext_ability_module.cpp b/interfaces/kits/napi/file_ext_ability/file_ext_ability_module.cpp index 6dca96b5..44c33497 100644 --- a/interfaces/kits/napi/file_ext_ability/file_ext_ability_module.cpp +++ b/interfaces/kits/napi/file_ext_ability/file_ext_ability_module.cpp @@ -20,8 +20,7 @@ extern const char _binary_file_ext_ability_js_end[]; extern const char _binary_file_ext_ability_abc_start[]; extern const char _binary_file_ext_ability_abc_end[]; -extern "C" __attribute__((constructor)) -void NAPI_application_FileExtensionAbility_AutoRegister() +extern "C" __attribute__((constructor)) void NAPI_application_FileExtensionAbility_AutoRegister() { auto moduleManager = NativeModuleManager::GetInstance(); NativeModule newModuleInfo = { @@ -32,8 +31,8 @@ void NAPI_application_FileExtensionAbility_AutoRegister() moduleManager->Register(&newModuleInfo); } -extern "C" __attribute__((visibility("default"))) -void NAPI_application_FileExtensionAbility_GetJSCode(const char **buf, int *bufLen) +extern "C" __attribute__((visibility("default"))) void NAPI_application_FileExtensionAbility_GetJSCode( + const char **buf, int *bufLen) { if (buf != nullptr) { *buf = _binary_file_ext_ability_js_start; @@ -45,8 +44,8 @@ void NAPI_application_FileExtensionAbility_GetJSCode(const char **buf, int *bufL } // file extension ability JS register -extern "C" __attribute__((visibility("default"))) -void NAPI_application_FileExtensionAbility_GetABCCode(const char **buf, int *buflen) +extern "C" __attribute__((visibility("default"))) void NAPI_application_FileExtensionAbility_GetABCCode( + const char **buf, int *buflen) { if (buf != nullptr) { *buf = _binary_file_ext_ability_abc_start; -- Gitee