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