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_FORM_FWK_FORM_PROVIDER_CLIENT_H
17 #define OHOS_FORM_FWK_FORM_PROVIDER_CLIENT_H
18 
19 #include <iremote_object.h>
20 #include <iremote_stub.h>
21 #include <map>
22 #include "ability.h"
23 #include "form_constants.h"
24 #include "form_js_info.h"
25 #include "form_provider_info.h"
26 #include "form_provider_stub.h"
27 
28 namespace OHOS {
29 namespace AppExecFwk {
30 /**
31  * @class FormProviderStub
32  * The service of the form provider.
33  */
34 class FormProviderClient : public FormProviderStub {
35 public:
36     FormProviderClient() = default;
37     virtual ~FormProviderClient() = default;
38 
39     /**
40      * @brief Acquire to give back an ProviderFormInfo. This is sync API.
41      * @param formJsInfo The form js info.
42      * @param want Indicates the {@link Want} structure containing form info.
43      * @param callerToken Caller ability token.
44      * @return Returns ERR_OK on success, others on failure.
45      */
46     int AcquireProviderFormInfo(const FormJsInfo &formJsInfo, const Want &want,
47         const sptr<IRemoteObject> &callerToken) override;
48 
49     /**
50      * @brief Notify provider when the form was deleted.
51      *
52      * @param formId The Id of the form.
53      * @param want Indicates the structure containing form info.
54      * @param callerToken Caller ability token.
55      * @return Returns ERR_OK on success, others on failure.
56      */
57     int NotifyFormDelete(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken) override;
58 
59     /**
60      * @brief Notify provider when the form was deleted.
61      *
62      * @param formIds The id list of forms.
63      * @param want Indicates the structure containing form info.
64      * @param callerToken Caller ability token.
65      * @return Returns ERR_OK on success, others on failure.
66      */
67     int NotifyFormsDelete(const std::vector<int64_t> &formIds, const Want &want,
68         const sptr<IRemoteObject> &callerToken) override;
69 
70     /**
71      * @brief Notify provider when the form need update.
72      *
73      * @param formId The Id of the form.
74      * @param want Indicates the structure containing form info.
75      * @param callerToken Caller ability token.
76      * @return Returns ERR_OK on success, others on failure.
77      */
78     int NotifyFormUpdate(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken) override;
79 
80     /**
81      * @brief Event notify when change the form visible.
82      *
83      * @param formIds The vector of form ids.
84      * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE.
85      * @param want Indicates the structure containing form info.
86      * @param callerToken Caller ability token.
87      * @return Returns ERR_OK on success, others on failure.
88      */
89     int EventNotify(const std::vector<int64_t> &formIds, const int32_t formVisibleType, const Want &want,
90         const sptr<IRemoteObject> &callerToken) override;
91 
92     /**
93      * @brief Notify provider when the temp form was cast to normal form.
94      *
95      * @param formId The Id of the form to update.
96      * @param want Indicates the structure containing form info.
97      * @param callerToken Caller ability token.
98      * @return Returns ERR_OK on success, others on failure.
99      */
100     int NotifyFormCastTempForm(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken) override;
101 
102     /**
103      * @brief Fire message event to form provider.
104      * @param formId The Id of the from.
105      * @param message Event message.
106      * @param want The want of the request.
107      * @param callerToken Form provider proxy object.
108      * @return Returns ERR_OK on success, others on failure.
109      */
110     int FireFormEvent(const int64_t formId, const std::string &message, const Want &want,
111         const sptr<IRemoteObject> &callerToken) override;
112 
113     /**
114      * @brief Acquire form state to form provider.
115      * @param wantArg The want of onAcquireFormState.
116      * @param provider The provider info.
117      * @param want The want of the request.
118      * @param callerToken Form provider proxy object.
119      * @return Returns ERR_OK on success, others on failure.
120      */
121     int AcquireState(const Want &wantArg, const std::string &provider, const Want &want,
122         const sptr<IRemoteObject> &callerToken) override;
123 
124     /**
125      * @brief Acquire to share form information data. This is sync API.
126      * @param formId The Id of the form.
127      * @param remoteDeviceId Indicates the remote device ID.
128      * @param formSupplyCallback Indicates lifecycle callbacks.
129      * @param requestCode Indicates the request code of this share form.
130      * @return Returns ERR_OK on success, others on failure.
131      */
132     int32_t AcquireShareFormData(int64_t formId, const std::string &remoteDeviceId,
133         const sptr<IRemoteObject> &formSupplyCallback, int64_t requestCode) override;
134 
135     /**
136      * @brief Acquire form data.
137      * @param formId The Id of the form.
138      * @param formSupplyCallback Indicates lifecycle callbacks.
139      * @param requestCode Indicates the request code.
140      * @return Returns ERR_OK on success, others on failure.
141      */
AcquireFormData(int64_t formId,const sptr<IRemoteObject> & formSupplyCallback,int64_t requestCode)142     int32_t AcquireFormData(int64_t formId, const sptr<IRemoteObject> &formSupplyCallback,
143         int64_t requestCode) override
144     {
145         return ERR_OK;
146     }
147 
148     /**
149      * @brief Set the owner ability of the form provider client.
150      *
151      * @param ability The owner ability of the form provider client.
152      * @return none.
153      */
154     void SetOwner(const std::shared_ptr<Ability> ability);
155 
156     /**
157      * @brief Clear the owner ability of the form provider client.
158      *
159      * @param ability The owner ability of the form provider client.
160      * @return none.
161      */
162     void ClearOwner(const std::shared_ptr<Ability> ability);
163 
164 protected:
165     bool CheckIsSystemApp() const;
166     int HandleDisconnect(const Want &want, const sptr<IRemoteObject> &callerToken);
167     int HandleAcquire(const FormProviderInfo &formProviderInfo, const Want &newWant,
168         const sptr<IRemoteObject> &callerToken);
169     int HandleAcquireStateResult(FormState state, const std::string &provider, const Want &wantArg, const Want &want,
170                                  const sptr<IRemoteObject> &callerToken);
171     void HandleRemoteAcquire(const FormJsInfo &formJsInfo, const FormProviderInfo &formProviderInfo,
172         const Want &want, const sptr<IRemoteObject> &token);
173     bool IsCallBySelfBundle();
174 
175 private:
176     std::shared_ptr<Ability> GetOwner();
177     /**
178      * @brief Eliminates duplicate code of returning functional error code and error code of handling disconnection.
179      * This function will call HandleDisconnect() and return functional errorCode when possible, only return error
180      * code of HandleDisconnect() if there's no functional error.
181      * @param errCode The functional error the caller has.
182      * @param want The input parameter of the caller function and is required by HandleDisconnect().
183      * @param callerToken callerToken
184      * @return int32_t The error code (functional/HandleDisconnect).
185      */
186     int DCRtnHelper(const int &errCode, const Want &want, const sptr<IRemoteObject> &callerToken);
187 
188 private:
189     DISALLOW_COPY_AND_MOVE(FormProviderClient);
190     mutable std::mutex abilityMutex_;
191     std::weak_ptr<Ability> owner_;
192 };
193 } // namespace AppExecFwk
194 } // namespace OHOS
195 #endif // OHOS_FORM_FWK_FORM_PROVIDER_CLIENT_H
196