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