• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 FOUNDATION_APPEXECFWK_SERVICES_FORMMGR_INCLUDE_FORM_MGR_SERVICE_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_FORMMGR_INCLUDE_FORM_MGR_SERVICE_H
18 
19 #include <codecvt>
20 #include <memory>
21 #include <singleton.h>
22 #include <system_ability.h>
23 #include <thread_ex.h>
24 #include <unordered_map>
25 
26 #include "event_handler.h"
27 #include "form_mgr_stub.h"
28 #include "form_provider_data.h"
29 #include "iremote_object.h"
30 
31 namespace OHOS {
32 namespace AppExecFwk {
33 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
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 envent for the form manager service.
46      */
47     void OnStart() override;
48     /**
49      * @brief Stop envent 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 Release forms with formIds, send formIds to form manager service.
74      * @param formId The Id of the forms to release.
75      * @param callerToken Caller ability token.
76      * @param delCache Delete Cache or not.
77      * @return Returns ERR_OK on success, others on failure.
78      */
79     int ReleaseForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const bool delCache) override;
80 
81     /**
82      * @brief Update form with formId, send formId to form manager service.
83      * @param formId The Id of the form to update.
84      * @param bundleName Provider ability bundleName.
85      * @param FormProviderData Form binding data.
86      * @return Returns ERR_OK on success, others on failure.
87      */
88     int UpdateForm(const int64_t formId, const std::string &bundleName,
89     const FormProviderData &FormProviderData) override;
90 
91     /**
92      * @brief set next refresh time.
93      * @param formId The id of the form.
94      * @param nextTime next refresh time.
95      * @return Returns ERR_OK on success, others on failure.
96      */
97     int SetNextRefreshTime(const int64_t formId, const int64_t nextTime) override;
98 
99     /**
100      * @brief lifecycle update.
101      * @param formIds formIds of hostclient.
102      * @param callerToken Caller ability token.
103      * @param updateType update type,enable or disable.
104      * @return Returns true on success, false on failure.
105      */
106     int LifecycleUpdate(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken,
107     const int32_t updateType) override;
108 
109     /**
110      * @brief Request form with formId and want, send formId and want to form manager service.
111      * @param formId The Id of the form to update.
112      * @param callerToken Caller ability token.
113      * @param want The want of the form to add.
114      * @return Returns ERR_OK on success, others on failure.
115      */
116     int RequestForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const Want &want) override;
117 
118     /**
119      * @brief Form visible/invisible notify, send formIds to form manager service.
120      * @param formIds The Id list of the forms to notify.
121      * @param callerToken Caller ability token.
122      * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE.
123      * @return Returns ERR_OK on success, others on failure.
124      */
125     int NotifyWhetherVisibleForms(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken,
126     const int32_t formVisibleType) override;
127 
128     /**
129      * @brief temp form to normal form.
130      * @param formId The Id of the form.
131      * @param callerToken Caller ability token.
132      * @return Returns ERR_OK on success, others on failure.
133      */
134     int CastTempForm(const int64_t formId, const sptr<IRemoteObject> &callerToken) override;
135 
136     /**
137      * @brief Dump all of form storage infos.
138      * @param formInfos All of form storage infos.
139      * @return Returns ERR_OK on success, others on failure.
140      */
141     int DumpStorageFormInfos(std::string &formInfos) override;
142     /**
143      * @brief Dump form info by a bundle name.
144      * @param bundleName The bundle name of form provider.
145      * @param formInfos Form infos.
146      * @return Returns ERR_OK on success, others on failure.
147      */
148     int DumpFormInfoByBundleName(const std::string &bundleName, std::string &formInfos) override;
149     /**
150      * @brief Dump form info by a bundle name.
151      * @param formId The id of the form.
152      * @param formInfo Form info.
153      * @return Returns ERR_OK on success, others on failure.
154      */
155     int DumpFormInfoByFormId(const std::int64_t formId, std::string &formInfo) override;
156 
157     /**
158      * @brief Process js message event.
159      * @param formId Indicates the unique id of form.
160      * @param want information passed to supplier.
161      * @param callerToken Caller ability token.
162      * @return Returns true if execute success, false otherwise.
163      */
164     int MessageEvent(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken) override;
165 
166     /**
167      * @brief Check whether if the form manager service is ready.
168      * @return Returns true if the form manager service is ready; returns false otherwise.
169      */
170     bool IsReady() const;
171 
172 private:
173     /**
174      * @brief initialization of form manager service.
175      */
176     ErrCode Init();
177 
178     /**
179      * @brief Permission check by callingUid.
180      * @param formId the id of the form.
181      * @return Returns true on success, false on failure.
182      */
183     bool CheckFormPermission();
184 
185     /**
186      * @brief Permission check.
187      * @param bundleName bundleName.
188      * @return Returns true on success, false on failure.
189      */
190     bool CheckFormPermission(const std::string &bundleName) const;
191 private:
192     ServiceRunningState state_;
193 
194     std::shared_ptr<EventRunner> runner_ = nullptr;
195     std::shared_ptr<EventHandler> handler_ = nullptr;
196 
197     bool resetFlag = false;
198 
199     mutable std::mutex instanceMutex_;
200 
201     sptr<IRemoteObject> remote = nullptr;
202 
203     static const int32_t ENABLE_FORM_UPDATE = 5;
204 
205     DISALLOW_COPY_AND_MOVE(FormMgrService);
206 };
207 static bool resetFlag = false;
208 }  // namespace AppExecFwk
209 }  // namespace OHOS
210 #endif  // FOUNDATION_APPEXECFWK_SERVICES_FORMMGR_INCLUDE_FORM_MGR_SERVICE_H
211