• 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_WATCH = "16";
33 const std::string FACTORY_LEVEL_PC = "8";
34 const std::string FACTORY_LEVEL_TABLET = "4";
35 const std::string FACTORY_LEVEL_PHONE = "2";
36 const std::string FACTORY_LEVEL_DEFAULT = "1";
37 
38 const std::string PROP_RENDER_DUMP = "web.render.dump";
39 const std::string PROP_DEBUG_TRACE = "web.debug.trace";
40 const std::unordered_map<std::string, PropertiesKey> PROP_KEY_MAP = {
41     {PROP_RENDER_DUMP, PropertiesKey::PROP_RENDER_DUMP},
42     {PROP_DEBUG_TRACE, PropertiesKey::PROP_DEBUG_TRACE}};
43 
SystemPropertiesChangeCallback(const char * key,const char * value,void * context)44 void SystemPropertiesChangeCallback(const char* key, const char* value, void* context)
45 {
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     }
133     return ProductDeviceType::DEVICE_TYPE_UNKNOWN;
134 }
135 
AnalysisFromConfig()136 ProductDeviceType SystemPropertiesAdapterImpl::AnalysisFromConfig()
137 {
138     std::string factoryLevel = NWebConfigHelper::Instance()
139         .ParsePerfConfig(FACTORY_CONFIG_VALUE, FACTORY_LEVEL_VALUE);
140     if (factoryLevel.empty()) {
141         NWebConfigHelper::Instance().ReadConfigIfNeeded();
142         factoryLevel = NWebConfigHelper::Instance().
143             ParsePerfConfig(FACTORY_CONFIG_VALUE, FACTORY_LEVEL_VALUE);
144     }
145     WVLOG_D("read config factoryLevel: %{public}s ", factoryLevel.c_str());
146     if (factoryLevel == FACTORY_LEVEL_PHONE || factoryLevel == FACTORY_LEVEL_DEFAULT) {
147         return ProductDeviceType::DEVICE_TYPE_MOBILE;
148     } else if (factoryLevel == FACTORY_LEVEL_TABLET) {
149         return ProductDeviceType::DEVICE_TYPE_TABLET;
150     } else if (factoryLevel == FACTORY_LEVEL_PC) {
151         return ProductDeviceType::DEVICE_TYPE_2IN1;
152     }
153     return ProductDeviceType::DEVICE_TYPE_UNKNOWN;
154 }
155 
GetWebOptimizationValue()156 bool SystemPropertiesAdapterImpl::GetWebOptimizationValue()
157 {
158     return OHOS::system::GetBoolParameter("web.optimization", true);
159 }
160 
IsAdvancedSecurityMode()161 bool SystemPropertiesAdapterImpl::IsAdvancedSecurityMode()
162 {
163     char buffer[32] = { 0 };
164     uint32_t buffSize = sizeof(buffer);
165 
166     if (SystemGetParameter("ohos.boot.advsecmode.state", buffer, &buffSize) == 0 && strcmp(buffer, "0") != 0) {
167         return true;
168     }
169     return false;
170 }
171 
GetUserAgentOSName()172 std::string SystemPropertiesAdapterImpl::GetUserAgentOSName()
173 {
174     return OHOS::system::GetParameter("const.product.os.dist.name", "");
175 }
176 
GetUserAgentOSVersion()177 std::string SystemPropertiesAdapterImpl::GetUserAgentOSVersion()
178 {
179     return OHOS::system::GetParameter("const.product.os.dist.apiname", "").empty() ?
180         OHOS::system::GetParameter("const.product.os.dist.version", "") :
181         OHOS::system::GetParameter("const.product.os.dist.apiname", "");
182 }
183 
GetUserAgentBaseOSName()184 std::string SystemPropertiesAdapterImpl::GetUserAgentBaseOSName()
185 {
186     return baseOsName_;
187 }
188 
GetSoftwareMajorVersion()189 int32_t SystemPropertiesAdapterImpl::GetSoftwareMajorVersion()
190 {
191     return softwareMajorVersion_;
192 }
193 
GetSoftwareSeniorVersion()194 int32_t SystemPropertiesAdapterImpl::GetSoftwareSeniorVersion()
195 {
196     return softwareSeniorVersion_;
197 }
198 
GetNetlogMode()199 std::string SystemPropertiesAdapterImpl::GetNetlogMode()
200 {
201     return OHOS::system::GetParameter("web.debug.netlog", "");
202 }
203 
GetTraceDebugEnable()204 bool SystemPropertiesAdapterImpl::GetTraceDebugEnable()
205 {
206     return OHOS::system::GetBoolParameter("web.debug.trace", false);
207 }
208 
GetSiteIsolationMode()209 std::string SystemPropertiesAdapterImpl::GetSiteIsolationMode()
210 {
211     return OHOS::system::GetParameter("web.debug.strictsiteIsolation.enable", "");
212 }
213 
GetFlowBufMaxFd()214 int32_t SystemPropertiesAdapterImpl::GetFlowBufMaxFd()
215 {
216     return OHOS::system::GetIntParameter("web.flowbuffer.maxfd", -1);
217 }
218 
GetOOPGPUEnable()219 bool SystemPropertiesAdapterImpl::GetOOPGPUEnable()
220 {
221     if (GetDeviceInfoProductModel() == "emulator") {
222         return false;
223     }
224     if (OHOS::system::GetParameter("web.oop.gpu", "") == "true") {
225         return true;
226     }
227 
228     return false;
229 }
230 
SetOOPGPUDisable()231 void SystemPropertiesAdapterImpl::SetOOPGPUDisable()
232 {
233     if (OHOS::system::GetParameter("web.oop.gpu", "") == "None") {
234         OHOS::system::SetParameter("web.oop.gpu", "false");
235     }
236     return;
237 }
238 
AddAllSysPropWatchers()239 void SystemPropertiesAdapterImpl::AddAllSysPropWatchers()
240 {
241     for (auto &item : PROP_KEY_MAP) {
242         auto errNo =  WatchParameter(item.first.c_str(), SystemPropertiesChangeCallback, nullptr);
243         if (errNo == 0) {
244             sysPropObserver_[item.second];
245             sysPropMutex_[item.second];
246         } else {
247             WVLOG_I("add watch error result: %{public}d", errNo);
248         }
249     }
250 }
251 
RemoveAllSysPropWatchers()252 void SystemPropertiesAdapterImpl::RemoveAllSysPropWatchers()
253 {
254     for (auto &item : PROP_KEY_MAP) {
255         auto errNo = RemoveParameterWatcher(item.first.c_str(), nullptr, nullptr);
256         if (errNo != 0) {
257             WVLOG_I("remove watch error result: %{public}d", errNo);
258         }
259     }
260 }
261 
DispatchAllWatcherInfo(const char * key,const char * value)262 void SystemPropertiesAdapterImpl::DispatchAllWatcherInfo(const char* key, const char* value)
263 {
264     auto propKeyIt = PROP_KEY_MAP.find(key);
265     if (propKeyIt == PROP_KEY_MAP.end()) {
266         WVLOG_I("sys prop change key is invalid: %{public}s", key);
267         return;
268     }
269 
270     PropertiesKey propkey = propKeyIt->second;
271     auto& keyObservers = sysPropObserver_[propkey];
272 
273     if (keyObservers.size() == 0) {
274         WVLOG_I("no observers in this key: %{public}s", key);
275         return;
276     }
277 
278     std::shared_lock lock(sysPropMutex_[propkey]);
279     for (auto &item : keyObservers) {
280         item->PropertiesUpdate(value);
281     }
282 }
283 
AttachSysPropObserver(PropertiesKey key,SystemPropertiesObserver * observer)284 void SystemPropertiesAdapterImpl::AttachSysPropObserver(PropertiesKey key, SystemPropertiesObserver* observer)
285 {
286     auto observerIt = sysPropObserver_.find(key);
287     if (observerIt == sysPropObserver_.end()) {
288         WVLOG_I("properties key invalid in attach");
289         return;
290     }
291 
292     if (observer == nullptr) {
293         WVLOG_I("properties key observer invalid in attach");
294         return;
295     }
296 
297     std::vector<SystemPropertiesObserver*>& observerVec = observerIt->second;
298     std::unique_lock lock(sysPropMutex_[key]);
299     observerVec.push_back(observer);
300 }
301 
DetachSysPropObserver(PropertiesKey key,SystemPropertiesObserver * observer)302 void SystemPropertiesAdapterImpl::DetachSysPropObserver(PropertiesKey key, SystemPropertiesObserver* observer)
303 {
304     auto observerIt = sysPropObserver_.find(key);
305     if (observerIt == sysPropObserver_.end()) {
306         WVLOG_I("properties key invalid in detach");
307         return;
308     }
309 
310     if (observer == nullptr) {
311         WVLOG_I("properties key observer invalid in detach");
312         return;
313     }
314 
315     std::vector<SystemPropertiesObserver*>& observerVec = observerIt->second;
316     std::unique_lock lock(sysPropMutex_[key]);
317 
318     auto it = std::find(observerVec.begin(), observerVec.end(), observer);
319     if (it != observerVec.end()) {
320         observerVec.erase(it);
321     }
322 }
323 
GetBoolParameter(const std::string & key,bool defaultValue)324 bool SystemPropertiesAdapterImpl::GetBoolParameter(const std::string& key, bool defaultValue)
325 {
326     return OHOS::system::GetBoolParameter(key, defaultValue);
327 }
328 
GetLTPOConfig(const std::string & settingName)329 std::vector<FrameRateSetting> SystemPropertiesAdapterImpl::GetLTPOConfig(const std::string& settingName)
330 {
331     return NWebConfigHelper::Instance().GetPerfConfig(settingName);
332 }
333 
GetOOPGPUStatus()334 std::string SystemPropertiesAdapterImpl::GetOOPGPUStatus()
335 {
336     if (GetDeviceInfoProductModel() == "emulator") {
337         return "false";
338     }
339     return OHOS::system::GetParameter("web.oop.gpu", "");
340 }
341 
GetCompatibleDeviceType()342 std::string SystemPropertiesAdapterImpl::GetCompatibleDeviceType()
343 {
344     char* compatibleDeviceType = OH_NativeBundle_GetCompatibleDeviceType();
345     if (compatibleDeviceType == nullptr) {
346         WVLOG_E("failed get compatible device type.");
347         return "";
348     }
349 
350     std::string deviceType(compatibleDeviceType);
351     free(compatibleDeviceType);
352 
353     return deviceType;
354 }
355 
GetScrollVelocityScale()356 std::string SystemPropertiesAdapterImpl::GetScrollVelocityScale()
357 {
358     return OHOS::system::GetParameter("persist.scrollable.velocityScale", "");
359 }
360 
GetScrollFriction()361 std::string SystemPropertiesAdapterImpl::GetScrollFriction()
362 {
363     return OHOS::system::GetParameter("persist.scrollable.friction", "");
364 }
365 } // namespace OHOS::NWeb
366