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 "ui_extension_record.h"
20 
21 namespace OHOS {
22 namespace AbilityRuntime {
23 namespace {
24 const std::string UIEXTENSION_ABILITY_ID = "ability.want.params.uiExtensionAbilityId";
25 }
26 
27 UIExtensionRecordFactory::UIExtensionRecordFactory() = default;
28 
29 UIExtensionRecordFactory::~UIExtensionRecordFactory() = default;
30 
NeedReuse(const AAFwk::AbilityRequest & abilityRequest,int32_t & extensionRecordId)31 bool UIExtensionRecordFactory::NeedReuse(const AAFwk::AbilityRequest &abilityRequest, int32_t &extensionRecordId)
32 {
33     int32_t uiExtensionAbilityId = abilityRequest.sessionInfo->want.GetIntParam(UIEXTENSION_ABILITY_ID,
34         INVALID_EXTENSION_RECORD_ID);
35     if (uiExtensionAbilityId == INVALID_EXTENSION_RECORD_ID) {
36         TAG_LOGD(AAFwkTag::ABILITYMGR, "UIEXTENSION_ABILITY_ID is not config, no reuse");
37         return false;
38     }
39     TAG_LOGI(AAFwkTag::ABILITYMGR, "UIExtensionAbility id: %{public}d.", uiExtensionAbilityId);
40     extensionRecordId = uiExtensionAbilityId;
41     return true;
42 }
43 
PreCheck(const AAFwk::AbilityRequest & abilityRequest,const std::string & hostBundleName)44 int32_t UIExtensionRecordFactory::PreCheck(
45     const AAFwk::AbilityRequest &abilityRequest, const std::string &hostBundleName)
46 {
47     return ExtensionRecordFactory::PreCheck(abilityRequest, hostBundleName);
48 }
49 
CreateRecord(const AAFwk::AbilityRequest & abilityRequest,std::shared_ptr<ExtensionRecord> & extensionRecord)50 int32_t UIExtensionRecordFactory::CreateRecord(
51     const AAFwk::AbilityRequest &abilityRequest, std::shared_ptr<ExtensionRecord> &extensionRecord)
52 {
53     auto abilityRecord = AAFwk::AbilityRecord::CreateAbilityRecord(abilityRequest);
54     if (abilityRecord == nullptr) {
55         TAG_LOGE(AAFwkTag::ABILITYMGR, "Failed to create ability record");
56         return ERR_NULL_OBJECT;
57     }
58     extensionRecord = std::make_shared<UIExtensionRecord>(abilityRecord);
59     extensionRecord->processMode_ = GetExtensionProcessMode(abilityRequest, extensionRecord->isHostSpecified_);
60     return ERR_OK;
61 }
62 } // namespace AbilityRuntime
63 } // namespace OHOS
64