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_TASK_MGR_H 17 #define OHOS_FORM_FWK_FORM_TASK_MGR_H 18 19 #include <singleton.h> 20 #include <vector> 21 #include <unordered_map> 22 #include <mutex> 23 24 #include "form_event_handler.h" 25 #include "form_js_info.h" 26 #include "form_record.h" 27 #include "form_serial_queue.h" 28 #include "form_state_info.h" 29 #include "iremote_object.h" 30 #include "running_form_info.h" 31 #include "want.h" 32 33 namespace OHOS { 34 namespace AppExecFwk { 35 using Want = OHOS::AAFwk::Want; 36 using WantParams = OHOS::AAFwk::WantParams; 37 enum class TaskType : int64_t { 38 RECYCLE_FORM, 39 RENDER_FORM, 40 }; 41 namespace { 42 constexpr int32_t FORM_TASK_DELAY_TIME = 20; // ms 43 constexpr int32_t FORM_FRS_DIED_TASK_DELAY_TIME = 100; // ms 44 constexpr int32_t FORM_BUILD_DELAY_TIME = 2000; // ms 45 } 46 /** 47 * @class FormTaskMgr 48 * form task manager. 49 */ 50 class FormTaskMgr final : public DelayedRefSingleton<FormTaskMgr> { 51 DECLARE_DELAYED_REF_SINGLETON(FormTaskMgr) 52 53 public: 54 DISALLOW_COPY_AND_MOVE(FormTaskMgr); 55 56 /** 57 * @brief SetSerialQueue. 58 * @param serialQueue serial queue 59 */ SetSerialQueue(const std::shared_ptr<FormSerialQueue> & serialQueue)60 inline void SetSerialQueue(const std::shared_ptr<FormSerialQueue> &serialQueue) 61 { 62 serialQueue_ = serialQueue; 63 } 64 65 /** 66 * @brief Acquire form data from form provider(task). 67 * @param formId The Id of the form. 68 * @param want The want of the request. 69 * @param remoteObject Form provider proxy object. 70 */ 71 void PostAcquireTask(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject); 72 73 /** 74 * @brief Acquire share form data from form provider(task). 75 * @param formId The Id of the form. 76 * @param remoteDeviceId The device ID to share. 77 * @param want The want of the request. 78 * @param remoteObject Form provider proxy object. 79 */ 80 void PostShareAcquireTask(int64_t formId, const std::string &remoteDeviceId, const Want &want, 81 const sptr<IRemoteObject> &remoteObject); 82 83 /** 84 * @brief Delete form data from form provider(task). 85 * @param formId The Id of the form. 86 * @param want The want of the request. 87 * @param remoteObject Form provider proxy object. 88 */ 89 void PostDeleteTask(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject); 90 /** 91 * @brief Notify provider batch delete. 92 * @param formIds The Id list. 93 * @param want The want of the request. 94 * @param remoteObject Form provider proxy object. 95 */ 96 void PostProviderBatchDeleteTask(std::set<int64_t> &formIds, const Want &want, 97 const sptr<IRemoteObject> &remoteObject); 98 /** 99 * @brief Refresh form data from form provider(task). 100 * 101 * @param formId The Id of the form. 102 * @param want The want of the form. 103 * @param remoteObject Form provider proxy object. 104 * @return none. 105 */ 106 void PostRefreshTask(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject); 107 108 /** 109 * @brief Cast temp form data from form provider(task). 110 * 111 * @param formId The Id of the form. 112 * @param want The want of the request. 113 * @param remoteObject Form provider proxy object. 114 * @return none. 115 */ 116 void PostCastTempTask(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject); 117 118 /** 119 * @brief Post form data to form host(task) when acquire form. 120 * @param formId The Id of the form. 121 * @param record form record. 122 * @param remoteObject Form provider proxy object. 123 */ 124 void PostAcquireTaskToHost(const int64_t formId, const FormRecord &record, const sptr<IRemoteObject> &remoteObject); 125 126 /** 127 * @brief Post form data to form host(task) when update form. 128 * @param formId The Id of the form. 129 * @param record form record. 130 * @param remoteObject Form provider proxy object. 131 */ 132 void PostUpdateTaskToHost(const int64_t formId, const FormRecord &record, const sptr<IRemoteObject> &remoteObject); 133 134 /** 135 * @brief Handel form host died(task). 136 * @param remoteHost Form host proxy object. 137 */ 138 void PostHostDiedTask(const sptr<IRemoteObject> &remoteHost); 139 140 /** 141 * @brief Post event notify to form provider. 142 * 143 * @param formEvent The vector of form ids. 144 * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE. 145 * @param want The want of the form. 146 * @param remoteObject The form provider proxy object. 147 */ 148 void PostEventNotifyTask(const std::vector<int64_t> &formEvent, const int32_t formVisibleType, const Want &want, 149 const sptr<IRemoteObject> &remoteObject); 150 151 /** 152 * @brief Post message event to form provider. 153 * @param formId The Id of the from. 154 * @param message Event message. 155 * @param want The want of the request. 156 * @param remoteObject Form provider proxy object. 157 */ 158 void PostFormEventTask(const int64_t formId, const std::string &message, const Want &want, 159 const sptr<IRemoteObject> &remoteObject); 160 161 /** 162 * @brief Post acquire state to form provider. 163 * @param wantArg The want of onAcquireFormState. 164 * @param provider The provider info. 165 * @param want The want of the request. 166 * @param remoteObject Form provider proxy object. 167 */ 168 void PostAcquireStateTask(const Want &wantArg, const std::string &provider, const Want &want, 169 const sptr <IRemoteObject> &remoteObject); 170 171 /** 172 * @brief Post acquire data to form provider. 173 * @param formId The Id of the from. 174 * @param want The want of the request. 175 * @param remoteObject Form provider proxy object. 176 */ 177 void PostAcquireDataTask(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject); 178 179 /** 180 * @brief Post uninstall message to form host(task). 181 * @param formIds The Id list of the forms. 182 * @param remoteObject Form provider proxy object. 183 */ 184 void PostUninstallTaskToHost(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &remoteObject); 185 186 /** 187 * @brief Post acquire form state message to form host(task). 188 * @param state The form state. 189 * @param want The want of onAcquireFormState. 190 * @param remoteObject Form provider proxy object. 191 */ 192 void PostAcquireStateTaskToHost(AppExecFwk::FormState state, const AAFwk::Want &want, 193 const sptr<IRemoteObject> &remoteObject); 194 195 /** 196 * @brief Post acquire form data message to form host(task). 197 * @param wantParams Indicates the data information acquired by the form. 198 * @param requestCode Indicates the requested id. 199 * @param remoteObject Form provider proxy object. 200 */ 201 void PostAcquireDataTaskToHost(const AAFwk::WantParams &wantParams, int64_t requestCode, 202 const sptr<IRemoteObject> &remoteObject); 203 204 /** 205 * @brief Post form share error code to form host(task). 206 * @param formShareRequestCode The request code for this share. 207 * @param result The error code of this share. 208 */ 209 void PostFormShareSendResponse(int64_t formShareRequestCode, int32_t result); 210 211 void PostRenderForm(const FormRecord &formRecord, const Want &want, const sptr<IRemoteObject> &remoteObject); 212 213 void PostStopRenderingForm(const FormRecord &formRecord, const Want &want, const sptr<IRemoteObject> &remoteObject); 214 215 void PostReloadForm(const std::vector<FormRecord> &&formRecords, const Want &want, 216 const sptr<IRemoteObject> &remoteObject); 217 218 void PostAddTaskToHost(const std::string bundleName, const sptr<IRemoteObject> &remoteObject, 219 const RunningFormInfo &runningFormInfo); 220 221 void PostRouterProxyToHost(const int64_t formId, const sptr<IRemoteObject> &remoteObject, const Want &want); 222 223 void PostRemoveTaskToHost(const std::string bundleName, const sptr<IRemoteObject> &remoteObject, 224 const RunningFormInfo &runningFormInfo); 225 226 void PostReleaseRenderer( 227 int64_t formId, const std::string &compId, const std::string &uid, const sptr<IRemoteObject> &remoteObject); 228 229 void PostOnUnlock(const sptr<IRemoteObject> &remoteObject); 230 231 void PostSetVisibleChange(int64_t formId, bool isVisible, const sptr<IRemoteObject> &remoteObject); 232 233 /** 234 * @brief Post Form visible/invisible notify. 235 * @param formIds the Ids of forms need to notify. 236 * @param formInstanceMaps formInstances for visibleNotify. 237 * @param eventMaps eventMaps for event notify. 238 * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE. 239 * @param visibleNotifyDelay delay time. 240 */ 241 void PostVisibleNotify(const std::vector<int64_t> &formIds, 242 std::map<std::string, std::vector<FormInstance>> &formInstanceMaps, 243 std::map<std::string, std::vector<int64_t>> &eventMaps, 244 const int32_t formVisibleType, int32_t visibleNotifyDelay); 245 246 /** 247 * @brief Post form click event. 248 * @param bundleName BundleName of the form host 249 * @param formEventType form event type. 250 * @param remoteObject thr remote observer. 251 * @param runningFormInfo Current form data. 252 */ 253 void PostFormClickEventToHost( 254 const std::string &bundleName, const std::string &formEventType, const sptr<IRemoteObject> &remoteObject, 255 const RunningFormInfo &runningFormInfo); 256 257 /** 258 * @brief Post recycle forms. 259 * @param formIds the Ids of forms to be recycled. 260 * @param want The want of the request. 261 * @param remoteObjectOfHost Form host proxy object. 262 * @param remoteObjectOfRender Form render proxy object. 263 */ 264 void PostRecycleForms(const std::vector<int64_t> &formIds, const Want &want, 265 const sptr<IRemoteObject> &remoteObjectOfHost, const sptr<IRemoteObject> &remoteObjectOfRender); 266 267 /** 268 * @brief Post recover form. 269 * @param formIds the Id of form to be recovered. 270 * @param want The want of the request. 271 * @param remoteObject Form render proxy object. 272 */ 273 void PostRecoverForm(const FormRecord &record, const Want &want, const sptr<IRemoteObject> &remoteObject); 274 275 /** 276 * @brief Cancel delay task. 277 * @param eventMsg Delay Task. 278 */ 279 void CancelDelayTask(const std::pair<int64_t, int64_t> &eventMsg); 280 281 /** 282 * @brief Post BatchRefresh forms. 283 * @param formRefreshType batch refresh forms type. 284 */ 285 void PostBatchRefreshForms(const int32_t formRefreshType); 286 287 /** 288 * @brief Post enable form task. 289 * @param formIds formIds The Id list of the forms. 290 * @param enable True or false. 291 * @param remoteObject Form host proxy object. 292 */ 293 void PostEnableFormsTaskToHost(const std::vector<int64_t> &formIds, const bool enable, 294 const sptr<IRemoteObject> &remoteObject); 295 296 /** 297 * @brief Post task to excute. 298 * @param func Task function. 299 * @param delayMs Task excute delay time(ms). 300 */ 301 void PostTask(const std::function<void()> &func, uint64_t delayMs = FORM_TASK_DELAY_TIME); 302 303 /** 304 * @brief Post re-add form task to form host when FormRenderService is died. 305 * @param remoteObject Form host proxy object. 306 */ 307 void PostFrsDiedTaskToHost(const sptr<IRemoteObject> &remoteObject); 308 309 private: 310 /** 311 * @brief Acquire form data from form provider. 312 * @param formId The Id of the from. 313 * @param want The want of the request. 314 * @param remoteObject Form provider proxy object. 315 */ 316 void AcquireProviderFormInfo(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject); 317 318 /** 319 * @brief Acquire share form data from form provider(task). 320 * @param formId The Id of the form. 321 * @param remoteDeviceId The device ID to share. 322 * @param want The want of the request. 323 * @param remoteObject Form provider proxy object. 324 */ 325 void AcquireShareFormData(int64_t formId, const std::string &remoteDeviceId, const Want &want, 326 const sptr<IRemoteObject> &remoteObject); 327 328 /** 329 * @brief Notify form provider for delete form. 330 * @param formId The Id of the from. 331 * @param want The want of the form. 332 * @param remoteObject Form provider proxy object. 333 */ 334 void NotifyFormDelete(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject); 335 336 /** 337 * @brief Notify form provider for updating form. 338 * @param formId The Id of the from. 339 * @param want The want of the form. 340 * @param remoteObject Form provider proxy object. 341 */ 342 void NotifyFormUpdate(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject); 343 344 /** 345 * @brief Event notify to form provider. 346 * 347 * @param formEvents The vector of form ids. 348 * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE. 349 * @param want The want of the form. 350 * @param remoteObject The form provider proxy object. 351 */ 352 void EventNotify(const std::vector<int64_t> &formEvents, const int32_t formVisibleType, const Want &want, 353 const sptr<IRemoteObject> &remoteObject); 354 355 /** 356 * @brief Notify form provider for cast temp form. 357 * 358 * @param formId The Id of the from. 359 * @param want The want of the form. 360 * @param remoteObject Form provider proxy object. 361 * @return none. 362 */ 363 void NotifyCastTemp(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject); 364 /** 365 * @brief Post form data to form host when acquire form.. 366 * @param formId The Id of the form. 367 * @param record form record. 368 * @param remoteObject Form provider proxy object. 369 */ 370 void AcquireTaskToHost(const int64_t formId, const FormRecord &record, const sptr<IRemoteObject> &remoteObject); 371 372 /** 373 * @brief Post form data to form host when update form. 374 * @param formId The Id of the form. 375 * @param record form record. 376 * @param remoteObject Form provider proxy object. 377 */ 378 void UpdateTaskToHost(const int64_t formId, const FormRecord &record, const sptr<IRemoteObject> &remoteObject); 379 380 /** 381 * @brief Handle form host died. 382 * @param remoteHost Form host proxy object. 383 */ 384 void HostDied(const sptr<IRemoteObject> &remoteHost); 385 386 /** 387 * @brief Post provider batch delete. 388 * @param formIds The Id list. 389 * @param want The want of the request. 390 * @param remoteObject Form provider proxy object. 391 */ 392 void ProviderBatchDelete(std::set<int64_t> &formIds, const Want &want, const sptr<IRemoteObject> &remoteObject); 393 /** 394 * @brief Fire message event to form provider. 395 * @param formId The Id of the from. 396 * @param message Event message. 397 * @param want The want of the request. 398 * @param remoteObject Form provider proxy object. 399 */ 400 void FireFormEvent(const int64_t formId, const std::string &message, const Want &want, 401 const sptr<IRemoteObject> &remoteObject); 402 403 /** 404 * @brief Acquire form state to form provider. 405 * @param wantArg The want of onAcquireFormState. 406 * @param provider The provider info. 407 * @param want The want of the request. 408 * @param remoteObject Form provider proxy object. 409 */ 410 void AcquireState(const Want &wantArg, const std::string &provider, const Want &want, 411 const sptr <IRemoteObject> &remoteObject); 412 /** 413 * @brief Handle acquire data. 414 * @param wantParams Indicates the data information acquired by the form. 415 * @param requestCode Indicates the requested id. 416 * @param remoteObject Form provider proxy object. 417 */ 418 void AcquireFormDataBack(const AAFwk::WantParams &wantParams, int64_t requestCode, 419 const sptr<IRemoteObject> &remoteObject); 420 421 /** 422 * @brief Acquire form data to form provider. 423 * @param formId The Id of the form. 424 * @param want The want of the request. 425 * @param remoteObject Form provider proxy object. 426 */ 427 void AcquireFormData(const int64_t formId, const Want &want, const sptr<IRemoteObject> &remoteObject); 428 429 /** 430 * @brief Handle uninstall message. 431 * @param formIds The Id list of the forms. 432 * @param remoteObject Form provider proxy object. 433 */ 434 void FormUninstall(const std::vector<int64_t> &formIds, const sptr<IRemoteObject> &remoteObject); 435 436 /** 437 * @brief Handle acquire state. 438 * @param state the form state. 439 * @param want The want of onAcquireFormState. 440 * @param remoteObject Form provider proxy object. 441 */ 442 void AcquireStateBack(AppExecFwk::FormState state, const AAFwk::Want &want, 443 const sptr <IRemoteObject> &remoteObject); 444 445 /** 446 * @brief Create form data for form host. 447 * @param formId The Id of the form. 448 * @param record Form record. 449 * @return Form data. 450 */ 451 FormJsInfo CreateFormJsInfo(const int64_t formId, const FormRecord &record); 452 453 /** 454 * @brief Post form share error code to form host(task). 455 * @param formShareRequestCode The request code for this share. 456 * @param result The error code of this share. 457 */ 458 void FormShareSendResponse(int64_t formShareRequestCode, int32_t result); 459 460 /** 461 * @brief Post form share error code to form host(task). 462 * @param formShareRequestCode The request code for this share. 463 * @param result The error code of this share. 464 */ 465 void RenderForm(const FormRecord &formRecord, const Want &want, const sptr<IRemoteObject> &remoteObject); 466 467 void StopRenderingForm(const FormRecord &formRecord, const Want &want, const sptr<IRemoteObject> &remoteObject); 468 469 void ReloadForm(const std::vector<FormRecord> &&formRecords, const Want &want, 470 const sptr<IRemoteObject> &remoteObject); 471 472 void FormAdd(const std::string bundleName, const sptr<IRemoteObject> &remoteObject, 473 const RunningFormInfo &runningFormInfo); 474 475 void FormRemove(const std::string bundleName, const sptr<IRemoteObject> &remoteObject, 476 const RunningFormInfo &runningFormInfo); 477 478 void ReleaseRenderer( 479 int64_t formId, const std::string &compId, const std::string &uid, const sptr<IRemoteObject> &remoteObject); 480 481 void OnUnlock(const sptr<IRemoteObject> &remoteObject); 482 483 void SetVisibleChange(int64_t formId, bool isVisible, const sptr<IRemoteObject> &remoteObject); 484 485 void RemoveConnection(int32_t connectId); 486 487 /** 488 * @brief Form router event proxy. 489 * @param formId The id of the form. 490 * @param remoteObject Form router proxy manager object. 491 * @param want The want of the form for router event. 492 */ 493 void FormRouterEventProxy(const int64_t formId, const sptr<IRemoteObject> &remoteObject, const Want &want); 494 495 /** 496 * @brief Form visible/invisible notify. 497 * @param formIds the Ids of forms need to notify. 498 * @param formInstanceMaps formInstances for visibleNotify. 499 * @param eventMaps eventMaps for event notify. 500 * @param formVisibleType The form visible type, including FORM_VISIBLE and FORM_INVISIBLE. 501 */ 502 void NotifyVisible(const std::vector<int64_t> &formIds, 503 std::map<std::string, std::vector<FormInstance>> formInstanceMaps, 504 std::map<std::string, std::vector<int64_t>> eventMaps, const int32_t formVisibleType); 505 506 /** 507 * @brief Handle recycle form message. 508 * @param formId The Id of form to be recycled. 509 * @param remoteObjectOfHost Form host proxy object. 510 * @param remoteObjectOfRender Form render proxy object. 511 */ 512 void RecycleForm(const int64_t &formId, const sptr<IRemoteObject> &remoteObjectOfHost, 513 const sptr<IRemoteObject> &remoteObjectOfRender); 514 515 /** 516 * @brief Handle recover form message. 517 * @param formId The Id of form to be recovered. 518 * @param want The want of the request. 519 * @param remoteObject Form render proxy object. 520 */ 521 void RecoverForm(const FormRecord &record, const Want &want, const sptr<IRemoteObject> &remoteObject); 522 523 /** 524 * @brief Notify remote observer form click event. 525 * @param bundleName BundleName of the form host 526 * @param formEventType form event type. 527 * @param remoteObject thr remote observer. 528 * @param runningFormInfo Current form data. 529 */ 530 void FormClickEvent(const std::string &bundleName, const std::string &formEventType, 531 const sptr<IRemoteObject> &remoteObject, const RunningFormInfo &runningFormInfo); 532 533 /** 534 * @brief Enable form or disable form. 535 * @param formIds formIds The Id list of the forms. 536 * @param enable True or false. 537 * @param remoteObject Form host proxy object. 538 */ 539 void EnableFormsTaskToHost(const std::vector<int64_t> &formIds, const bool enable, 540 const sptr<IRemoteObject> &remoteObject); 541 542 /** 543 * @brief Post re-add form task to form host when FormRenderService is died. 544 * @param remoteObject Form host proxy object. 545 */ 546 void FrsDiedTaskToHost(const sptr<IRemoteObject> &remoteObject); 547 548 private: 549 std::mutex formRecoverTimesMutex_; 550 std::unordered_map<int64_t, int64_t> formLastRecoverTimes; 551 std::shared_ptr<FormSerialQueue> serialQueue_ = nullptr; 552 }; 553 } // namespace AppExecFwk 554 } // namespace OHOS 555 #endif // OHOS_FORM_FWK_FORM_TASK_MGR_H 556