1 /*
2 * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "insight_intent_executor_mgr.h"
17
18 #include "ability_business_error.h"
19 #include "hilog_tag_wrapper.h"
20
21 namespace OHOS {
22 namespace AbilityRuntime {
InsightIntentExecutorMgr()23 InsightIntentExecutorMgr::InsightIntentExecutorMgr()
24 {
25 TAG_LOGD(AAFwkTag::INTENT, "called");
26 }
27
~InsightIntentExecutorMgr()28 InsightIntentExecutorMgr::~InsightIntentExecutorMgr()
29 {
30 TAG_LOGI(AAFwkTag::INTENT, "called");
31 }
32
ExecuteInsightIntent(Runtime & runtime,const InsightIntentExecutorInfo & executeInfo,std::unique_ptr<InsightIntentExecutorAsyncCallback> callback)33 bool InsightIntentExecutorMgr::ExecuteInsightIntent(Runtime& runtime, const InsightIntentExecutorInfo& executeInfo,
34 std::unique_ptr<InsightIntentExecutorAsyncCallback> callback)
35 {
36 TAG_LOGD(AAFwkTag::INTENT, "called");
37 auto executeParam = executeInfo.executeParam;
38 if (executeParam == nullptr || executeParam->insightIntentParam_ == nullptr) {
39 TAG_LOGE(AAFwkTag::INTENT, "null executeParam or insightIntentParam_");
40 TriggerCallbackInner(std::move(callback), static_cast<int32_t>(AbilityErrorCode::ERROR_CODE_INVALID_PARAM));
41 return false;
42 }
43
44 auto asyncCallback =
45 [weak = weak_from_this(), intentId = executeParam->insightIntentId_](InsightIntentExecuteResult result) {
46 // erase map when called
47 TAG_LOGD(AAFwkTag::INTENT, "called");
48 auto executorMgr = weak.lock();
49 if (executorMgr == nullptr) {
50 TAG_LOGE(AAFwkTag::INTENT, "null executorMgr");
51 return;
52 }
53 executorMgr->RemoveInsightIntentExecutor(intentId);
54 };
55 callback->Push(asyncCallback);
56
57 // Create insight intent executor
58 auto intentExecutor = InsightIntentExecutor::Create(runtime);
59 if (intentExecutor == nullptr) {
60 TAG_LOGE(AAFwkTag::INTENT, "null intentExecutor");
61 TriggerCallbackInner(std::move(callback), static_cast<int32_t>(AbilityErrorCode::ERROR_CODE_INVALID_PARAM));
62 return false;
63 }
64
65 if (!intentExecutor->Init(executeInfo)) {
66 TAG_LOGE(AAFwkTag::INTENT, "Init intent executor failed");
67 TriggerCallbackInner(std::move(callback), static_cast<int32_t>(AbilityErrorCode::ERROR_CODE_INVALID_PARAM));
68 return false;
69 }
70 AddInsightIntentExecutor(executeParam->insightIntentId_, intentExecutor);
71
72 bool isAsync = false;
73 auto ret = intentExecutor->HandleExecuteIntent(static_cast<InsightIntentExecuteMode>(executeParam->executeMode_),
74 executeParam->insightIntentName_, *executeParam->insightIntentParam_, executeInfo.pageLoader,
75 std::move(callback), isAsync);
76 if (!ret) {
77 TAG_LOGE(AAFwkTag::INTENT, "Handle Execute intent failed");
78 // callback has removed, if execute insight intent failed, call in sub function.
79 return false;
80 }
81
82 return true;
83 }
84
AddInsightIntentExecutor(uint64_t intentId,const std::shared_ptr<InsightIntentExecutor> & executor)85 void InsightIntentExecutorMgr::AddInsightIntentExecutor(uint64_t intentId,
86 const std::shared_ptr<InsightIntentExecutor>& executor)
87 {
88 TAG_LOGD(AAFwkTag::INTENT, "called");
89 std::lock_guard<std::mutex> lock(mutex_);
90 insightIntentExecutors_[intentId] = executor;
91 }
92
RemoveInsightIntentExecutor(uint64_t intentId)93 void InsightIntentExecutorMgr::RemoveInsightIntentExecutor(uint64_t intentId)
94 {
95 TAG_LOGD(AAFwkTag::INTENT, "called");
96 std::lock_guard<std::mutex> lock(mutex_);
97 insightIntentExecutors_.erase(intentId);
98 }
99
TriggerCallbackInner(std::unique_ptr<InsightIntentExecutorAsyncCallback> callback,int32_t errCode)100 void InsightIntentExecutorMgr::TriggerCallbackInner(
101 std::unique_ptr<InsightIntentExecutorAsyncCallback> callback, int32_t errCode)
102 {
103 TAG_LOGD(AAFwkTag::INTENT, "called");
104 AppExecFwk::InsightIntentExecuteResult result;
105 result.innerErr = errCode;
106 callback->Call(result);
107 callback.reset();
108 }
109 } // namespace AbilityRuntime
110 } // namespace OHOS
111