• 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 <filesystem>
20 #include <memory>
21 #include <string>
22 #include <unordered_map>
23 
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 #include "print_manager_adapter.h"
33 
34 #include "web_scheme_handler_request.h"
35 
36 namespace OHOS {
37 namespace NWeb {
38 enum class WebHitTestType : int {
39     EDIT = 0,
40     EMAIL,
41     HTTP,
42     HTTP_IMG,
43     IMG,
44     MAP,
45     PHONE,
46     UNKNOWN
47 };
48 
49 enum class SecureDnsModeType : int {
50     OFF = 0,
51     AUTO,
52     SECURE_ONLY
53 };
54 
55 enum class ResourceType : uint32_t {
56     COLOR = 10001,
57     FLOAT,
58     STRING,
59     PLURAL,
60     BOOLEAN,
61     INTARRAY,
62     INTEGER,
63     PATTERN,
64     STRARRAY,
65     MEDIA = 20000,
66     RAWFILE = 30000
67 };
68 
69 enum class WebMessageType : int {
70     NOTSUPPORT = 0,
71     STRING,
72     NUMBER,
73     BOOLEAN,
74     ARRAYBUFFER,
75     ARRAY,
76     ERROR
77 };
78 
79 enum class MediaPlaybackState : int {
80     NONE = 0,
81     PLAYING,
82     PAUSED,
83     STOP
84 };
85 
86 enum class SecurityLevel : int {
87     NONE = 0,
88     SECURE,
89     WARNING,
90     DANGEROUS
91 };
92 
93 enum class CoreSecurityLevel : int {
94     NONE = 0,
95     SECURE = 3,
96     DANGEROUS = 5,
97     WARNING = 6
98 };
99 
100 enum class OfflineResourceType : int {
101     IMAGE = 0,
102     CSS,
103     CLASSIC_JS,
104     MODULE_JS
105 };
106 
107 enum class ParseURLResult : int {
108     OK = 0,
109     FAILED,
110     INVALID_URL
111 };
112 
113 enum class UrlListSetResult : int {
114     INIT_ERROR = -2,
115     PARAM_ERROR = -1,
116     SET_OK = 0,
117 };
118 
119 enum class PressureLevel : int {
120     MEMORY_PRESSURE_LEVEL_MODERATE = 1,
121     MEMORY_PRESSURE_LEVEL_CRITICAL = 2,
122 };
123 
124 enum class ScrollType : int {
125     EVENT = 0,
126 };
127 
128 class WebPrintDocument;
129 class WebviewController {
130 public:
131     explicit WebviewController() = default;
132     explicit WebviewController(int32_t nwebId);
133     explicit WebviewController(const std::string& webTag);
134     ~WebviewController();
135 
136     bool IsInit() const;
137 
138     void SetWebId(int32_t nwebId);
139 
140     WebviewController* FromID(int32_t nwebId);
141 
142     bool AccessForward() const;
143 
144     bool AccessBackward() const;
145 
146     bool AccessStep(int32_t step) const;
147 
148     void ClearHistory();
149 
150     void Forward();
151 
152     void Backward();
153 
154     void OnActive();
155 
156     void OnInactive();
157 
158     void Refresh();
159 
160     ErrCode ZoomIn();
161 
162     ErrCode ZoomOut();
163 
164     int32_t GetWebId() const;
165 
166     std::string GetUserAgent();
167 
168     std::string GetCustomUserAgent() const;
169 
170     ErrCode SetCustomUserAgent(const std::string& userAgent);
171 
172     std::string GetTitle();
173 
174     int32_t GetPageHeight();
175 
176     ErrCode BackOrForward(int32_t step);
177 
178     void StoreWebArchiveCallback(const std::string &baseName, bool autoName, napi_env env, napi_ref jsCallback);
179 
180     void StoreWebArchivePromise(const std::string &baseName, bool autoName, napi_env env, napi_deferred deferred);
181 
182     std::vector<std::string> CreateWebMessagePorts();
183 
184     ErrCode PostWebMessage(std::string& message, std::vector<std::string>& ports, std::string& targetUrl);
185 
186     std::shared_ptr<HitTestResult> GetHitTestValue();
187 
188     void RequestFocus();
189 
190     bool ParseUrl(napi_env env, napi_value urlObj, std::string& result) const;
191 
192     ErrCode LoadUrl(std::string url);
193 
194     ErrCode LoadUrl(std::string url, std::map<std::string, std::string> httpHeaders);
195 
196     ErrCode LoadData(std::string data, std::string mimeType, std::string encoding,
197         std::string baseUrl, std::string historyUrl);
198 
199     int GetHitTest();
200 
201     void ClearMatches();
202 
203     void SearchNext(bool forward);
204 
205     void SearchAllAsync(const std::string& searchString);
206 
207     void ClearSslCache();
208 
209     void ClearClientAuthenticationCache();
210 
211     void Stop();
212 
213     ErrCode Zoom(float factor);
214 
215     void InnerCompleteWindowNew(int32_t parentNwebId);
216 
217     void SetNWebJavaScriptResultCallBack();
218 
219     void RegisterJavaScriptProxy(RegisterJavaScriptProxyParam& param);
220 
221     ErrCode DeleteJavaScriptRegister(const std::string& objName,
222         const std::vector<std::string>& methodList);
223 
224     void RunJavaScriptCallback(const std::string &script, napi_env env, napi_ref jsCallback, bool extention);
225 
226     void RunJavaScriptPromise(const std::string &script, napi_env env, napi_deferred deferred, bool extention);
227 
228     void RunJavaScriptCallbackExt(
229         const int fd, const size_t scriptLength, napi_env env, napi_ref jsCallback, bool extention);
230 
231     void RunJavaScriptPromiseExt(
232         const int fd, const size_t scriptLength, napi_env env, napi_deferred deferred, bool extention);
233 
234     std::string GetUrl();
235 
236     std::string GetOriginalUrl();
237 
238     bool TerminateRenderProcess() const;
239 
240     void PutNetworkAvailable(bool available);
241 
242     bool HasImage(std::shared_ptr<NWebBoolValueCallback> callback);
243 
244     ErrCode HasImagesCallback(napi_env env, napi_ref jsCallback);
245 
246     ErrCode HasImagesPromise(napi_env env, napi_deferred deferred);
247 
248     void RemoveCache(bool includeDiskFiles);
249 
250     std::shared_ptr<NWebHistoryList> GetHistoryList();
251 
252     bool GetFavicon(
253         const void **data, size_t &width, size_t &height, ImageColorType &colorType, ImageAlphaType &alphaType) const;
254 
255     std::vector<uint8_t> SerializeWebState();
256 
257     bool RestoreWebState(const std::vector<uint8_t> &state) const;
258 
259     void ScrollPageDown(bool bottom);
260 
261     void ScrollPageUp(bool top);
262 
263     void ScrollTo(float x, float y);
264 
265     void ScrollBy(float deltaX, float deltaY);
266 
267     void SlideScroll(float vx, float vy);
268 
269     void SetScrollable(bool enable);
270 
271     void SetScrollable(bool enable, int32_t scrollType);
272 
273     bool GetScrollable() const;
274 
275     void InnerSetHapPath(const std::string &hapPath);
276 
277     bool GetCertChainDerData(std::vector<std::string> &certChainDerData) const;
278 
279     ErrCode SetAudioMuted(bool muted);
280 
281     ErrCode PrefetchPage(std::string& url, std::map<std::string, std::string> additionalHttpHeaders);
282 
283     void* CreateWebPrintDocumentAdapter(const std::string &jobName);
284 
285     ErrCode PostUrl(std::string& url, std::vector<char>& postData);
286 
287     int GetSecurityLevel();
288 
289     void EnableSafeBrowsing(bool enable);
290 
291     bool IsSafeBrowsingEnabled() const;
292 
293     bool IsIncognitoMode() const;
294 
295     void SetPrintBackground(bool enable);
296 
297     bool GetPrintBackground() const;
298 
299     std::string GetLastJavascriptProxyCallingFrameUrl();
300 
301     static std::string GenerateWebTag();
302 
303     bool SetWebSchemeHandler(const char* scheme, WebSchemeHandler* handler) const;
304 
305     int32_t ClearWebSchemeHandler();
306 
307     static bool SetWebServiveWorkerSchemeHandler(
308         const char* scheme, WebSchemeHandler* handler);
309 
310     static int32_t ClearWebServiceWorkerSchemeHandler();
311 
312     void CloseAllMediaPresentations();
313 
314     void StopAllMedia();
315 
316     void ResumeAllMedia();
317 
318     void PauseAllMedia();
319 
320     int GetMediaPlaybackState();
321 
322     void EnableIntelligentTrackingPrevention(bool enable);
323 
324     bool IsIntelligentTrackingPreventionEnabled() const;
325 
326     ErrCode StartCamera();
327 
328     ErrCode StopCamera();
329 
330     ErrCode CloseCamera();
331 
332     void OnCreateNativeMediaPlayer(napi_env env, napi_ref callback);
333 
334     bool ParseScriptContent(napi_env env, napi_value value, std::string &script);
335 
336     std::shared_ptr<CacheOptions> ParseCacheOptions(napi_env env, napi_value value);
337 
338     void PrecompileJavaScriptPromise(napi_env env,
339                                      napi_deferred deferred,
340                                      const std::string &url, const std::string &script,
341                                      std::shared_ptr<CacheOptions> cacheOptions);
342 
343     bool ParseResponseHeaders(napi_env env,
344                               napi_value value,
345                               std::map<std::string, std::string> &responseHeaders) const;
346 
347     ParseURLResult ParseURLList(napi_env env, napi_value value, std::vector<std::string>& urlList);
348 
349     bool CheckURL(std::string& url) const;
350 
351     std::vector<uint8_t> ParseUint8Array(napi_env env, napi_value value);
352 
353     void InjectOfflineResource(const std::vector<std::string>& urlList,
354                                const std::vector<uint8_t>& resource,
355                                const std::map<std::string, std::string>& response_headers,
356                                const uint32_t type);
357 
358     ErrCode SetUrlTrustList(const std::string& urlTrustList, std::string& detailErrMsg);
359 
360     void EnableAdsBlock(bool enable);
361 
362     bool IsAdsBlockEnabled() const;
363 
364     bool IsAdsBlockEnabledForCurPage() const;
365 
366     std::string GetSurfaceId();
367 
368     void UpdateInstanceId(int32_t newId);
369 
370     bool ParseJsLengthToInt(napi_env env,
371                             napi_value jsLength,
372                             PixelUnit& type,
373                             int32_t& result) const;
374 
375     ErrCode WebPageSnapshot(const char* id,
376                             PixelUnit type,
377                             int32_t width,
378                             int32_t height,
379                             const WebSnapshotCallback callback);
380 
381     void SetPathAllowingUniversalAccess(const std::vector<std::string>& pathList,
382                                         std::string& errorPath);
383 
384     void ScrollToWithAnime(float x, float y, int32_t duration) ;
385 
386     void ScrollByWithAnime(float deltaX, float deltaY, int32_t duration) ;
387 
388     void SetBackForwardCacheOptions(int32_t size, int32_t timeToLive);
389 
390     void GetScrollOffset(float* offset_x, float* offset_y);
391 
392     void CreatePDFCallbackExt(
393         napi_env env, std::shared_ptr<NWebPDFConfigArgs> pdfConfig, napi_ref pdfCallback);
394 
395     void CreatePDFPromiseExt(
396         napi_env env, std::shared_ptr<NWebPDFConfigArgs> pdfConfig, napi_deferred deferred);
397 
398     bool ScrollByWithResult(float deltaX, float deltaY) const;
399 
400     std::shared_ptr<HitTestResult> GetLastHitTest();
401 private:
402     int ConverToWebHitTestType(int hitType);
403 
404     bool GetRawFileUrl(const std::string &fileName,
405         const std::string& bundleName, const std::string& moduleName, std::string &result) const;
406 
407     bool ParseRawFileUrl(napi_env env, napi_value urlObj, std::string& result) const;
408 
409     bool GetResourceUrl(napi_env env, napi_value urlObj, std::string& result) const;
410 
411     bool ParseJsLengthResourceToInt(napi_env env,
412                                     napi_value jsLength,
413                                     PixelUnit& type,
414                                     int32_t& result) const;
415     bool GetHapModuleInfo();
416 
417 public:
418     static std::string customeSchemeCmdLine_;
419     static bool existNweb_;
420     static bool webDebuggingAccess_;
421     static std::set<std::string> webTagSet_;
422     static int32_t webTagStrId_;
423 
424 private:
425     std::mutex webMtx_;
426     int32_t nwebId_ = -1;
427     std::shared_ptr<WebviewJavaScriptResultCallBack> javaScriptResultCb_ = nullptr;
428     std::string hapPath_ = "";
429     std::string webTag_ = "";
430     std::vector<std::string> moduleName_;
431 };
432 
433 class WebMessagePort {
434 public:
435     WebMessagePort(int32_t nwebId, std::string& port, bool isExtentionType);
436 
437     ~WebMessagePort() = default;
438 
439     ErrCode ClosePort();
440 
441     ErrCode PostPortMessage(std::shared_ptr<NWebMessage> data);
442 
443     ErrCode SetPortMessageCallback(std::shared_ptr<NWebMessageValueCallback> callback);
444 
445     std::string GetPortHandle() const;
446 
IsExtentionType()447     bool IsExtentionType()
448     {
449         return isExtentionType_;
450     }
451 
452 private:
453     int32_t nwebId_ = -1;
454     std::string portHandle_;
455     bool isExtentionType_;
456 };
457 
458 class WebMessageExt {
459 public:
WebMessageExt(std::shared_ptr<NWebMessage> data)460     explicit WebMessageExt(std::shared_ptr<NWebMessage> data) : data_(data) {};
461     ~WebMessageExt() = default;
462 
463     void SetType(int type);
464 
465     int ConvertNwebType2JsType(NWebValue::Type type);
466 
GetType()467     int GetType()
468     {
469         if (data_) {
470             return ConvertNwebType2JsType(data_->GetType());
471         }
472         return static_cast<int>(WebMessageType::NOTSUPPORT);
473     }
474 
SetString(std::string value)475     void SetString(std::string value)
476     {
477         if (data_) {
478             data_->SetType(NWebValue::Type::STRING);
479             data_->SetString(value);
480         }
481     }
482 
SetNumber(double value)483     void SetNumber(double value)
484     {
485         if (data_) {
486             data_->SetType(NWebValue::Type::DOUBLE);
487             data_->SetDouble(value);
488         }
489     }
490 
SetBoolean(bool value)491     void SetBoolean(bool value)
492     {
493         if (data_) {
494             data_->SetType(NWebValue::Type::BOOLEAN);
495             data_->SetBoolean(value);
496         }
497     }
498 
SetArrayBuffer(std::vector<uint8_t> & value)499     void SetArrayBuffer(std::vector<uint8_t>& value)
500     {
501         if (data_) {
502             data_->SetType(NWebValue::Type::BINARY);
503             data_->SetBinary(value);
504         }
505     }
506 
SetStringArray(std::vector<std::string> value)507     void SetStringArray(std::vector<std::string> value)
508     {
509         if (data_) {
510             data_->SetType(NWebValue::Type::STRINGARRAY);
511             data_->SetStringArray(value);
512         }
513     }
514 
SetDoubleArray(std::vector<double> value)515     void SetDoubleArray(std::vector<double> value)
516     {
517         if (data_) {
518             data_->SetType(NWebValue::Type::DOUBLEARRAY);
519             data_->SetDoubleArray(value);
520         }
521     }
522 
SetInt64Array(std::vector<int64_t> value)523     void SetInt64Array(std::vector<int64_t> value)
524     {
525         if (data_) {
526             data_->SetType(NWebValue::Type::INT64ARRAY);
527             data_->SetInt64Array(value);
528         }
529     }
530 
SetBooleanArray(std::vector<bool> value)531     void SetBooleanArray(std::vector<bool> value)
532     {
533         if (data_) {
534             data_->SetType(NWebValue::Type::BOOLEANARRAY);
535             data_->SetBooleanArray(value);
536         }
537     }
538 
SetError(std::string name,std::string message)539     void SetError(std::string name, std::string message)
540     {
541         if (data_) {
542             data_->SetType(NWebValue::Type::ERROR);
543             data_->SetErrName(name);
544             data_->SetErrMsg(message);
545         }
546     }
547 
GetData()548     std::shared_ptr<NWebMessage> GetData() const
549     {
550         return data_;
551     }
552 
553 private:
554     int type_ = 0;
555     std::shared_ptr<NWebMessage> data_;
556 };
557 
558 class WebHistoryList {
559 public:
WebHistoryList(std::shared_ptr<NWebHistoryList> sptrHistoryList)560     explicit WebHistoryList(std::shared_ptr<NWebHistoryList> sptrHistoryList) : sptrHistoryList_(sptrHistoryList) {};
561     ~WebHistoryList() = default;
562 
563     int32_t GetCurrentIndex();
564 
565     std::shared_ptr<NWebHistoryItem> GetItem(int32_t index);
566 
567     int32_t GetListSize();
568 
569 private:
570     OHOS::NWeb::NWeb* nweb_ = nullptr;
571     std::shared_ptr<NWebHistoryList> sptrHistoryList_ = nullptr;
572 };
573 
574 class WebPrintDocument {
575 public:
WebPrintDocument(void * webPrintdoc)576     explicit WebPrintDocument(void* webPrintdoc) : printDocAdapter_((PrintDocumentAdapterAdapter*)webPrintdoc) {};
577     ~WebPrintDocument() = default;
578     void OnStartLayoutWrite(const std::string& jobId, const PrintAttributesAdapter& oldAttrs,
579         const PrintAttributesAdapter& newAttrs, uint32_t fd,
580         std::function<void(std::string, uint32_t)> writeResultCallback);
581 
582     void OnJobStateChanged(const std::string& jobId, uint32_t state);
583 
584 private:
585     std::unique_ptr<PrintDocumentAdapterAdapter> printDocAdapter_ = nullptr;
586 };
587 
588 class WebPrintWriteResultCallbackAdapter : public PrintWriteResultCallbackAdapter {
589 public:
WebPrintWriteResultCallbackAdapter(std::function<void (std::string,uint32_t)> & cb)590     explicit WebPrintWriteResultCallbackAdapter(std::function<void(std::string, uint32_t)>& cb) : cb_(cb) {};
591 
592     void WriteResultCallback(std::string jobId, uint32_t code) override;
593 
594 private:
595     std::function<void(std::string, uint32_t)> cb_;
596 };
597 } // namespace NWeb
598 } // namespace OHOS
599 
600 #endif // NWEB_WEBVIEW_CONTROLLER_H
601