1 /*
2 * Copyright (c) 2021-2023 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 "base/utils/system_properties.h"
17
18 #include <memory>
19 #include <mutex>
20 #include <shared_mutex>
21 #include <string>
22 #include <unistd.h>
23
24 #include "parameter.h"
25 #include "parameters.h"
26
27 #include "base/log/log.h"
28 #include "base/utils/utils.h"
29 #include "core/common/ace_application_info.h"
30 #ifdef OHOS_STANDARD_SYSTEM
31 #include "systemcapability.h"
32 #endif
33
34 namespace OHOS::Ace {
35 namespace {
36 constexpr char PROPERTY_DEVICE_TYPE[] = "const.product.devicetype";
37 constexpr char PROPERTY_DEVICE_TYPE_DEFAULT[] = "default";
38 constexpr char PROPERTY_DEVICE_TYPE_TV[] = "tv";
39 constexpr char PROPERTY_DEVICE_TYPE_TABLET[] = "tablet";
40 constexpr char PROPERTY_DEVICE_TYPE_TWOINONE[] = "2in1";
41 constexpr char PROPERTY_DEVICE_TYPE_WATCH[] = "watch";
42 constexpr char PROPERTY_DEVICE_TYPE_CAR[] = "car";
43 constexpr char ENABLE_DEBUG_BOUNDARY_KEY[] = "persist.ace.debug.boundary.enabled";
44 constexpr char ANIMATION_SCALE_KEY[] = "persist.sys.arkui.animationscale";
45 constexpr int32_t ORIENTATION_PORTRAIT = 0;
46 constexpr int32_t ORIENTATION_LANDSCAPE = 1;
47 constexpr float DEFAULT_ANIMATION_SCALE = 1.0f;
48 float animationScale_ = DEFAULT_ANIMATION_SCALE;
49 std::shared_mutex mutex_;
50 #ifdef ENABLE_ROSEN_BACKEND
51 constexpr char DISABLE_ROSEN_FILE_PATH[] = "/etc/disablerosen";
52 constexpr char DISABLE_WINDOW_ANIMATION_PATH[] = "/etc/disable_window_size_animation";
53 #endif
54
Swap(int32_t & deviceWidth,int32_t & deviceHeight)55 void Swap(int32_t& deviceWidth, int32_t& deviceHeight)
56 {
57 int32_t temp = deviceWidth;
58 deviceWidth = deviceHeight;
59 deviceHeight = temp;
60 }
61
IsDebugBoundaryEnabled()62 bool IsDebugBoundaryEnabled()
63 {
64 return system::GetParameter(ENABLE_DEBUG_BOUNDARY_KEY, "false") == "true";
65 }
66
IsTraceEnabled()67 bool IsTraceEnabled()
68 {
69 return (system::GetParameter("persist.ace.trace.enabled", "0") == "1" ||
70 system::GetParameter("debug.ace.trace.enabled", "0") == "1");
71 }
72
IsSvgTraceEnabled()73 bool IsSvgTraceEnabled()
74 {
75 return (system::GetParameter("persist.ace.trace.svg.enabled", "0") == "1");
76 }
77
IsHookModeEnabled()78 bool IsHookModeEnabled()
79 {
80 #ifdef PREVIEW
81 return false;
82 #endif
83 const int bufferLen = 128;
84 char paramOutBuf[bufferLen] = { 0 };
85 constexpr char hook_mode[] = "startup:";
86 int ret = GetParameter("persist.libc.hook_mode", "", paramOutBuf, bufferLen);
87 if (ret <= 0 || strncmp(paramOutBuf, hook_mode, strlen(hook_mode)) != 0) {
88 return false;
89 }
90 return true;
91 }
92
IsRosenBackendEnabled()93 bool IsRosenBackendEnabled()
94 {
95 #ifdef PREVIEW
96 return false;
97 #endif
98 #ifdef ENABLE_ROSEN_BACKEND
99 if (system::GetParameter("persist.ace.rosen.backend.enabled", "0") == "1") {
100 return true;
101 }
102 if (system::GetParameter("persist.ace.rosen.backend.enabled", "0") == "2") {
103 return false;
104 }
105 if (access(DISABLE_ROSEN_FILE_PATH, F_OK) == 0) {
106 return false;
107 }
108 return true;
109 #else
110 return false;
111 #endif
112 }
113
IsWindowAnimationEnabled()114 bool IsWindowAnimationEnabled()
115 {
116 #ifdef PREVIEW
117 return false;
118 #endif
119 #ifdef ENABLE_ROSEN_BACKEND
120 if (access(DISABLE_WINDOW_ANIMATION_PATH, F_OK) == 0) {
121 return false;
122 }
123 return true;
124 #else
125 return false;
126 #endif
127 }
128
IsAccessibilityEnabled()129 bool IsAccessibilityEnabled()
130 {
131 return (system::GetParameter("persist.ace.testmode.enabled", "0") == "1" ||
132 system::GetParameter("debug.ace.testmode.enabled", "0") == "1");
133 }
134
IsDebugEnabled()135 bool IsDebugEnabled()
136 {
137 return (system::GetParameter("persist.ace.debug.enabled", "0") == "1");
138 }
139
IsGpuUploadEnabled()140 bool IsGpuUploadEnabled()
141 {
142 return (system::GetParameter("persist.ace.gpuupload.enabled", "0") == "1" ||
143 system::GetParameter("debug.ace.gpuupload.enabled", "0") == "1");
144 }
145
OnAnimationScaleChanged(const char * key,const char * value,void * context)146 void OnAnimationScaleChanged(const char* key, const char* value, void* context)
147 {
148 CHECK_NULL_VOID(key);
149 if (strcmp(key, ANIMATION_SCALE_KEY) != 0) {
150 LOGE("AnimationScale key not matched. key: %{public}s", key);
151 return;
152 }
153 std::unique_lock<std::shared_mutex> lock(mutex_);
154 if (value == nullptr) {
155 LOGW("AnimationScale changes with null value, use default. key: %{public}s", key);
156 animationScale_ = DEFAULT_ANIMATION_SCALE;
157 return;
158 }
159 auto animationScale = std::atof(value);
160 if (animationScale < 0.0f) {
161 LOGE("AnimationScale changes with invalid value: %{public}s. ignore", value);
162 return;
163 }
164 LOGI("AnimationScale: %{public}f -> %{public}f", animationScale_, animationScale);
165 animationScale_ = animationScale;
166 }
167
GetSysDumpFrameCount()168 uint32_t GetSysDumpFrameCount()
169 {
170 return system::GetUintParameter<uint32_t>("persist.ace.framedumpcount", 10);
171 }
172
GetAstcEnabled()173 bool GetAstcEnabled()
174 {
175 return system::GetParameter("persist.astc.enable", "true") == "true";
176 }
177
GetAstcMaxErrorProp()178 int32_t GetAstcMaxErrorProp()
179 {
180 return system::GetIntParameter<int>("persist.astc.max", 50000);
181 }
182
GetAstcPsnrProp()183 int32_t GetAstcPsnrProp()
184 {
185 return system::GetIntParameter<int>("persist.astc.psnr", 0);
186 }
187
IsUseMemoryMonitor()188 bool IsUseMemoryMonitor()
189 {
190 return (system::GetParameter("persist.ace.memorymonitor.enabled", "0") == "1");
191 }
192
IsExtSurfaceEnabled()193 bool IsExtSurfaceEnabled()
194 {
195 #ifdef EXT_SURFACE_ENABLE
196 return true;
197 #else
198 return false;
199 #endif
200 }
201 } // namespace
202
203 bool SystemProperties::traceEnabled_ = IsTraceEnabled();
204 bool SystemProperties::svgTraceEnable_ = IsSvgTraceEnabled();
205 bool SystemProperties::accessibilityEnabled_ = IsAccessibilityEnabled();
206 bool SystemProperties::isRound_ = false;
207 bool SystemProperties::isDeviceAccess_ = false;
208 int32_t SystemProperties::deviceWidth_ = 0;
209 int32_t SystemProperties::deviceHeight_ = 0;
210 ACE_WEAK_SYM double SystemProperties::resolution_ = 1.0;
211 ACE_WEAK_SYM DeviceType SystemProperties::deviceType_ { DeviceType::UNKNOWN };
212 ACE_WEAK_SYM DeviceOrientation SystemProperties::orientation_ { DeviceOrientation::PORTRAIT };
213 std::string SystemProperties::brand_ = INVALID_PARAM;
214 std::string SystemProperties::manufacturer_ = INVALID_PARAM;
215 std::string SystemProperties::model_ = INVALID_PARAM;
216 std::string SystemProperties::product_ = INVALID_PARAM;
217 std::string SystemProperties::apiVersion_ = INVALID_PARAM;
218 std::string SystemProperties::releaseType_ = INVALID_PARAM;
219 std::string SystemProperties::paramDeviceType_ = INVALID_PARAM;
220 int32_t SystemProperties::mcc_ = MCC_UNDEFINED;
221 int32_t SystemProperties::mnc_ = MNC_UNDEFINED;
222 ColorMode SystemProperties::colorMode_ { ColorMode::LIGHT };
223 ScreenShape SystemProperties::screenShape_ { ScreenShape::NOT_ROUND };
224 LongScreenType SystemProperties::LongScreen_ { LongScreenType::NOT_LONG };
225 bool SystemProperties::unZipHap_ = true;
226 ACE_WEAK_SYM bool SystemProperties::rosenBackendEnabled_ = IsRosenBackendEnabled();
227 ACE_WEAK_SYM bool SystemProperties::isHookModeEnabled_ = IsHookModeEnabled();
228 bool SystemProperties::debugBoundaryEnabled_ = IsDebugBoundaryEnabled();
229 ACE_WEAK_SYM bool SystemProperties::windowAnimationEnabled_ = IsWindowAnimationEnabled();
230 bool SystemProperties::debugEnabled_ = IsDebugEnabled();
231 bool SystemProperties::gpuUploadEnabled_ = IsGpuUploadEnabled();
232 bool SystemProperties::astcEnabled_ = GetAstcEnabled();
233 int32_t SystemProperties::astcMax_ = GetAstcMaxErrorProp();
234 int32_t SystemProperties::astcPsnr_ = GetAstcPsnrProp();
235 ACE_WEAK_SYM bool SystemProperties::extSurfaceEnabled_ = IsExtSurfaceEnabled();
236 ACE_WEAK_SYM uint32_t SystemProperties::dumpFrameCount_ = GetSysDumpFrameCount();
237
IsSyscapExist(const char * cap)238 bool SystemProperties::IsSyscapExist(const char* cap)
239 {
240 #ifdef OHOS_STANDARD_SYSTEM
241 return HasSystemCapability(cap);
242 #else
243 return false;
244 #endif
245 }
246
InitDeviceType(DeviceType)247 void SystemProperties::InitDeviceType(DeviceType)
248 {
249 // Do nothing, no need to store type here, use system property at 'GetDeviceType' instead.
250 }
251
GetArkProperties()252 int SystemProperties::GetArkProperties()
253 {
254 return system::GetIntParameter<int>("persist.ark.properties", -1);
255 }
256
GetArkBundleName()257 std::string SystemProperties::GetArkBundleName()
258 {
259 return system::GetParameter("persist.ark.arkbundlename", "");
260 }
261
GetGcThreadNum()262 size_t SystemProperties::GetGcThreadNum()
263 {
264 size_t defaultGcThreadNums = 7;
265 return system::GetUintParameter<size_t>("persist.ark.gcthreads", defaultGcThreadNums);
266 }
267
GetLongPauseTime()268 size_t SystemProperties::GetLongPauseTime()
269 {
270 size_t defaultLongPauseTime = 40; // 40ms
271 return system::GetUintParameter<size_t>("persist.ark.longpausetime", defaultLongPauseTime);
272 }
273
GetAsmInterpreterEnabled()274 bool SystemProperties::GetAsmInterpreterEnabled()
275 {
276 return system::GetParameter("persist.ark.asminterpreter", "true") == "true";
277 }
278
GetAsmOpcodeDisableRange()279 std::string SystemProperties::GetAsmOpcodeDisableRange()
280 {
281 return system::GetParameter("persist.ark.asmopcodedisablerange", "");
282 }
283
IsScoringEnabled(const std::string & name)284 bool SystemProperties::IsScoringEnabled(const std::string& name)
285 {
286 if (name.empty()) {
287 return false;
288 }
289 std::string filePath = "/etc/" + name;
290 if (access(filePath.c_str(), F_OK) == 0) {
291 return true;
292 }
293 std::string prop = system::GetParameter("persist.ace.trace.scoringtool", "");
294 return prop == name;
295 }
296
GetDeviceType()297 ACE_WEAK_SYM DeviceType SystemProperties::GetDeviceType()
298 {
299 InitDeviceTypeBySystemProperty();
300 return deviceType_;
301 }
302
InitDeviceTypeBySystemProperty()303 void SystemProperties::InitDeviceTypeBySystemProperty()
304 {
305 if (deviceType_ != DeviceType::UNKNOWN) {
306 return;
307 }
308
309 auto deviceProp = system::GetParameter(PROPERTY_DEVICE_TYPE, PROPERTY_DEVICE_TYPE_DEFAULT);
310 // Properties: "default", "tv", "tablet", "watch", "car"
311 LOGD("GetDeviceType, deviceProp=%{private}s.", deviceProp.c_str());
312 if (deviceProp == PROPERTY_DEVICE_TYPE_TV) {
313 deviceType_ = DeviceType::TV;
314 } else if (deviceProp == PROPERTY_DEVICE_TYPE_CAR) {
315 deviceType_ = DeviceType::CAR;
316 } else if (deviceProp == PROPERTY_DEVICE_TYPE_WATCH) {
317 deviceType_ = DeviceType::WATCH;
318 } else if (deviceProp == PROPERTY_DEVICE_TYPE_TABLET) {
319 deviceType_ = DeviceType::TABLET;
320 } else if (deviceProp == PROPERTY_DEVICE_TYPE_TWOINONE) {
321 deviceType_ = DeviceType::TWO_IN_ONE;
322 } else {
323 deviceType_ = DeviceType::PHONE;
324 }
325 }
326
InitDeviceInfo(int32_t deviceWidth,int32_t deviceHeight,int32_t orientation,double resolution,bool isRound)327 void SystemProperties::InitDeviceInfo(
328 int32_t deviceWidth, int32_t deviceHeight, int32_t orientation, double resolution, bool isRound)
329 {
330 // SetDeviceOrientation should be earlier than deviceWidth/deviceHeight init.
331 SetDeviceOrientation(orientation);
332
333 isRound_ = isRound;
334 resolution_ = resolution;
335 deviceWidth_ = deviceWidth;
336 deviceHeight_ = deviceHeight;
337 brand_ = ::GetBrand();
338 manufacturer_ = ::GetManufacture();
339 model_ = ::GetProductModel();
340 product_ = ::GetMarketName();
341 apiVersion_ = std::to_string(::GetSdkApiVersion());
342 releaseType_ = ::GetOsReleaseType();
343 paramDeviceType_ = ::GetDeviceType();
344
345 debugEnabled_ = IsDebugEnabled();
346 traceEnabled_ = IsTraceEnabled();
347 svgTraceEnable_ = IsSvgTraceEnabled();
348 accessibilityEnabled_ = IsAccessibilityEnabled();
349 rosenBackendEnabled_ = IsRosenBackendEnabled();
350 isHookModeEnabled_ = IsHookModeEnabled();
351 debugBoundaryEnabled_ = system::GetParameter(ENABLE_DEBUG_BOUNDARY_KEY, "false") == "true";
352 animationScale_ = std::atof(system::GetParameter(ANIMATION_SCALE_KEY, "1").c_str());
353 WatchParameter(ANIMATION_SCALE_KEY, OnAnimationScaleChanged, nullptr);
354
355 if (isRound_) {
356 screenShape_ = ScreenShape::ROUND;
357 } else {
358 screenShape_ = ScreenShape::NOT_ROUND;
359 }
360
361 InitDeviceTypeBySystemProperty();
362 }
363
SetDeviceOrientation(int32_t orientation)364 void SystemProperties::SetDeviceOrientation(int32_t orientation)
365 {
366 if (orientation == ORIENTATION_PORTRAIT && orientation_ != DeviceOrientation::PORTRAIT) {
367 Swap(deviceWidth_, deviceHeight_);
368 orientation_ = DeviceOrientation::PORTRAIT;
369 } else if (orientation == ORIENTATION_LANDSCAPE && orientation_ != DeviceOrientation::LANDSCAPE) {
370 Swap(deviceWidth_, deviceHeight_);
371 orientation_ = DeviceOrientation::LANDSCAPE;
372 }
373 }
374
GetFontWeightScale()375 ACE_WEAK_SYM float SystemProperties::GetFontWeightScale()
376 {
377 // Default value of font weight scale is 1.0.
378 std::string prop =
379 "persist.sys.font_wght_scale_for_user" + std::to_string(AceApplicationInfo::GetInstance().GetUserId());
380 return std::stof(system::GetParameter(prop, "1.0"));
381 }
382
InitMccMnc(int32_t mcc,int32_t mnc)383 void SystemProperties::InitMccMnc(int32_t mcc, int32_t mnc)
384 {
385 mcc_ = mcc;
386 mnc_ = mnc;
387 }
388
GetDebugEnabled()389 ACE_WEAK_SYM bool SystemProperties::GetDebugEnabled()
390 {
391 return debugEnabled_;
392 }
393
GetLanguage()394 std::string SystemProperties::GetLanguage()
395 {
396 return system::GetParameter("const.global.language", INVALID_PARAM);
397 }
398
GetRegion()399 std::string SystemProperties::GetRegion()
400 {
401 return system::GetParameter("const.global.region", INVALID_PARAM);
402 }
403
GetNewPipePkg()404 std::string SystemProperties::GetNewPipePkg()
405 {
406 return system::GetParameter("persist.ace.newpipe.pkgname", "");
407 }
408
GetAnimationScale()409 ACE_WEAK_SYM float SystemProperties::GetAnimationScale()
410 {
411 std::shared_lock<std::shared_mutex> lock(mutex_);
412 return animationScale_;
413 }
414
GetPartialUpdatePkg()415 std::string SystemProperties::GetPartialUpdatePkg()
416 {
417 return system::GetParameter("persist.ace.partial.pkgname", "");
418 }
419
GetSvgMode()420 int32_t SystemProperties::GetSvgMode()
421 {
422 return system::GetIntParameter<int>("persist.ace.svg.mode", 1);
423 }
424
GetAllowWindowOpenMethodEnabled()425 bool SystemProperties::GetAllowWindowOpenMethodEnabled()
426 {
427 return system::GetBoolParameter("persist.web.allowWindowOpenMethod.enabled", false);
428 }
429
GetImageFrameworkEnabled()430 bool SystemProperties::GetImageFrameworkEnabled()
431 {
432 return system::GetBoolParameter("persist.ace.image.framework.enabled", true);
433 }
434
GetIsUseMemoryMonitor()435 ACE_WEAK_SYM bool SystemProperties::GetIsUseMemoryMonitor()
436 {
437 static bool isUseMemoryMonitor = IsUseMemoryMonitor();
438 return isUseMemoryMonitor;
439 }
440
IsFormAnimationLimited()441 bool SystemProperties::IsFormAnimationLimited()
442 {
443 return system::GetBoolParameter("persist.sys.arkui.formAnimationLimit", true);
444 }
445
446 } // namespace OHOS::Ace
447