1 /*
2  * Copyright (c) 2021-2022 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_FORM_FWK_FORM_ABILITY_CONNECTION_H
17 #define OHOS_FORM_FWK_FORM_ABILITY_CONNECTION_H
18 
19 #include "app_mgr_interface.h"
20 #include "provider_connect_stub.h"
21 #include "want.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 using WantParams = OHOS::AAFwk::WantParams;
26 
27 /**
28  * @class FormAbilityConnection
29  * Form Ability Connection Stub.
30  */
31 class FormAbilityConnection : public ProviderConnectStub {
32 public:
33     FormAbilityConnection() = default;
34     virtual ~FormAbilityConnection() = default;
35 
36     /**
37      * @brief OnAbilityConnectDone, AbilityMs notify caller ability the result of connect.
38      * @param element service ability's ElementName.
39      * @param remoteObject the session proxy of service ability.
40      * @param resultCode ERR_OK on success, others on failure.
41      */
42     virtual void OnAbilityConnectDone(
43         const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode) override;
44 
45     /**
46      * @brief OnAbilityDisconnectDone, AbilityMs notify caller ability the result of disconnect.
47      * @param element service ability's ElementName.
48      * @param resultCode ERR_OK on success, others on failure.
49      */
50     virtual void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override;
51 
52     /**
53      * @brief remote object died event.
54      * @param remoteObject the remote object of service ability.
55      */
56     void OnConnectDied(const wptr<IRemoteObject> &remoteObject);
57 
58     /**
59      * @brief onFormAppConnect, when ability connetDone.
60      * @return result of onFormAppConnect.
61     */
62     bool onFormAppConnect();
63 
64     /**
65      * @brief Report Hisys FormApp UnbindEvent.
66     */
67     void ReportFormAppUnbindEvent();
68 
69     /**
70      * @brief Get connectId.
71      * @return The ability connection id.
72      */
73     int32_t GetConnectId() const;
74 
75     /**
76      * @brief Set connectId.
77      * @param connectId The ability connection id.
78      */
79     void SetConnectId(int32_t connectId);
80     /**
81      * @brief Get the provider Key
82      *
83      * @return The provider Key
84      */
85     std::string GetProviderKey() const;
86     /**
87      * @brief Set the Provider Key
88      *
89      * @param bundleName bundleName
90      * @param abilityName abilityName
91      */
92     void SetProviderKey(const std::string &bundleName, const std::string &abilityName);
93 
94     /**
95      * @brief Set free install true or false.
96      *
97      * @param isFreeInstall Indicates the free install flag is true or false.
98      */
99     void SetFreeInstall(bool isFreeInstall);
100 
101     /**
102      * @brief Set form ID.
103      *
104      * @param formId Indicates the form ID.
105      */
106     void SetFormId(int64_t formId);
107 
108     /**
109      * @brief Get form ID.
110      */
111     int64_t GetFormId() const;
112 
113     /**
114      * @brief Set host token.
115      * @param hostToken Indicates the host token.
116      */
117     void SetHostToken(const sptr<IRemoteObject> hostToken);
118 
119     /**
120      * @brief Get host token.
121      */
122     sptr<IRemoteObject> GetHostToken() const;
123 
124     /**
125      * @brief Set provider token.
126      * @param hostToken Indicates the provider token.
127      */
128     void SetProviderToken(const sptr<IRemoteObject> providerToken);
129 
130     /**
131      * @brief Get provider token.
132      */
133     sptr<IRemoteObject> GetProviderToken() const;
134 
135     /**
136      * @brief Add death recipient.
137      */
138     void AddHostDeathRecipient();
139 
140     /**
141      * @brief Get bundle name.
142      */
143     std::string GetBundleName();
144 
145     /**
146      * @brief Get process pid.
147      */
148     int32_t GetAppFormPid();
149 
150 private:
151     /**
152      * @brief Get app manager proxy.
153      * @return App manager proxy.
154      */
155     sptr<OHOS::AppExecFwk::IAppMgr> GetAppMgr();
156 
157     int64_t formId_ = -1;
158     std::string deviceId_ = "";
159     std::string bundleName_ = "";
160     std::string abilityName_ = "";
161     bool isFreeInstall_ = false;
162     int32_t connectId_ = 0;
163     int32_t appFormPid_ = -1;
164     sptr<IRemoteObject> hostToken_ = nullptr;
165     sptr<IRemoteObject> providerToken_ = nullptr;
166 
167     DISALLOW_COPY_AND_MOVE(FormAbilityConnection);
168 };
169 }  // namespace AppExecFwk
170 }  // namespace OHOS
171 
172 #endif // OHOS_FORM_FWK_FORM_ABILITY_CONNECTION_H
173