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_EXECUTE_MANAGER_H
17 #define OHOS_ABILITY_RUNTIME_INSIGHT_INTENT_EXECUTE_MANAGER_H
18 
19 #include <map>
20 #include "ability_connect_callback_stub.h"
21 #include "cpp/mutex.h"
22 #include "insight_intent_execute_param.h"
23 #include "insight_intent_execute_result.h"
24 #include "iremote_object.h"
25 #include "singleton.h"
26 
27 namespace OHOS {
28 namespace AAFwk {
29 enum class InsightIntentExecuteState {
30     UNKNOWN = 0,
31     EXECUTING,
32     EXECUTE_DONE,
33     REMOTE_DIED
34 };
35 
36 struct InsightIntentExecuteRecord {
37     uint64_t key = 0;
38     sptr<IRemoteObject> callerToken = nullptr;
39     sptr<IRemoteObject::DeathRecipient> deathRecipient = nullptr;
40     InsightIntentExecuteState state = InsightIntentExecuteState::UNKNOWN;
41     std::string bundleName;
42 };
43 
44 class InsightIntentExecuteConnection : public AbilityConnectionStub {
45 public:
46     InsightIntentExecuteConnection() = default;
47 
48     ~InsightIntentExecuteConnection() override = default;
49 
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)50     void OnAbilityConnectDone(
51         const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode) override
52     {}
53 
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)54     void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override
55     {}
56 };
57 
58 class InsightIntentExecuteRecipient : public IRemoteObject::DeathRecipient {
59 public:
InsightIntentExecuteRecipient(uint64_t intentId)60     explicit InsightIntentExecuteRecipient(uint64_t intentId) : intentId_(intentId)
61     {}
62 
63     ~InsightIntentExecuteRecipient() override = default;
64 
65     void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
66 
67 private:
68     uint64_t intentId_ = 0;
69 };
70 
71 class InsightIntentExecuteManager : public std::enable_shared_from_this<InsightIntentExecuteManager> {
72 DECLARE_DELAYED_SINGLETON(InsightIntentExecuteManager)
73 public:
74     int32_t CheckAndUpdateParam(uint64_t key, const sptr<IRemoteObject> &callerToken,
75         const std::shared_ptr<AppExecFwk::InsightIntentExecuteParam> &param);
76 
77     int32_t CheckAndUpdateWant(Want &want, AppExecFwk::ExecuteMode executeMode);
78 
79     int32_t RemoveExecuteIntent(uint64_t intentId);
80 
81     int32_t ExecuteIntentDone(uint64_t intentId, int32_t resultCode,
82         const AppExecFwk::InsightIntentExecuteResult &result);
83 
84     int32_t RemoteDied(uint64_t intentId);
85 
86     int32_t GetBundleName(uint64_t intentId, std::string &bundleName) const;
87 
88     static int32_t GenerateWant(const std::shared_ptr<AppExecFwk::InsightIntentExecuteParam> &param, Want &want);
89 
90 private:
91     mutable ffrt::mutex mutex_;
92     uint64_t intentIdCount_ = 0;
93     std::map<uint64_t, std::shared_ptr<InsightIntentExecuteRecord>> records_;
94 
95     int32_t AddRecord(uint64_t key, const sptr<IRemoteObject> &callerToken, const std::string &bundleName,
96         uint64_t &intentId);
97 
98     static int32_t IsValidCall(const Want &want);
99 
100     static int32_t CheckCallerPermission();
101 };
102 } // namespace AAFwk
103 } // namespace OHOS
104 #endif // OHOS_ABILITY_RUNTIME_INSIGHT_INTENT_EXECUTE_MANAGER_H
105