• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include "prop_n_operation.h"
16 
17 #include "b_error/b_error.h"
18 #include "b_incremental_data.h"
19 #include "b_resources/b_constants.h"
20 #include "b_sa/b_sa_utils.h"
21 #include "filemgmt_libhilog.h"
22 #include "filemgmt_libn.h"
23 #include "incremental_backup_data.h"
24 #include "parse_inc_info_from_js.h"
25 #include "service_proxy.h"
26 #include "access_token.h"
27 #include "accesstoken_kit.h"
28 #include "ipc_skeleton.h"
29 #include "tokenid_kit.h"
30 
31 namespace OHOS::FileManagement::Backup {
32 using namespace std;
33 using namespace LibN;
34 
AsyncCallback(napi_env env,const NFuncArg & funcArg)35 static napi_value AsyncCallback(napi_env env, const NFuncArg& funcArg)
36 {
37     HILOGD("called LocalCapabilities::AsyncCallback begin");
38 
39     auto fd = make_shared<UniqueFd>();
40     auto cbExec = [fd]() -> NError {
41         HILOGI("called LocalCapabilities::AsyncCallback cbExec");
42         ServiceProxy::InvaildInstance();
43         auto proxy = ServiceProxy::GetInstance();
44         if (!proxy) {
45             HILOGI("called LocalCapabilities::AsyncCallback cbExec, failed to get proxy");
46             return NError(errno);
47         }
48         *fd = proxy->GetLocalCapabilities();
49         HILOGI("called LocalCapabilities::AsyncCallback cbExec success");
50         return NError(ERRNO_NOERR);
51     };
52     auto cbCompl = [fd](napi_env env, NError err) -> NVal {
53         HILOGI("called LocalCapabilities::AsyncCallback cbCompl");
54         if (err) {
55             return {env, err.GetNapiErr(env)};
56         }
57         NVal obj = NVal::CreateObject(env);
58         obj.AddProp({NVal::DeclareNapiProperty(BConstants::FD.c_str(), NVal::CreateInt32(env, fd->Release()).val_)});
59         return {obj};
60     };
61 
62     NVal thisVar(env, funcArg.GetThisVar());
63     if (funcArg.GetArgc() == NARG_CNT::ZERO) {
64         HILOGI("called LocalCapabilities::Async::promise");
65         return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_LOCALCAPABILITIES_NAME, cbExec, cbCompl).val_;
66     } else {
67         HILOGI("called LocalCapabilities::Async::callback");
68         NVal cb(env, funcArg[NARG_POS::FIRST]);
69         return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_LOCALCAPABILITIES_NAME, cbExec, cbCompl).val_;
70     }
71 }
72 
AsyncDataList(napi_env env,const NFuncArg & funcArg)73 static napi_value AsyncDataList(napi_env env, const NFuncArg& funcArg)
74 {
75     HILOGD("called LocalCapabilities::AsyncDataList begin");
76     auto [succ, bundles] = Parse::ParseDataList(env, funcArg[NARG_POS::FIRST]);
77     if (!succ) {
78         HILOGE("bundles array invalid.");
79         NError(BError(BError::Codes::SDK_INVAL_ARG, "bundles array invalid.").GetCode()).ThrowErr(env);
80         return nullptr;
81     }
82 
83     auto fd = make_shared<UniqueFd>();
84     auto cbExec = [fd, bundles { move(bundles) }]() -> NError {
85         HILOGI("called LocalCapabilities::AsyncDataList cbExec");
86         ServiceProxy::InvaildInstance();
87         auto proxy = ServiceProxy::GetInstance();
88         if (!proxy) {
89             HILOGI("called LocalCapabilities::AsyncDataList cbExec, failed to get proxy");
90             return NError(errno);
91         }
92         *fd = proxy->GetLocalCapabilitiesIncremental(bundles);
93         return NError(ERRNO_NOERR);
94     };
95     auto cbCompl = [fd](napi_env env, NError err) -> NVal {
96         if (err) {
97             return {env, err.GetNapiErr(env)};
98         }
99         NVal obj = NVal::CreateObject(env);
100         obj.AddProp({NVal::DeclareNapiProperty(BConstants::FD.c_str(), NVal::CreateInt32(env, fd->Release()).val_)});
101         return {obj};
102     };
103 
104     HILOGI("called LocalCapabilities::Async::promise");
105     NVal thisVar(env, funcArg.GetThisVar());
106     return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_LOCALCAPABILITIES_NAME, cbExec, cbCompl).val_;
107 }
108 
Async(napi_env env,napi_callback_info info)109 napi_value PropNOperation::Async(napi_env env, napi_callback_info info)
110 {
111     HILOGD("called LocalCapabilities::Async begin");
112     if (!SAUtils::CheckBackupPermission()) {
113         HILOGE("Has not permission!");
114         NError(E_PERMISSION).ThrowErr(env);
115         return nullptr;
116     }
117     if (!SAUtils::IsSystemApp()) {
118         HILOGE("System App check fail!");
119         NError(E_PERMISSION_SYS).ThrowErr(env);
120         return nullptr;
121     }
122     NFuncArg funcArg(env, info);
123     if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) {
124         HILOGE("Number of arguments unmatched.");
125         NError(BError(BError::Codes::SDK_INVAL_ARG, "Number of arguments unmatched.").GetCode()).ThrowErr(env);
126         return nullptr;
127     }
128 
129     if (funcArg.GetArgc() == 1) {
130         bool result = 0;
131         napi_status status = napi_is_array(env, funcArg[NARG_POS::FIRST], &result);
132         if (status == napi_ok && result) {
133             return AsyncDataList(env, funcArg);
134         }
135     }
136 
137     return AsyncCallback(env, funcArg);
138 }
139 
DoGetBackupInfo(napi_env env,napi_callback_info info)140 napi_value PropNOperation::DoGetBackupInfo(napi_env env, napi_callback_info info)
141 {
142     HILOGD("called DoGetBackupInfo begin");
143     if (!SAUtils::CheckBackupPermission()) {
144         HILOGE("Has not permission!");
145         NError(E_PERMISSION).ThrowErr(env);
146         return nullptr;
147     }
148     if (!SAUtils::IsSystemApp()) {
149         HILOGE("System App check fail!");
150         NError(E_PERMISSION_SYS).ThrowErr(env);
151         return nullptr;
152     }
153     std::string result;
154     NFuncArg funcArg(env, info);
155     if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
156         HILOGE("Number of arguments unmatched.");
157         NError(BError(BError::Codes::SDK_INVAL_ARG, "Number of arguments unmatched.").GetCode()).ThrowErr(env);
158         return nullptr;
159     }
160     NVal jsBundle(env, funcArg[NARG_POS::FIRST]);
161     auto [succ, bundle, size] = jsBundle.ToUTF8String();
162     if (!succ) {
163         HILOGE("First argument is not string.");
164         NError(EINVAL).ThrowErr(env);
165         return nullptr;
166     }
167 
168     ServiceProxy::InvaildInstance();
169     auto proxy = ServiceProxy::GetInstance();
170     if (!proxy) {
171         HILOGE("called DoGetBackupInfo,failed to get proxy");
172         return nullptr;
173     }
174     std::string bundleName = bundle.get();
175     ErrCode errcode = proxy->GetBackupInfo(bundleName, result);
176     if (errcode != 0) {
177         HILOGE("proxy->GetBackupInfo faild.");
178         return nullptr;
179     }
180 
181     napi_value nResult;
182     napi_status status = napi_create_string_utf8(env, result.c_str(), result.size(), &nResult);
183     if (status != napi_ok) {
184         HILOGE("napi_create_string_utf8 faild.");
185         return nullptr;
186     }
187     HILOGI("DoGetBackupInfo success with result: %{public}s", result.c_str());
188     return nResult;
189 }
190 
UpdateSendRate(std::string & bundleName,int32_t sendRate)191 bool PropNOperation::UpdateSendRate(std::string &bundleName, int32_t sendRate)
192 {
193     bool result = false;
194     ServiceProxy::InvaildInstance();
195     auto proxy = ServiceProxy::GetInstance();
196     if (!proxy) {
197         HILOGE("called UpdateSendRate,failed to get proxy");
198         return result;
199     }
200     ErrCode errCode = proxy->UpdateSendRate(bundleName, sendRate, result);
201     if (errCode != 0) {
202         HILOGE("Proxy execute UpdateSendRate failed. errCode:%{public}d", errCode);
203         return result;
204     }
205     return result;
206 }
207 
UpdateTimer(std::string & bundleName,uint32_t timeout)208 bool PropNOperation::UpdateTimer(std::string &bundleName, uint32_t timeout)
209 {
210     bool result = false;
211     ServiceProxy::InvaildInstance();
212     auto proxy = ServiceProxy::GetInstance();
213     if (!proxy) {
214         HILOGE("called DoUpdateTimer,failed to get proxy");
215         return result;
216     }
217     ErrCode errcode = proxy->UpdateTimer(bundleName, timeout, result);
218     if (errcode != 0) {
219         HILOGE("proxy->UpdateTimer faild.");
220         return result;
221     }
222     return result;
223 }
224 
DoUpdateTimer(napi_env env,napi_callback_info info)225 napi_value PropNOperation::DoUpdateTimer(napi_env env, napi_callback_info info)
226 {
227     HILOGD("called DoUpdateTimer begin");
228     if (!SAUtils::CheckBackupPermission()) {
229         HILOGE("Has not permission!");
230         NError(E_PERMISSION).ThrowErr(env);
231         return nullptr;
232     }
233     if (!SAUtils::IsSystemApp()) {
234         HILOGE("System App check fail!");
235         NError(E_PERMISSION_SYS).ThrowErr(env);
236         return nullptr;
237     }
238     NFuncArg funcArg(env, info);
239     if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
240         HILOGE("Number of arguments unmatched.");
241         NError(E_PARAMS).ThrowErr(env);
242         return nullptr;
243     }
244     NVal jsBundleStr(env, funcArg[NARG_POS::FIRST]);
245     auto [succStr, bundle, sizeStr] = jsBundleStr.ToUTF8String();
246     if (!succStr) {
247         HILOGE("First argument is not string.");
248         NError(E_PARAMS).ThrowErr(env);
249         return nullptr;
250     }
251     NVal jsBundleInt(env, funcArg[NARG_POS::SECOND]);
252     auto [succInt, time] = jsBundleInt.ToInt32();
253     if (!succInt || time < 0 || time > static_cast<int32_t>(BConstants::MAX_UPDATE_TIMER)) {
254         HILOGE("Second argument is not number.");
255         NError(E_PARAMS).ThrowErr(env);
256         return nullptr;
257     }
258 
259     std::string bundleName = bundle.get();
260     uint32_t timeout = static_cast<uint32_t>(time);
261     bool result = UpdateTimer(bundleName, timeout);
262 
263     napi_value nResult;
264     napi_status status = napi_get_boolean(env, result, &nResult);
265     if (status != napi_ok) {
266         HILOGE("napi_get_boolean faild.");
267         return nullptr;
268     }
269     HILOGI("DoUpdateTimer success with result: %{public}d", result);
270     return nResult;
271 }
272 
DoUpdateSendRate(napi_env env,napi_callback_info info)273 napi_value PropNOperation::DoUpdateSendRate(napi_env env, napi_callback_info info)
274 {
275     HILOGD("called DoUpdateSendRate begin");
276     if (!SAUtils::CheckBackupPermission()) {
277         HILOGE("Has not permission!");
278         NError(E_PERMISSION).ThrowErr(env);
279         return nullptr;
280     }
281     if (!SAUtils::IsSystemApp()) {
282         HILOGE("System App check fail!");
283         NError(E_PERMISSION_SYS).ThrowErr(env);
284         return nullptr;
285     }
286     NFuncArg funcArg(env, info);
287     if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) {
288         HILOGE("Number of arguments unmatched.");
289         NError(E_PARAMS).ThrowErr(env);
290         return nullptr;
291     }
292     NVal jsBundleStr(env, funcArg[NARG_POS::FIRST]);
293     auto [succStr, bundle, sizeStr] = jsBundleStr.ToUTF8String();
294     if (!succStr) {
295         HILOGE("First argument is not string.");
296         NError(E_PARAMS).ThrowErr(env);
297         return nullptr;
298     }
299     std::string bundleName = bundle.get();
300     NVal jsBundleInt(env, funcArg[NARG_POS::SECOND]);
301     auto [succInt, jsRate] = jsBundleInt.ToInt32();
302     if (!succInt || jsRate < 0) {
303         HILOGE("Second argument is invalid. bundleName:%{public}s", bundleName.c_str());
304         NError(E_PARAMS).ThrowErr(env);
305         return nullptr;
306     }
307     int32_t sendFdRate = static_cast<int32_t>(jsRate);
308     if (sendFdRate > BConstants::MAX_FD_SEND_RATE) {
309         HILOGI("sendFdRate is too large, %{public}d", sendFdRate);
310         sendFdRate = BConstants::MAX_FD_SEND_RATE;
311     }
312     bool result = UpdateSendRate(bundleName, sendFdRate);
313     napi_value nResult;
314     napi_status status = napi_get_boolean(env, result, &nResult);
315     if (status != napi_ok) {
316         HILOGE("napi_get_boolean failed.");
317         return nullptr;
318     }
319     HILOGI("DoUpdateSendRate success with result: %{public}d", result);
320     return nResult;
321 }
322 
DoGetBackupVersion(napi_env env,napi_callback_info info)323 napi_value PropNOperation::DoGetBackupVersion(napi_env env, napi_callback_info info)
324 {
325     HILOGD("called DoGetBackupVersion begin");
326     if (!SAUtils::CheckBackupPermission()) {
327         HILOGE("Has not permission!");
328         NError(E_PERMISSION).ThrowErr(env);
329         return nullptr;
330     }
331     if (!SAUtils::IsSystemApp()) {
332         HILOGE("System App check fail!");
333         NError(E_PERMISSION_SYS).ThrowErr(env);
334         return nullptr;
335     }
336     std::string result = BConstants::BACKUP_VERSION;
337     napi_value nResult;
338     napi_status status = napi_create_string_utf8(env, result.c_str(), result.size(), &nResult);
339     if (status != napi_ok) {
340         HILOGE("napi_create_string_utf8 faild.");
341         return nullptr;
342     }
343     HILOGI("DoGetBackupVersion success with result: %{public}s", result.c_str());
344     return nResult;
345 }
346 } // namespace OHOS::FileManagement::Backup