1 /*
2 * Copyright (c) 2021-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 "ability_helper.h"
17
18 #include "log.h"
19 #include "napi/n_func_arg.h"
20 #include "napi/uni_header.h"
21
22 namespace OHOS {
23 namespace DistributedFS {
24 using namespace std;
25 using OHOS::AppExecFwk::Ability;
26 using OHOS::AppExecFwk::AbilityContext;
27
GetJsAbility(napi_env env)28 Ability* AbilityHelper::GetJsAbility(napi_env env)
29 {
30 napi_value global = nullptr;
31 napi_value abilityContext = nullptr;
32 napi_status status = napi_get_global(env, &global);
33 if (status != napi_ok || global == nullptr) {
34 HILOGE("Cannot get global instance for %{public}d", status);
35 return nullptr;
36 }
37
38 status = napi_get_named_property(env, global, "ability", &abilityContext);
39 if (status != napi_ok || abilityContext == nullptr) {
40 HILOGE("Cannot get ability context for %{public}d", status);
41 return nullptr;
42 }
43
44 Ability *ability = nullptr;
45 status = napi_get_value_external(env, abilityContext, (void **)&ability);
46 if (status != napi_ok) {
47 HILOGE("Get ability form property failed for %{public}d", status);
48 return nullptr;
49 }
50
51 return ability;
52 }
53 } // namespace DistributedFS
54 } // namespace OHOS