1 /*
2 * Copyright (c) 2021 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
33 napi_status status = napi_get_global(env, &global);
34 if (status != napi_ok || global == nullptr) {
35 HILOGE("Cannot get global instance for %{public}d", status);
36 return nullptr;
37 }
38
39 status = napi_get_named_property(env, global, "ability", &abilityContext);
40 if (status != napi_ok || abilityContext == nullptr) {
41 HILOGE("Cannot get ability context for %{public}d", status);
42 return nullptr;
43 }
44
45 Ability *ability = nullptr;
46 status = napi_get_value_external(env, abilityContext, (void **)&ability);
47 if (status != napi_ok || ability == nullptr) {
48 HILOGE("Get ability form property failed for %{public}d", status);
49 }
50
51 return ability;
52 }
53 } // namespace DistributedFS
54 } // namespace OHOS