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 #include "core/components/test/json/json_frontend.h"
17
18 #include "base/log/dump_log.h"
19 #include "core/components/test/json/json_ace_page.h"
20 #include "core/pipeline/pipeline_context.h"
21
22 namespace OHOS::Ace {
23
CreateDefault()24 RefPtr<Frontend> Frontend::CreateDefault()
25 {
26 return AceType::MakeRefPtr<JsonFrontend>();
27 }
28
Initialize(FrontendType type,const RefPtr<TaskExecutor> &)29 bool JsonFrontend::Initialize(FrontendType type, const RefPtr<TaskExecutor>&)
30 {
31 // This interface is inherited from base class 'Frontend', but we never use 'TaskExecutor'.
32 type_ = type;
33 return true;
34 }
35
AttachPipelineContext(const RefPtr<PipelineBase> & context)36 void JsonFrontend::AttachPipelineContext(const RefPtr<PipelineBase>& context)
37 {
38 pipelineContext_ = context;
39 }
40
SetAssetManager(const RefPtr<AssetManager> &)41 void JsonFrontend::SetAssetManager(const RefPtr<AssetManager>&)
42 {
43 // This interface is inherited from base class 'Frontend', but we never use 'AssetManager'.
44 }
45
AddPage(const RefPtr<AcePage> & page)46 void JsonFrontend::AddPage(const RefPtr<AcePage>& page)
47 {
48 if (!page) {
49 LOGE("the page is nullptr");
50 return;
51 }
52 const auto pageId = page->GetPageId();
53 const auto result = pageMap_.try_emplace(pageId, page);
54 if (!result.second) {
55 LOGW("the page has already in the map");
56 }
57 }
58
GetPage(int32_t pageId) const59 RefPtr<AcePage> JsonFrontend::GetPage(int32_t pageId) const
60 {
61 const auto iter = pageMap_.find(pageId);
62 if (iter == pageMap_.end()) {
63 LOGE("the page is not in the page map, id is %{public}d", pageId);
64 return nullptr;
65 }
66 return iter->second;
67 }
68
RunPage(int32_t pageId,const std::string & content,const std::string & params)69 void JsonFrontend::RunPage(int32_t pageId, const std::string& content, const std::string& params)
70 {
71 if (!pipelineContext_) {
72 LOGE("Not attached to pipeline context yet");
73 return;
74 }
75
76 auto page = GetPage(pageId);
77 if (!page) {
78 LOGE("the page is nullptr");
79 return;
80 }
81 auto taskRunner = pipelineContext_->GetTaskExecutor();
82 if (!taskRunner) {
83 LOGE("the taskRunner is nullptr");
84 return;
85 }
86 taskRunner->PostTask(
87 [page, content, context = AceType::DynamicCast<PipelineContext>(pipelineContext_)] {
88 auto pageComponent = page->BuildPage(content);
89 context->PushPage(pageComponent);
90 },
91 TaskExecutor::TaskType::UI);
92 }
93
PushPage(const std::string & content,const std::string & params)94 void JsonFrontend::PushPage(const std::string& content, const std::string& params) {}
95
UpdatePage(int32_t pageId,const std::string & content)96 void JsonFrontend::UpdatePage(int32_t pageId, const std::string& content)
97 {
98 if (!pipelineContext_) {
99 LOGE("Not attached to pipeline context yet");
100 return;
101 }
102
103 auto page = GetPage(pageId);
104 if (!page) {
105 LOGE("the page is nullptr");
106 return;
107 }
108 RefPtr<JsonAcePage> jsonPage = AceType::DynamicCast<JsonAcePage>(page);
109 auto taskRunner = pipelineContext_->GetTaskExecutor();
110 if (!taskRunner) {
111 LOGE("the taskRunner is nullptr");
112 return;
113 }
114 taskRunner->PostTask(
115 [jsonPage, content, context = AceType::DynamicCast<PipelineContext>(pipelineContext_)] {
116 auto patchComponent = jsonPage->BuildPagePatch(content);
117 context->ScheduleUpdate(patchComponent);
118 },
119 TaskExecutor::TaskType::UI);
120 }
121
GetEventHandler()122 RefPtr<AceEventHandler> JsonFrontend::GetEventHandler()
123 {
124 return handler_;
125 }
126
UpdateState(Frontend::State state)127 void JsonFrontend::UpdateState(Frontend::State state)
128 {
129 switch (state) {
130 case Frontend::State::ON_CREATE:
131 LOGD("update state in JsonFrontend with state ON_CREATE");
132 break;
133 case Frontend::State::ON_DESTROY:
134 LOGD("update state in JsonFrontend with state ON_DESTROY");
135 break;
136 case Frontend::State::ON_SHOW:
137 LOGD("update state in JsonFrontend with state ON_SHOW");
138 break;
139 case Frontend::State::ON_HIDE:
140 LOGD("update state in JsonFrontend with state ON_HIDE");
141 break;
142 default:
143 LOGE("error State: %{public}d", state);
144 break;
145 }
146 }
147
IsForeground()148 bool JsonFrontend::IsForeground()
149 {
150 return false;
151 }
152
GetAccessibilityManager() const153 RefPtr<AccessibilityManager> JsonFrontend::GetAccessibilityManager() const
154 {
155 return nullptr;
156 }
157
OnBackPressed()158 bool JsonFrontend::OnBackPressed()
159 {
160 return false;
161 }
162
OnShow()163 void JsonFrontend::OnShow()
164 {
165 LOGD("JsonFrontend OnShow");
166 }
167
OnHide()168 void JsonFrontend::OnHide()
169 {
170 LOGD("JsonFrontend OnHide");
171 }
172
OnActive()173 void JsonFrontend::OnActive()
174 {
175 LOGD("JsonFrontend OnActive");
176 }
177
OnInactive()178 void JsonFrontend::OnInactive()
179 {
180 LOGD("JsonFrontend OnInactive");
181 }
182
OnStartContinuation()183 bool JsonFrontend::OnStartContinuation()
184 {
185 LOGD("JsonFrontend OnStartContinuation");
186 return false;
187 }
188
OnCompleteContinuation(int32_t code)189 void JsonFrontend::OnCompleteContinuation(int32_t code)
190 {
191 LOGD("JsonFrontend OnCompleteContinuation");
192 }
193
OnSaveData(std::string & data)194 void JsonFrontend::OnSaveData(std::string& data)
195 {
196 data = "";
197 LOGD("JsonFrontend OnSaveData %{private}s", data.c_str());
198 }
199
OnRestoreData(const std::string & data)200 bool JsonFrontend::OnRestoreData(const std::string& data)
201 {
202 LOGD("JsonFrontend OnRestoreData");
203 return false;
204 }
205
OnNewRequest(const std::string & data)206 void JsonFrontend::OnNewRequest(const std::string& data)
207 {
208 LOGD("JsonFrontend OnNewRequest");
209 }
210
CallRouterBack()211 void JsonFrontend::CallRouterBack() {}
212
213 } // namespace OHOS::Ace
214