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