• 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_HELPER_H
17 #define NWEB_HELPER_H
18 
19 #include <iosfwd>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include <vector>
24 
25 #include "nweb.h"
26 #include "nweb_cookie_manager.h"
27 #include "nweb_data_base.h"
28 #include "nweb_engine.h"
29 #include "nweb_export.h"
30 #include "nweb_web_storage.h"
31 #include "nweb_proxy_changed_callback.h"
32 #include "application_state_change_callback.h"
33 
34 namespace OHOS::NWeb {
35 struct NwebScheme {
36     const std::string name;
37     int32_t option = 0;
38 };
39 
40 struct FrameRateSetting {
41     int32_t min_ { 0 };
42     int32_t max_ { 0 };
43     int32_t preferredFrameRate_ { 0 };
44 };
45 
46 class WebApplicationStateChangeCallback : public AbilityRuntime::ApplicationStateChangeCallback {
47 public:
GetInstance()48     static std::shared_ptr<WebApplicationStateChangeCallback> GetInstance()
49     {
50         static std::shared_ptr<WebApplicationStateChangeCallback> instance(
51         new WebApplicationStateChangeCallback,
52         [](WebApplicationStateChangeCallback*) {}
53         );
54         return instance;
55     }
56     WebApplicationStateChangeCallback(const WebApplicationStateChangeCallback&) = delete;
57     WebApplicationStateChangeCallback& operator=(const WebApplicationStateChangeCallback&) = delete;
58     ~WebApplicationStateChangeCallback() = default;
59     void NotifyApplicationForeground() override;
60     void NotifyApplicationBackground() override;
61     std::shared_ptr<NWeb> nweb_ = nullptr;
62     bool isRegistered = false;
63 private:
64     WebApplicationStateChangeCallback() = default;
65 };
66 
67 class OHOS_NWEB_EXPORT NWebHelper {
68 public:
69     static NWebHelper& Instance();
70     ~NWebHelper() = default;
71     bool Init(bool from_ark = true);
72     bool InitAndRun(bool from_ark = true);
73     bool LoadWebEngine(bool fromArk, bool runFlag);
74     void* LoadFuncSymbol(const char* funcName);
75     static void TryPreReadLib(bool isFirstTimeStartUpWeb, const std::string& bundlePath);
76 
77     std::shared_ptr<NWeb> CreateNWeb(std::shared_ptr<NWebCreateInfo> create_info);
78     std::shared_ptr<NWebCookieManager> GetCookieManager();
79     std::shared_ptr<NWebDataBase> GetDataBase();
80     std::shared_ptr<NWebWebStorage> GetWebStorage();
81     std::shared_ptr<NWeb> GetNWeb(int32_t nweb_id);
82     void SetBundlePath(const std::string& path);
83     void SetHttpDns(std::shared_ptr<NWebDOHConfig> config);
84     void SetWebTag(int32_t nwebId, const char* webTag);
85     void PrepareForPageLoad(std::string url, bool preconnectable, int32_t numSockets);
86     bool LoadNWebSDK();
87     void SetConnectionTimeout(const int32_t& timeout);
SetCustomSchemeCmdLine(const std::string & cmd)88     void SetCustomSchemeCmdLine(const std::string& cmd)
89     {
90         customSchemeCmdLine_ = cmd;
91     }
92     void PauseAllTimers();
93     void ResumeAllTimers();
94 
95     void AddIntelligentTrackingPreventionBypassingList(const std::vector<std::string>& hosts);
96     void RemoveIntelligentTrackingPreventionBypassingList(const std::vector<std::string>& hosts);
97     void ClearIntelligentTrackingPreventionBypassingList();
98 
99     void PrefetchResource(const std::shared_ptr<NWebEnginePrefetchArgs>& pre_args,
100         const std::map<std::string, std::string>& additional_http_headers, const std::string& cache_key,
101         const uint32_t& cache_valid_time);
102 
103     std::string GetDefaultUserAgent();
104 
105     void ClearPrefetchedResource(const std::vector<std::string>& cache_key_list);
106 
107     void SetRenderProcessMode(RenderProcessMode mode);
108     RenderProcessMode GetRenderProcessMode();
109 
110     void SetHostIP(const std::string& hostName, const std::string& address, int32_t aliveTime);
111     void ClearHostIP(const std::string& hostName);
112 
113     void SetAppCustomUserAgent(const std::string& userAgent);
114 
115     void SetUserAgentForHosts(const std::string& userAgent, const std::vector<std::string>& hosts);
116 
117     void WarmupServiceWorker(const std::string& url);
118 
119     void EnableWholeWebPageDrawing();
120 
121     std::shared_ptr<NWebAdsBlockManager> GetAdsBlockManager();
122 
123     void EnableBackForwardCache(bool enableNativeEmbed, bool enableMediaTakeOver);
124 
125     void TrimMemoryByPressureLevel(int32_t memoryLevel);
126 
127     void RemoveAllCache(bool includeDiskFiles);
128 
129     void SetProxyOverride(const std::vector<std::string>& proxyUrls,
130                           const std::vector<std::string>& proxySchemeFilters,
131                           const std::vector<std::string>& bypassRules,
132                           const bool& reverseBypass,
133                           std::shared_ptr<NWebProxyChangedCallback> callback);
134 
135     void RemoveProxyOverride(std::shared_ptr<NWebProxyChangedCallback> callback);
136 
137     void SetWebDebuggingAccess(bool isEnableDebug);
138     void SetWebDebuggingAccessAndPort(bool isEnableDebug, int32_t port);
139 
140     bool HasLoadWebEngine();
141 
142     void SaveSchemeVector(const char* name, int32_t option);
143 
144     bool RegisterCustomSchemes();
145 
146     void SetBlanklessLoadingCacheCapacity(int32_t capacity);
147 
148     void ClearBlanklessLoadingCache(const std::vector<std::string>& urls);
149 
150     std::string CheckBlankOptEnable(const std::string& url, int32_t nweb_id);
151 
152     void EnablePrivateNetworkAccess(bool enable);
153 
154     bool IsPrivateNetworkAccessEnabled();
155 
156     void SetWebDestroyMode(WebDestroyMode mode);
157 
158 private:
159     NWebHelper() = default;
160     bool GetWebEngine(bool fromArk);
161     bool InitWebEngine();
162 
163 private:
164     int coreApiLevel_ = 0;
165     bool initFlag_ = false;
166     std::string bundlePath_;
167     std::string customSchemeCmdLine_;
168     std::shared_ptr<NWebEngine> nwebEngine_ = nullptr;
169     std::vector<NwebScheme> schemeVector_;
170     std::vector<std::string> backForwardCacheCmdLine_;
171     std::shared_ptr<WebApplicationStateChangeCallback> webApplicationStateCallback_;
172     mutable std::mutex lock_;
173 };
174 } // namespace OHOS::NWeb
175 
176 #endif // NWEB_HELPER_H
177