• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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_STATUS_QUEUE_H
17 #define OHOS_FORM_FWK_FORM_STATUS_QUEUE_H
18 
19 #include <map>
20 #include <unordered_map>
21 #include <shared_mutex>
22 #include <string>
23 
24 #include <singleton.h>
25 #include "ffrt.h"
26 #include "iremote_object.h"
27 #include "form_command_queue.h"
28 #include "form_status_mgr.h"
29 
30 namespace OHOS {
31 namespace AppExecFwk {
32 namespace {
33 constexpr int32_t FORM_MAX_TIME_OUT = 5000; // ms
34 }
35 
36 class FormStatusQueue final : public DelayedRefSingleton<FormStatusQueue> {
37     DECLARE_DELAYED_REF_SINGLETON(FormStatusQueue)
38 
39 public:
40     DISALLOW_COPY_AND_MOVE(FormStatusQueue);
41     bool ScheduleTask(uint64_t ms, std::function<void()> func);
42     void ScheduleDelayTask(const std::pair<int64_t, int64_t> &eventMsg, uint32_t ms, std::function<void()> func);
43     void CancelDelayTask(const std::pair<int64_t, int64_t> &eventMsg);
44 
45     void PostFormStatusTask(FormCommand formCommand, sptr<IRemoteObject> remoteObjectOfHost = nullptr);
46     void PostFormDeleteTask(FormCommand formCommand, const std::string compId);
47 
48     void PostFormCommandTask(std::shared_ptr<FormCommandQueue> formCommandQueue, const int64_t formId);
49     void PostFormCommandTaskByFormId(const int64_t formId);
50     void ProcessTask(FormCommand &formCommand);
51 
52     void PostTimeOutReAddForm(const int64_t formId);
53     void CancelTimeOutReAddForm(const int64_t &formId);
54 
55 private:
56     std::shared_ptr<FormCommandQueue> GetOrCreateFormStatusQueue(
57         const int64_t formId, const sptr<IRemoteObject> &remoteObjectOfHost, FormStatus formStatus);
58     void DeleteFormStatusQueueIfNecessary(const int64_t formId, const std::string compId);
59 
60     void TimeOutReAddForm(const int64_t &formId, const sptr<IRemoteObject> &remoteObjectOfHost);
61 
62     //<formid, CommandQueue>
63     std::shared_mutex formCommandQueueMapMutex_;
64     std::unordered_map<int64_t, std::shared_ptr<FormCommandQueue>> formCommandQueueMap_;
65     // <formId, hostToken>
66     std::unordered_map<int64_t, sptr<IRemoteObject>> formHostTokenMap_;
67 
68     std::shared_mutex mutex_;
69     std::map<std::pair<int64_t, int64_t>, ffrt::task_handle> taskMap_;
70     ffrt::queue queue_;
71 };
72 }
73 }
74 #endif // OHOS_FORM_FWK_FORM_STATUS_QUEUE_H
75