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_HELPER_H 17 #define NWEB_HELPER_H 18 19 #include <iosfwd> 20 #include <memory> 21 #include <string> 22 #include <vector> 23 24 #include "nweb.h" 25 #include "nweb_cookie_manager.h" 26 #include "nweb_data_base.h" 27 #include "nweb_engine.h" 28 #include "nweb_export.h" 29 #include "nweb_web_storage.h" 30 #include "nweb_proxy_changed_callback.h" 31 32 namespace OHOS::NWeb { 33 struct FrameRateSetting { 34 int32_t min_ { 0 }; 35 int32_t max_ { 0 }; 36 int32_t preferredFrameRate_ { 0 }; 37 }; 38 39 class OHOS_NWEB_EXPORT NWebHelper { 40 public: 41 static NWebHelper& Instance(); 42 ~NWebHelper() = default; 43 bool Init(bool from_ark = true); 44 bool InitAndRun(bool from_ark = true); 45 bool LoadWebEngine(bool fromArk, bool runFlag); 46 void* LoadFuncSymbol(const char* funcName); 47 static void TryPreReadLib(bool isFirstTimeStartUpWeb, const std::string& bundlePath); 48 49 std::shared_ptr<NWeb> CreateNWeb(std::shared_ptr<NWebCreateInfo> create_info); 50 std::shared_ptr<NWebCookieManager> GetCookieManager(); 51 std::shared_ptr<NWebDataBase> GetDataBase(); 52 std::shared_ptr<NWebWebStorage> GetWebStorage(); 53 std::shared_ptr<NWeb> GetNWeb(int32_t nweb_id); 54 void SetBundlePath(const std::string& path); 55 void SetHttpDns(std::shared_ptr<NWebDOHConfig> config); 56 void SetWebTag(int32_t nwebId, const char* webTag); 57 void PrepareForPageLoad(std::string url, bool preconnectable, int32_t numSockets); 58 bool LoadNWebSDK(); 59 void SetConnectionTimeout(const int32_t& timeout); SetCustomSchemeCmdLine(const std::string & cmd)60 void SetCustomSchemeCmdLine(const std::string& cmd) 61 { 62 customSchemeCmdLine_ = cmd; 63 } 64 void PauseAllTimers(); 65 void ResumeAllTimers(); 66 67 void AddIntelligentTrackingPreventionBypassingList(const std::vector<std::string>& hosts); 68 void RemoveIntelligentTrackingPreventionBypassingList(const std::vector<std::string>& hosts); 69 void ClearIntelligentTrackingPreventionBypassingList(); 70 std::string GetDefaultUserAgent(); 71 72 void PrefetchResource(const std::shared_ptr<NWebEnginePrefetchArgs>& pre_args, 73 const std::map<std::string, std::string>& additional_http_headers, const std::string& cache_key, 74 const uint32_t& cache_valid_time); 75 76 void ClearPrefetchedResource(const std::vector<std::string>& cache_key_list); 77 78 void SetRenderProcessMode(RenderProcessMode mode); 79 RenderProcessMode GetRenderProcessMode(); 80 81 void SetHostIP(const std::string& hostName, const std::string& address, int32_t aliveTime); 82 void ClearHostIP(const std::string& hostName); 83 84 void WarmupServiceWorker(const std::string& url); 85 86 void SetWholeWebDrawing(); 87 88 std::shared_ptr<NWebAdsBlockManager> GetAdsBlockManager(); 89 90 void EnableBackForwardCache(bool enableNativeEmbed, bool enableMediaTakeOver); 91 92 void TrimMemoryByPressureLevel(int32_t memoryLevel); 93 94 void SetProxyOverride(const std::vector<std::string>& proxyUrls, 95 const std::vector<std::string>& proxySchemeFilters, 96 const std::vector<std::string>& bypassRules, 97 const bool& reverseBypass, 98 std::shared_ptr<NWebProxyChangedCallback> callback); 99 100 void RemoveProxyOverride(std::shared_ptr<NWebProxyChangedCallback> callback); 101 102 private: 103 NWebHelper() = default; 104 bool GetWebEngine(bool fromArk); 105 bool InitWebEngine(); 106 107 private: 108 bool initFlag_ = false; 109 std::string bundlePath_; 110 std::string customSchemeCmdLine_; 111 std::shared_ptr<NWebEngine> nwebEngine_ = nullptr; 112 std::vector<std::string> backForwardCacheCmdLine_; 113 }; 114 } // namespace OHOS::NWeb 115 116 #endif // NWEB_HELPER_H 117