1 /*
2 * Copyright (c) 2021-2022 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 "print_service_helper.h"
17 #include <thread>
18 #include "accesstoken_kit.h"
19 #include "ipc_skeleton.h"
20 #include "iservice_registry.h"
21 #include "os_account_manager.h"
22 #include "print_constant.h"
23 #include "print_log.h"
24 #include "system_ability_definition.h"
25 #include "common_event_data.h"
26 #include "common_event_manager.h"
27 #include "common_event_support.h"
28
29 namespace OHOS::Print {
30 const uint32_t MAX_RETRY_TIMES = 2;
31 const uint32_t START_ABILITY_INTERVAL = 200;
32 using namespace Security::AccessToken;
33
~PrintServiceHelper()34 PrintServiceHelper::~PrintServiceHelper()
35 {
36 }
37
CheckPermission(const std::string & name)38 bool PrintServiceHelper::CheckPermission(const std::string &name)
39 {
40 AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID();
41 TypeATokenTypeEnum tokenType = AccessTokenKit::GetTokenTypeFlag(tokenId);
42 if (tokenType == TOKEN_INVALID) {
43 PRINT_HILOGE("invalid token id %{public}d", tokenId);
44 return false;
45 }
46 int result = AccessTokenKit::VerifyAccessToken(tokenId, name);
47 if (result != PERMISSION_GRANTED) {
48 if (name == PERMISSION_NAME_PRINT) {
49 result = AccessTokenKit::VerifyAccessToken(tokenId, PERMISSION_NAME_PRINT_JOB);
50 }
51 PRINT_HILOGE("Current tokenId permission is %{public}d", result);
52 }
53 return result == PERMISSION_GRANTED;
54 }
55
StartAbility(const AAFwk::Want & want)56 bool PrintServiceHelper::StartAbility(const AAFwk::Want &want)
57 {
58 AppExecFwk::ElementName element = want.GetElement();
59 AAFwk::AbilityManagerClient::GetInstance()->Connect();
60 uint32_t retry = 0;
61 while (retry++ < MAX_RETRY_TIMES) {
62 PRINT_HILOGD("PrintServiceHelper::StartAbility %{public}s %{public}s",
63 element.GetBundleName().c_str(), element.GetAbilityName().c_str());
64 auto ret = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
65 if (!ret) {
66 break;
67 } else {
68 PRINT_HILOGE("PrintServiceHelper::StartAbility failed, err is %{public}d, retry is %{public}d", ret, retry);
69 std::this_thread::sleep_for(std::chrono::milliseconds(START_ABILITY_INTERVAL));
70 }
71 }
72 if (retry > MAX_RETRY_TIMES) {
73 PRINT_HILOGE("PrintServiceHelper::StartAbility --> failed ");
74 return false;
75 }
76 return true;
77 }
78
StartPluginPrintIconExtAbility(const AAFwk::Want & want)79 bool PrintServiceHelper::StartPluginPrintIconExtAbility(const AAFwk::Want &want)
80 {
81 PRINT_HILOGD("enter PrintServiceHelper::StartPluginPrintIconExtAbility");
82 PRINT_HILOGD("want: %{public}s", want.ToUri().c_str());
83 AppExecFwk::ElementName element = want.GetElement();
84 AAFwk::AbilityManagerClient::GetInstance()->Connect();
85 uint32_t retry = 0;
86 sptr<PrintAbilityConnection> printAbilityConnection = new (std::nothrow) PrintAbilityConnection();
87 if (printAbilityConnection == nullptr) {
88 PRINT_HILOGE("fail to create printAbilityConnection");
89 return false;
90 }
91 PRINT_HILOGD("PrintServiceHelper::StartPluginPrintIconExtAbility %{public}s %{public}s",
92 element.GetBundleName().c_str(),
93 element.GetAbilityName().c_str());
94 while (retry++ < MAX_RETRY_TIMES) {
95 if (AAFwk::AbilityManagerClient::GetInstance()->ConnectAbility(want, printAbilityConnection, -1) == 0) {
96 PRINT_HILOGI("PrintServiceHelper::StartPluginPrintIconExtAbility ConnectAbility success");
97 break;
98 }
99 std::this_thread::sleep_for(std::chrono::milliseconds(START_ABILITY_INTERVAL));
100 PRINT_HILOGE("PrintServiceHelper::StartPluginPrintIconExtAbility %{public}d", retry);
101 }
102 if (retry > MAX_RETRY_TIMES) {
103 PRINT_HILOGE("PrintServiceHelper::StartPluginPrintIconExtAbility --> failed ");
104 return false;
105 }
106 return true;
107 }
108
GetBundleMgr()109 sptr<IRemoteObject> PrintServiceHelper::GetBundleMgr()
110 {
111 auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
112 if (systemAbilityManager == nullptr) {
113 PRINT_HILOGE("Failed to get system ability mgr.");
114 return nullptr;
115 }
116
117 return systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
118 }
119
QueryAccounts(std::vector<int> & accountList)120 bool PrintServiceHelper::QueryAccounts(std::vector<int> &accountList)
121 {
122 if (AccountSA::OsAccountManager::QueryActiveOsAccountIds(accountList) != ERR_OK) {
123 PRINT_HILOGE("failed to QueryActiveOsAccountIds!");
124 return false;
125 }
126 if (accountList.size() == 0) {
127 PRINT_HILOGE("no os account acquired!");
128 return false;
129 }
130 return true;
131 }
132
QueryExtension(sptr<AppExecFwk::IBundleMgr> mgr,int userId,std::vector<AppExecFwk::ExtensionAbilityInfo> & extensionInfos)133 bool PrintServiceHelper::QueryExtension(sptr<AppExecFwk::IBundleMgr> mgr, int userId,
134 std::vector<AppExecFwk::ExtensionAbilityInfo> &extensionInfos)
135 {
136 if (mgr != nullptr) {
137 mgr->QueryExtensionAbilityInfos(AppExecFwk::ExtensionAbilityType::PRINT, userId, extensionInfos);
138 return true;
139 }
140 PRINT_HILOGE("Invalid bundle manager");
141 return false;
142 }
143
QueryNameForUid(sptr<AppExecFwk::IBundleMgr> mgr,int32_t userId,std::string & name)144 bool PrintServiceHelper::QueryNameForUid(sptr<AppExecFwk::IBundleMgr> mgr, int32_t userId, std::string& name)
145 {
146 if (mgr != nullptr) {
147 mgr->GetNameForUid(userId, name);
148 return true;
149 }
150 PRINT_HILOGE("Invalid bundle manager");
151 return false;
152 }
153
IsSyncMode()154 bool PrintServiceHelper::IsSyncMode()
155 {
156 return false;
157 }
158
PrintSubscribeCommonEvent()159 void PrintServiceHelper::PrintSubscribeCommonEvent()
160 {
161 if (isSubscribeCommonEvent) {
162 return;
163 }
164 isSubscribeCommonEvent = true;
165 PRINT_HILOGI("listen common event.");
166 EventFwk::MatchingSkills matchingSkills;
167 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
168 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
169 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_ENTER_HIBERNATE);
170 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_EXIT_HIBERNATE);
171 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
172 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON);
173 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_ENTER_FORCE_SLEEP);
174 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_EXIT_FORCE_SLEEP);
175 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
176 subscribeInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
177
178 userStatusListener = std::make_shared<PrintEventSubscriber>(subscribeInfo);
179 if (userStatusListener == nullptr) {
180 PRINT_HILOGE("create userStatusListener failed.");
181 return;
182 }
183 if (!EventFwk::CommonEventManager::SubscribeCommonEvent(userStatusListener)) {
184 PRINT_HILOGE("subscribe common event failed");
185 }
186 }
187 } // namespace OHOS
188