• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     auto executeParam = executeInfo.executeParam;
37     if (callback == nullptr) {
38         TAG_LOGW(AAFwkTag::INTENT, "null callback");
39         return false;
40     }
41     if (executeParam == nullptr || executeParam->insightIntentParam_ == nullptr) {
42         TAG_LOGE(AAFwkTag::INTENT, "null executeParam or insightIntentParam_");
43         TriggerCallbackInner(std::move(callback), static_cast<int32_t>(AbilityErrorCode::ERROR_CODE_INVALID_PARAM));
44         return false;
45     }
46 
47     auto asyncCallback =
48         [weak = weak_from_this(), intentId = executeParam->insightIntentId_](InsightIntentExecuteResult result) {
49             // erase map when called
50             TAG_LOGD(AAFwkTag::INTENT, "called");
51             auto executorMgr = weak.lock();
52             if (executorMgr == nullptr) {
53                 TAG_LOGE(AAFwkTag::INTENT, "null executorMgr");
54                 return;
55             }
56             executorMgr->RemoveInsightIntentExecutor(intentId);
57         };
58     callback->Push(asyncCallback);
59 
60     // Create insight intent executor
61     InsightIntentType type = static_cast<InsightIntentType>(executeParam->decoratorType_);
62     auto intentExecutor = InsightIntentExecutor::Create(runtime, type);
63     if (intentExecutor == nullptr) {
64         TAG_LOGE(AAFwkTag::INTENT, "null intentExecutor");
65         TriggerCallbackInner(std::move(callback), static_cast<int32_t>(AbilityErrorCode::ERROR_CODE_INVALID_PARAM));
66         return false;
67     }
68 
69     if (!intentExecutor->Init(executeInfo)) {
70         TAG_LOGE(AAFwkTag::INTENT, "Init intent executor failed");
71         TriggerCallbackInner(std::move(callback), static_cast<int32_t>(AbilityErrorCode::ERROR_CODE_INVALID_PARAM));
72         return false;
73     }
74     AddInsightIntentExecutor(executeParam->insightIntentId_, intentExecutor);
75 
76     TAG_LOGI(AAFwkTag::INTENT, "execute insight intent, type: %{public}hhu", type);
77     bool isAsync = false;
78     bool ret = false;
79     if (runtime.GetLanguage() == AbilityRuntime::Runtime::Language::ETS) {
80         ret = intentExecutor->HandleExecuteIntent(executeParam, executeInfo.etsPageLoader,
81             std::move(callback), isAsync);
82     } else {
83         ret = intentExecutor->HandleExecuteIntent(executeParam, executeInfo.pageLoader, std::move(callback), isAsync);
84     }
85     if (!ret) {
86         TAG_LOGE(AAFwkTag::INTENT, "Handle Execute intent failed");
87         // callback has removed, if execute insight intent failed, call in sub function.
88         return false;
89     }
90 
91     return true;
92 }
93 
AddInsightIntentExecutor(uint64_t intentId,const std::shared_ptr<InsightIntentExecutor> & executor)94 void InsightIntentExecutorMgr::AddInsightIntentExecutor(uint64_t intentId,
95     const std::shared_ptr<InsightIntentExecutor>& executor)
96 {
97     TAG_LOGD(AAFwkTag::INTENT, "called");
98     std::lock_guard<std::mutex> lock(mutex_);
99     insightIntentExecutors_[intentId] = executor;
100 }
101 
RemoveInsightIntentExecutor(uint64_t intentId)102 void InsightIntentExecutorMgr::RemoveInsightIntentExecutor(uint64_t intentId)
103 {
104     TAG_LOGD(AAFwkTag::INTENT, "called");
105     std::lock_guard<std::mutex> lock(mutex_);
106     insightIntentExecutors_.erase(intentId);
107 }
108 
TriggerCallbackInner(std::unique_ptr<InsightIntentExecutorAsyncCallback> callback,int32_t errCode)109 void InsightIntentExecutorMgr::TriggerCallbackInner(
110     std::unique_ptr<InsightIntentExecutorAsyncCallback> callback, int32_t errCode)
111 {
112     TAG_LOGD(AAFwkTag::INTENT, "called");
113     AppExecFwk::InsightIntentExecuteResult result;
114     result.innerErr = errCode;
115     if (callback) {
116         callback->Call(result);
117     }
118     callback.reset();
119 }
120 } // namespace AbilityRuntime
121 } // namespace OHOS
122