1 /*
2 * Copyright (c) 2023-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 "disposed_observer.h"
17
18 #include "interceptor/disposed_rule_interceptor.h"
19 #include "ability_record.h"
20 #include "modal_system_ui_extension.h"
21
22 namespace OHOS {
23 namespace AAFwk {
24 namespace {
25 constexpr const char* UIEXTENSION_MODAL_TYPE = "ability.want.params.modalType";
26 constexpr const char* INTERCEPT_MISSION_ID = "intercept_missionId";
27 }
28
DisposedObserver(const AppExecFwk::DisposedRule & disposedRule,const std::shared_ptr<DisposedRuleInterceptor> & interceptor,int32_t uid)29 DisposedObserver::DisposedObserver(const AppExecFwk::DisposedRule &disposedRule,
30 const std::shared_ptr<DisposedRuleInterceptor> &interceptor, int32_t uid)
31 : interceptor_(interceptor), disposedRule_(disposedRule), uid_(uid)
32 {}
33
OnAbilityStateChanged(const AppExecFwk::AbilityStateData & abilityStateData)34 void DisposedObserver::OnAbilityStateChanged(const AppExecFwk::AbilityStateData &abilityStateData)
35 {
36 std::lock_guard<ffrt::mutex> guard(observerLock_);
37 if (abilityStateData.abilityState != static_cast<int32_t>(AppExecFwk::AbilityState::ABILITY_STATE_FOREGROUND)) {
38 return;
39 }
40 TAG_LOGD(AAFwkTag::ABILITYMGR, "Call");
41 token_ = abilityStateData.token;
42 auto abilityRecord = Token::GetAbilityRecordByToken(token_);
43 if (abilityRecord && !abilityRecord->GetAbilityInfo().isStageBasedModel) {
44 auto systemUIExtension = std::make_shared<OHOS::Rosen::ModalSystemUiExtension>();
45 Want want = *disposedRule_.want;
46 want.SetParam(UIEXTENSION_MODAL_TYPE, 1);
47 auto sessionInfo = abilityRecord->GetSessionInfo();
48 if (sessionInfo != nullptr) {
49 want.SetParam(INTERCEPT_MISSION_ID, sessionInfo->persistentId);
50 } else {
51 want.SetParam(INTERCEPT_MISSION_ID, abilityRecord->GetMissionId());
52 }
53 TAG_LOGD(AAFwkTag::ABILITYMGR, "FA modal system");
54 bool ret = IN_PROCESS_CALL(systemUIExtension->CreateModalUIExtension(want));
55 if (!ret) {
56 TAG_LOGE(AAFwkTag::ABILITYMGR, "call failed");
57 }
58 interceptor_->UnregisterObserver(abilityStateData.uid);
59 }
60 }
61
OnPageShow(const AppExecFwk::PageStateData & pageStateData)62 void DisposedObserver::OnPageShow(const AppExecFwk::PageStateData &pageStateData)
63 {
64 if (pageStateData.uid != uid_) {
65 TAG_LOGI(AAFwkTag::ABILITYMGR, "currentUid:%{public}d, paramUid:%{public}d", uid_, pageStateData.uid);
66 return;
67 }
68 if (disposedRule_.componentType == AppExecFwk::ComponentType::UI_ABILITY) {
69 TAG_LOGD(AAFwkTag::ABILITYMGR, "Call");
70 int ret = IN_PROCESS_CALL(AbilityManagerClient::GetInstance()->StartAbility(*disposedRule_.want));
71 if (ret != ERR_OK) {
72 interceptor_->UnregisterObserver(pageStateData.uid);
73 TAG_LOGE(AAFwkTag::ABILITYMGR, "call failed");
74 return;
75 }
76 }
77 if (disposedRule_.componentType == AppExecFwk::ComponentType::UI_EXTENSION) {
78 auto abilityRecord = Token::GetAbilityRecordByToken(token_);
79 if (abilityRecord == nullptr || abilityRecord->GetAbilityInfo().type != AppExecFwk::AbilityType::PAGE) {
80 auto systemUIExtension = std::make_shared<OHOS::Rosen::ModalSystemUiExtension>();
81 Want want = *disposedRule_.want;
82 want.SetParam(UIEXTENSION_MODAL_TYPE, 1);
83 TAG_LOGD(AAFwkTag::ABILITYMGR, "modal system");
84 bool ret = IN_PROCESS_CALL(systemUIExtension->CreateModalUIExtension(want));
85 if (!ret) {
86 interceptor_->UnregisterObserver(pageStateData.uid);
87 TAG_LOGE(AAFwkTag::ABILITYMGR, "call failed");
88 return;
89 }
90 } else {
91 Want want = *disposedRule_.want;
92 auto sessionInfo = abilityRecord->GetSessionInfo();
93 if (sessionInfo != nullptr) {
94 want.SetParam(INTERCEPT_MISSION_ID, sessionInfo->persistentId);
95 } else {
96 want.SetParam(INTERCEPT_MISSION_ID, abilityRecord->GetMissionId());
97 }
98 TAG_LOGD(AAFwkTag::ABILITYMGR, "modal app");
99 int ret = abilityRecord->CreateModalUIExtension(want);
100 if (ret != ERR_OK) {
101 interceptor_->UnregisterObserver(pageStateData.uid);
102 TAG_LOGE(AAFwkTag::ABILITYMGR, "call failed");
103 return;
104 }
105 }
106 }
107 interceptor_->UnregisterObserver(pageStateData.uid);
108 }
109 } // namespace AAFwk
110 } // namespace OHOS
111