• 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 "usb_right_manager.h"
17 #include <semaphore.h>
18 #include <unistd.h>
19 
20 #include "ability_manager_client.h"
21 #include "ipc_skeleton.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24 #include "usb_errors.h"
25 
26 using namespace OHOS::AppExecFwk;
27 
28 namespace OHOS {
29 namespace USB {
30 
31 sem_t UsbRightManager::waitDialogDisappear_ {0};
32 
Init()33 void UsbRightManager::Init() {}
34 
HasRight(const std::string & deviceName,const std::string & bundleName)35 bool UsbRightManager::HasRight(const std::string &deviceName, const std::string &bundleName)
36 {
37     auto itMap = rightMap.find(deviceName);
38     if (itMap == rightMap.end()) {
39         USB_HILOGE(MODULE_USB_SERVICE, "hasRight deviceName false");
40         return false;
41     } else {
42         BundleNameList bundleNameList = itMap->second;
43         auto itVevtor = std::find(bundleNameList.begin(), bundleNameList.end(), bundleName);
44         if (itVevtor == bundleNameList.end()) {
45             USB_HILOGE(MODULE_USB_SERVICE, "hasRight bundleName false");
46             return false;
47         }
48     }
49     USB_HILOGI(MODULE_USB_SERVICE, "Request Right Success");
50     return true;
51 }
52 
RequestRight(const std::string & deviceName,const std::string & bundleName)53 int32_t UsbRightManager::RequestRight(const std::string &deviceName, const std::string &bundleName)
54 {
55     if (HasRight(deviceName, bundleName)) {
56         USB_HILOGE(MODULE_USB_SERVICE, "device has Right ");
57         return UEC_OK;
58     }
59 
60     if (!GetUserAgreementByDiag(deviceName, bundleName)) {
61         USB_HILOGW(MODULE_USB_SERVICE, "user don't agree");
62         return UEC_SERVICE_PERMISSION_DENIED;
63     }
64     return UEC_OK;
65 }
66 
AddDeviceRight(const std::string & deviceName,const std::string & bundleName)67 bool UsbRightManager::AddDeviceRight(const std::string &deviceName, const std::string &bundleName)
68 {
69     auto itMap = rightMap.find(deviceName);
70     if (itMap != rightMap.end()) {
71         auto v = itMap->second;
72         auto itVevtor = std::find(v.begin(), v.end(), bundleName);
73         if (itVevtor != v.end()) {
74             USB_HILOGE(MODULE_USB_SERVICE, "addDeviceRight false");
75             return false;
76         }
77         itMap->second.push_back(bundleName);
78         USB_HILOGI(MODULE_USB_SERVICE, "addDeviceRight success");
79     }
80     BundleNameList bundleNameList;
81     bundleNameList.push_back(bundleName);
82     rightMap.insert(RightMap::value_type(deviceName, bundleNameList));
83     USB_HILOGI(MODULE_USB_SERVICE, "addDeviceRight success");
84     return true;
85 }
86 
RemoveDeviceRight(const std::string & deviceName,const std::string & bundleName)87 bool UsbRightManager::RemoveDeviceRight(const std::string &deviceName, const std::string &bundleName)
88 {
89     auto it = rightMap.find(deviceName);
90     if (it != rightMap.end()) {
91         auto &v = it->second;
92         auto itVevtor = std::find(v.begin(), v.end(), bundleName);
93         if (itVevtor != v.end()) {
94             it->second.erase(itVevtor);
95             return true;
96         }
97     }
98     USB_HILOGI(MODULE_USB_SERVICE, "RemoveDeviceRight failed");
99     return false;
100 }
101 
RemoveDeviceAllRight(const std::string & deviceName)102 bool UsbRightManager::RemoveDeviceAllRight(const std::string &deviceName)
103 {
104     auto it = rightMap.find(deviceName);
105     if (it != rightMap.end()) {
106         rightMap.erase(it);
107         USB_HILOGI(MODULE_USB_SERVICE, "removeDeviceAllRight success");
108         return true;
109     }
110     return false;
111 }
112 
ShowUsbDialog(const std::string & deviceName,const std::string & bundleName)113 bool UsbRightManager::ShowUsbDialog(const std::string &deviceName, const std::string &bundleName)
114 {
115     auto abmc = AAFwk::AbilityManagerClient::GetInstance();
116     if (abmc == nullptr) {
117         USB_HILOGE(MODULE_USB_SERVICE, "GetInstance failed");
118         return false;
119     }
120 
121     AAFwk::Want want;
122     want.SetElementName("com.usb.right", "UsbServiceExtAbility");
123     want.SetParam("bundleName", bundleName);
124     want.SetParam("deviceName", deviceName);
125 
126     sptr<UsbAbilityConn> usbAbilityConn_ = new (std::nothrow) UsbAbilityConn();
127     sem_init(&waitDialogDisappear_, 1, 0);
128     auto ret = abmc->ConnectAbility(want, usbAbilityConn_, -1);
129     if (ret != UEC_OK) {
130         USB_HILOGE(MODULE_SERVICE, "connectAbility failed %{public}d", ret);
131         return false;
132     }
133 
134     // Waiting for the user to click
135     sem_wait(&waitDialogDisappear_);
136     return true;
137 }
138 
GetUserAgreementByDiag(const std::string & deviceName,const std::string & bundleName)139 bool UsbRightManager::GetUserAgreementByDiag(const std::string &deviceName, const std::string &bundleName)
140 {
141 #ifdef USB_RIGHT_TEST
142     return true;
143 #endif
144     // There can only be one dialog at a time
145     std::lock_guard<std::mutex> guard(dialogRunning_);
146     if (!ShowUsbDialog(deviceName, bundleName)) {
147         USB_HILOGE(MODULE_USB_SERVICE, "ShowUsbDialog failed");
148         return false;
149     }
150 
151     return HasRight(deviceName, bundleName);
152 }
153 
GetBundleMgr()154 sptr<IBundleMgr> UsbRightManager::GetBundleMgr()
155 {
156     auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
157     if (sam == nullptr) {
158         USB_HILOGW(MODULE_USB_SERVICE, "GetSystemAbilityManager return nullptr");
159         return nullptr;
160     }
161     auto bundleMgrSa = sam->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
162     if (bundleMgrSa == nullptr) {
163         USB_HILOGW(MODULE_USB_SERVICE, "GetSystemAbility return nullptr");
164         return nullptr;
165     }
166     auto bundleMgr = iface_cast<IBundleMgr>(bundleMgrSa);
167     if (bundleMgr == nullptr) {
168         USB_HILOGW(MODULE_USB_SERVICE, "iface_cast return nullptr");
169     }
170     return bundleMgr;
171 }
172 
IsSystemHap()173 bool UsbRightManager::IsSystemHap()
174 {
175     pid_t uid = IPCSkeleton::GetCallingUid();
176     auto bundleMgr = GetBundleMgr();
177     if (bundleMgr == nullptr) {
178         USB_HILOGW(MODULE_USB_SERVICE, "BundleMgr is nullptr, return false");
179         return false;
180     }
181     return bundleMgr->CheckIsSystemAppByUid(uid);
182 }
183 } // namespace USB
184 } // namespace OHOS
185