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_BRIDGE_JS_FRONTEND_JS_FRONTEND_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_JS_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/js_frontend/frontend_delegate_impl.h"
30 #include "frameworks/bridge/js_frontend/js_ace_page.h"
31 
32 namespace OHOS::Ace {
33 
34 // JsFrontend is the unique entrance from ACE backend to frontend.
35 // The relationship between AceActivity, AceContainer and JsFrontend is 1:1:1.
36 // So JsFrontend would be responsible for below things:
37 // - Create and initialize QuickJS engine.
38 // - Load pages of a JS app, and parse the manifest.json before loading main page.
39 // - Maintain the page stack of JS app by FrontendDelegateImpl.
40 // - Lifecycle of JS app (also AceActivity).
41 class JsFrontend : public Frontend {
42     DECLARE_ACE_TYPE(JsFrontend, Frontend);
43 
44 public:
45     JsFrontend() = default;
46     ~JsFrontend() 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.
63     void AddPage(const RefPtr<AcePage>& page) override {}
64 
65     RefPtr<AcePage> GetPage(int32_t pageId) const override
66     {
67         CHECK_NULL_RETURN(delegate_, nullptr);
68         return delegate_->GetPage(pageId);
69     }
70 
GetCurrentReadyPage() const71     WeakPtr<AcePage> GetCurrentReadyPage() const
72     {
73         CHECK_NULL_RETURN(delegate_, nullptr);
74         return delegate_->GetCurrentReadyPage();
75     }
76 
77     void TriggerGarbageCollection() override;
78     void DumpHeapSnapshot(bool isPrivate) override;
79     void DestroyHeapProfiler() override;
80     void ForceFullGC() override;
81     void NotifyUIIdle() override;
82 
83     void SendCallbackMessage(const std::string& callbackId, const std::string& data) const override;
84     // platform channel.
85     void SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher>& dispatcher) const override;
86     void TransferComponentResponseData(int32_t callbackId, int32_t code,
87         std::vector<uint8_t>&& data) const override;
88     void TransferJsResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const override;
89 #if defined(PREVIEW)
90     void RunNativeEngineLoop() override
91     {
92         if (jsEngine_) {
93             jsEngine_->RunNativeEngineLoop();
94         }
95     }
96     void TransferJsResponseDataPreview(int32_t callbackId, int32_t code, ResponseData responseData) const;
97 #endif
98     void TransferJsPluginGetError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const override;
99     void TransferJsEventData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const override;
100     void LoadPluginJsCode(std::string&& jsCode) const override;
101     void LoadPluginJsByteCode(std::vector<uint8_t>&& jsCode, std::vector<int32_t>&& jsCodeLen) const override;
102 
103     // application lifecycle.
104     void UpdateState(Frontend::State state) override;
105 
106     // page lifecycle.
107     bool OnBackPressed() override;
108     void OnSaveAbilityState(std::string& data) override {}
109     void OnRestoreAbilityState(const std::string& data) override {}
110     void OnShow() override;
111     void OnHide() override;
112     void OnConfigurationUpdated(const std::string& data) override;
113     void OnActive() override;
114     void OnInactive() override;
115     bool OnStartContinuation() override;
116     void OnCompleteContinuation(int32_t code) override;
117     void OnRemoteTerminated() override;
118     void OnSaveData(std::string& data) override;
119     bool OnRestoreData(const std::string& data) override;
120 
121     void OnMemoryLevel(const int32_t level) override {}
122     void OnNewRequest(const std::string& data) override;
123     void OnNewWant(const std::string& data) override {}
124     void CallRouterBack() override;
125     void SetColorMode(ColorMode colorMode) override;
126 
127     void OnSurfaceChanged(int32_t width, int32_t height) override;
128 
129     void OnLayoutCompleted(const std::string& componentId) override;
130     void OnDrawCompleted(const std::string& componentId) override;
131 
132     void DumpFrontend() const override;
133     std::string GetPagePath() const override;
134 
135     RefPtr<AceEventHandler> GetEventHandler() override
136     {
137         return handler_;
138     }
139 
140     // judge frontend is foreground frontend.
141     bool IsForeground() override
142     {
143         return foregroundFrontend_;
144     }
145 
146     RefPtr<AccessibilityManager> GetAccessibilityManager() const override;
147     WindowConfig& GetWindowConfig() override;
148 
SetJsEngine(const RefPtr<Framework::JsEngine>& jsEngine)149     void SetJsEngine(const RefPtr<Framework::JsEngine>& jsEngine)
150     {
151         jsEngine_ = jsEngine;
152     }
153 
SetNeedDebugBreakPoint(bool value)154     void SetNeedDebugBreakPoint(bool value)
155     {
156         if (jsEngine_) {
157             jsEngine_->SetNeedDebugBreakPoint(value);
158         }
159     }
160 
SetDebugVersion(bool value)161     void SetDebugVersion(bool value)
162     {
163         if (jsEngine_) {
164             jsEngine_->SetDebugVersion(value);
165         }
166     }
167 
SetInstanceName(const std::string& name)168     void SetInstanceName(const std::string& name)
169     {
170         if (jsEngine_) {
171             jsEngine_->SetInstanceName(name);
172         }
173     }
174 
175     void RebuildAllPages() override;
176 
177     void GetPluginsUsed(std::string& data) override;
178 
179 private:
180     void InitializeFrontendDelegate(const RefPtr<TaskExecutor>& taskExecutor);
181 
182     RefPtr<Framework::FrontendDelegateImpl> delegate_;
183     RefPtr<AceEventHandler> handler_;
184     RefPtr<Framework::JsEngine> jsEngine_;
185     bool foregroundFrontend_ = false;
186 };
187 
188 class JsEventHandler : public AceEventHandler {
189 public:
JsEventHandler(const RefPtr<Framework::FrontendDelegateImpl>& delegate)190     explicit JsEventHandler(const RefPtr<Framework::FrontendDelegateImpl>& delegate) : delegate_(delegate)
191     {
192         ACE_DCHECK(delegate_);
193     }
194 
195     ~JsEventHandler() override = default;
196 
197     void HandleAsyncEvent(const EventMarker& eventMarker) override;
198 
199     void HandleAsyncEvent(const EventMarker& eventMarker, int32_t param) override;
200 
201     void HandleAsyncEvent(const EventMarker& eventMarker, const BaseEventInfo& info) override;
202 
203     void HandleAsyncEvent(const EventMarker& eventMarker, const KeyEvent& info) override;
204 
205     void HandleAsyncEvent(const EventMarker& eventMarker, const GestureEvent& info) override;
206 
207     void HandleAsyncEvent(const EventMarker& eventMarker, const RotationEvent& info) override;
208 
209     void HandleAsyncEvent(const EventMarker& eventMarker, const std::string& param) override;
210 
211     void HandleSyncEvent(const EventMarker& eventMarker, bool& result) override;
212 
213     void HandleSyncEvent(const EventMarker& eventMarker, const BaseEventInfo& info, bool& result) override;
214 
215     void HandleSyncEvent(const EventMarker& eventMarker, const KeyEvent& info, bool& result) override;
216 
217     void HandleSyncEvent(const EventMarker& eventMarker, const std::string& param, std::string& result) override;
218 
219     void HandleSyncEvent(const EventMarker& eventMarker, const std::string& componentId, const int32_t nodeId,
220         const bool isDestroy) override;
221 
222 private:
223     RefPtr<Framework::FrontendDelegateImpl> delegate_;
224 };
225 
226 } // namespace OHOS::Ace
227 
228 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_JS_FRONTEND_H
229