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 #ifndef FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_DECLARATIVE_FRONTEND_NG_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_DECLARATIVE_FRONTEND_NG_H
18 
19 #include <string>
20 
21 #include "base/memory/ace_type.h"
22 #include "base/utils/string_utils.h"
23 #include "core/common/ace_page.h"
24 #include "core/common/container.h"
25 #include "core/common/frontend.h"
26 #include "core/pipeline/pipeline_base.h"
27 #include "frameworks/bridge/declarative_frontend/ng/frontend_delegate_declarative_ng.h"
28 #include "frameworks/bridge/js_frontend/engine/common/js_engine.h"
29 
30 namespace OHOS::Ace {
31 
32 class DeclarativeFrontendNG : public Frontend {
33     DECLARE_ACE_TYPE(DeclarativeFrontendNG, Frontend);
34 
35 public:
36     DeclarativeFrontendNG() = default;
37     ~DeclarativeFrontendNG() override;
38 
39     bool Initialize(FrontendType type, const RefPtr<TaskExecutor>& taskExecutor) override;
40 
41     void Destroy() override;
42 
43     void AttachPipelineContext(const RefPtr<PipelineBase>& context) override;
44 
45     void SetAssetManager(const RefPtr<AssetManager>& assetManager) override;
46 
47     UIContentErrorCode RunPage(const std::string& url, const std::string& params) override;
48     UIContentErrorCode RunPage(
49         const std::shared_ptr<std::vector<uint8_t>>& content, const std::string& params) override;
50     UIContentErrorCode RunPageByNamedRouter(const std::string& name, const std::string& params) override;
51 
52     void ReplacePage(const std::string& url, const std::string& params) override;
53 
54     void PushPage(const std::string& url, const std::string& params) override;
55 
56     // Js frontend manages all pages self.
AddPage(const RefPtr<AcePage> & page)57     void AddPage(const RefPtr<AcePage>& page) override {}
58 
GetPage(int32_t pageId)59     RefPtr<AcePage> GetPage(int32_t pageId) const override
60     {
61         return nullptr;
62     }
63 
SendCallbackMessage(const std::string & callbackId,const std::string & data)64     void SendCallbackMessage(const std::string& callbackId, const std::string& data) const override {}
65 
66     // platform channel.
SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher> & dispatcher)67     void SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher>& dispatcher) const override {}
TransferComponentResponseData(int32_t callbackId,int32_t code,std::vector<uint8_t> && data)68     void TransferComponentResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const override {}
TransferJsResponseData(int32_t callbackId,int32_t code,std::vector<uint8_t> && data)69     void TransferJsResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const override {}
70     napi_value GetContextValue() override;
71     napi_value GetFrameNodeValueByNodeId(int32_t nodeId) override;
72 
73 #if defined(PREVIEW)
RunNativeEngineLoop()74     void RunNativeEngineLoop() override
75     {
76         if (jsEngine_) {
77             jsEngine_->RunNativeEngineLoop();
78         }
79     }
80 #endif
TransferJsPluginGetError(int32_t callbackId,int32_t errorCode,std::string && errorMessage)81     void TransferJsPluginGetError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const override {}
TransferJsEventData(int32_t callbackId,int32_t code,std::vector<uint8_t> && data)82     void TransferJsEventData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const override {}
LoadPluginJsCode(std::string && jsCode)83     void LoadPluginJsCode(std::string&& jsCode) const override {}
LoadPluginJsByteCode(std::vector<uint8_t> && jsCode,std::vector<int32_t> && jsCodeLen)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 OnActive() override;
OnSaveAbilityState(std::string & data)95     void OnSaveAbilityState(std::string& data) override {}
OnRestoreAbilityState(const std::string & data)96     void OnRestoreAbilityState(const std::string& data) override {}
OnNewWant(const std::string & data)97     void OnNewWant(const std::string& data) override {}
OnInactive()98     void OnInactive() override {}
99     bool OnStartContinuation() override;
100     void OnCompleteContinuation(int32_t code) override;
101     void OnSaveData(std::string& data) override;
102     bool OnRestoreData(const std::string& data) override;
103     void OnRemoteTerminated() override;
OnNewRequest(const std::string & data)104     void OnNewRequest(const std::string& data) override {}
OnMemoryLevel(const int32_t level)105     void OnMemoryLevel(const int32_t level) override {}
106     void CallRouterBack() override;
107     void OnSurfaceChanged(int32_t width, int32_t height) override;
108 
109     void OnLayoutCompleted(const std::string& componentId) override;
110     void OnDrawCompleted(const std::string& componentId) override;
111 
112     void DumpFrontend() const override;
113     std::string GetPagePath() const override;
114     void TriggerGarbageCollection() override;
115     void DumpHeapSnapshot(bool isPrivate) override;
116     void NotifyUIIdle() override;
117     void SetColorMode(ColorMode colorMode) override;
118     void RebuildAllPages() override;
119     void NotifyAppStorage(const std::string& key, const std::string& value) override;
120 
121     RefPtr<NG::PageRouterManager> GetPageRouterManager() const;
122 
GetEventHandler()123     RefPtr<AceEventHandler> GetEventHandler() override
124     {
125         return nullptr;
126     }
127 
128     // judge frontend is foreground frontend.
IsForeground()129     bool IsForeground() override
130     {
131         return foregroundFrontend_;
132     }
133 
134     RefPtr<AccessibilityManager> GetAccessibilityManager() const override;
135     WindowConfig& GetWindowConfig() override;
136 
137     // navigator component call router
138     void NavigatePage(uint8_t type, const PageTarget& target, const std::string& params) override;
139 
140     // restore
141     std::pair<RouterRecoverRecord, UIContentErrorCode> RestoreRouterStack(
142         const std::string& contentInfo, ContentInfoType type) override;
143     std::string GetContentInfo(ContentInfoType type) const override;
144 
145     int32_t GetRouterSize() const override;
146 
147     void OnWindowDisplayModeChanged(bool isShownInMultiWindow, const std::string& data);
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 
SetPageProfile(const std::string & pageProfile)175     void SetPageProfile(const std::string& pageProfile)
176     {
177         pageProfile_ = pageProfile;
178     }
179 
MarkIsSubWindow(bool isSubWindow)180     void MarkIsSubWindow(bool isSubWindow)
181     {
182         isSubWindow_ = isSubWindow;
183     }
184 
GetJsEngine()185     RefPtr<Framework::JsEngine> GetJsEngine()
186     {
187         return jsEngine_;
188     }
189 
190     void AttachSubPipelineContext(const RefPtr<PipelineBase>& context);
191 
192     void FlushReload() override;
193     void HotReload() override;
194 
195     std::string GetCurrentPageUrl() const override;
196 
197 private:
198     void InitializeDelegate(const RefPtr<TaskExecutor>& taskExecutor);
199 
200     RefPtr<Framework::JsEngine> jsEngine_;
201     RefPtr<Framework::FrontendDelegateDeclarativeNG> delegate_;
202     RefPtr<AccessibilityManager> accessibilityManager_;
203 
204     std::string pageProfile_;
205     bool foregroundFrontend_ = false;
206     bool isSubWindow_ = false;
207 };
208 
209 } // namespace OHOS::Ace
210 
211 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_DECLARATIVE_FRONTEND_H
212