• 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_CORE_COMMON_FRONTEND_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_FRONTEND_H
18 
19 #include <string>
20 #include <utility>
21 
22 #include "base/memory/ace_type.h"
23 #include "base/utils/macros.h"
24 #include "bridge/common/utils/source_map.h"
25 #include "core/accessibility/accessibility_manager.h"
26 #include "core/common/ace_page.h"
27 #include "core/common/js_message_dispatcher.h"
28 #include "core/event/ace_event_handler.h"
29 #include "core/pipeline/pipeline_base.h"
30 #include "frameworks/base/utils/resource_configuration.h"
31 
32 using FrontendDialogCallback = std::function<void(const std::string& event, const std::string& param)>;
33 
34 namespace OHOS::Ace {
35 
36 #ifndef WEARABLE_PRODUCT
37 constexpr int32_t DEFAULT_DESIGN_WIDTH = 720;
38 #else
39 constexpr int32_t DEFAULT_DESIGN_WIDTH = 454;
40 #endif
41 
42 // Window config of frontend.
43 struct WindowConfig {
44     // NOT runtime real design width, this is config value set by user.
45     // Runtime design width should be considered together with autoDesignWidth.
46     int32_t designWidth = DEFAULT_DESIGN_WIDTH;
47     bool autoDesignWidth = false;
48     bool boxWrap = false;
49     double designWidthScale = 0.0;
50 
GetDesignWidthScaleWindowConfig51     double GetDesignWidthScale(const double viewWidth)
52     {
53         if (NearEqual(designWidthScale, 0.0)) {
54             if (designWidth <= 0) {
55                 LOGI("designWidth <= 0");
56                 designWidth = DEFAULT_DESIGN_WIDTH;
57             }
58             return viewWidth / designWidth;
59         }
60         return designWidthScale;
61     }
62 };
63 
64 enum class FrontendType { JSON, JS, JS_CARD, DECLARATIVE_JS, JS_PLUGIN, ETS_CARD };
65 struct PageTarget;
66 
67 class ACE_EXPORT Frontend : public AceType {
68     DECLARE_ACE_TYPE(Frontend, AceType);
69 
70 public:
71     Frontend() = default;
72     ~Frontend() override = default;
73 
74     enum class State { ON_CREATE, ON_DESTROY, ON_SHOW, ON_HIDE, ON_ACTIVE, ON_INACTIVE, UNDEFINED };
75 
76     static RefPtr<Frontend> Create();
77     static RefPtr<Frontend> CreateDefault();
78 
79     virtual bool Initialize(FrontendType type, const RefPtr<TaskExecutor>& taskExecutor) = 0;
80 
81     virtual void Destroy() = 0;
82 
83     virtual void AttachPipelineContext(const RefPtr<PipelineBase>& context) = 0;
84 
85     virtual void SetAssetManager(const RefPtr<AssetManager>& assetManager) = 0;
86 
87     virtual void AddPage(const RefPtr<AcePage>& page) = 0;
88 
89     virtual RefPtr<AcePage> GetPage(int32_t pageId) const = 0;
90 
91     // Get the currently running JS page information in NG structure.
GetCurrentPageUrl()92     virtual std::string GetCurrentPageUrl() const
93     {
94         return "";
95     };
96 
97     // Get the currently running JS page information in NG structure.
GetCurrentPageSourceMap()98     virtual RefPtr<Framework::RevSourceMap> GetCurrentPageSourceMap() const
99     {
100         return nullptr;
101     };
102 
103     // Get the currently running JS page information in NG structure.
GetFaAppSourceMap()104     virtual RefPtr<Framework::RevSourceMap> GetFaAppSourceMap() const
105     {
106         return nullptr;
107     };
108 
109     // Get the stage mode sourceMap.
GetStageSourceMap(std::unordered_map<std::string,RefPtr<Framework::RevSourceMap>> & sourceMap)110     virtual void GetStageSourceMap(
111         std::unordered_map<std::string, RefPtr<Framework::RevSourceMap>>& sourceMap) const {};
112 
113     virtual void RunPage(int32_t pageId, const std::string& content, const std::string& params) = 0;
114 
115     virtual void ReplacePage(const std::string& url, const std::string& params) = 0;
116 
117     virtual void PushPage(const std::string& url, const std::string& params) = 0;
118 
119     // Gets front-end event handler to handle ace event.
120     virtual RefPtr<AceEventHandler> GetEventHandler() = 0;
121 
122     // Get window config of front end, which is used to calculate the pixel ratio of the real device.
123     virtual WindowConfig& GetWindowConfig() = 0;
124 
125     virtual FrontendType GetType() = 0;
126 
127     // inform the frontend that onCreate or onDestroy
128     virtual void UpdateState(State) = 0;
129 
130     // dump frontend info
131     virtual void DumpFrontend() const = 0;
132 
133     virtual std::string GetPagePath() const = 0;
134 
135     // send the message by js callback
136     virtual void SendCallbackMessage(const std::string& callbackId, const std::string& data) const = 0;
137 
138     // set the message transfer to js instance
139     virtual void SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher>& transfer) const = 0;
140 
141     // transfer data back from platform side to component side
142     virtual void TransferComponentResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const = 0;
143 
144     // transfer data back from platform side to js side
145     virtual void TransferJsResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const = 0;
146 
147     // transfer error message get in plugin from platform side to js side
148     virtual void TransferJsPluginGetError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const = 0;
149 
150     // transfer event data from platform side to js side
151     virtual void TransferJsEventData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const = 0;
152 
153     // get system plugin used in application
GetPluginsUsed(std::string & data)154     virtual void GetPluginsUsed(std::string& data) {}
155 
156     // get js code from plugin and load in js engine
157     virtual void LoadPluginJsCode(std::string&& jsCode) const = 0;
158 
159     virtual void LoadPluginJsByteCode(std::vector<uint8_t>&& jsCode, std::vector<int32_t>&& jsCodeLen) const = 0;
160 
161     // when this is foreground frontend
162     virtual bool IsForeground() = 0;
163 
164     // get accessibility manager handler.
165     virtual RefPtr<AccessibilityManager> GetAccessibilityManager() const = 0;
166 
167     // when back pressed
168     virtual bool OnBackPressed() = 0;
169 
170     // when interface show up
171     virtual void OnShow() = 0;
172 
173     // when interface hide
174     virtual void OnHide() = 0;
175 
176     // when configuration update
OnConfigurationUpdated(const std::string & data)177     virtual void OnConfigurationUpdated(const std::string& data) {}
178 
179     virtual void OnSaveAbilityState(std::string& data) = 0;
180 
181     virtual void OnMemoryLevel(int32_t level) = 0;
182 
183     virtual void OnRestoreAbilityState(const std::string& data) = 0;
184 
185     // when front on active
186     virtual void OnActive() = 0;
187 
188     // when front on inactive
189     virtual void OnInactive() = 0;
190 
191     // when front on asks a user whether to start the migration
192     virtual bool OnStartContinuation() = 0;
193 
194     // when front on a local ability migration is complete
195     virtual void OnCompleteContinuation(int32_t code) = 0;
196 
197     // interface to save the user data
198     virtual void OnSaveData(std::string& data) = 0;
199 
200     // interface to restores the user data on the remote device
201     virtual bool OnRestoreData(const std::string& data) = 0;
202 
203     virtual void OnRemoteTerminated() = 0;
204 
205     // start the ability when it's running
206     virtual void OnNewRequest(const std::string& data) = 0;
207 
208     virtual void OnNewWant(const std::string& data) = 0;
209 
210     // call router back
211     virtual void CallRouterBack() = 0;
212 
213     virtual void OnSurfaceChanged(int32_t width, int32_t height) = 0;
214 
TriggerGarbageCollection()215     virtual void TriggerGarbageCollection() {}
216 
DumpHeapSnapshot(bool isPrivate)217     virtual void DumpHeapSnapshot(bool isPrivate) {}
218 
RebuildAllPages()219     virtual void RebuildAllPages() {}
220 
SetColorMode(ColorMode colorMode)221     virtual void SetColorMode(ColorMode colorMode) {}
222 
223     // navigator component call router
NavigatePage(uint8_t type,const PageTarget & target,const std::string & params)224     virtual void NavigatePage(uint8_t type, const PageTarget& target, const std::string& params) {}
225 
226     // distribute
RestoreRouterStack(const std::string & contentInfo)227     virtual std::string RestoreRouterStack(const std::string& contentInfo)
228     {
229         return "";
230     }
GetContentInfo()231     virtual std::string GetContentInfo() const
232     {
233         return "";
234     }
GetRouterSize()235     virtual int32_t GetRouterSize() const
236     {
237         // only declarative frontend need to support
238         return -1;
239     }
240 
NotifyAppStorage(const std::string & key,const std::string & value)241     virtual void NotifyAppStorage(const std::string& key, const std::string& value) {}
242 #if defined(PREVIEW)
RunNativeEngineLoop()243     virtual void RunNativeEngineLoop() {}
244 #endif
245 
246     // Disallow pop last page
DisallowPopLastPage()247     void DisallowPopLastPage()
248     {
249         disallowPopLastPage_ = true;
250     }
251 
SetDialogCallback(FrontendDialogCallback callback)252     virtual void SetDialogCallback(FrontendDialogCallback callback)
253     {
254         dialogCallback_ = std::move(callback);
255     }
256 
FlushReload()257     virtual void FlushReload() {}
258 
SetErrorEventHandler(std::function<void (const std::string &,const std::string &)> && errorCallback)259     virtual void SetErrorEventHandler(
260         std::function<void(const std::string&, const std::string&)>&& errorCallback) {}
261 
262 protected:
263     bool disallowPopLastPage_ = false;
264     FrontendDialogCallback dialogCallback_ = nullptr;
265 };
266 
267 } // namespace OHOS::Ace
268 
269 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_FRONTEND_H
270