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 "multi_instance_utils.h"
17
18 #include <vector>
19 #include <unordered_map>
20
21 #include "ability_util.h"
22 #include "app_mgr_util.h"
23 #include "scene_board_judgement.h"
24
25 namespace OHOS {
26 namespace AAFwk {
27 namespace {
28 constexpr const char* APP_INSTANCE_KEY_0 = "app_instance_0";
29 }
GetInstanceKey(const Want& want)30 std::string MultiInstanceUtils::GetInstanceKey(const Want& want)
31 {
32 return want.GetStringParam(Want::APP_INSTANCE_KEY);
33 }
34
GetValidExtensionInstanceKey(const AbilityRequest &abilityRequest)35 std::string MultiInstanceUtils::GetValidExtensionInstanceKey(const AbilityRequest &abilityRequest)
36 {
37 if (!IsSupportedExtensionType(abilityRequest.abilityInfo.extensionAbilityType)) {
38 return APP_INSTANCE_KEY_0;
39 }
40 auto instanceKey = abilityRequest.want.GetStringParam(Want::APP_INSTANCE_KEY);
41 if (instanceKey.empty()) {
42 instanceKey = GetSelfCallerInstanceKey(abilityRequest);
43 if (instanceKey.empty()) {
44 return APP_INSTANCE_KEY_0;
45 }
46 }
47 return instanceKey;
48 }
49
GetSelfCallerInstanceKey(const AbilityRequest &abilityRequest)50 std::string MultiInstanceUtils::GetSelfCallerInstanceKey(const AbilityRequest &abilityRequest)
51 {
52 auto callerRecord = Token::GetAbilityRecordByToken(abilityRequest.callerToken);
53 if (callerRecord && callerRecord->GetAbilityInfo().bundleName == abilityRequest.want.GetBundle()) {
54 return callerRecord->GetInstanceKey();
55 }
56 return "";
57 }
58
IsDefaultInstanceKey(const std::string& key)59 bool MultiInstanceUtils::IsDefaultInstanceKey(const std::string& key)
60 {
61 return key == APP_INSTANCE_KEY_0;
62 }
63
IsMultiInstanceApp(AppExecFwk::ApplicationInfo appInfo)64 bool MultiInstanceUtils::IsMultiInstanceApp(AppExecFwk::ApplicationInfo appInfo)
65 {
66 if (appInfo.multiAppMode.multiAppModeType != AppExecFwk::MultiAppModeType::MULTI_INSTANCE) {
67 return false;
68 }
69 if (!Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
70 return false;
71 }
72 return true;
73 }
74
IsSupportedExtensionType(AppExecFwk::ExtensionAbilityType type)75 bool MultiInstanceUtils::IsSupportedExtensionType(AppExecFwk::ExtensionAbilityType type)
76 {
77 static const std::unordered_set<AppExecFwk::ExtensionAbilityType> supportMultiInstanceExtensionSet {
78 AppExecFwk::ExtensionAbilityType::WORK_SCHEDULER,
79 AppExecFwk::ExtensionAbilityType::BACKUP,
80 AppExecFwk::ExtensionAbilityType::SHARE
81 };
82 return supportMultiInstanceExtensionSet.find(type) != supportMultiInstanceExtensionSet.end();
83 }
84
IsInstanceKeyExist(const std::string& bundleName, const std::string& key)85 bool MultiInstanceUtils::IsInstanceKeyExist(const std::string& bundleName, const std::string& key)
86 {
87 auto appMgr = AppMgrUtil::GetAppMgr();
88 CHECK_POINTER_AND_RETURN(appMgr, false);
89 std::vector<std::string> instanceKeyArray;
90 auto result = IN_PROCESS_CALL(appMgr->GetAllRunningInstanceKeysByBundleName(bundleName, instanceKeyArray));
91 if (result != ERR_OK) {
92 TAG_LOGE(AAFwkTag::ABILITYMGR, "Failed to get instance key");
93 return false;
94 }
95 for (const auto& item: instanceKeyArray) {
96 if (item == key) {
97 return true;
98 }
99 }
100 return false;
101 }
102 } // namespace AAFwk
103 } // namespace OHOS