• 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_BRIDGE_PLUGIN_FRONTEND_PLUGIN_FRONTEND_DELEGATE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_PLUGIN_FRONTEND_PLUGIN_FRONTEND_DELEGATE_H
18 
19 #include <future>
20 #include <mutex>
21 #include <unordered_map>
22 
23 #include "base/memory/ace_type.h"
24 #include "base/thread/cancelable_callback.h"
25 #include "base/utils/measure_util.h"
26 #include "core/common/js_message_dispatcher.h"
27 #include "core/pipeline/pipeline_context.h"
28 #include "frameworks/bridge/common/accessibility/accessibility_node_manager.h"
29 #include "frameworks/bridge/common/manifest/manifest_parser.h"
30 #include "frameworks/bridge/js_frontend/engine/common/group_js_bridge.h"
31 #include "frameworks/bridge/js_frontend/engine/common/js_engine.h"
32 #include "frameworks/bridge/js_frontend/frontend_delegate.h"
33 #include "frameworks/bridge/js_frontend/frontend_delegate_impl.h"
34 #include "frameworks/bridge/js_frontend/js_ace_page.h"
35 
36 namespace OHOS::Ace::Framework {
37 class PluginFrontendDelegate : public FrontendDelegate {
38     DECLARE_ACE_TYPE(PluginFrontendDelegate, FrontendDelegate);
39 
40 public:
41     using onPluginUpdateWithValueParams = std::function<void(const std::string&)>;
42 
43     PluginFrontendDelegate(const RefPtr<TaskExecutor>& taskExecutor, const LoadJsCallback& loadCallback,
44         const JsMessageDispatcherSetterCallback& transferCallback, const EventCallback& asyncEventCallback,
45         const EventCallback& syncEventCallback, const UpdatePageCallback& updatePageCallback,
46         const ResetStagingPageCallback& resetLoadingPageCallback, const DestroyPageCallback& destroyPageCallback,
47         const DestroyApplicationCallback& destroyApplicationCallback,
48         const UpdateApplicationStateCallback& updateApplicationStateCallback, const TimerCallback& timerCallback,
49         const MediaQueryCallback& mediaQueryCallback, const RequestAnimationCallback& requestAnimationCallback,
50         const JsCallback& jsCallback, const OnWindowDisplayModeChangedCallBack& onWindowDisplayModeChangedCallBack,
51         const OnConfigurationUpdatedCallBack& onConfigurationUpdatedCallBack,
52         const OnSaveAbilityStateCallBack& onSaveAbilityStateCallBack,
53         const OnRestoreAbilityStateCallBack& onRestoreAbilityStateCallBack,
54         const OnNewWantCallBack& onNewWantCallBack,
55         const OnActiveCallBack& onActiveCallBack, const OnInactiveCallBack& onInactiveCallBack,
56         const OnMemoryLevelCallBack& onMemoryLevelCallBack,
57         const OnStartContinuationCallBack& onStartContinuationCallBack,
58         const OnCompleteContinuationCallBack& onCompleteContinuationCallBack,
59         const OnRemoteTerminatedCallBack& onRemoteTerminatedCallBack,
60         const OnSaveDataCallBack& onSaveDataCallBack,
61         const OnRestoreDataCallBack& onRestoreDataCallBack);
62     ~PluginFrontendDelegate() override;
63 
64     void AttachPipelineContext(const RefPtr<PipelineBase>& context) override;
65 
66     // JSFrontend delegate functions.
67     void RunPage(const std::string& url, const std::string& params);
68     void SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher>& dispatcher) const;
69     void TransferComponentResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data);
70     void TransferJsResponseData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const;
71 #if defined(PREVIEW)
72     void TransferJsResponseDataPreview(int32_t callbackId, int32_t code, ResponseData responseData) const;
73 #endif
74     void TransferJsPluginGetError(int32_t callbackId, int32_t errorCode, std::string&& errorMessage) const;
75     void TransferJsEventData(int32_t callbackId, int32_t code, std::vector<uint8_t>&& data) const;
76     void LoadPluginJsCode(std::string&& jsCode) const;
77     void LoadPluginJsByteCode(std::vector<uint8_t>&& jsCode, std::vector<int32_t>&& jsCodeLen) const;
78     void OnJSCallback(const std::string& callbackId, const std::string& data);
79     bool OnPageBackPress();
80     void OnBackGround();
81     void OnForeground();
82     void OnConfigurationUpdated(const std::string& data);
83     void OnSaveAbilityState(std::string& data);
84     void OnRestoreAbilityState(const std::string& data);
85     void OnNewWant(const std::string& data);
86     void OnSuspended();
87     bool OnStartContinuation();
88     void OnCompleteContinuation(int32_t code);
89     void OnMemoryLevel(const int32_t level);
90     void OnSaveData(std::string& data);
91     void GetPluginsUsed(std::string& data);
92     bool OnRestoreData(const std::string& data);
93     void OnRemoteTerminated();
94     void OnActive();
95     void OnInactive();
96     void OnNewRequest(const std::string& data);
97     void SetColorMode(ColorMode colorMode);
98     void CallPopPage();
99     void OnApplicationDestroy(const std::string& packageName);
100     void UpdateApplicationState(const std::string& packageName, Frontend::State state);
101     void OnWindowDisplayModeChanged(bool isShownInMultiWindow, const std::string& data);
102     void NotifyAppStorage(const WeakPtr<Framework::JsEngine>& jsEngineWeak,
103         const std::string& key, const std::string& value);
104 
105     // Accessibility delegate functions.
GetJSAccessibilityManager()106     RefPtr<Framework::AccessibilityNodeManager> GetJSAccessibilityManager() const
107     {
108         return jsAccessibilityManager_;
109     }
110     void FireAccessibilityEvent(const AccessibilityEvent& accessibilityEvent);
111     void InitializeAccessibilityCallback();
112 
113     void OnMediaQueryUpdate() override;
114     void OnSurfaceChanged();
115     // JSEventHandler delegate functions.
116     void FireAsyncEvent(const std::string& eventId, const std::string& param, const std::string& jsonArgs);
117     bool FireSyncEvent(const std::string& eventId, const std::string& param, const std::string& jsonArgs);
118     void FireSyncEvent(
119         const std::string& eventId, const std::string& param, const std::string& jsonArgs, std::string& result);
120 
121     // FrontendDelegate overrides.
122     void Push(const PageTarget& target, const std::string& params);
123     void Replace(const PageTarget& target, const std::string& params);
124     void BackWithTarget(const PageTarget& target, const std::string& params);
125     void Push(const std::string& uri, const std::string& params) override;
126     void Replace(const std::string& uri, const std::string& params) override;
127     void Back(const std::string& uri, const std::string& params) override;
128     void PostponePageTransition() override;
129     void LaunchPageTransition() override;
130     void Clear() override;
131     int32_t GetStackSize() const override;
132     void GetState(int32_t& index, std::string& name, std::string& path) override;
133     size_t GetComponentsCount() override;
134     std::string GetParams() override;
135     void TriggerPageUpdate(int32_t pageId, bool directExecute = false) override;
136 
137     void PostJsTask(std::function<void()>&& task) override;
138 
139     const std::string& GetAppID() const override;
140     const std::string& GetAppName() const override;
141     const std::string& GetVersionName() const override;
142     int32_t GetVersionCode() const override;
GetWindowConfig()143     WindowConfig& GetWindowConfig()
144     {
145         return manifestParser_->GetWindowConfig();
146     }
147 
148     double MeasureText(const MeasureContext& context) override;
149     Size MeasureTextSize(const MeasureContext& context) override;
150 
151     void ShowToast(const std::string& message, int32_t duration, const std::string& bottom) override;
152     void ShowDialog(const std::string& title, const std::string& message,
153         const std::vector<ButtonInfo>& buttons, bool autoCancel, std::function<void(int32_t, int32_t)>&& callback,
154         const std::set<std::string>& callbacks) override;
155 
EnableAlertBeforeBackPage(const std::string & message,std::function<void (int32_t)> && callback)156     void EnableAlertBeforeBackPage(const std::string& message, std::function<void(int32_t)>&& callback) override {}
157 
DisableAlertBeforeBackPage()158     void DisableAlertBeforeBackPage() override {}
159 
ShowActionMenu(const std::string & title,const std::vector<ButtonInfo> & button,std::function<void (int32_t,int32_t)> && callback)160     void ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button,
161         std::function<void(int32_t, int32_t)>&& callback) override {}
162 
163     Rect GetBoundingRectData(NodeId nodeId) override;
164 
165     std::string GetInspector(NodeId nodeId) override;
166 
167     void PushJsCallbackToRenderNode(NodeId id, double ratio, std::function<void(bool, double)>&& callback) override;
168     void RemoveVisibleChangeNode(NodeId id) override;
169     // For async event.
170     void SetCallBackResult(const std::string& callBackId, const std::string& result) override;
171 
172     void WaitTimer(const std::string& callbackId, const std::string& delay, bool isInterval, bool isFirst) override;
173     void ClearTimer(const std::string& callbackId) override;
174 
175     void PostSyncTaskToPage(std::function<void()>&& task) override;
176     void AddTaskObserver(std::function<void()>&& task) override;
177     void RemoveTaskObserver() override;
178 
179     bool GetAssetContent(const std::string& url, std::string& content) override;
180     bool GetAssetContent(const std::string& url, std::vector<uint8_t>& content) override;
181     std::string GetAssetPath(const std::string& url) override;
182 
183     // i18n
184     void GetI18nData(std::unique_ptr<JsonValue>& json) override;
185 
186     void GetResourceConfiguration(std::unique_ptr<JsonValue>& json) override;
187 
188     void GetConfigurationCommon(const std::string& filePath, std::unique_ptr<JsonValue>& data) override;
189 
190     int32_t GetMinPlatformVersion() override;
191 
192     void LoadResourceConfiguration(std::map<std::string, std::string>& mediaResourceFileMap,
193         std::unique_ptr<JsonValue>& currentResourceData) override;
194 
195     void ChangeLocale(const std::string& language, const std::string& countryOrRegion) override;
196 
197     void RegisterFont(const std::string& familyName, const std::string& familySrc) override;
198 
199     void HandleImage(const std::string& src, std::function<void(bool, int32_t, int32_t)>&& callback) override;
200 
201     void RequestAnimationFrame(const std::string& callbackId) override;
202 
203     void CancelAnimationFrame(const std::string& callbackId) override;
204 
205     SingleTaskExecutor GetAnimationJsTask() override;
206 
207     SingleTaskExecutor GetUiTask() override;
208 
GetMediaQueryInfoInstance()209     const RefPtr<MediaQueryInfo>& GetMediaQueryInfoInstance() override
210     {
211         return mediaQueryInfo_;
212     }
213 
GetGroupJsBridge()214     const RefPtr<GroupJsBridge>& GetGroupJsBridge() override
215     {
216         return groupJsBridge_;
217     }
218 
219     RefPtr<PipelineBase> GetPipelineContext() override;
220 
SetGroupJsBridge(const RefPtr<GroupJsBridge> & groupJsBridge)221     void SetGroupJsBridge(const RefPtr<GroupJsBridge>& groupJsBridge)
222     {
223         groupJsBridge_ = groupJsBridge;
224     }
225 
226     RefPtr<JsAcePage> GetPage(int32_t pageId) const override;
227 
228     void RebuildAllPages();
229 
230     void UpdatePlugin(const std::string& content);
231 
SetDeclarativeOnUpdateWithValueParamsCallback(onPluginUpdateWithValueParams && callback)232     void SetDeclarativeOnUpdateWithValueParamsCallback(onPluginUpdateWithValueParams&& callback)
233     {
234         if (callback) {
235             onPluginUpdateWithValueParams_ = callback;
236         }
237     }
238 
FireDeclarativeOnUpdateWithValueParamsCallback(const std::string & params)239     void FireDeclarativeOnUpdateWithValueParamsCallback(const std::string& params) const
240     {
241         if (onPluginUpdateWithValueParams_) {
242             onPluginUpdateWithValueParams_(params);
243         }
244     }
245 
246 private:
247     int32_t GenerateNextPageId();
248     void RecyclePageId(int32_t pageId);
249 
250     void LoadPage(int32_t pageId, const PageTarget& target, bool isMainPage, const std::string& params);
251     void LoadJS(const RefPtr<Framework::JsAcePage>& page, const std::string& url, bool isMainPage);
252     void OnPageReady(
253         const RefPtr<Framework::JsAcePage>& page, const std::string& url, bool isMainPage);
254     void FlushPageCommand(
255         const RefPtr<Framework::JsAcePage>& page, const std::string& url, bool isMainPage);
256     void AddPageLocked(const RefPtr<JsAcePage>& page);
257     void OnPrePageChange(const RefPtr<JsAcePage>& page);
258     void SetCurrentPage(int32_t pageId);
259 
260     void OnPushPageSuccess(const RefPtr<JsAcePage>& page, const std::string& url);
261     void OnPopToPageSuccess(const std::string& url);
262     void PopToPage(const std::string& url);
263     int32_t OnPopPageSuccess();
264     void PopPage();
265 
266     void PopPageTransitionListener(const TransitionEvent& event, int32_t destroyPageId);
267 
268     void PopToPageTransitionListener(
269         const TransitionEvent& event, const std::string& url, int32_t pageId);
270 
271     int32_t OnClearInvisiblePagesSuccess();
272     void ClearInvisiblePages();
273 
274     void OnReplacePageSuccess(const RefPtr<JsAcePage>& page, const std::string& url);
275     void ReplacePage(const RefPtr<JsAcePage>& page, const std::string& url);
276     void LoadReplacePage(int32_t pageId, const PageTarget& url, const std::string& params);
277 
278     uint64_t GetSystemRealTime();
279 
280     // Page lifecycle
281     void OnPageShow();
282     void OnPageHide();
283     void OnPageDestroy(int32_t pageId);
284 
285     int32_t GetRunningPageId() const;
286     std::string GetRunningPageUrl() const;
287     int32_t GetPageIdByUrl(const std::string& url);
288 
289     void ResetStagingPage();
290     void FlushAnimationTasks();
291 
292     std::atomic<uint64_t> pageIdPool_ = 0;
293     int32_t callbackCnt_ = 0;
294     int32_t pageId_ = -1;
295     bool isRouteStackFull_ = false;
296     bool isStagingPageExist_ = false;
297     std::string mainPagePath_;
298     std::vector<PageInfo> pageRouteStack_;
299     std::unordered_map<int32_t, RefPtr<JsAcePage>> pageMap_;
300     std::unordered_map<int32_t, std::string> pageParamMap_;
301     std::unordered_map<int32_t, std::string> jsCallBackResult_;
302 
303     LoadJsCallback loadJs_;
304     JsMessageDispatcherSetterCallback dispatcherCallback_;
305     EventCallback asyncEvent_;
306     EventCallback syncEvent_;
307     UpdatePageCallback updatePage_;
308     ResetStagingPageCallback resetStagingPage_;
309     DestroyPageCallback destroyPage_;
310     DestroyApplicationCallback destroyApplication_;
311     UpdateApplicationStateCallback updateApplicationState_;
312     TimerCallback timer_;
313     std::unordered_map<std::string, CancelableCallback<void()>> timeoutTaskMap_;
314     MediaQueryCallback mediaQueryCallback_;
315     RequestAnimationCallback requestAnimationCallback_;
316     JsCallback jsCallback_;
317     OnWindowDisplayModeChangedCallBack onWindowDisplayModeChanged_;
318     OnConfigurationUpdatedCallBack onConfigurationUpdated_;
319     OnSaveAbilityStateCallBack onSaveAbilityState_;
320     OnRestoreAbilityStateCallBack onRestoreAbilityState_;
321     OnNewWantCallBack onNewWant_;
322     OnActiveCallBack onActive_;
323     OnInactiveCallBack onInactive_;
324     OnMemoryLevelCallBack onMemoryLevel_;
325     onPluginUpdateWithValueParams onPluginUpdateWithValueParams_;
326     OnStartContinuationCallBack onStartContinuationCallBack_;
327     OnCompleteContinuationCallBack onCompleteContinuationCallBack_;
328     OnRemoteTerminatedCallBack onRemoteTerminatedCallBack_;
329     OnSaveDataCallBack onSaveDataCallBack_;
330     OnRestoreDataCallBack onRestoreDataCallBack_;
331     RefPtr<Framework::ManifestParser> manifestParser_;
332     RefPtr<Framework::AccessibilityNodeManager> jsAccessibilityManager_;
333     RefPtr<MediaQueryInfo> mediaQueryInfo_;
334     RefPtr<GroupJsBridge> groupJsBridge_;
335 
336     RefPtr<TaskExecutor> taskExecutor_;
337 
338     PipelineContextHolder pipelineContextHolder_;
339 
340     BaseId::IdType pageTransitionListenerId_ = 0L;
341     std::queue<std::string> animationFrameTaskIds_;
342     std::unordered_map<std::string, CancelableCallback<void()>> animationFrameTaskMap_;
343 
344     mutable std::mutex mutex_;
345 };
346 } // namespace OHOS::Ace::Framework
347 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_PLUGIN_FRONTEND_PLUGIN_FRONTEND_DELEGATE_H
348