• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_JS_FRONTEND_FRONTEND_DELEGATE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_FRONTEND_DELEGATE_H
18 
19 #include <vector>
20 
21 #include "base/json/json_util.h"
22 #include "base/memory/ace_type.h"
23 #include "base/utils/noncopyable.h"
24 #include "core/event/ace_event_helper.h"
25 #include "core/pipeline/pipeline_context.h"
26 #include "frameworks/bridge/common/media_query/media_query_info.h"
27 #include "frameworks/bridge/js_frontend/engine/common/group_js_bridge.h"
28 #include "frameworks/bridge/js_frontend/engine/common/js_constants.h"
29 #include "frameworks/bridge/js_frontend/js_ace_page.h"
30 
31 namespace OHOS::Ace::Framework {
32 enum class AlertState { USER_CANCEL = 0, USER_CONFIRM, RECOVERY };
33 
34 // A virtual interface which must be implemented as a backing for
35 // FrontendDelegateImpl instances.
36 //
37 // This is the primary interface by which the JsFrontend delegates
38 // FrontendDelegateImpl behavior out to QjsEngine for js to native calls.
39 class FrontendDelegate : public AceType {
40     DECLARE_ACE_TYPE(FrontendDelegate, AceType);
41 
42 public:
43     FrontendDelegate() = default;
44     ~FrontendDelegate() override = default;
45 
46     virtual void AttachPipelineContext(const RefPtr<PipelineContext>& context) = 0;
SetAssetManager(const RefPtr<AssetManager> & assetManager)47     ACE_EXPORT void SetAssetManager(const RefPtr<AssetManager>& assetManager)
48     {
49         assetManager_ = assetManager;
50     }
51 
52     // ----------------
53     // system.router
54     // ----------------
55     // Jump to the specified page.
56     virtual void Push(const std::string& uri, const std::string& params) = 0;
57     // Jump to the specified page, but current page will be removed from the stack.
58     virtual void Replace(const std::string& uri, const std::string& params) = 0;
59     // Back to specified page or the previous page if url not set.
60     virtual void Back(const std::string& uri, const std::string& params = "") = 0;
61     // Postpone page transition after Begin called, usually to wait some async operation
62     virtual void PostponePageTransition() = 0;
63     // Begin page transition after Postpone called, usually to wait some async operation
64     virtual void LaunchPageTransition() = 0;
65     // Clear all the pages in stack except the top page, that is current page.
66     virtual void Clear() = 0;
67     // Gets the number of pages in the page stack.
68     virtual int32_t GetStackSize() const = 0;
69     // Gets current page's states
70     virtual void GetState(int32_t& index, std::string& name, std::string& path) = 0;
71     // Gets current page's params
GetParams()72     virtual std::string GetParams()
73     {
74         return "";
75     }
76 
77     // distribute
RestoreRouterStack(const std::string & contentInfo)78     virtual std::string RestoreRouterStack(const std::string& contentInfo)
79     {
80         return "";
81     }
GetContentInfo()82     virtual std::string GetContentInfo()
83     {
84         return "";
85     }
86 
87     virtual void TriggerPageUpdate(int32_t pageId, bool directExecute = false) = 0;
88 
89     // posting js task from jsengine
90     virtual void PostJsTask(std::function<void()>&& task) = 0;
91 
92     virtual void RemoveVisibleChangeNode(NodeId id) = 0;
93 
94     // ----------------
95     // system.app
96     // ----------------
97     virtual const std::string& GetAppID() const = 0;
98     virtual const std::string& GetAppName() const = 0;
99     virtual const std::string& GetVersionName() const = 0;
100     virtual int32_t GetVersionCode() const = 0;
101 
102     // ----------------
103     // system.prompt
104     // ----------------
105     virtual void ShowToast(const std::string& message, int32_t duration, const std::string& bottom) = 0;
106     virtual void ShowDialog(const std::string& title, const std::string& message,
107         const std::vector<ButtonInfo>& buttons, bool autoCancel, std::function<void(int32_t, int32_t)>&& callback,
108         const std::set<std::string>& callbacks) = 0;
109 
110     virtual void EnableAlertBeforeBackPage(const std::string& message, std::function<void(int32_t)>&& callback) = 0;
111     virtual void DisableAlertBeforeBackPage() = 0;
112 
113     virtual void ShowActionMenu(const std::string& title, const std::vector<ButtonInfo>& button,
114         std::function<void(int32_t, int32_t)>&& callback) = 0;
115 
116     virtual Rect GetBoundingRectData(NodeId nodeId) = 0;
117 
118     virtual std::string GetInspector(NodeId nodeId) = 0;
119 
120     virtual void PushJsCallbackToRenderNode(NodeId id, double ratio, std::function<void(bool, double)>&& callback) = 0;
121 
122     // ----------------
123     // system.configuration
124     // ----------------
125     virtual void ChangeLocale(const std::string& language, const std::string& countryOrRegion) = 0;
126 
127     // ----------------
128     // system.image
129     // ----------------
130     virtual void HandleImage(const std::string& src, std::function<void(bool, int32_t, int32_t)>&& callback) = 0;
131     // ----------------
132     // internal.jsResult
133     // ----------------
134     virtual void SetCallBackResult(const std::string& callBackId, const std::string& result) = 0;
135 
136     // ----------------
137     // system.animation
138     // ----------------
139     virtual void RequestAnimationFrame(const std::string& callbackId) = 0;
140     virtual void CancelAnimationFrame(const std::string& callbackId) = 0;
141 
142     virtual bool GetAssetContent(const std::string& url, std::string& content) = 0;
143     virtual bool GetAssetContent(const std::string& url, std::vector<uint8_t>& content) = 0;
144     virtual std::string GetAssetPath(const std::string& url) = 0;
145 
146     virtual void WaitTimer(const std::string& callbackId, const std::string& delay, bool isInterval, bool isFirst) = 0;
147     virtual void ClearTimer(const std::string& callbackId) = 0;
148 
149     virtual void PostSyncTaskToPage(std::function<void()>&& task) = 0;
150 
151     virtual void AddTaskObserver(std::function<void()>&& task) = 0;
152 
153     virtual void RemoveTaskObserver() = 0;
154 
155     virtual void GetI18nData(std::unique_ptr<JsonValue>& json) = 0;
156 
157     virtual void GetResourceConfiguration(std::unique_ptr<JsonValue>& json) = 0;
158 
159     virtual void GetConfigurationCommon(const std::string& filePath, std::unique_ptr<JsonValue>& data) = 0;
160 
161     virtual const RefPtr<MediaQueryInfo>& GetMediaQueryInfoInstance() = 0;
162 
163     virtual void OnMediaQueryUpdate() = 0;
164 
165     virtual void RegisterFont(const std::string& familyName, const std::string& familySrc) = 0;
166 
167     virtual SingleTaskExecutor GetAnimationJsTask() = 0;
168 
169     virtual SingleTaskExecutor GetUiTask() = 0;
170 
171     virtual RefPtr<PipelineContext> GetPipelineContext() = 0;
172 
173     virtual const RefPtr<GroupJsBridge>& GetGroupJsBridge() = 0;
174 
175     virtual RefPtr<JsAcePage> GetPage(int32_t pageId) const = 0;
176 
177     virtual int32_t GetMinPlatformVersion() = 0;
178 
179     template<typename T>
180     bool ACE_EXPORT GetResourceData(const std::string& fileUri, T& content);
181 
182     template<typename T>
183     bool ACE_EXPORT GetResourceData(const std::string& fileUri, T& content, std::string& ami);
184 
185     template<typename T>
186     ACE_EXPORT static bool GetResourceData(const std::string& fileUri, const RefPtr<AssetManager>& assetManager,
187         T& content);
188 
GetAssetManager()189     ACE_EXPORT RefPtr<AssetManager> GetAssetManager() const
190     {
191         return assetManager_;
192     }
193 
194     virtual void LoadResourceConfiguration(std::map<std::string, std::string>& sortedResourcePath,
195         std::unique_ptr<JsonValue>& currentResourceData) = 0;
196 
DisallowPopLastPage()197     void DisallowPopLastPage()
198     {
199         disallowPopLastPage_ = true;
200     }
201 
CallNativeHandler(const std::string & event,const std::string & params)202     virtual void CallNativeHandler(const std::string& event, const std::string& params) {}
203 
204 protected:
205     RefPtr<AssetManager> assetManager_;
206     bool disallowPopLastPage_ = false;
207 
208     ACE_DISALLOW_COPY_AND_MOVE(FrontendDelegate);
209 };
210 
211 } // namespace OHOS::Ace::Framework
212 
213 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_FRONTEND_DELEGATE_H
214