• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
26 namespace OHOS::Print {
27 const uint32_t MAX_RETRY_TIMES = 3;
28 const uint32_t START_ABILITY_INTERVAL = 2;
29 using namespace Security::AccessToken;
30 
~PrintServiceHelper()31 PrintServiceHelper::~PrintServiceHelper()
32 {
33 }
34 
CheckPermission(const std::string & name)35 bool PrintServiceHelper::CheckPermission(const std::string &name)
36 {
37     AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID();
38     TypeATokenTypeEnum tokenType = AccessTokenKit::GetTokenTypeFlag(tokenId);
39     if (tokenType == TOKEN_INVALID) {
40         PRINT_HILOGE("invalid token id %{public}d", tokenId);
41         return false;
42     }
43     int result = AccessTokenKit::VerifyAccessToken(tokenId, name);
44     if (result != PERMISSION_GRANTED) {
45         PRINT_HILOGE("Current tokenId permission is %{public}d", result);
46     }
47     return result == PERMISSION_GRANTED;
48 }
49 
StartAbility(const AAFwk::Want & want)50 bool PrintServiceHelper::StartAbility(const AAFwk::Want &want)
51 {
52     AppExecFwk::ElementName element = want.GetElement();
53     AAFwk::AbilityManagerClient::GetInstance()->Connect();
54     uint32_t retry = 0;
55     while (retry++ < MAX_RETRY_TIMES) {
56         PRINT_HILOGD("PrintServiceHelper::StartAbility %{public}s %{public}s",
57             element.GetBundleName().c_str(), element.GetAbilityName().c_str());
58         if (AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want) == 0) {
59             break;
60         }
61         std::this_thread::sleep_for(std::chrono::seconds(START_ABILITY_INTERVAL));
62         PRINT_HILOGD("PrintServiceHelper::StartAbility %{public}d", retry);
63     }
64     if (retry > MAX_RETRY_TIMES) {
65         PRINT_HILOGE("PrintServiceHelper::StartAbility --> failed ");
66         return false;
67     }
68     return true;
69 }
70 
KillAbility(const std::string & bundleName)71 bool PrintServiceHelper::KillAbility(const std::string &bundleName)
72 {
73     for (uint32_t retry = 0; retry < MAX_RETRY_TIMES; ++retry) {
74         if (AAFwk::AbilityManagerClient::GetInstance()->KillProcess(bundleName) == ERR_OK) {
75             return true;
76         }
77         PRINT_HILOGD("PrintServiceHelper::KillAbility %{public}d %{public}s",
78             retry, bundleName.c_str());
79     }
80     PRINT_HILOGE("PrintServiceHelper::KillAbility %{public}s failed", bundleName.c_str());
81     return false;
82 }
83 
StartPrintServiceExtension(const AAFwk::Want & want,int32_t requestCode_)84 bool PrintServiceHelper::StartPrintServiceExtension(const AAFwk::Want &want, int32_t requestCode_)
85 {
86     AppExecFwk::ElementName element = want.GetElement();
87     AAFwk::AbilityManagerClient::GetInstance()->Connect();
88     uint32_t retry = 0;
89     while (retry++ < MAX_RETRY_TIMES) {
90         PRINT_HILOGD("PrintServiceHelper::StartPrintServiceExtension %{public}s %{public}s",
91             element.GetBundleName().c_str(), element.GetAbilityName().c_str());
92         if (AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, requestCode_) == 0) {
93             break;
94         }
95         std::this_thread::sleep_for(std::chrono::seconds(START_ABILITY_INTERVAL));
96         PRINT_HILOGD("PrintServiceHelper::StartPrintServiceExtension %{public}d", retry);
97     }
98     if (retry > MAX_RETRY_TIMES) {
99         PRINT_HILOGE("PrintServiceHelper::StartPrintServiceExtension --> failed ");
100         return false;
101     }
102     return true;
103 }
104 
GetBundleMgr()105 sptr<IRemoteObject> PrintServiceHelper::GetBundleMgr()
106 {
107     auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
108     if (systemAbilityManager == nullptr) {
109         PRINT_HILOGE("Failed to get system ability mgr.");
110         return nullptr;
111     }
112 
113     return systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
114 }
115 
QueryAccounts(std::vector<int> & accountList)116 bool PrintServiceHelper::QueryAccounts(std::vector<int> &accountList)
117 {
118     if (AccountSA::OsAccountManager::QueryActiveOsAccountIds(accountList) != ERR_OK) {
119         PRINT_HILOGE("failed to QueryActiveOsAccountIds!");
120         return false;
121     }
122     if (accountList.size() == 0) {
123         PRINT_HILOGE("no os account acquired!");
124         return false;
125     }
126     return true;
127 }
128 
QueryExtension(sptr<AppExecFwk::IBundleMgr> mgr,int userId,std::vector<AppExecFwk::ExtensionAbilityInfo> & extensionInfos)129 bool PrintServiceHelper::QueryExtension(sptr<AppExecFwk::IBundleMgr> mgr, int userId,
130     std::vector<AppExecFwk::ExtensionAbilityInfo> &extensionInfos)
131 {
132     if (mgr != nullptr) {
133         mgr->QueryExtensionAbilityInfos(AppExecFwk::ExtensionAbilityType::PRINT, userId, extensionInfos);
134         return true;
135     }
136     PRINT_HILOGE("Invalid bundle manager");
137     return false;
138 }
139 
QueryNameForUid(sptr<AppExecFwk::IBundleMgr> mgr,int32_t userId,std::string & name)140 bool PrintServiceHelper::QueryNameForUid(sptr<AppExecFwk::IBundleMgr> mgr, int32_t userId, std::string& name)
141 {
142     if (mgr != nullptr) {
143         mgr->GetNameForUid(userId, name);
144         return true;
145     }
146     PRINT_HILOGE("Invalid bundle manager");
147     return false;
148 }
149 
IsSyncMode()150 bool PrintServiceHelper::IsSyncMode()
151 {
152     return false;
153 }
154 }  // namespace OHOS
155