• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "frameworks/bridge/card_frontend/card_frontend_declarative.h"
17 
18 #include <memory>
19 #include <vector>
20 
21 #include "base/log/event_report.h"
22 #include "base/log/log_wrapper.h"
23 #include "base/utils/utils.h"
24 #include "core/common/thread_checker.h"
25 #include "frameworks/bridge/common/utils/utils.h"
26 #include "frameworks/core/pipeline_ng/pipeline_context.h"
27 
28 namespace OHOS::Ace {
29 namespace {
30 
31 const char FILE_TYPE_BIN[] = ".abc";
32 
33 } // namespace
34 
~CardFrontendDeclarative()35 CardFrontendDeclarative::~CardFrontendDeclarative()
36 {
37     LOG_DESTROY();
38 }
39 
Initialize(FrontendType type,const RefPtr<TaskExecutor> & taskExecutor)40 bool CardFrontendDeclarative::Initialize(FrontendType type, const RefPtr<TaskExecutor>& taskExecutor)
41 {
42     type_ = type;
43     taskExecutor_ = taskExecutor;
44     InitializeDelegate(taskExecutor);
45     manifestParser_ = AceType::MakeRefPtr<Framework::ManifestParser>();
46     return true;
47 }
48 
InitializeDelegate(const RefPtr<TaskExecutor> & taskExecutor)49 void CardFrontendDeclarative::InitializeDelegate(const RefPtr<TaskExecutor>& taskExecutor)
50 {
51     auto pageRouterManager = AceType::MakeRefPtr<NG::PageRouterManager>();
52     delegate_ = AceType::MakeRefPtr<Framework::CardFrontendDelegateDeclarative>(taskExecutor);
53     delegate_->SetPageRouterManager(pageRouterManager);
54 }
55 
GetPageRouterManager() const56 RefPtr<NG::PageRouterManager> CardFrontendDeclarative::GetPageRouterManager() const
57 {
58     CHECK_NULL_RETURN(delegate_, nullptr);
59     return delegate_->GetPageRouterManager();
60 }
61 
Destroy()62 void CardFrontendDeclarative::Destroy()
63 {
64     CHECK_RUN_ON(JS);
65     delegate_.Reset();
66     eventHandler_.Reset();
67 }
68 
AttachPipelineContext(const RefPtr<PipelineBase> & context)69 void CardFrontendDeclarative::AttachPipelineContext(const RefPtr<PipelineBase>& context)
70 {
71     auto pipelineContext = DynamicCast<NG::PipelineContext>(context);
72     CHECK_NULL_VOID(delegate_);
73     CHECK_NULL_VOID(pipelineContext);
74     eventHandler_ = AceType::MakeRefPtr<CardEventHandlerDeclarative>(delegate_);
75 
76     holder_.Attach(context);
77     delegate_->AttachPipelineContext(context);
78 }
79 
SetAssetManager(const RefPtr<AssetManager> & assetManager)80 void CardFrontendDeclarative::SetAssetManager(const RefPtr<AssetManager>& assetManager)
81 {
82     assetManager_ = assetManager;
83     if (delegate_) {
84         delegate_->SetAssetManager(assetManager);
85     }
86 }
87 
RunPage(const std::string & url,const std::string & params)88 UIContentErrorCode CardFrontendDeclarative::RunPage(const std::string& url, const std::string& params)
89 {
90     std::string urlPath;
91     if (GetFormSrc().empty()) {
92         ParseManifest();
93         if (!url.empty()) {
94             urlPath = manifestParser_->GetRouter()->GetPagePath(url, FILE_TYPE_BIN);
95         }
96         if (urlPath.empty()) {
97             urlPath = manifestParser_->GetRouter()->GetEntry(FILE_TYPE_BIN);
98         }
99     } else {
100         urlPath = GetFormSrcPath(GetFormSrc(), FILE_TYPE_BIN);
101     }
102     if (urlPath.empty()) {
103         return UIContentErrorCode::NULL_URL;
104     }
105 
106     if (delegate_) {
107         auto container = Container::Current();
108         if (!container) {
109             return UIContentErrorCode::NULL_POINTER;
110         }
111         container->SetCardFrontend(AceType::WeakClaim(this), cardId_);
112         return delegate_->RunCard(urlPath, params, "", cardId_);
113     }
114 
115     return UIContentErrorCode::NULL_POINTER;
116 }
117 
OnPageLoaded(const RefPtr<Framework::JsAcePage> & page)118 void CardFrontendDeclarative::OnPageLoaded(const RefPtr<Framework::JsAcePage>& page)
119 {
120     CHECK_RUN_ON(JS);
121     // Pop all JS command and execute them in UI thread.
122     auto jsCommands = std::make_shared<std::vector<RefPtr<Framework::JsCommand>>>();
123     page->PopAllCommands(*jsCommands);
124     page->SetPipelineContext(holder_.Get());
125     taskExecutor_->PostTask(
126         [weak = AceType::WeakClaim(this), page, jsCommands] {
127             auto frontend = weak.Upgrade();
128             CHECK_NULL_VOID(frontend);
129             // Flush all JS commands.
130             for (const auto& command : *jsCommands) {
131                 command->Execute(page);
132             }
133 
134             auto pipelineContext = AceType::DynamicCast<PipelineContext>(frontend->holder_.Get());
135             CHECK_NULL_VOID(pipelineContext);
136             auto minSdk = frontend->manifestParser_->GetMinPlatformVersion();
137             pipelineContext->SetMinPlatformVersion(minSdk);
138 
139             auto document = page->GetDomDocument();
140             if (frontend->pageLoaded_) {
141                 page->ClearShowCommand();
142                 std::vector<NodeId> dirtyNodes;
143                 page->PopAllDirtyNodes(dirtyNodes);
144                 if (dirtyNodes.empty()) {
145                     return;
146                 }
147                 auto rootNodeId = dirtyNodes.front();
148                 if (rootNodeId == DOM_ROOT_NODE_ID_BASE) {
149                     auto patchComponent = page->BuildPagePatch(rootNodeId);
150                     if (patchComponent) {
151                         pipelineContext->ScheduleUpdate(patchComponent);
152                     }
153                 }
154                 if (document) {
155                     // When a component is configured with "position: fixed", there is a proxy node in root tree
156                     // instead of the real composed node. So here updates the real composed node.
157                     for (int32_t nodeId : document->GetProxyRelatedNodes()) {
158                         auto patchComponent = page->BuildPagePatch(nodeId);
159                         if (patchComponent) {
160                             pipelineContext->ScheduleUpdate(patchComponent);
161                         }
162                     }
163                 }
164                 return;
165             }
166 
167             // Just clear all dirty nodes.
168             page->ClearAllDirtyNodes();
169             if (document) {
170                 document->HandleComponentPostBinding();
171             }
172             if (pipelineContext->GetAccessibilityManager()) {
173                 pipelineContext->GetAccessibilityManager()->HandleComponentPostBinding();
174             }
175             if (pipelineContext->CanPushPage()) {
176                 pipelineContext->PushPage(page->BuildPage(page->GetUrl()));
177                 frontend->pageLoaded_ = true;
178             }
179         },
180         TaskExecutor::TaskType::UI);
181 }
182 
UpdateData(const std::string & dataList)183 void CardFrontendDeclarative::UpdateData(const std::string& dataList)
184 {
185     taskExecutor_->PostTask(
186         [weak = AceType::WeakClaim(this), dataList] {
187             auto frontend = weak.Upgrade();
188             if (frontend) {
189                 frontend->UpdatePageData(dataList);
190             }
191         },
192         TaskExecutor::TaskType::UI); // eTSCard UI == Main JS/UI/PLATFORM
193 }
194 
UpdatePageData(const std::string & dataList)195 void CardFrontendDeclarative::UpdatePageData(const std::string& dataList)
196 {
197     CHECK_RUN_ON(UI); // eTSCard UI == Main JS/UI/PLATFORM
198     if (!delegate_) {
199         return;
200     }
201     delegate_->UpdatePageData(dataList);
202 }
203 
SetColorMode(ColorMode colorMode)204 void CardFrontendDeclarative::SetColorMode(ColorMode colorMode)
205 {
206     taskExecutor_->PostTask(
207         [weak = AceType::WeakClaim(this), colorMode]() {
208             auto frontend = weak.Upgrade();
209             if (frontend) {
210                 frontend->colorMode_ = colorMode;
211                 if (!frontend->delegate_) {
212                     return;
213                 }
214                 frontend->OnMediaFeatureUpdate();
215             }
216         },
217         TaskExecutor::TaskType::JS);
218 }
219 
RebuildAllPages()220 void CardFrontendDeclarative::RebuildAllPages()
221 {
222 }
223 
OnSurfaceChanged(int32_t width,int32_t height)224 void CardFrontendDeclarative::OnSurfaceChanged(int32_t width, int32_t height)
225 {
226     taskExecutor_->PostTask(
227         [weak = AceType::WeakClaim(this), width, height] {
228             auto frontend = weak.Upgrade();
229             if (frontend) {
230                 frontend->HandleSurfaceChanged(width, height);
231             }
232         },
233         TaskExecutor::TaskType::JS);
234 }
235 
HandleSurfaceChanged(int32_t width,int32_t height)236 void CardFrontendDeclarative::HandleSurfaceChanged(int32_t width, int32_t height)
237 {
238     CHECK_RUN_ON(JS);
239     OnMediaFeatureUpdate();
240 }
241 
OnMediaFeatureUpdate()242 void CardFrontendDeclarative::OnMediaFeatureUpdate()
243 {
244     CHECK_RUN_ON(JS);
245 }
246 
247 } // namespace OHOS::Ace
248