From e93a5b97aea4963385d29276b2f86dbe6efcd828 Mon Sep 17 00:00:00 2001 From: zhangyafei-echo Date: Fri, 27 Oct 2023 16:38:51 +0800 Subject: [PATCH] Support insight intent. Signed-off-by: zhangyafei-echo Change-Id: I0bed1194857048d7a74324c9b178d651298cf8a0 Change-Id: I15b1c6406ea451c728d51d31b53db5505a9ccf5d --- .../js/napi/insight_intent_context/BUILD.gn | 50 +-- .../insight_intent_context.js | 27 ++ .../insight_intent_context_module.cpp | 94 ++++-- frameworks/native/ability/native/BUILD.gn | 4 + .../insight_intent_executor.cpp | 18 ++ .../insight_intent_executor_mgr.cpp | 111 +++++++ .../js_insight_intent_executor.cpp | 46 ++- .../native/insight_intent_host_client.cpp | 2 +- .../insight_intent_context/BUILD.gn | 24 +- .../js_insight_intent_context.cpp | 0 ...nsight_intent_execute_callback_interface.h | 2 +- .../insight_intent_execute_callback_proxy.h | 2 +- .../include/insight_intent_execute_param.h | 1 + .../include/insight_intent_profile.h | 63 ++++ .../include/insight_intent_utils.h | 33 ++ .../js_insight_intent_context.h | 0 .../insight_intent_executor.h | 16 +- .../insight_intent_executor_info.h | 2 +- .../insight_intent_executor_mgr.h | 49 +++ .../js_insight_intent_executor.h | 11 +- .../native/insight_intent_host_client.h | 2 +- services/abilitymgr/BUILD.gn | 1 + services/abilitymgr/abilitymgr.gni | 2 + .../include/insight_intent_execute_manager.h | 3 +- .../insight_intent_execute_callback_proxy.cpp | 2 +- .../src/insight_intent_execute_manager.cpp | 17 +- .../abilitymgr/src/insight_intent_profile.cpp | 303 ++++++++++++++++++ .../abilitymgr/src/insight_intent_utils.cpp | 92 ++++++ test/unittest/BUILD.gn | 3 + 29 files changed, 891 insertions(+), 89 deletions(-) create mode 100644 frameworks/js/napi/insight_intent_context/insight_intent_context.js create mode 100644 frameworks/native/ability/native/insight_intent_executor/insight_intent_executor_mgr.cpp rename frameworks/{js/napi => native/insight_intent}/insight_intent_context/js_insight_intent_context.cpp (100%) create mode 100644 interfaces/inner_api/ability_manager/include/insight_intent_profile.h create mode 100644 interfaces/inner_api/ability_manager/include/insight_intent_utils.h rename {frameworks/js/napi => interfaces/inner_api/insight_intent}/insight_intent_context/js_insight_intent_context.h (100%) create mode 100644 interfaces/kits/native/ability/native/insight_intent_executor/insight_intent_executor_mgr.h create mode 100644 services/abilitymgr/src/insight_intent_profile.cpp create mode 100644 services/abilitymgr/src/insight_intent_utils.cpp diff --git a/frameworks/js/napi/insight_intent_context/BUILD.gn b/frameworks/js/napi/insight_intent_context/BUILD.gn index 0387dfc3276..19113b57423 100644 --- a/frameworks/js/napi/insight_intent_context/BUILD.gn +++ b/frameworks/js/napi/insight_intent_context/BUILD.gn @@ -9,40 +9,40 @@ # 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. +# limitations under the License. +import("//arkcompiler/ets_frontend/es2panda/es2abc_config.gni") import("//build/ohos.gni") -import("//foundation/ability/ability_runtime/ability_runtime.gni") -ohos_shared_library("insightintentcontext_napi") { - include_dirs = [ - "./", - "${ability_runtime_innerkits_path}/insight_intent/insight_intent_context", - "${ability_runtime_path}/interfaces/kits/native/ability/native/ability_business_error", - ] +es2abc_gen_abc("gen_insight_intent_context_abc") { + src_js = rebase_path("insight_intent_context.js") + dst_file = rebase_path(target_out_dir + "/insight_intent_context.abc") + in_puts = [ "insight_intent_context.js" ] + out_puts = [ target_out_dir + "/insight_intent_context.abc" ] + extra_args = [ "--module" ] +} - sources = [ - "insight_intent_context_module.cpp", - "js_insight_intent_context.cpp", - ] +gen_js_obj("insight_intent_context_js") { + input = "insight_intent_context.js" + output = target_out_dir + "/insight_intent_context.o" +} - defines = [ "AMS_LOG_TAG = \"InsigtIntent\"" ] - defines += [ "AMS_LOG_DOMAIN = 0xD001308" ] +gen_js_obj("insight_intent_context_abc") { + input = get_label_info(":gen_insight_intent_context_abc", "target_out_dir") + + "/insight_intent_context.abc" + output = target_out_dir + "/insight_intent_context_abc.o" + dep = ":gen_insight_intent_context_abc" +} + +ohos_shared_library("insightintentcontext_napi") { + sources = [ "insight_intent_context_module.cpp" ] deps = [ - "${ability_runtime_napi_path}/inner/napi_common:napi_common", - "${ability_runtime_native_path}/ability/native:ability_business_error", - "${ability_runtime_native_path}/insight_intent/insight_intent_context:insightintentcontext", + ":insight_intent_context_abc", + ":insight_intent_context_js", ] - external_deps = [ - "ability_base:want", - "c_utils:utils", - "hilog:libhilog", - "hitrace:hitrace_meter", - "ipc:ipc_single", - "napi:ace_napi", - ] + external_deps = [ "napi:ace_napi" ] relative_install_dir = "module/app/ability" subsystem_name = "ability" diff --git a/frameworks/js/napi/insight_intent_context/insight_intent_context.js b/frameworks/js/napi/insight_intent_context/insight_intent_context.js new file mode 100644 index 00000000000..55a22ca49c1 --- /dev/null +++ b/frameworks/js/napi/insight_intent_context/insight_intent_context.js @@ -0,0 +1,27 @@ +/* + * 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. + */ + +class InsightIntentContext { + constructor(obj) { + this.__context_impl__ = obj; + } + + startAbility(want, callback) { + console.log('startAbility'); + return this.__context_impl__.startAbility(want, callback); + } +} + +export default InsightIntentContext; diff --git a/frameworks/js/napi/insight_intent_context/insight_intent_context_module.cpp b/frameworks/js/napi/insight_intent_context/insight_intent_context_module.cpp index aa64e92b547..b62565b3fe3 100644 --- a/frameworks/js/napi/insight_intent_context/insight_intent_context_module.cpp +++ b/frameworks/js/napi/insight_intent_context/insight_intent_context_module.cpp @@ -1,33 +1,61 @@ -/* - * 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 "napi/native_api.h" - -/* - * The insight intent context module definition. - */ -static napi_module _module = { - .nm_version = 0, - .nm_modname = "app.ability.InsightIntentContext", - .nm_filename = "app/ability/insightintentcontext_napi.so", -}; - -/* - * The insight intent context module registration. - */ -extern "C" __attribute__((constructor)) void NAPI_Insight_Intent_Context_AutoRegister(void) -{ - napi_module_register(&_module); -} +/* + * 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 "napi/native_api.h" + +extern const char _binary_insight_intent_context_js_start[]; +extern const char _binary_insight_intent_context_js_end[]; +extern const char _binary_insight_intent_context_abc_start[]; +extern const char _binary_insight_intent_context_abc_end[]; + +/* + * The insight intent context module definition. + */ +static napi_module _module = { + .nm_version = 0, + .nm_modname = "app.ability.InsightIntentContext", + .nm_filename = "app/ability/insightintentcontext_napi.so", +}; + +/* + * The insight intent context module registration. + */ +extern "C" __attribute__((constructor)) void NAPI_app_ability_InsightIntentContext_AutoRegister(void) +{ + napi_module_register(&_module); +} + +extern "C" __attribute__((visibility("default"))) +void NAPI_app_ability_InsightIntentContext_GetJSCode(const char **buf, int *bufLen) +{ + if (buf != nullptr) { + *buf = _binary_insight_intent_context_js_start; + } + + if (bufLen != nullptr) { + *bufLen = _binary_insight_intent_context_js_end - _binary_insight_intent_context_js_start; + } +} + +extern "C" __attribute__((visibility("default"))) +void NAPI_app_ability_InsightIntentContext_GetABCCode(const char **buf, int *buflen) +{ + if (buf != nullptr) { + *buf = _binary_insight_intent_context_abc_start; + } + if (buflen != nullptr) { + *buflen = _binary_insight_intent_context_abc_end - _binary_insight_intent_context_abc_start; + } +} diff --git a/frameworks/native/ability/native/BUILD.gn b/frameworks/native/ability/native/BUILD.gn index 83854f153d5..2487d7690a3 100644 --- a/frameworks/native/ability/native/BUILD.gn +++ b/frameworks/native/ability/native/BUILD.gn @@ -247,9 +247,11 @@ ohos_shared_library("abilitykit_native") { "${ability_runtime_innerkits_path}/wantagent:wantagent_innerkits", "${ability_runtime_native_path}/ability:ability_context_native", "${ability_runtime_native_path}/ability/native:ability_business_error", + "${ability_runtime_native_path}/ability/native:insight_intent_executor", "${ability_runtime_native_path}/appkit:app_context", "${ability_runtime_native_path}/appkit:app_context_utils", "${ability_runtime_native_path}/appkit:appkit_delegator", + "${ability_runtime_native_path}/insight_intent/insight_intent_context:insightintentcontext", "${ability_runtime_path}/utils/global/freeze:freeze_util", "${ability_runtime_services_path}/common:event_report", ] @@ -413,6 +415,7 @@ config("insight_intent_executor_public_config") { ohos_shared_library("insight_intent_executor") { sources = [ "${ability_runtime_native_path}/ability/native/insight_intent_executor/insight_intent_executor.cpp", + "${ability_runtime_native_path}/ability/native/insight_intent_executor/insight_intent_executor_mgr.cpp", "${ability_runtime_native_path}/ability/native/insight_intent_executor/js_insight_intent_executor.cpp", ] @@ -423,6 +426,7 @@ ohos_shared_library("insight_intent_executor") { "${ability_runtime_innerkits_path}/runtime:runtime", "${ability_runtime_napi_path}/inner/napi_common:napi_common", "${ability_runtime_native_path}/ability/native:ability_business_error", + "${ability_runtime_native_path}/insight_intent/insight_intent_context:insightintentcontext", ] external_deps = [ diff --git a/frameworks/native/ability/native/insight_intent_executor/insight_intent_executor.cpp b/frameworks/native/ability/native/insight_intent_executor/insight_intent_executor.cpp index 4ff65c2130f..fff87767133 100644 --- a/frameworks/native/ability/native/insight_intent_executor/insight_intent_executor.cpp +++ b/frameworks/native/ability/native/insight_intent_executor/insight_intent_executor.cpp @@ -32,4 +32,22 @@ std::shared_ptr InsightIntentExecutor::Create(Runtime& ru return nullptr; } } + +bool InsightIntentExecutor::Init(const InsightIntentExecutorInfo& intentInfo) +{ + auto executeParam = intentInfo.executeParam; + if (executeParam == nullptr) { + HILOG_ERROR("Execute param invalid."); + return false; + } + + context_ = std::make_shared(intentInfo.token, executeParam->bundleName_, + intentInfo.windowMode, executeParam->insightIntentId_); + return true; +} + +std::shared_ptr InsightIntentExecutor::GetContext() +{ + return context_; +} } // namespace OHOS::AbilityRuntime diff --git a/frameworks/native/ability/native/insight_intent_executor/insight_intent_executor_mgr.cpp b/frameworks/native/ability/native/insight_intent_executor/insight_intent_executor_mgr.cpp new file mode 100644 index 00000000000..232d0968b9f --- /dev/null +++ b/frameworks/native/ability/native/insight_intent_executor/insight_intent_executor_mgr.cpp @@ -0,0 +1,111 @@ +/* + * 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 "insight_intent_executor_mgr.h" + +#include "ability_business_error.h" +#include "hilog_wrapper.h" + +namespace OHOS { +namespace AbilityRuntime { +InsightIntentExecutorMgr::InsightIntentExecutorMgr() +{ + HILOG_DEBUG("constructor"); +} + +InsightIntentExecutorMgr::~InsightIntentExecutorMgr() +{ + HILOG_INFO("deconstructor"); +} + +bool InsightIntentExecutorMgr::ExecuteInsightIntent(Runtime& runtime, const InsightIntentExecutorInfo& executeInfo, + std::unique_ptr callback) +{ + HILOG_DEBUG("called."); + auto executeParam = executeInfo.executeParam; + if (executeParam == nullptr || executeParam->insightIntentParam_ == nullptr) { + HILOG_ERROR("Execute param invalid."); + TriggerCallbackInner(std::move(callback), static_cast(AbilityErrorCode::ERROR_CODE_INVALID_PARAM)); + return false; + } + + auto asyncCallback = + [weak = weak_from_this(), intentId = executeParam->insightIntentId_](InsightIntentExecuteResult result) { + // erase map when called + HILOG_DEBUG("Begin remove executor."); + auto executorMgr = weak.lock(); + if (executorMgr == nullptr) { + HILOG_ERROR("Executor manager invalid."); + return; + } + executorMgr->RemoveInsightIntentExecutor(intentId); + }; + callback->Push(asyncCallback); + + // Create insight intent executor + auto intentExecutor = InsightIntentExecutor::Create(runtime); + if (intentExecutor == nullptr) { + HILOG_ERROR("Create insight intent executor failed."); + TriggerCallbackInner(std::move(callback), static_cast(AbilityErrorCode::ERROR_CODE_INVALID_PARAM)); + return false; + } + + if (!intentExecutor->Init(executeInfo)) { + HILOG_ERROR("Init intent executor failed."); + TriggerCallbackInner(std::move(callback), static_cast(AbilityErrorCode::ERROR_CODE_INVALID_PARAM)); + return false; + } + + AddInsightIntentExecutor(executeParam->insightIntentId_, intentExecutor); + + bool isAsync = false; + auto ret = intentExecutor->HandleExecuteIntent(static_cast(executeParam->executeMode_), + executeParam->insightIntentName_, *executeParam->insightIntentParam_, executeInfo.pageLoader, + std::move(callback), isAsync); + if (!ret) { + HILOG_ERROR("Execute intent failed."); + // callback has removed, if execute insight intent failed, call in sub function. + return false; + } + + return true; +} + +void InsightIntentExecutorMgr::AddInsightIntentExecutor(uint64_t intentId, + const std::shared_ptr& executor) +{ + HILOG_DEBUG("called."); + std::lock_guard lock(mutex_); + insightIntentExecutors_[intentId] = executor; +} + +void InsightIntentExecutorMgr::RemoveInsightIntentExecutor(uint64_t intentId) +{ + HILOG_DEBUG("called."); + std::lock_guard lock(mutex_); + insightIntentExecutors_.erase(intentId); +} + +void InsightIntentExecutorMgr::TriggerCallbackInner( + std::unique_ptr callback, int32_t errCode) +{ + HILOG_DEBUG("called."); + AppExecFwk::InsightIntentExecuteResult result; + result.innerErr = errCode; + callback->Call(result); + callback.reset(); +} +} // namespace AbilityRuntime +} // namespace OHOS diff --git a/frameworks/native/ability/native/insight_intent_executor/js_insight_intent_executor.cpp b/frameworks/native/ability/native/insight_intent_executor/js_insight_intent_executor.cpp index 73b9a87f6f5..a30a86e30a9 100644 --- a/frameworks/native/ability/native/insight_intent_executor/js_insight_intent_executor.cpp +++ b/frameworks/native/ability/native/insight_intent_executor/js_insight_intent_executor.cpp @@ -21,6 +21,7 @@ #include "hilog_wrapper.h" #include "insight_intent_constant.h" #include "insight_intent_execute_result.h" +#include "js_insight_intent_context.h" #include "js_runtime.h" #include "js_runtime_utils.h" #include "napi_common_execute_result.h" @@ -58,6 +59,7 @@ bool JsInsightIntentExecutor::Init(const InsightIntentExecutorInfo& insightInten HILOG_DEBUG("enter"); STATE_PATTERN_NAIVE_ACCEPT(State::CREATED, false); state_ = State::INITIALIZED; + InsightIntentExecutor::Init(insightIntentInfo); HandleScope handleScope(runtime_); jsObj_ = JsInsightIntentExecutor::LoadJsCode(insightIntentInfo, runtime_); @@ -66,6 +68,34 @@ bool JsInsightIntentExecutor::Init(const InsightIntentExecutorInfo& insightInten STATE_PATTERN_NAIVE_STATE_SET_AND_RETURN(State::INVALID, false); } + auto env = runtime_.GetNapiEnv(); + if (env == nullptr) { + HILOG_ERROR("Napi env invalid."); + STATE_PATTERN_NAIVE_STATE_SET_AND_RETURN(State::INVALID, false); + } + + auto context = GetContext(); + if (context == nullptr) { + HILOG_ERROR("Context invalid."); + STATE_PATTERN_NAIVE_STATE_SET_AND_RETURN(State::INVALID, false); + } + + napi_value contextObj = CreateJsInsightIntentContext(env, context); + contextObj_ = JsRuntime::LoadSystemModuleByEngine(env, "app.ability.InsightIntentContext", &contextObj, 1); + if (contextObj_ == nullptr) { + HILOG_ERROR("Load system module failed."); + STATE_PATTERN_NAIVE_STATE_SET_AND_RETURN(State::INVALID, false); + } + + auto executorNapiVal = jsObj_->GetNapiValue(); + auto contextNapiVal = contextObj_->GetNapiValue(); + if (!CheckTypeForNapiValue(env, executorNapiVal, napi_object) || + !CheckTypeForNapiValue(env, contextNapiVal, napi_object) || + napi_set_named_property(env, executorNapiVal, "context", contextNapiVal) != napi_ok) { + HILOG_ERROR("Set property failed."); + STATE_PATTERN_NAIVE_STATE_SET_AND_RETURN(State::INVALID, false); + } + return true; } @@ -204,12 +234,12 @@ bool JsInsightIntentExecutor::CallJsFunctionWithResultInner( result); } -std::shared_ptr JsInsightIntentExecutor::GetResultFromJs( +std::shared_ptr JsInsightIntentExecutor::GetResultFromJs( napi_env env, napi_value resultJs) { HILOG_DEBUG("enter"); - auto resultCpp = std::make_shared(); - if (!AppExecFwk::UnwrapExecuteResult(env, resultJs, *resultCpp)) { + auto resultCpp = std::make_shared(); + if (!UnwrapExecuteResult(env, resultJs, *resultCpp)) { return nullptr; } return resultCpp; @@ -229,7 +259,7 @@ napi_value JsInsightIntentExecutor::ResolveCbCpp(napi_env env, napi_callback_inf JsInsightIntentExecutor::ReplyFailed(callback); return nullptr; } - std::shared_ptr resultCpp = + std::shared_ptr resultCpp = JsInsightIntentExecutor::GetResultFromJs(env, resultJs); JsInsightIntentExecutor::ReplySucceeded(callback, resultCpp); return nullptr; @@ -251,14 +281,14 @@ void JsInsightIntentExecutor::ReplyFailed(InsightIntentExecutorAsyncCallback* ca if (callback == nullptr) { return; } - AAFwk::IntentExecuteResult errorResult{}; + AppExecFwk::InsightIntentExecuteResult errorResult{}; errorResult.innerErr = innerErr; callback->Call(errorResult); delete callback; } void JsInsightIntentExecutor::ReplySucceeded(InsightIntentExecutorAsyncCallback* callback, - std::shared_ptr resultCpp) + std::shared_ptr resultCpp) { HILOG_DEBUG("enter"); if (callback == nullptr) { @@ -281,7 +311,7 @@ void JsInsightIntentExecutor::ReplyFailedInner(InsightIntentInnerErr innerErr) JsInsightIntentExecutor::ReplyFailed(callback, innerErr); } -void JsInsightIntentExecutor::ReplySucceededInner(std::shared_ptr resultCpp) +void JsInsightIntentExecutor::ReplySucceededInner(std::shared_ptr resultCpp) { HILOG_DEBUG("enter"); state_ = JsInsightIntentExecutor::State::EXECUTATION_DONE; @@ -325,7 +355,7 @@ bool JsInsightIntentExecutor::HandleResultReturnedFromJsFunc(napi_value resultJs napi_call_function(env, resultJs, promiseCatch, argcCatch, argvCatch, nullptr); } else { HILOG_INFO("Not promise"); - std::shared_ptr resultCpp = JsInsightIntentExecutor::GetResultFromJs(env, resultJs); + auto resultCpp = JsInsightIntentExecutor::GetResultFromJs(env, resultJs); if (resultCpp == nullptr) { HILOG_ERROR("Result invalid"); ReplyFailedInner(); diff --git a/frameworks/native/ability/native/insight_intent_host_client.cpp b/frameworks/native/ability/native/insight_intent_host_client.cpp index 48a39ddf03f..06e3aa75cec 100644 --- a/frameworks/native/ability/native/insight_intent_host_client.cpp +++ b/frameworks/native/ability/native/insight_intent_host_client.cpp @@ -54,7 +54,7 @@ void InsightIntentHostClient::RemoveInsightIntentExecute(uint64_t key) } void InsightIntentHostClient::OnExecuteDone(uint64_t key, int32_t resultCode, - AppExecFwk::InsightIntentExecuteResult &executeResult) + const AppExecFwk::InsightIntentExecuteResult &executeResult) { HILOG_DEBUG("called."); diff --git a/frameworks/native/insight_intent/insight_intent_context/BUILD.gn b/frameworks/native/insight_intent/insight_intent_context/BUILD.gn index 914cde4cf3a..66264511ed4 100644 --- a/frameworks/native/insight_intent/insight_intent_context/BUILD.gn +++ b/frameworks/native/insight_intent/insight_intent_context/BUILD.gn @@ -9,31 +9,45 @@ # 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. +# limitations under the License. import("//build/ohos.gni") import("//foundation/ability/ability_runtime/ability_runtime.gni") -ohos_shared_library("insightintentcontext") { +config("insight_intent_context_public_config") { + visibility = [ ":*" ] include_dirs = [ "${ability_runtime_innerkits_path}/insight_intent/insight_intent_context", ] +} - sources = [ "insight_intent_context.cpp" ] +ohos_shared_library("insightintentcontext") { + sources = [ + "insight_intent_context.cpp", + "js_insight_intent_context.cpp", + ] defines = [ "AMS_LOG_TAG = \"InsigtIntent\"" ] defines += [ "AMS_LOG_DOMAIN = 0xD001308" ] - deps = [ "${ability_runtime_innerkits_path}/ability_manager:ability_manager" ] + public_configs = [ ":insight_intent_context_public_config" ] + + deps = [ + "${ability_runtime_innerkits_path}/ability_manager:ability_manager", + "${ability_runtime_innerkits_path}/runtime:runtime", + "${ability_runtime_napi_path}/inner/napi_common:napi_common", + "${ability_runtime_native_path}/ability/native:ability_business_error", + ] external_deps = [ "ability_base:want", "c_utils:utils", "hilog:libhilog", "hitrace:hitrace_meter", + "ipc:ipc_single", + "napi:ace_napi", ] - relative_install_dir = "insight_intent" subsystem_name = "ability" part_name = "ability_runtime" } diff --git a/frameworks/js/napi/insight_intent_context/js_insight_intent_context.cpp b/frameworks/native/insight_intent/insight_intent_context/js_insight_intent_context.cpp similarity index 100% rename from frameworks/js/napi/insight_intent_context/js_insight_intent_context.cpp rename to frameworks/native/insight_intent/insight_intent_context/js_insight_intent_context.cpp diff --git a/interfaces/inner_api/ability_manager/include/insight_intent_execute_callback_interface.h b/interfaces/inner_api/ability_manager/include/insight_intent_execute_callback_interface.h index 21fbc468b37..a9187b909a6 100644 --- a/interfaces/inner_api/ability_manager/include/insight_intent_execute_callback_interface.h +++ b/interfaces/inner_api/ability_manager/include/insight_intent_execute_callback_interface.h @@ -37,7 +37,7 @@ public: * @param executeResult, the execute result. */ virtual void OnExecuteDone(uint64_t key, int32_t resultCode, - AppExecFwk::InsightIntentExecuteResult &executeResult) = 0; + const AppExecFwk::InsightIntentExecuteResult &executeResult) = 0; enum { // ipc id for OnExecuteDone diff --git a/interfaces/inner_api/ability_manager/include/insight_intent_execute_callback_proxy.h b/interfaces/inner_api/ability_manager/include/insight_intent_execute_callback_proxy.h index f491e7b3576..bbc9fed5754 100644 --- a/interfaces/inner_api/ability_manager/include/insight_intent_execute_callback_proxy.h +++ b/interfaces/inner_api/ability_manager/include/insight_intent_execute_callback_proxy.h @@ -36,7 +36,7 @@ public: * @param executeResult, the execute result. */ void OnExecuteDone(uint64_t key, int32_t resultCode, - AppExecFwk::InsightIntentExecuteResult &executeResult) override; + const AppExecFwk::InsightIntentExecuteResult &executeResult) override; private: static inline BrokerDelegator delegator_; diff --git a/interfaces/inner_api/ability_manager/include/insight_intent_execute_param.h b/interfaces/inner_api/ability_manager/include/insight_intent_execute_param.h index 567dd1d5745..3f16cc607e3 100644 --- a/interfaces/inner_api/ability_manager/include/insight_intent_execute_param.h +++ b/interfaces/inner_api/ability_manager/include/insight_intent_execute_param.h @@ -39,6 +39,7 @@ constexpr char INSIGHT_INTENT_EXECUTE_PARAM_NAME[] = "ohos.insightIntent.execute constexpr char INSIGHT_INTENT_EXECUTE_PARAM_PARAM[] = "ohos.insightIntent.executeParam.param"; constexpr char INSIGHT_INTENT_EXECUTE_PARAM_MODE[] = "ohos.insightIntent.executeParam.mode"; constexpr char INSIGHT_INTENT_EXECUTE_PARAM_ID[] = "ohos.insightIntent.executeParam.id"; +constexpr char INSIGHT_INTENT_SRC_ENTRY[] = "ohos.insightIntent.srcEntry"; class InsightIntentExecuteParam : public Parcelable { public: diff --git a/interfaces/inner_api/ability_manager/include/insight_intent_profile.h b/interfaces/inner_api/ability_manager/include/insight_intent_profile.h new file mode 100644 index 00000000000..df41f5db57b --- /dev/null +++ b/interfaces/inner_api/ability_manager/include/insight_intent_profile.h @@ -0,0 +1,63 @@ +/* + * 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. + */ + +#ifndef OHOS_ABILITY_RUNTIME_INSIGHT_INTENT_PROFILE_H +#define OHOS_ABILITY_RUNTIME_INSIGHT_INTENT_PROFILE_H + +#include +#include + +#include "insight_intent_execute_param.h" + +namespace OHOS { +namespace AbilityRuntime { +using ExecuteMode = AppExecFwk::ExecuteMode; + +struct UIAbilityIntentInfo { + std::string abilityName; + std::vector supportExecuteMode {}; +}; + +struct UIExtensionIntentInfo { + std::string abilityName; +}; + +struct ServiceExtensionIntentInfo { + std::string abilityName; +}; + +struct FormIntentInfo { + std::string abilityName; + std::string formName; +}; + +struct InsightIntentInfo { + std::string intentName; + std::string intentDomain; + std::string intentVersion; + std::string srcEntry; + UIAbilityIntentInfo uiAbilityIntentInfo; + UIExtensionIntentInfo uiExtensionIntentInfo; + ServiceExtensionIntentInfo serviceExtensionIntentInfo; + FormIntentInfo formIntentInfo; +}; + +class InsightIntentProfile { +public: + static bool TransformTo(const std::string &profileStr, std::vector &infos); +}; +} // namespace AbilityRuntime +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_INSIGHT_INTENT_PROFILE_H diff --git a/interfaces/inner_api/ability_manager/include/insight_intent_utils.h b/interfaces/inner_api/ability_manager/include/insight_intent_utils.h new file mode 100644 index 00000000000..bf408dac7b3 --- /dev/null +++ b/interfaces/inner_api/ability_manager/include/insight_intent_utils.h @@ -0,0 +1,33 @@ +/* + * 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. + */ + +#ifndef OHOS_ABILITY_RUNTIME_INSIGHT_INTENT_UTILS_H +#define OHOS_ABILITY_RUNTIME_INSIGHT_INTENT_UTILS_H + +#include + +#include "bundlemgr/bundle_mgr_interface.h" + +namespace OHOS { +namespace AbilityRuntime { +class InsightIntentUtils { +public: + static sptr GetBundleManagerProxy(); + static std::string GetSrcEntry(const std::string &bundleName, const std::string &moduleName, + const std::string &intentName); +}; +} // namespace AbilityRuntime +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_INSIGHT_INTENT_UTILS_H diff --git a/frameworks/js/napi/insight_intent_context/js_insight_intent_context.h b/interfaces/inner_api/insight_intent/insight_intent_context/js_insight_intent_context.h similarity index 100% rename from frameworks/js/napi/insight_intent_context/js_insight_intent_context.h rename to interfaces/inner_api/insight_intent/insight_intent_context/js_insight_intent_context.h diff --git a/interfaces/kits/native/ability/native/insight_intent_executor/insight_intent_executor.h b/interfaces/kits/native/ability/native/insight_intent_executor/insight_intent_executor.h index 70a8976d6ff..cc4dd762de1 100644 --- a/interfaces/kits/native/ability/native/insight_intent_executor/insight_intent_executor.h +++ b/interfaces/kits/native/ability/native/insight_intent_executor/insight_intent_executor.h @@ -20,20 +20,21 @@ #include #include #include "insight_intent_constant.h" +#include "insight_intent_context.h" #include "insight_intent_executor_info.h" namespace OHOS { namespace AAFwk { - struct InsightIntentExecuteResult; class WantParams; } // namespace AAFwk namespace AppExecFwk { + struct InsightIntentExecuteResult; template class AbilityTransactionCallbackInfo; } // namespace AppExecFwk namespace AbilityRuntime { class Runtime; using InsightIntentExecutorAsyncCallback = - AppExecFwk::AbilityTransactionCallbackInfo; + AppExecFwk::AbilityTransactionCallbackInfo; class InsightIntentExecutor { public: static std::shared_ptr Create(Runtime& runtime); @@ -66,8 +67,17 @@ public: const std::shared_ptr& pageLoader, std::unique_ptr callback, bool& isAsync) = 0; + + /** + * @brief Get current insight intent context. + * + * @return std::shared_ptr + */ + std::shared_ptr GetContext(); + +private: + std::shared_ptr context_ = nullptr; }; } // namespace AbilityRuntime } // namespace OHOS - #endif // OHOS_ABILITY_RUNTIME_INSIGHT_INTENT_EXECUTOR_H diff --git a/interfaces/kits/native/ability/native/insight_intent_executor/insight_intent_executor_info.h b/interfaces/kits/native/ability/native/insight_intent_executor/insight_intent_executor_info.h index bdd8700b800..69f210a8868 100644 --- a/interfaces/kits/native/ability/native/insight_intent_executor/insight_intent_executor_info.h +++ b/interfaces/kits/native/ability/native/insight_intent_executor/insight_intent_executor_info.h @@ -24,7 +24,7 @@ class NativeReference; namespace OHOS::AbilityRuntime { -using InsightIntentExecuteParam = AAFwk::InsightIntentExecuteParam; +using InsightIntentExecuteParam = AppExecFwk::InsightIntentExecuteParam; struct InsightIntentExecutorInfo { std::string srcEntry; std::string hapPath; diff --git a/interfaces/kits/native/ability/native/insight_intent_executor/insight_intent_executor_mgr.h b/interfaces/kits/native/ability/native/insight_intent_executor/insight_intent_executor_mgr.h new file mode 100644 index 00000000000..7d1c3fc7b3c --- /dev/null +++ b/interfaces/kits/native/ability/native/insight_intent_executor/insight_intent_executor_mgr.h @@ -0,0 +1,49 @@ +/* + * 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. + */ + +#ifndef OHOS_ABILITY_RUNTIME_INSIGHT_INTENT_EXECUTOR_MGR_H +#define OHOS_ABILITY_RUNTIME_INSIGHT_INTENT_EXECUTOR_MGR_H + +#include + +#include "ability_transaction_callback_info.h" +#include "insight_intent_executor.h" +#include "insight_intent_executor_info.h" +#include "insight_intent_execute_result.h" +#include "singleton.h" + +namespace OHOS { +namespace AbilityRuntime { +using InsightIntentExecuteResult = AppExecFwk::InsightIntentExecuteResult; +using InsightIntentExecutorAsyncCallback = AppExecFwk::AbilityTransactionCallbackInfo; + +class InsightIntentExecutorMgr : public std::enable_shared_from_this { + DECLARE_DELAYED_SINGLETON(InsightIntentExecutorMgr) + +public: + bool ExecuteInsightIntent(Runtime& runtime, const InsightIntentExecutorInfo &executeInfo, + std::unique_ptr callback); + static void TriggerCallbackInner(std::unique_ptr callback, int32_t errCode); + +private: + void AddInsightIntentExecutor(uint64_t intentId, const std::shared_ptr& executor); + void RemoveInsightIntentExecutor(uint64_t intentId); + + std::mutex mutex_; + std::map> insightIntentExecutors_; +}; +} // namespace AbilityRuntime +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_INSIGHT_INTENT_EXECUTOR_MGR_H diff --git a/interfaces/kits/native/ability/native/insight_intent_executor/js_insight_intent_executor.h b/interfaces/kits/native/ability/native/insight_intent_executor/js_insight_intent_executor.h index 26c8866c56d..fc78561ba3c 100644 --- a/interfaces/kits/native/ability/native/insight_intent_executor/js_insight_intent_executor.h +++ b/interfaces/kits/native/ability/native/insight_intent_executor/js_insight_intent_executor.h @@ -23,8 +23,8 @@ class NativeReference; namespace OHOS { -namespace AAFwk { - struct IntentExecuteResult; +namespace AppExecFwk { + struct InsightIntentExecuteResult; } // namespace AAFwk namespace AbilityRuntime { class JsRuntime; @@ -107,7 +107,7 @@ private: napi_value& result ); - static std::shared_ptr GetResultFromJs(napi_env env, + static std::shared_ptr GetResultFromJs(napi_env env, napi_value resultJs); static napi_value ResolveCbCpp(napi_env env, napi_callback_info info); @@ -116,9 +116,9 @@ private: static void ReplyFailed(InsightIntentExecutorAsyncCallback* callback, InsightIntentInnerErr innerErr = InsightIntentInnerErr::INSIGHT_INTENT_INTERNAL_ERROR); static void ReplySucceeded(InsightIntentExecutorAsyncCallback* callback, - std::shared_ptr resultCpp); + std::shared_ptr resultCpp); void ReplyFailedInner(InsightIntentInnerErr innerErr = InsightIntentInnerErr::INSIGHT_INTENT_INTERNAL_ERROR); - void ReplySucceededInner(std::shared_ptr resultCpp); + void ReplySucceededInner(std::shared_ptr resultCpp); bool HandleResultReturnedFromJsFunc(napi_value resultJs); @@ -147,6 +147,7 @@ private: JsRuntime& runtime_; State state_ = State::CREATED; std::unique_ptr jsObj_ = nullptr; + std::unique_ptr contextObj_ = nullptr; std::unique_ptr callback_; bool isAsync_ = false; }; diff --git a/interfaces/kits/native/ability/native/insight_intent_host_client.h b/interfaces/kits/native/ability/native/insight_intent_host_client.h index 33193250c3b..324d8010adc 100644 --- a/interfaces/kits/native/ability/native/insight_intent_host_client.h +++ b/interfaces/kits/native/ability/native/insight_intent_host_client.h @@ -55,7 +55,7 @@ public: * @param executeResult, the execute result. */ void OnExecuteDone(uint64_t key, int32_t resultCode, - AppExecFwk::InsightIntentExecuteResult &executeResult) override; + const AppExecFwk::InsightIntentExecuteResult &executeResult) override; private: static std::mutex instanceMutex_; diff --git a/services/abilitymgr/BUILD.gn b/services/abilitymgr/BUILD.gn index f3e4b574fc1..b22aa4e152e 100644 --- a/services/abilitymgr/BUILD.gn +++ b/services/abilitymgr/BUILD.gn @@ -149,6 +149,7 @@ ohos_shared_library("abilityms") { "access_token:libaccesstoken_sdk", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", + "bundle_framework:libappexecfwk_common", "c_utils:utils", "common_event_service:cesfwk_core", "common_event_service:cesfwk_innerkits", diff --git a/services/abilitymgr/abilitymgr.gni b/services/abilitymgr/abilitymgr.gni index 09269e8079c..adb9872aae7 100644 --- a/services/abilitymgr/abilitymgr.gni +++ b/services/abilitymgr/abilitymgr.gni @@ -49,6 +49,8 @@ abilityms_files = [ "src/pending_want_common_event.cpp", "src/ams_configuration_parameter.cpp", "src/image_info.cpp", + "src/insight_intent_utils.cpp", + "src/insight_intent_profile.cpp", "src/window_focus_changed_listener.cpp", # start ability handler diff --git a/services/abilitymgr/include/insight_intent_execute_manager.h b/services/abilitymgr/include/insight_intent_execute_manager.h index 1cb3ea3b648..6bd77f6b123 100644 --- a/services/abilitymgr/include/insight_intent_execute_manager.h +++ b/services/abilitymgr/include/insight_intent_execute_manager.h @@ -79,7 +79,8 @@ public: int32_t RemoveExecuteIntent(uint64_t intentId); - int32_t ExecuteIntentDone(uint64_t intentId, int32_t resultCode, AppExecFwk::InsightIntentExecuteResult &result); + int32_t ExecuteIntentDone(uint64_t intentId, int32_t resultCode, + const AppExecFwk::InsightIntentExecuteResult &result); int32_t RemoteDied(uint64_t intentId); diff --git a/services/abilitymgr/src/insight_intent_execute_callback_proxy.cpp b/services/abilitymgr/src/insight_intent_execute_callback_proxy.cpp index c93ee221245..86a0e59ccb0 100644 --- a/services/abilitymgr/src/insight_intent_execute_callback_proxy.cpp +++ b/services/abilitymgr/src/insight_intent_execute_callback_proxy.cpp @@ -22,7 +22,7 @@ namespace OHOS { namespace AAFwk { void InsightIntentExecuteCallbackProxy::OnExecuteDone(uint64_t key, int32_t resultCode, - AppExecFwk::InsightIntentExecuteResult &executeResult) + const AppExecFwk::InsightIntentExecuteResult &executeResult) { HILOG_DEBUG("call"); MessageParcel data; diff --git a/services/abilitymgr/src/insight_intent_execute_manager.cpp b/services/abilitymgr/src/insight_intent_execute_manager.cpp index 71131170695..c3bc3fc8dc6 100644 --- a/services/abilitymgr/src/insight_intent_execute_manager.cpp +++ b/services/abilitymgr/src/insight_intent_execute_manager.cpp @@ -16,9 +16,11 @@ #include "insight_intent_execute_manager.h" #include -#include "insight_intent_execute_callback_interface.h" -#include "hilog_wrapper.h" + #include "ability_manager_errors.h" +#include "hilog_wrapper.h" +#include "insight_intent_execute_callback_interface.h" +#include "insight_intent_utils.h" #include "permission_verification.h" #include "want_params_wrapper.h" @@ -124,7 +126,7 @@ int32_t InsightIntentExecuteManager::RemoveExecuteIntent(uint64_t intentId) } int32_t InsightIntentExecuteManager::ExecuteIntentDone(uint64_t intentId, int32_t resultCode, - AppExecFwk::InsightIntentExecuteResult &result) + const AppExecFwk::InsightIntentExecuteResult &result) { std::lock_guard lock(mutex_); auto findResult = records_.find(intentId); @@ -209,6 +211,15 @@ int32_t InsightIntentExecuteManager::GenerateWant( want.SetParams(wantParams); } } + + auto srcEntry = AbilityRuntime::InsightIntentUtils::GetSrcEntry(param->bundleName_, param->moduleName_, + param->insightIntentName_); + if (srcEntry.empty()) { + HILOG_ERROR("Insight intent srcEntry invalid."); + return ERR_INVALID_VALUE; + } + want.SetParam(INSIGHT_INTENT_SRC_ENTRY, srcEntry); + want.SetParam(INSIGHT_INTENT_EXECUTE_PARAM_NAME, param->insightIntentName_); want.SetParam(INSIGHT_INTENT_EXECUTE_PARAM_MODE, param->executeMode_); want.SetParam(INSIGHT_INTENT_EXECUTE_PARAM_ID, std::to_string(param->insightIntentId_)); diff --git a/services/abilitymgr/src/insight_intent_profile.cpp b/services/abilitymgr/src/insight_intent_profile.cpp new file mode 100644 index 00000000000..a336c9f73bd --- /dev/null +++ b/services/abilitymgr/src/insight_intent_profile.cpp @@ -0,0 +1,303 @@ +/* + * 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 "insight_intent_profile.h" + +#include "hilog_wrapper.h" +#include "json_util.h" +#include "nlohmann/json.hpp" + +namespace OHOS { +namespace AbilityRuntime { +using JsonType = AppExecFwk::JsonType; +using ArrayType = AppExecFwk::ArrayType; + +namespace { +int32_t g_parseResult = ERR_OK; +std::mutex g_mutex; + +const std::string INSIGHT_INTENTS = "insightIntents"; +const std::string INSIGHT_INTENT_NAME = "intentName"; +const std::string INSIGHT_INTENT_DOMAIN = "domain"; +const std::string INSIGHT_INTENT_VERSION = "intentVersion"; +const std::string INSIGHT_INTENT_SRC_ENTRY = "srcEntry"; +const std::string INSIGHT_INTENT_UI_ABILITY = "uiAbility"; +const std::string INSIGHT_INTENT_UI_EXTENSION = "uiExtension"; +const std::string INSIGHT_INTENT_SERVICE_EXTENSION = "serviceExtension"; +const std::string INSIGHT_INTENT_FORM = "form"; +const std::string INSIGHT_INTENT_ABILITY = "ability"; +const std::string INSIGHT_INTENT_EXECUTE_MODE = "executeMode"; +const std::string INSIGHT_INTENT_FORM_NAME = "formName"; + +const std::map executeModeMap = { + {"foreground", ExecuteMode::UI_ABILITY_FOREGROUND}, + {"background", ExecuteMode::UI_ABILITY_BACKGROUND} +}; + +struct UIAbilityProfileInfo { + std::string abilityName; + std::vector supportExecuteMode {}; +}; + +struct UIExtensionProfileInfo { + std::string abilityName; +}; + +struct ServiceExtensionProfileInfo { + std::string abilityName; +}; + +struct FormProfileInfo { + std::string abilityName; + std::string formName; +}; + +struct InsightIntentProfileInfo { + std::string intentName; + std::string intentDomain; + std::string intentVersion; + std::string srcEntry; + UIAbilityProfileInfo uiAbilityProfileInfo; + UIExtensionProfileInfo uiExtensionProfileInfo; + ServiceExtensionProfileInfo serviceExtensionProfileInfo; + FormProfileInfo formProfileInfo; +}; + +struct InsightIntentProfileInfoVec { + std::vector insightIntents {}; +}; + +void from_json(const nlohmann::json &jsonObject, UIAbilityProfileInfo &info) +{ + const auto &jsonObjectEnd = jsonObject.end(); + AppExecFwk::GetValueIfFindKey(jsonObject, + jsonObjectEnd, + INSIGHT_INTENT_ABILITY, + info.abilityName, + JsonType::STRING, + true, + g_parseResult, + ArrayType::NOT_ARRAY); + AppExecFwk::GetValueIfFindKey>(jsonObject, + jsonObjectEnd, + INSIGHT_INTENT_EXECUTE_MODE, + info.supportExecuteMode, + JsonType::ARRAY, + true, + g_parseResult, + ArrayType::STRING); +} + +void from_json(const nlohmann::json &jsonObject, UIExtensionProfileInfo &info) +{ + const auto &jsonObjectEnd = jsonObject.end(); + AppExecFwk::GetValueIfFindKey(jsonObject, + jsonObjectEnd, + INSIGHT_INTENT_ABILITY, + info.abilityName, + JsonType::STRING, + true, + g_parseResult, + ArrayType::NOT_ARRAY); +} + +void from_json(const nlohmann::json &jsonObject, ServiceExtensionProfileInfo &info) +{ + const auto &jsonObjectEnd = jsonObject.end(); + AppExecFwk::GetValueIfFindKey(jsonObject, + jsonObjectEnd, + INSIGHT_INTENT_ABILITY, + info.abilityName, + JsonType::STRING, + true, + g_parseResult, + ArrayType::NOT_ARRAY); +} + +void from_json(const nlohmann::json &jsonObject, FormProfileInfo &info) +{ + const auto &jsonObjectEnd = jsonObject.end(); + AppExecFwk::GetValueIfFindKey(jsonObject, + jsonObjectEnd, + INSIGHT_INTENT_ABILITY, + info.abilityName, + JsonType::STRING, + true, + g_parseResult, + ArrayType::NOT_ARRAY); + AppExecFwk::GetValueIfFindKey(jsonObject, + jsonObjectEnd, + INSIGHT_INTENT_FORM_NAME, + info.formName, + JsonType::STRING, + true, + g_parseResult, + ArrayType::NOT_ARRAY); +} + +void from_json(const nlohmann::json &jsonObject, InsightIntentProfileInfo &insightIntentInfo) +{ + const auto &jsonObjectEnd = jsonObject.end(); + AppExecFwk::GetValueIfFindKey(jsonObject, + jsonObjectEnd, + INSIGHT_INTENT_NAME, + insightIntentInfo.intentName, + JsonType::STRING, + true, + g_parseResult, + ArrayType::NOT_ARRAY); + AppExecFwk::GetValueIfFindKey(jsonObject, + jsonObjectEnd, + INSIGHT_INTENT_DOMAIN, + insightIntentInfo.intentDomain, + JsonType::STRING, + true, + g_parseResult, + ArrayType::NOT_ARRAY); + AppExecFwk::GetValueIfFindKey(jsonObject, + jsonObjectEnd, + INSIGHT_INTENT_VERSION, + insightIntentInfo.intentVersion, + JsonType::STRING, + true, + g_parseResult, + ArrayType::NOT_ARRAY); + AppExecFwk::GetValueIfFindKey(jsonObject, + jsonObjectEnd, + INSIGHT_INTENT_SRC_ENTRY, + insightIntentInfo.srcEntry, + JsonType::STRING, + true, + g_parseResult, + ArrayType::NOT_ARRAY); + AppExecFwk::GetValueIfFindKey(jsonObject, + jsonObjectEnd, + INSIGHT_INTENT_UI_ABILITY, + insightIntentInfo.uiAbilityProfileInfo, + JsonType::OBJECT, + false, + g_parseResult, + ArrayType::NOT_ARRAY); + AppExecFwk::GetValueIfFindKey(jsonObject, + jsonObjectEnd, + INSIGHT_INTENT_UI_EXTENSION, + insightIntentInfo.uiExtensionProfileInfo, + JsonType::OBJECT, + false, + g_parseResult, + ArrayType::NOT_ARRAY); + AppExecFwk::GetValueIfFindKey(jsonObject, + jsonObjectEnd, + INSIGHT_INTENT_SERVICE_EXTENSION, + insightIntentInfo.serviceExtensionProfileInfo, + JsonType::OBJECT, + false, + g_parseResult, + ArrayType::NOT_ARRAY); + AppExecFwk::GetValueIfFindKey(jsonObject, + jsonObjectEnd, + INSIGHT_INTENT_FORM, + insightIntentInfo.formProfileInfo, + JsonType::OBJECT, + false, + g_parseResult, + ArrayType::NOT_ARRAY); +} + +void from_json(const nlohmann::json &jsonObject, InsightIntentProfileInfoVec &infos) +{ + const auto &jsonObjectEnd = jsonObject.end(); + AppExecFwk::GetValueIfFindKey>(jsonObject, + jsonObjectEnd, + INSIGHT_INTENTS, + infos.insightIntents, + JsonType::ARRAY, + false, + g_parseResult, + ArrayType::OBJECT); +} + +bool TransformToInsightIntentInfo(const InsightIntentProfileInfo &insightIntent, InsightIntentInfo &info) +{ + if (insightIntent.intentName.empty()) { + HILOG_ERROR("Intent name invalid."); + return false; + } + + info.intentName = insightIntent.intentName; + info.intentDomain = insightIntent.intentDomain; + info.intentVersion = insightIntent.intentVersion; + info.srcEntry = insightIntent.srcEntry; + + info.uiAbilityIntentInfo.abilityName = insightIntent.uiAbilityProfileInfo.abilityName; + for (const auto &executeMode: insightIntent.uiAbilityProfileInfo.supportExecuteMode) { + auto mode = std::find_if(std::begin(executeModeMap), std::end(executeModeMap), + [&executeMode](const auto &item) { + return item.first == executeMode; + }); + if (mode == executeModeMap.end()) { + continue; + } + info.uiAbilityIntentInfo.supportExecuteMode.emplace_back(mode->second); + } + + info.uiExtensionIntentInfo.abilityName = insightIntent.uiExtensionProfileInfo.abilityName; + info.serviceExtensionIntentInfo.abilityName = insightIntent.serviceExtensionProfileInfo.abilityName; + info.formIntentInfo.abilityName = insightIntent.formProfileInfo.abilityName; + info.formIntentInfo.formName = insightIntent.formProfileInfo.formName; + return true; +} + +bool TransformToInfos(const InsightIntentProfileInfoVec &profileInfos, std::vector &intentInfos) +{ + HILOG_DEBUG("called"); + for (const auto &insightIntent : profileInfos.insightIntents) { + InsightIntentInfo info; + if (!TransformToInsightIntentInfo(insightIntent, info)) { + return false; + } + intentInfos.push_back(info); + } + return true; +} +} // namespace + +bool InsightIntentProfile::TransformTo(const std::string &profileStr, std::vector &intentInfos) +{ + HILOG_DEBUG("called"); + auto jsonObject = nlohmann::json::parse(profileStr, nullptr, false); + if (jsonObject.is_discarded()) { + HILOG_ERROR("Profile invalid."); + return false; + } + + InsightIntentProfileInfoVec profileInfos; + { + std::lock_guard lock(g_mutex); + g_parseResult = ERR_OK; + profileInfos = jsonObject.get(); + if (g_parseResult != ERR_OK) { + HILOG_ERROR("g_parseResult is %{public}d", g_parseResult); + int32_t ret = g_parseResult; + // need recover parse result to ERR_OK + g_parseResult = ERR_OK; + return ret; + } + } + + return TransformToInfos(profileInfos, intentInfos); +} +} // namespace AbilityRuntime +} // namespace OHOS diff --git a/services/abilitymgr/src/insight_intent_utils.cpp b/services/abilitymgr/src/insight_intent_utils.cpp new file mode 100644 index 00000000000..003c198e1b3 --- /dev/null +++ b/services/abilitymgr/src/insight_intent_utils.cpp @@ -0,0 +1,92 @@ +/* + * 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 "insight_intent_utils.h" + +#include "hilog_wrapper.h" +#include "if_system_ability_manager.h" +#include "in_process_call_wrapper.h" +#include "insight_intent_profile.h" +#include "iservice_registry.h" +#include "module_info.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace AbilityRuntime { +sptr InsightIntentUtils::GetBundleManagerProxy() +{ + auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityMgr == nullptr) { + HILOG_ERROR("Failed to get system ability manager."); + return nullptr; + } + + auto remoteObj = systemAbilityMgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (remoteObj == nullptr) { + HILOG_ERROR("Remote object is nullptr."); + return nullptr; + } + + auto bundleMgr = iface_cast(remoteObj); + if (bundleMgr == nullptr) { + HILOG_ERROR("Failed to get bundle manager."); + return nullptr; + } + + return bundleMgr; +} + +std::string InsightIntentUtils::GetSrcEntry(const std::string &bundleName, const std::string &moduleName, + const std::string &intentName) +{ + HILOG_DEBUG("Get srcEntry, bundleName: %{public}s, moduleName: %{public}s, intentName: %{public}s.", + bundleName.c_str(), moduleName.c_str(), intentName.c_str()); + if (bundleName.empty() || moduleName.empty() || intentName.empty()) { + HILOG_ERROR("Invalid param."); + return std::string(""); + } + + auto bundleMgr = GetBundleManagerProxy(); + if (bundleMgr == nullptr) { + return std::string(""); + } + + // Get json profile firstly + std::string profile; + auto ret = IN_PROCESS_CALL(bundleMgr->GetJsonProfile(AppExecFwk::INTENT_PROFILE, bundleName, moduleName, profile)); + if (ret != ERR_OK) { + HILOG_ERROR("Get json profile failed, error code: %{public}d.", ret); + return std::string(""); + } + + // Transform json string + std::vector infos; + if (!InsightIntentProfile::TransformTo(profile, infos)) { + HILOG_ERROR("Transform profile failed."); + return std::string(""); + } + + // Get srcEntry when intentName matched + std::string srcEntry(""); + for (const auto &info: infos) { + if (info.intentName == intentName) { + srcEntry = info.srcEntry; + } + } + HILOG_DEBUG("srcEntry: %{public}s", srcEntry.c_str()); + return srcEntry; +} +} // namespace AbilityRuntime +} // namespace OHOS diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 34cb92e8d12..6711bd651bd 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -139,6 +139,8 @@ ohos_source_set("abilityms_test_source") { "${ability_runtime_services_path}/abilitymgr/src/dlp_state_item.cpp", "${ability_runtime_services_path}/abilitymgr/src/free_install_manager.cpp", "${ability_runtime_services_path}/abilitymgr/src/insight_intent_execute_manager.cpp", + "${ability_runtime_services_path}/abilitymgr/src/insight_intent_profile.cpp", + "${ability_runtime_services_path}/abilitymgr/src/insight_intent_utils.cpp", "${ability_runtime_services_path}/abilitymgr/src/lifecycle_deal.cpp", "${ability_runtime_services_path}/abilitymgr/src/pending_want_common_event.cpp", "${ability_runtime_services_path}/abilitymgr/src/pending_want_key.cpp", @@ -205,6 +207,7 @@ ohos_source_set("abilityms_test_source") { "access_token:libaccesstoken_sdk", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", + "bundle_framework:libappexecfwk_common", "c_utils:utils", "common_event_service:cesfwk_core", "common_event_service:cesfwk_innerkits", -- Gitee