1 /*
2 * Copyright (c) 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 "ui_extension_record_factory.h"
17
18 #include "hilog_tag_wrapper.h"
19 #include "multi_instance_utils.h"
20 #include "ui_extension_record.h"
21
22 namespace OHOS {
23 namespace AbilityRuntime {
24 namespace {
25 const std::string UIEXTENSION_ABILITY_ID = "ability.want.params.uiExtensionAbilityId";
26 }
27
28 UIExtensionRecordFactory::UIExtensionRecordFactory() = default;
29
30 UIExtensionRecordFactory::~UIExtensionRecordFactory() = default;
31
NeedReuse(const AAFwk::AbilityRequest & abilityRequest,int32_t & extensionRecordId)32 bool UIExtensionRecordFactory::NeedReuse(const AAFwk::AbilityRequest &abilityRequest, int32_t &extensionRecordId)
33 {
34 auto sessionInfo = abilityRequest.sessionInfo;
35 if (sessionInfo == nullptr) {
36 TAG_LOGE(AAFwkTag::ABILITYMGR, "null sessionInfo, no reuse");
37 return false;
38 }
39 int32_t uiExtensionAbilityId = sessionInfo->want.GetIntParam(UIEXTENSION_ABILITY_ID,
40 INVALID_EXTENSION_RECORD_ID);
41 if (uiExtensionAbilityId == INVALID_EXTENSION_RECORD_ID) {
42 TAG_LOGD(AAFwkTag::ABILITYMGR, "UIEXTENSION_ABILITY_ID is not config, no reuse");
43 return false;
44 }
45 TAG_LOGI(AAFwkTag::ABILITYMGR, "UIExtensionAbility id: %{public}d.", uiExtensionAbilityId);
46 extensionRecordId = uiExtensionAbilityId;
47 return true;
48 }
49
PreCheck(const AAFwk::AbilityRequest & abilityRequest,const std::string & hostBundleName)50 int32_t UIExtensionRecordFactory::PreCheck(
51 const AAFwk::AbilityRequest &abilityRequest, const std::string &hostBundleName)
52 {
53 return ExtensionRecordFactory::PreCheck(abilityRequest, hostBundleName);
54 }
55
CreateRecord(const AAFwk::AbilityRequest & abilityRequest,std::shared_ptr<ExtensionRecord> & extensionRecord)56 int32_t UIExtensionRecordFactory::CreateRecord(
57 const AAFwk::AbilityRequest &abilityRequest, std::shared_ptr<ExtensionRecord> &extensionRecord)
58 {
59 auto abilityRecord = AAFwk::AbilityRecord::CreateAbilityRecord(abilityRequest);
60 if (abilityRecord == nullptr) {
61 TAG_LOGE(AAFwkTag::ABILITYMGR, "create record failed");
62 return ERR_NULL_OBJECT;
63 }
64 if (abilityRequest.extensionType == AppExecFwk::ExtensionAbilityType::EMBEDDED_UI &&
65 abilityRequest.sessionInfo != nullptr) {
66 auto callerToken = abilityRequest.sessionInfo->callerToken;
67 auto callerAbilityRecord = AAFwk::Token::GetAbilityRecordByToken(callerToken);
68 if (callerAbilityRecord != nullptr) {
69 int32_t appIndex = callerAbilityRecord->GetAppIndex();
70 abilityRecord->SetAppIndex(appIndex);
71 abilityRecord->SetWantAppIndex(appIndex);
72 }
73 }
74 if (AAFwk::MultiInstanceUtils::IsMultiInstanceApp(abilityRequest.appInfo)) {
75 abilityRecord->SetInstanceKey(AAFwk::MultiInstanceUtils::GetValidExtensionInstanceKey(abilityRequest));
76 }
77 CreateDebugRecord(abilityRequest, abilityRecord);
78 extensionRecord = std::make_shared<UIExtensionRecord>(abilityRecord);
79 uint32_t processMode = GetExtensionProcessMode(abilityRequest, extensionRecord->isHostSpecified_);
80 extensionRecord->processMode_ = processMode;
81 abilityRecord->SetExtensionProcessMode(processMode);
82 return ERR_OK;
83 }
84
CreateDebugRecord(const AAFwk::AbilityRequest & abilityRequest,std::shared_ptr<AAFwk::AbilityRecord> abilityRecord)85 void UIExtensionRecordFactory::CreateDebugRecord(
86 const AAFwk::AbilityRequest &abilityRequest, std::shared_ptr<AAFwk::AbilityRecord> abilityRecord)
87 {
88 auto callerRecord = AAFwk::Token::GetAbilityRecordByToken(abilityRequest.callerToken);
89 if (!callerRecord) {
90 TAG_LOGD(AAFwkTag::ABILITYMGR, "No caller record");
91 return;
92 }
93 if (!callerRecord->IsDebug() ||
94 callerRecord->GetApplicationInfo().appProvisionType !=
95 AppExecFwk::Constants::APP_PROVISION_TYPE_DEBUG) {
96 TAG_LOGD(AAFwkTag::ABILITYMGR, "Not debug UIExtension");
97 return;
98 }
99 auto callerBundleName = callerRecord->GetAbilityInfo().bundleName;
100 auto isSameApp = callerBundleName == abilityRequest.abilityInfo.bundleName;
101 auto isCallerUIAbility = callerRecord->GetAbilityInfo().type == AppExecFwk::AbilityType::PAGE;
102 if (isSameApp && isCallerUIAbility) {
103 TAG_LOGD(AAFwkTag::ABILITYMGR, "Setting up debug UIExtension");
104 abilityRecord->SetDebugUIExtension();
105 }
106 }
107 } // namespace AbilityRuntime
108 } // namespace OHOS
109