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