1 /*
2 * Copyright (c) 2021-2024 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 <cstdio>
17 #include <cstring>
18 #include <fcntl.h>
19
20 #include <sys/stat.h>
21
22 #include "ohos_application.h"
23
24 #include "ability.h"
25 #include "ability_record_mgr.h"
26 #include "ability_thread.h"
27 #include "app_loader.h"
28 #include "application_context.h"
29 #include "application_cleaner.h"
30 #include "application_impl.h"
31 #include "bundle_mgr_helper.h"
32 #include "configuration_utils.h"
33 #include "context_impl.h"
34 #include "hilog_tag_wrapper.h"
35 #include "hitrace_meter.h"
36 #include "iservice_registry.h"
37 #include "runtime.h"
38 #include "system_ability_definition.h"
39 #include "syspara/parameter.h"
40 #include "ui_ability.h"
41 #include "application_configuration_manager.h"
42 #ifdef SUPPORT_GRAPHICS
43 #include "window.h"
44 #endif
45
46 namespace OHOS {
47 namespace AppExecFwk {
48 namespace {
49 constexpr const char* PERSIST_DARKMODE_KEY = "persist.ace.darkmode";
50 }
51 REGISTER_APPLICATION(OHOSApplication, OHOSApplication)
52 constexpr int32_t APP_ENVIRONMENT_OVERWRITE = 1;
53 using ApplicationConfigurationManager = AbilityRuntime::ApplicationConfigurationManager;
OHOSApplication()54 OHOSApplication::OHOSApplication()
55 {
56 TAG_LOGD(AAFwkTag::APPKIT, "called");
57 }
58
~OHOSApplication()59 OHOSApplication::~OHOSApplication()
60 {
61 TAG_LOGD(AAFwkTag::APPKIT, "called");
62 }
63
64 /**
65 *
66 * @brief Will be called the application foregrounds
67 *
68 */
OnForeground()69 void OHOSApplication::OnForeground()
70 {
71 TAG_LOGD(AAFwkTag::APPKIT, "begin");
72 if (abilityRuntimeContext_) {
73 abilityRuntimeContext_->NotifyApplicationForeground();
74 }
75
76 if (runtime_ == nullptr) {
77 TAG_LOGD(AAFwkTag::APPKIT, "NotifyApplicationState, runtime_ is nullptr");
78 return;
79 }
80 runtime_->NotifyApplicationState(false);
81 TAG_LOGD(AAFwkTag::APPKIT, "NotifyApplicationState::OnForeground end");
82 }
83
84 /**
85 *
86 * @brief Will be called the application backgrounds
87 *
88 */
OnBackground()89 void OHOSApplication::OnBackground()
90 {
91 TAG_LOGD(AAFwkTag::APPKIT, "begin");
92 if (abilityRuntimeContext_) {
93 abilityRuntimeContext_->NotifyApplicationBackground();
94 }
95
96 if (runtime_ == nullptr) {
97 TAG_LOGD(AAFwkTag::APPKIT, "runtime_ is nullptr");
98 return;
99 }
100 runtime_->NotifyApplicationState(true);
101 }
102
DumpApplication()103 void OHOSApplication::DumpApplication()
104 {
105 TAG_LOGD(AAFwkTag::APPKIT, "called");
106 // create and initialize abilityInfos
107 std::shared_ptr<AbilityInfo> abilityInfo = nullptr;
108 std::shared_ptr<AbilityLocalRecord> record = nullptr;
109
110 if (abilityRecordMgr_) {
111 record = abilityRecordMgr_->GetAbilityItem(abilityRecordMgr_->GetToken());
112 }
113
114 if (record) {
115 abilityInfo = record->GetAbilityInfo();
116 }
117
118 if (abilityInfo) {
119 TAG_LOGD(AAFwkTag::APPKIT, "==============AbilityInfo==============");
120 TAG_LOGD(AAFwkTag::APPKIT, "abilityInfo: package: %{public}s", abilityInfo->package.c_str());
121 TAG_LOGD(AAFwkTag::APPKIT, "abilityInfo: name: %{public}s", abilityInfo->name.c_str());
122 TAG_LOGD(AAFwkTag::APPKIT, "abilityInfo: label: %{public}s", abilityInfo->label.c_str());
123 TAG_LOGD(AAFwkTag::APPKIT, "abilityInfo: description: %{public}s", abilityInfo->description.c_str());
124 TAG_LOGD(AAFwkTag::APPKIT, "abilityInfo: iconPath: %{public}s", abilityInfo->iconPath.c_str());
125 TAG_LOGD(AAFwkTag::APPKIT, "abilityInfo: visible: %{public}d", abilityInfo->visible);
126 TAG_LOGD(AAFwkTag::APPKIT, "abilityInfo: kind: %{public}s", abilityInfo->kind.c_str());
127 TAG_LOGD(AAFwkTag::APPKIT, "abilityInfo: type: %{public}d", abilityInfo->type);
128 TAG_LOGD(AAFwkTag::APPKIT, "abilityInfo: orientation: %{public}d", abilityInfo->orientation);
129 TAG_LOGD(AAFwkTag::APPKIT, "abilityInfo: launchMode: %{public}d", abilityInfo->launchMode);
130 for (auto permission : abilityInfo->permissions) {
131 TAG_LOGD(AAFwkTag::APPKIT, "abilityInfo: permission: %{public}s", permission.c_str());
132 }
133 TAG_LOGD(AAFwkTag::APPKIT, "abilityInfo: bundleName: %{public}s", abilityInfo->bundleName.c_str());
134 TAG_LOGD(AAFwkTag::APPKIT, "abilityInfo: applicationName: %{public}s", abilityInfo->applicationName.c_str());
135 }
136
137 // create and initialize applicationInfo
138 std::shared_ptr<ApplicationInfo> applicationInfoPtr = GetApplicationInfo();
139 if (applicationInfoPtr != nullptr) {
140 TAG_LOGD(AAFwkTag::APPKIT, "applicationInfo: name: %{public}s", applicationInfoPtr->name.c_str());
141 TAG_LOGD(AAFwkTag::APPKIT, "applicationInfo: bundleName: %{public}s", applicationInfoPtr->bundleName.c_str());
142 TAG_LOGD(
143 AAFwkTag::APPKIT, "applicationInfo: signatureKey: %{public}s", applicationInfoPtr->signatureKey.c_str());
144 }
145 }
146
147 /**
148 * @brief Set Runtime
149 *
150 * @param runtime Runtime instance.
151 */
SetRuntime(std::unique_ptr<AbilityRuntime::Runtime>&& runtime)152 void OHOSApplication::SetRuntime(std::unique_ptr<AbilityRuntime::Runtime>&& runtime)
153 {
154 TAG_LOGD(AAFwkTag::APPKIT, "begin");
155 if (runtime == nullptr) {
156 TAG_LOGE(AAFwkTag::APPKIT, "runtime is empty");
157 return;
158 }
159 runtime_ = std::move(runtime);
160 }
161
162 /**
163 * @brief Set ApplicationContext
164 *
165 * @param abilityRuntimeContext ApplicationContext instance.
166 */
SetApplicationContext( const std::shared_ptr<AbilityRuntime::ApplicationContext> &abilityRuntimeContext)167 void OHOSApplication::SetApplicationContext(
168 const std::shared_ptr<AbilityRuntime::ApplicationContext> &abilityRuntimeContext)
169 {
170 TAG_LOGD(AAFwkTag::APPKIT, "called");
171 if (abilityRuntimeContext == nullptr) {
172 TAG_LOGE(AAFwkTag::APPKIT, "context is empty");
173 return;
174 }
175 abilityRuntimeContext_ = abilityRuntimeContext;
176 auto application = std::static_pointer_cast<OHOSApplication>(shared_from_this());
177 std::weak_ptr<OHOSApplication> applicationWptr = application;
178 abilityRuntimeContext_->RegisterAppConfigUpdateObserver([applicationWptr](const Configuration &config) {
179 std::shared_ptr<OHOSApplication> applicationSptr = applicationWptr.lock();
180 if (applicationSptr == nullptr) {
181 TAG_LOGE(AAFwkTag::APPKIT, "application is nullptr.");
182 return;
183 }
184 applicationSptr->OnConfigurationUpdated(config, AbilityRuntime::SetLevel::Application);
185 });
186 abilityRuntimeContext_->RegisterAppFontObserver([applicationWptr](const Configuration &config) {
187 std::shared_ptr<OHOSApplication> applicationSptr = applicationWptr.lock();
188 if (applicationSptr == nullptr) {
189 TAG_LOGE(AAFwkTag::APPKIT, "application is nullptr.");
190 return;
191 }
192 applicationSptr->OnFontUpdated(config);
193 });
194 }
195
196 /**
197 *
198 * @brief Set the abilityRecordMgr to the OHOSApplication.
199 *
200 * @param abilityRecordMgr
201 */
SetAbilityRecordMgr(const std::shared_ptr<AbilityRecordMgr> &abilityRecordMgr)202 void OHOSApplication::SetAbilityRecordMgr(const std::shared_ptr<AbilityRecordMgr> &abilityRecordMgr)
203 {
204 TAG_LOGD(AAFwkTag::APPKIT, "called");
205 if (abilityRecordMgr == nullptr) {
206 TAG_LOGE(AAFwkTag::APPKIT, "abilityRecordMgr is nullptr");
207 return;
208 }
209 abilityRecordMgr_ = abilityRecordMgr;
210 }
211
212 /**
213 *
214 * @brief Will be Called when the system configuration of the device changes.
215 *
216 * @param config Indicates the new Configuration object.
217 */
OnConfigurationUpdated(Configuration config, AbilityRuntime::SetLevel level)218 void OHOSApplication::OnConfigurationUpdated(Configuration config, AbilityRuntime::SetLevel level)
219 {
220 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
221 if (!abilityRecordMgr_ || !configuration_ || !abilityRuntimeContext_) {
222 TAG_LOGD(AAFwkTag::APPKIT, "abilityRecordMgr_ or configuration_ or abilityRuntimeContext_ is null");
223 return;
224 }
225 // Whether the color changes with the system
226 bool isUpdateAppColor = IsUpdateColorNeeded(config, level);
227 // Whether the font changes with the system
228 bool isUpdateAppFontSize = isUpdateFontSize(config, level);
229 // Whether the language changes with the system
230 bool isUpdateAppLanguage = IsUpdateLanguageNeeded(config, level);
231 if (!isUpdateAppColor && !isUpdateAppFontSize && !isUpdateAppLanguage && config.GetItemSize() == 0) {
232 TAG_LOGD(AAFwkTag::APPKIT, "configuration need not updated");
233 return;
234 }
235 std::vector<std::string> changeKeyV;
236 {
237 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, "configuration_->CompareDifferent");
238 configuration_->CompareDifferent(changeKeyV, config);
239 configuration_->Merge(changeKeyV, config);
240 }
241 TAG_LOGI(AAFwkTag::APPKIT, "configuration_: %{public}s, config: %{public}s",
242 configuration_->GetName().c_str(), config.GetName().c_str());
243 // Update resConfig of resource manager, which belongs to application context.
244 UpdateAppContextResMgr(config);
245 // Notify all abilities
246 for (const auto &abilityToken : abilityRecordMgr_->GetAllTokens()) {
247 auto abilityRecord = abilityRecordMgr_->GetAbilityItem(abilityToken);
248 if (abilityRecord && abilityRecord->GetAbilityThread()) {
249 abilityRecord->GetAbilityThread()->ScheduleUpdateConfiguration(config);
250 }
251 }
252
253 for (auto it = abilityStages_.begin(); it != abilityStages_.end(); it++) {
254 auto abilityStage = it->second;
255 if (abilityStage) {
256 abilityStage->OnConfigurationUpdated(config);
257 }
258 }
259
260 #ifdef SUPPORT_GRAPHICS
261 TAG_LOGD(AAFwkTag::APPKIT, "Update configuration for all window.");
262 auto diffConfiguration = std::make_shared<AppExecFwk::Configuration>(config);
263 Rosen::Window::UpdateConfigurationForAll(diffConfiguration);
264 #endif
265
266 abilityRuntimeContext_->DispatchConfigurationUpdated(*configuration_);
267 abilityRuntimeContext_->SetMcc(configuration_->GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_MCC));
268 abilityRuntimeContext_->SetMnc(configuration_->GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_MNC));
269 abilityRuntimeContext_->SetConfiguration(configuration_);
270 }
271
272 /**
273 *
274 * @brief Will be Called when the application font of the device changes.
275 *
276 * @param config Indicates the new Configuration object.
277 */
OnFontUpdated(Configuration config)278 void OHOSApplication::OnFontUpdated(Configuration config)
279 {
280 #ifdef SUPPORT_GRAPHICS
281 // Notify Window
282 auto diffConfiguration = std::make_shared<AppExecFwk::Configuration>(config);
283 Rosen::Window::UpdateConfigurationForAll(diffConfiguration);
284 #endif
285 }
286
287 /**
288 *
289 * @brief Called when the system has determined to trim the memory, for example,
290 * when the ability is running in the background and there is no enough memory for
291 * running as many background processes as possible.
292 *
293 * @param level Indicates the memory trim level, which shows the current memory usage status.
294 */
OnMemoryLevel(int level)295 void OHOSApplication::OnMemoryLevel(int level)
296 {
297 TAG_LOGD(AAFwkTag::APPKIT, "called");
298 if (abilityRuntimeContext_ == nullptr) {
299 TAG_LOGE(AAFwkTag::APPKIT, "abilityRuntimeContext_ is nullptr");
300 return;
301 }
302 if (abilityRecordMgr_) {
303 TAG_LOGD(
304 AAFwkTag::APPKIT, "Number of ability to be notified : [%{public}d]", abilityRecordMgr_->GetRecordCount());
305 for (const auto &abilityToken : abilityRecordMgr_->GetAllTokens()) {
306 auto abilityRecord = abilityRecordMgr_->GetAbilityItem(abilityToken);
307 if (abilityRecord && abilityRecord->GetAbilityThread()) {
308 abilityRecord->GetAbilityThread()->NotifyMemoryLevel(level);
309 }
310 }
311 }
312
313 TAG_LOGD(AAFwkTag::APPKIT, "Number of abilityStage to be notified : [%{public}zu]", abilityStages_.size());
314 for (auto it = abilityStages_.begin(); it != abilityStages_.end(); it++) {
315 auto abilityStage = it->second;
316 if (abilityStage) {
317 abilityStage->OnMemoryLevel(level);
318 }
319 }
320
321 TAG_LOGD(AAFwkTag::APPKIT, "called");
322 abilityRuntimeContext_->DispatchMemoryLevel(level);
323 }
324
325 /**
326 *
327 * @brief Will be called the application starts
328 *
329 */
OnStart()330 void OHOSApplication::OnStart()
331 {}
332
333 /**
334 *
335 * @brief Will be called the application ends
336 */
OnTerminate()337 void OHOSApplication::OnTerminate()
338 {}
339
SetAppEnv(const std::vector<AppEnvironment>& appEnvironments)340 void OHOSApplication::SetAppEnv(const std::vector<AppEnvironment>& appEnvironments)
341 {
342 if (appEnvironments.empty()) {
343 return;
344 }
345
346 for (const auto &appEnvironment : appEnvironments) {
347 if (setenv(appEnvironment.name.c_str(), appEnvironment.value.c_str(), APP_ENVIRONMENT_OVERWRITE)) {
348 TAG_LOGE(AAFwkTag::APPKIT, "appEnvironment: %{public}s set failed", appEnvironment.name.c_str());
349 return;
350 }
351 TAG_LOGI(AAFwkTag::APPKIT, "appEnvironment set successfully: %{public}s = %{public}s",
352 appEnvironment.name.c_str(), appEnvironment.value.c_str());
353 }
354 return;
355 }
356
AddAbilityStage( const std::shared_ptr<AbilityLocalRecord> &abilityRecord, const std::function<void(const std::shared_ptr<AbilityRuntime::Context> &)> &callback, bool &isAsyncCallback)357 std::shared_ptr<AbilityRuntime::Context> OHOSApplication::AddAbilityStage(
358 const std::shared_ptr<AbilityLocalRecord> &abilityRecord,
359 const std::function<void(const std::shared_ptr<AbilityRuntime::Context> &)> &callback, bool &isAsyncCallback)
360 {
361 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
362 if (abilityRecord == nullptr) {
363 TAG_LOGE(AAFwkTag::APPKIT, "abilityRecord is nullptr");
364 return nullptr;
365 }
366 const std::shared_ptr<AbilityInfo> &abilityInfo = abilityRecord->GetAbilityInfo();
367 if (abilityInfo == nullptr) {
368 TAG_LOGE(AAFwkTag::APPKIT, "abilityInfo is nullptr");
369 return nullptr;
370 }
371 std::string moduleName = abilityInfo->moduleName;
372 std::shared_ptr<AbilityRuntime::AbilityStage> abilityStage;
373 auto iterator = abilityStages_.find(moduleName);
374 if (iterator == abilityStages_.end()) {
375 auto stageContext = std::make_shared<AbilityRuntime::AbilityStageContext>();
376 stageContext->SetParentContext(abilityRuntimeContext_);
377 stageContext->InitHapModuleInfo(abilityInfo);
378 stageContext->SetConfiguration(GetConfiguration());
379 std::shared_ptr<AppExecFwk::HapModuleInfo> hapModuleInfo = stageContext->GetHapModuleInfo();
380 if (hapModuleInfo == nullptr) {
381 TAG_LOGE(AAFwkTag::APPKIT, "hapModuleInfo is nullptr");
382 return nullptr;
383 }
384 if (runtime_) {
385 runtime_->UpdatePkgContextInfoJson(
386 hapModuleInfo->moduleName, hapModuleInfo->hapPath, hapModuleInfo->packageName);
387 }
388 SetAppEnv(hapModuleInfo->appEnvironments);
389
390 if (abilityInfo->applicationInfo.multiProjects) {
391 auto moduleContext = stageContext->CreateModuleContext(hapModuleInfo->moduleName);
392 auto rm = moduleContext != nullptr ? moduleContext->GetResourceManager() : nullptr;
393 stageContext->SetResourceManager(rm);
394 }
395
396 abilityStage = AbilityRuntime::AbilityStage::Create(runtime_, *hapModuleInfo);
397 if (abilityStage == nullptr) {
398 TAG_LOGE(AAFwkTag::APPKIT, "ability stage invalid");
399 return nullptr;
400 }
401 auto application = std::static_pointer_cast<OHOSApplication>(shared_from_this());
402 std::weak_ptr<OHOSApplication> weak = application;
403 abilityStage->Init(stageContext, weak);
404 auto autoStartupCallback = CreateAutoStartupCallback(abilityStage, abilityRecord, callback);
405 if (autoStartupCallback != nullptr) {
406 abilityStage->RunAutoStartupTask(autoStartupCallback, isAsyncCallback, stageContext);
407 if (isAsyncCallback) {
408 TAG_LOGI(AAFwkTag::APPKIT, "waiting for startup");
409 return nullptr;
410 }
411 }
412 Want want;
413 if (abilityRecord->GetWant()) {
414 TAG_LOGD(AAFwkTag::APPKIT, "want is ok, transport to abilityStage");
415 want = *(abilityRecord->GetWant());
416 }
417 abilityStage->OnCreate(want);
418 abilityStages_[moduleName] = abilityStage;
419 } else {
420 abilityStage = iterator->second;
421 }
422 const sptr<IRemoteObject> &token = abilityRecord->GetToken();
423 if (token == nullptr) {
424 TAG_LOGE(AAFwkTag::APPKIT, "token is null");
425 return nullptr;
426 }
427 abilityStage->AddAbility(token, abilityRecord);
428 return abilityStage->GetContext();
429 }
430
CreateAutoStartupCallback( const std::shared_ptr<AbilityRuntime::AbilityStage> abilityStage, const std::shared_ptr<AbilityLocalRecord> abilityRecord, const std::function<void(const std::shared_ptr<AbilityRuntime::Context>&)>& callback)431 const std::function<void()> OHOSApplication::CreateAutoStartupCallback(
432 const std::shared_ptr<AbilityRuntime::AbilityStage> abilityStage,
433 const std::shared_ptr<AbilityLocalRecord> abilityRecord,
434 const std::function<void(const std::shared_ptr<AbilityRuntime::Context>&)>& callback)
435 {
436 const std::shared_ptr<AbilityInfo> &abilityInfo = abilityRecord->GetAbilityInfo();
437 if (abilityInfo == nullptr) {
438 TAG_LOGE(AAFwkTag::APPKIT, "null abilityInfo");
439 return nullptr;
440 }
441 if (!IsMainProcess(abilityInfo->bundleName, abilityInfo->applicationInfo.process)) {
442 return nullptr;
443 }
444 std::string moduleName = abilityInfo->moduleName;
445 auto application = std::static_pointer_cast<OHOSApplication>(shared_from_this());
446 std::weak_ptr<OHOSApplication> weak = application;
447
448 auto autoStartupCallback = [weak, abilityStage, abilityRecord, moduleName, callback]() {
449 auto ohosApplication = weak.lock();
450 if (ohosApplication == nullptr) {
451 TAG_LOGE(AAFwkTag::APPKIT, "null ohosApplication");
452 return;
453 }
454 ohosApplication->AutoStartupDone(abilityRecord, abilityStage, moduleName);
455 if (callback == nullptr) {
456 TAG_LOGE(AAFwkTag::APPKIT, "null callback");
457 return;
458 }
459 callback(abilityStage->GetContext());
460 };
461
462 return autoStartupCallback;
463 }
464
CreateAutoStartupCallback( const std::shared_ptr<AbilityRuntime::AbilityStage> &abilityStage, const AppExecFwk::HapModuleInfo &hapModuleInfo, const std::function<void()>& callback)465 const std::function<void()> OHOSApplication::CreateAutoStartupCallback(
466 const std::shared_ptr<AbilityRuntime::AbilityStage> &abilityStage,
467 const AppExecFwk::HapModuleInfo &hapModuleInfo,
468 const std::function<void()>& callback)
469 {
470 auto applicationInfo = abilityRuntimeContext_->GetApplicationInfo();
471 if (applicationInfo == nullptr) {
472 TAG_LOGE(AAFwkTag::APPKIT, "null applicationInfo");
473 return nullptr;
474 }
475 if (!IsMainProcess(hapModuleInfo.bundleName, applicationInfo->process)) {
476 return nullptr;
477 }
478 auto application = std::static_pointer_cast<OHOSApplication>(shared_from_this());
479 std::weak_ptr<OHOSApplication> weak = application;
480
481 auto autoStartupCallback = [weak, abilityStage, hapModuleInfo, callback]() {
482 auto ohosApplication = weak.lock();
483 if (ohosApplication == nullptr) {
484 TAG_LOGE(AAFwkTag::APPKIT, "null ohosApplication");
485 return;
486 }
487 ohosApplication->AutoStartupDone(abilityStage, hapModuleInfo);
488 if (callback == nullptr) {
489 TAG_LOGE(AAFwkTag::APPKIT, "null callback");
490 return;
491 }
492 callback();
493 };
494
495 return autoStartupCallback;
496 }
497
AutoStartupDone(const std::shared_ptr<AbilityLocalRecord> &abilityRecord, const std::shared_ptr<AbilityRuntime::AbilityStage> &abilityStage, const std::string &moduleName)498 void OHOSApplication::AutoStartupDone(const std::shared_ptr<AbilityLocalRecord> &abilityRecord,
499 const std::shared_ptr<AbilityRuntime::AbilityStage> &abilityStage, const std::string &moduleName)
500 {
501 if (abilityStage == nullptr) {
502 TAG_LOGE(AAFwkTag::APPKIT, "abilityStage is nullptr");
503 return;
504 }
505
506 Want want;
507 if (abilityRecord->GetWant()) {
508 TAG_LOGD(AAFwkTag::APPKIT, "want is ok, transport to abilityStage");
509 want = *(abilityRecord->GetWant());
510 }
511
512 abilityStage->OnCreate(want);
513 abilityStages_[moduleName] = abilityStage;
514 const sptr<IRemoteObject> &token = abilityRecord->GetToken();
515 if (token == nullptr) {
516 TAG_LOGE(AAFwkTag::APPKIT, "token is null");
517 return;
518 }
519 abilityStage->AddAbility(token, abilityRecord);
520 }
521
AutoStartupDone( const std::shared_ptr<AbilityRuntime::AbilityStage> &abilityStage, const AppExecFwk::HapModuleInfo &hapModuleInfo)522 void OHOSApplication::AutoStartupDone(
523 const std::shared_ptr<AbilityRuntime::AbilityStage> &abilityStage,
524 const AppExecFwk::HapModuleInfo &hapModuleInfo)
525 {
526 if (abilityStage == nullptr) {
527 TAG_LOGE(AAFwkTag::APPKIT, "abilityStage is nullptr");
528 return;
529 }
530
531 Want want;
532 abilityStage->OnCreate(want);
533 abilityStages_[hapModuleInfo.moduleName] = abilityStage;
534 TAG_LOGI(AAFwkTag::APPKIT, "abilityStage insert and initialization");
535 return;
536 }
537
538 /**
539 *
540 * @brief update the application info after new module installed.
541 *
542 * @param appInfo The latest application info obtained from bms for update abilityRuntimeContext.
543 *
544 */
UpdateApplicationInfoInstalled(const AppExecFwk::ApplicationInfo &appInfo)545 void OHOSApplication::UpdateApplicationInfoInstalled(const AppExecFwk::ApplicationInfo &appInfo)
546 {
547 TAG_LOGD(AAFwkTag::APPKIT, "called");
548
549 if (abilityRuntimeContext_ == nullptr) {
550 TAG_LOGE(AAFwkTag::APPKIT, "abilityRuntimeContext_ is nullptr");
551 return;
552 }
553
554 abilityRuntimeContext_->SetApplicationInfo(std::make_shared<AppExecFwk::ApplicationInfo>(appInfo));
555 }
556
AddAbilityStage( const AppExecFwk::HapModuleInfo &hapModuleInfo, const std::function<void()> &callback, bool &isAsyncCallback)557 bool OHOSApplication::AddAbilityStage(
558 const AppExecFwk::HapModuleInfo &hapModuleInfo,
559 const std::function<void()> &callback, bool &isAsyncCallback)
560 {
561 TAG_LOGD(AAFwkTag::APPKIT, "called");
562 if (abilityRuntimeContext_ == nullptr) {
563 TAG_LOGE(AAFwkTag::APPKIT, "abilityRuntimeContext_ is nullptr");
564 return false;
565 }
566
567 if (runtime_ == nullptr) {
568 TAG_LOGE(AAFwkTag::APPKIT, "runtime_ is nullptr");
569 return false;
570 }
571
572 if (abilityStages_.find(hapModuleInfo.moduleName) != abilityStages_.end()) {
573 TAG_LOGE(AAFwkTag::APPKIT, "object exist");
574 return false;
575 }
576
577 auto stageContext = std::make_shared<AbilityRuntime::AbilityStageContext>();
578 stageContext->SetParentContext(abilityRuntimeContext_);
579 stageContext->InitHapModuleInfo(hapModuleInfo);
580 stageContext->SetConfiguration(GetConfiguration());
581 auto moduleInfo = stageContext->GetHapModuleInfo();
582 if (moduleInfo == nullptr) {
583 TAG_LOGE(AAFwkTag::APPKIT, "moduleInfo is nullptr");
584 return false;
585 }
586
587 if (abilityRuntimeContext_->GetApplicationInfo() && abilityRuntimeContext_->GetApplicationInfo()->multiProjects) {
588 auto rm = stageContext->CreateModuleContext(hapModuleInfo.moduleName)->GetResourceManager();
589 stageContext->SetResourceManager(rm);
590 }
591
592 auto abilityStage = AbilityRuntime::AbilityStage::Create(runtime_, *moduleInfo);
593 if (abilityStage == nullptr) {
594 TAG_LOGE(AAFwkTag::APPKIT, "ability stage invalid");
595 return false;
596 }
597 auto application = std::static_pointer_cast<OHOSApplication>(shared_from_this());
598 std::weak_ptr<OHOSApplication> weak = application;
599 abilityStage->Init(stageContext, weak);
600 auto autoStartupCallback = CreateAutoStartupCallback(abilityStage, hapModuleInfo, callback);
601 if (autoStartupCallback != nullptr) {
602 abilityStage->RunAutoStartupTask(autoStartupCallback, isAsyncCallback, stageContext);
603 if (isAsyncCallback) {
604 TAG_LOGI(AAFwkTag::APPKIT, "waiting for startup");
605 return false;
606 }
607 }
608 Want want;
609 abilityStage->OnCreate(want);
610 abilityStages_[hapModuleInfo.moduleName] = abilityStage;
611 TAG_LOGI(AAFwkTag::APPKIT, "abilityStage insert and initialization");
612 return true;
613 }
614
CleanAbilityStage(const sptr<IRemoteObject> &token, const std::shared_ptr<AbilityInfo> &abilityInfo, bool isCacheProcess)615 void OHOSApplication::CleanAbilityStage(const sptr<IRemoteObject> &token,
616 const std::shared_ptr<AbilityInfo> &abilityInfo, bool isCacheProcess)
617 {
618 if (abilityInfo == nullptr) {
619 TAG_LOGE(AAFwkTag::APPKIT, "abilityInfo is nullptr");
620 return;
621 }
622 if (token == nullptr) {
623 TAG_LOGE(AAFwkTag::APPKIT, "token is nullptr");
624 return;
625 }
626 std::string moduleName = abilityInfo->moduleName;
627 auto iterator = abilityStages_.find(moduleName);
628 if (iterator != abilityStages_.end()) {
629 auto abilityStage = iterator->second;
630 if (abilityStage == nullptr) {
631 TAG_LOGE(AAFwkTag::APPKIT, "abilityStage is nullptr");
632 return;
633 }
634 abilityStage->RemoveAbility(token);
635 if (!abilityStage->ContainsAbility() && !isCacheProcess) {
636 abilityStage->OnDestroy();
637 abilityStages_.erase(moduleName);
638 }
639 }
640 }
641
GetAppContext() const642 std::shared_ptr<AbilityRuntime::Context> OHOSApplication::GetAppContext() const
643 {
644 return abilityRuntimeContext_;
645 }
646
GetRuntime()647 const std::unique_ptr<AbilityRuntime::Runtime>& OHOSApplication::GetRuntime()
648 {
649 return runtime_;
650 }
651
SetConfiguration(const Configuration &config)652 void OHOSApplication::SetConfiguration(const Configuration &config)
653 {
654 if (!configuration_) {
655 configuration_ = std::make_shared<Configuration>(config);
656 }
657 auto colorMode = config.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE);
658 AbilityRuntime::ApplicationConfigurationManager::GetInstance().
659 SetColorModeSetLevel(AbilityRuntime::SetLevel::System, colorMode);
660
661 if (abilityRuntimeContext_ && configuration_) {
662 abilityRuntimeContext_->SetConfiguration(configuration_);
663 }
664 }
665
ScheduleAcceptWant(const AAFwk::Want &want, const std::string &moduleName, std::string &flag)666 void OHOSApplication::ScheduleAcceptWant(const AAFwk::Want &want, const std::string &moduleName, std::string &flag)
667 {
668 TAG_LOGD(AAFwkTag::APPKIT, "called");
669 auto iter = abilityStages_.find(moduleName);
670 if (iter != abilityStages_.end()) {
671 auto abilityStage = iter->second;
672 if (abilityStage) {
673 flag = abilityStage->OnAcceptWant(want);
674 }
675 }
676 }
677
ScheduleNewProcessRequest(const AAFwk::Want &want, const std::string &moduleName, std::string &flag)678 void OHOSApplication::ScheduleNewProcessRequest(const AAFwk::Want &want, const std::string &moduleName,
679 std::string &flag)
680 {
681 TAG_LOGD(AAFwkTag::APPKIT, "call");
682 if (abilityStages_.empty()) {
683 TAG_LOGE(AAFwkTag::APPKIT, "abilityStages_ is empty");
684 return;
685 }
686 auto iter = abilityStages_.find(moduleName);
687 if (iter == abilityStages_.end()) {
688 TAG_LOGE(AAFwkTag::APPKIT, "%{public}s is not in abilityStage", moduleName.c_str());
689 return;
690 }
691 auto abilityStage = iter->second;
692 if (abilityStage) {
693 flag = abilityStage->OnNewProcessRequest(want);
694 }
695 }
696
GetConfiguration() const697 std::shared_ptr<Configuration> OHOSApplication::GetConfiguration() const
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 TAG_LOGD(AAFwkTag::APPKIT, "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 TAG_LOGD(AAFwkTag::APPKIT, "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 TAG_LOGD(AAFwkTag::APPKIT, "runtime is nullptr");
731 return true;
732 }
733
734 return runtime_->UnLoadRepairPatch(hqfFile);
735 }
736
CleanAppTempData(bool isLastProcess)737 void OHOSApplication::CleanAppTempData(bool isLastProcess)
738 {
739 if (!isLastProcess) {
740 TAG_LOGE(AAFwkTag::APPKIT, "failed");
741 return;
742 }
743 if (abilityRuntimeContext_ == nullptr) {
744 TAG_LOGE(AAFwkTag::APPKIT, "Context is nullptr");
745 return;
746 }
747
748 auto cleaner = ApplicationCleaner::GetInstance();
749 if (cleaner) {
750 cleaner->SetRuntimeContext(abilityRuntimeContext_);
751 cleaner->RenameTempData();
752 }
753 }
754
CleanUselessTempData()755 void OHOSApplication::CleanUselessTempData()
756 {
757 if (abilityRuntimeContext_ == nullptr) {
758 TAG_LOGE(AAFwkTag::APPKIT, "Context is nullptr");
759 return;
760 }
761
762 auto cleaner = ApplicationCleaner::GetInstance();
763 if (cleaner) {
764 cleaner->SetRuntimeContext(abilityRuntimeContext_);
765 cleaner->ClearTempData();
766 }
767 }
768
UpdateAppContextResMgr(const Configuration &config)769 void OHOSApplication::UpdateAppContextResMgr(const Configuration &config)
770 {
771 auto context = GetAppContext();
772 if (context == nullptr) {
773 TAG_LOGE(AAFwkTag::APPKIT, "Application context is nullptr");
774 return;
775 }
776
777 auto configUtils = std::make_shared<AbilityRuntime::ConfigurationUtils>();
778 configUtils->UpdateGlobalConfig(config, context->GetResourceManager());
779 }
780
CleanEmptyAbilityStage()781 void OHOSApplication::CleanEmptyAbilityStage()
782 {
783 bool containsNonEmpty = false;
784 for (auto it = abilityStages_.begin(); it != abilityStages_.end();) {
785 auto abilityStage = it->second;
786 if (abilityStage == nullptr) {
787 it++;
788 continue;
789 }
790 if (!abilityStage->ContainsAbility()) {
791 abilityStage->OnDestroy();
792 it = abilityStages_.erase(it);
793 } else {
794 containsNonEmpty = true;
795 it++;
796 }
797 }
798 if (containsNonEmpty) {
799 TAG_LOGI(AAFwkTag::APPKIT, "Application contains none empty abilityStage");
800 }
801 }
802
IsUpdateColorNeeded(Configuration &config, AbilityRuntime::SetLevel level)803 bool OHOSApplication::IsUpdateColorNeeded(Configuration &config, AbilityRuntime::SetLevel level)
804 {
805 std::string colorMode = config.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE);
806 std::string colorModeIsSetBySa =
807 config.GetItem(AAFwk::GlobalConfigurationKey::COLORMODE_IS_SET_BY_SA);
808 if (level < AbilityRuntime::SetLevel::SA && !colorModeIsSetBySa.empty()) {
809 level = AbilityRuntime::SetLevel::SA;
810 }
811
812 TAG_LOGI(AAFwkTag::APPKIT, "current %{public}d, pre %{public}d",
813 static_cast<uint8_t>(level),
814 static_cast<uint8_t>(AbilityRuntime::ApplicationConfigurationManager::GetInstance().GetColorModeSetLevel()));
815
816 bool needUpdate = true;
817
818 if (level < AbilityRuntime::ApplicationConfigurationManager::GetInstance().GetColorModeSetLevel() ||
819 colorMode.empty()) {
820 config.RemoveItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE);
821 config.RemoveItem(AAFwk::GlobalConfigurationKey::COLORMODE_IS_SET_BY_SA);
822 TAG_LOGI(AAFwkTag::APPKIT, "color remove");
823 needUpdate = false;
824 }
825
826 if (!colorMode.empty()) {
827 config.AddItem(AAFwk::GlobalConfigurationKey::SYSTEM_COLORMODE,
828 AbilityRuntime::ApplicationConfigurationManager::GetInstance().SetColorModeSetLevel(level, colorMode));
829
830 if (level > AbilityRuntime::SetLevel::System) {
831 config.AddItem(AAFwk::GlobalConfigurationKey::COLORMODE_IS_SET_BY_APP,
832 AppExecFwk::ConfigurationInner::IS_SET_BY_APP);
833 }
834 }
835
836 return needUpdate;
837 }
838
isUpdateFontSize(Configuration &config, AbilityRuntime::SetLevel level)839 bool OHOSApplication::isUpdateFontSize(Configuration &config, AbilityRuntime::SetLevel level)
840 {
841 std::string fontSize = config.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_FONT_SIZE_SCALE);
842 if (fontSize.empty()) {
843 TAG_LOGW(AAFwkTag::APPKIT, "fontSize is empty");
844 return false;
845 }
846
847 auto preLevle = ApplicationConfigurationManager::GetInstance().GetFontSetLevel();
848 if (level < preLevle) {
849 config.RemoveItem(AAFwk::GlobalConfigurationKey::SYSTEM_FONT_SIZE_SCALE);
850 return false;
851 }
852
853 std::string globalFontFollowSysteme = configuration_->GetItem(AAFwk::GlobalConfigurationKey::APP_FONT_SIZE_SCALE);
854 if (level == preLevle && !globalFontFollowSysteme.empty()) {
855 if (globalFontFollowSysteme.compare(ConfigurationInner::IS_APP_FONT_FOLLOW_SYSTEM) == 0) {
856 return true;
857 }
858 config.RemoveItem(AAFwk::GlobalConfigurationKey::SYSTEM_FONT_SIZE_SCALE);
859 return false;
860 }
861
862 // level > preLevle
863 configuration_->RemoveItem(AAFwk::GlobalConfigurationKey::APP_FONT_SIZE_SCALE);
864 ApplicationConfigurationManager::GetInstance().SetfontSetLevel(level);
865 return true;
866 }
867
IsUpdateLanguageNeeded(Configuration &config, AbilityRuntime::SetLevel level)868 bool OHOSApplication::IsUpdateLanguageNeeded(Configuration &config, AbilityRuntime::SetLevel level)
869 {
870 TAG_LOGI(AAFwkTag::APPKIT, "current %{public}d, pre %{public}d",
871 static_cast<uint8_t>(level),
872 static_cast<uint8_t>(AbilityRuntime::ApplicationConfigurationManager::GetInstance().GetLanguageSetLevel()));
873
874 std::string language = config.GetItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE);
875 if (language.empty()) {
876 TAG_LOGW(AAFwkTag::APPKIT, "language is empty");
877 return false;
878 }
879 if (level < AbilityRuntime::ApplicationConfigurationManager::GetInstance().GetLanguageSetLevel()) {
880 config.RemoveItem(AAFwk::GlobalConfigurationKey::SYSTEM_LANGUAGE);
881 TAG_LOGI(AAFwkTag::APPKIT, "language remove");
882 return false;
883 }
884 AbilityRuntime::ApplicationConfigurationManager::GetInstance().SetLanguageSetLevel(level);
885 config.AddItem(AAFwk::GlobalConfigurationKey::IS_PREFERRED_LANGUAGE,
886 level == AbilityRuntime::SetLevel::Application ? "1" : "0");
887 return true;
888 }
889
IsMainProcess(const std::string &bundleName, const std::string &process)890 bool OHOSApplication::IsMainProcess(const std::string &bundleName, const std::string &process)
891 {
892 auto processInfo = GetProcessInfo();
893 if (processInfo == nullptr) {
894 TAG_LOGE(AAFwkTag::APPKIT, "null processInfo");
895 return false;
896 }
897 ProcessType processType = processInfo->GetProcessType();
898 if (processType == ProcessType::NORMAL) {
899 return true;
900 }
901
902 std::string processName = processInfo->GetProcessName();
903 if (processName == bundleName || processName == process) {
904 return true;
905 }
906 TAG_LOGD(AAFwkTag::APPKIT, "not main process");
907 return false;
908 }
909 } // namespace AppExecFwk
910 } // namespace OHOS
911