• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_ApplicationContextGetResourceDir(const char * moduleName,char * buffer,const int32_t bufferSize,int32_t * writeLength)277 AbilityRuntime_ErrorCode OH_AbilityRuntime_ApplicationContextGetResourceDir(const char* moduleName,
278     char* buffer, const int32_t bufferSize, int32_t* writeLength)
279 {
280     TAG_LOGD(AAFwkTag::APPKIT, "getResourceDir called");
281     if (moduleName == nullptr) {
282         TAG_LOGE(AAFwkTag::APPKIT, "moduleName is null");
283         return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
284     }
285     if (std::strlen(moduleName) == 0) {
286         TAG_LOGE(AAFwkTag::APPKIT, "moduleName is empty");
287         return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
288     }
289     auto ret = CheckParameters(buffer, writeLength);
290     if (ret != ABILITY_RUNTIME_ERROR_CODE_NO_ERROR) {
291         return ret;
292     }
293     const auto appContext = Context::GetApplicationContext();
294     if (appContext == nullptr) {
295         TAG_LOGE(AAFwkTag::APPKIT, "appContext is null");
296         return ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST;
297     }
298     if (!appContext->IsModuleExist(moduleName)) {
299         TAG_LOGE(AAFwkTag::APPKIT, "moduleName is not exist");
300         return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
301     }
302     const std::string resourceDir = appContext->GetResourceDir(moduleName);
303     return WriteStringToBuffer(resourceDir, buffer, bufferSize, writeLength);
304 }
305 
OH_AbilityRuntime_StartSelfUIAbility(AbilityBase_Want * want)306 AbilityRuntime_ErrorCode OH_AbilityRuntime_StartSelfUIAbility(AbilityBase_Want *want)
307 {
308     TAG_LOGD(AAFwkTag::APPKIT, "StartSelfUIAbility called");
309     auto ret = CheckWant(want);
310     if (ret != ABILITY_RUNTIME_ERROR_CODE_NO_ERROR) {
311         TAG_LOGE(AAFwkTag::APPKIT, "CheckWant failed: %{public}d", ret);
312         return ret;
313     }
314     Want abilityWant;
315     AbilityBase_ErrorCode errCode = CWantManager::TransformToWant(*want, false, abilityWant);
316     if (errCode != ABILITY_BASE_ERROR_CODE_NO_ERROR) {
317         TAG_LOGE(AAFwkTag::APPKIT, "transform error:%{public}d", errCode);
318         return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
319     }
320     return ConvertToCommonBusinessErrorCode(AbilityManagerClient::GetInstance()->StartSelfUIAbility(abilityWant));
321 }
322 
OH_AbilityRuntime_StartSelfUIAbilityWithStartOptions(AbilityBase_Want * want,AbilityRuntime_StartOptions * options)323 AbilityRuntime_ErrorCode OH_AbilityRuntime_StartSelfUIAbilityWithStartOptions(AbilityBase_Want *want,
324     AbilityRuntime_StartOptions *options)
325 {
326     TAG_LOGD(AAFwkTag::APPKIT, "StartSelfUIAbilityWithStartOptions called");
327     auto ret = CheckWant(want);
328     if (ret != ABILITY_RUNTIME_ERROR_CODE_NO_ERROR) {
329         TAG_LOGE(AAFwkTag::APPKIT, "CheckWant failed: %{public}d", ret);
330         return ret;
331     }
332     if (options == nullptr) {
333         TAG_LOGE(AAFwkTag::APPKIT, "null options");
334         return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
335     }
336     Want abilityWant;
337     AbilityBase_ErrorCode errCode = CWantManager::TransformToWant(*want, false, abilityWant);
338     if (errCode != ABILITY_BASE_ERROR_CODE_NO_ERROR) {
339         TAG_LOGE(AAFwkTag::APPKIT, "transform error:%{public}d", errCode);
340         return ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID;
341     }
342     OHOS::AAFwk::StartOptions startOptions = options->GetInnerStartOptions();
343     return ConvertToAPI18BusinessErrorCode(AbilityManagerClient::GetInstance()->StartSelfUIAbilityWithStartOptions(
344         abilityWant, startOptions));
345 }