• 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 <string>
22 #include <vector>
23 #include "nweb.h"
24 #include "nweb_cookie_manager.h"
25 #include "nweb_data_base.h"
26 #include "nweb_engine.h"
27 #include "nweb_export.h"
28 #include "nweb_web_storage.h"
29 
30 namespace OHOS::NWeb {
31 struct FrameRateSetting {
32     int32_t min_ {0};
33     int32_t max_ {0};
34     int32_t preferredFrameRate_ {0};
35 };
36 
37 class OHOS_NWEB_EXPORT NWebHelper {
38 public:
39     static NWebHelper &Instance();
40     ~NWebHelper();
41     bool Init(bool from_ark = true);
42     bool InitAndRun(bool from_ark = true);
43     static void TryPreReadLib(bool isFirstTimeStartUpWeb, const std::string &bundlePath);
44 
45     std::shared_ptr<NWeb> CreateNWeb(std::shared_ptr<NWebCreateInfo> create_info);
46     std::shared_ptr<NWebCookieManager> GetCookieManager();
47     std::shared_ptr<NWebDataBase> GetDataBase();
48     std::shared_ptr<NWebWebStorage> GetWebStorage();
49     std::shared_ptr<NWeb> GetNWeb(int32_t nweb_id);
50     void SetBundlePath(const std::string &path);
51     void SetHttpDns(std::shared_ptr<NWebDOHConfig> config);
52     void SetWebTag(int32_t nwebId, const char* webTag);
53     void PrepareForPageLoad(std::string url, bool preconnectable, int32_t numSockets);
54     bool LoadNWebSDK();
55     void* GetWebEngineHandler(bool shouldRun = false);
56     void SetConnectionTimeout(const int32_t& timeout);
SetCustomSchemeCmdLine(const std::string & cmd)57     void SetCustomSchemeCmdLine(const std::string& cmd)
58     {
59         customSchemeCmdLine_ = cmd;
60     }
61     void PauseAllTimers();
62     void ResumeAllTimers();
63 
64     void AddIntelligentTrackingPreventionBypassingList(
65         const std::vector<std::string>& hosts);
66     void RemoveIntelligentTrackingPreventionBypassingList(
67         const std::vector<std::string>& hosts);
68     void ClearIntelligentTrackingPreventionBypassingList();
69 
70     void PrefetchResource(const std::shared_ptr<NWebEnginePrefetchArgs> &pre_args,
71                           const std::map<std::string, std::string> &additional_http_headers,
72                           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(
81         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 private:
93     NWebHelper() = default;
94     bool LoadLib(bool from_ark);
95     void UnloadLib();
96     bool LoadEngine();
97 
98 private:
99     void *libHandleWebEngine_ = nullptr;
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