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_ACE_FRAMEWORKS_CORE_COMPONENTS_FORM_RESOURCE_FORM_CALLBACK_CLIENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_FORM_RESOURCE_FORM_CALLBACK_CLIENT_H
18 
19 #include "form_callback_interface.h"
20 
21 #include "core/common/ace_engine.h"
22 namespace OHOS::Ace {
23 
24 class FormCallbackClient : public OHOS::AppExecFwk::FormCallbackInterface,
25     public std::enable_shared_from_this<FormCallbackClient> {
26 public:
27     FormCallbackClient() = default;
28     virtual ~FormCallbackClient() = default;
29 
ProcessFormUpdate(const AppExecFwk::FormJsInfo & formJsInfo)30     void ProcessFormUpdate(const AppExecFwk::FormJsInfo& formJsInfo) override
31     {
32         auto container = AceEngine::Get().GetContainer(instanceId_);
33         CHECK_NULL_VOID(container);
34         ContainerScope scope(instanceId_);
35         auto taskExecutor = container->GetTaskExecutor();
36         CHECK_NULL_VOID(taskExecutor);
37         taskExecutor->PostTask(
38             [delegate = delegate_, formJsInfo]() {
39                 auto formManagerDelegate = delegate.Upgrade();
40                 if (formManagerDelegate) {
41                     formManagerDelegate->ProcessFormUpdate(formJsInfo);
42                 }
43             },
44             TaskExecutor::TaskType::UI, "ArkUIFormProcessUpdate");
45     }
46 
ProcessFormUninstall(const int64_t formId)47     void ProcessFormUninstall(const int64_t formId) override
48     {
49         auto container = AceEngine::Get().GetContainer(instanceId_);
50         CHECK_NULL_VOID(container);
51         ContainerScope scope(instanceId_);
52         auto taskExecutor = container->GetTaskExecutor();
53         CHECK_NULL_VOID(taskExecutor);
54         taskExecutor->PostTask(
55             [delegate = delegate_, formId]() {
56                 auto formManagerDelegate = delegate.Upgrade();
57                 if (formManagerDelegate) {
58                     formManagerDelegate->ProcessFormUninstall(formId);
59                 }
60             },
61             TaskExecutor::TaskType::UI, "ArkUIFormProcessUninstall");
62     }
63 
OnDeathReceived()64     void OnDeathReceived() override
65     {
66         auto container = AceEngine::Get().GetContainer(instanceId_);
67         CHECK_NULL_VOID(container);
68         ContainerScope scope(instanceId_);
69         auto taskExecutor = container->GetTaskExecutor();
70         CHECK_NULL_VOID(taskExecutor);
71         taskExecutor->PostTask(
72             [delegate = delegate_]() {
73                 auto formManagerDelegate = delegate.Upgrade();
74                 if (formManagerDelegate) {
75                     formManagerDelegate->OnDeathReceived();
76                 }
77             },
78             TaskExecutor::TaskType::UI, "ArkUIFormDeathReceived");
79     }
80 
OnError(const int32_t errorCode,const std::string & errorMsg)81     void OnError(const int32_t errorCode, const std::string& errorMsg) override
82     {
83         auto container = AceEngine::Get().GetContainer(instanceId_);
84         CHECK_NULL_VOID(container);
85         ContainerScope scope(instanceId_);
86         auto taskExecutor = container->GetTaskExecutor();
87         CHECK_NULL_VOID(taskExecutor);
88         taskExecutor->PostTask(
89             [delegate = delegate_, errorCode, errorMsg]() {
90                 auto formManagerDelegate = delegate.Upgrade();
91                 if (formManagerDelegate) {
92                     formManagerDelegate->OnFormError(std::to_string(errorCode), errorMsg);
93                 }
94             },
95             TaskExecutor::TaskType::UI, "ArkUIFormError");
96     }
97 
SetFormManagerDelegate(WeakPtr<FormManagerDelegate> delegate)98     void SetFormManagerDelegate(WeakPtr<FormManagerDelegate> delegate)
99     {
100         delegate_ = delegate;
101     }
102 
SetInstanceId(const int32_t instanceId)103     void SetInstanceId(const int32_t instanceId)
104     {
105         instanceId_ = instanceId;
106     }
107 
ProcessRecycleForm()108     void ProcessRecycleForm() override
109     {
110         auto container = AceEngine::Get().GetContainer(instanceId_);
111         CHECK_NULL_VOID(container);
112         ContainerScope scope(instanceId_);
113         auto taskExecutor = container->GetTaskExecutor();
114         CHECK_NULL_VOID(taskExecutor);
115         taskExecutor->PostTask(
116             [delegate = delegate_]() {
117                 auto formManagerDelegate = delegate.Upgrade();
118                 if (formManagerDelegate) {
119                     formManagerDelegate->ProcessRecycleForm();
120                 }
121             },
122             TaskExecutor::TaskType::UI, "ArkUIFormProcessRecycleForm");
123     }
124 
ProcessEnableForm(bool enable)125     void ProcessEnableForm(bool enable) override
126     {
127         TAG_LOGI(AceLogTag::ACE_FORM, "FormCallbackClient::ProcessEnableForm");
128         auto container = AceEngine::Get().GetContainer(instanceId_);
129         CHECK_NULL_VOID(container);
130         ContainerScope scope(instanceId_);
131         auto taskExecutor = container->GetTaskExecutor();
132         CHECK_NULL_VOID(taskExecutor);
133         taskExecutor->PostTask(
134             [delegate = delegate_, enable]() {
135                 auto formManagerDelegate = delegate.Upgrade();
136                 if (!formManagerDelegate) {
137                     TAG_LOGE(AceLogTag::ACE_FORM, "formManagerDelegate is nullptr");
138                     return;
139                 }
140                 formManagerDelegate->ProcessEnableForm(enable);
141             },
142             TaskExecutor::TaskType::UI, "ArkUIFormProcessEnableForm");
143     }
144 
145 private:
146     int32_t instanceId_ = -1;
147     WeakPtr<FormManagerDelegate> delegate_;
148 };
149 
150 } // namespace OHOS::Ace
151 
152 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_FORM_RESOURCE_FORM_CALLBACK_CLIENT_H
153