• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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_FRONTEND_DELEGATE_DECLARATIVE_NG_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_FRONTEND_DELEGATE_DECLARATIVE_NG_H
18 
19 #include "base/memory/ace_type.h"
20 #include "base/thread/cancelable_callback.h"
21 #include "base/utils/measure_util.h"
22 #include "core/components_ng/render/snapshot_param.h"
23 #include "frameworks/bridge/common/accessibility/accessibility_node_manager.h"
24 #include "frameworks/bridge/common/manifest/manifest_parser.h"
25 #include "frameworks/bridge/common/utils/pipeline_context_holder.h"
26 #include "frameworks/bridge/declarative_frontend/ng/page_router_manager.h"
27 #include "frameworks/bridge/js_frontend/engine/common/group_js_bridge.h"
28 #include "frameworks/bridge/js_frontend/engine/common/js_engine.h"
29 #include "frameworks/bridge/js_frontend/frontend_delegate.h"
30 #include "frameworks/bridge/js_frontend/frontend_delegate_impl.h"
31 #include "frameworks/bridge/js_frontend/js_ace_page.h"
32 
33 namespace OHOS::Ace::Framework {
34 
35 class FrontendDelegateDeclarativeNG : public FrontendDelegate {
36     DECLARE_ACE_TYPE(FrontendDelegateDeclarativeNG, FrontendDelegate);
37 
38 public:
39     FrontendDelegateDeclarativeNG(const RefPtr<TaskExecutor>& taskExecutor);
40     ~FrontendDelegateDeclarativeNG() override = default;
41 
42     void AttachPipelineContext(const RefPtr<PipelineBase>& context) override;
43     void AttachSubPipelineContext(const RefPtr<PipelineBase>& context);
44     // distribute
RestoreRouterStack(const std::string & contentInfo)45     std::pair<std::string, UIContentErrorCode> RestoreRouterStack(const std::string& contentInfo) override
46     {
47         return std::make_pair("", UIContentErrorCode::NO_ERRORS);
48     }
49     std::string GetContentInfo() override;
50     // JSFrontend delegate NG functions.
51     void RunPage(const std::string& url, const std::string& params, const std::string& profile);
52     void RunPage(const std::shared_ptr<std::vector<uint8_t>>& content,
53         const std::string& params, const std::string& profile);
54     void OnConfigurationUpdated(const std::string& data);
55     bool OnStartContinuation();
56     void OnCompleteContinuation(int32_t code);
57     void OnSaveData(std::string& data);
58     bool OnRestoreData(const std::string& data);
59     void OnRemoteTerminated();
60     void SetColorMode(ColorMode colorMode);
61     void CallPopPage();
62     void OnApplicationDestroy(const std::string& packageName);
63     void UpdateApplicationState(const std::string& packageName, Frontend::State state);
64     void OnWindowDisplayModeChanged(bool isShownInMultiWindow, const std::string& data);
65     void NotifyAppStorage(
66         const WeakPtr<Framework::JsEngine>& jsEngineWeak, const std::string& key, const std::string& value);
67 
68     // set callback
69     void SetMediaQueryCallback(MediaQueryCallback&& mediaQueryCallback);
70     void SetLayoutInspectorCallback(const LayoutInspectorCallback& layoutInspectorCallback);
71     void SetDrawInspectorCallback(const DrawInspectorCallback& drawInspectorCallback);
72     void SetOnStartContinuationCallBack(OnStartContinuationCallBack&& onStartContinuationCallBack);
73     void SetOnCompleteContinuationCallBack(OnCompleteContinuationCallBack&& onCompleteContinuationCallBack);
74     void SetOnSaveDataCallBack(OnSaveDataCallBack&& onSaveDataCallBack);
75     void SetOnRemoteTerminatedCallBack(OnRemoteTerminatedCallBack&& onRemoteTerminatedCallBack);
76     void SetOnRestoreDataCallBack(OnRestoreDataCallBack&& onRestoreDataCallBack);
77     void SetDestroyApplicationCallback(DestroyApplicationCallback&& destroyApplicationCallback);
78     void SetUpdateApplicationStateCallback(UpdateApplicationStateCallback&& updateApplicationStateCallback);
79     void SetOnWindowDisplayModeChangedCallback(
80         OnWindowDisplayModeChangedCallBack&& onWindowDisplayModeChangedCallBack);
81     void SetExternalEventCallback(ExternalEventCallback&& externalEventCallback);
82     void SetTimerCallback(TimerCallback&& timerCallback);
83 
GetJSAccessibilityManager()84     RefPtr<Framework::AccessibilityNodeManager> GetJSAccessibilityManager() const
85     {
86         return jsAccessibilityManager_;
87     }
88     void FireAccessibilityEvent(const AccessibilityEvent& accessibilityEvent);
89     void InitializeAccessibilityCallback();
90 
91     void OnSurfaceChanged();
92     void OnMediaQueryUpdate(bool isSynchronous = false) override;
93     void OnLayoutCompleted(const std::string& componentId);
94     void OnDrawCompleted(const std::string& componentId);
95     void FireExternalEvent(const std::string& eventId, const std::string& componentId, uint32_t nodeId, bool isDestroy);
96 
97     // FrontendDelegate overrides.
98     void Push(const std::string& uri, const std::string& params) override;
99     void PushWithMode(const std::string& uri, const std::string& params, uint32_t routerMode) override;
100     void PushWithCallback(const std::string& uri, const std::string& params,
101         const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode = 0) override;
102     void PushNamedRoute(const std::string& uri, const std::string& params,
103         const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode = 0) override;
104     void Replace(const std::string& uri, const std::string& params) override;
105     void ReplaceWithMode(const std::string& uri, const std::string& params, uint32_t routerMode) override;
106     void ReplaceWithCallback(const std::string& uri, const std::string& params,
107         const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode = 0) override;
108     void ReplaceNamedRoute(const std::string& uri, const std::string& params,
109         const std::function<void(const std::string&, int32_t)>& errorCallback, uint32_t routerMode = 0) override;
110     void Back(const std::string& uri, const std::string& params) override;
111     void BackToIndex(int32_t index, const std::string& params) override;
112     void PostponePageTransition() override;
113     void LaunchPageTransition() override;
114     void Clear() override;
115     int32_t GetStackSize() const override;
116     void GetState(int32_t& index, std::string& name, std::string& path) override;
117     void GetRouterStateByIndex(int32_t& index, std::string& name, std::string& path, std::string& params) override;
118     void GetRouterStateByUrl(std::string& url, std::vector<StateInfo>& stateArray) override;
GetPage(int32_t pageId)119     RefPtr<JsAcePage> GetPage(int32_t pageId) const override
120     {
121         return nullptr;
122     }
123     size_t GetComponentsCount() override;
124     std::string GetParams() override;
125     void NavigatePage(uint8_t type, const PageTarget& target, const std::string& params);
126 
127     void TriggerPageUpdate(int32_t pageId, bool directExecute = false) override {}
128 
129     void PostJsTask(std::function<void()>&& task, const std::string& name) override;
130     const std::string& GetAppID() const override;
131     const std::string& GetAppName() const override;
132     const std::string& GetVersionName() const override;
133     int32_t GetVersionCode() const override;
134 
135     double MeasureText(MeasureContext context) override;
136     Size MeasureTextSize(MeasureContext context) override;
137     void ShowToast(const NG::ToastInfo& toastInfo) override;
138     void ShowDialog(const std::string& title, const std::string& message, const std::vector<ButtonInfo>& buttons,
139         bool autoCancel, std::function<void(int32_t, int32_t)>&& callback,
140         const std::set<std::string>& callbacks) override;
141     void ShowDialog(const std::string& title, const std::string& message, const std::vector<ButtonInfo>& buttons,
142         bool autoCancel, std::function<void(int32_t, int32_t)>&& callback, const std::set<std::string>& callbacks,
143         std::function<void(bool)>&& onStatusChanged) override;
144     void ShowDialog(const PromptDialogAttr &dialogAttr, const std::vector<ButtonInfo> &buttons,
145         std::function<void(int32_t, int32_t)> &&callback, const std::set<std::string> &callbacks) override;
146     void ShowDialog(const PromptDialogAttr &dialogAttr, const std::vector<ButtonInfo> &buttons,
147         std::function<void(int32_t, int32_t)> &&callback, const std::set<std::string> &callbacks,
148         std::function<void(bool)>&& onStatusChanged) override;
149     void OpenCustomDialog(const PromptDialogAttr &dialogAttr, std::function<void(int32_t)> &&callback) override;
150     void CloseCustomDialog(const int32_t dialogId) override;
151     void CloseCustomDialog(const WeakPtr<NG::UINode>& node, std::function<void(int32_t)> &&callback) override;
152     void UpdateCustomDialog(const WeakPtr<NG::UINode>& node, const PromptDialogAttr &dialogAttr,
153         std::function<void(int32_t)> &&callback) override;
154 
155     void EnableAlertBeforeBackPage(const std::string& message, std::function<void(int32_t)>&& callback) override;
156 
157     void DisableAlertBeforeBackPage() override;
158 
159     void ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button,
160         std::function<void(int32_t, int32_t)>&& callback) override;
161 
162     void ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button,
163         std::function<void(int32_t, int32_t)>&& callback, std::function<void(bool)>&& onStatusChanged) override;
164 
165     void ShowActionMenuInner(DialogProperties& dialogProperties, const std::vector<ButtonInfo>& button,
166         std::function<void(int32_t, int32_t)>&& callback);
167 
168     void WaitTimer(const std::string& callbackId, const std::string& delay, bool isInterval, bool isFirst) override;
169     void ClearTimer(const std::string& callbackId) override;
AddTaskObserver(std::function<void ()> && task)170     void AddTaskObserver(std::function<void()>&& task) override {}
RemoveTaskObserver()171     void RemoveTaskObserver() override {}
PushJsCallbackToRenderNode(NodeId id,double ratio,std::function<void (bool,double)> && callback)172     void PushJsCallbackToRenderNode(NodeId id, double ratio, std::function<void(bool, double)>&& callback) override {}
173     void PostSyncTaskToPage(std::function<void()>&& task, const std::string& name) override;
SetCallBackResult(const std::string & callBackId,const std::string & result)174     void SetCallBackResult(const std::string& callBackId, const std::string& result) override {}
175     bool GetAssetContent(const std::string& url, std::string& content) override;
176     bool GetAssetContent(const std::string& url, std::vector<uint8_t>& content) override;
177     std::string GetAssetPath(const std::string& url) override;
178     // i18n
GetI18nData(std::unique_ptr<JsonValue> & json)179     void GetI18nData(std::unique_ptr<JsonValue>& json) override {}
180 
GetResourceConfiguration(std::unique_ptr<JsonValue> & json)181     void GetResourceConfiguration(std::unique_ptr<JsonValue>& json) override {}
182 
GetConfigurationCommon(const std::string & filePath,std::unique_ptr<JsonValue> & data)183     void GetConfigurationCommon(const std::string& filePath, std::unique_ptr<JsonValue>& data) override {}
GetBoundingRectData(NodeId nodeId)184     Rect GetBoundingRectData(NodeId nodeId) override
185     {
186         return Rect();
187     }
188 
GetInspector(NodeId nodeId)189     std::string GetInspector(NodeId nodeId) override
190     {
191         return "";
192     }
GetMinPlatformVersion()193     int32_t GetMinPlatformVersion() override
194     {
195         return manifestParser_->GetMinPlatformVersion();
196     }
197 
LoadResourceConfiguration(std::map<std::string,std::string> & mediaResourceFileMap,std::unique_ptr<JsonValue> & currentResourceData)198     void LoadResourceConfiguration(std::map<std::string, std::string>& mediaResourceFileMap,
199         std::unique_ptr<JsonValue>& currentResourceData) override
200     {}
201 
202     void ChangeLocale(const std::string& language, const std::string& countryOrRegion) override;
203 
204     void RegisterFont(const std::string& familyName, const std::string& familySrc, const std::string& bundleName = "",
205         const std::string& moduleName = "") override;
206 
207     void GetSystemFontList(std::vector<std::string>& fontList) override;
208 
209     bool GetSystemFont(const std::string& fontName, FontInfo& fontInfo) override;
210 
211     void GetUIFontConfig(FontConfigJsonInfo& fontConfigJsonInfo) override;
212 
HandleImage(const std::string & src,std::function<void (bool,int32_t,int32_t)> && callback)213     void HandleImage(const std::string& src, std::function<void(bool, int32_t, int32_t)>&& callback) override {}
214 
215     void GetSnapshot(const std::string& componentId,
216         std::function<void(std::shared_ptr<Media::PixelMap>, int32_t, std::function<void()>)>&& callback,
217         const NG::SnapshotOptions& options) override;
218 
219     void CreateSnapshot(std::function<void()>&& customBuilder,
220         std::function<void(std::shared_ptr<Media::PixelMap>, int32_t, std::function<void()>)>&& callback,
221         bool enableInspector, const NG::SnapshotParam& param) override;
222 
223     std::pair<int32_t, std::shared_ptr<Media::PixelMap>> GetSyncSnapshot(const std::string& componentId,
224         const NG::SnapshotOptions& options) override;
225 
226     void AddFrameNodeToOverlay(
227         const RefPtr<NG::FrameNode>& node, std::optional<int32_t> index = std::nullopt) override;
228     void RemoveFrameNodeOnOverlay(const RefPtr<NG::FrameNode>& node) override;
229     void ShowNodeOnOverlay(const RefPtr<NG::FrameNode>& node) override;
230     void HideNodeOnOverlay(const RefPtr<NG::FrameNode>& node) override;
231     void ShowAllNodesOnOverlay() override;
232     void HideAllNodesOnOverlay() override;
233 
RequestAnimationFrame(const std::string & callbackId)234     void RequestAnimationFrame(const std::string& callbackId) override {}
235 
CancelAnimationFrame(const std::string & callbackId)236     void CancelAnimationFrame(const std::string& callbackId) override {}
237     SingleTaskExecutor GetAnimationJsTask() override;
238 
239     SingleTaskExecutor GetUiTask() override;
240 
GetMediaQueryInfoInstance()241     const RefPtr<MediaQueryInfo>& GetMediaQueryInfoInstance() override
242     {
243         return mediaQueryInfo_;
244     }
245 
SetGroupJsBridge(const RefPtr<GroupJsBridge> & groupJsBridge)246     void SetGroupJsBridge(const RefPtr<GroupJsBridge>& groupJsBridge)
247     {
248         groupJsBridge_ = groupJsBridge;
249     }
GetGroupJsBridge()250     const RefPtr<GroupJsBridge>& GetGroupJsBridge() override
251     {
252         return groupJsBridge_;
253     }
254 
255     RefPtr<PipelineBase> GetPipelineContext() override;
256 
SetPageRouterManager(const RefPtr<NG::PageRouterManager> & routerMgr)257     void SetPageRouterManager(const RefPtr<NG::PageRouterManager>& routerMgr)
258     {
259         pageRouterManager_ = routerMgr;
260     }
GetPageRouterManager()261     const RefPtr<NG::PageRouterManager>& GetPageRouterManager() const
262     {
263         return pageRouterManager_;
264     }
265 
GetWindowConfig()266     WindowConfig& GetWindowConfig()
267     {
268         return manifestParser_->GetWindowConfig();
269     }
270 
GetManifestParser()271     const RefPtr<Framework::ManifestParser>& GetManifestParser() const
272     {
273         return manifestParser_;
274     }
275 
GetTaskExecutor()276     const RefPtr<TaskExecutor>& GetTaskExecutor() const
277     {
278         return taskExecutor_;
279     }
280 
281     bool OnPageBackPress();
282 
283     void OnPageShow();
284 
285     void OnPageHide();
286     void OnForeground();
287 
288     std::string GetCurrentPageUrl();
289 
290     // Get the currently running JS page information in NG structure.
291     RefPtr<RevSourceMap> GetCurrentPageSourceMap();
292 
293     // Get the currently running JS page information in NG structure.
294     RefPtr<RevSourceMap> GetFaAppSourceMap();
295 
296     void GetStageSourceMap(std::unordered_map<std::string, RefPtr<Framework::RevSourceMap>>& sourceMap);
297     void ShowDialogInner(DialogProperties& dialogProperties, std::function<void(int32_t, int32_t)>&& callback,
298         const std::set<std::string>& callbacks);
299 
300     void RebuildAllPages();
301 
302 private:
303     DialogProperties ParsePropertiesFromAttr(const PromptDialogAttr &dialogAttr);
304 
305     PipelineContextHolder pipelineContextHolder_;
306     RefPtr<TaskExecutor> taskExecutor_;
307     RefPtr<NG::PageRouterManager> pageRouterManager_;
308     RefPtr<Framework::ManifestParser> manifestParser_;
309     RefPtr<MediaQueryInfo> mediaQueryInfo_;
310     RefPtr<GroupJsBridge> groupJsBridge_;
311 
312     RefPtr<Framework::AccessibilityNodeManager> jsAccessibilityManager_;
313     RefPtr<RevSourceMap> appSourceMap_;
314     std::queue<std::string> animationFrameTaskIds_;
315     std::unordered_map<std::string, CancelableCallback<void()>> animationFrameTaskMap_;
316 
317     std::unordered_map<std::string, CancelableCallback<void()>> timeoutTaskMap_;
318 
319     MediaQueryCallback mediaQueryCallback_;
320     LayoutInspectorCallback layoutInspectorCallback_;
321     DrawInspectorCallback drawInspectorCallback_;
322     OnStartContinuationCallBack onStartContinuationCallBack_;
323     OnCompleteContinuationCallBack onCompleteContinuationCallBack_;
324     OnSaveDataCallBack onSaveDataCallBack_;
325     OnRemoteTerminatedCallBack onRemoteTerminatedCallBack_;
326     OnRestoreDataCallBack onRestoreDataCallBack_;
327     DestroyApplicationCallback destroyApplication_;
328     UpdateApplicationStateCallback updateApplicationState_;
329     OnWindowDisplayModeChangedCallBack onWindowDisplayModeChanged_;
330     ExternalEventCallback externalEvent_;
331     TimerCallback timer_;
332 
333     mutable std::mutex mutex_;
334 };
335 
336 } // namespace OHOS::Ace::Framework
337 
338 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_FRONTEND_DELEGATE_DECLARATIVE_NG_H
339