1 /*
2  * Copyright (c) 2023 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 #ifndef OHOS_ABILITY_RUNTIME_INSIGHT_INTENT_HOST_CLIENT_H
17 #define OHOS_ABILITY_RUNTIME_INSIGHT_INTENT_HOST_CLIENT_H
18 
19 #include <map>
20 #include <memory>
21 #include <mutex>
22 #include "insight_intent_callback_interface.h"
23 #include "insight_intent_execute_callback_stub.h"
24 
25 namespace OHOS {
26 namespace AbilityRuntime {
27 class InsightIntentHostClient : public AAFwk::InsightIntentExecuteCallbackStub {
28 public:
29     InsightIntentHostClient() = default;
30     virtual ~InsightIntentHostClient() = default;
31 
32     /**
33      * @brief Get InsightIntentHostClient instance.
34      * @return InsightIntentHostClient instance.
35      */
36     static sptr<InsightIntentHostClient> GetInstance();
37 
38     /**
39      * @brief Add InsightIntent host.
40      * @param callback the host of the InsightIntent executing.
41      * @return Returns the execute callback key.
42      */
43     uint64_t AddInsightIntentExecute(const std::shared_ptr<InsightIntentExecuteCallbackInterface> &callback);
44 
45     /**
46      * @brief Remove InsightIntent host.
47      * @param key, the execute callback key.
48      */
49     void RemoveInsightIntentExecute(uint64_t key);
50 
51     /**
52      * @brief Indicates that the InsightIntent executing is complete
53      * @param key, the execute callback key.
54      * @param resultCode, ERR_OK on success, others on failure.
55      * @param executeResult, the execute result.
56      */
57     void OnExecuteDone(uint64_t key, int32_t resultCode,
58         const AppExecFwk::InsightIntentExecuteResult &executeResult) override;
59 
60 private:
61     static std::mutex instanceMutex_;
62     static std::once_flag singletonFlag_;
63     uint64_t key_ = 0;
64     static sptr<InsightIntentHostClient> instance_;
65     mutable std::mutex insightIntentExecutebackMutex_;
66     std::map<uint64_t, std::shared_ptr<InsightIntentExecuteCallbackInterface>> callbackMap_;
67     DISALLOW_COPY_AND_MOVE(InsightIntentHostClient);
68 };
69 } // namespace AbilityRuntime
70 } // namespace OHOS
71 #endif // OHOS_ABILITY_RUNTIME_INSIGHT_INTENT_HOST_CLIENT_H