• 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 #include "system_properties_adapter_impl.h"
17 
18 #include <locale>
19 #include <securec.h>
20 #include <native_interface_bundle.h>
21 
22 #include "init_param.h"
23 #include "nweb_config_helper.h"
24 #include "nweb_log.h"
25 #include "parameter.h"
26 #include "parameters.h"
27 #include "sysversion.h"
28 #include "hitrace_adapter_impl.h"
29 
30 namespace OHOS::NWeb {
31 const std::string FACTORY_CONFIG_VALUE = "factoryConfig";
32 const std::string FACTORY_LEVEL_VALUE = "factoryLevel";
33 const std::string FACTORY_LEVEL_TV = "32";
34 const std::string FACTORY_LEVEL_WATCH = "16";
35 const std::string FACTORY_LEVEL_PC = "8";
36 const std::string FACTORY_LEVEL_TABLET = "4";
37 const std::string FACTORY_LEVEL_PHONE = "2";
38 const std::string FACTORY_LEVEL_DEFAULT = "1";
39 constexpr int DEFAULT_INITIAL_CONGESTION_WINDOW_SIZE = -1;
40 constexpr size_t INT_MAX_LEN = 10;
41 
42 const std::string PROP_RENDER_DUMP = "web.render.dump";
43 const std::string PROP_DEBUG_TRACE = "web.debug.trace";
44 const std::unordered_map<std::string, PropertiesKey> PROP_KEY_MAP = {
45     {PROP_RENDER_DUMP, PropertiesKey::PROP_RENDER_DUMP},
46     {PROP_DEBUG_TRACE, PropertiesKey::PROP_DEBUG_TRACE}};
47 
SystemPropertiesChangeCallback(const char * key,const char * value,void * context)48 void SystemPropertiesChangeCallback(const char* key, const char* value, void* context)
49 {
50     WVLOG_D("sys prop change key: %{public}s ,value : %{public}s ", key,  value);
51     SystemPropertiesAdapterImpl::GetInstance().DispatchAllWatcherInfo(key, value);
52 }
53 
54 // static
GetInstance()55 SystemPropertiesAdapterImpl& SystemPropertiesAdapterImpl::GetInstance()
56 {
57     static SystemPropertiesAdapterImpl instance;
58     return instance;
59 }
60 
SystemPropertiesAdapterImpl()61 SystemPropertiesAdapterImpl::SystemPropertiesAdapterImpl()
62 {
63     std::string osFullName =
64         OHOS::system::GetParameter("const.ohos.fullname", "");
65     if (osFullName.empty()) {
66         WVLOG_E("get os full name failed");
67         return;
68     }
69     size_t index = osFullName.find('-');
70     if (index == std::string::npos) {
71         return;
72     }
73     std::string baseOsNameTmp = osFullName.substr(0, index);
74 
75     int versionPartOne;
76     int versionPartTwo;
77     int versionPartThree;
78     int versionPartFour;
79     const char* tmp = strstr(osFullName.c_str(), "-");
80     if (tmp == NULL) {
81         return;
82     }
83     tmp++;
84     int ret = sscanf_s(tmp, "%d.%d.%d.%d",
85         &versionPartOne, &versionPartTwo, &versionPartThree, &versionPartFour);
86     if (ret <= 0) {
87         WVLOG_E("paser os full name failed");
88         return;
89     }
90     softwareMajorVersion_ = versionPartOne;
91     softwareSeniorVersion_ = versionPartTwo;
92     baseOsName_ = baseOsNameTmp;
93     AddAllSysPropWatchers();
94 }
95 
~SystemPropertiesAdapterImpl()96 SystemPropertiesAdapterImpl::~SystemPropertiesAdapterImpl()
97 {
98     RemoveAllSysPropWatchers();
99 }
100 
GetResourceUseHapPathEnable()101 bool SystemPropertiesAdapterImpl::GetResourceUseHapPathEnable()
102 {
103     return OHOS::system::GetBoolParameter("compress", false);
104 }
105 
GetDeviceInfoProductModel()106 std::string SystemPropertiesAdapterImpl::GetDeviceInfoProductModel()
107 {
108     return GetProductModel();
109 }
110 
GetDeviceInfoBrand()111 std::string SystemPropertiesAdapterImpl::GetDeviceInfoBrand()
112 {
113     return GetBrand();
114 }
115 
GetDeviceInfoMajorVersion()116 int32_t SystemPropertiesAdapterImpl::GetDeviceInfoMajorVersion()
117 {
118     return GetMajorVersion();
119 }
120 
GetProductDeviceType()121 ProductDeviceType SystemPropertiesAdapterImpl::GetProductDeviceType()
122 {
123     ProductDeviceType factoryLevel = AnalysisFromConfig();
124     if (factoryLevel != ProductDeviceType::DEVICE_TYPE_UNKNOWN) {
125         return factoryLevel;
126     }
127     WVLOG_W("read config factoryLevel: fail");
128     // RK or other device cant read config,need read from system deviceType
129     std::string deviceType = OHOS::system::GetDeviceType();
130     if (deviceType == "phone" || deviceType == "default") {
131         return ProductDeviceType::DEVICE_TYPE_MOBILE;
132     } else if (deviceType == "tablet") {
133         return ProductDeviceType::DEVICE_TYPE_TABLET;
134     } else if (deviceType == "2in1") {
135         return ProductDeviceType::DEVICE_TYPE_2IN1;
136     } else if (deviceType == "wearable") {
137         return ProductDeviceType::DEVICE_TYPE_WEARABLE;
138     } else if (deviceType == "tv") {
139         return ProductDeviceType::DEVICE_TYPE_TV;
140     }
141     return ProductDeviceType::DEVICE_TYPE_UNKNOWN;
142 }
143 
AnalysisFromConfig()144 ProductDeviceType SystemPropertiesAdapterImpl::AnalysisFromConfig()
145 {
146     std::string factoryLevel = NWebConfigHelper::Instance()
147         .ParsePerfConfig(FACTORY_CONFIG_VALUE, FACTORY_LEVEL_VALUE);
148     if (factoryLevel.empty()) {
149         NWebConfigHelper::Instance().ReadConfigIfNeeded();
150         factoryLevel = NWebConfigHelper::Instance().
151             ParsePerfConfig(FACTORY_CONFIG_VALUE, FACTORY_LEVEL_VALUE);
152     }
153     WVLOG_D("read config factoryLevel: %{public}s ", factoryLevel.c_str());
154     if (factoryLevel == FACTORY_LEVEL_PHONE || factoryLevel == FACTORY_LEVEL_DEFAULT) {
155         return ProductDeviceType::DEVICE_TYPE_MOBILE;
156     } else if (factoryLevel == FACTORY_LEVEL_TABLET) {
157         return ProductDeviceType::DEVICE_TYPE_TABLET;
158     } else if (factoryLevel == FACTORY_LEVEL_PC) {
159         return ProductDeviceType::DEVICE_TYPE_2IN1;
160     } else if (factoryLevel == FACTORY_LEVEL_WATCH) {
161         return ProductDeviceType::DEVICE_TYPE_WEARABLE;
162     } else if (factoryLevel == FACTORY_LEVEL_TV) {
163         return ProductDeviceType::DEVICE_TYPE_TV;
164     }
165     return ProductDeviceType::DEVICE_TYPE_UNKNOWN;
166 }
167 
GetWebOptimizationValue()168 bool SystemPropertiesAdapterImpl::GetWebOptimizationValue()
169 {
170     return OHOS::system::GetBoolParameter("web.optimization", true);
171 }
172 
IsAdvancedSecurityMode()173 bool SystemPropertiesAdapterImpl::IsAdvancedSecurityMode()
174 {
175     char buffer[32] = { 0 };
176     uint32_t buffSize = sizeof(buffer);
177 
178     if (SystemGetParameter("ohos.boot.advsecmode.state", buffer, &buffSize) == 0 && strcmp(buffer, "0") != 0) {
179         return true;
180     }
181     return false;
182 }
183 
GetUserAgentOSName()184 std::string SystemPropertiesAdapterImpl::GetUserAgentOSName()
185 {
186     return OHOS::system::GetParameter("const.product.os.dist.name", "");
187 }
188 
GetUserAgentOSVersion()189 std::string SystemPropertiesAdapterImpl::GetUserAgentOSVersion()
190 {
191     return OHOS::system::GetParameter("const.product.os.dist.apiname", "").empty() ?
192         OHOS::system::GetParameter("const.product.os.dist.version", "") :
193         OHOS::system::GetParameter("const.product.os.dist.apiname", "");
194 }
195 
GetUserAgentBaseOSName()196 std::string SystemPropertiesAdapterImpl::GetUserAgentBaseOSName()
197 {
198     return baseOsName_;
199 }
200 
GetSoftwareMajorVersion()201 int32_t SystemPropertiesAdapterImpl::GetSoftwareMajorVersion()
202 {
203     return softwareMajorVersion_;
204 }
205 
GetSoftwareSeniorVersion()206 int32_t SystemPropertiesAdapterImpl::GetSoftwareSeniorVersion()
207 {
208     return softwareSeniorVersion_;
209 }
210 
GetNetlogMode()211 std::string SystemPropertiesAdapterImpl::GetNetlogMode()
212 {
213     return OHOS::system::GetParameter("web.debug.netlog", "");
214 }
215 
GetTraceDebugEnable()216 bool SystemPropertiesAdapterImpl::GetTraceDebugEnable()
217 {
218     return OHOS::system::GetBoolParameter("web.debug.trace", false);
219 }
220 
GetSiteIsolationMode()221 std::string SystemPropertiesAdapterImpl::GetSiteIsolationMode()
222 {
223     return OHOS::system::GetParameter("web.debug.strictsiteIsolation.enable", "");
224 }
225 
GetFlowBufMaxFd()226 int32_t SystemPropertiesAdapterImpl::GetFlowBufMaxFd()
227 {
228     return OHOS::system::GetIntParameter("web.flowbuffer.maxfd", -1);
229 }
230 
GetOOPGPUEnable()231 bool SystemPropertiesAdapterImpl::GetOOPGPUEnable()
232 {
233     if (GetDeviceInfoProductModel() == "emulator") {
234         return false;
235     }
236     if (OHOS::system::GetParameter("web.oop.gpu", "") == "true") {
237         return true;
238     }
239 
240     return false;
241 }
242 
SetOOPGPUDisable()243 void SystemPropertiesAdapterImpl::SetOOPGPUDisable()
244 {
245     if (OHOS::system::GetParameter("web.oop.gpu", "") == "None") {
246         OHOS::system::SetParameter("web.oop.gpu", "false");
247     }
248     return;
249 }
250 
GetInitialCongestionWindowSize()251 int32_t SystemPropertiesAdapterImpl::GetInitialCongestionWindowSize()
252 {
253     std::string init_cwnd_str = NWebConfigHelper::Instance()
254         .ParsePerfConfig("TCPConnectedSocketConfig", "initialCongestionWindowSize");
255     if (init_cwnd_str.size() > 0 && init_cwnd_str.size() < INT_MAX_LEN - 1 ) {
256         for (char character : init_cwnd_str) {
257             if (!std::isdigit(character, std::locale::classic())) {
258                 WVLOG_E("parse initialCongestionWindowSize failed: invalid argument");
259                 return DEFAULT_INITIAL_CONGESTION_WINDOW_SIZE;
260             }
261         }
262 
263         return std::stoi(init_cwnd_str);
264     }
265 
266     return DEFAULT_INITIAL_CONGESTION_WINDOW_SIZE;
267 }
268 
AddAllSysPropWatchers()269 void SystemPropertiesAdapterImpl::AddAllSysPropWatchers()
270 {
271     for (auto &item : PROP_KEY_MAP) {
272         auto errNo =  WatchParameter(item.first.c_str(), SystemPropertiesChangeCallback, nullptr);
273         if (errNo == 0) {
274             sysPropObserver_[item.second];
275             sysPropMutex_[item.second];
276         } else {
277             WVLOG_I("add watch error result: %{public}d", errNo);
278         }
279     }
280 }
281 
RemoveAllSysPropWatchers()282 void SystemPropertiesAdapterImpl::RemoveAllSysPropWatchers()
283 {
284     for (auto &item : PROP_KEY_MAP) {
285         auto errNo = RemoveParameterWatcher(item.first.c_str(), nullptr, nullptr);
286         if (errNo != 0) {
287             WVLOG_I("remove watch error result: %{public}d", errNo);
288         }
289     }
290 }
291 
DispatchAllWatcherInfo(const char * key,const char * value)292 void SystemPropertiesAdapterImpl::DispatchAllWatcherInfo(const char* key, const char* value)
293 {
294     auto propKeyIt = PROP_KEY_MAP.find(key);
295     if (propKeyIt == PROP_KEY_MAP.end()) {
296         WVLOG_I("sys prop change key is invalid: %{public}s", key);
297         return;
298     }
299 
300     PropertiesKey propkey = propKeyIt->second;
301     auto& keyObservers = sysPropObserver_[propkey];
302 
303     if (keyObservers.size() == 0) {
304         WVLOG_I("no observers in this key: %{public}s", key);
305         return;
306     }
307 
308     std::shared_lock lock(sysPropMutex_[propkey]);
309     for (auto &item : keyObservers) {
310         item->PropertiesUpdate(value);
311     }
312 }
313 
AttachSysPropObserver(PropertiesKey key,SystemPropertiesObserver * observer)314 void SystemPropertiesAdapterImpl::AttachSysPropObserver(PropertiesKey key, SystemPropertiesObserver* observer)
315 {
316     auto observerIt = sysPropObserver_.find(key);
317     if (observerIt == sysPropObserver_.end()) {
318         WVLOG_I("properties key invalid in attach");
319         return;
320     }
321 
322     if (observer == nullptr) {
323         WVLOG_I("properties key observer invalid in attach");
324         return;
325     }
326 
327     std::vector<SystemPropertiesObserver*>& observerVec = observerIt->second;
328     std::unique_lock lock(sysPropMutex_[key]);
329     observerVec.push_back(observer);
330 }
331 
DetachSysPropObserver(PropertiesKey key,SystemPropertiesObserver * observer)332 void SystemPropertiesAdapterImpl::DetachSysPropObserver(PropertiesKey key, SystemPropertiesObserver* observer)
333 {
334     auto observerIt = sysPropObserver_.find(key);
335     if (observerIt == sysPropObserver_.end()) {
336         WVLOG_I("properties key invalid in detach");
337         return;
338     }
339 
340     if (observer == nullptr) {
341         WVLOG_I("properties key observer invalid in detach");
342         return;
343     }
344 
345     std::vector<SystemPropertiesObserver*>& observerVec = observerIt->second;
346     std::unique_lock lock(sysPropMutex_[key]);
347 
348     auto it = std::find(observerVec.begin(), observerVec.end(), observer);
349     if (it != observerVec.end()) {
350         observerVec.erase(it);
351     }
352 }
353 
GetBoolParameter(const std::string & key,bool defaultValue)354 bool SystemPropertiesAdapterImpl::GetBoolParameter(const std::string& key, bool defaultValue)
355 {
356     return OHOS::system::GetBoolParameter(key, defaultValue);
357 }
358 
GetLTPOConfig(const std::string & settingName)359 std::vector<FrameRateSetting> SystemPropertiesAdapterImpl::GetLTPOConfig(const std::string& settingName)
360 {
361     return NWebConfigHelper::Instance().GetPerfConfig(settingName);
362 }
363 
GetOOPGPUStatus()364 std::string SystemPropertiesAdapterImpl::GetOOPGPUStatus()
365 {
366     if (GetDeviceInfoProductModel() == "emulator") {
367         return "false";
368     }
369     return OHOS::system::GetParameter("web.oop.gpu", "");
370 }
371 
IsLTPODynamicApp(const std::string & bundleName)372 bool SystemPropertiesAdapterImpl::IsLTPODynamicApp(const std::string& bundleName)
373 {
374     return NWebConfigHelper::Instance().IsLTPODynamicApp(bundleName);
375 }
376 
GetLTPOStrategy()377 int32_t SystemPropertiesAdapterImpl::GetLTPOStrategy()
378 {
379     return NWebConfigHelper::Instance().GetLTPOStrategy();
380 }
381 
GetVulkanStatus()382 std::string SystemPropertiesAdapterImpl::GetVulkanStatus()
383 {
384     if ((OHOS::system::GetParameter("const.gpu.vendor", "0").compare("higpu.v200") == 0)
385         || (OHOS::system::GetParameter("const.gpu.vendor", "0").compare("higpu.v210") == 0)
386         || (OHOS::system::GetParameter("const.gpu.vendor", "0").compare("higpu.v300") == 0)) {
387         return OHOS::system::GetParameter("web.ohos.vulkan", "");
388     } else {
389         return "false";
390     }
391 }
392 
GetCompatibleDeviceType()393 std::string SystemPropertiesAdapterImpl::GetCompatibleDeviceType()
394 {
395     char* compatibleDeviceType = OH_NativeBundle_GetCompatibleDeviceType();
396     if (compatibleDeviceType == nullptr) {
397         WVLOG_E("failed get compatible device type.");
398         return "";
399     }
400 
401     std::string deviceType(compatibleDeviceType);
402     free(compatibleDeviceType);
403 
404     return deviceType;
405 }
406 
GetDeviceInfoApiVersion()407 std::string SystemPropertiesAdapterImpl::GetDeviceInfoApiVersion()
408 {
409     return OHOS::system::GetParameter("const.product.os.dist.apiversion", "");
410 }
411 
GetPRPPreloadMode()412 std::string SystemPropertiesAdapterImpl::GetPRPPreloadMode()
413 {
414     return OHOS::system::GetParameter("web.prppreload.mode", "none");
415 }
416 
GetScrollVelocityScale()417 std::string SystemPropertiesAdapterImpl::GetScrollVelocityScale()
418 {
419     return OHOS::system::GetParameter("persist.scrollable.velocityScale", "");
420 }
421 
GetScrollFriction()422 std::string SystemPropertiesAdapterImpl::GetScrollFriction()
423 {
424     return OHOS::system::GetParameter("persist.scrollable.friction", "");
425 }
426 
GetBundleName()427 std::string SystemPropertiesAdapterImpl::GetBundleName()
428 {
429     return NWebConfigHelper::Instance().GetBundleName();
430 }
431 
GetStringParameter(const std::string & key,const std::string & defaultValue)432 std::string SystemPropertiesAdapterImpl::GetStringParameter(const std::string& key, const std::string& defaultValue)
433 {
434     return OHOS::system::GetParameter(key, defaultValue);
435 }
436 
GetIntParameter(const std::string & key,int32_t defaultValue)437 int32_t SystemPropertiesAdapterImpl::GetIntParameter(const std::string& key, int32_t defaultValue)
438 {
439     return OHOS::system::GetIntParameter(key, defaultValue);
440 }
441 } // namespace OHOS::NWeb
442