• 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 "base/utils/resource_configuration.h"
25 #include "bridge/common/utils/source_map.h"
26 #include "core/common/js_message_dispatcher.h"
27 #include "core/common/router_recover_record.h"
28 #include "core/event/ace_event_handler.h"
29 #include "interfaces/inner_api/ace/constants.h"
30 
31 using FrontendDialogCallback = std::function<void(const std::string& event, const std::string& param)>;
32 
33 typedef struct napi_value__* napi_value;
34 
35 namespace OHOS::Ace {
36 
37 class AcePage;
38 class PipelineBase;
39 class AssetManager;
40 class TaskExecutor;
41 class AccessibilityManager;
42 enum class ContentInfoType;
43 
44 #ifndef WEARABLE_PRODUCT
45 constexpr int32_t DEFAULT_DESIGN_WIDTH = 720;
46 #else
47 constexpr int32_t DEFAULT_DESIGN_WIDTH = 454;
48 #endif
49 
50 // Window config of frontend.
51 struct WindowConfig {
52     // NOT runtime real design width, this is config value set by user.
53     // Runtime design width should be considered together with autoDesignWidth.
54     int32_t designWidth = DEFAULT_DESIGN_WIDTH;
55     bool autoDesignWidth = false;
56     bool boxWrap = false;
57     double designWidthScale = 0.0;
58 
GetDesignWidthScaleWindowConfig59     double GetDesignWidthScale(const double viewWidth)
60     {
61         if (NearEqual(designWidthScale, 0.0)) {
62             if (designWidth <= 0) {
63                 LOGI("designWidth <= 0");
64                 designWidth = DEFAULT_DESIGN_WIDTH;
65             }
66             return viewWidth / designWidth;
67         }
68         return designWidthScale;
69     }
70 
UpdateDesignWidthScaleWindowConfig71     void UpdateDesignWidthScale(const double viewWidth)
72     {
73         if (designWidth <= 0) {
74             LOGI("designWidth <= 0");
75             designWidth = DEFAULT_DESIGN_WIDTH;
76         }
77         designWidthScale = viewWidth / designWidth;
78     }
79 };
80 
81 enum class FrontendType { JSON, JS, JS_CARD, DECLARATIVE_JS, JS_PLUGIN, ETS_CARD, DECLARATIVE_CJ };
82 struct PageTarget;
83 
84 class ACE_FORCE_EXPORT Frontend : public AceType {
85     DECLARE_ACE_TYPE(Frontend, AceType);
86 
87 public:
88     Frontend() = default;
89     ~Frontend() override;
90 
91     enum State : uint8_t { ON_CREATE = 0, ON_DESTROY, ON_SHOW, ON_HIDE, ON_ACTIVE, ON_INACTIVE, UNDEFINE };
92     static std::string stateToString(int state);
93 
94     static RefPtr<Frontend> Create();
95     static RefPtr<Frontend> CreateDefault();
96 
97     virtual bool Initialize(FrontendType type, const RefPtr<TaskExecutor>& taskExecutor) = 0;
98 
99     virtual void Destroy() = 0;
100 
101     virtual void AttachPipelineContext(const RefPtr<PipelineBase>& context) = 0;
102 
103     virtual void SetAssetManager(const RefPtr<AssetManager>& assetManager) = 0;
104 
105     virtual void AddPage(const RefPtr<AcePage>& page) = 0;
106 
107     virtual RefPtr<AcePage> GetPage(int32_t pageId) const = 0;
108 
109     // Get the currently running JS page information in NG structure.
GetCurrentPageUrl()110     virtual std::string GetCurrentPageUrl() const
111     {
112         return "";
113     }
114 
115     // Get the currently running JS page information in NG structure.
GetCurrentPageSourceMap()116     virtual RefPtr<Framework::RevSourceMap> GetCurrentPageSourceMap() const
117     {
118         return nullptr;
119     }
120 
121     // Get the currently running JS page information in NG structure.
GetFaAppSourceMap()122     virtual RefPtr<Framework::RevSourceMap> GetFaAppSourceMap() const
123     {
124         return nullptr;
125     }
126 
127     // Get the stage mode sourceMap.
GetStageSourceMap(std::unordered_map<std::string,RefPtr<Framework::RevSourceMap>> & sourceMap)128     virtual void GetStageSourceMap(std::unordered_map<std::string, RefPtr<Framework::RevSourceMap>>& sourceMap) const {}
129 
RunPage(const std::string & content,const std::string & params)130     virtual UIContentErrorCode RunPage(const std::string& content, const std::string& params)
131     {
132         return UIContentErrorCode::NO_ERRORS;
133     }
RunPage(const std::shared_ptr<std::vector<uint8_t>> & content,const std::string & params)134     virtual UIContentErrorCode RunPage(const std::shared_ptr<std::vector<uint8_t>>& content, const std::string& params)
135     {
136         return UIContentErrorCode::NO_ERRORS;
137     }
RunDynamicPage(const std::string & content,const std::string & params,const std::string & entryPoint)138     virtual UIContentErrorCode RunDynamicPage(
139         const std::string& content, const std::string& params, const std::string& entryPoint)
140     {
141         return UIContentErrorCode::NO_ERRORS;
142     }
143 
RunPageByNamedRouter(const std::string & name,const std::string & params)144     virtual UIContentErrorCode RunPageByNamedRouter(const std::string& name, const std::string& params)
145     {
146         return UIContentErrorCode::NO_ERRORS;
147     }
148 
149     virtual void ReplacePage(const std::string& url, const std::string& params) = 0;
150 
151     virtual void PushPage(const std::string& url, const std::string& params) = 0;
152 
153     // Gets front-end event handler to handle ace event.
154     virtual RefPtr<AceEventHandler> GetEventHandler() = 0;
155 
156     // Get window config of front end, which is used to calculate the pixel ratio of the real device.
157     virtual WindowConfig& GetWindowConfig() = 0;
158 
GetLock()159     std::unique_lock<std::recursive_mutex> GetLock() const
160     {
161         return std::unique_lock<std::recursive_mutex>(mutex_);
162     }
163 
GetType()164     FrontendType GetType() const
165     {
166         return type_;
167     }
168 
169     RefPtr<TaskExecutor> GetTaskExecutor() const;
170 
171     // inform the frontend that onCreate or onDestroy
172     virtual void UpdateState(State) = 0;
173 
174     // dump frontend info
175     virtual void DumpFrontend() const = 0;
176 
177     virtual std::string GetPagePath() const = 0;
178 
179     // send the message by js callback
180     virtual void SendCallbackMessage(const std::string& callbackId, const std::string& data) const = 0;
181 
182     // set the message transfer to js instance
183     virtual void SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher>& transfer) const = 0;
184 
185     // transfer data back from platform side to component side
186     virtual void TransferComponentResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const = 0;
187 
188     // transfer data back from platform side to js side
189     virtual void TransferJsResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const = 0;
190 
191     // transfer error message get in plugin from platform side to js side
192     virtual void TransferJsPluginGetError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const = 0;
193 
194     // transfer event data from platform side to js side
195     virtual void TransferJsEventData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const = 0;
196 
197     // get system plugin used in application
GetPluginsUsed(std::string & data)198     virtual void GetPluginsUsed(std::string& data) {}
199 
200     // get js code from plugin and load in js engine
201     virtual void LoadPluginJsCode(std::string&& jsCode) const = 0;
202 
203     virtual void LoadPluginJsByteCode(std::vector<uint8_t>&& jsCode, std::vector<int32_t>&& jsCodeLen) const = 0;
204 
205     // when this is foreground frontend
206     virtual bool IsForeground() = 0;
207 
208     // get accessibility manager handler.
209     virtual RefPtr<AccessibilityManager> GetAccessibilityManager() const = 0;
210 
211     // when back pressed
212     virtual bool OnBackPressed() = 0;
213 
214     // when interface show up
215     virtual void OnShow() = 0;
216 
217     // when interface hide
218     virtual void OnHide() = 0;
219 
220     // when configuration update
OnConfigurationUpdated(const std::string & data)221     virtual void OnConfigurationUpdated(const std::string& data) {}
222 
223     virtual void OnSaveAbilityState(std::string& data) = 0;
224 
225     virtual void OnMemoryLevel(int32_t level) = 0;
226 
227     virtual void OnRestoreAbilityState(const std::string& data) = 0;
228 
229     // when front on active
230     virtual void OnActive() = 0;
231 
232     // when front on inactive
233     virtual void OnInactive() = 0;
234 
235     // when front on asks a user whether to start the migration
236     virtual bool OnStartContinuation() = 0;
237 
238     // when front on a local ability migration is complete
239     virtual void OnCompleteContinuation(int32_t code) = 0;
240 
241     // interface to save the user data
242     virtual void OnSaveData(std::string& data) = 0;
243 
244     // interface to restores the user data on the remote device
245     virtual bool OnRestoreData(const std::string& data) = 0;
246 
247     virtual void OnRemoteTerminated() = 0;
248 
249     // start the ability when it's running
250     virtual void OnNewRequest(const std::string& data) = 0;
251 
252     virtual void OnNewWant(const std::string& data) = 0;
253 
254     // call router back
255     virtual void CallRouterBack() = 0;
256 
257     virtual void OnSurfaceChanged(int32_t width, int32_t height) = 0;
258 
259     virtual void OnLayoutCompleted(const std::string& componentId) = 0;
260     virtual void OnDrawCompleted(const std::string& componentId) = 0;
261 
TriggerGarbageCollection()262     virtual void TriggerGarbageCollection() {}
263 
DumpHeapSnapshot(bool isPrivate)264     virtual void DumpHeapSnapshot(bool isPrivate) {}
265 
DestroyHeapProfiler()266     virtual void DestroyHeapProfiler() {}
267 
ForceFullGC()268     virtual void ForceFullGC() {}
269 
NotifyUIIdle()270     virtual void NotifyUIIdle() {}
271 
RebuildAllPages()272     virtual void RebuildAllPages() {}
273 
SetColorMode(ColorMode colorMode)274     virtual void SetColorMode(ColorMode colorMode) {}
275 
276     // navigator component call router
NavigatePage(uint8_t type,const PageTarget & target,const std::string & params)277     virtual void NavigatePage(uint8_t type, const PageTarget& target, const std::string& params) {}
278 
279     // restore
RestoreRouterStack(const std::string & contentInfo,ContentInfoType type)280     virtual std::pair<RouterRecoverRecord, UIContentErrorCode> RestoreRouterStack(
281         const std::string& contentInfo, ContentInfoType type)
282     {
283         return std::make_pair(RouterRecoverRecord(), UIContentErrorCode::NO_ERRORS);
284     }
285 
GetContentInfo(ContentInfoType type)286     virtual std::string GetContentInfo(ContentInfoType type) const
287     {
288         return "";
289     }
290 
291     // only declarative frontend need to support
GetRouterSize()292     virtual int32_t GetRouterSize() const
293     {
294         return -1;
295     }
296 
NotifyAppStorage(const std::string & key,const std::string & value)297     virtual void NotifyAppStorage(const std::string& key, const std::string& value) {}
298 
GetContextValue()299     virtual napi_value GetContextValue()
300     {
301         napi_value value = nullptr;
302         return value;
303     }
304 
GetFrameNodeValueByNodeId(int32_t nodeId)305     virtual napi_value GetFrameNodeValueByNodeId(int32_t nodeId)
306     {
307         return nullptr;
308     }
309 #ifdef PREVIEW
RunNativeEngineLoop()310     virtual void RunNativeEngineLoop() {}
311 #endif
312 
313     // Disallow pop last page
DisallowPopLastPage()314     void DisallowPopLastPage()
315     {
316         disallowPopLastPage_ = true;
317     }
318 
SetDialogCallback(FrontendDialogCallback callback)319     virtual void SetDialogCallback(FrontendDialogCallback callback)
320     {
321         dialogCallback_ = std::move(callback);
322     }
323 
FlushReload()324     virtual void FlushReload() {}
325     // flush frontend for HotReload feature in NG
HotReload()326     virtual void HotReload() {}
327 
GetState()328     State GetState() const
329     {
330         return state_;
331     }
332 
SetErrorEventHandler(std::function<void (const std::string &,const std::string &)> && errorCallback)333     virtual void SetErrorEventHandler(std::function<void(const std::string&, const std::string&)>&& errorCallback) {}
334 
GetPagePathByUrl(const std::string & url)335     virtual std::string GetPagePathByUrl(const std::string& url) const
336     {
337         return "";
338     }
339 
340 protected:
341     virtual bool MaybeRelease() override;
342     FrontendType type_ = FrontendType::JS;
343     RefPtr<TaskExecutor> taskExecutor_;
344     bool disallowPopLastPage_ = false;
345     FrontendDialogCallback dialogCallback_ = nullptr;
346     State state_ = State::UNDEFINE;
347     mutable std::recursive_mutex mutex_;
348     mutable std::mutex destructMutex_;
349 };
350 
351 } // namespace OHOS::Ace
352 
353 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_FRONTEND_H
354