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