• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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 
17 #include <cstdint>
18 #include <dirent.h>
19 #include <dlfcn.h>
20 #include <memory>
21 #include <sys/prctl.h>
22 #include <sys/stat.h>
23 #include <thread>
24 #include <unistd.h>
25 #include <fcntl.h>
26 
27 #include "application_context.h"
28 #include "bundle_mgr_interface.h"
29 #include "config_policy_utils.h"
30 #include "iservice_registry.h"
31 #include "nweb_config_helper.h"
32 #include "nweb_log.h"
33 #include "parameters.h"
34 #include "system_ability_definition.h"
35 
36 namespace {
37 const std::string WEB_CONFIG_PATH = "etc/web/web_config.xml";
38 const std::string INIT_CONFIG = "initConfig";
39 const std::string DELETE_CONFIG = "deleteArgsConfig";
40 const std::string PERFORMANCE_CONFIG = "performanceConfig";
41 // The config used in base/web/webview
42 const std::string BASE_WEB_CONFIG = "baseWebConfig";
43 const std::string WEB_ANIMATION_DYNAMIC_SETTING_CONFIG = "property_animation_dynamic_settings";
44 const std::string WEB_ANIMATION_DYNAMIC_APP = "dynamic_apps";
45 const std::string WEB_LTPO_STRATEGY = "ltpo_strategy";
46 const std::string WEB_WINDOW_ORIENTATION_CONFIG = "window_orientation_config";
47 const std::string WEB_ALL_BUNDLE_NAME = "*";
48 const auto XML_ATTR_NAME = "name";
49 const auto XML_ATTR_MIN = "min";
50 const auto XML_ATTR_MAX = "max";
51 const auto XML_ATTR_FPS = "preferred_fps";
52 const auto XML_BUNDLE_NAME = "bundle_name";
53 const auto XML_ENABLE_WINDOW_ORIENTATION = "enable_window_orientation";
54 const std::unordered_map<std::string_view, std::function<std::string(std::string&)>> configMap = {
55     { "renderConfig/renderProcessCount",
__anond0e5f3e50202() 56         [](std::string& contentStr) { return std::string("--renderer-process-limit=") + contentStr; } },
57     { "mediaConfig/backgroundMediaShouldSuspend",
__anond0e5f3e50302() 58         [](std::string& contentStr) {
59             return contentStr == "false" ? std::string("--disable-background-media-suspend") : std::string();
60         } },
61     { "loadurlSocPerfConfig/loadurlSocPerfParam",
__anond0e5f3e50402() 62         [](std::string& contentStr) {
63             return contentStr == "true" ? std::string("--ohos-enable-loadurl-soc-perf") : std::string();
64         } },
65     { "mouseWheelSocPerfConfig/mouseWheelSocPerfParam",
__anond0e5f3e50502() 66         [](std::string& contentStr) {
67             return contentStr == "true" ? std::string("--ohos-enable-mousewheel-soc-perf") : std::string();
68         } },
69     { "touchEventConfig/touchEventShouldRegister",
__anond0e5f3e50602() 70         [](std::string& contentStr) {
71             return contentStr == "false" ? std::string("--disable-touch-event-register") : std::string();
72         } },
73     { "settingConfig/enableWaitForUsername",
__anond0e5f3e50702() 74         [](std::string& contentStr) {
75             return contentStr == "true" ? std::string("--ohos-enable-wait-for-username") : std::string();
76         } },
77     { "settingConfig/enableMaxNumberOfSavedFrames",
__anond0e5f3e50802() 78         [](std::string& contentStr) {
79             return contentStr == "true" ? std::string("--ohos-enable-max-number-of-saved-frames") : std::string();
80         } },
81     { "settingConfig/enableNumRasterThreads",
__anond0e5f3e50902() 82         [](std::string& contentStr) {
83             return contentStr == "true" ? std::string("--ohos-enable-num-raster-threads") : std::string();
84         } },
85     { "settingConfig/enableSingleRenderProcess",
__anond0e5f3e50a02() 86         [](std::string& contentStr) {
87             return contentStr == "true" ? std::string("--ohos-enable-single-render-process") : std::string();
88         } },
89     { "userAgentConfig/userAgentValue",
__anond0e5f3e50b02() 90         [](std::string& contentStr) { return std::string("--ohos-user-agent-value=") + contentStr; } },
91     { "settingConfig/enableSimpleBackendIsDefault",
__anond0e5f3e50c02() 92         [](std::string& contentStr) {
93             return contentStr == "true" ? std::string("--ohos-enable-simple-backend-is-default") : std::string();
94         } },
95     { "settingConfig/enableEmbedMode",
__anond0e5f3e50d02() 96         [](std::string& contentStr) {
97             return contentStr == "true" ? std::string("--ohos-enable-embed-mode") : std::string();
98         } },
99     { "settingConfig/enableWebViewImplForLargeScreen",
__anond0e5f3e50e02() 100         [](std::string& contentStr) {
101             return contentStr == "true" ? std::string("--ohos-enable-web-view-impl-for-large-screen") : std::string();
102         } },
103     { "settingConfig/enableDeleteUnusedResourcesDelay",
__anond0e5f3e50f02() 104         [](std::string& contentStr) {
105             return contentStr == "true" ? std::string("--ohos-enable-delete-unused-resources-delay") : std::string();
106         } },
107     { "settingConfig/enableSetHttpCacheMaxSize",
__anond0e5f3e51002() 108         [](std::string& contentStr) {
109             return contentStr == "true" ? std::string("--ohos-enable-set-http-cache-max-size") : std::string();
110         } },
111     { "settingConfig/enableCookieConfigPersistSession",
__anond0e5f3e51102() 112         [](std::string& contentStr) {
113             return contentStr == "true" ? std::string("--ohos-enable-cookie-config-persist-session") : std::string();
114         } },
115     { "settingConfig/enableDoubleTapForPlatform",
__anond0e5f3e51202() 116         [](std::string& contentStr) {
117             return contentStr == "true" ? std::string("--ohos-enable-double-tap-for-platform") : std::string();
118         } },
119     { "settingConfig/enableIgnoreLockdownMode",
__anond0e5f3e51302() 120         [](std::string& contentStr) {
121             return contentStr == "true" ? std::string("--ohos-enable-Ignore-lockdown-mode") : std::string();
122         } },
123     { "settingConfig/enablePrinting",
__anond0e5f3e51402() 124         [](std::string& contentStr) {
125             return contentStr == "true" ? std::string("--ohos-enable-printing") : std::string();
126         } },
127     { "settingConfig/enableHttpCacheSimple",
__anond0e5f3e51502() 128         [](std::string& contentStr) {
129             return contentStr == "true" ? std::string("--ohos-enable-http-cache-simple") : std::string();
130         } },
131     { "settingConfig/enableCalcTabletMode",
__anond0e5f3e51602() 132         [](std::string& contentStr) {
133             return contentStr == "true" ? std::string("--ohos-enable-calc-tablet-mode") : std::string();
134         } },
135     { "settingConfig/disableMobileStyleSheet",
__anond0e5f3e51702() 136         [](std::string& contentStr) {
137             return contentStr == "true" ? std::string("--ohos-disable-mobile-style-sheet") : std::string();
138         } },
139     { "outOfProcessGPUConfig/enableOopGpu",
__anond0e5f3e51802() 140         [](std::string& contentStr) {
141             return contentStr == "true" ? std::string("--in-process-gpu") : std::string();
142         } },
143     { "enableVulkanConfig/enableVulkan",
__anond0e5f3e51902() 144         [](std::string& contentStr) {
145             return contentStr == "true" ? std::string("--ohos-enable-vulkan") : std::string();
146         } },
147     { "disableDrdcConfig/disableDrdc",
__anond0e5f3e51a02() 148         [](std::string& contentStr) {
149             return contentStr == "true" ? std::string("--ohos-enable-drdc") : std::string();
150         } }
151 };
152 } // namespace
153 
154 namespace OHOS::NWeb {
NWebConfigHelper()155 NWebConfigHelper::NWebConfigHelper()
156 {
157     auto saManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
158     bool hasPlayGround = false;
159     bool isDebugApp = false;
160     bool isDeveloperMode = IsDeveloperModeEnabled();
161     if (saManager == nullptr) {
162         WVLOG_E("webPlayGround get saManager fail");
163         return;
164     }
165 
166     sptr<IRemoteObject> remoteObject =
167         saManager->GetSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
168     sptr<OHOS::AppExecFwk::IBundleMgr> bundleMgrProxy =
169         OHOS::iface_cast<OHOS::AppExecFwk::IBundleMgr>(remoteObject);
170     if (bundleMgrProxy == nullptr) {
171         WVLOG_E("webPlayGround get bundleMgrProxy fail");
172         return;
173     }
174 
175     OHOS::AppExecFwk::BundleInfo bundleInfo;
176     bundleMgrProxy->GetBundleInfoForSelf(
177         OHOS::AppExecFwk::BundleFlag::GET_BUNDLE_WITH_ABILITIES, bundleInfo);
178     isDebugApp = (bundleInfo.applicationInfo.appProvisionType ==
179                     AppExecFwk::Constants::APP_PROVISION_TYPE_DEBUG);
180     for (auto appEnv : bundleInfo.applicationInfo.appEnvironments) {
181         if (appEnv.name == PLAYGROUND && appEnv.value == "true") {
182             hasPlayGround = true;
183             break;
184         }
185     }
186     web_play_ground_enabled_ = isDebugApp && hasPlayGround && isDeveloperMode;
187 
188     if (web_play_ground_enabled_) {
189         #define XPM_KICKER (0x6a6974)
190         prctl(XPM_KICKER, 0, 0);
191         WVLOG_I("webPlayGround opened");
192     } else {
193         if (hasPlayGround) {
194             WVLOG_I("webPlayGround not opened for isDebugApp %{public}d"
195                     " hasPlayGround %{public}d isDeveloperMode %{public}d",
196                     isDebugApp, hasPlayGround, isDeveloperMode);
197         }
198     }
199 }
200 
IsWebPlayGroundEnable()201 bool NWebConfigHelper::IsWebPlayGroundEnable()
202 {
203     return web_play_ground_enabled_;
204 }
205 
GetWebPlayGroundInitArg()206 const std::string& NWebConfigHelper::GetWebPlayGroundInitArg()
207 {
208     return web_play_ground_enabled_ ? SINGLE_PROCESS : NULL_STR;
209 }
210 
GetWebPlayGroundHapPath()211 const std::string& NWebConfigHelper::GetWebPlayGroundHapPath()
212 {
213     return web_play_ground_enabled_ ? PLAY_GROUND_HAP_PATH : NULL_STR;
214 }
215 
IsDeveloperModeEnabled()216 bool NWebConfigHelper::IsDeveloperModeEnabled()
217 {
218     return OHOS::system::GetBoolParameter("const.security.developermode.state", false);
219 }
220 
Instance()221 NWebConfigHelper &NWebConfigHelper::Instance()
222 {
223     static NWebConfigHelper helper;
224     return helper;
225 }
226 
IsPerfConfigEmpty()227 bool NWebConfigHelper::IsPerfConfigEmpty()
228 {
229     std::lock_guard<std::mutex> lock(lock_);
230     return perfConfig_.empty();
231 }
232 
ReadConfigIfNeeded()233 void NWebConfigHelper::ReadConfigIfNeeded()
234 {
235     if (IsPerfConfigEmpty()) {
236         std::shared_ptr<NWebEngineInitArgsImpl> initArgs = std::make_shared<NWebEngineInitArgsImpl>();
237         NWebConfigHelper::Instance().ParseConfig(initArgs);
238     }
239 }
240 
GetConfigPath(const std::string & configFileName)241 std::string NWebConfigHelper::GetConfigPath(const std::string &configFileName)
242 {
243     char buf[PATH_MAX + 1];
244     char *configPath = GetOneCfgFile(configFileName.c_str(), buf, PATH_MAX + 1);
245     char tmpPath[PATH_MAX + 1] = { 0 };
246     if (!configPath || strlen(configPath) == 0 || strlen(configPath) > PATH_MAX || !realpath(configPath, tmpPath)) {
247         WVLOG_I("can not get customization config file");
248         return "/system/" + configFileName;
249     }
250     return std::string(tmpPath);
251 }
252 
ReadConfig(const xmlNodePtr & rootPtr,std::shared_ptr<NWebEngineInitArgsImpl> initArgs)253 void NWebConfigHelper::ReadConfig(const xmlNodePtr &rootPtr, std::shared_ptr<NWebEngineInitArgsImpl> initArgs)
254 {
255     for (xmlNodePtr curNodePtr = rootPtr->xmlChildrenNode; curNodePtr != nullptr; curNodePtr = curNodePtr->next) {
256         if (curNodePtr->name == nullptr || curNodePtr->type == XML_COMMENT_NODE) {
257             WVLOG_E("invalid node!");
258             continue;
259         }
260         std::string nodeName = reinterpret_cast<const char *>(curNodePtr->name);
261         for (xmlNodePtr curChildNodePtr = curNodePtr->xmlChildrenNode; curChildNodePtr != nullptr;
262             curChildNodePtr = curChildNodePtr->next) {
263             if (curChildNodePtr->name == nullptr || curChildNodePtr->type == XML_COMMENT_NODE) {
264                 WVLOG_E("invalid node!");
265                 continue;
266             }
267             std::string childNodeName = reinterpret_cast<const char *>(curChildNodePtr->name);
268             xmlChar *content = xmlNodeGetContent(curChildNodePtr);
269             if (content == nullptr) {
270                 WVLOG_E("read xml node error: nodeName:(%{public}s)", curChildNodePtr->name);
271                 continue;
272             }
273             std::string contentStr = reinterpret_cast<const char *>(content);
274             xmlFree(content);
275             auto it = configMap.find(nodeName + "/" + childNodeName);
276             if (it == configMap.end()) {
277                 WVLOG_W("not found for web_config: %{public}s/%{public}s", nodeName.c_str(), childNodeName.c_str());
278                 continue;
279             }
280             std::string param = it->second(contentStr);
281             if (!param.empty()) {
282                 initArgs->AddArg(param);
283             }
284         }
285     }
286 }
287 
GetChildrenNode(xmlNodePtr NodePtr,const std::string & childrenNodeName)288 xmlNodePtr NWebConfigHelper::GetChildrenNode(xmlNodePtr NodePtr, const std::string &childrenNodeName)
289 {
290     WVLOG_D("GetChildrenNode:(%{public}s)", childrenNodeName.c_str());
291     for (xmlNodePtr curNodePtr = NodePtr->xmlChildrenNode; curNodePtr != nullptr; curNodePtr = curNodePtr->next) {
292         if (curNodePtr->name == nullptr || curNodePtr->type == XML_COMMENT_NODE) {
293             WVLOG_E("invalid node!");
294             continue;
295         }
296         if (!xmlStrcmp(curNodePtr->name, reinterpret_cast<const xmlChar*>(childrenNodeName.c_str()))) {
297             return curNodePtr;
298         }
299     }
300     return nullptr;
301 }
302 
ParseConfig(std::shared_ptr<NWebEngineInitArgsImpl> initArgs)303 void NWebConfigHelper::ParseConfig(std::shared_ptr<NWebEngineInitArgsImpl> initArgs)
304 {
305     CfgFiles* cfgFiles = GetCfgFiles(WEB_CONFIG_PATH.c_str());
306     std::string defaultConfigPath = "/system/" + WEB_CONFIG_PATH;
307     if (cfgFiles == nullptr) {
308         WVLOG_E("Not found webConfigxml,read system config");
309         ParseWebConfigXml(defaultConfigPath, initArgs);
310         return;
311     }
312 
313     // When i is 0 ,it means /system/ + WEB_CONFIG_PATH, ignore
314     for (int32_t i = 1; i < MAX_CFG_POLICY_DIRS_CNT; i++) {
315         auto cfgFilePath = cfgFiles->paths[i];
316         if (!cfgFilePath || *(cfgFilePath) == '\0') {
317             break;
318         }
319         WVLOG_D("web config file path:%{public}s", cfgFilePath);
320         if (!cfgFilePath || strlen(cfgFilePath) == 0 || strlen(cfgFilePath) > PATH_MAX) {
321             WVLOG_W("can not get customization config file");
322             ParseWebConfigXml(defaultConfigPath, initArgs);
323             continue;
324         }
325         ParseWebConfigXml(cfgFiles->paths[i], initArgs);
326     }
327     FreeCfgFiles(cfgFiles);
328 }
329 
ParseWebConfigXml(const std::string & configFilePath,std::shared_ptr<NWebEngineInitArgsImpl> initArgs)330 void NWebConfigHelper::ParseWebConfigXml(const std::string& configFilePath,
331     std::shared_ptr<NWebEngineInitArgsImpl> initArgs)
332 {
333     xmlDocPtr docPtr = xmlReadFile(configFilePath.c_str(), nullptr, XML_PARSE_NOBLANKS);
334     if (docPtr == nullptr) {
335         WVLOG_E("load xml error!");
336         return;
337     }
338 
339     xmlNodePtr rootPtr = xmlDocGetRootElement(docPtr);
340     if (rootPtr == nullptr || rootPtr->name == nullptr ||
341         xmlStrcmp(rootPtr->name, reinterpret_cast<const xmlChar *>("WEB"))) {
342         WVLOG_E("get root element failed!");
343         xmlFreeDoc(docPtr);
344         return;
345     }
346 
347     xmlNodePtr initNodePtr = GetChildrenNode(rootPtr, INIT_CONFIG);
348     if (initNodePtr != nullptr) {
349         WVLOG_D("read config from init node");
350         ReadConfig(initNodePtr, initArgs);
351     } else {
352         WVLOG_D("read config from root node");
353         ReadConfig(rootPtr, initArgs);
354     }
355 
356     xmlNodePtr deleteNodePtr = GetChildrenNode(rootPtr, DELETE_CONFIG);
357     if (deleteNodePtr != nullptr) {
358         WVLOG_D("read config from delete node");
359         ParseDeleteConfig(deleteNodePtr, initArgs);
360     }
361 
362     if (IsPerfConfigEmpty()) {
363         xmlNodePtr perfNodePtr = GetChildrenNode(rootPtr, PERFORMANCE_CONFIG);
364         if (perfNodePtr != nullptr) {
365             ParsePerfConfig(perfNodePtr);
366         }
367         xmlNodePtr adapterNodePtr = GetChildrenNode(rootPtr, BASE_WEB_CONFIG);
368         if (adapterNodePtr != nullptr) {
369             ParsePerfConfig(adapterNodePtr);
370         }
371     }
372 
373     if (ltpoConfig_.empty() && ltpoStrategy_ == 0) {
374         xmlNodePtr ltpoConfigNodePtr = GetChildrenNode(rootPtr, WEB_ANIMATION_DYNAMIC_SETTING_CONFIG);
375         if (ltpoConfigNodePtr != nullptr) {
376             ParseNWebLTPOConfig(ltpoConfigNodePtr);
377         }
378     }
379 
380     xmlNodePtr windowOrientationNodePtr = GetChildrenNode(rootPtr, WEB_WINDOW_ORIENTATION_CONFIG);
381     if (windowOrientationNodePtr != nullptr) {
382         WVLOG_D("read config from window orientation node");
383         ParseWindowOrientationConfig(windowOrientationNodePtr, initArgs);
384     }
385     xmlFreeDoc(docPtr);
386 }
387 
ParseNWebLTPOConfig(xmlNodePtr nodePtr)388 void NWebConfigHelper::ParseNWebLTPOConfig(xmlNodePtr nodePtr)
389 {
390     for (xmlNodePtr curNodePtr = nodePtr->xmlChildrenNode; curNodePtr; curNodePtr = curNodePtr->next) {
391         if (curNodePtr->name == nullptr || curNodePtr->type == XML_COMMENT_NODE) {
392             WVLOG_E("invalid node!");
393             continue;
394         }
395         char* namePtr = (char *)xmlGetProp(curNodePtr, BAD_CAST(XML_ATTR_NAME));
396         if (!namePtr) {
397             WVLOG_E("invalid name!");
398             continue;
399         }
400         std::string settingName(namePtr);
401         xmlFree(namePtr);
402         if (settingName == WEB_ANIMATION_DYNAMIC_APP) {
403             ParseNWebLTPOApp(curNodePtr);
404             continue;
405         }
406         if (settingName == WEB_LTPO_STRATEGY) {
407             ParseNWebLTPOStrategy(curNodePtr);
408             continue;
409         }
410         std::vector<FrameRateSetting> frameRateSetting;
411         for (xmlNodePtr curDynamicNodePtr = curNodePtr->xmlChildrenNode; curDynamicNodePtr;
412             curDynamicNodePtr = curDynamicNodePtr->next) {
413             if (curDynamicNodePtr->name == nullptr || curDynamicNodePtr->type == XML_COMMENT_NODE) {
414                 WVLOG_E("invalid node!");
415                 continue;
416             }
417             FrameRateSetting setting;
418             int defaultValue = 0;
419             setting.min_ = safeGetPropAsInt(curDynamicNodePtr, BAD_CAST(XML_ATTR_MIN), defaultValue);
420             setting.max_ = safeGetPropAsInt(curDynamicNodePtr, BAD_CAST(XML_ATTR_MAX), defaultValue);
421             setting.preferredFrameRate_ = safeGetPropAsInt(curDynamicNodePtr, BAD_CAST(XML_ATTR_FPS), defaultValue);
422             if ((setting.max_ >= 0 && setting.min_ >= setting.max_) || setting.preferredFrameRate_ <= 0) {
423                 continue;
424             }
425             frameRateSetting.emplace_back(setting);
426         }
427         ltpoConfig_[settingName] = frameRateSetting;
428     }
429 }
430 
ParseNWebLTPOApp(xmlNodePtr nodePtr)431 void NWebConfigHelper::ParseNWebLTPOApp(xmlNodePtr nodePtr)
432 {
433     for (xmlNodePtr curDynamicNodePtr = nodePtr->xmlChildrenNode; curDynamicNodePtr;
434         curDynamicNodePtr = curDynamicNodePtr->next) {
435         if (curDynamicNodePtr->name == nullptr || curDynamicNodePtr->type == XML_COMMENT_NODE) {
436             WVLOG_E("invalid node!");
437             continue;
438         }
439         char* bundleNamePtr = (char *)xmlGetProp(curDynamicNodePtr, BAD_CAST(XML_ATTR_NAME));
440         if (!bundleNamePtr) {
441             WVLOG_E("invalid bundleName!");
442             continue;
443         }
444         std::string bundleName(bundleNamePtr);
445         xmlFree(bundleNamePtr);
446         ltpoAllowedApps_.emplace(bundleName);
447         WVLOG_D("ltpo dynamic app: %{public}s", bundleName.c_str());
448     }
449 }
450 
ParseNWebLTPOStrategy(xmlNodePtr nodePtr)451 void NWebConfigHelper::ParseNWebLTPOStrategy(xmlNodePtr nodePtr)
452 {
453     ltpoStrategy_ = atoi((char *)xmlNodeGetContent(nodePtr));
454     WVLOG_D("ltpo strategy is: %{public}d", ltpoStrategy_);
455 }
456 
IsLTPODynamicApp(const std::string & bundleName)457 bool NWebConfigHelper::IsLTPODynamicApp(const std::string& bundleName)
458 {
459     return ltpoAllowedApps_.find(bundleName) != ltpoAllowedApps_.end();
460 }
461 
GetLTPOStrategy()462 int32_t NWebConfigHelper::GetLTPOStrategy()
463 {
464     return ltpoStrategy_;
465 }
466 
GetPerfConfig(const std::string & settingName)467 std::vector<FrameRateSetting> NWebConfigHelper::GetPerfConfig(const std::string& settingName)
468 {
469     if (ltpoConfig_.find(settingName) == ltpoConfig_.end()) {
470         WVLOG_E("%{public}s is not exist", settingName.c_str()) ;
471         return {};
472     }
473     return ltpoConfig_[settingName];
474 }
475 
ParsePerfConfig(xmlNodePtr NodePtr)476 void NWebConfigHelper::ParsePerfConfig(xmlNodePtr NodePtr)
477 {
478     WVLOG_D("read performance config");
479     for (xmlNodePtr curNodePtr = NodePtr->xmlChildrenNode; curNodePtr != nullptr; curNodePtr = curNodePtr->next) {
480         if (curNodePtr->name == nullptr || curNodePtr->type == XML_COMMENT_NODE) {
481             WVLOG_E("invalid node!");
482             continue;
483         }
484         std::string nodeName = reinterpret_cast<const char*>(curNodePtr->name);
485         for (xmlNodePtr curChildNodePtr = curNodePtr->xmlChildrenNode; curChildNodePtr != nullptr;
486              curChildNodePtr = curChildNodePtr->next) {
487             if (curChildNodePtr->name == nullptr || curChildNodePtr->type == XML_COMMENT_NODE) {
488                 WVLOG_E("invalid node!");
489                 continue;
490             }
491             std::string childNodeName = reinterpret_cast<const char*>(curChildNodePtr->name);
492             xmlChar* content = xmlNodeGetContent(curChildNodePtr);
493             if (content == nullptr) {
494                 WVLOG_E("read xml node error: nodeName:(%{public}s)", childNodeName.c_str());
495                 continue;
496             }
497             std::string contentStr = reinterpret_cast<const char*>(content);
498             xmlFree(content);
499             {
500             std::lock_guard<std::mutex> lock(lock_);
501             perfConfig_.emplace(nodeName + "/" + childNodeName, contentStr);
502             }
503             WriteConfigValueToSysPara(nodeName + "/" + childNodeName, contentStr);
504         }
505     }
506 }
507 
ParsePerfConfig(const std::string & configNodeName,const std::string & argsNodeName)508 std::string NWebConfigHelper::ParsePerfConfig(const std::string &configNodeName, const std::string &argsNodeName)
509 {
510     std::lock_guard<std::mutex> lock(lock_);
511     auto it = perfConfig_.find(configNodeName + "/" + argsNodeName);
512     if (it == perfConfig_.end()) {
513         WVLOG_W("not found perf config for web_config: %{public}s/%{public}s", configNodeName.c_str(),
514                 argsNodeName.c_str());
515         return "";
516     }
517     WVLOG_D("find performance config %{public}s/%{public}s, value is %{public}s.", configNodeName.c_str(),
518         argsNodeName.c_str(), it->second.c_str());
519     return it->second;
520 }
521 
WriteConfigValueToSysPara(const std::string & configName,const std::string & value)522 void NWebConfigHelper::WriteConfigValueToSysPara(const std::string &configName, const std::string &value)
523 {
524     if (configName == "flowBufferConfig/maxFdNumber") {
525         OHOS::system::SetParameter("web.flowbuffer.maxfd", value);
526     } else if (configName == "TCPConnectedSocketConfig/initialCongestionWindowSize") {
527         OHOS::system::SetParameter("web.TCPConnectedSocket.initialCongestionWindowSize", value);
528     }
529 }
530 
ParseDeleteConfig(const xmlNodePtr & rootPtr,std::shared_ptr<NWebEngineInitArgsImpl> initArgs)531 void NWebConfigHelper::ParseDeleteConfig(const xmlNodePtr &rootPtr, std::shared_ptr<NWebEngineInitArgsImpl> initArgs)
532 {
533     for (xmlNodePtr curNodePtr = rootPtr->xmlChildrenNode; curNodePtr != nullptr; curNodePtr = curNodePtr->next) {
534         if (curNodePtr->name == nullptr || curNodePtr->type == XML_COMMENT_NODE) {
535             WVLOG_E("invalid node!");
536             continue;
537         }
538         std::string nodeName = reinterpret_cast<const char *>(curNodePtr->name);
539         for (xmlNodePtr curChildNodePtr = curNodePtr->xmlChildrenNode; curChildNodePtr != nullptr;
540             curChildNodePtr = curChildNodePtr->next) {
541             if (curChildNodePtr->name == nullptr || curChildNodePtr->type == XML_COMMENT_NODE) {
542                 WVLOG_E("invalid node!");
543                 continue;
544             }
545             std::string childNodeName = reinterpret_cast<const char *>(curChildNodePtr->name);
546             xmlChar *content = xmlNodeGetContent(curChildNodePtr);
547             if (content == nullptr) {
548                 WVLOG_E("read xml node error: nodeName:(%{public}s)", curChildNodePtr->name);
549                 continue;
550             }
551             std::string contentStr = reinterpret_cast<const char *>(content);
552             xmlFree(content);
553             auto it = configMap.find(nodeName + "/" + childNodeName);
554             if (it == configMap.end()) {
555                 WVLOG_W("not found for web_config: %{public}s/%{public}s", nodeName.c_str(), childNodeName.c_str());
556                 continue;
557             }
558             std::string param = it->second(contentStr);
559             if (!param.empty()) {
560                 initArgs->AddDeleteArg(param);
561             }
562         }
563     }
564 }
565 
ParseWindowOrientationConfig(xmlNodePtr nodePtr,std::shared_ptr<NWebEngineInitArgsImpl> initArgs)566 void NWebConfigHelper::ParseWindowOrientationConfig(xmlNodePtr nodePtr,
567     std::shared_ptr<NWebEngineInitArgsImpl> initArgs)
568 {
569     for (xmlNodePtr curNodePtr = nodePtr->xmlChildrenNode; curNodePtr; curNodePtr = curNodePtr->next) {
570         if (curNodePtr->name == nullptr || curNodePtr->type == XML_COMMENT_NODE) {
571             WVLOG_E("invalid node!");
572             continue;
573         }
574         char* bundleNamePtr = (char *)xmlGetProp(curNodePtr,BAD_CAST(XML_BUNDLE_NAME));
575         char* orientationPtr = (char *)xmlGetProp(curNodePtr,BAD_CAST(XML_ENABLE_WINDOW_ORIENTATION));
576         if (!bundleNamePtr || !orientationPtr) {
577             WVLOG_E("invalid bundleNamePtr or orientationPtr!");
578             if (bundleNamePtr) {
579                 xmlFree(bundleNamePtr);
580             }
581             if (orientationPtr) {
582                 xmlFree(orientationPtr);
583             }
584             continue;
585         }
586         std::string bundleName(bundleNamePtr);
587         std::string orientation(orientationPtr);
588         xmlFree(bundleNamePtr);
589         xmlFree(orientationPtr);
590 
591         std::string curBundleName;
592         std::shared_ptr<AbilityRuntime::ApplicationContext> ctx =
593             AbilityRuntime::ApplicationContext::GetApplicationContext();
594         if (ctx) {
595             std::string curBundleName = ctx->GetBundleName();
596         }
597         if (curBundleName.empty()) {
598             WVLOG_E("invalid curBundleName, no need to continue!");
599             return;
600         }
601         if (bundleName == curBundleName || bundleName == WEB_ALL_BUNDLE_NAME) {
602             if (orientation == "true") {
603                 initArgs->AddArg("--enable-blink-features=OrientationEvent");
604             } else if (orientation == "false") {
605                 initArgs->AddArg("--disable-blink-features=OrientationEvent");
606             } else {
607                 WVLOG_E("invalid orientation value!");
608             }
609             break;
610         }
611     }
612 }
613 
safeGetPropAsInt(xmlNode * node,const xmlChar * propName,int defaultValue)614 int NWebConfigHelper::safeGetPropAsInt(xmlNode* node, const xmlChar* propName, int defaultValue)
615 {
616     xmlChar* propValue = xmlGetProp(node, propName);
617     int value = (propValue) ? atoi((const char*)propValue) : defaultValue;
618     xmlFree(propValue);
619     return value;
620 }
621 
SetBundleName(const std::string & bundleName)622 void NWebConfigHelper::SetBundleName(const std::string& bundleName)
623 {
624     bundleName_ = bundleName;
625 }
626 
GetBundleName()627 std::string NWebConfigHelper::GetBundleName()
628 {
629     return bundleName_;
630 }
631 
632 } // namespace OHOS::NWeb
633