1 /*
2 * Copyright (c) 2025 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 #define LOG_TAG "JSAbility"
16
17 #include <memory>
18
19 #include "aip_log.h"
20 #include "extension_context.h"
21 #include "js_ability.h"
22
23 namespace OHOS {
24 namespace AppDataMgrJsKit {
25 constexpr int API_VERSION_MOD = 20;
Context(std::shared_ptr<AbilityRuntime::Context> stageContext)26 Context::Context(std::shared_ptr<AbilityRuntime::Context> stageContext)
27 {
28 this->stageContext_ = stageContext;
29 isStageMode_ = true;
30 databaseDir_ = stageContext->GetDatabaseDir();
31 bundleName_ = stageContext->GetBundleName();
32 cacheDir_ = stageContext->GetCacheDir();
33 area_ = stageContext->GetArea();
34 auto hapInfo = stageContext->GetHapModuleInfo();
35 if (hapInfo != nullptr) {
36 moduleName_ = hapInfo->moduleName;
37 }
38
39 if (hapInfo == nullptr || hapInfo->proxyDatas.size() != 0) {
40 hasProxyDataConfig_ = true;
41 } else {
42 auto extensionContext = AbilityRuntime::Context::ConvertTo<AbilityRuntime::ExtensionContext>(stageContext);
43 if (extensionContext != nullptr) {
44 auto abilityInfo = extensionContext->GetAbilityInfo();
45 if (abilityInfo != nullptr) {
46 uri_ = abilityInfo->uri;
47 writePermission_ = abilityInfo->writePermission;
48 readPermission_ = abilityInfo->readPermission;
49 AIP_HILOGI("QueryAbilityInfo, uri: %{private}s, readPermission: %{public}s, writePermission: "
50 "%{public}s.",
51 abilityInfo->uri.c_str(), abilityInfo->readPermission.c_str(),
52 abilityInfo->writePermission.c_str());
53 }
54 }
55 }
56 auto appInfo = stageContext->GetApplicationInfo();
57 isSystemAppCalled_ = appInfo == nullptr ? false : appInfo->isSystemApp;
58 }
59
Context(std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext)60 Context::Context(std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext)
61 {
62 databaseDir_ = abilityContext->GetDatabaseDir();
63 cacheDir_ = abilityContext->GetCacheDir();
64 bundleName_ = abilityContext->GetBundleName();
65 area_ = abilityContext->GetArea();
66 auto abilityInfo = abilityContext->GetAbilityInfo();
67 if (abilityInfo != nullptr) {
68 moduleName_ = abilityInfo->moduleName;
69 }
70 }
71
GetDatabaseDir()72 std::string Context::GetDatabaseDir()
73 {
74 return databaseDir_;
75 }
76
GetCacheDir()77 std::string Context::GetCacheDir()
78 {
79 return cacheDir_;
80 }
81
GetSystemDatabaseDir(const std::string & dataGroupId,std::string & databaseDir)82 int Context::GetSystemDatabaseDir(const std::string &dataGroupId, std::string &databaseDir)
83 {
84 return stageContext_->GetSystemDatabaseDir(dataGroupId, false, databaseDir);
85 }
86
GetBundleName()87 std::string Context::GetBundleName()
88 {
89 return bundleName_;
90 }
91
GetModuleName()92 std::string Context::GetModuleName()
93 {
94 return moduleName_;
95 }
96
GetArea() const97 int32_t Context::GetArea() const
98 {
99 return area_;
100 }
GetUri()101 std::string Context::GetUri()
102 {
103 return uri_;
104 }
GetReadPermission()105 std::string Context::GetReadPermission()
106 {
107 return readPermission_;
108 }
GetWritePermission()109 std::string Context::GetWritePermission()
110 {
111 return writePermission_;
112 }
IsSystemAppCalled()113 bool Context::IsSystemAppCalled()
114 {
115 return isSystemAppCalled_;
116 }
117
IsHasProxyDataConfig() const118 bool Context::IsHasProxyDataConfig() const
119 {
120 return hasProxyDataConfig_;
121 }
122
IsStageMode() const123 bool Context::IsStageMode() const
124 {
125 return isStageMode_;
126 }
127
CheckContext(napi_env env,napi_callback_info info)128 bool JSAbility::CheckContext(napi_env env, napi_callback_info info)
129 {
130 size_t argc = 1;
131 napi_value args[1] = { 0 };
132 bool mode = false;
133 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
134 napi_status status = AbilityRuntime::IsStageContext(env, args[0], mode);
135 AIP_HILOGD("Check context as stage mode, mode is %{public}d, status is %{public}d", mode, status == napi_ok);
136 return status == napi_ok;
137 }
138
GetContext(napi_env env,napi_value value)139 std::shared_ptr<Context> JSAbility::GetContext(napi_env env, napi_value value)
140 {
141 bool mode = false;
142 AbilityRuntime::IsStageContext(env, value, mode);
143 if (mode) {
144 return GetStageModeContext(env, value);
145 }
146 return GetCurrentAbility(env, value);
147 }
148
GetStageModeContext(napi_env env,napi_value value)149 std::shared_ptr<Context> JSAbility::GetStageModeContext(napi_env env, napi_value value)
150 {
151 AIP_HILOGD("Get context as stage mode.");
152 auto stageContext = AbilityRuntime::GetStageModeContext(env, value);
153 if (stageContext == nullptr) {
154 AIP_HILOGE("GetStageModeContext failed.");
155 return nullptr;
156 }
157 return std::make_shared<Context>(stageContext);
158 }
159
GetCurrentAbility(napi_env env,napi_value value)160 std::shared_ptr<Context> JSAbility::GetCurrentAbility(napi_env env, napi_value value)
161 {
162 AIP_HILOGD("Get context as feature ability mode.");
163 auto ability = AbilityRuntime::GetCurrentAbility(env);
164 if (ability == nullptr) {
165 AIP_HILOGE("GetCurrentAbility failed.");
166 return nullptr;
167 }
168 auto abilityContext = ability->GetAbilityContext();
169 if (abilityContext == nullptr) {
170 AIP_HILOGE("GetAbilityContext failed.");
171 return nullptr;
172 }
173 return std::make_shared<Context>(abilityContext);
174 }
175
GetHapVersion(napi_env env,napi_value value)176 int32_t JSAbility::GetHapVersion(napi_env env, napi_value value)
177 {
178 auto stageContext = AbilityRuntime::GetStageModeContext(env, value);
179 if (stageContext == nullptr) {
180 AIP_HILOGE("GetStageModeContext failed.");
181 return INVALID_HAP_VERSION ;
182 }
183 auto appInfo = stageContext->GetApplicationInfo();
184 if (appInfo != nullptr) {
185 return appInfo->apiTargetVersion % API_VERSION_MOD;
186 }
187 return INVALID_HAP_VERSION ;
188 }
189 } // namespace AppDataMgrJsKit
190 } // namespace OHOS