• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
17 
18 #include "ability.h"
19 
20 #include "napi/native_api.h"
21 #include "napi/native_node_api.h"
22 
23 #define GET_PARAMS(env, info, num) \
24     size_t argc = num;             \
25     napi_value argv[num];          \
26     napi_value thisVar = nullptr;  \
27     void* data = nullptr;          \
28     napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)
29 
30 /*
31  * Get bundle path
32  */
JSAbilityGetBundlePath(napi_env env,napi_callback_info info)33 static napi_value JSAbilityGetBundlePath(napi_env env, napi_callback_info info)
34 {
35     GET_PARAMS(env, info, 0);
36     // get global value
37     napi_value global = nullptr;
38     napi_get_global(env, &global);
39 
40     // get ability
41     napi_value abilityObj = nullptr;
42     napi_get_named_property(env, global, "ability", &abilityObj);
43 
44     // get ability pointer
45     OHOS::AppExecFwk::Ability* ability = nullptr;
46     napi_get_value_external(env, abilityObj, (void**)&ability);
47 
48     // get bundle path
49     std::string path = ability->GetBundleCodePath();
50     napi_value bundlePath = nullptr;
51     napi_create_string_utf8(env, path.c_str(), path.length(), &bundlePath);
52     return bundlePath;
53 }
54 
AbilityExport(napi_env env,napi_value exports)55 static napi_value AbilityExport(napi_env env, napi_value exports)
56 {
57     static napi_property_descriptor desc[] = {
58         DECLARE_NAPI_FUNCTION("getBundlePath", JSAbilityGetBundlePath),
59     };
60     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
61     return exports;
62 }
63 
64 // ability module
65 static napi_module abilityModule = {
66     .nm_version = 1,
67     .nm_flags = 0,
68     .nm_filename = nullptr,
69     .nm_register_func = AbilityExport,
70     .nm_modname = "ability",
71     .nm_priv = ((void*)0),
72     .reserved = { 0 },
73 };
74 
75 // ability module register
AbilityRegister()76 extern "C" __attribute__((constructor)) void AbilityRegister()
77 {
78     napi_module_register(&abilityModule);
79 }