• 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 "napi/native_api.h"
24 #include "napi/native_common.h"
25 #include "napi/native_node_api.h"
26 #include "nweb.h"
27 #include "nweb_helper.h"
28 #include "nweb_web_message.h"
29 #include "web_errors.h"
30 #include "webview_javascript_result_callback.h"
31 
32 namespace OHOS {
33 namespace NWeb {
34 enum class WebHitTestType : int {
35     EDIT = 0,
36     EMAIL,
37     HTTP,
38     HTTP_IMG,
39     IMG,
40     MAP,
41     PHONE,
42     UNKNOWN
43 };
44 
45 enum class SecureDnsModeType : int {
46     OFF = 0,
47     AUTO,
48     SECURE_ONLY
49 };
50 
51 enum class ResourceType : uint32_t {
52     COLOR = 10001,
53     FLOAT,
54     STRING,
55     PLURAL,
56     BOOLEAN,
57     INTARRAY,
58     INTEGER,
59     PATTERN,
60     STRARRAY,
61     MEDIA = 20000,
62     RAWFILE = 30000
63 };
64 
65 enum class WebMessageType : int {
66     NOTSUPPORT = 0,
67     STRING,
68     NUMBER,
69     BOOLEAN,
70     ARRAYBUFFER,
71     ARRAY,
72     ERROR
73 };
74 
75 class WebviewController {
76 public:
77     explicit WebviewController() = default;
78     explicit WebviewController(int32_t nwebId);
79     ~WebviewController() = default;
80 
81     bool IsInit();
82 
83     void SetWebId(int32_t nwebId);
84 
85     bool AccessForward();
86 
87     bool AccessBackward();
88 
89     bool AccessStep(int32_t step);
90 
91     void ClearHistory();
92 
93     void Forward();
94 
95     void Backward();
96 
97     void OnActive();
98 
99     void OnInactive();
100 
101     void Refresh();
102 
103     ErrCode ZoomIn();
104 
105     ErrCode ZoomOut();
106 
107     int32_t GetWebId() const;
108 
109     std::string GetUserAgent();
110 
111     std::string GetCustomUserAgent() const;
112 
113     ErrCode SetCustomUserAgent(const std::string& userAgent);
114 
115     std::string GetTitle();
116 
117     int32_t GetPageHeight();
118 
119     ErrCode BackOrForward(int32_t step);
120 
121     void StoreWebArchiveCallback(const std::string &baseName, bool autoName, napi_env env, napi_ref jsCallback);
122 
123     void StoreWebArchivePromise(const std::string &baseName, bool autoName, napi_env env, napi_deferred deferred);
124 
125     ErrCode CreateWebMessagePorts(std::vector<std::string>& ports);
126 
127     ErrCode PostWebMessage(std::string& message, std::vector<std::string>& ports, std::string& targetUrl);
128 
129     HitTestResult GetHitTestValue();
130 
131     void RequestFocus();
132 
133     bool ParseUrl(napi_env env, napi_value urlObj, std::string& result);
134 
135     ErrCode LoadUrl(std::string url);
136 
137     ErrCode LoadUrl(std::string url, std::map<std::string, std::string> httpHeaders);
138 
139     ErrCode LoadData(std::string data, std::string mimeType, std::string encoding,
140         std::string baseUrl, std::string historyUrl);
141 
142     int GetHitTest();
143 
144     void ClearMatches();
145 
146     void SearchNext(bool forward);
147 
148     void SearchAllAsync(const std::string& searchString);
149 
150     void ClearSslCache();
151 
152     void ClearClientAuthenticationCache();
153 
154     void Stop();
155 
156     ErrCode Zoom(float factor);
157 
158     void SetNWebJavaScriptResultCallBack();
159 
160     void RegisterJavaScriptProxy(napi_env env, napi_value obj,
161         const std::string& objName, const std::vector<std::string>& methodList);
162 
163     ErrCode DeleteJavaScriptRegister(const std::string& objName,
164         const std::vector<std::string>& methodList);
165 
166     void RunJavaScriptCallback(const std::string &script, napi_env env, napi_ref jsCallback, bool extention);
167 
168     void RunJavaScriptPromise(const std::string &script, napi_env env, napi_deferred deferred, bool extention);
169 
170     std::string GetUrl();
171 
172     std::string GetOriginalUrl();
173 
174     void PutNetworkAvailable(bool available);
175 
176     bool HasImage(std::shared_ptr<NWebValueCallback<bool>> callback);
177 
178     ErrCode HasImagesCallback(napi_env env, napi_ref jsCallback);
179 
180     ErrCode HasImagesPromise(napi_env env, napi_deferred deferred);
181 
182     void RemoveCache(bool include_disk_files);
183 
184     std::shared_ptr<NWebHistoryList> GetHistoryList();
185 
186     bool GetFavicon(
187         const void **data, size_t &width, size_t &height, ImageColorType &colorType, ImageAlphaType &alphaType);
188 
189     WebState SerializeWebState();
190 
191     bool RestoreWebState(WebState state);
192 
193     void ScrollPageDown(bool bottom);
194 
195     void ScrollPageUp(bool top);
196 
197     void ScrollTo(float x, float y);
198 
199     void ScrollBy(float deltaX, float deltaY);
200 
201     void SlideScroll(float vx, float vy);
202 
203     void InnerSetHapPath(const std::string &hapPath);
204 
205     bool GetCertChainDerData(std::vector<std::string> &certChainDerData);
206 
207     ErrCode SetAudioMuted(bool muted);
208 
209     ErrCode PrefetchPage(std::string& url, std::map<std::string, std::string> additionalHttpHeaders);
210 
211 private:
212     int ConverToWebHitTestType(int hitType);
213 
214     bool GetRawFileUrl(const std::string &fileName, std::string &result);
215 
216 public:
217     static std::string customeSchemeCmdLine_;
218     static bool existNweb_;
219     static bool webDebuggingAccess_;
220 
221 private:
222     std::weak_ptr<OHOS::NWeb::NWeb> nweb_;
223     std::shared_ptr<WebviewJavaScriptResultCallBack> javaScriptResultCb_ = nullptr;
224     std::string hapPath_ = "";
225 };
226 
227 class WebMessagePort {
228 public:
229     WebMessagePort(int32_t nwebId, std::string& port, bool isExtentionType);
230 
231     ~WebMessagePort() = default;
232 
233     ErrCode ClosePort();
234 
235     ErrCode PostPortMessage(std::shared_ptr<NWebMessage> data);
236 
237     ErrCode SetPortMessageCallback(std::shared_ptr<NWebValueCallback<std::shared_ptr<NWebMessage>>> callback);
238 
239     std::string GetPortHandle() const;
240 
IsExtentionType()241     bool IsExtentionType()
242     {
243         return isExtentionType_;
244     }
245 
246 private:
247     std::weak_ptr<OHOS::NWeb::NWeb> nweb_;
248     std::string portHandle_;
249     bool isExtentionType_;
250 };
251 
252 class WebMessageExt {
253 public:
WebMessageExt(std::shared_ptr<NWebMessage> data)254     explicit WebMessageExt(std::shared_ptr<NWebMessage> data) : data_(data) {};
255     ~WebMessageExt() = default;
256 
SetType(int type)257     void SetType(int type)
258     {
259         type_ = type;
260         WebMessageType jsType = static_cast<WebMessageType>(type);
261         NWebValue::Type nwebType = NWebValue::Type::NONE;
262         switch (jsType) {
263             case WebMessageType::STRING: {
264                 nwebType = NWebValue::Type::STRING;
265                 break;
266             }
267             case WebMessageType::NUMBER: {
268                 nwebType = NWebValue::Type::DOUBLE;
269                 break;
270             }
271             case WebMessageType::BOOLEAN: {
272                 nwebType = NWebValue::Type::BOOLEAN;
273                 break;
274             }
275             case WebMessageType::ARRAYBUFFER: {
276                 nwebType = NWebValue::Type::STRINGARRAY;
277                 break;
278             }
279             case WebMessageType::ARRAY: {
280                 nwebType = NWebValue::Type::STRINGARRAY;
281                 break;
282             }
283             case WebMessageType::ERROR: {
284                 nwebType = NWebValue::Type::ERROR;
285                 break;
286             }
287             default: {
288                 nwebType = NWebValue::Type::NONE;
289                 break;
290             }
291         }
292         if (data_) {
293             data_->SetType(nwebType);
294         }
295     }
296 
ConvertNwebType2JsType(NWebValue::Type type)297     int ConvertNwebType2JsType(NWebValue::Type type)
298     {
299         WebMessageType jsType = WebMessageType::NOTSUPPORT;
300         switch (type) {
301             case NWebValue::Type::STRING: {
302                 jsType = WebMessageType::STRING;
303                 break;
304             }
305             case NWebValue::Type::DOUBLE:
306             case NWebValue::Type::INTEGER: {
307                 jsType = WebMessageType::NUMBER;
308                 break;
309             }
310             case NWebValue::Type::BOOLEAN: {
311                 jsType = WebMessageType::BOOLEAN;
312                 break;
313             }
314             case NWebValue::Type::STRINGARRAY:
315             case NWebValue::Type::DOUBLEARRAY:
316             case NWebValue::Type::INT64ARRAY:
317             case NWebValue::Type::BOOLEANARRAY: {
318                 jsType = WebMessageType::ARRAY;
319                 break;
320             }
321             case NWebValue::Type::BINARY: {
322                 jsType = WebMessageType::ARRAYBUFFER;
323                 break;
324             }
325             case NWebValue::Type::ERROR: {
326                 jsType = WebMessageType::ERROR;
327                 break;
328             }
329             default: {
330                 jsType = WebMessageType::NOTSUPPORT;
331                 break;
332             }
333         }
334         return static_cast<int>(jsType);
335     }
336 
GetType()337     int GetType()
338     {
339         if (data_) {
340             return ConvertNwebType2JsType(data_->GetType());
341         }
342         return static_cast<int>(WebMessageType::NOTSUPPORT);
343     }
344 
SetString(std::string value)345     void SetString(std::string value)
346     {
347         if (data_) {
348             data_->SetType(NWebValue::Type::STRING);
349             data_->SetString(value);
350         }
351     }
352 
SetNumber(double value)353     void SetNumber(double value)
354     {
355         if (data_) {
356             data_->SetType(NWebValue::Type::DOUBLE);
357             data_->SetDouble(value);
358         }
359     }
360 
SetBoolean(bool value)361     void SetBoolean(bool value)
362     {
363         if (data_) {
364             data_->SetType(NWebValue::Type::BOOLEAN);
365             data_->SetBoolean(value);
366         }
367     }
368 
SetArrayBuffer(std::vector<uint8_t> & value)369     void SetArrayBuffer(std::vector<uint8_t>& value)
370     {
371         if (data_) {
372             data_->SetType(NWebValue::Type::BINARY);
373             data_->SetBinary(value);
374         }
375     }
376 
SetStringArray(std::vector<std::string> value)377     void SetStringArray(std::vector<std::string> value)
378     {
379         if (data_) {
380             data_->SetType(NWebValue::Type::STRINGARRAY);
381             data_->SetStringArray(value);
382         }
383     }
384 
SetDoubleArray(std::vector<double> value)385     void SetDoubleArray(std::vector<double> value)
386     {
387         if (data_) {
388             data_->SetType(NWebValue::Type::DOUBLEARRAY);
389             data_->SetDoubleArray(value);
390         }
391     }
392 
SetInt64Array(std::vector<int64_t> value)393     void SetInt64Array(std::vector<int64_t> value)
394     {
395         if (data_) {
396             data_->SetType(NWebValue::Type::INT64ARRAY);
397             data_->SetInt64Array(value);
398         }
399     }
400 
SetBooleanArray(std::vector<bool> value)401     void SetBooleanArray(std::vector<bool> value)
402     {
403         if (data_) {
404             data_->SetType(NWebValue::Type::BOOLEANARRAY);
405             data_->SetBooleanArray(value);
406         }
407     }
408 
SetError(std::string name,std::string message)409     void SetError(std::string name, std::string message)
410     {
411         if (data_) {
412             data_->SetType(NWebValue::Type::ERROR);
413             data_->SetErrName(name);
414             data_->SetErrMsg(message);
415         }
416     }
417 
GetData()418     std::shared_ptr<NWebMessage> GetData()
419     {
420         return data_;
421     }
422 
423 private:
424     int type_ = 0;
425     std::shared_ptr<NWebMessage> data_;
426 };
427 
428 class WebHistoryList {
429 public:
WebHistoryList(std::shared_ptr<NWebHistoryList> sptrHistoryList)430     explicit WebHistoryList(std::shared_ptr<NWebHistoryList> sptrHistoryList) : sptrHistoryList_(sptrHistoryList) {};
431     ~WebHistoryList() = default;
432 
433     int32_t GetCurrentIndex();
434 
435     std::shared_ptr<NWebHistoryItem> GetItem(int32_t index);
436 
437     int32_t GetListSize();
438 
439 private:
440     OHOS::NWeb::NWeb* nweb_ = nullptr;
441     std::shared_ptr<NWebHistoryList> sptrHistoryList_ = nullptr;
442 };
443 } // namespace NWeb
444 } // namespace OHOS
445 
446 #endif // NWEB_WEBVIEW_CONTROLLER_H
447