• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 NWEB_WEBVIEW_CONTROLLER_H
17 #define NWEB_WEBVIEW_CONTROLLER_H
18 
19 #include <memory>
20 #include <string>
21 #include <unordered_map>
22 
23 #include "context/application_context.h"
24 #include "napi/native_api.h"
25 #include "napi/native_common.h"
26 #include "napi/native_node_api.h"
27 #include "nweb.h"
28 #include "nweb_helper.h"
29 #include "nweb_web_message.h"
30 #include "web_errors.h"
31 #include "webview_javascript_result_callback.h"
32 
33 namespace OHOS {
34 namespace NWeb {
35 enum class WebHitTestType : int {
36     EDIT = 0,
37     EMAIL,
38     HTTP,
39     HTTP_IMG,
40     IMG,
41     MAP,
42     PHONE,
43     UNKNOWN
44 };
45 
46 enum class ResourceType : uint32_t {
47     COLOR = 10001,
48     FLOAT,
49     STRING,
50     PLURAL,
51     BOOLEAN,
52     INTARRAY,
53     INTEGER,
54     PATTERN,
55     STRARRAY,
56     MEDIA = 20000,
57     RAWFILE = 30000
58 };
59 
60 class WebviewController {
61 public:
62     explicit WebviewController() = default;
63     explicit WebviewController(int32_t nwebId);
64     ~WebviewController() = default;
65 
66     bool IsInit();
67 
68     void SetWebId(int32_t nwebId);
69 
70     bool AccessForward();
71 
72     bool AccessBackward();
73 
74     bool AccessStep(int32_t step);
75 
76     void ClearHistory();
77 
78     void Forward();
79 
80     void Backward();
81 
82     void OnActive();
83 
84     void OnInactive();
85 
86     void Refresh();
87 
88     ErrCode ZoomIn();
89 
90     ErrCode ZoomOut();
91 
92     int32_t GetWebId() const;
93 
94     std::string GetUserAgent();
95 
96     std::string GetTitle();
97 
98     int32_t GetPageHeight();
99 
100     ErrCode BackOrForward(int32_t step);
101 
102     void StoreWebArchiveCallback(const std::string &baseName, bool autoName, napi_env env, napi_ref jsCallback);
103 
104     void StoreWebArchivePromise(const std::string &baseName, bool autoName, napi_env env, napi_deferred deferred);
105 
106     ErrCode CreateWebMessagePorts(std::vector<std::string>& ports);
107 
108     ErrCode PostWebMessage(std::string& message, std::vector<std::string>& ports, std::string& targetUrl);
109 
110     HitTestResult GetHitTestValue();
111 
112     void RequestFocus();
113 
114     ErrCode ParseUrl(napi_env env, napi_value urlObj, std::string& result);
115 
116     ErrCode LoadUrl(std::string url);
117 
118     ErrCode LoadUrl(std::string url, std::map<std::string, std::string> httpHeaders);
119 
120     ErrCode LoadData(std::string data, std::string mimeType, std::string encoding,
121         std::string baseUrl, std::string historyUrl);
122 
123     int GetHitTest();
124 
125     void ClearMatches();
126 
127     void SearchNext(bool forward);
128 
129     void SearchAllAsync(const std::string& searchString);
130 
131     void ClearSslCache();
132 
133     void ClearClientAuthenticationCache();
134 
135     void Stop();
136 
137     ErrCode Zoom(float factor);
138 
139     void SetNWebJavaScriptResultCallBack();
140 
141     void RegisterJavaScriptProxy(napi_env env, napi_value obj,
142         const std::string& objName, const std::vector<std::string>& methodList);
143 
144     ErrCode DeleteJavaScriptRegister(const std::string& objName,
145         const std::vector<std::string>& methodList);
146 
147     void RunJavaScriptCallback(const std::string &script, napi_env env, napi_ref jsCallback);
148 
149     void RunJavaScriptPromise(const std::string &script, napi_env env, napi_deferred deferred);
150 
151     std::string GetUrl();
152 
153     std::string GetOriginalUrl();
154 
155     void PutNetworkAvailable(bool available);
156 
157     bool HasImage(std::shared_ptr<NWebValueCallback<bool>> callback);
158 
159     ErrCode HasImagesCallback(napi_env env, napi_ref jsCallback);
160 
161     ErrCode HasImagesPromise(napi_env env, napi_deferred deferred);
162 
163     void RemoveCache(bool include_disk_files);
164 
165     std::shared_ptr<NWebHistoryList> GetHistoryList();
166 
167     bool GetFavicon(
168         const void **data, size_t &width, size_t &height, ImageColorType &colorType, ImageAlphaType &alphaType);
169 
170     WebState SerializeWebState();
171 
172     bool RestoreWebState(WebState state);
173 
174     void ScrollPageDown(bool bottom);
175 
176     void ScrollPageUp(bool top);
177 
178     void ScrollTo(float x, float y);
179 
180     void ScrollBy(float deltaX, float deltaY);
181 
182     void SlideScroll(float vx, float vy);
183 
184 private:
185     std::shared_ptr<Global::Resource::ResourceManager> GetResourceMgr(std::string bundleName, std::string moduleName);
186 
187     int ConverToWebHitTestType(int hitType);
188 
189 public:
190     static std::string customeSchemeCmdLine_;
191     static bool existNweb_;
192     static bool webDebuggingAccess_;
193 
194 private:
195     std::weak_ptr<OHOS::NWeb::NWeb> nweb_;
196     std::shared_ptr<WebviewJavaScriptResultCallBack> javaScriptResultCb_ = nullptr;
197 };
198 
199 class WebMessagePort {
200 public:
201     WebMessagePort(int32_t nwebId, std::string& port);
202 
203     ~WebMessagePort() = default;
204 
205     ErrCode ClosePort();
206 
207     ErrCode PostPortMessage(std::shared_ptr<NWebMessage> data);
208 
209     ErrCode SetPortMessageCallback(std::shared_ptr<NWebValueCallback<std::shared_ptr<NWebMessage>>> callback);
210 
211     std::string GetPortHandle() const;
212 
213 private:
214     std::weak_ptr<OHOS::NWeb::NWeb> nweb_;
215     std::string portHandle_;
216 };
217 
218 class WebHistoryList {
219 public:
WebHistoryList(std::shared_ptr<NWebHistoryList> sptrHistoryList)220     explicit WebHistoryList(std::shared_ptr<NWebHistoryList> sptrHistoryList) : sptrHistoryList_(sptrHistoryList) {};
221     ~WebHistoryList() = default;
222 
223     int32_t GetCurrentIndex();
224 
225     std::shared_ptr<NWebHistoryItem> GetItem(int32_t index);
226 
227     int32_t GetListSize();
228 private:
229     OHOS::NWeb::NWeb* nweb_ = nullptr;
230     std::shared_ptr<NWebHistoryList> sptrHistoryList_ = nullptr;
231 };
232 } // namespace NWeb
233 } // namespace OHOS
234 
235 #endif // NWEB_WEBVIEW_CONTROLLER_H
236