• 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_PLUGIN_FRONTEND_PLUGIN_FRONTEND_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_PLUGIN_FRONTEND_PLUGIN_FRONTEND_H
18 
19 #include <string>
20 #include <unordered_map>
21 
22 #include "base/memory/ace_type.h"
23 #include "base/utils/string_utils.h"
24 #include "core/common/ace_page.h"
25 #include "core/common/container.h"
26 #include "core/common/frontend.h"
27 #include "core/common/js_message_dispatcher.h"
28 #include "frameworks/bridge/js_frontend/engine/common/js_engine.h"
29 #include "frameworks/bridge/plugin_frontend/plugin_frontend_delegate.h"
30 
31 namespace OHOS::Ace {
32 // PluginFrontend is the unique entrance from ACE backend to frontend.
33 // The relationship between AceActivity, AceContainer and PluginFrontend is 1:1:1.
34 // So PluginFrontend would be responsible for below things:
35 // - Create and initialize QuickJS engine.
36 // - Load pages of a JS app, and parse the manifest.json before loading main page.
37 // - Maintain the page stack of JS app by PluginFrontendDelegate.
38 // - Lifecycle of JS app (also AceActivity).
39 class PluginFrontend : public Frontend {
40     DECLARE_ACE_TYPE(PluginFrontend, Frontend);
41 
42 public:
43     using onPluginUpdateWithValueParams = std::function<void(const std::string&)>;
44 
45     PluginFrontend() = default;
46     ~PluginFrontend() override;
47 
48     bool Initialize(FrontendType type, const RefPtr<TaskExecutor>& taskExecutor) override;
49 
50     void Destroy() override;
51 
52     void AttachPipelineContext(const RefPtr<PipelineBase>& context) override;
53 
54     void SetAssetManager(const RefPtr<AssetManager>& assetManager) override;
55 
56     UIContentErrorCode RunPage(const std::string& url, const std::string& params) override;
57 
58     void ReplacePage(const std::string& url, const std::string& params) override;
59 
60     void PushPage(const std::string& url, const std::string& params) override;
61 
62     // Js frontend manages all pages self.
AddPage(const RefPtr<AcePage> & page)63     void AddPage(const RefPtr<AcePage>& page) override {}
64 
GetPage(int32_t pageId)65     RefPtr<AcePage> GetPage(int32_t pageId) const override
66     {
67         return nullptr;
68     }
69 
70     void TriggerGarbageCollection() override;
71 
72     void SendCallbackMessage(const std::string& callbackId, const std::string& data) const override;
73     // platform channel.
74     void SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher>& dispatcher) const override;
75     void TransferComponentResponseData(int32_t callbackId, int32_t code,
76         std::vector<uint8_t>&& data) const override;
77     void TransferJsResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const override;
78 #if defined(PREVIEW)
79     void TransferJsResponseDataPreview(int32_t callbackId, int32_t code, ResponseData responseData) const;
80 #endif
81     void TransferJsPluginGetError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const override;
82     void TransferJsEventData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const override;
83     void LoadPluginJsCode(std::string&& jsCode) const override;
84     void LoadPluginJsByteCode(std::vector<uint8_t>&& jsCode, std::vector<int32_t>&& jsCodeLen) const override;
85 
86     // application lifecycle.
87     void UpdateState(Frontend::State state) override;
88 
89     // page lifecycle.
90     bool OnBackPressed() override;
91     void OnShow() override;
92     void OnHide() override;
93     void OnConfigurationUpdated(const std::string& data) override;
94     void OnSaveAbilityState(std::string& data) override;
95     void OnRestoreAbilityState(const std::string& data) override;
96     void OnNewWant(const std::string& data) override;
97     void OnActive() override;
98     void OnInactive() override;
99     bool OnStartContinuation() override;
100     void OnCompleteContinuation(int32_t code) override;
101     void OnSaveData(std::string& data) override;
102     void GetPluginsUsed(std::string& data) override;
103     bool OnRestoreData(const std::string& data) override;
104     void OnRemoteTerminated() override;
105     void OnNewRequest(const std::string& data) override;
106     void OnMemoryLevel(const int32_t level) override;
107     void SetColorMode(ColorMode colorMode) override;
108     void CallRouterBack() override;
109     void NotifyAppStorage(const std::string& key, const std::string& value) override;
110 
111     void OnSurfaceChanged(int32_t width, int32_t height) override;
112 
113     void OnLayoutCompleted(const std::string& componentId) override;
114     void OnDrawCompleted(const std::string& componentId) override;
115     void OnDrawChildrenCompleted(const std::string& componentId) override;
116     bool IsDrawChildrenCallbackFuncExist(const std::string& componentId) override;
117 
118     void DumpFrontend() const override;
119     std::string GetPagePath() const override;
120 
GetEventHandler()121     RefPtr<AceEventHandler> GetEventHandler() override
122     {
123         return handler_;
124     }
125 
126     // judge frontend is foreground frontend.
IsForeground()127     bool IsForeground() override
128     {
129         return foregroundFrontend_;
130     }
131 
132     RefPtr<AccessibilityManager> GetAccessibilityManager() const override;
133     WindowConfig& GetWindowConfig() override;
134 
135     // navigator component call router
136     void NavigatePage(uint8_t type, const PageTarget& target, const std::string& params) override;
137 
138     void OnWindowDisplayModeChanged(bool isShownInMultiWindow, const std::string& data);
139 
SetJsEngine(const RefPtr<Framework::JsEngine> & jsEngine)140     void SetJsEngine(const RefPtr<Framework::JsEngine>& jsEngine)
141     {
142         jsEngine_ = jsEngine;
143     }
144 
SetNeedDebugBreakPoint(bool value)145     void SetNeedDebugBreakPoint(bool value)
146     {
147         if (jsEngine_) {
148             jsEngine_->SetNeedDebugBreakPoint(value);
149         }
150     }
151 
SetDebugVersion(bool value)152     void SetDebugVersion(bool value)
153     {
154         if (jsEngine_) {
155             jsEngine_->SetDebugVersion(value);
156         }
157     }
158 
SetInstanceName(const std::string & name)159     void SetInstanceName(const std::string& name)
160     {
161         if (jsEngine_) {
162             jsEngine_->SetInstanceName(name);
163         }
164     }
165 
SetPluginBundleName(const std::string & pluginBundleName)166     virtual void SetPluginBundleName(const std::string& pluginBundleName)
167     {
168         if (jsEngine_) {
169             jsEngine_->SetPluginBundleName(pluginBundleName);
170         }
171     }
172 
SetPluginModuleName(const std::string & pluginModuleName)173     virtual void SetPluginModuleName(const std::string& pluginModuleName)
174     {
175         if (jsEngine_) {
176             jsEngine_->SetPluginModuleName(pluginModuleName);
177         }
178     }
179 
MarkIsSubWindow(bool isSubWindow)180     void MarkIsSubWindow(bool isSubWindow)
181     {
182         isSubWindow_ = isSubWindow;
183     }
184 
185     void RebuildAllPages() override;
186 
SetDensity(double density)187     void SetDensity(double density)
188     {
189         density_ = density;
190     }
191 
ResetPageLoadState()192     void ResetPageLoadState()
193     {
194         pageLoaded_ = false;
195     }
196 
197     void UpdatePlugin(const std::string& content);
198 
SetDeclarativeOnUpdateWithValueParamsCallback(onPluginUpdateWithValueParams && callback)199     void SetDeclarativeOnUpdateWithValueParamsCallback(onPluginUpdateWithValueParams&& callback)
200     {
201         if (delegate_) {
202             delegate_->SetDeclarativeOnUpdateWithValueParamsCallback(std::move(callback));
203         }
204     }
205 
FireDeclarativeOnUpdateWithValueParamsCallback(const std::string & params)206     void FireDeclarativeOnUpdateWithValueParamsCallback(const std::string& params) const
207     {
208         if (delegate_) {
209             delegate_->FireDeclarativeOnUpdateWithValueParamsCallback(params);
210         }
211     }
212 
213 private:
214     void InitializeFrontendDelegate(const RefPtr<TaskExecutor>& taskExecutor);
215 
216     RefPtr<Framework::PluginFrontendDelegate> delegate_;
217     RefPtr<AceEventHandler> handler_;
218     RefPtr<Framework::JsEngine> jsEngine_;
219     RefPtr<AccessibilityManager> accessibilityManager_;
220     bool foregroundFrontend_ = false;
221     bool isSubWindow_ = false;
222     bool pageLoaded_ = false;
223     double density_ = 1.0;
224 };
225 
226 class PluginEventHandler : public AceEventHandler {
227 public:
PluginEventHandler(const RefPtr<Framework::PluginFrontendDelegate> & delegate)228     explicit PluginEventHandler(const RefPtr<Framework::PluginFrontendDelegate>& delegate) : delegate_(delegate)
229     {
230         ACE_DCHECK(delegate_);
231     }
PluginEventHandler()232     PluginEventHandler() {}
233 
234     ~PluginEventHandler() override = default;
235 
236     void HandleAsyncEvent(const EventMarker& eventMarker) override;
237 
238     void HandleAsyncEvent(const EventMarker& eventMarker, int32_t param) override;
239 
240     void HandleAsyncEvent(const EventMarker& eventMarker, const BaseEventInfo& info) override;
241 
242     void HandleAsyncEvent(const EventMarker& eventMarker, const std::shared_ptr<BaseEventInfo>& info) override;
243 
244     void HandleAsyncEvent(const EventMarker& eventMarker, const KeyEvent& info) override;
245 
246     void HandleAsyncEvent(const EventMarker& eventMarker, const std::string& param) override;
247 
248     void HandleSyncEvent(const EventMarker& eventMarker, bool& result) override;
249 
250     void HandleSyncEvent(const EventMarker& eventMarker, const BaseEventInfo& info, bool& result) override;
251 
252     void HandleSyncEvent(const EventMarker& eventMarker, const KeyEvent& info, bool& result) override;
253 
254     void HandleSyncEvent(const EventMarker& eventMarker, const std::string& param, std::string& result) override;
255 
256     void HandleSyncEvent(const EventMarker& eventMarker, const std::shared_ptr<BaseEventInfo>& info) override;
257 
258     void HandleSyncEvent(const EventMarker& eventMarker, const std::string& componentId, const int32_t nodeId,
259         const bool isDestroy) override;
260 
261 private:
262     RefPtr<Framework::PluginFrontendDelegate> delegate_;
263 };
264 } // namespace OHOS::Ace
265 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_PLUGIN_FRONTEND_PLUGIN_FRONTEND_H
266