• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "usb_errors.h"
18 
19 namespace OHOS {
20 namespace USB {
Init()21 void UsbRightManager::Init() {}
22 
HasRight(std::string deviceName,std::string bundleName)23 int32_t UsbRightManager::HasRight(std::string deviceName, std::string bundleName)
24 {
25     auto itMap = rightMap.find(deviceName);
26     if (itMap == rightMap.end()) {
27         USB_HILOGE(MODULE_USB_SERVICE, "hasRight deviceName false");
28         return UEC_SERVICE_INVALID_VALUE;
29     } else {
30         BundleNameList bundleNameList = itMap->second;
31         auto itVevtor = std::find(bundleNameList.begin(), bundleNameList.end(), bundleName);
32         if (itVevtor == bundleNameList.end()) {
33             USB_HILOGE(MODULE_USB_SERVICE, "hasRight bundleName false");
34             return UEC_SERVICE_INVALID_VALUE;
35         }
36     }
37     USB_HILOGI(MODULE_USB_SERVICE, "Request Right Success");
38     return UEC_OK;
39 }
40 
RequestRight(std::string deviceName,std::string bundleName)41 int32_t UsbRightManager::RequestRight(std::string deviceName, std::string bundleName)
42 {
43     if (HasRight(deviceName, bundleName) == 0) {
44         USB_HILOGE(MODULE_USB_SERVICE, "device has Right ");
45         return UEC_OK;
46     }
47     if (!AddDeviceRight(deviceName, bundleName)) {
48         return UEC_SERVICE_INVALID_VALUE;
49     }
50     if (HasRight(deviceName, bundleName) == 0) {
51         USB_HILOGI(MODULE_USB_SERVICE, "requestRight Success");
52         return UEC_OK;
53     }
54     USB_HILOGE(MODULE_USB_SERVICE, "requestRight False ");
55     return UEC_SERVICE_INVALID_VALUE;
56 }
57 
AddDeviceRight(std::string deviceName,std::string bundleName)58 bool UsbRightManager::AddDeviceRight(std::string deviceName, std::string bundleName)
59 {
60     auto itMap = rightMap.find(deviceName);
61     if (itMap != rightMap.end()) {
62         auto v = itMap->second;
63         auto itVevtor = std::find(v.begin(), v.end(), bundleName);
64         if (itVevtor != v.end()) {
65             USB_HILOGE(MODULE_USB_SERVICE, "addDeviceRight false");
66             return false;
67         }
68         itMap->second.push_back(bundleName);
69         USB_HILOGI(MODULE_USB_SERVICE, "addDeviceRight success");
70     }
71     BundleNameList bundleNameList;
72     bundleNameList.push_back(bundleName);
73     rightMap.insert(RightMap::value_type(deviceName, bundleNameList));
74     USB_HILOGI(MODULE_USB_SERVICE, "addDeviceRight success");
75     return true;
76 }
77 
RemoveDeviceRight(std::string deviceName)78 bool UsbRightManager::RemoveDeviceRight(std::string deviceName)
79 {
80     auto it = rightMap.find(deviceName);
81     if (it != rightMap.end()) {
82         rightMap.erase(it);
83         USB_HILOGI(MODULE_USB_SERVICE, "removeDeviceRight success");
84         return true;
85     }
86     return false;
87 }
88 } // namespace USB
89 } // namespace OHOS