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