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