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 WARNING = 6, 97 DANGEROUS = 5 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 ScrollType : int { 120 EVENT = 0, 121 }; 122 123 enum class PressureLevel : int { 124 MEMORY_PRESSURE_LEVEL_MODERATE = 1, 125 MEMORY_PRESSURE_LEVEL_CRITICAL = 2, 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(); 137 138 void SetWebId(int32_t nwebId); 139 140 WebviewController* FromID(int32_t nwebId); 141 142 bool AccessForward(); 143 144 bool AccessBackward(); 145 146 bool AccessStep(int32_t step); 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); 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(); 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); 254 255 std::vector<uint8_t> SerializeWebState(); 256 257 bool RestoreWebState(const std::vector<uint8_t> &state); 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(); 274 275 void InnerSetHapPath(const std::string &hapPath); 276 277 bool GetCertChainDerData(std::vector<std::string> &certChainDerData); 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 void EnableSafeBrowsing(bool enable); 288 289 bool IsSafeBrowsingEnabled(); 290 291 int GetSecurityLevel(); 292 293 bool IsIncognitoMode(); 294 295 void SetPrintBackground(bool enable); 296 297 bool GetPrintBackground(); 298 299 static std::string GenerateWebTag(); 300 301 bool SetWebSchemeHandler(const char* scheme, WebSchemeHandler* handler); 302 303 int32_t ClearWebSchemeHandler(); 304 305 static bool SetWebServiveWorkerSchemeHandler( 306 const char* scheme, WebSchemeHandler* handler); 307 308 static int32_t ClearWebServiceWorkerSchemeHandler(); 309 310 std::string GetLastJavascriptProxyCallingFrameUrl(); 311 312 void EnableIntelligentTrackingPrevention(bool enable); 313 314 bool IsIntelligentTrackingPreventionEnabled(); 315 316 ErrCode StartCamera(); 317 318 ErrCode StopCamera(); 319 320 ErrCode CloseCamera(); 321 322 void CloseAllMediaPresentations(); 323 324 void StopAllMedia(); 325 326 void ResumeAllMedia(); 327 328 void PauseAllMedia(); 329 330 int GetMediaPlaybackState(); 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); 346 347 ParseURLResult ParseURLList(napi_env env, napi_value value, std::vector<std::string>& urlList); 348 349 bool CheckURL(std::string& url); 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 std::string GetSurfaceId(); 361 void EnableAdsBlock(bool enable); 362 363 bool IsAdsBlockEnabled(); 364 365 bool IsAdsBlockEnabledForCurPage(); 366 367 bool ParseJsLengthToInt(napi_env env, 368 napi_value jsLength, 369 PixelUnit& type, 370 int32_t& result); 371 372 ErrCode WebPageSnapshot(const char* id, 373 PixelUnit type, 374 int32_t width, 375 int32_t height, 376 const WebSnapshotCallback callback); 377 378 void UpdateInstanceId(int32_t newId); 379 380 void SetBackForwardCacheOptions(int32_t size, int32_t timeToLive); 381 382 void SetPathAllowingUniversalAccess(const std::vector<std::string>& pathList, 383 std::string& errorPath); 384 385 bool ScrollByWithResult(float deltaX, float deltaY); 386 387 void GetScrollOffset(float* offset_x, float* offset_y); 388 389 void ScrollToWithAnime(float x, float y, int32_t duration) ; 390 391 void ScrollByWithAnime(float deltaX, float deltaY, int32_t duration) ; 392 393 void CreatePDFCallbackExt( 394 napi_env env, std::shared_ptr<NWebPDFConfigArgs> pdfConfig, napi_ref pdfCallback); 395 396 void CreatePDFPromiseExt( 397 napi_env env, std::shared_ptr<NWebPDFConfigArgs> pdfConfig, napi_deferred deferred); 398 private: 399 int ConverToWebHitTestType(int hitType); 400 401 bool GetRawFileUrl(const std::string &fileName, 402 const std::string& bundleName, const std::string& moduleName, std::string &result); 403 404 bool ParseRawFileUrl(napi_env env, napi_value urlObj, std::string& result); 405 406 bool GetResourceUrl(napi_env env, napi_value urlObj, std::string& result); 407 408 bool ParseJsLengthResourceToInt(napi_env env, 409 napi_value jsLength, 410 PixelUnit& type, 411 int32_t& result); 412 bool GetHapModuleInfo(); 413 414 public: 415 static std::string customeSchemeCmdLine_; 416 static bool existNweb_; 417 static bool webDebuggingAccess_; 418 static std::set<std::string> webTagSet_; 419 static int32_t webTagStrId_; 420 421 private: 422 std::mutex webMtx_; 423 int32_t nwebId_ = -1; 424 std::shared_ptr<WebviewJavaScriptResultCallBack> javaScriptResultCb_ = nullptr; 425 std::string hapPath_ = ""; 426 std::string webTag_ = ""; 427 std::vector<std::string> moduleName_; 428 }; 429 430 class WebMessagePort { 431 public: 432 WebMessagePort(int32_t nwebId, std::string& port, bool isExtentionType); 433 434 ~WebMessagePort() = default; 435 436 ErrCode ClosePort(); 437 438 ErrCode PostPortMessage(std::shared_ptr<NWebMessage> data); 439 440 ErrCode SetPortMessageCallback(std::shared_ptr<NWebMessageValueCallback> callback); 441 442 std::string GetPortHandle() const; 443 IsExtentionType()444 bool IsExtentionType() 445 { 446 return isExtentionType_; 447 } 448 449 private: 450 int32_t nwebId_ = -1; 451 std::string portHandle_; 452 bool isExtentionType_; 453 }; 454 455 class WebMessageExt { 456 public: WebMessageExt(std::shared_ptr<NWebMessage> data)457 explicit WebMessageExt(std::shared_ptr<NWebMessage> data) : data_(data) {}; 458 ~WebMessageExt() = default; 459 460 void SetType(int type); 461 462 int ConvertNwebType2JsType(NWebValue::Type type); 463 GetType()464 int GetType() 465 { 466 if (data_) { 467 return ConvertNwebType2JsType(data_->GetType()); 468 } 469 return static_cast<int>(WebMessageType::NOTSUPPORT); 470 } 471 SetString(std::string value)472 void SetString(std::string value) 473 { 474 if (data_) { 475 data_->SetType(NWebValue::Type::STRING); 476 data_->SetString(value); 477 } 478 } 479 SetNumber(double value)480 void SetNumber(double value) 481 { 482 if (data_) { 483 data_->SetType(NWebValue::Type::DOUBLE); 484 data_->SetDouble(value); 485 } 486 } 487 SetBoolean(bool value)488 void SetBoolean(bool value) 489 { 490 if (data_) { 491 data_->SetType(NWebValue::Type::BOOLEAN); 492 data_->SetBoolean(value); 493 } 494 } 495 SetArrayBuffer(std::vector<uint8_t> & value)496 void SetArrayBuffer(std::vector<uint8_t>& value) 497 { 498 if (data_) { 499 data_->SetType(NWebValue::Type::BINARY); 500 data_->SetBinary(value); 501 } 502 } 503 SetStringArray(std::vector<std::string> value)504 void SetStringArray(std::vector<std::string> value) 505 { 506 if (data_) { 507 data_->SetType(NWebValue::Type::STRINGARRAY); 508 data_->SetStringArray(value); 509 } 510 } 511 SetDoubleArray(std::vector<double> value)512 void SetDoubleArray(std::vector<double> value) 513 { 514 if (data_) { 515 data_->SetType(NWebValue::Type::DOUBLEARRAY); 516 data_->SetDoubleArray(value); 517 } 518 } 519 SetInt64Array(std::vector<int64_t> value)520 void SetInt64Array(std::vector<int64_t> value) 521 { 522 if (data_) { 523 data_->SetType(NWebValue::Type::INT64ARRAY); 524 data_->SetInt64Array(value); 525 } 526 } 527 SetBooleanArray(std::vector<bool> value)528 void SetBooleanArray(std::vector<bool> value) 529 { 530 if (data_) { 531 data_->SetType(NWebValue::Type::BOOLEANARRAY); 532 data_->SetBooleanArray(value); 533 } 534 } 535 SetError(std::string name,std::string message)536 void SetError(std::string name, std::string message) 537 { 538 if (data_) { 539 data_->SetType(NWebValue::Type::ERROR); 540 data_->SetErrName(name); 541 data_->SetErrMsg(message); 542 } 543 } 544 GetData()545 std::shared_ptr<NWebMessage> GetData() const 546 { 547 return data_; 548 } 549 550 private: 551 int type_ = 0; 552 std::shared_ptr<NWebMessage> data_; 553 }; 554 555 class WebHistoryList { 556 public: WebHistoryList(std::shared_ptr<NWebHistoryList> sptrHistoryList)557 explicit WebHistoryList(std::shared_ptr<NWebHistoryList> sptrHistoryList) : sptrHistoryList_(sptrHistoryList) {}; 558 ~WebHistoryList() = default; 559 560 int32_t GetCurrentIndex(); 561 562 std::shared_ptr<NWebHistoryItem> GetItem(int32_t index); 563 564 int32_t GetListSize(); 565 566 private: 567 OHOS::NWeb::NWeb* nweb_ = nullptr; 568 std::shared_ptr<NWebHistoryList> sptrHistoryList_ = nullptr; 569 }; 570 571 class WebPrintDocument { 572 public: WebPrintDocument(void * webPrintdoc)573 explicit WebPrintDocument(void* webPrintdoc) : printDocAdapter_((PrintDocumentAdapterAdapter*)webPrintdoc) {}; 574 ~WebPrintDocument() = default; 575 void OnStartLayoutWrite(const std::string& jobId, const PrintAttributesAdapter& oldAttrs, 576 const PrintAttributesAdapter& newAttrs, uint32_t fd, 577 std::function<void(std::string, uint32_t)> writeResultCallback); 578 579 void OnJobStateChanged(const std::string& jobId, uint32_t state); 580 581 private: 582 std::unique_ptr<PrintDocumentAdapterAdapter> printDocAdapter_ = nullptr; 583 }; 584 585 class WebPrintWriteResultCallbackAdapter : public PrintWriteResultCallbackAdapter { 586 public: WebPrintWriteResultCallbackAdapter(std::function<void (std::string,uint32_t)> & cb)587 explicit WebPrintWriteResultCallbackAdapter(std::function<void(std::string, uint32_t)>& cb) : cb_(cb) {}; 588 589 void WriteResultCallback(std::string jobId, uint32_t code) override; 590 591 private: 592 std::function<void(std::string, uint32_t)> cb_; 593 }; 594 } // namespace NWeb 595 } // namespace OHOS 596 597 #endif // NWEB_WEBVIEW_CONTROLLER_H 598