1 /* 2 * Copyright (c) 2021-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_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_instance.h" 25 #include "form_instances_filter.h" 26 #include "form_mgr_stub.h" 27 #include "form_provider_data.h" 28 #include "form_serial_queue.h" 29 #include "form_sys_event_receiver.h" 30 #include "iremote_object.h" 31 namespace OHOS { 32 namespace AppExecFwk { 33 enum class ServiceRunningState { 34 STATE_NOT_START, 35 STATE_RUNNING, 36 }; 37 /** 38 * @class FormMgrService 39 * FormMgrService provides a facility for managing form life cycle. 40 */ 41 class FormMgrService : public SystemAbility, 42 public FormMgrStub, 43 public std::enable_shared_from_this<FormMgrService> { 44 DECLARE_DELAYED_SINGLETON(FormMgrService); 45 DECLEAR_SYSTEM_ABILITY(FormMgrService); 46 public: 47 /** 48 * @brief Start event for the form manager service. 49 */ 50 void OnStart() override; 51 /** 52 * @brief Stop event for the form manager service. 53 */ 54 void OnStop() override; 55 56 /** 57 * @brief Add form with want, send want to form manager service. 58 * @param formId The Id of the forms to add. 59 * @param want The want of the form to add. 60 * @param callerToken Caller ability token. 61 * @param formInfo Form info. 62 * @return Returns ERR_OK on success, others on failure. 63 */ 64 int AddForm(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken, 65 FormJsInfo &formInfo) override; 66 67 /** 68 * @brief Delete forms with formIds, send formIds to form manager service. 69 * @param formId The Id of the forms to delete. 70 * @param callerToken Caller ability token. 71 * @return Returns ERR_OK on success, others on failure. 72 */ 73 int DeleteForm(const int64_t formId, const sptr<IRemoteObject> &callerToken) override; 74 75 /** 76 * @brief Stop rendering form. 77 * @param formId The Id of the forms to delete. 78 * @param compId The compId of the forms to delete. 79 * @return Returns ERR_OK on success, others on failure. 80 */ 81 int StopRenderingForm(const int64_t formId, const std::string &compId) override; 82 83 /** 84 * @brief Release forms with formIds, send formIds to form manager service. 85 * @param formId The Id of the forms to release. 86 * @param callerToken Caller ability token. 87 * @param delCache Delete Cache or not. 88 * @return Returns ERR_OK on success, others on failure. 89 */ 90 int ReleaseForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const bool delCache) override; 91 92 /** 93 * @brief Update form with formId, send formId to form manager service. 94 * @param formId The Id of the form to update. 95 * @param bundleName Provider ability bundleName. 96 * @param FormProviderData Form binding data. 97 * @return Returns ERR_OK on success, others on failure. 98 */ 99 int UpdateForm(const int64_t formId, const FormProviderData &FormProviderData) override; 100 101 /** 102 * @brief set next refresh time. 103 * @param formId The id of the form. 104 * @param nextTime next refresh time. 105 * @return Returns ERR_OK on success, others on failure. 106 */ 107 int SetNextRefreshTime(const int64_t formId, const int64_t nextTime) override; 108 109 /** 110 * @brief Release renderer. 111 * @param formId The Id of the forms to release. 112 * @param compId The compId of the forms to release. 113 * @return Returns ERR_OK on success, others on failure. 114 */ 115 int ReleaseRenderer(int64_t formId, const std::string &compId) override; 116 117 /** 118 * @brief Request to publish a form to the form host. 119 * 120 * @param want The want of the form to publish. 121 * @param withFormBindingData Indicates whether the formBindingData is carried with. 122 * @param formBindingData Indicates the form data. 123 * @param formId Return the form id to be published. 124 * @return Returns ERR_OK on success, others on failure. 125 */ 126 ErrCode RequestPublishForm(Want &want, bool withFormBindingData, 127 std::unique_ptr<FormProviderData> &formBindingData, int64_t &formId) override; 128 129 /** 130 * @brief lifecycle update. 131 * @param formIds formIds of host client. 132 * @param callerToken Caller ability token. 133 * @param updateType update type, enable if true and disable if false. 134 * @return Returns true on success, false on failure. 135 */ 136 int LifecycleUpdate(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken, 137 bool updateType) override; 138 139 /** 140 * @brief Request form with formId and want, send formId and want to form manager service. 141 * @param formId The Id of the form to update. 142 * @param callerToken Caller ability token. 143 * @param want The want of the form to add. 144 * @return Returns ERR_OK on success, others on failure. 145 */ 146 int RequestForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const Want &want) override; 147 148 /** 149 * @brief Form visible/invisible notify, send formIds to form manager service. 150 * @param formIds The Id list of the forms to notify. 151 * @param callerToken Caller ability token. 152 * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE. 153 * @return Returns ERR_OK on success, others on failure. 154 */ 155 int NotifyWhetherVisibleForms(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken, 156 const int32_t formVisibleType) override; 157 158 /** 159 * @brief temp form to normal form. 160 * @param formId The Id of the form. 161 * @param callerToken Caller ability token. 162 * @return Returns ERR_OK on success, others on failure. 163 */ 164 int CastTempForm(const int64_t formId, const sptr<IRemoteObject> &callerToken) override; 165 166 /** 167 * @brief Dump all of form storage infos. 168 * @param formInfos All of form storage infos. 169 * @return Returns ERR_OK on success, others on failure. 170 */ 171 int DumpStorageFormInfos(std::string &formInfos) override; 172 /** 173 * @brief Dump form info by a bundle name. 174 * @param bundleName The bundle name of form provider. 175 * @param formInfos Form infos. 176 * @return Returns ERR_OK on success, others on failure. 177 */ 178 int DumpFormInfoByBundleName(const std::string &bundleName, std::string &formInfos) override; 179 /** 180 * @brief Dump form info by a bundle name. 181 * @param formId The id of the form. 182 * @param formInfo Form info. 183 * @return Returns ERR_OK on success, others on failure. 184 */ 185 int DumpFormInfoByFormId(const std::int64_t formId, std::string &formInfo) override; 186 /** 187 * @brief Dump form timer by form id. 188 * @param formId The id of the form. 189 * @param formInfo Form info. 190 * @return Returns ERR_OK on success, others on failure. 191 */ 192 int DumpFormTimerByFormId(const std::int64_t formId, std::string &isTimingService) override; 193 /** 194 * @brief Process js message event. 195 * @param formId Indicates the unique id of form. 196 * @param want information passed to supplier. 197 * @param callerToken Caller ability token. 198 * @return Returns true if execute success, false otherwise. 199 */ 200 int MessageEvent(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken) override; 201 202 /** 203 * @brief Process js router event. 204 * @param formId Indicates the unique id of form. 205 * @param want the want of the ability to start. 206 * @param callerToken Caller ability token. 207 * @return Returns true if execute success, false otherwise. 208 */ 209 int RouterEvent(const int64_t formId, Want &want, const sptr<IRemoteObject> &callerToken) override; 210 211 /** 212 * @brief Process Background event. 213 * @param formId Indicates the unique id of form. 214 * @param want the want of the ability to start. 215 * @param callerToken Caller ability token. 216 * @return Returns true if execute success, false otherwise. 217 */ 218 int BackgroundEvent(const int64_t formId, Want &want, const sptr<IRemoteObject> &callerToken) override; 219 220 /** 221 * @brief Check whether if the form manager service is ready. 222 * @return Returns true if the form manager service is ready; returns false otherwise. 223 */ 224 bool IsReady() const; 225 226 /** 227 * @brief Delete the invalid forms. 228 * @param formIds Indicates the ID of the valid forms. 229 * @param callerToken Caller ability token. 230 * @param numFormsDeleted Returns the number of the deleted forms. 231 * @return Returns ERR_OK on success, others on failure. 232 */ 233 virtual int DeleteInvalidForms(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken, 234 int32_t &numFormsDeleted) override; 235 236 /** 237 * @brief Acquire form state info by passing a set of parameters (using Want) to the form provider. 238 * @param want Indicates a set of parameters to be transparently passed to the form provider. 239 * @param callerToken Caller ability token. 240 * @param stateInfo Returns the form's state info of the specify. 241 * @return Returns ERR_OK on success, others on failure. 242 */ 243 virtual int AcquireFormState(const Want &want, const sptr<IRemoteObject> &callerToken, 244 FormStateInfo &stateInfo) override; 245 246 /** 247 * @brief Notify the form is visible or not. 248 * @param formIds Indicates the ID of the forms. 249 * @param isVisible Visible or not. 250 * @param callerToken Host client. 251 * @return Returns ERR_OK on success, others on failure. 252 */ 253 virtual int NotifyFormsVisible(const std::vector<int64_t> &formIds, bool isVisible, 254 const sptr<IRemoteObject> &callerToken) override; 255 256 /** 257 * @brief Notify the form is privacy protected or not. 258 * @param formIds Indicates the ID of the forms. 259 * @param isProtected isProtected or not. 260 * @param callerToken Host client. 261 * @return Returns ERR_OK on success, others on failure. 262 */ 263 int NotifyFormsPrivacyProtected(const std::vector<int64_t> &formIds, bool isProtected, 264 const sptr<IRemoteObject> &callerToken) override; 265 266 /** 267 * @brief Notify the form is enable to be updated or not. 268 * @param formIds Indicates the ID of the forms. 269 * @param isEnableUpdate enable update or not. 270 * @param callerToken Host client. 271 * @return Returns ERR_OK on success, others on failure. 272 */ 273 virtual int NotifyFormsEnableUpdate(const std::vector<int64_t> &formIds, bool isEnableUpdate, 274 const sptr<IRemoteObject> &callerToken) override; 275 276 /** 277 * @brief Get All FormsInfo. 278 * @param formInfos Return the forms' information of all forms provided. 279 * @return Returns ERR_OK on success, others on failure. 280 */ 281 int GetAllFormsInfo(std::vector<FormInfo> &formInfos) override; 282 283 /** 284 * @brief Get forms info by bundle name . 285 * @param bundleName Application name. 286 * @param formInfos Return the forms' information of the specify application name. 287 * @return Returns ERR_OK on success, others on failure. 288 */ 289 int GetFormsInfoByApp(std::string &bundleName, std::vector<FormInfo> &formInfos) override; 290 291 /** 292 * @brief Get forms info by bundle name and module name. 293 * @param bundleName bundle name. 294 * @param moduleName Module name of hap. 295 * @param formInfos Return the forms' information of the specify bundle name and module name. 296 * @return Returns ERR_OK on success, others on failure. 297 */ 298 int GetFormsInfoByModule(std::string &bundleName, std::string &moduleName, 299 std::vector<FormInfo> &formInfos) override; 300 301 /** 302 * @brief This function is called by formProvider and gets forms info by the bundle name of the calling ability. 303 * The bundle name will be retrieved here. 304 * @param filter Filter that contains attributes that the formInfos have to have. 305 * @param formInfos Return the forms' information of the calling bundle name 306 * @return Returns ERR_OK on success, others on failure. 307 */ 308 int32_t GetFormsInfo(const FormInfoFilter &filter, std::vector<FormInfo> &formInfos) override; 309 310 /** 311 * @brief Acquire form data by formId. 312 * @param formId The Id of the form to acquire data. 313 * @param callerToken Indicates the host client. 314 * @param requestCode The request code of this acquire form. 315 * @param formData Return the forms' information of customization 316 * @return Returns ERR_OK on success, others on failure. 317 */ 318 int32_t AcquireFormData(int64_t formId, int64_t requestCode, const sptr<IRemoteObject> &callerToken, 319 AAFwk::WantParams &formData) override; 320 321 /** 322 * @brief Check if the request of publishing a form is supported by the host. 323 * @return Returns true if the request is supported and false otherwise. 324 */ 325 bool IsRequestPublishFormSupported() override; 326 327 /** 328 * @brief Start an ability. This function can only be called by a form extension of a system app. 329 * @param want includes ability name, parameters and relative info sending to an ability. 330 * @param callerToken token of the ability that initially calls this function. 331 * @return Returns ERR_OK on success, others on failure. 332 */ 333 int32_t StartAbility(const Want &want, const sptr<IRemoteObject> &callerToken) override; 334 335 /** 336 * @brief Share form by formID and deviceID. 337 * @param formId Indicates the unique id of form. 338 * @param deviceId Indicates the remote device ID. 339 * @param callerToken Indicates the host client. 340 * @param requestCode The request code of this share form. 341 * @return Returns ERR_OK on success, others on failure. 342 */ 343 int32_t ShareForm(int64_t formId, const std::string &deviceId, 344 const sptr<IRemoteObject> &callerToken, int64_t requestCode) override; 345 346 /** 347 * @brief Receive form sharing information from remote. 348 * @param info Indicates form sharing information. 349 * @return Returns ERR_OK on success, others on failure. 350 */ 351 int32_t RecvFormShareInfoFromRemote(const FormShareInfo &info) override; 352 353 /** 354 * @brief Dump form. 355 * @param fd Indicates the file descriptor for result. 356 * @param args Indicates the input arguments. 357 * @return Returns ERR_OK on success, others on failure. 358 */ 359 int Dump(int fd, const std::vector<std::u16string> &args) override; 360 361 /** 362 * @brief Register form add observer by bundle. 363 * @param bundleName BundleName of the form host 364 * @param callerToken Caller ability token. 365 * @return Returns ERR_OK on success, others on failure. 366 */ 367 ErrCode RegisterFormAddObserverByBundle(const std::string bundleName, 368 const sptr<IRemoteObject> &callerToken) override; 369 370 /** 371 * @brief Register form remove observer by bundle. 372 * @param bundleName BundleName of the form host 373 * @param callerToken Caller ability token. 374 * @return Returns ERR_OK on success, others on failure. 375 */ 376 ErrCode RegisterFormRemoveObserverByBundle(const std::string bundleName, 377 const sptr<IRemoteObject> &callerToken) override; 378 379 /** 380 * @brief Check form manager service ready. 381 * @return Return true if form manager service Ready; return false otherwise. 382 */ 383 bool CheckFMSReady() override; 384 385 /** 386 * @brief The Call Event triggers the callee method. 387 * @param funcName function name which is used by callee. 388 * @param params parameter which is used by callee. 389 * @return Returns ERR_OK on success, others on failure. 390 */ SetBackgroundFunction(const std::string funcName,const std::string params)391 int32_t SetBackgroundFunction(const std::string funcName, const std::string params) override 392 { 393 return ERR_OK; 394 } 395 /** 396 * @brief get forms count. 397 * @param isTempFormFlag Indicates temp form or not. 398 * @param formCount Returns the number of the cast or temp form. 399 * @return Returns ERR_OK on success, others on failure. 400 */ 401 int32_t GetFormsCount(bool isTempFormFlag, int32_t &formCount) override; 402 403 /** 404 * @brief get host forms count. 405 * @param bundleName Indicates form host bundleName. 406 * @param formCount Returns the number of the host form. 407 * @return Returns ERR_OK on success, others on failure. 408 */ 409 int32_t GetHostFormsCount(std::string &bundleName, int32_t &formCount) override; 410 411 /** 412 * @brief Get all running form infos. 413 * @param runningFormInfos Return the running forms' infos currently. 414 * @return Returns ERR_OK on success, others on failure. 415 */ 416 ErrCode GetRunningFormInfos(std::vector<RunningFormInfo> &runningFormInfos) override; 417 418 /** 419 * @brief Get the running form infos by bundle name. 420 * @param bundleName Application name. 421 * @param runningFormInfos Return the running forms' infos of the specify application name. 422 * @return Returns ERR_OK on success, others on failure. 423 */ 424 ErrCode GetRunningFormInfosByBundleName(const std::string &bundleName, 425 std::vector<RunningFormInfo> &runningFormInfos) override; 426 427 /** 428 * @brief Get form instances by filter info. 429 * @param formInstancesFilter include bundleName, moduleName, formName, abilityName to get formInstances. 430 * @param formInstances return formInstances 431 * @return return ERR_OK on get info success,other on failure. 432 */ 433 ErrCode GetFormInstancesByFilter(const FormInstancesFilter &formInstancesFilter, 434 std::vector<FormInstance> &formInstances) override; 435 436 /** 437 * @brief Get form instances by formId. 438 * @param formId formId Indicates the unique id of form. 439 * @param formInstance return formInstance 440 * @return return ERR_OK on get info success, others on failure. 441 */ 442 ErrCode GetFormInstanceById(const int64_t formId, FormInstance &formInstance) override; 443 444 /** 445 * @brief Get form instance by formId, include form store in DB. 446 * @param formId formId Indicates the unique id of form. 447 * @param isIncludeUnused Indicates whether to include unused form instances. 448 * @param formInstance return formInstance 449 * @return return ERR_OK on get info success, others on failure. 450 */ 451 ErrCode GetFormInstanceById(const int64_t formId, bool isIncludeUnused, FormInstance &formInstance) override; 452 453 /** 454 * @brief Register form add observer. 455 * @param bundleName BundleName of the form host 456 * @param callerToken Caller ability token. 457 * @return Returns ERR_OK on success, others on failure. 458 */ 459 ErrCode RegisterAddObserver(const std::string &bundleName, const sptr<IRemoteObject> &callerToken) override; 460 461 /** 462 * @brief Register form remove observer. 463 * @param bundleName BundleName of the form host 464 * @param callerToken Caller ability token. 465 * @return Returns ERR_OK on success, others on failure. 466 */ 467 ErrCode RegisterRemoveObserver(const std::string &bundleName, const sptr<IRemoteObject> &callerToken) override; 468 469 /** 470 * @brief Update proxy form with formId. 471 * @param formId The Id of the form to update. 472 * @param FormProviderData Form binding data. 473 * @param std::vector<FormDataProxy> Form proxy vector. 474 * @return Returns ERR_OK on success, others on failure. 475 */ 476 ErrCode UpdateProxyForm(int64_t formId, const FormProviderData &FormProviderData, 477 const std::vector<FormDataProxy> &formDataProxies) override; 478 479 /** 480 * @brief Request to publish a proxy form to the form host. 481 * @param want The want of the form to publish. 482 * @param withFormBindingData Indicates whether the formBindingData is carried with. 483 * @param formBindingData Indicates the form data. 484 * @param formId Return the form id to be published. 485 * @param std::vector<FormDataProxy> Form proxy vector. 486 * @return Returns ERR_OK on success, others on failure. 487 */ 488 ErrCode RequestPublishProxyForm(Want &want, bool withFormBindingData, 489 std::unique_ptr<FormProviderData> &formBindingData, int64_t &formId, 490 const std::vector<FormDataProxy> &formDataProxies) override; 491 492 /** 493 * @brief Read the form_config.xml. 494 * @return Returns ERR_OK on success, others on failure. 495 */ 496 ErrCode ReadFormConfigXML(); 497 498 /** 499 * @brief Registers the callback for publish form. The callback is used to process the publish form request 500 * when the system handler is not found. 501 * @param interceptorCallback The injected callback, should implementation IFormPublishInterceptor. 502 * @return Returns ERR_OK on success, others on failure. 503 */ 504 int32_t RegisterPublishFormInterceptor(const sptr<IRemoteObject> &interceptorCallback) override; 505 506 /** 507 * @brief Unregisters the callback for publish form. The callback is used to process the publish form request 508 * when the system handler is not found. 509 * @param interceptorCallback The injected callback, should implementation IFormPublishInterceptor. 510 * @return Returns ERR_OK on success, others on failure. 511 */ 512 int32_t UnregisterPublishFormInterceptor(const sptr<IRemoteObject> &interceptorCallback) override; 513 private: 514 enum class DumpKey { 515 KEY_DUMP_HELP = 0, 516 KEY_DUMP_STORAGE, 517 KEY_DUMP_BY_BUNDLE_NAME, 518 KEY_DUMP_BY_FORM_ID, 519 KEY_DUMP_TEMPORARY, 520 KEY_DUMP_STATIC, 521 }; 522 /** 523 * @brief initialization of form manager service. 524 */ 525 ErrCode Init(); 526 527 ErrCode CheckFormObserverPermission(); 528 529 ErrCode CheckFormPermission(); 530 531 bool CheckAcrossLocalAccountsPermission() const; 532 533 void InitFormShareMgrSerialQueue(); 534 535 void DumpInit(); 536 void Dump(const std::vector<std::u16string> &args, std::string &result); 537 bool ParseOption(const std::vector<std::u16string> &args, DumpKey &key, std::string &value, std::string &result); 538 void HiDumpHelp([[maybe_unused]] const std::string &args, std::string &result); 539 void HiDumpStorageFormInfos([[maybe_unused]] const std::string &args, std::string &result); 540 void HiDumpTemporaryFormInfos([[maybe_unused]] const std::string &args, std::string &result); 541 void HiDumpStaticBundleFormInfos([[maybe_unused]] const std::string &args, std::string &result); 542 void HiDumpFormInfoByBundleName(const std::string &args, std::string &result); 543 void HiDumpFormInfoByFormId(const std::string &args, std::string &result); 544 bool CheckCallerIsSystemApp() const; 545 private: 546 static const int32_t ENABLE_FORM_UPDATE = 5; 547 const static std::map<std::string, DumpKey> dumpKeyMap_; 548 using DumpFuncType = void (FormMgrService::*)(const std::string &args, std::string &result); 549 std::map<DumpKey, DumpFuncType> dumpFuncMap_; 550 ServiceRunningState state_ = ServiceRunningState::STATE_NOT_START; 551 std::shared_ptr<FormEventHandler> handler_ = nullptr; 552 std::shared_ptr<FormSerialQueue> serialQueue_ = nullptr; 553 std::shared_ptr<FormSysEventReceiver> formSysEventReceiver_ = nullptr; 554 sptr<FormBundleEventCallback> formBundleEventCallback_ = nullptr; 555 mutable std::mutex instanceMutex_; 556 DISALLOW_COPY_AND_MOVE(FormMgrService); 557 }; 558 } // namespace AppExecFwk 559 } // namespace OHOS 560 #endif // OHOS_FORM_FWK_FORM_MGR_SERVICE_H 561