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