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