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_ADAPTER_H 17 #define OHOS_FORM_FWK_FORM_MGR_ADAPTER_H 18 19 #include <singleton.h> 20 21 #include "bundle_info.h" 22 #include "bundle_mgr_interface.h" 23 #include "form_info.h" 24 #include "form_item_info.h" 25 #include "form_js_info.h" 26 #include "form_provider_data.h" 27 #include "form_state_info.h" 28 #include "iremote_object.h" 29 #include "want.h" 30 31 namespace OHOS { 32 namespace AppExecFwk { 33 using Want = OHOS::AAFwk::Want; 34 using WantParams = OHOS::AAFwk::WantParams; 35 /** 36 * @class FormMgrAdapter 37 * Form request handler from form host. 38 */ 39 class FormMgrAdapter final : public DelayedRefSingleton<FormMgrAdapter> { 40 DECLARE_DELAYED_REF_SINGLETON(FormMgrAdapter) 41 public: 42 DISALLOW_COPY_AND_MOVE(FormMgrAdapter); 43 44 /** 45 * @brief Add form with want, send want to form manager service. 46 * @param formId The Id of the forms to add. 47 * @param want The want of the form to add. 48 * @param callerToken Caller ability token. 49 * @param formInfo Form info. 50 * @return Returns ERR_OK on success, others on failure. 51 */ 52 int AddForm(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken, FormJsInfo &formInfo); 53 54 /** 55 * @brief Delete forms with formIds, send formIds to form manager service. 56 * @param formId The Id of the forms to delete. 57 * @param callerToken Caller ability token. 58 * @return Returns ERR_OK on success, others on failure. 59 */ 60 int DeleteForm(const int64_t formId, const sptr<IRemoteObject> &callerToken); 61 62 /** 63 * @brief Stop rendering form. 64 * @param formId The Id of the forms to delete. 65 * @param compId The compId of the forms to delete. 66 * @return Returns ERR_OK on success, others on failure. 67 */ 68 int StopRenderingForm(const int64_t formId, const std::string &compId); 69 70 /** 71 * @brief Release forms with formIds, send formIds to form Mgr service. 72 * @param formId The Id of the forms to release. 73 * @param callerToken Caller ability token. 74 * @param delCache Delete Cache or not. 75 * @return Returns ERR_OK on success, others on failure. 76 */ 77 int ReleaseForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const bool delCache); 78 79 /** 80 * @brief Update form with formId, send formId to form manager service. 81 * @param formId The Id of the form to update. 82 * @param bundleName Provider ability bundleName. 83 * @param formProviderData form provider data. 84 * @return Returns ERR_OK on success, others on failure. 85 */ 86 int UpdateForm(const int64_t formId, const std::string &bundleName, const FormProviderData &formProviderData); 87 88 /** 89 * @brief Request form with formId and want, send formId and want to form manager service. 90 * 91 * @param formId The Id of the form to update. 92 * @param callerToken Caller ability token. 93 * @param want The want of the form to request. 94 * @return Returns ERR_OK on success, others on failure. 95 */ 96 int RequestForm(const int64_t formId, const sptr<IRemoteObject> &callerToken, const Want &want); 97 98 /** 99 * @brief Form visible/invisible notify, send formIds to form manager service. 100 * 101 * @param formIds The vector of form Ids. 102 * @param callerToken Caller ability token. 103 * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE. 104 * @return Returns ERR_OK on success, others on failure. 105 */ 106 ErrCode NotifyWhetherVisibleForms(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken, 107 const int32_t formVisibleType); 108 109 /** 110 * @brief temp form to normal form. 111 * @param formId The Id of the form. 112 * @param callerToken Caller ability token. 113 * @return Returns ERR_OK on success, others on failure. 114 */ 115 int CastTempForm(const int64_t formId, const sptr<IRemoteObject> &callerToken); 116 117 /** 118 * @brief Dump all of form storage infos. 119 * @param formInfos All of form storage infos. 120 * @return Returns ERR_OK on success, others on failure. 121 */ 122 int DumpStorageFormInfos(std::string &formInfos) const; 123 /** 124 * @brief Dump form info by a bundle name. 125 * @param bundleName The bundle name of form provider. 126 * @param formInfos Form infos. 127 * @return Returns ERR_OK on success, others on failure. 128 */ 129 int DumpFormInfoByBundleName(const std::string &bundleName, std::string &formInfos) const; 130 /** 131 * @brief Dump form info by a bundle name. 132 * @param formId The id of the form. 133 * @param formInfo Form info. 134 * @return Returns ERR_OK on success, others on failure. 135 */ 136 int DumpFormInfoByFormId(const std::int64_t formId, std::string &formInfo) const; 137 /** 138 * @brief Dump form timer by form id. 139 * @param formId The id of the form. 140 * @param isTimingService "true" or "false". 141 * @return Returns ERR_OK on success, others on failure. 142 */ 143 int DumpFormTimerByFormId(const std::int64_t formId, std::string &isTimingService) const; 144 145 /** 146 * @brief set next refresh time. 147 * @param formId The id of the form. 148 * @param nextTime next refresh time. 149 * @return Returns ERR_OK on success, others on failure. 150 */ 151 int SetNextRefreshTime(const int64_t formId, const int64_t nextTime); 152 153 /** 154 * @brief Request to publish a form to the form host. 155 * 156 * @param want The want of the form to publish. 157 * @param withFormBindingData Indicates whether the formBindingData is carried with. 158 * @param formBindingData Indicates the form data. 159 * @param formId Return the form id to be published. 160 * @return Returns ERR_OK on success, others on failure. 161 */ 162 ErrCode RequestPublishForm(Want &want, bool withFormBindingData, 163 std::unique_ptr<FormProviderData> &formBindingData, int64_t &formId); 164 165 /** 166 * @brief Check if the request of publishing a form is supported by the host. 167 * @return Returns true if the request is supported and false otherwise. 168 */ 169 bool IsRequestPublishFormSupported(); 170 171 /** 172 * @brief enable update form. 173 * @param formIDs The id of the forms. 174 * @param callerToken Caller ability token. 175 * @return Returns ERR_OK on success, others on failure. 176 */ 177 int EnableUpdateForm(const std::vector<int64_t> formIDs, const sptr<IRemoteObject> &callerToken); 178 179 /** 180 * @brief disable update form. 181 * @param formIDs The id of the forms. 182 * @param callerToken Caller ability token. 183 * @return Returns ERR_OK on success, others on failure. 184 */ 185 int DisableUpdateForm(const std::vector<int64_t> formIDs, const sptr<IRemoteObject> &callerToken); 186 187 /** 188 * @brief Process js message event. 189 * @param formId Indicates the unique id of form. 190 * @param want information passed to supplier. 191 * @param callerToken Caller ability token. 192 * @return Returns true if execute success, false otherwise. 193 */ 194 int MessageEvent(const int64_t formId, const Want &want, const sptr<IRemoteObject> &callerToken); 195 196 /** 197 * @brief Process js router event. 198 * @param formId Indicates the unique id of form. 199 * @param want the want of the ability to start. 200 * @param callerToken Caller ability token. 201 * @return Returns true if execute success, false otherwise. 202 */ 203 int RouterEvent(const int64_t formId, Want &want, const sptr<IRemoteObject> &callerToken); 204 205 /** 206 * @brief Process background router event. 207 * @param formId Indicates the unique id of form. 208 * @param want the want of the ability to start. 209 * @param callerToken Caller ability token. 210 * @return Returns true if execute success, false otherwise. 211 */ 212 int BackgroundEvent(const int64_t formId, Want &want, const sptr<IRemoteObject> &callerToken); 213 214 /** 215 * @brief Acquire form data from form provider. 216 * @param formId The Id of the from. 217 * @param want The want of the request. 218 * @param remoteObject Form provider proxy object. 219 */ 220 void AcquireProviderFormInfo(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject); 221 /** 222 * @brief Notify form provider for delete form. 223 * @param formId The Id of the from. 224 * @param want The want of the form. 225 * @param remoteObject Form provider proxy object. 226 * @return none. 227 */ 228 void NotifyFormDelete(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject); 229 230 /** 231 * @brief Delete the invalid forms. 232 * @param formIds Indicates the ID of the valid forms. 233 * @param callerToken Caller ability token. 234 * @param numFormsDeleted Returns the number of the deleted forms. 235 * @return Returns ERR_OK on success, others on failure. 236 */ 237 int DeleteInvalidForms(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken, 238 int32_t &numFormsDeleted); 239 240 /** 241 * @brief Acquire form state info by passing a set of parameters (using Want) to the form provider. 242 * @param want Indicates a set of parameters to be transparently passed to the form provider. 243 * @param callerToken Caller ability token. 244 * @param stateInfo Returns the form's state info of the specify. 245 * @return Returns ERR_OK on success, others on failure. 246 */ 247 int AcquireFormState(const Want &want, const sptr<IRemoteObject> &callerToken, FormStateInfo &stateInfo); 248 249 /** 250 * @brief Notify the form is visible or not. 251 * @param formIds Indicates the ID of the forms. 252 * @param isVisible Visible or not. 253 * @param callerToken Host client. 254 * @return Returns ERR_OK on success, others on failure. 255 */ 256 int NotifyFormsVisible(const std::vector<int64_t> &formIds, bool isVisible, const sptr<IRemoteObject> &callerToken); 257 258 /** 259 * @brief Notify the form is enable to be updated or not. 260 * @param formIds Indicates the ID of the forms. 261 * @param isEnableUpdate enable update or not. 262 * @param callerToken Host client. 263 * @return Returns ERR_OK on success, others on failure. 264 */ 265 int NotifyFormsEnableUpdate(const std::vector<int64_t> &formIds, bool isEnableUpdate, 266 const sptr<IRemoteObject> &callerToken); 267 268 /** 269 * @brief Get All FormsInfo. 270 * @param formInfos Return the forms' information of all forms provided. 271 * @return Returns ERR_OK on success, others on failure. 272 */ 273 int GetAllFormsInfo(std::vector<FormInfo> &formInfos); 274 275 /** 276 * @brief Get forms info by bundle name . 277 * @param bundleName Application name. 278 * @param formInfos Return the forms' information of the specify application name. 279 * @return Returns ERR_OK on success, others on failure. 280 */ 281 int GetFormsInfoByApp(const std::string &bundleName, std::vector<FormInfo> &formInfos); 282 283 /** 284 * @brief Get forms info by bundle name and module name. 285 * @param bundleName bundle name. 286 * @param moduleName Module name of hap. 287 * @param formInfos Return the forms' information of the specify bundle name and module name. 288 * @return Returns ERR_OK on success, others on failure. 289 */ 290 int GetFormsInfoByModule(const std::string &bundleName, const std::string &moduleName, 291 std::vector<FormInfo> &formInfos); 292 private: 293 /** 294 * @brief Get form configure info. 295 * @param want The want of the request. 296 * @param formItemInfo Form configure info. 297 * @return Returns ERR_OK on success, others on failure. 298 */ 299 ErrCode GetFormConfigInfo(const Want& want, FormItemInfo &formItemInfo); 300 /** 301 * @brief Get bundle info. 302 * @param want The want of the request. 303 * @param bundleInfo Bundle info. 304 * @param packageName Package name. 305 * @return Returns ERR_OK on success, others on failure. 306 */ 307 ErrCode GetBundleInfo(const AAFwk::Want &want, BundleInfo &bundleInfo, std::string &packageName); 308 /** 309 * @brief Get form info. 310 * @param want The want of the request. 311 * @param formInfo Form info. 312 * @return Returns ERR_OK on success, others on failure. 313 */ 314 ErrCode GetFormInfo(const AAFwk::Want &want, FormInfo &formInfo); 315 /** 316 * @brief Get form configure info. 317 * @param want The want of the request. 318 * @param bundleInfo Bundle info. 319 * @param formInfo Form info. 320 * @param formItemInfo Form configure info. 321 * @return Returns ERR_OK on success, others on failure. 322 */ 323 ErrCode GetFormItemInfo(const AAFwk::Want &want, const BundleInfo &bundleInfo, const FormInfo &formInfo, 324 FormItemInfo &formItemInfo); 325 /** 326 * @brief Dimension valid check. 327 * @param formInfo Form info. 328 * @param dimensionId Dimension id. 329 * @return Returns true on success, false on failure. 330 */ 331 bool IsDimensionValid(const FormInfo &formInfo, int dimensionId) const; 332 /** 333 * @brief Create form configure info. 334 * @param bundleInfo Bundle info. 335 * @param formInfo Form info. 336 * @param itemInfo Form configure info. 337 * @param want The want of the request. 338 * @return Returns ERR_OK on success, others on failure. 339 */ 340 ErrCode CreateFormItemInfo(const BundleInfo& bundleInfo, const FormInfo& formInfo, FormItemInfo& itemInfo, 341 const AAFwk::Want &want); 342 /** 343 * @brief Allocate form by formId. 344 * @param info Form configure info. 345 * @param callerToken Caller ability token. 346 * @param wantParams WantParams of the request. 347 * @param formInfo Form info for form host. 348 * @return Returns ERR_OK on success, others on failure. 349 */ 350 ErrCode AllotFormById(const FormItemInfo &info, const sptr<IRemoteObject> &callerToken, 351 const WantParams &wantParams, FormJsInfo &formInfo); 352 /** 353 * @brief Allocate form by form configure info. 354 * @param info Form configure info. 355 * @param callerToken Caller ability token. 356 * @param wantParams WantParams of the request. 357 * @param formInfo Form info for form host. 358 * @return Returns ERR_OK on success, others on failure. 359 */ 360 ErrCode AllotFormByInfo(const FormItemInfo &info, const sptr<IRemoteObject> &callerToken, 361 const WantParams& wantParams, FormJsInfo &formInfo); 362 /** 363 * @brief Acquire form data from form provider. 364 * @param formId The Id of the form.. 365 * @param info Form configure info. 366 * @param wantParams WantParams of the request. 367 * @return Returns ERR_OK on success, others on failure. 368 */ 369 ErrCode AcquireProviderFormInfoAsync(const int64_t formId, const FormItemInfo &info, const WantParams &wantParams); 370 371 /** 372 * @brief Handle release form. 373 * @param formId The form id. 374 * @param callerToken Caller ability token. 375 * @return Returns ERR_OK on success, others on failure. 376 */ 377 ErrCode HandleReleaseForm(const int64_t formId, const sptr<IRemoteObject> &callerToken); 378 379 /** 380 * @brief Handle delete form. 381 * @param formId The form id. 382 * @param callerToken Caller ability token. 383 * @return Returns ERR_OK on success, others on failure. 384 */ 385 ErrCode HandleDeleteForm(const int64_t formId, const sptr<IRemoteObject> &callerToken); 386 387 /** 388 * @brief Handle delete temp form. 389 * @param formId The form id. 390 * @param callerToken Caller ability token. 391 * @return Returns ERR_OK on success, others on failure. 392 */ 393 ErrCode HandleDeleteTempForm(const int64_t formId, const sptr<IRemoteObject> &callerToken); 394 395 /** 396 * @brief Handle delete form storage. 397 * @param dbRecord Form storage information. 398 * @param uid calling user id. 399 * @param formId The form id. 400 * @return Function result and has other host flag. 401 */ 402 ErrCode HandleDeleteFormCache(FormRecord &dbRecord, const int uid, const int64_t formId); 403 404 /** 405 * @brief Add existed form record. 406 * @param info Form configure info. 407 * @param callerToken Caller ability token. 408 * @param record Form data. 409 * @param formId The form id. 410 * @param wantParams WantParams of the request. 411 * @param formInfo Form info for form host. 412 * @return Returns ERR_OK on success, others on failure. 413 */ 414 ErrCode AddExistFormRecord(const FormItemInfo &info, const sptr<IRemoteObject> &callerToken, 415 const FormRecord &record, const int64_t formId, const WantParams &wantParams, FormJsInfo &formInfo); 416 417 /** 418 * @brief Add new form record. 419 * @param info Form configure info. 420 * @param formId The form id. 421 * @param callerToken Caller ability token. 422 * @param wantParams WantParams of the request. 423 * @param formInfo Form info for form host. 424 * @return Returns ERR_OK on success, others on failure. 425 */ 426 ErrCode AddNewFormRecord(const FormItemInfo &info, const int64_t formId, 427 const sptr<IRemoteObject> &callerToken, const WantParams &wantParams, FormJsInfo &formInfo); 428 429 /** 430 * @brief Send event notify to form provider. The event notify type include FORM_VISIBLE and FORM_INVISIBLE. 431 * 432 * @param providerKey The provider key string which consists of the provider bundle name and ability name. 433 * @param formIdsByProvider The map of form Ids and their event type which have the same provider. 434 * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE. 435 * @return Returns ERR_OK on success, others on failure. 436 */ 437 ErrCode HandleEventNotify(const std::string &providerKey, const std::vector<int64_t> &formIdsByProvider, 438 const int32_t formVisibleType); 439 440 /** 441 * @brief Increase the timer refresh count. 442 * 443 * @param formId The form id. 444 */ 445 void IncreaseTimerRefreshCount(const int64_t formId); 446 447 /** 448 * @brief handle update form flag. 449 * @param formIDs The id of the forms. 450 * @param callerToken Caller ability token. 451 * @param flag form flag. 452 * @param isOnlyEnableUpdate form enable update form flag. 453 * @return Returns ERR_OK on success, others on failure. 454 */ 455 ErrCode HandleUpdateFormFlag(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &callerToken, 456 bool flag, bool isOnlyEnableUpdate); 457 458 /** 459 * @brief check form cached. 460 * @param record Form information. 461 * @return Returns true on cached, false on not. 462 */ 463 bool IsFormCached(const FormRecord record); 464 465 /** 466 * @brief set next refresh time locked. 467 * @param formId The form's id. 468 * @param nextTime next refresh time. 469 * @param userId User ID. 470 * @return Returns ERR_OK on success, others on failure. 471 */ 472 int SetNextRefreshTimeLocked(const int64_t formId, const int64_t nextTime, const int32_t userId = 0); 473 474 /** 475 * @brief check if update is valid. 476 * @param formId The form's id. 477 * @param bundleName Provider ability bundleName. 478 * @return Returns true or false. 479 */ 480 bool IsUpdateValid(const int64_t formId, const std::string &bundleName); 481 /** 482 * @brief Handle cast temp form. 483 * @param formId The form id. 484 * @param record Form information. 485 * @return Returns ERR_OK on success, others on failure. 486 */ 487 ErrCode HandleCastTempForm(const int64_t formId, const FormRecord &record); 488 489 /** 490 * @brief Add form timer. 491 * @param formRecord Form information. 492 * @return Returns ERR_OK on success, others on failure. 493 */ 494 ErrCode AddFormTimer(const FormRecord &formRecord); 495 496 /** 497 * @brief check the publish form. 498 * @param want The want of the form to publish. 499 * @return Returns ERR_OK on success, others on failure. 500 */ 501 ErrCode CheckPublishForm(Want &want); 502 503 /** 504 * @brief Query the request host. 505 * @param want The want of the form to publish. 506 * @return Returns ERR_OK on success, others on failure. 507 */ 508 ErrCode QueryPublishFormToHost(Want &want); 509 510 /** 511 * @brief Post request publish form to host. 512 * @param want The want of the form to publish. 513 * @return Returns ERR_OK on success, others on failure. 514 */ 515 ErrCode RequestPublishFormToHost(Want &want); 516 517 /** 518 * @brief check the argv of AddRequestPublishForm. 519 * @param want The want of the form to add. 520 * @param formProviderWant The want of the form to publish from provider. 521 * @return Returns ERR_OK on success, others on failure. 522 */ 523 ErrCode CheckAddRequestPublishForm(const Want &want, const Want &formProviderWant); 524 525 /** 526 * @brief add request publish form. 527 * @param formItemInfo Form configure info. 528 * @param want The want of the form to add. 529 * @param callerToken Caller ability token. 530 * @param formJsInfo Return form info to form host. 531 * @return Returns ERR_OK on success, others on failure. 532 */ 533 ErrCode AddRequestPublishForm(const FormItemInfo &formItemInfo, const Want &want, 534 const sptr<IRemoteObject> &callerToken, FormJsInfo &formJsInfo); 535 536 /** 537 * @brief get bundleName. 538 * @param bundleName for output. 539 * @return Returns true on success, others on failure. 540 */ 541 bool GetBundleName(std::string &bundleName); 542 543 /** 544 * @brief Update provider info to host 545 * 546 * @param matchedFormId The Id of the form 547 * @param callerToken Caller ability token. 548 * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE. 549 * @param formRecord Form storage information 550 * @return Returns true on success, false on failure. 551 */ 552 bool UpdateProviderInfoToHost(const int64_t matchedFormId, const sptr<IRemoteObject> &callerToken, 553 const int32_t formVisibleType, FormRecord &formRecord); 554 555 /** 556 * @brief If the form provider is system app and the config item 'formVisibleNotify' is true, 557 * notify the form provider that the current form is visible. 558 * 559 * @param iBundleMgr BundleManager 560 * @param bundleName BundleName 561 * @return Returns true if the form provider is system app, false if not. 562 */ 563 bool CheckIsSystemAppByBundleName(const sptr<IBundleMgr> &iBundleMgr, const std::string &bundleName); 564 565 /** 566 * @brief if the ability have permission for keeping background running is true, 567 * @param iBundleMgr BundleManagerProxy 568 * @param bundleName BundleName 569 * @return Returns true if the ability have permission for keeping background running, false if not. 570 */ 571 bool CheckKeepBackgroundRunningPermission(const sptr<IBundleMgr> &iBundleMgr, const std::string &bundleName); 572 /** 573 * @brief Create eventMaps for event notify. 574 * 575 * @param matchedFormId The Id of the form 576 * @param formRecord Form storage information 577 * @param eventMaps eventMaps for event notify 578 * @return Returns true on success, false on failure. 579 */ 580 bool CreateHandleEventMap(const int64_t matchedFormId, const FormRecord &formRecord, 581 std::map<std::string, std::vector<int64_t>> &eventMaps); 582 /** 583 * @brief Get current user ID. 584 * @param callingUid calling Uid. 585 * @return Returns user ID. 586 */ 587 int32_t GetCurrentUserId(const int callingUid); 588 /** 589 * @brief AcquireFormState want check. 590 * @param bundleName The bundle name of the form. 591 * @param abilityName The ability name of the form. 592 * @param want The want of the form. 593 * @param provider the provider info. 594 * @return Returns ERR_OK on success, others on failure. 595 */ 596 ErrCode AcquireFormStateCheck(const std::string &bundleName, const std::string &abilityName, const Want &want, 597 std::string &provider); 598 /** 599 * @brief check if the form host is system app 600 * @param formRecord Form storage information 601 * @return Returns true if the form host is system app, false if not. 602 */ 603 bool checkFormHostHasSaUid(const FormRecord &formRecord); 604 }; 605 } // namespace AppExecFwk 606 } // namespace OHOS 607 608 #endif // OHOS_FORM_FWK_FORM_MGR_ADAPTER_H 609