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