• 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 "ability_stage_context.h"
17 
18 #include "context.h"
19 #include "context_impl.h"
20 #include "hilog_tag_wrapper.h"
21 #include "resource_manager.h"
22 
23 namespace OHOS {
24 namespace AbilityRuntime {
25 const size_t AbilityStageContext::CONTEXT_TYPE_ID(std::hash<const char*> {} ("AbilityStageContext"));
26 
AbilityStageContext()27 AbilityStageContext::AbilityStageContext()
28 {
29     contextImpl_ = std::make_shared<ContextImpl>();
30 }
31 
SetParentContext(const std::shared_ptr<Context> & context)32 void AbilityStageContext::SetParentContext(const std::shared_ptr<Context> &context)
33 {
34     if (contextImpl_ == nullptr) {
35         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
36         return;
37     }
38 
39     contextImpl_->SetParentContext(context);
40 }
41 
InitHapModuleInfo(const std::shared_ptr<AppExecFwk::AbilityInfo> & abilityInfo)42 void AbilityStageContext::InitHapModuleInfo(const std::shared_ptr<AppExecFwk::AbilityInfo> &abilityInfo)
43 {
44     if (contextImpl_ == nullptr) {
45         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
46         return;
47     }
48 
49     contextImpl_->InitHapModuleInfo(abilityInfo);
50 }
51 
InitPluginHapModuleInfo(const std::shared_ptr<AppExecFwk::AbilityInfo> & abilityInfo,const std::string & hostBundleName)52 void AbilityStageContext::InitPluginHapModuleInfo(const std::shared_ptr<AppExecFwk::AbilityInfo> &abilityInfo,
53     const std::string &hostBundleName)
54 {
55     if (contextImpl_ == nullptr) {
56         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
57         return;
58     }
59 
60     contextImpl_->InitPluginHapModuleInfo(abilityInfo, hostBundleName);
61 }
62 
SetIsPlugin(bool isPlugin)63 void AbilityStageContext::SetIsPlugin(bool isPlugin)
64 {
65     if (contextImpl_ == nullptr) {
66         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
67         return;
68     }
69     contextImpl_->isPlugin_ = isPlugin;
70 }
71 
InitHapModuleInfo(const AppExecFwk::HapModuleInfo & hapModuleInfo)72 void AbilityStageContext::InitHapModuleInfo(const AppExecFwk::HapModuleInfo &hapModuleInfo)
73 {
74     if (contextImpl_ == nullptr) {
75         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
76         return;
77     }
78 
79     contextImpl_->InitHapModuleInfo(hapModuleInfo);
80 }
81 
GetHapModuleInfo() const82 std::shared_ptr<AppExecFwk::HapModuleInfo> AbilityStageContext::GetHapModuleInfo() const
83 {
84     if (contextImpl_ == nullptr) {
85         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
86         return nullptr;
87     }
88 
89     return contextImpl_->GetHapModuleInfo();
90 }
91 
SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> & config)92 void AbilityStageContext::SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> &config)
93 {
94     if (contextImpl_ == nullptr) {
95         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
96         return;
97     }
98 
99     contextImpl_->SetConfiguration(config);
100 }
101 
GetConfiguration() const102 std::shared_ptr<AppExecFwk::Configuration> AbilityStageContext::GetConfiguration() const
103 {
104     if (contextImpl_ == nullptr) {
105         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
106         return nullptr;
107     }
108 
109     return contextImpl_->GetConfiguration();
110 }
111 
SetResourceManager(const std::shared_ptr<Global::Resource::ResourceManager> & resourceManager)112 void AbilityStageContext::SetResourceManager(const std::shared_ptr<Global::Resource::ResourceManager> &resourceManager)
113 {
114     if (contextImpl_ == nullptr) {
115         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
116         return;
117     }
118 
119     contextImpl_->SetResourceManager(resourceManager);
120 }
121 
GetResourceManager() const122 std::shared_ptr<Global::Resource::ResourceManager> AbilityStageContext::GetResourceManager() const
123 {
124     if (contextImpl_ == nullptr) {
125         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
126         return nullptr;
127     }
128 
129     return contextImpl_->GetResourceManager();
130 }
131 
GetBundleName() const132 std::string AbilityStageContext::GetBundleName() const
133 {
134     if (contextImpl_ == nullptr) {
135         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
136         return {};
137     }
138 
139     return contextImpl_->GetBundleName();
140 }
141 
GetApplicationInfo() const142 std::shared_ptr<AppExecFwk::ApplicationInfo> AbilityStageContext::GetApplicationInfo() const
143 {
144     if (contextImpl_ == nullptr) {
145         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
146         return nullptr;
147     }
148 
149     return contextImpl_->GetApplicationInfo();
150 }
151 
CreatePluginContext(const std::string & pluginBundleName,const std::string & moduleName,std::shared_ptr<Context> inputContext)152 std::shared_ptr<Context> AbilityStageContext::CreatePluginContext(const std::string &pluginBundleName,
153     const std::string &moduleName, std::shared_ptr<Context> inputContext)
154 {
155     if (contextImpl_ == nullptr) {
156         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
157         return nullptr;
158     }
159 
160     return contextImpl_->CreatePluginContext(pluginBundleName, moduleName, inputContext);
161 }
162 
CreateBundleContext(const std::string & bundleName)163 std::shared_ptr<Context> AbilityStageContext::CreateBundleContext(const std::string &bundleName)
164 {
165     if (contextImpl_ == nullptr) {
166         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
167         return nullptr;
168     }
169 
170     return contextImpl_->CreateBundleContext(bundleName);
171 }
172 
CreateModuleContext(const std::string & moduleName)173 std::shared_ptr<Context> AbilityStageContext::CreateModuleContext(const std::string &moduleName)
174 {
175     if (contextImpl_ == nullptr) {
176         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
177         return nullptr;
178     }
179 
180     return contextImpl_->CreateModuleContext(moduleName);
181 }
182 
CreateModuleContext(const std::string & bundleName,const std::string & moduleName)183 std::shared_ptr<Context> AbilityStageContext::CreateModuleContext(const std::string &bundleName,
184     const std::string &moduleName)
185 {
186     if (contextImpl_ == nullptr) {
187         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
188         return nullptr;
189     }
190 
191     return contextImpl_->CreateModuleContext(bundleName, moduleName);
192 }
193 
CreateModuleResourceManager(const std::string & bundleName,const std::string & moduleName)194 std::shared_ptr<Global::Resource::ResourceManager> AbilityStageContext::CreateModuleResourceManager(
195     const std::string &bundleName, const std::string &moduleName)
196 {
197     if (contextImpl_ == nullptr) {
198         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
199         return nullptr;
200     }
201 
202     return contextImpl_->CreateModuleResourceManager(bundleName, moduleName);
203 }
204 
CreateSystemHspModuleResourceManager(const std::string & bundleName,const std::string & moduleName,std::shared_ptr<Global::Resource::ResourceManager> & resourceManager)205 int32_t AbilityStageContext::CreateSystemHspModuleResourceManager(const std::string &bundleName,
206     const std::string &moduleName, std::shared_ptr<Global::Resource::ResourceManager> &resourceManager)
207 {
208     if (contextImpl_ == nullptr) {
209         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
210         return ERR_INVALID_VALUE;
211     }
212 
213     return contextImpl_->CreateSystemHspModuleResourceManager(bundleName, moduleName, resourceManager);
214 }
215 
GetBundleCodePath() const216 std::string AbilityStageContext::GetBundleCodePath() const
217 {
218     if (contextImpl_ == nullptr) {
219         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
220         return {};
221     }
222 
223     return contextImpl_->GetBundleCodePath();
224 }
225 
GetBundleCodeDir()226 std::string AbilityStageContext::GetBundleCodeDir()
227 {
228     if (contextImpl_ == nullptr) {
229         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
230         return {};
231     }
232 
233     return contextImpl_->GetBundleCodeDir();
234 }
235 
GetCacheDir()236 std::string AbilityStageContext::GetCacheDir()
237 {
238     if (contextImpl_ == nullptr) {
239         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
240         return {};
241     }
242 
243     return contextImpl_->GetCacheDir();
244 }
245 
GetTempDir()246 std::string AbilityStageContext::GetTempDir()
247 {
248     if (contextImpl_ == nullptr) {
249         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
250         return {};
251     }
252 
253     return contextImpl_->GetTempDir();
254 }
255 
GetFilesDir()256 std::string AbilityStageContext::GetFilesDir()
257 {
258     if (contextImpl_ == nullptr) {
259         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
260         return {};
261     }
262 
263     return contextImpl_->GetFilesDir();
264 }
265 
GetResourceDir(const std::string & moduleName)266 std::string AbilityStageContext::GetResourceDir(const std::string &moduleName)
267 {
268     if (contextImpl_ == nullptr) {
269         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
270         return {};
271     }
272 
273     return contextImpl_->GetResourceDir(moduleName);
274 }
275 
GetDatabaseDir()276 std::string AbilityStageContext::GetDatabaseDir()
277 {
278     if (contextImpl_ == nullptr) {
279         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
280         return {};
281     }
282 
283     return contextImpl_->GetDatabaseDir();
284 }
285 
GetPreferencesDir()286 std::string AbilityStageContext::GetPreferencesDir()
287 {
288     if (contextImpl_ == nullptr) {
289         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
290         return {};
291     }
292 
293     return contextImpl_->GetPreferencesDir();
294 }
295 
GetGroupDir(std::string groupId)296 std::string AbilityStageContext::GetGroupDir(std::string groupId)
297 {
298     if (contextImpl_ == nullptr) {
299         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
300         return {};
301     }
302 
303     return contextImpl_->GetGroupDir(groupId);
304 }
305 
GetSystemDatabaseDir(const std::string & groupId,bool checkExist,std::string & databaseDir)306 int32_t AbilityStageContext::GetSystemDatabaseDir(const std::string &groupId, bool checkExist, std::string &databaseDir)
307 {
308     if (contextImpl_ == nullptr) {
309         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
310         return ERR_INVALID_VALUE;
311     }
312 
313     return contextImpl_->GetSystemDatabaseDir(groupId, checkExist, databaseDir);
314 }
315 
GetSystemPreferencesDir(const std::string & groupId,bool checkExist,std::string & preferencesDir)316 int32_t AbilityStageContext::GetSystemPreferencesDir(const std::string &groupId, bool checkExist,
317     std::string &preferencesDir)
318 {
319     if (contextImpl_ == nullptr) {
320         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
321         return ERR_INVALID_VALUE;
322     }
323 
324     return contextImpl_->GetSystemPreferencesDir(groupId, checkExist, preferencesDir);
325 }
326 
GetDistributedFilesDir()327 std::string AbilityStageContext::GetDistributedFilesDir()
328 {
329     if (contextImpl_ == nullptr) {
330         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
331         return {};
332     }
333 
334     return contextImpl_->GetDistributedFilesDir();
335 }
336 
GetCloudFileDir()337 std::string AbilityStageContext::GetCloudFileDir()
338 {
339     if (contextImpl_ == nullptr) {
340         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
341         return {};
342     }
343 
344     return contextImpl_->GetCloudFileDir();
345 }
346 
GetBaseDir() const347 std::string AbilityStageContext::GetBaseDir() const
348 {
349     if (contextImpl_ == nullptr) {
350         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
351         return {};
352     }
353 
354     return contextImpl_->GetBaseDir();
355 }
356 
IsUpdatingConfigurations()357 bool AbilityStageContext::IsUpdatingConfigurations()
358 {
359     if (contextImpl_ == nullptr) {
360         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
361         return false;
362     }
363 
364     return contextImpl_->IsUpdatingConfigurations();
365 }
366 
PrintDrawnCompleted()367 bool AbilityStageContext::PrintDrawnCompleted()
368 {
369     if (contextImpl_ == nullptr) {
370         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
371         return false;
372     }
373 
374     return contextImpl_->PrintDrawnCompleted();
375 }
376 
GetToken()377 sptr<IRemoteObject> AbilityStageContext::GetToken()
378 {
379     if (contextImpl_ == nullptr) {
380         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
381         return nullptr;
382     }
383 
384     return contextImpl_->GetToken();
385 }
386 
SetToken(const sptr<IRemoteObject> & token)387 void AbilityStageContext::SetToken(const sptr<IRemoteObject> &token)
388 {
389     if (contextImpl_ == nullptr) {
390         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
391         return;
392     }
393 
394     contextImpl_->SetToken(token);
395 }
396 
SwitchArea(int mode)397 void AbilityStageContext::SwitchArea(int mode)
398 {
399     if (contextImpl_ == nullptr) {
400         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
401         return;
402     }
403 
404     contextImpl_->SwitchArea(mode);
405 }
406 
GetArea()407 int AbilityStageContext::GetArea()
408 {
409     if (contextImpl_ == nullptr) {
410         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
411         return ContextImpl::EL_DEFAULT;
412     }
413 
414     return contextImpl_->GetArea();
415 }
416 
GetProcessName()417 std::string AbilityStageContext::GetProcessName()
418 {
419     if (contextImpl_ == nullptr) {
420         TAG_LOGE(AAFwkTag::APPKIT, "Invalid contextImpl");
421         return {};
422     }
423 
424     return contextImpl_->GetProcessName();
425 }
426 
SetProcessName(const std::string & processName)427 void AbilityStageContext::SetProcessName(const std::string &processName)
428 {
429     if (contextImpl_ == nullptr) {
430         TAG_LOGE(AAFwkTag::APPKIT, "Invalid contextImpl");
431         return;
432     }
433 
434     contextImpl_->SetProcessName(processName);
435 }
436 
GetDeviceType() const437 Global::Resource::DeviceType AbilityStageContext::GetDeviceType() const
438 {
439     if (contextImpl_ == nullptr) {
440         TAG_LOGE(AAFwkTag::APPKIT, "null contextImpl");
441         return Global::Resource::DeviceType::DEVICE_PHONE;
442     }
443 
444     return contextImpl_->GetDeviceType();
445 }
446 
CreateAreaModeContext(int areaMode)447 std::shared_ptr<Context> AbilityStageContext::CreateAreaModeContext(int areaMode)
448 {
449     if (contextImpl_ == nullptr) {
450         TAG_LOGE(AAFwkTag::APPKIT, "invalid contextImpl");
451         return nullptr;
452     }
453 
454     return contextImpl_->CreateAreaModeContext(areaMode);
455 }
456 
457 #ifdef SUPPORT_GRAPHICS
CreateDisplayContext(uint64_t displayId)458 std::shared_ptr<Context> AbilityStageContext::CreateDisplayContext(uint64_t displayId)
459 {
460     if (contextImpl_ == nullptr) {
461         TAG_LOGE(AAFwkTag::APPKIT, "invalid contextImpl");
462         return nullptr;
463     }
464 
465     return contextImpl_->CreateDisplayContext(displayId);
466 }
467 #endif
468 } // namespace AbilityRuntime
469 } // namespace OHOS
470