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_OHOS_FORM_MGR_H 17 #define FOUNDATION_APPEXECFWK_OHOS_FORM_MGR_H 18 19 #include <mutex> 20 #include <singleton.h> 21 22 #include "form_callback_interface.h" 23 #include "form_constants.h" 24 #include "form_death_callback.h" 25 #include "form_js_info.h" 26 #include "form_mgr_interface.h" 27 #include "form_provider_data.h" 28 #include "iremote_object.h" 29 #include "want.h" 30 31 namespace OHOS { 32 namespace AppExecFwk { 33 using OHOS::AAFwk::Want; 34 35 static volatile int recoverStatus_ = Constants::NOT_IN_RECOVERY; 36 37 /** 38 * @class FormMgr 39 * FormMgr is used to access form manager services. 40 */ 41 class FormMgr final : public DelayedRefSingleton<FormMgr> { 42 DECLARE_DELAYED_REF_SINGLETON(FormMgr) 43 public: 44 DISALLOW_COPY_AND_MOVE(FormMgr); 45 46 /** 47 * @brief Add form with want, send want to form manager service. 48 * @param formId The Id of the forms to add. 49 * @param want The want of the form to add. 50 * @param callerToken Caller ability token. 51 * @param formInfo Form info. 52 * @return Returns ERR_OK on success, others on failure. 53 */ 54 int AddForm(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken, 55 FormJsInfo &formInfo); 56 57 /** 58 * @brief Delete forms with formIds, send formIds to form manager service. 59 * @param formId The Id of the forms to delete. 60 * @param callerToken Caller ability token. 61 * @return Returns ERR_OK on success, others on failure. 62 */ 63 int DeleteForm(const int64_t formId, const sptr<IRemoteObject> &callerToken); 64 65 /** 66 * @brief Release forms with formIds, send formIds to form manager service. 67 * @param formId The Id of the forms to release. 68 * @param callerToken Caller ability token. 69 * @param delCache Delete Cache or not. 70 * @return Returns ERR_OK on success, others on failure. 71 */ 72 int ReleaseForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const bool delCache); 73 74 /** 75 * @brief Update form with formId, send formId to form manager service. 76 * @param formId The Id of the form to update. 77 * @param bundleName Provider ability bundleName. 78 * @param formBindingData Form binding data. 79 * @return Returns ERR_OK on success, others on failure. 80 */ 81 int UpdateForm(const int64_t formId, const std::string &bundleName, const FormProviderData &formBindingData); 82 83 /** 84 * @brief Notify the form service that the form user's lifecycle is updated. 85 * 86 * This should be called when form user request form. 87 * 88 * @param formId Indicates the unique id of form. 89 * @param callerToken Indicates the callback remote object of specified form user. 90 * @param want information passed to supplier. 91 * @return Returns true if execute success, false otherwise. 92 */ 93 int RequestForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const Want &want); 94 95 /** 96 * @brief Form visible/invisible notify, send formIds to form manager service. 97 * @param formIds The Id list of the forms to notify. 98 * @param callerToken Caller ability token. 99 * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE. 100 * @return Returns ERR_OK on success, others on failure. 101 */ 102 int NotifyWhetherVisibleForms(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken, 103 const int32_t formVisibleType); 104 105 /** 106 * @brief temp form to normal form. 107 * @param formId The Id of the form. 108 * @param callerToken Caller ability token. 109 * @return None. 110 */ 111 int CastTempForm(const int64_t formId, const sptr<IRemoteObject> &callerToken); 112 113 /** 114 * @brief Dump all of form storage infos. 115 * @param formInfos All of form storage infos. 116 * @return Returns ERR_OK on success, others on failure. 117 */ 118 int DumpStorageFormInfos(std::string &formInfos); 119 /** 120 * @brief Dump form info by a bundle name. 121 * @param bundleName The bundle name of form provider. 122 * @param formInfos Form infos. 123 * @return Returns ERR_OK on success, others on failure. 124 */ 125 int DumpFormInfoByBundleName(const std::string bundleName, std::string &formInfos); 126 /** 127 * @brief Dump form info by a bundle name. 128 * @param formId The id of the form. 129 * @param formInfo Form info. 130 * @return Returns ERR_OK on success, others on failure. 131 */ 132 int DumpFormInfoByFormId(const std::int64_t formId, std::string &formInfo); 133 /** 134 * @brief Process js message event. 135 * @param formId Indicates the unique id of form. 136 * @param want information passed to supplier. 137 * @param callerToken Caller ability token. 138 * @return Returns true if execute success, false otherwise. 139 */ 140 int MessageEvent(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken); 141 142 /** 143 * @brief Get fms recoverStatus. 144 * 145 * @return The current recover status. 146 */ 147 static int GetRecoverStatus(); 148 149 /** 150 * @brief Register death callback. 151 * 152 * @param formDeathCallback The death callback. 153 */ 154 void RegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> &formDeathCallback); 155 156 /** 157 * @brief UnRegister death callback. 158 * 159 * @param formDeathCallback The death callback. 160 */ 161 void UnRegisterDeathCallback(const std::shared_ptr<FormCallbackInterface> &formDeathCallback); 162 163 int SetNextRefreshTime(const int64_t formId, const int64_t nextTime); 164 165 /** 166 * @brief Lifecycle Update. 167 * @param formIds The id of the forms. 168 * @param callerToken Host client. 169 * @param updateType Next refresh time. 170 * @return Returns ERR_OK on success, others on failure. 171 */ 172 int LifecycleUpdate(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken, 173 const int32_t updateType); 174 175 /** 176 * @brief Set fms recoverStatus. 177 * 178 * @param recoverStatus The recover status. 179 */ 180 static void SetRecoverStatus(int recoverStatus); 181 182 /** 183 * @brief Set form mgr service for test. 184 */ 185 void SetFormMgrService(sptr<IFormMgr> formMgrService); 186 187 /** 188 * @brief Get death recipient. 189 * @return deathRecipient_. 190 */ 191 sptr<IRemoteObject::DeathRecipient> GetDeathRecipient() const; 192 193 /** 194 * @brief Check whether the specified death callback is registered in form mgr. 195 * @param formDeathCallback The specified death callback for checking. 196 * @return Return true on success, false on failure. 197 */ 198 bool CheckIsDeathCallbackRegistered(const std::shared_ptr<FormCallbackInterface> &formDeathCallback); 199 200 /** 201 * @brief Get the error message content. 202 * 203 * @param errCode Error code. 204 * @return Message content. 205 */ 206 std::string GetErrorMessage(int errCode); 207 private: 208 /** 209 * @brief Connect form manager service. 210 * @return Returns ERR_OK on success, others on failure. 211 */ 212 ErrCode Connect(); 213 214 /** 215 * @brief Reconnect form manager service once per 1000 milliseconds, 216 * until the connection succeeds or reaching the max retry times. 217 * @return Returns true if execute success, false otherwise. 218 */ 219 bool Reconnect(); 220 /** 221 * @brief Reset proxy. 222 * @param remote remote object. 223 */ 224 void ResetProxy(const wptr<IRemoteObject> &remote); 225 226 public: 227 friend class FormMgrDeathRecipient; 228 229 private: 230 /** 231 * @class FormMgrDeathRecipient 232 * FormMgrDeathRecipient notices IRemoteBroker died. 233 */ 234 class FormMgrDeathRecipient : public IRemoteObject::DeathRecipient { 235 public: 236 FormMgrDeathRecipient() = default; 237 ~FormMgrDeathRecipient() = default; 238 239 /** 240 * @brief Notices IRemoteBroker died. 241 * @param remote remote object. 242 */ 243 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 244 private: 245 DISALLOW_COPY_AND_MOVE(FormMgrDeathRecipient); 246 }; 247 248 std::mutex connectMutex_; 249 sptr<IFormMgr> remoteProxy_; 250 251 sptr<IRemoteObject::DeathRecipient> deathRecipient_ {nullptr}; 252 253 // True: need to get a new fms remote object, 254 // False: no need to get a new fms remote object. 255 volatile bool resetFlag_ = false; 256 257 std::vector<std::shared_ptr<FormCallbackInterface>> formDeathCallbacks_; 258 }; 259 } // namespace AppExecFwk 260 } // namespace OHOS 261 #endif // FOUNDATION_APPEXECFWK_OHOS_FORM_MGR_H