1 /*
2 * Copyright (c) 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 #include "local_capabilities.h"
16
17 #include "filemgmt_libhilog.h"
18 #include "filemgmt_libn.h"
19 #include "service_proxy.h"
20
21 namespace OHOS::FileManagement::Backup {
22 using namespace std;
23 using namespace LibN;
24
Async(napi_env env,napi_callback_info info)25 napi_value LocalCapabilities::Async(napi_env env, napi_callback_info info)
26 {
27 HILOGI("called LocalCapabilities::Async begin");
28 NFuncArg funcArg(env, info);
29 if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) {
30 HILOGE("Number of arguments unmatched");
31 NError(EINVAL).ThrowErr(env);
32 return nullptr;
33 }
34
35 auto fd = make_shared<UniqueFd>();
36 auto cbExec = [fd]() -> NError {
37 HILOGI("called LocalCapabilities::Async cbExec");
38 auto proxy = ServiceProxy::GetInstance();
39 if (!proxy) {
40 HILOGI("called LocalCapabilities::Async cbExec, failed to get proxy");
41 return NError(errno);
42 }
43 *fd = proxy->GetLocalCapabilities();
44 return NError(ERRNO_NOERR);
45 };
46 auto cbCompl = [fd](napi_env env, NError err) -> NVal {
47 if (err) {
48 return {env, err.GetNapiErr(env)};
49 }
50 NVal obj = NVal::CreateObject(env);
51 obj.AddProp({NVal::DeclareNapiProperty("fd", NVal::CreateInt32(env, fd->Release()).val_)});
52 return {obj};
53 };
54
55 NVal thisVar(env, funcArg.GetThisVar());
56 if (funcArg.GetArgc() == NARG_CNT::ZERO) {
57 HILOGI("called LocalCapabilities::Async::promise");
58 return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_LOCALCAPABILITIES_NAME, cbExec, cbCompl).val_;
59 } else {
60 HILOGI("called LocalCapabilities::Async::callback");
61 NVal cb(env, funcArg[NARG_POS::FIRST]);
62 return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_LOCALCAPABILITIES_NAME, cbExec, cbCompl).val_;
63 }
64 }
65 } // namespace OHOS::FileManagement::Backup