1 /* 2 * Copyright (c) 2021-2023 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 #ifndef USB_RIGHT_MANAGER_H 17 #define USB_RIGHT_MANAGER_H 18 19 #include <algorithm> 20 #include <map> 21 #include <mutex> 22 #include <semaphore.h> 23 #include <string> 24 #include <vector> 25 26 #include "ability_connect_callback_stub.h" 27 #include "bundle_mgr_interface.h" 28 #include "bundle_resource_interface.h" 29 #include "usb_common.h" 30 #include "parameter.h" 31 #include "usb_accessory.h" 32 #include "serial_device_identity.h" 33 namespace OHOS { 34 namespace USB { 35 36 class UsbRightManager { 37 public: 38 int32_t Init(); 39 /* deviceName is in VID-PID format */ 40 bool HasRight(const std::string &deviceName, const std::string &bundleName, 41 const std::string &tokenId, const int32_t &userId); 42 /* busDev is in busNum-devAddr format */ 43 #ifdef USB_MANAGER_FEATURE_HOST 44 int32_t RequestRight(const std::string &busDev, const std::string &deviceName, const std::string &bundleName, 45 const std::string &tokenId, const int32_t &userId); 46 #endif // USB_MANAGER_FEATURE_HOST 47 int32_t RequestRight(const USBAccessory &access, const std::string &seriaValue, const std::string &bundleName, 48 const std::string &tokenId, const int32_t &userId, bool &result); 49 int32_t RequestRight(const int32_t portId, const SerialDeviceIdentity &serialDeviceIdentity, 50 const std::string &bundleName, const std::string &tokenId, const int32_t &userId); 51 bool AddDeviceRight(const std::string &deviceName, const std::string &tokenIdStr); 52 bool AddDeviceRight(const std::string &deviceName, const std::string &bundleName, 53 const std::string &tokenId, const int32_t &userId); 54 bool RemoveDeviceRight(const std::string &deviceName, const std::string &bundleName, 55 const std::string &tokenId, const int32_t &userId); 56 int32_t CancelDeviceRight(const std::string &deviceName, const std::string &bundleName, 57 const std::string &tokenId, const int32_t &userId); 58 bool RemoveDeviceAllRight(const std::string &deviceName); 59 bool IsSystemAppOrSa(); 60 bool VerifyPermission(); 61 int32_t CleanUpRightExpired(std::vector<std::string> &devices); 62 static int32_t CleanUpRightUserDeleted(int32_t &totalUsers, int32_t &deleteUsers); 63 static int32_t CleanUpRightUserStopped(int32_t uid); 64 static int32_t IsOsAccountExists(int32_t id, bool &isAccountExists); 65 static int32_t CleanUpRightAppUninstalled(int32_t uid, const std::string &bundleName); 66 int32_t HasSetFuncRight(int32_t functions); 67 68 private: 69 #ifdef USB_MANAGER_FEATURE_HOST 70 bool GetUserAgreementByDiag(const std::string &busDev, const std::string &deviceName, const std::string &bundleName, 71 const std::string &tokenId, const int32_t &userId); 72 bool ShowUsbDialog(const std::string &busDev, const std::string &deviceName, 73 const std::string &bundleName, const std::string &tokenId); 74 bool GetProductName(const std::string &devName, std::string &productName); 75 #endif // USB_MANAGER_FEATURE_HOST 76 bool GetUserAgreementByDiag(const USBAccessory &access, const std::string &seriaValue, 77 const std::string &bundleName, const std::string &tokenId, const int32_t &userId); 78 bool GetUserAgreementByDiag(const int32_t portId, const SerialDeviceIdentity &serialDeviceIdentity, 79 const std::string &bundleName, const std::string &tokenId, const int32_t &userId); 80 bool ShowUsbDialog(const USBAccessory &access, const std::string &seriaValue, 81 const std::string &bundleName, const std::string &tokenId); 82 bool ShowSerialDialog(const int32_t portId, const uint32_t tokenId, const std::string &bundleName, 83 const std::string &busDev); 84 bool IsAllDigits(const std::string &bundleName); 85 sptr<AppExecFwk::IBundleMgr> GetBundleMgr(); 86 sptr<AppExecFwk::IBundleResource> GetBundleResMgr(); 87 bool GetAppName(const std::string &bundleName, std::string &appName); 88 bool GetAccessoryName(const USBAccessory &access, std::string &accessName); 89 static sem_t waitDialogDisappear_; 90 class UsbAbilityConn : public AAFwk::AbilityConnectionStub { 91 void OnAbilityConnectDone(const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, 92 int32_t resultCode) override; 93 void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int32_t resultCode) override; 94 sptr<IRemoteObject> remoteObject_ = nullptr; 95 public: UsbAbilityConn()96 UsbAbilityConn() 97 { 98 USB_HILOGI(MODULE_USB_SERVICE, "%{public}s", __func__); 99 } ~UsbAbilityConn()100 ~UsbAbilityConn() 101 { 102 USB_HILOGI(MODULE_USB_SERVICE, "%{public}s", __func__); 103 } 104 void CloseDialog(); 105 }; 106 107 std::mutex dialogRunning_; 108 sptr<UsbAbilityConn> usbAbilityConn_ = nullptr; 109 static std::map<std::string, std::string> usbDialogParams_; 110 static std::mutex usbDialogParamsMutex_; 111 bool IsAppInstalled(int32_t uid, const std::string &bundleName); 112 void GetCurrentUserId(int32_t &uid); 113 bool GetBundleInstallAndUpdateTime( 114 int32_t uid, const std::string &bundleName, uint64_t &installTime, uint64_t &updateTime); 115 uint64_t GetCurrentTimestamp(); 116 void StringVectorSortAndUniq(std::vector<std::string> &strings); 117 static bool StringVectorFound(const std::vector<std::string> &strings, const std::string &value, int32_t &index); 118 119 int32_t CleanUpRightAppUninstalled(int32_t uid, int32_t &totalApps, int32_t &deleteApps); 120 int32_t CleanUpRightTemporaryExpired(const std::string &deviceName); 121 int32_t CleanUpRightNormalExpired(int32_t uid); 122 int32_t CleanUpRightAppReinstalled(int32_t uid, uint32_t &totalApps, uint32_t &deleteApps); 123 int32_t TidyUpRight(uint32_t choose); 124 bool UnShowUsbDialog(); 125 int32_t ConnectAbility(); 126 }; 127 128 } // namespace USB 129 } // namespace OHOS 130 131 #endif 132