• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_MGR_SERVICE_H
17 #define OHOS_FORM_FWK_FORM_MGR_SERVICE_H
18 
19 #include <singleton.h>
20 #include <system_ability.h>
21 
22 #include "form_bundle_event_callback.h"
23 #include "form_event_handler.h"
24 #include "form_mgr_stub.h"
25 #include "form_provider_data.h"
26 #include "form_sys_event_receiver.h"
27 #include "iremote_object.h"
28 namespace OHOS {
29 namespace AppExecFwk {
30 enum class ServiceRunningState {
31     STATE_NOT_START,
32     STATE_RUNNING,
33 };
34 /**
35  * @class FormMgrService
36  * FormMgrService provides a facility for managing form life cycle.
37  */
38 class FormMgrService : public SystemAbility,
39                        public FormMgrStub,
40                        public std::enable_shared_from_this<FormMgrService> {
41     DECLARE_DELAYED_SINGLETON(FormMgrService);
42     DECLEAR_SYSTEM_ABILITY(FormMgrService);
43 public:
44     /**
45      * @brief Start event for the form manager service.
46      */
47     void OnStart() override;
48     /**
49      * @brief Stop event for the form manager service.
50      */
51     void OnStop() override;
52 
53     /**
54      * @brief Add form with want, send want to form manager service.
55      * @param formId The Id of the forms to add.
56      * @param want The want of the form to add.
57      * @param callerToken Caller ability token.
58      * @param formInfo Form info.
59      * @return Returns ERR_OK on success, others on failure.
60      */
61     int AddForm(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken,
62         FormJsInfo &formInfo) override;
63 
64     /**
65      * @brief Delete forms with formIds, send formIds to form manager service.
66      * @param formId The Id of the forms to delete.
67      * @param callerToken Caller ability token.
68      * @return Returns ERR_OK on success, others on failure.
69      */
70     int DeleteForm(const int64_t formId, const sptr<IRemoteObject> &callerToken) override;
71 
72     /**
73      * @brief Stop rendering form.
74      * @param formId The Id of the forms to delete.
75      * @param compId The compId of the forms to delete.
76      * @return Returns ERR_OK on success, others on failure.
77      */
78     int StopRenderingForm(const int64_t formId, const std::string &compId) override;
79 
80     /**
81      * @brief Release forms with formIds, send formIds to form manager service.
82      * @param formId The Id of the forms to release.
83      * @param callerToken Caller ability token.
84      * @param delCache Delete Cache or not.
85      * @return Returns ERR_OK on success, others on failure.
86      */
87     int ReleaseForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const bool delCache) override;
88 
89     /**
90      * @brief Update form with formId, send formId to form manager service.
91      * @param formId The Id of the form to update.
92      * @param bundleName Provider ability bundleName.
93      * @param FormProviderData Form binding data.
94      * @return Returns ERR_OK on success, others on failure.
95      */
96     int UpdateForm(const int64_t formId, const FormProviderData &FormProviderData) override;
97 
98     /**
99      * @brief set next refresh time.
100      * @param formId The id of the form.
101      * @param nextTime next refresh time.
102      * @return Returns ERR_OK on success, others on failure.
103      */
104     int SetNextRefreshTime(const int64_t formId, const int64_t nextTime) override;
105 
106     /**
107      * @brief Request to publish a form to the form host.
108      *
109      * @param want The want of the form to publish.
110      * @param withFormBindingData Indicates whether the formBindingData is carried with.
111      * @param formBindingData Indicates the form data.
112      * @param formId Return the form id to be published.
113      * @return Returns ERR_OK on success, others on failure.
114      */
115     ErrCode RequestPublishForm(Want &want, bool withFormBindingData,
116                                std::unique_ptr<FormProviderData> &formBindingData, int64_t &formId) override;
117 
118     /**
119      * @brief lifecycle update.
120      * @param formIds formIds of host client.
121      * @param callerToken Caller ability token.
122      * @param updateType update type, enable if true and disable if false.
123      * @return Returns true on success, false on failure.
124      */
125     int LifecycleUpdate(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken,
126         bool updateType) override;
127 
128     /**
129      * @brief Request form with formId and want, send formId and want to form manager service.
130      * @param formId The Id of the form to update.
131      * @param callerToken Caller ability token.
132      * @param want The want of the form to add.
133      * @return Returns ERR_OK on success, others on failure.
134      */
135     int RequestForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const Want &want) override;
136 
137     /**
138      * @brief Form visible/invisible notify, send formIds to form manager service.
139      * @param formIds The Id list of the forms to notify.
140      * @param callerToken Caller ability token.
141      * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE.
142      * @return Returns ERR_OK on success, others on failure.
143      */
144     int NotifyWhetherVisibleForms(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken,
145         const int32_t formVisibleType) override;
146 
147     /**
148      * @brief temp form to normal form.
149      * @param formId The Id of the form.
150      * @param callerToken Caller ability token.
151      * @return Returns ERR_OK on success, others on failure.
152      */
153     int CastTempForm(const int64_t formId, const sptr<IRemoteObject> &callerToken) override;
154 
155     /**
156      * @brief Dump all of form storage infos.
157      * @param formInfos All of form storage infos.
158      * @return Returns ERR_OK on success, others on failure.
159      */
160     int DumpStorageFormInfos(std::string &formInfos) override;
161     /**
162      * @brief Dump form info by a bundle name.
163      * @param bundleName The bundle name of form provider.
164      * @param formInfos Form infos.
165      * @return Returns ERR_OK on success, others on failure.
166      */
167     int DumpFormInfoByBundleName(const std::string &bundleName, std::string &formInfos) override;
168     /**
169      * @brief Dump form info by a bundle name.
170      * @param formId The id of the form.
171      * @param formInfo Form info.
172      * @return Returns ERR_OK on success, others on failure.
173      */
174     int DumpFormInfoByFormId(const std::int64_t formId, std::string &formInfo) override;
175     /**
176      * @brief Dump form timer by form id.
177      * @param formId The id of the form.
178      * @param formInfo Form info.
179      * @return Returns ERR_OK on success, others on failure.
180      */
181     int DumpFormTimerByFormId(const std::int64_t formId, std::string &isTimingService) override;
182     /**
183      * @brief Process js message event.
184      * @param formId Indicates the unique id of form.
185      * @param want information passed to supplier.
186      * @param callerToken Caller ability token.
187      * @return Returns true if execute success, false otherwise.
188      */
189     int MessageEvent(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken) override;
190 
191     /**
192      * @brief Process js router event.
193      * @param formId Indicates the unique id of form.
194      * @param want the want of the ability to start.
195      * @param callerToken Caller ability token.
196      * @return Returns true if execute success, false otherwise.
197      */
198     int RouterEvent(const int64_t formId, Want &want, const sptr<IRemoteObject> &callerToken) override;
199 
200     /**
201      * @brief Process Background event.
202      * @param formId Indicates the unique id of form.
203      * @param want the want of the ability to start.
204      * @param callerToken Caller ability token.
205      * @return Returns true if execute success, false otherwise.
206      */
207     int BackgroundEvent(const int64_t formId, Want &want, const sptr<IRemoteObject> &callerToken) override;
208 
209     /**
210      * @brief Check whether if the form manager service is ready.
211      * @return Returns true if the form manager service is ready; returns false otherwise.
212      */
213     bool IsReady() const;
214 
215     /**
216      * @brief Delete the invalid forms.
217      * @param formIds Indicates the ID of the valid forms.
218      * @param callerToken Caller ability token.
219      * @param numFormsDeleted Returns the number of the deleted forms.
220      * @return Returns ERR_OK on success, others on failure.
221      */
222     virtual int DeleteInvalidForms(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken,
223                                    int32_t &numFormsDeleted) override;
224 
225     /**
226      * @brief Acquire form state info by passing a set of parameters (using Want) to the form provider.
227      * @param want Indicates a set of parameters to be transparently passed to the form provider.
228      * @param callerToken Caller ability token.
229      * @param stateInfo Returns the form's state info of the specify.
230      * @return Returns ERR_OK on success, others on failure.
231      */
232     virtual int AcquireFormState(const Want &want, const sptr<IRemoteObject> &callerToken,
233                                  FormStateInfo &stateInfo) override;
234 
235     /**
236      * @brief Notify the form is visible or not.
237      * @param formIds Indicates the ID of the forms.
238      * @param isVisible Visible or not.
239      * @param callerToken Host client.
240      * @return Returns ERR_OK on success, others on failure.
241      */
242     virtual int NotifyFormsVisible(const std::vector<int64_t> &formIds, bool isVisible,
243                                    const sptr<IRemoteObject> &callerToken) override;
244 
245     /**
246      * @brief Notify the form is privacy protected or not.
247      * @param formIds Indicates the ID of the forms.
248      * @param isProtected isProtected or not.
249      * @param callerToken Host client.
250      * @return Returns ERR_OK on success, others on failure.
251      */
252     int NotifyFormsPrivacyProtected(const std::vector<int64_t> &formIds, bool isProtected,
253                                             const sptr<IRemoteObject> &callerToken) override;
254 
255     /**
256      * @brief Notify the form is enable to be updated or not.
257      * @param formIds Indicates the ID of the forms.
258      * @param isEnableUpdate enable update or not.
259      * @param callerToken Host client.
260      * @return Returns ERR_OK on success, others on failure.
261      */
262     virtual int NotifyFormsEnableUpdate(const std::vector<int64_t> &formIds, bool isEnableUpdate,
263                                         const sptr<IRemoteObject> &callerToken) override;
264 
265     /**
266      * @brief Get All FormsInfo.
267      * @param formInfos Return the forms' information of all forms provided.
268      * @return Returns ERR_OK on success, others on failure.
269      */
270     int GetAllFormsInfo(std::vector<FormInfo> &formInfos) override;
271 
272     /**
273      * @brief Get forms info by bundle name .
274      * @param bundleName Application name.
275      * @param formInfos Return the forms' information of the specify application name.
276      * @return Returns ERR_OK on success, others on failure.
277      */
278     int GetFormsInfoByApp(std::string &bundleName, std::vector<FormInfo> &formInfos) override;
279 
280     /**
281      * @brief Get forms info by bundle name and module name.
282      * @param bundleName bundle name.
283      * @param moduleName Module name of hap.
284      * @param formInfos Return the forms' information of the specify bundle name and module name.
285      * @return Returns ERR_OK on success, others on failure.
286      */
287     int GetFormsInfoByModule(std::string &bundleName, std::string &moduleName,
288                              std::vector<FormInfo> &formInfos) override;
289 
290     /**
291     * @brief This function is called by formProvider and gets forms info by the bundle name of the calling ability.
292     *        The bundle name will be retrieved here.
293     * @param filter Filter that contains attributes that the formInfos have to have.
294     * @param formInfos Return the forms' information of the calling bundle name
295     * @return Returns ERR_OK on success, others on failure.
296     */
297     int32_t GetFormsInfo(const FormInfoFilter &filter, std::vector<FormInfo> &formInfos) override;
298 
299     /**
300      * @brief Check if the request of publishing a form is supported by the host.
301      * @return Returns true if the request is supported and false otherwise.
302      */
303     bool IsRequestPublishFormSupported() override;
304 
305     /**
306      * @brief Start an ability. This function can only be called by a form extension of a system app.
307      * @param want includes ability name, parameters and relative info sending to an ability.
308      * @param callerToken token of the ability that initially calls this function.
309      * @return Returns ERR_OK on success, others on failure.
310      */
311     int32_t StartAbility(const Want &want, const sptr<IRemoteObject> &callerToken) override;
312 
313     /**
314      * @brief Share form by formID and deviceID.
315      * @param formId Indicates the unique id of form.
316      * @param deviceId Indicates the remote device ID.
317      * @param callerToken Indicates the host client.
318      * @param requestCode The request code of this share form.
319      * @return Returns ERR_OK on success, others on failure.
320      */
321     int32_t ShareForm(int64_t formId, const std::string &deviceId,
322         const sptr<IRemoteObject> &callerToken, int64_t requestCode) override;
323 
324     /**
325      * @brief Receive form sharing information from remote.
326      * @param info Indicates form sharing information.
327      * @return Returns ERR_OK on success, others on failure.
328      */
329     int32_t RecvFormShareInfoFromRemote(const FormShareInfo &info) override;
330 
331     /**
332      * @brief Dump form.
333      * @param fd Indicates the file descriptor for result.
334      * @param args Indicates the input arguments.
335      * @return Returns ERR_OK on success, others on failure.
336      */
337     int Dump(int fd, const std::vector<std::u16string> &args) override;
338 
339     /**
340     * @brief Check form manager service ready.
341     * @return Return true if form manager service Ready; return false otherwise.
342     */
343     bool CheckFMSReady() override;
344 
345     /**
346      * @brief The Call Event triggers the callee method.
347      * @param funcName function name which is used by callee.
348      * @param params parameter which is used by callee.
349      * @return Returns ERR_OK on success, others on failure.
350      */
SetBackgroundFunction(const std::string funcName,const std::string params)351     int32_t SetBackgroundFunction(const std::string funcName, const std::string params) override
352     {
353         return ERR_OK;
354     }
355 private:
356     enum class DumpKey {
357         KEY_DUMP_HELP = 0,
358         KEY_DUMP_STORAGE,
359         KEY_DUMP_BY_BUNDLE_NAME,
360         KEY_DUMP_BY_FORM_ID,
361     };
362     /**
363      * @brief initialization of form manager service.
364      */
365     ErrCode Init();
366 
367     ErrCode CheckFormPermission();
368 
369     void InitFormShareMgrEventHandler();
370 
371     void DumpInit();
372     void Dump(const std::vector<std::u16string> &args, std::string &result);
373     bool ParseOption(const std::vector<std::u16string> &args, DumpKey &key, std::string &value, std::string &result);
374     void HiDumpHelp([[maybe_unused]] const std::string &args, std::string &result);
375     void HiDumpStorageFormInfos([[maybe_unused]] const std::string &args, std::string &result);
376     void HiDumpFormInfoByBundleName(const std::string &args, std::string &result);
377     void HiDumpFormInfoByFormId(const std::string &args, std::string &result);
378 private:
379     static const int32_t ENABLE_FORM_UPDATE = 5;
380     const static std::map<std::string, DumpKey> dumpKeyMap_;
381     using DumpFuncType = void (FormMgrService::*)(const std::string &args, std::string &result);
382     std::map<DumpKey, DumpFuncType> dumpFuncMap_;
383     ServiceRunningState state_ = ServiceRunningState::STATE_NOT_START;
384     std::shared_ptr<EventRunner> runner_ = nullptr;
385     std::shared_ptr<FormEventHandler> handler_ = nullptr;
386     std::shared_ptr<FormSysEventReceiver> formSysEventReceiver_ = nullptr;
387     sptr<FormBundleEventCallback> formBundleEventCallback_ = nullptr;
388     mutable std::mutex instanceMutex_;
389     DISALLOW_COPY_AND_MOVE(FormMgrService);
390 };
391 }  // namespace AppExecFwk
392 }  // namespace OHOS
393 #endif  // OHOS_FORM_FWK_FORM_MGR_SERVICE_H
394