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 = 3;
31 const uint32_t START_ABILITY_INTERVAL = 2;
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         if (AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want) == 0) {
65             break;
66         }
67         std::this_thread::sleep_for(std::chrono::seconds(START_ABILITY_INTERVAL));
68         PRINT_HILOGD("PrintServiceHelper::StartAbility %{public}d", retry);
69     }
70     if (retry > MAX_RETRY_TIMES) {
71         PRINT_HILOGE("PrintServiceHelper::StartAbility --> failed ");
72         return false;
73     }
74     return true;
75 }
76 
StartPluginPrintIconExtAbility(const AAFwk::Want & want)77 bool PrintServiceHelper::StartPluginPrintIconExtAbility(const AAFwk::Want &want)
78 {
79     PRINT_HILOGD("enter PrintServiceHelper::StartPluginPrintIconExtAbility");
80     PRINT_HILOGD("want: %{public}s", want.ToUri().c_str());
81     AppExecFwk::ElementName element = want.GetElement();
82     AAFwk::AbilityManagerClient::GetInstance()->Connect();
83     uint32_t retry = 0;
84     sptr<PrintAbilityConnection> printAbilityConnection = new (std::nothrow) PrintAbilityConnection();
85     if (printAbilityConnection == nullptr) {
86         PRINT_HILOGE("fail to create printAbilityConnection");
87         return false;
88     }
89     PRINT_HILOGD("PrintServiceHelper::StartPluginPrintIconExtAbility %{public}s %{public}s",
90         element.GetBundleName().c_str(),
91         element.GetAbilityName().c_str());
92     while (retry++ < MAX_RETRY_TIMES) {
93         if (AAFwk::AbilityManagerClient::GetInstance()->ConnectAbility(want, printAbilityConnection, -1) == 0) {
94             PRINT_HILOGI("PrintServiceHelper::StartPluginPrintIconExtAbility ConnectAbility success");
95             break;
96         }
97         std::this_thread::sleep_for(std::chrono::seconds(START_ABILITY_INTERVAL));
98         PRINT_HILOGE("PrintServiceHelper::StartPluginPrintIconExtAbility %{public}d", retry);
99     }
100     if (retry > MAX_RETRY_TIMES) {
101         PRINT_HILOGE("PrintServiceHelper::StartPluginPrintIconExtAbility --> failed ");
102         return false;
103     }
104     return true;
105 }
106 
GetBundleMgr()107 sptr<IRemoteObject> PrintServiceHelper::GetBundleMgr()
108 {
109     auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
110     if (systemAbilityManager == nullptr) {
111         PRINT_HILOGE("Failed to get system ability mgr.");
112         return nullptr;
113     }
114 
115     return systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
116 }
117 
QueryAccounts(std::vector<int> & accountList)118 bool PrintServiceHelper::QueryAccounts(std::vector<int> &accountList)
119 {
120     if (AccountSA::OsAccountManager::QueryActiveOsAccountIds(accountList) != ERR_OK) {
121         PRINT_HILOGE("failed to QueryActiveOsAccountIds!");
122         return false;
123     }
124     if (accountList.size() == 0) {
125         PRINT_HILOGE("no os account acquired!");
126         return false;
127     }
128     return true;
129 }
130 
QueryExtension(sptr<AppExecFwk::IBundleMgr> mgr,int userId,std::vector<AppExecFwk::ExtensionAbilityInfo> & extensionInfos)131 bool PrintServiceHelper::QueryExtension(sptr<AppExecFwk::IBundleMgr> mgr, int userId,
132     std::vector<AppExecFwk::ExtensionAbilityInfo> &extensionInfos)
133 {
134     if (mgr != nullptr) {
135         mgr->QueryExtensionAbilityInfos(AppExecFwk::ExtensionAbilityType::PRINT, userId, extensionInfos);
136         return true;
137     }
138     PRINT_HILOGE("Invalid bundle manager");
139     return false;
140 }
141 
QueryNameForUid(sptr<AppExecFwk::IBundleMgr> mgr,int32_t userId,std::string & name)142 bool PrintServiceHelper::QueryNameForUid(sptr<AppExecFwk::IBundleMgr> mgr, int32_t userId, std::string& name)
143 {
144     if (mgr != nullptr) {
145         mgr->GetNameForUid(userId, name);
146         return true;
147     }
148     PRINT_HILOGE("Invalid bundle manager");
149     return false;
150 }
151 
IsSyncMode()152 bool PrintServiceHelper::IsSyncMode()
153 {
154     return false;
155 }
156 
PrintSubscribeCommonEvent()157 void PrintServiceHelper::PrintSubscribeCommonEvent()
158 {
159     if (isSubscribeCommonEvent) {
160         return;
161     }
162     isSubscribeCommonEvent = true;
163     PRINT_HILOGI("listen user status.");
164     EventFwk::MatchingSkills matchingSkills;
165     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
166     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
167     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
168     subscribeInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON);
169 
170     userStatusListener = std::make_shared<PrintEventSubscriber>(subscribeInfo);
171     if (userStatusListener == nullptr) {
172         PRINT_HILOGE("create userStatusListener failed.");
173         return;
174     }
175     if (!EventFwk::CommonEventManager::SubscribeCommonEvent(userStatusListener)) {
176         PRINT_HILOGE("subscribe common event failed");
177     }
178 }
179 }  // namespace OHOS
180