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 "efficiency_resources_operation.h"
17
18 #include "singleton.h"
19
20 #include "common.h"
21 #include "background_task_manager.h"
22 #include "efficiency_resource_info.h"
23 #include "efficiency_resource_log.h"
24 #include "hitrace_meter.h"
25
26 namespace OHOS {
27 namespace BackgroundTaskMgr {
28 namespace {
29 static constexpr int32_t APPLY_EFFICIENCY_RESOURCES_PARAMS = 1;
30 }
31
GetNamedBoolValue(const napi_env & env,napi_value & object,const char * utf8name,bool & result,bool isNecessary)32 napi_value GetNamedBoolValue(const napi_env &env, napi_value &object, const char* utf8name,
33 bool& result, bool isNecessary)
34 {
35 bool hasNamedProperty = false;
36 napi_value boolValue = nullptr;
37 if (napi_has_named_property(env, object, utf8name, &hasNamedProperty) != napi_ok || !hasNamedProperty) {
38 if (isNecessary) {
39 BGTASK_LOGE("ParseParameters failed, %{public}s not exist, is nullptr", utf8name);
40 return nullptr;
41 } else {
42 return Common::NapiGetNull(env);
43 }
44 }
45 BGTASK_NAPI_CALL(env, napi_get_named_property(env, object, utf8name, &boolValue));
46 if (!Common::GetBooleanValue(env, boolValue, result)) {
47 BGTASK_LOGE("ParseParameters failed, %{public}s is nullptr", utf8name);
48 return nullptr;
49 }
50 BGTASK_LOGD("GetNamedBoolValue: %{public}s is %{public}d", utf8name, result);
51 return Common::NapiGetNull(env);
52 }
53
GetNamedInt32Value(const napi_env & env,napi_value & object,const char * utf8name,int32_t & result)54 napi_value GetNamedInt32Value(const napi_env &env, napi_value &object, const char* utf8name,
55 int32_t& result)
56 {
57 bool hasNamedProperty = false;
58 napi_value intValue = nullptr;
59 if (napi_has_named_property(env, object, utf8name, &hasNamedProperty) != napi_ok || !hasNamedProperty) {
60 BGTASK_LOGE("ParseParameters failed, %{public}s not exist, is nullptr", utf8name);
61 return nullptr;
62 }
63 BGTASK_NAPI_CALL(env, napi_get_named_property(env, object, utf8name, &intValue));
64 if (!Common::GetInt32NumberValue(env, intValue, result)) {
65 BGTASK_LOGE("ParseParameters failed, %{public}s is nullptr", utf8name);
66 return nullptr;
67 }
68 if (result < 0) {
69 BGTASK_LOGE("%{public}s can't be a negtive number: %{public}d", utf8name, result);
70 return nullptr;
71 }
72 BGTASK_LOGD("GetNamedInt32Value: %{public}s is %{public}d", utf8name, result);
73 return Common::NapiGetNull(env);
74 }
75
GetNamedStringValue(const napi_env & env,napi_value & object,std::string & result)76 napi_value GetNamedStringValue(const napi_env &env, napi_value &object, std::string& result)
77 {
78 bool hasNamedProperty = false;
79 napi_value stringValue = nullptr;
80 if (napi_has_named_property(env, object, "reason", &hasNamedProperty) != napi_ok || !hasNamedProperty) {
81 BGTASK_LOGE("ParseParameters failed, reason not exist, is nullptr");
82 return nullptr;
83 }
84 BGTASK_NAPI_CALL(env, napi_get_named_property(env, object, "reason", &stringValue));
85 if (!Common::GetStringValue(env, stringValue, result)) {
86 BGTASK_LOGE("ParseParameters failed, reason is nullptr");
87 return nullptr;
88 }
89 BGTASK_LOGD("GetNamedStringValue: reason is %{public}s", result.c_str());
90 return Common::NapiGetNull(env);
91 }
92
ParseParameters(const napi_env & env,const napi_callback_info & info,EfficiencyResourceInfo & params,bool isThrow)93 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info,
94 EfficiencyResourceInfo ¶ms, bool isThrow)
95 {
96 size_t argc = APPLY_EFFICIENCY_RESOURCES_PARAMS;
97 napi_value argv[APPLY_EFFICIENCY_RESOURCES_PARAMS] = {nullptr};
98 BGTASK_NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
99 if (argc != APPLY_EFFICIENCY_RESOURCES_PARAMS) {
100 Common::HandleParamErr(env, ERR_PARAM_NUMBER_ERR, isThrow);
101 return nullptr;
102 }
103 int32_t resourceNumber {0};
104 bool isApply {false};
105 int32_t timeOut {0};
106 std::string reason {""};
107 bool isPersist {false};
108 bool isProcess {false};
109
110 if (!GetNamedInt32Value(env, argv[0], "resourceTypes", resourceNumber)) {
111 Common::HandleParamErr(env, ERR_RESOURCE_TYPES_INVALID, isThrow);
112 return nullptr;
113 }
114 if (!GetNamedBoolValue(env, argv[0], "isApply", isApply, true)) {
115 Common::HandleParamErr(env, ERR_ISAPPLY_NULL_OR_TYPE_ERR, isThrow);
116 return nullptr;
117 }
118 if (!GetNamedInt32Value(env, argv[0], "timeOut", timeOut)) {
119 Common::HandleParamErr(env, ERR_TIMEOUT_INVALID, isThrow);
120 return nullptr;
121 }
122 if (!GetNamedStringValue(env, argv[0], reason)) {
123 Common::HandleParamErr(env, ERR_REASON_NULL_OR_TYPE_ERR, isThrow);
124 return nullptr;
125 }
126 if (!GetNamedBoolValue(env, argv[0], "isPersist", isPersist, false)) {
127 Common::HandleParamErr(env, ERR_ISPERSIST_NULL_OR_TYPE_ERR, isThrow);
128 return nullptr;
129 }
130 if (!GetNamedBoolValue(env, argv[0], "isProcess", isProcess, false)) {
131 Common::HandleParamErr(env, ERR_ISPROCESS_NULL_OR_TYPE_ERR, isThrow);
132 return nullptr;
133 }
134 params = EfficiencyResourceInfo {resourceNumber, isApply, timeOut, reason, isPersist, isProcess};
135 return Common::NapiGetNull(env);
136 }
137
CheckValidInfo(napi_env env,const EfficiencyResourceInfo & params,bool isThrow)138 bool CheckValidInfo(napi_env env, const EfficiencyResourceInfo ¶ms, bool isThrow)
139 {
140 if (params.GetResourceNumber() == 0) {
141 Common::HandleParamErr(env, ERR_RESOURCE_TYPES_INVALID, isThrow);
142 return false;
143 }
144 if (params.IsApply() && !params.IsPersist() && params.GetTimeOut() == 0) {
145 Common::HandleParamErr(env, ERR_TIMEOUT_INVALID, isThrow);
146 return false;
147 }
148 return true;
149 }
150
ApplyEfficiencyResources(napi_env env,napi_callback_info info)151 napi_value ApplyEfficiencyResources(napi_env env, napi_callback_info info)
152 {
153 HitraceScoped traceScoped(HITRACE_TAG_OHOS,
154 "BackgroundTaskManager::EfficiencyResource::Napi::ApplyEfficiencyResources");
155 EfficiencyResourceInfo params;
156 if (ParseParameters(env, info, params, true) == nullptr || !CheckValidInfo(env, params, true)) {
157 return Common::NapiGetNull(env);
158 }
159 ErrCode errCode = DelayedSingleton<BackgroundTaskManager>::GetInstance()->ApplyEfficiencyResources(params);
160 Common::HandleErrCode(env, errCode, true);
161 return Common::NapiGetNull(env);
162 }
163
ResetAllEfficiencyResources(napi_env env,napi_callback_info info)164 napi_value ResetAllEfficiencyResources(napi_env env, napi_callback_info info)
165 {
166 HitraceScoped traceScoped(HITRACE_TAG_OHOS,
167 "BackgroundTaskManager::EfficiencyResource::Napi::ResetAllEfficiencyResources");
168 ErrCode errCode = DelayedSingleton<BackgroundTaskManager>::GetInstance()->ResetAllEfficiencyResources();
169 Common::HandleErrCode(env, errCode, true);
170 return Common::NapiGetNull(env);
171 }
172 }
173 }