1 /*
2  * Copyright (c) 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 #include "insight_intent_host_client.h"
16 #include "hilog_tag_wrapper.h"
17 
18 namespace OHOS {
19 namespace AbilityRuntime {
20 sptr<InsightIntentHostClient> InsightIntentHostClient::instance_ = nullptr;
21 std::mutex InsightIntentHostClient::instanceMutex_;
22 std::once_flag InsightIntentHostClient::singletonFlag_;
23 
GetInstance()24 sptr<InsightIntentHostClient> InsightIntentHostClient::GetInstance()
25 {
26     std::call_once(singletonFlag_, []() {
27         instance_ = new (std::nothrow) InsightIntentHostClient();
28         if (instance_ == nullptr) {
29             TAG_LOGE(AAFwkTag::INTENT, "failed to create InsightIntentHostClient.");
30         }
31     });
32     return instance_;
33 }
34 
AddInsightIntentExecute(const std::shared_ptr<InsightIntentExecuteCallbackInterface> & callback)35 uint64_t InsightIntentHostClient::AddInsightIntentExecute(
36     const std::shared_ptr<InsightIntentExecuteCallbackInterface> &callback)
37 {
38     TAG_LOGD(AAFwkTag::INTENT, "called");
39     std::lock_guard<std::mutex> lock(insightIntentExecutebackMutex_);
40     callbackMap_.emplace(++key_, callback);
41     return key_;
42 }
43 
RemoveInsightIntentExecute(uint64_t key)44 void InsightIntentHostClient::RemoveInsightIntentExecute(uint64_t key)
45 {
46     TAG_LOGD(AAFwkTag::INTENT, "called");
47     std::lock_guard<std::mutex> lock(insightIntentExecutebackMutex_);
48     auto iter = callbackMap_.find(key);
49     if (iter != callbackMap_.end()) {
50         callbackMap_.erase(key);
51     }
52 }
53 
OnExecuteDone(uint64_t key,int32_t resultCode,const AppExecFwk::InsightIntentExecuteResult & executeResult)54 void InsightIntentHostClient::OnExecuteDone(uint64_t key, int32_t resultCode,
55     const AppExecFwk::InsightIntentExecuteResult &executeResult)
56 {
57     TAG_LOGD(AAFwkTag::INTENT, "called");
58 
59     std::shared_ptr<InsightIntentExecuteCallbackInterface> callback = nullptr;
60     {
61         std::lock_guard<std::mutex> lock(insightIntentExecutebackMutex_);
62         auto iter = callbackMap_.find(key);
63         if (iter == callbackMap_.end()) {
64             TAG_LOGI(AAFwkTag::INTENT, "InsightIntent execute callback not found");
65         } else {
66             callback = iter->second;
67             callbackMap_.erase(key);
68         }
69     }
70 
71     if (callback != nullptr) {
72         callback->ProcessInsightIntentExecute(resultCode, executeResult);
73     }
74 }
75 } // namespace AbilityRuntime
76 } // namespace OHOS