1 /*
2 * Copyright (c) 2022-2023 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 #include "bundle_manager_adapter.h"
16 #include "account_log_wrapper.h"
17
18 namespace OHOS {
19 namespace AccountSA {
20 namespace {
21 const std::string STRING_BUNDLE_NAME_NOT_INSTALLED = "com.example.not_installed";
22 const std::string STRING_OWNER = "com.example.owner";
23 const std::string STRING_NORMAL_BUNDLENAME = "com.example.normal.bundle";
24 const std::string STRING_BUNDLEINFO_WITH_NO_VALID_EXTENSION = "com.bundleInfo.noExtension";
25 const std::string STRING_BUNDLEINFO_WITH_NO_VALID_TYPE_EXTENSION = "com.bundleInfo.noValidTypeExtension";
26 const std::string STRING_BUNDLEINFO_WITH_MULTIPLE_VALID_EXTENSION = "com.bundleInfo.noExtension";
27 const std::string STRING_ABILITY_NAME = "com.example.owner.MainAbility";
28 const std::string STRING_ABILITY_NAME_TWO = "com.example.owner.MainAbility2";
29 const std::string STRING_ABILITY_NAME_WITH_NO_INFO = "com.example.owner.MainAbilityWithNoInfo";
30 const std::string STRING_ABILITY_NAME_WITH_CONNECT_FAILED = "com.example.MainAbilityWithConnectFailed";
31 const std::string STRING_ABILITY_NAME_WITH_NO_PROXY = "com.example.MainAbilityWithNoProxy";
32 } // namespace
33
GetInstance()34 BundleManagerAdapter *BundleManagerAdapter::GetInstance()
35 {
36 static BundleManagerAdapter *instance = new (std::nothrow) BundleManagerAdapter();
37 return instance;
38 }
39
BundleManagerAdapter()40 BundleManagerAdapter::BundleManagerAdapter()
41 {
42 ACCOUNT_LOGI("create BundleManagerAdapter mock");
43 }
44
~BundleManagerAdapter()45 BundleManagerAdapter::~BundleManagerAdapter()
46 {
47 ACCOUNT_LOGI("destroy BundleManagerAdapter mock");
48 }
49
GetNameForUid(const int uid,std::string & bundleName)50 ErrCode BundleManagerAdapter::GetNameForUid(const int uid, std::string &bundleName)
51 {
52 ACCOUNT_LOGI("mock enter, uid = %{public}d", uid);
53 bundleName = STRING_OWNER;
54 ACCOUNT_LOGI("mock bundleName = %{public}s", bundleName.c_str());
55 return ERR_OK;
56 }
57
GetBundleInfo(const std::string & bundleName,const AppExecFwk::BundleFlag flag,AppExecFwk::BundleInfo & bundleInfo,int32_t userId)58 bool BundleManagerAdapter::GetBundleInfo(const std::string &bundleName, const AppExecFwk::BundleFlag flag,
59 AppExecFwk::BundleInfo &bundleInfo, int32_t userId)
60 {
61 ACCOUNT_LOGI("mock enter, bundleName = %{public}s", bundleName.c_str());
62 if (bundleName == STRING_BUNDLE_NAME_NOT_INSTALLED) {
63 return false;
64 }
65 if (bundleName == STRING_NORMAL_BUNDLENAME) {
66 AppExecFwk::ExtensionAbilityInfo extensionInfo;
67 extensionInfo.name = STRING_ABILITY_NAME;
68 extensionInfo.type = AppExecFwk::ExtensionAbilityType::APP_ACCOUNT_AUTHORIZATION;
69 bundleInfo.extensionInfos.emplace_back(extensionInfo);
70 return true;
71 }
72 if (bundleName == STRING_BUNDLEINFO_WITH_NO_VALID_EXTENSION) {
73 return true;
74 }
75 if (bundleName == STRING_BUNDLEINFO_WITH_NO_VALID_TYPE_EXTENSION) {
76 AppExecFwk::ExtensionAbilityInfo extensionInfo;
77 extensionInfo.name = STRING_ABILITY_NAME;
78 bundleInfo.extensionInfos.emplace_back(extensionInfo);
79 return true;
80 }
81 if (bundleName == STRING_BUNDLEINFO_WITH_MULTIPLE_VALID_EXTENSION) {
82 AppExecFwk::ExtensionAbilityInfo extensionInfo1;
83 extensionInfo1.name = STRING_ABILITY_NAME;
84 extensionInfo1.type = AppExecFwk::ExtensionAbilityType::APP_ACCOUNT_AUTHORIZATION;
85 bundleInfo.extensionInfos.emplace_back(extensionInfo1);
86 AppExecFwk::ExtensionAbilityInfo extensionInfo2;
87 extensionInfo2.name = STRING_ABILITY_NAME_TWO;
88 extensionInfo2.type = AppExecFwk::ExtensionAbilityType::APP_ACCOUNT_AUTHORIZATION;
89 bundleInfo.extensionInfos.emplace_back(extensionInfo2);
90 return true;
91 }
92 return true;
93 }
94
QueryAbilityInfos(const AAFwk::Want & want,int32_t flags,int32_t userId,std::vector<AppExecFwk::AbilityInfo> & abilityInfos)95 bool BundleManagerAdapter::QueryAbilityInfos(const AAFwk::Want &want, int32_t flags, int32_t userId,
96 std::vector<AppExecFwk::AbilityInfo> &abilityInfos)
97 {
98 ACCOUNT_LOGI("mock enter, userId = %{public}d", userId);
99 return false;
100 }
101
QueryExtensionAbilityInfos(const AAFwk::Want & want,const int32_t & flag,const int32_t & userId,std::vector<AppExecFwk::ExtensionAbilityInfo> & extensionInfos)102 bool BundleManagerAdapter::QueryExtensionAbilityInfos(const AAFwk::Want &want, const int32_t &flag,
103 const int32_t &userId, std::vector<AppExecFwk::ExtensionAbilityInfo> &extensionInfos)
104 {
105 ACCOUNT_LOGI("mock enter, userId = %{public}d", userId);
106 return false;
107 }
108
QueryExtensionAbilityInfos(const AAFwk::Want & want,const AppExecFwk::ExtensionAbilityType & extensionType,const int32_t & flag,const int32_t & userId,std::vector<AppExecFwk::ExtensionAbilityInfo> & extensionInfos)109 bool BundleManagerAdapter::QueryExtensionAbilityInfos(
110 const AAFwk::Want &want, const AppExecFwk::ExtensionAbilityType &extensionType,
111 const int32_t &flag, const int32_t &userId, std::vector<AppExecFwk::ExtensionAbilityInfo> &extensionInfos)
112 {
113 ACCOUNT_LOGI("mock enter, userId = %{public}d", userId);
114 AppExecFwk::ElementName element = want.GetElement();
115 std::string abilityName = element.GetAbilityName();
116 ACCOUNT_LOGI("mock enter, abilityName = %{public}s", abilityName.c_str());
117 if ((abilityName == STRING_ABILITY_NAME) || (abilityName == STRING_ABILITY_NAME_WITH_CONNECT_FAILED) ||
118 (abilityName == STRING_ABILITY_NAME_WITH_NO_PROXY)) {
119 AppExecFwk::ExtensionAbilityInfo extensionInfo;
120 extensionInfo.name = abilityName;
121 extensionInfo.type = AppExecFwk::ExtensionAbilityType::APP_ACCOUNT_AUTHORIZATION;
122 extensionInfos.emplace_back(extensionInfo);
123 return true;
124 }
125 if (abilityName == STRING_ABILITY_NAME_WITH_NO_INFO) {
126 return true;
127 }
128 return false;
129 }
130
GetUidByBundleName(const std::string & bundleName,const int userId)131 int BundleManagerAdapter::GetUidByBundleName(const std::string &bundleName, const int userId)
132 {
133 ACCOUNT_LOGI("mock enter, bundleName = %{public}s, userId = %{public}d.", bundleName.c_str(), userId);
134 return -1;
135 }
136 } // namespace AccountSA
137 } // namespace OHOS