• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "ohos_application.h"
17 
18 #include "ability_record_mgr.h"
19 #include "app_loader.h"
20 #include "application_impl.h"
21 #include "context_impl.h"
22 #include "hilog_wrapper.h"
23 #include "iservice_registry.h"
24 #include "runtime.h"
25 #include "system_ability_definition.h"
26 #ifdef SUPPORT_GRAPHICS
27 #include "window.h"
28 #endif
29 #include "ability_thread.h"
30 
31 namespace OHOS {
32 namespace AppExecFwk {
REGISTER_APPLICATION(OHOSApplication,OHOSApplication)33 REGISTER_APPLICATION(OHOSApplication, OHOSApplication)
34 
35 OHOSApplication::OHOSApplication()
36 {
37     abilityLifecycleCallbacks_.clear();
38     elementsCallbacks_.clear();
39 }
40 
41 OHOSApplication::~OHOSApplication() = default;
42 
43 /**
44  *
45  * @brief Called when Ability#onSaveAbilityState(PacMap) was called on an ability.
46  *
47  * @param outState Indicates the PacMap object passed to Ability#onSaveAbilityState(PacMap)
48  * for storing user data and states. This parameter cannot be null.
49  */
50 
DispatchAbilitySavedState(const PacMap & outState)51 void OHOSApplication::DispatchAbilitySavedState(const PacMap &outState)
52 {
53     HILOG_DEBUG("OHOSApplication::dispatchAbilitySavedState: called");
54     for (auto callback : abilityLifecycleCallbacks_) {
55         if (callback != nullptr) {
56             callback->OnAbilitySaveState(outState);
57         }
58     }
59 }
60 
61 /**
62  *
63  * @brief Will be called the application foregrounds
64  *
65  */
OnForeground()66 void OHOSApplication::OnForeground()
67 {
68     HILOG_DEBUG("NotifyApplicationState::OnForeground begin");
69     if (runtime_ == nullptr) {
70         HILOG_DEBUG("NotifyApplicationState, runtime_ is nullptr");
71         return;
72     }
73     runtime_->NotifyApplicationState(false);
74     HILOG_DEBUG("NotifyApplicationState::OnForeground end");
75 }
76 
77 /**
78  *
79  * @brief Will be called the application backgrounds
80  *
81  */
OnBackground()82 void OHOSApplication::OnBackground()
83 {
84     HILOG_DEBUG("NotifyApplicationState::OnBackground begin");
85     if (runtime_ == nullptr) {
86         HILOG_DEBUG("NotifyApplicationState, runtime_ is nullptr");
87         return;
88     }
89     runtime_->NotifyApplicationState(true);
90     HILOG_DEBUG("NotifyApplicationState::OnBackground end");
91 }
92 
DumpApplication()93 void OHOSApplication::DumpApplication()
94 {
95     HILOG_DEBUG("OHOSApplication::Dump called");
96     // create and initialize abilityInfos
97     std::shared_ptr<AbilityInfo> abilityInfo = nullptr;
98     std::shared_ptr<AbilityLocalRecord> record = nullptr;
99 
100     if (abilityRecordMgr_) {
101         record = abilityRecordMgr_->GetAbilityItem(abilityRecordMgr_->GetToken());
102     }
103 
104     if (record) {
105         abilityInfo = record->GetAbilityInfo();
106     }
107 
108     if (abilityInfo) {
109         HILOG_DEBUG("==============AbilityInfo==============");
110         HILOG_DEBUG("abilityInfo: package: %{public}s", abilityInfo->package.c_str());
111         HILOG_DEBUG("abilityInfo: name: %{public}s", abilityInfo->name.c_str());
112         HILOG_DEBUG("abilityInfo: label: %{public}s", abilityInfo->label.c_str());
113         HILOG_DEBUG("abilityInfo: description: %{public}s", abilityInfo->description.c_str());
114         HILOG_DEBUG("abilityInfo: iconPath: %{public}s", abilityInfo->iconPath.c_str());
115         HILOG_DEBUG("abilityInfo: visible: %{public}d", abilityInfo->visible);
116         HILOG_DEBUG("abilityInfo: kind: %{public}s", abilityInfo->kind.c_str());
117         HILOG_DEBUG("abilityInfo: type: %{public}d", abilityInfo->type);
118         HILOG_DEBUG("abilityInfo: orientation: %{public}d", abilityInfo->orientation);
119         HILOG_DEBUG("abilityInfo: launchMode: %{public}d", abilityInfo->launchMode);
120         for (auto permission : abilityInfo->permissions) {
121             HILOG_DEBUG("abilityInfo: permission: %{public}s", permission.c_str());
122         }
123         HILOG_DEBUG("abilityInfo: bundleName: %{public}s", abilityInfo->bundleName.c_str());
124         HILOG_DEBUG("abilityInfo: applicationName: %{public}s", abilityInfo->applicationName.c_str());
125     }
126 
127     // create and initialize applicationInfo
128     HILOG_DEBUG("==============applicationInfo==============");
129     std::shared_ptr<ApplicationInfo> applicationInfoPtr = GetApplicationInfo();
130     if (applicationInfoPtr != nullptr) {
131         HILOG_DEBUG("applicationInfo: name: %{public}s", applicationInfoPtr->name.c_str());
132         HILOG_DEBUG("applicationInfo: bundleName: %{public}s", applicationInfoPtr->bundleName.c_str());
133         HILOG_DEBUG("applicationInfo: signatureKey: %{public}s", applicationInfoPtr->signatureKey.c_str());
134     }
135 }
136 
137 /**
138  * @brief Set Runtime
139  *
140  * @param runtime Runtime instance.
141  */
SetRuntime(std::unique_ptr<AbilityRuntime::Runtime> && runtime)142 void OHOSApplication::SetRuntime(std::unique_ptr<AbilityRuntime::Runtime>&& runtime)
143 {
144     HILOG_DEBUG("OHOSApplication::SetRuntime begin");
145     if (runtime == nullptr) {
146         HILOG_ERROR("OHOSApplication::SetRuntime failed, runtime is empty");
147         return;
148     }
149     runtime_ = std::move(runtime);
150     HILOG_DEBUG("OHOSApplication::SetRuntime end");
151 }
152 
153 /**
154  * @brief Set ApplicationContext
155  *
156  * @param abilityRuntimeContext ApplicationContext instance.
157  */
SetApplicationContext(const std::shared_ptr<AbilityRuntime::ApplicationContext> & abilityRuntimeContext)158 void OHOSApplication::SetApplicationContext(
159     const std::shared_ptr<AbilityRuntime::ApplicationContext> &abilityRuntimeContext)
160 {
161     HILOG_DEBUG("OHOSApplication::SetApplicationContext");
162     if (abilityRuntimeContext == nullptr) {
163         HILOG_ERROR("OHOSApplication::SetApplicationContext failed, context is empty");
164         return;
165     }
166     abilityRuntimeContext_ = abilityRuntimeContext;
167 }
168 
169 /**
170  *
171  * @brief Set the abilityRecordMgr to the OHOSApplication.
172  *
173  * @param abilityRecordMgr
174  */
SetAbilityRecordMgr(const std::shared_ptr<AbilityRecordMgr> & abilityRecordMgr)175 void OHOSApplication::SetAbilityRecordMgr(const std::shared_ptr<AbilityRecordMgr> &abilityRecordMgr)
176 {
177     HILOG_DEBUG("OHOSApplication::SetAbilityRecordMgr");
178     if (abilityRecordMgr == nullptr) {
179         HILOG_ERROR("ContextDeal::SetAbilityRecordMgr failed, abilityRecordMgr is nullptr");
180         return;
181     }
182     abilityRecordMgr_ = abilityRecordMgr;
183 }
184 
185 /**
186  *
187  * Register AbilityLifecycleCallbacks with OHOSApplication
188  *
189  * @param callBack callBack When the life cycle of the ability in the application changes,
190  */
RegisterAbilityLifecycleCallbacks(const std::shared_ptr<AbilityLifecycleCallbacks> & callBack)191 void OHOSApplication::RegisterAbilityLifecycleCallbacks(const std::shared_ptr<AbilityLifecycleCallbacks> &callBack)
192 {
193     HILOG_DEBUG("OHOSApplication::RegisterAbilityLifecycleCallbacks: called");
194 
195     if (callBack == nullptr) {
196         HILOG_DEBUG("OHOSApplication::RegisterAbilityLifecycleCallbacks: observer is null");
197         return;
198     }
199 
200     abilityLifecycleCallbacks_.emplace_back(callBack);
201 }
202 
203 /**
204  *
205  * Unregister AbilityLifecycleCallbacks with OHOSApplication
206  *
207  * @param callBack RegisterAbilityLifecycleCallbacks`s callBack
208  */
UnregisterAbilityLifecycleCallbacks(const std::shared_ptr<AbilityLifecycleCallbacks> & callBack)209 void OHOSApplication::UnregisterAbilityLifecycleCallbacks(const std::shared_ptr<AbilityLifecycleCallbacks> &callBack)
210 {
211     HILOG_DEBUG("OHOSApplication::UnregisterAbilityLifecycleCallbacks: called");
212 
213     if (callBack == nullptr) {
214         HILOG_DEBUG("OHOSApplication::UnregisterAbilityLifecycleCallbacks: observer is null");
215         return;
216     }
217 
218     abilityLifecycleCallbacks_.remove(callBack);
219 }
220 
221 /**
222  *
223  * Will be called when the given ability calls Ability->onStart
224  *
225  * @param Ability Indicates the ability object that calls the onStart() method.
226  */
OnAbilityStart(const std::shared_ptr<Ability> & ability)227 void OHOSApplication::OnAbilityStart(const std::shared_ptr<Ability> &ability)
228 {
229     if (ability == nullptr) {
230         HILOG_ERROR("ContextDeal::OnAbilityStart failed, ability is nullptr");
231         return;
232     }
233 
234     HILOG_DEBUG("OHOSApplication::OnAbilityStart: called");
235     for (auto callback : abilityLifecycleCallbacks_) {
236         if (callback != nullptr) {
237             callback->OnAbilityStart(ability);
238         }
239     }
240 }
241 
242 /**
243  *
244  * Will be called when the given ability calls Ability->onInactive
245  *
246  * @param Ability Indicates the Ability object that calls the onInactive() method.
247  */
OnAbilityInactive(const std::shared_ptr<Ability> & ability)248 void OHOSApplication::OnAbilityInactive(const std::shared_ptr<Ability> &ability)
249 {
250     if (ability == nullptr) {
251         HILOG_ERROR("ContextDeal::OnAbilityInactive failed, ability is nullptr");
252         return;
253     }
254 
255     HILOG_DEBUG("OHOSApplication::OnAbilityInactive: called");
256     for (auto callback : abilityLifecycleCallbacks_) {
257         if (callback != nullptr) {
258             callback->OnAbilityInactive(ability);
259         }
260     }
261 }
262 
263 /**
264  *
265  * Will be called when the given ability calls Ability->onBackground
266  *
267  * @param Ability Indicates the Ability object that calls the onBackground() method.
268  */
OnAbilityBackground(const std::shared_ptr<Ability> & ability)269 void OHOSApplication::OnAbilityBackground(const std::shared_ptr<Ability> &ability)
270 {
271     if (ability == nullptr) {
272         HILOG_ERROR("ContextDeal::OnAbilityBackground failed, ability is nullptr");
273         return;
274     }
275 
276     HILOG_DEBUG("OHOSApplication::OnAbilityBackground: called");
277     for (auto callback : abilityLifecycleCallbacks_) {
278         if (callback != nullptr) {
279             callback->OnAbilityBackground(ability);
280         }
281     }
282 }
283 
284 /**
285  *
286  * Will be called when the given ability calls Ability->onForeground
287  *
288  * @param Ability Indicates the Ability object that calls the onForeground() method.
289  */
OnAbilityForeground(const std::shared_ptr<Ability> & ability)290 void OHOSApplication::OnAbilityForeground(const std::shared_ptr<Ability> &ability)
291 {
292     if (ability == nullptr) {
293         HILOG_ERROR("ContextDeal::OnAbilityForeground failed, ability is nullptr");
294         return;
295     }
296 
297     HILOG_DEBUG("OHOSApplication::OnAbilityForeground: called");
298     for (auto callback : abilityLifecycleCallbacks_) {
299         if (callback != nullptr) {
300             callback->OnAbilityForeground(ability);
301         }
302     }
303 }
304 
305 /**
306  *
307  * Will be called when the given ability calls Ability->onActive
308  *
309  * @param Ability Indicates the Ability object that calls the onActive() method.
310  */
OnAbilityActive(const std::shared_ptr<Ability> & ability)311 void OHOSApplication::OnAbilityActive(const std::shared_ptr<Ability> &ability)
312 {
313     if (ability == nullptr) {
314         HILOG_ERROR("ContextDeal::OnAbilityActive failed, ability is nullptr");
315         return;
316     }
317 
318     HILOG_DEBUG("OHOSApplication::OnAbilityActive: called");
319     for (auto callback : abilityLifecycleCallbacks_) {
320         if (callback != nullptr) {
321             callback->OnAbilityActive(ability);
322         }
323     }
324 }
325 
326 /**
327  *
328  * Will be called when the given ability calls Ability->onStop
329  *
330  * @param Ability Indicates the Ability object that calls the onStop() method.
331  */
OnAbilityStop(const std::shared_ptr<Ability> & ability)332 void OHOSApplication::OnAbilityStop(const std::shared_ptr<Ability> &ability)
333 {
334     if (ability == nullptr) {
335         HILOG_ERROR("ContextDeal::OnAbilityStop failed, ability is nullptr");
336         return;
337     }
338 
339     HILOG_DEBUG("OHOSApplication::OnAbilityStop: called");
340     for (auto callback : abilityLifecycleCallbacks_) {
341         if (callback != nullptr) {
342             callback->OnAbilityStop(ability);
343         }
344     }
345 }
346 
347 /**
348  *
349  * @brief Register ElementsCallback with OHOSApplication
350  *
351  * @param callBack callBack when the system configuration of the device changes.
352  */
RegisterElementsCallbacks(const std::shared_ptr<ElementsCallback> & callback)353 void OHOSApplication::RegisterElementsCallbacks(const std::shared_ptr<ElementsCallback> &callback)
354 {
355     HILOG_DEBUG("OHOSApplication::RegisterElementsCallbacks: called");
356 
357     if (callback == nullptr) {
358         HILOG_DEBUG("OHOSApplication::RegisterElementsCallbacks: observer is null");
359         return;
360     }
361 
362     elementsCallbacks_.emplace_back(callback);
363 }
364 
365 /**
366  *
367  * @brief Unregister ElementsCallback with OHOSApplication
368  *
369  * @param callback RegisterElementsCallbacks`s callback
370  */
UnregisterElementsCallbacks(const std::shared_ptr<ElementsCallback> & callback)371 void OHOSApplication::UnregisterElementsCallbacks(const std::shared_ptr<ElementsCallback> &callback)
372 {
373     HILOG_DEBUG("OHOSApplication::UnregisterElementsCallbacks: called");
374 
375     if (callback == nullptr) {
376         HILOG_DEBUG("OHOSApplication::UnregisterElementsCallbacks: observer is null");
377         return;
378     }
379 
380     elementsCallbacks_.remove(callback);
381 }
382 
383 /**
384  *
385  * @brief Will be Called when the system configuration of the device changes.
386  *
387  * @param config Indicates the new Configuration object.
388  */
OnConfigurationUpdated(const Configuration & config)389 void OHOSApplication::OnConfigurationUpdated(const Configuration &config)
390 {
391     HILOG_DEBUG("OHOSApplication::OnConfigurationUpdated: called");
392     if (!abilityRecordMgr_ || !configuration_) {
393         HILOG_DEBUG("abilityRecordMgr_ or configuration_ is null");
394         return;
395     }
396 
397     // Update own object configuration_
398     std::vector<std::string> changeKeyV;
399     configuration_->CompareDifferent(changeKeyV, config);
400     configuration_->Merge(changeKeyV, config);
401 
402     // Notify all abilities
403     HILOG_INFO(
404         "Number of ability to be notified : [%{public}d]", static_cast<int>(abilityRecordMgr_->GetRecordCount()));
405     for (const auto &abilityToken : abilityRecordMgr_->GetAllTokens()) {
406         auto abilityRecord = abilityRecordMgr_->GetAbilityItem(abilityToken);
407         if (abilityRecord && abilityRecord->GetAbilityThread()) {
408             abilityRecord->GetAbilityThread()->ScheduleUpdateConfiguration(config);
409         }
410     }
411 
412     // Notify AbilityStage
413     HILOG_INFO("Number of abilityStage to be notified : [%{public}zu]", abilityStages_.size());
414     for (auto it = abilityStages_.begin(); it != abilityStages_.end(); it++) {
415         auto abilityStage = it->second;
416         if (abilityStage) {
417             abilityStage->OnConfigurationUpdated(config);
418         }
419     }
420 
421 #ifdef SUPPORT_GRAPHICS
422     // Notify Window
423     HILOG_DEBUG("Update configuration for all window.");
424     auto diffConfiguration = std::make_shared<AppExecFwk::Configuration>(config);
425     Rosen::Window::UpdateConfigurationForAll(diffConfiguration);
426 #endif
427 
428     for (auto callback : elementsCallbacks_) {
429         if (callback != nullptr) {
430             callback->OnConfigurationUpdated(nullptr, config);
431         }
432     }
433 
434     abilityRuntimeContext_->DispatchConfigurationUpdated(config);
435 }
436 
437 /**
438  *
439  * @brief Called when the system has determined to trim the memory, for example,
440  * when the ability is running in the background and there is no enough memory for
441  * running as many background processes as possible.
442  *
443  * @param level Indicates the memory trim level, which shows the current memory usage status.
444  */
OnMemoryLevel(int level)445 void OHOSApplication::OnMemoryLevel(int level)
446 {
447     HILOG_DEBUG("OHOSApplication::OnMemoryLevel: called");
448 
449     if (abilityRecordMgr_) {
450         HILOG_DEBUG("Number of ability to be notified : [%{public}d]", abilityRecordMgr_->GetRecordCount());
451         for (const auto &abilityToken : abilityRecordMgr_->GetAllTokens()) {
452             auto abilityRecord = abilityRecordMgr_->GetAbilityItem(abilityToken);
453             if (abilityRecord && abilityRecord->GetAbilityThread()) {
454                 abilityRecord->GetAbilityThread()->NotifyMemoryLevel(level);
455             }
456         }
457     }
458 
459     HILOG_DEBUG("Number of abilityStage to be notified : [%{public}zu]", abilityStages_.size());
460     for (auto it = abilityStages_.begin(); it != abilityStages_.end(); it++) {
461         auto abilityStage = it->second;
462         if (abilityStage) {
463             abilityStage->OnMemoryLevel(level);
464         }
465     }
466 
467     HILOG_DEBUG("OHOSApplication::OnMemoryLevel: called");
468     for (auto callback : elementsCallbacks_) {
469         if (callback != nullptr) {
470             callback->OnMemoryLevel(level);
471         }
472     }
473 
474     abilityRuntimeContext_->DispatchMemoryLevel(level);
475 }
476 
477 /**
478  *
479  * @brief Will be called the application starts
480  *
481  */
OnStart()482 void OHOSApplication::OnStart()
483 {
484     HILOG_DEBUG("OnStart called.");
485 }
486 
487 /**
488  *
489  * @brief Will be called the application ends
490  *
491  */
OnTerminate()492 void OHOSApplication::OnTerminate()
493 {
494     HILOG_DEBUG("OHOSApplication::OnTerminate: called");
495 }
496 
497 /**
498  *
499  * @brief Called when an ability calls Ability#onSaveAbilityState(PacMap).
500  * You can implement your own logic in this method.
501  * @param outState IIndicates the {@link PacMap} object passed to the onSaveAbilityState() callback.
502  *
503  */
OnAbilitySaveState(const PacMap & outState)504 void OHOSApplication::OnAbilitySaveState(const PacMap &outState)
505 {
506     DispatchAbilitySavedState(outState);
507 }
508 
AddAbilityStage(const std::shared_ptr<AbilityLocalRecord> & abilityRecord)509 std::shared_ptr<AbilityRuntime::Context> OHOSApplication::AddAbilityStage(
510     const std::shared_ptr<AbilityLocalRecord> &abilityRecord)
511 {
512     if (abilityRecord == nullptr) {
513         HILOG_ERROR("AddAbilityStage:abilityRecord is nullptr");
514         return nullptr;
515     }
516     const std::shared_ptr<AbilityInfo> &abilityInfo = abilityRecord->GetAbilityInfo();
517     if (abilityInfo == nullptr) {
518         HILOG_ERROR("AddAbilityStage:abilityInfo is nullptr");
519         return nullptr;
520     }
521     std::string moduleName = abilityInfo->moduleName;
522     std::shared_ptr<AbilityRuntime::AbilityStage> abilityStage;
523     auto iterator = abilityStages_.find(moduleName);
524     if (iterator == abilityStages_.end()) {
525         std::shared_ptr<AbilityRuntime::ContextImpl> stageContext = std::make_shared<AbilityRuntime::ContextImpl>();
526         stageContext->SetParentContext(abilityRuntimeContext_);
527         stageContext->InitHapModuleInfo(abilityInfo);
528         stageContext->SetConfiguration(GetConfiguration());
529         std::shared_ptr<AppExecFwk::HapModuleInfo> hapModuleInfo = stageContext->GetHapModuleInfo();
530         if (hapModuleInfo == nullptr) {
531             HILOG_ERROR("AddAbilityStage:hapModuleInfo is nullptr");
532             return nullptr;
533         }
534 
535         if (abilityInfo->applicationInfo.multiProjects) {
536             auto rm = stageContext->CreateModuleContext(hapModuleInfo->moduleName)->GetResourceManager();
537             stageContext->SetResourceManager(rm);
538         }
539 
540         abilityStage = AbilityRuntime::AbilityStage::Create(runtime_, *hapModuleInfo);
541         abilityStage->Init(stageContext);
542         Want want;
543         if (abilityRecord->GetWant()) {
544             HILOG_DEBUG("want is ok, transport to abilityStage");
545             want = *(abilityRecord->GetWant());
546         }
547         abilityStage->OnCreate(want);
548         abilityStages_[moduleName] = abilityStage;
549     } else {
550         abilityStage = iterator->second;
551     }
552     const sptr<IRemoteObject> &token = abilityRecord->GetToken();
553     if (token == nullptr) {
554         HILOG_ERROR("AddAbilityStage:token is null");
555         return nullptr;
556     }
557     abilityStage->AddAbility(token, abilityRecord);
558     return abilityStage->GetContext();
559 }
560 
561 /**
562  *
563  * @brief update the application info after new module installed.
564  *
565  * @param appInfo The latest application info obtained from bms for update abilityRuntimeContext.
566  *
567  */
UpdateApplicationInfoInstalled(const AppExecFwk::ApplicationInfo & appInfo)568 void OHOSApplication::UpdateApplicationInfoInstalled(const AppExecFwk::ApplicationInfo &appInfo)
569 {
570     HILOG_DEBUG("OHOSApplication::UpdateApplicationInfoInstalled");
571 
572     if (abilityRuntimeContext_ == nullptr) {
573         HILOG_ERROR("OHOSApplication::UpdateApplicationInfoInstalled abilityRuntimeContext_ is nullptr.");
574         return;
575     }
576 
577     abilityRuntimeContext_->SetApplicationInfo(std::make_shared<AppExecFwk::ApplicationInfo>(appInfo));
578 }
579 
AddAbilityStage(const AppExecFwk::HapModuleInfo & hapModuleInfo)580 bool OHOSApplication::AddAbilityStage(const AppExecFwk::HapModuleInfo &hapModuleInfo)
581 {
582     HILOG_DEBUG("OHOSApplication::AddAbilityStage");
583     if (abilityRuntimeContext_ == nullptr) {
584         HILOG_ERROR("OHOSApplication::AddAbilityStage abilityRuntimeContext_ is nullptr.");
585         return false;
586     }
587 
588     if (runtime_ == nullptr) {
589         HILOG_ERROR("OHOSApplication::AddAbilityStage abilityRuntimeContext_ is nullptr.");
590         return false;
591     }
592 
593     if (abilityStages_.find(hapModuleInfo.moduleName) != abilityStages_.end()) {
594         HILOG_ERROR("OHOSApplication::%{public}s: object already exists ", __func__);
595         return false;
596     }
597 
598     auto stageContext = std::make_shared<AbilityRuntime::ContextImpl>();
599     stageContext->SetParentContext(abilityRuntimeContext_);
600     stageContext->InitHapModuleInfo(hapModuleInfo);
601     stageContext->SetConfiguration(GetConfiguration());
602     auto moduleInfo = stageContext->GetHapModuleInfo();
603     if (moduleInfo == nullptr) {
604         HILOG_ERROR("OHOSApplication::%{public}s: moduleInfo is nullptr", __func__);
605         return false;
606     }
607 
608     if (abilityRuntimeContext_->GetApplicationInfo() && abilityRuntimeContext_->GetApplicationInfo()->multiProjects) {
609         auto rm = stageContext->CreateModuleContext(hapModuleInfo.moduleName)->GetResourceManager();
610         stageContext->SetResourceManager(rm);
611     }
612 
613     auto abilityStage = AbilityRuntime::AbilityStage::Create(runtime_, *moduleInfo);
614     abilityStage->Init(stageContext);
615     Want want;
616     abilityStage->OnCreate(want);
617     abilityStages_[hapModuleInfo.moduleName] = abilityStage;
618     HILOG_ERROR("OHOSApplication::%{public}s: abilityStage insert and initialization", __func__);
619     return true;
620 }
621 
CleanAbilityStage(const sptr<IRemoteObject> & token,const std::shared_ptr<AbilityInfo> & abilityInfo)622 void OHOSApplication::CleanAbilityStage(const sptr<IRemoteObject> &token,
623     const std::shared_ptr<AbilityInfo> &abilityInfo)
624 {
625     if (abilityInfo == nullptr) {
626         HILOG_ERROR("CleanAbilityStage:abilityInfo is nullptr");
627         return;
628     }
629     if (token == nullptr) {
630         HILOG_ERROR("CleanAbilityStage:token is nullptr");
631         return;
632     }
633     std::string moduleName = abilityInfo->moduleName;
634     auto iterator = abilityStages_.find(moduleName);
635     if (iterator != abilityStages_.end()) {
636         auto abilityStage = iterator->second;
637         abilityStage->RemoveAbility(token);
638         if (!abilityStage->ContainsAbility()) {
639             abilityStage->OnDestroy();
640             abilityStages_.erase(moduleName);
641         }
642     }
643 }
644 
GetAppContext() const645 std::shared_ptr<AbilityRuntime::Context> OHOSApplication::GetAppContext() const
646 {
647     return abilityRuntimeContext_;
648 }
649 
GetRuntime()650 const std::unique_ptr<AbilityRuntime::Runtime>& OHOSApplication::GetRuntime()
651 {
652     return runtime_;
653 }
654 
SetConfiguration(const Configuration & config)655 void OHOSApplication::SetConfiguration(const Configuration &config)
656 {
657     if (!configuration_) {
658         configuration_ = std::make_shared<Configuration>(config);
659     }
660 }
661 
ScheduleAcceptWant(const AAFwk::Want & want,const std::string & moduleName,std::string & flag)662 void OHOSApplication::ScheduleAcceptWant(const AAFwk::Want &want, const std::string &moduleName, std::string &flag)
663 {
664     HILOG_DEBUG("OHOSApplication::ScheduleAcceptWant: called");
665     auto iter = abilityStages_.find(moduleName);
666     if (iter != abilityStages_.end()) {
667         auto abilityStage = iter->second;
668         if (abilityStage) {
669             flag = abilityStage->OnAcceptWant(want);
670         }
671     }
672 }
673 
GetConfiguration()674 std::shared_ptr<Configuration> OHOSApplication::GetConfiguration()
675 {
676     return configuration_;
677 }
678 
SetExtensionTypeMap(std::map<int32_t,std::string> map)679 void OHOSApplication::SetExtensionTypeMap(std::map<int32_t, std::string> map)
680 {
681     extensionTypeMap_ = map;
682 }
683 
NotifyLoadRepairPatch(const std::string & hqfFile,const std::string & hapPath)684 bool OHOSApplication::NotifyLoadRepairPatch(const std::string &hqfFile, const std::string &hapPath)
685 {
686     if (runtime_ == nullptr) {
687         HILOG_DEBUG("runtime is nullptr.");
688         return true;
689     }
690 
691     return runtime_->LoadRepairPatch(hqfFile, hapPath);
692 }
693 
NotifyHotReloadPage()694 bool OHOSApplication::NotifyHotReloadPage()
695 {
696     if (runtime_ == nullptr) {
697         HILOG_DEBUG("runtime is nullptr.");
698         return true;
699     }
700 
701     return runtime_->NotifyHotReloadPage();
702 }
703 
NotifyUnLoadRepairPatch(const std::string & hqfFile)704 bool OHOSApplication::NotifyUnLoadRepairPatch(const std::string &hqfFile)
705 {
706     if (runtime_ == nullptr) {
707         HILOG_DEBUG("runtime is nullptr.");
708         return true;
709     }
710 
711     return runtime_->UnLoadRepairPatch(hqfFile);
712 }
713 }  // namespace AppExecFwk
714 }  // namespace OHOS
715