1 /*
2  * Copyright (c) 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 FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_DISTRIBUTE_UI_MANAGER_H
17 #define FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_DISTRIBUTE_UI_MANAGER_H
18 
19 #include "interfaces/inner_api/ace/serializeable_object.h"
20 
21 #include "adapter/ohos/entrance/ace_container.h"
22 #include "core/common/container_scope.h"
23 #include "core/components_ng/base/distributed_ui.h"
24 
25 namespace OHOS::Ace {
26 class DistributedUIManager {
27 public:
DistributedUIManager(int32_t instanceid,const std::shared_ptr<NG::DistributedUI> & distributedUI)28     DistributedUIManager(int32_t instanceid, const std::shared_ptr<NG::DistributedUI>& distributedUI)
29         : instanceId_(instanceid), distributedUI_(distributedUI)
30     {}
31     ~DistributedUIManager() = default;
32 
DumpUITree()33     SerializeableObjectArray DumpUITree()
34     {
35         SerializeableObjectArray ret;
36         auto task = [this, &ret]() { ret = distributedUI_->DumpUITree(); };
37         PostSyncTaskToUI(task, "ArkUIDistributedDumpUITree");
38         return ret;
39     }
40 
SubscribeUpdate(const std::function<void (int32_t,SerializeableObjectArray &)> & onUpdate)41     void SubscribeUpdate(const std::function<void(int32_t, SerializeableObjectArray&)>& onUpdate)
42     {
43         distributedUI_->SubscribeUpdate(onUpdate);
44     }
45 
UnSubscribeUpdate()46     void UnSubscribeUpdate()
47     {
48         distributedUI_->UnSubscribeUpdate();
49     }
50 
ProcessSerializeableInputEvent(const SerializeableObjectArray & event)51     void ProcessSerializeableInputEvent(const SerializeableObjectArray& event)
52     {
53         auto task = [this, &event]() { distributedUI_->ProcessSerializeableInputEvent(event); };
54         PostSyncTaskToUI(task, "ArkUIDistributedProcessSerializeableInput");
55     }
56 
RestoreUITree(const SerializeableObjectArray & uiTree)57     void RestoreUITree(const SerializeableObjectArray& uiTree)
58     {
59         auto task = [this, &uiTree]() { distributedUI_->RestoreUITree(uiTree); };
60         PostSyncTaskToUI(task, "ArkUIDistributedRestoreUITree");
61     }
62 
UpdateUITree(const SerializeableObjectArray & update)63     void UpdateUITree(const SerializeableObjectArray& update)
64     {
65         auto task = [this, &update]() { distributedUI_->UpdateUITree(update); };
66         PostSyncTaskToUI(task, "ArkUIDistributedUpdateUITree");
67     }
68 
SubscribeInputEventProcess(const std::function<void (SerializeableObjectArray &)> & onEvent)69     void SubscribeInputEventProcess(const std::function<void(SerializeableObjectArray&)>& onEvent)
70     {
71         distributedUI_->SubscribeInputEventProcess(onEvent);
72     }
73 
UnSubscribeInputEventProcess()74     void UnSubscribeInputEventProcess()
75     {
76         distributedUI_->UnSubscribeInputEventProcess();
77     }
78 
79 private:
PostSyncTaskToUI(const std::function<void ()> & task,const std::string & name)80     void PostSyncTaskToUI(const std::function<void()>& task, const std::string& name)
81     {
82         auto container = Platform::AceContainer::GetContainer(instanceId_);
83         CHECK_NULL_VOID(container);
84         auto taskExecutor = container->GetTaskExecutor();
85         CHECK_NULL_VOID(taskExecutor);
86         ContainerScope scope(instanceId_);
87         taskExecutor->PostSyncTask(task, TaskExecutor::TaskType::UI, name);
88     }
89 
90     int32_t instanceId_ = -1;
91     std::shared_ptr<NG::DistributedUI> distributedUI_;
92 };
93 } // namespace OHOS::Ace
94 
95 #endif // FOUNDATION_ACE_ADAPTER_OHOS_ENTRANCE_DISTRIBUTE_UI_MANAGER_H
96