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 = 10;
28 const uint32_t START_ABILITY_INTERVAL = 6;
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 break;
62 std::this_thread::sleep_for(std::chrono::seconds(START_ABILITY_INTERVAL));
63 PRINT_HILOGD("PrintServiceHelper::StartAbility %{public}d", retry);
64 }
65 if (retry > MAX_RETRY_TIMES) {
66 PRINT_HILOGE("PrintServiceHelper::StartAbility --> failed ");
67 return false;
68 }
69 return true;
70 }
71
StartPrintServiceExtension(const AAFwk::Want & want,int32_t requestCode_)72 bool PrintServiceHelper::StartPrintServiceExtension(const AAFwk::Want &want, int32_t requestCode_)
73 {
74 AppExecFwk::ElementName element = want.GetElement();
75 AAFwk::AbilityManagerClient::GetInstance()->Connect();
76 uint32_t retry = 0;
77 while (retry++ < MAX_RETRY_TIMES) {
78 PRINT_HILOGD("PrintServiceHelper::StartPrintServiceExtension %{public}s %{public}s",
79 element.GetBundleName().c_str(), element.GetAbilityName().c_str());
80 if (AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, requestCode_) == 0) {
81 break;
82 }
83 break;
84 std::this_thread::sleep_for(std::chrono::seconds(START_ABILITY_INTERVAL));
85 PRINT_HILOGD("PrintServiceHelper::StartPrintServiceExtension %{public}d", retry);
86 }
87 if (retry > MAX_RETRY_TIMES) {
88 PRINT_HILOGE("PrintServiceHelper::StartPrintServiceExtension --> failed ");
89 return false;
90 }
91 return true;
92 }
93
GetBundleMgr()94 sptr<IRemoteObject> PrintServiceHelper::GetBundleMgr()
95 {
96 auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
97 if (systemAbilityManager == nullptr) {
98 PRINT_HILOGE("Failed to get system ability mgr.");
99 return nullptr;
100 }
101
102 return systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
103 }
104
QueryAccounts(std::vector<int> & accountList)105 bool PrintServiceHelper::QueryAccounts(std::vector<int> &accountList)
106 {
107 if (AccountSA::OsAccountManager::QueryActiveOsAccountIds(accountList) != ERR_OK) {
108 PRINT_HILOGE("failed to QueryActiveOsAccountIds!");
109 return false;
110 }
111 if (accountList.size() == 0) {
112 PRINT_HILOGE("no os account acquired!");
113 return false;
114 }
115 return true;
116 }
117
QueryExtension(sptr<AppExecFwk::IBundleMgr> mgr,int userId,std::vector<AppExecFwk::ExtensionAbilityInfo> & extensionInfos)118 bool PrintServiceHelper::QueryExtension(sptr<AppExecFwk::IBundleMgr> mgr, int userId,
119 std::vector<AppExecFwk::ExtensionAbilityInfo> &extensionInfos)
120 {
121 if (mgr != nullptr) {
122 mgr->QueryExtensionAbilityInfos(AppExecFwk::ExtensionAbilityType::PRINT, userId, extensionInfos);
123 return true;
124 }
125 PRINT_HILOGE("Invalid bundle manager");
126 return false;
127 }
128
QueryNameForUid(sptr<AppExecFwk::IBundleMgr> mgr,int32_t userId,std::string & name)129 bool PrintServiceHelper::QueryNameForUid(sptr<AppExecFwk::IBundleMgr> mgr, int32_t userId, std::string& name)
130 {
131 if (mgr != nullptr) {
132 mgr->GetNameForUid(userId, name);
133 }
134 PRINT_HILOGE("Invalid bundle manager");
135 return false;
136 }
137
IsSyncMode()138 bool PrintServiceHelper::IsSyncMode()
139 {
140 return false;
141 }
142 } // namespace OHOS
143