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