• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #ifndef FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_DECLARATIVE_FRONTEND_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_DECLARATIVE_FRONTEND_H
18 
19 #include <string>
20 #include <unordered_map>
21 
22 #include "base/memory/ace_type.h"
23 #include "base/utils/noncopyable.h"
24 #include "base/utils/string_utils.h"
25 #include "bridge/declarative_frontend/ng/page_router_manager.h"
26 #include "core/common/ace_page.h"
27 #include "core/common/container.h"
28 #include "core/common/frontend.h"
29 #include "core/common/js_message_dispatcher.h"
30 #include "core/pipeline_ng/ui_task_scheduler.h"
31 #include "frameworks/bridge/declarative_frontend/frontend_delegate_declarative.h"
32 #include "frameworks/bridge/js_frontend/engine/common/js_engine.h"
33 
34 namespace OHOS::Ace {
35 
36 // DeclarativeFrontend is the unique entrance from ACE backend to frontend.
37 // The relationship between AceActivity, AceContainer and DeclarativeFrontend is 1:1:1.
38 // So DeclarativeFrontend would be responsible for below things:
39 // - Create and initialize JS engine.
40 // - Load pages of a JS app, and parse the manifest.json before loading main page.
41 // - Maintain the page stack of JS app by FrontendDelegateDeclarative.
42 // - Lifecycle of JS app (also AceActivity).
43 class ACE_EXPORT DeclarativeFrontend : public Frontend {
44     DECLARE_ACE_TYPE(DeclarativeFrontend, Frontend);
45 
46 public:
47     DeclarativeFrontend() = default;
48     ~DeclarativeFrontend() override;
49 
50     bool Initialize(FrontendType type, const RefPtr<TaskExecutor>& taskExecutor) override;
51 
52     void Destroy() override;
53 
54     void AttachPipelineContext(const RefPtr<PipelineBase>& context) override;
55 
56     void SetAssetManager(const RefPtr<AssetManager>& assetManager) override;
57 
58     void RunPage(int32_t pageId, const std::string& url, const std::string& params) override;
59 
60     void ReplacePage(const std::string& url, const std::string& params) override;
61 
62     void PushPage(const std::string& url, const std::string& params) override;
63 
64     // Js frontend manages all pages self.
AddPage(const RefPtr<AcePage> & page)65     void AddPage(const RefPtr<AcePage>& page) override {};
66 
GetPage(int32_t)67     RefPtr<AcePage> GetPage(int32_t /*pageId*/) const override
68     {
69         return nullptr;
70     };
71 
72     std::string GetCurrentPageUrl() const override;
73 
74     // Get the currently running JS page information in NG structure.
75     RefPtr<Framework::RevSourceMap> GetCurrentPageSourceMap() const override;
76 
77     // Get the currently running JS page information in NG structure.
78     RefPtr<Framework::RevSourceMap> GetFaAppSourceMap() const override;
79 
80     void GetStageSourceMap(
81         std::unordered_map<std::string, RefPtr<Framework::RevSourceMap>>& sourceMap) const override;
82 
83     RefPtr<NG::PageRouterManager> GetPageRouterManager() const;
84 
85     void SendCallbackMessage(const std::string& callbackId, const std::string& data) const override;
86     // platform channel.
87     void SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher>& dispatcher) const override;
88     void TransferComponentResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const override;
89     void TransferJsResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const override;
90 #if defined(PREVIEW)
SetPagePath(const std::string & pagePath)91     void SetPagePath(const std::string& pagePath)
92     {
93         if (delegate_) {
94             delegate_->SetPagePath(pagePath);
95         }
96     }
RunNativeEngineLoop()97     void RunNativeEngineLoop() override
98     {
99         if (jsEngine_) {
100             jsEngine_->RunNativeEngineLoop();
101         }
102     }
103 
104     void TransferJsResponseDataPreview(int32_t callbackId, int32_t code, ResponseData responseData) const;
105     RefPtr<Component> GetNewComponentWithJsCode(const std::string& jsCode, const std::string& viewID);
InitializeModuleSearcher(const std::string & bundleName,const std::string & moduleName,const std::string & assetPath,bool isBundle)106     void InitializeModuleSearcher(const std::string& bundleName, const std::string& moduleName,
107                                   const std::string& assetPath, bool isBundle)
108     {
109         if (jsEngine_) {
110             jsEngine_->InitializeModuleSearcher(bundleName, moduleName, assetPath, isBundle);
111         }
112     }
113 #endif
114     void TransferJsPluginGetError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const override;
115     void TransferJsEventData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const override;
116     void LoadPluginJsCode(std::string&& jsCode) const override;
117     void LoadPluginJsByteCode(std::vector<uint8_t>&& jsCode, std::vector<int32_t>&& jsCodeLen) const override;
118 
119     // application lifecycle.
120     void UpdateState(Frontend::State state) override;
121 
122     // page lifecycle.
123     bool OnBackPressed() override;
124     void OnShow() override;
125     void OnHide() override;
126     void OnConfigurationUpdated(const std::string& data) override;
127     void OnSaveAbilityState(std::string& data) override;
128     void OnRestoreAbilityState(const std::string& data) override;
129     void OnNewWant(const std::string& data) override;
130     void OnActive() override;
131     void OnInactive() override;
132     bool OnStartContinuation() override;
133     void OnCompleteContinuation(int32_t code) override;
134     void OnSaveData(std::string& data) override;
135     void GetPluginsUsed(std::string& data) override;
136     bool OnRestoreData(const std::string& data) override;
137     void OnRemoteTerminated() override;
138     void OnNewRequest(const std::string& data) override;
139     void OnMemoryLevel(const int32_t level) override;
140     void CallRouterBack() override;
141     void OnSurfaceChanged(int32_t width, int32_t height) override;
142 
143     void DumpFrontend() const override;
144     std::string GetPagePath() const override;
145     void TriggerGarbageCollection() override;
146     void DumpHeapSnapshot(bool isPrivate) override;
147     void SetColorMode(ColorMode colorMode) override;
148     void RebuildAllPages() override;
149     void NotifyAppStorage(const std::string& key, const std::string& value) override;
GetEventHandler()150     RefPtr<AceEventHandler> GetEventHandler() override
151     {
152         return handler_;
153     };
154 
GetType()155     FrontendType GetType() override
156     {
157         return type_;
158     }
159 
160     // judge frontend is foreground frontend.
IsForeground()161     bool IsForeground() override
162     {
163         return foregroundFrontend_;
164     }
165 
166     RefPtr<AccessibilityManager> GetAccessibilityManager() const override;
167     WindowConfig& GetWindowConfig() override;
168 
169     // navigator component call router
170     void NavigatePage(uint8_t type, const PageTarget& target, const std::string& params) override;
171 
172     // distribute
173     std::string RestoreRouterStack(const std::string& contentInfo) override;
174     std::string GetContentInfo() const override;
175     int32_t GetRouterSize() const override;
176 
177     void OnWindowDisplayModeChanged(bool isShownInMultiWindow, const std::string& data);
178 
SetJsEngine(const RefPtr<Framework::JsEngine> & jsEngine)179     void SetJsEngine(const RefPtr<Framework::JsEngine>& jsEngine)
180     {
181         jsEngine_ = jsEngine;
182     }
183 
SetNeedDebugBreakPoint(bool value)184     void SetNeedDebugBreakPoint(bool value)
185     {
186         if (jsEngine_) {
187             jsEngine_->SetNeedDebugBreakPoint(value);
188         }
189     }
190 
SetDebugVersion(bool value)191     void SetDebugVersion(bool value)
192     {
193         if (jsEngine_) {
194             jsEngine_->SetDebugVersion(value);
195         }
196     }
197 
SetInstanceName(const std::string & name)198     void SetInstanceName(const std::string& name)
199     {
200         if (jsEngine_) {
201             jsEngine_->SetInstanceName(name);
202         }
203     }
204 
SetPageProfile(const std::string & pageProfile)205     void SetPageProfile(const std::string& pageProfile)
206     {
207         pageProfile_ = pageProfile;
208     }
209 
MarkIsSubWindow(bool isSubWindow)210     void MarkIsSubWindow(bool isSubWindow)
211     {
212         isSubWindow_ = isSubWindow;
213     }
214 
GetJsEngine()215     RefPtr<Framework::JsEngine> GetJsEngine()
216     {
217         return jsEngine_;
218     }
219 
220     void AttachSubPipelineContext(const RefPtr<PipelineContext>& context);
221 
222     void FlushReload() override;
223     void HotReload() override;
224 
GetDelegate()225     RefPtr<Framework::FrontendDelegate> GetDelegate() const
226     {
227         return AceType::DynamicCast<Framework::FrontendDelegate>(delegate_);
228     }
229 
230 protected:
231     bool isFormRender_ = false;
232     RefPtr<Framework::FrontendDelegateDeclarative> delegate_;
233 
234 private:
235     void InitializeFrontendDelegate(const RefPtr<TaskExecutor>& taskExecutor);
236 
237     FrontendType type_ = FrontendType::DECLARATIVE_JS;
238     RefPtr<AceEventHandler> handler_;
239     RefPtr<Framework::JsEngine> jsEngine_;
240     RefPtr<AccessibilityManager> accessibilityManager_;
241     std::string pageProfile_;
242     bool foregroundFrontend_ = false;
243     bool isSubWindow_ = false;
244 
245     ACE_DISALLOW_COPY_AND_MOVE(DeclarativeFrontend);
246 };
247 
248 class DeclarativeEventHandler : public AceEventHandler {
249 public:
DeclarativeEventHandler(const RefPtr<Framework::FrontendDelegateDeclarative> & delegate)250     explicit DeclarativeEventHandler(const RefPtr<Framework::FrontendDelegateDeclarative>& delegate)
251         : delegate_(delegate)
252     {
253         ACE_DCHECK(delegate_);
254     }
DeclarativeEventHandler()255     DeclarativeEventHandler() {}
256 
257     ~DeclarativeEventHandler() override = default;
258 
259     void HandleAsyncEvent(const EventMarker& eventMarker) override;
260 
261     void HandleAsyncEvent(const EventMarker& eventMarker, int32_t param) override;
262 
263     void HandleAsyncEvent(const EventMarker& eventMarker, const BaseEventInfo& info) override;
264 
265     void HandleAsyncEvent(const EventMarker& eventMarker, const std::shared_ptr<BaseEventInfo>& info) override;
266 
267     void HandleAsyncEvent(const EventMarker& eventMarker, const KeyEvent& info) override;
268 
269     void HandleAsyncEvent(const EventMarker& eventMarker, const std::string& param) override;
270 
271     void HandleSyncEvent(const EventMarker& eventMarker, bool& result) override;
272 
273     void HandleSyncEvent(const EventMarker& eventMarker, const BaseEventInfo& info, bool& result) override;
274 
275     void HandleSyncEvent(const EventMarker& eventMarker, const KeyEvent& info, bool& result) override;
276 
277     void HandleSyncEvent(const EventMarker& eventMarker, const std::string& param, std::string& result) override;
278 
279     void HandleSyncEvent(const EventMarker& eventMarker, const std::shared_ptr<BaseEventInfo>& info) override;
280 
281     void HandleSyncEvent(const EventMarker& eventMarker, const std::string& componentId, const int32_t nodeId,
282         const bool isDestroy) override;
283 
284 private:
285     RefPtr<Framework::FrontendDelegateDeclarative> delegate_;
286 };
287 
288 } // namespace OHOS::Ace
289 
290 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_DECLARATIVE_FRONTEND_H
291