1 /*
2 * Copyright (c) 2024-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
16 #include "application_context.h"
17
18 #include "ability_business_error_utils.h"
19 #include "ability_manager_client.h"
20 #include "context.h"
21 #include "context/application_context.h"
22 #include "hilog_tag_wrapper.h"
23 #include "start_options_impl.h"
24 #include "want_manager.h"
25 #include "want_utils.h"
26
27 using namespace OHOS::AbilityRuntime;
28 using namespace OHOS::AAFwk;
29 using namespace OHOS;
30
31 namespace {
WriteStringToBuffer(const std::string & src,char * buffer,const int32_t bufferSize,int32_t * writeLength)32 AbilityRuntime_ErrorCode WriteStringToBuffer(
33 const std::string &src, char* buffer, const int32_t bufferSize, int32_t* writeLength)
34 {
35 const auto srcLength = static_cast<int32_t>(src.length());
36 if (bufferSize - 1 < srcLength) {
37 TAG_LOGE(AAFwkTag::APPKIT, "the buffer size is less than the minimum buffer size");
38 return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
39 }
40 src.copy(buffer, srcLength);
41 buffer[srcLength] = '\0';
42 *writeLength = srcLength;
43 return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
44 }
45
CheckParameters(char * buffer,int32_t * writeLength)46 AbilityRuntime_ErrorCode CheckParameters(char* buffer, int32_t* writeLength)
47 {
48 if (buffer == nullptr) {
49 TAG_LOGE(AAFwkTag::APPKIT, "buffer is null");
50 return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
51 }
52 if (writeLength == nullptr) {
53 TAG_LOGE(AAFwkTag::APPKIT, "writeLength is null");
54 return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
55 }
56 return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
57 }
58 }
59
OH_AbilityRuntime_ApplicationContextGetCacheDir(char * buffer,const int32_t bufferSize,int32_t * writeLength)60 AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetCacheDir(
61 char* buffer, const int32_t bufferSize, int32_t* writeLength)
62 {
63 TAG_LOGD(AAFwkTag::APPKIT, "called");
64 if (buffer == nullptr) {
65 TAG_LOGE(AAFwkTag::APPKIT, "buffer is null");
66 return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
67 }
68 if (writeLength == nullptr) {
69 TAG_LOGE(AAFwkTag::APPKIT, "writeLength is null");
70 return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
71 }
72
73 const auto appContext = Context::GetApplicationContext();
74 if (appContext == nullptr) {
75 TAG_LOGE(AAFwkTag::APPKIT, "appContext is null");
76 return ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST;
77 }
78 const std::string cacheDir = appContext->GetCacheDir();
79 if (cacheDir.empty()) {
80 TAG_LOGE(AAFwkTag::APPKIT, "cacheDir is empty");
81 return ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST;
82 }
83 return WriteStringToBuffer(cacheDir, buffer, bufferSize, writeLength);
84 }
85
OH_AbilityRuntime_ApplicationContextGetAreaMode(AbilityRuntime_AreaMode * areaMode)86 AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetAreaMode(AbilityRuntime_AreaMode* areaMode)
87 {
88 TAG_LOGD(AAFwkTag::APPKIT, "called");
89 if (areaMode == nullptr) {
90 TAG_LOGE(AAFwkTag::APPKIT, "areaMode is null");
91 return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
92 }
93
94 const auto appContext = Context::GetApplicationContext();
95 if (appContext == nullptr) {
96 TAG_LOGE(AAFwkTag::APPKIT, "appContext is null");
97 return ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST;
98 }
99 int32_t area = appContext->GetArea();
100 *areaMode = static_cast<AbilityRuntime_AreaMode>(area);
101 return ABILITY_RUNTIME_ERROR_CODE_NO_ERROR;
102 }
103
OH_AbilityRuntime_ApplicationContextGetBundleName(char * buffer,const int32_t bufferSize,int32_t * writeLength)104 AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetBundleName(
105 char* buffer, const int32_t bufferSize, int32_t* writeLength)
106 {
107 TAG_LOGD(AAFwkTag::APPKIT, "called");
108 if (buffer == nullptr) {
109 TAG_LOGE(AAFwkTag::APPKIT, "buffer is null");
110 return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
111 }
112 if (writeLength == nullptr) {
113 TAG_LOGE(AAFwkTag::APPKIT, "writeLength is null");
114 return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
115 }
116
117 const auto appContext = Context::GetApplicationContext();
118 if (appContext == nullptr) {
119 TAG_LOGE(AAFwkTag::APPKIT, "appContext is null");
120 return ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST;
121 }
122 const std::string bundleName = appContext->GetBundleName();
123 if (bundleName.empty()) {
124 TAG_LOGE(AAFwkTag::APPKIT, "bundleName is empty");
125 return ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST;
126 }
127 return WriteStringToBuffer(bundleName, buffer, bufferSize, writeLength);
128 }
129
OH_AbilityRuntime_ApplicationContextGetTempDir(char * buffer,const int32_t bufferSize,int32_t * writeLength)130 AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetTempDir(
131 char* buffer, const int32_t bufferSize, int32_t* writeLength)
132 {
133 TAG_LOGD(AAFwkTag::APPKIT, "getTempDir called");
134 auto ret = CheckParameters(buffer, writeLength);
135 if (ret != ABILITY_RUNTIME_ERROR_CODE_NO_ERROR) {
136 return ret;
137 }
138 const auto appContext = Context::GetApplicationContext();
139 if (appContext == nullptr) {
140 TAG_LOGE(AAFwkTag::APPKIT, "appContext is null");
141 return ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST;
142 }
143 const std::string tempDir = appContext->GetTempDir();
144 if (tempDir.empty()) {
145 TAG_LOGE(AAFwkTag::APPKIT, "tempDir is empty");
146 return ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST;
147 }
148 return WriteStringToBuffer(tempDir, buffer, bufferSize, writeLength);
149 }
150
OH_AbilityRuntime_ApplicationContextGetFilesDir(char * buffer,const int32_t bufferSize,int32_t * writeLength)151 AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetFilesDir(
152 char* buffer, const int32_t bufferSize, int32_t* writeLength)
153 {
154 TAG_LOGD(AAFwkTag::APPKIT, "getFilesDir called");
155 auto ret = CheckParameters(buffer, writeLength);
156 if (ret != ABILITY_RUNTIME_ERROR_CODE_NO_ERROR) {
157 return ret;
158 }
159 const auto appContext = Context::GetApplicationContext();
160 if (appContext == nullptr) {
161 TAG_LOGE(AAFwkTag::APPKIT, "appContext is null");
162 return ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST;
163 }
164 const std::string filesDir = appContext->GetFilesDir();
165 if (filesDir.empty()) {
166 TAG_LOGE(AAFwkTag::APPKIT, "filesDir is empty");
167 return ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST;
168 }
169 return WriteStringToBuffer(filesDir, buffer, bufferSize, writeLength);
170 }
171
OH_AbilityRuntime_ApplicationContextGetDatabaseDir(char * buffer,const int32_t bufferSize,int32_t * writeLength)172 AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetDatabaseDir(
173 char* buffer, const int32_t bufferSize, int32_t* writeLength)
174 {
175 TAG_LOGD(AAFwkTag::APPKIT, "getDatabaseDir called");
176 auto ret = CheckParameters(buffer, writeLength);
177 if (ret != ABILITY_RUNTIME_ERROR_CODE_NO_ERROR) {
178 return ret;
179 }
180 const auto appContext = Context::GetApplicationContext();
181 if (appContext == nullptr) {
182 TAG_LOGE(AAFwkTag::APPKIT, "appContext is null");
183 return ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST;
184 }
185 const std::string databaseDir = appContext->GetDatabaseDir();
186 if (databaseDir.empty()) {
187 TAG_LOGE(AAFwkTag::APPKIT, "databaseDir is empty");
188 return ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST;
189 }
190 return WriteStringToBuffer(databaseDir, buffer, bufferSize, writeLength);
191 }
192
OH_AbilityRuntime_ApplicationContextGetPreferencesDir(char * buffer,const int32_t bufferSize,int32_t * writeLength)193 AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetPreferencesDir(
194 char* buffer, const int32_t bufferSize, int32_t* writeLength)
195 {
196 TAG_LOGD(AAFwkTag::APPKIT, "getPreferencesDir called");
197 auto ret = CheckParameters(buffer, writeLength);
198 if (ret != ABILITY_RUNTIME_ERROR_CODE_NO_ERROR) {
199 return ret;
200 }
201 const auto appContext = Context::GetApplicationContext();
202 if (appContext == nullptr) {
203 TAG_LOGE(AAFwkTag::APPKIT, "appContext is null");
204 return ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST;
205 }
206 const std::string preferencesDir = appContext->GetPreferencesDir();
207 if (preferencesDir.empty()) {
208 TAG_LOGE(AAFwkTag::APPKIT, "preferencesDir is empty");
209 return ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST;
210 }
211 return WriteStringToBuffer(preferencesDir, buffer, bufferSize, writeLength);
212 }
213
OH_AbilityRuntime_ApplicationContextGetBundleCodeDir(char * buffer,const int32_t bufferSize,int32_t * writeLength)214 AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetBundleCodeDir(
215 char* buffer, const int32_t bufferSize, int32_t* writeLength)
216 {
217 TAG_LOGD(AAFwkTag::APPKIT, "getBundleCodeDir called");
218 auto ret = CheckParameters(buffer, writeLength);
219 if (ret != ABILITY_RUNTIME_ERROR_CODE_NO_ERROR) {
220 return ret;
221 }
222 const auto appContext = Context::GetApplicationContext();
223 if (appContext == nullptr) {
224 TAG_LOGE(AAFwkTag::APPKIT, "appContext is null");
225 return ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST;
226 }
227 const std::string bundleCodeDir = appContext->GetBundleCodeDir();
228 if (bundleCodeDir.empty()) {
229 TAG_LOGE(AAFwkTag::APPKIT, "bundleCodeDir is empty");
230 return ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST;
231 }
232 return WriteStringToBuffer(bundleCodeDir, buffer, bufferSize, writeLength);
233 }
234
OH_AbilityRuntime_ApplicationContextGetDistributedFilesDir(char * buffer,const int32_t bufferSize,int32_t * writeLength)235 AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetDistributedFilesDir(
236 char* buffer, const int32_t bufferSize, int32_t* writeLength)
237 {
238 TAG_LOGD(AAFwkTag::APPKIT, "getDistributedFilesDir called");
239 auto ret = CheckParameters(buffer, writeLength);
240 if (ret != ABILITY_RUNTIME_ERROR_CODE_NO_ERROR) {
241 return ret;
242 }
243 const auto appContext = Context::GetApplicationContext();
244 if (appContext == nullptr) {
245 TAG_LOGE(AAFwkTag::APPKIT, "appContext is null");
246 return ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST;
247 }
248 const std::string distributedFilesDir = appContext->GetDistributedFilesDir();
249 if (distributedFilesDir.empty()) {
250 TAG_LOGE(AAFwkTag::APPKIT, "distributedFilesDir is empty");
251 return ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST;
252 }
253 return WriteStringToBuffer(distributedFilesDir, buffer, bufferSize, writeLength);
254 }
255
OH_AbilityRuntime_ApplicationContextGetCloudFileDir(char * buffer,const int32_t bufferSize,int32_t * writeLength)256 AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetCloudFileDir(
257 char* buffer, const int32_t bufferSize, int32_t* writeLength)
258 {
259 TAG_LOGD(AAFwkTag::APPKIT, "getCloudFileDir called");
260 auto ret = CheckParameters(buffer, writeLength);
261 if (ret != ABILITY_RUNTIME_ERROR_CODE_NO_ERROR) {
262 return ret;
263 }
264 const auto appContext = Context::GetApplicationContext();
265 if (appContext == nullptr) {
266 TAG_LOGE(AAFwkTag::APPKIT, "appContext is null");
267 return ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST;
268 }
269 const std::string cloudFileDir = appContext->GetCloudFileDir();
270 if (cloudFileDir.empty()) {
271 TAG_LOGE(AAFwkTag::APPKIT, "cloudFileDir is empty");
272 return ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST;
273 }
274 return WriteStringToBuffer(cloudFileDir, buffer, bufferSize, writeLength);
275 }
276
OH_AbilityRuntime_StartSelfUIAbility(AbilityBase_Want * want)277 AbilityRuntime_ErrorCode OH_AbilityRuntime_StartSelfUIAbility(AbilityBase_Want *want)
278 {
279 TAG_LOGD(AAFwkTag::APPKIT, "startSelfUIAbility called");
280 auto ret = CheckWant(want);
281 if (ret != ABILITY_RUNTIME_ERROR_CODE_NO_ERROR) {
282 TAG_LOGE(AAFwkTag::APPKIT, "CheckWant failed: %{public}d", ret);
283 return ret;
284 }
285 Want abilityWant;
286 AbilityBase_ErrorCode errCode = CWantManager::TransformToWant(*want, false, abilityWant);
287 if (errCode != ABILITY_BASE_ERROR_CODE_NO_ERROR) {
288 TAG_LOGE(AAFwkTag::APPKIT, "transform error:%{public}d", errCode);
289 return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
290 }
291 return ConvertToCommonBusinessErrorCode(AbilityManagerClient::GetInstance()->StartSelfUIAbility(abilityWant));
292 }
293
OH_AbilityRuntime_StartSelfUIAbilityWithStartOptions(AbilityBase_Want * want,AbilityRuntime_StartOptions * options)294 AbilityRuntime_ErrorCode OH_AbilityRuntime_StartSelfUIAbilityWithStartOptions(AbilityBase_Want *want,
295 AbilityRuntime_StartOptions *options)
296 {
297 TAG_LOGD(AAFwkTag::APPKIT, "startSelfUIAbility called");
298 auto ret = CheckWant(want);
299 if (ret != ABILITY_RUNTIME_ERROR_CODE_NO_ERROR) {
300 TAG_LOGE(AAFwkTag::APPKIT, "CheckWant failed: %{public}d", ret);
301 return ret;
302 }
303 if (options == nullptr) {
304 TAG_LOGE(AAFwkTag::APPKIT, "null options");
305 return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
306 }
307 Want abilityWant;
308 AbilityBase_ErrorCode errCode = CWantManager::TransformToWant(*want, false, abilityWant);
309 if (errCode != ABILITY_BASE_ERROR_CODE_NO_ERROR) {
310 TAG_LOGE(AAFwkTag::APPKIT, "transform error:%{public}d", errCode);
311 return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
312 }
313 OHOS::AAFwk::StartOptions startOptions = options->GetInnerStartOptions();
314 return ConvertToAPI18BusinessErrorCode(AbilityManagerClient::GetInstance()->StartSelfUIAbilityWithStartOptions(
315 abilityWant, startOptions));
316 }