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 32 namespace OHOS { 33 namespace USB { 34 35 class UsbRightManager { 36 public: 37 int32_t Init(); 38 /* deviceName is in VID-PID format */ 39 bool HasRight(const std::string &deviceName, const std::string &bundleName, 40 const std::string &tokenId, const int32_t &userId); 41 /* busDev is in busNum-devAddr format */ 42 int32_t RequestRight(const std::string &busDev, const std::string &deviceName, const std::string &bundleName, 43 const std::string &tokenId, const int32_t &userId); 44 bool AddDeviceRight(const std::string &deviceName, const std::string &tokenIdStr); 45 bool AddDeviceRight(const std::string &deviceName, const std::string &bundleName, 46 const std::string &tokenId, const int32_t &userId); 47 bool RemoveDeviceRight(const std::string &deviceName, const std::string &bundleName, 48 const std::string &tokenId, const int32_t &userId); 49 bool RemoveDeviceAllRight(const std::string &deviceName); 50 bool IsSystemAppOrSa(); 51 bool VerifyPermission(); 52 int32_t CleanUpRightExpired(std::vector<std::string> &devices); 53 static int32_t CleanUpRightUserDeleted(int32_t &totalUsers, int32_t &deleteUsers); 54 static int32_t CleanUpRightUserStopped(int32_t uid); 55 static int32_t IsOsAccountExists(int32_t id, bool &isAccountExists); 56 static int32_t CleanUpRightAppUninstalled(int32_t uid, const std::string &bundleName); 57 int32_t HasSetFuncRight(int32_t functions); 58 59 private: 60 bool GetUserAgreementByDiag(const std::string &busDev, const std::string &deviceName, const std::string &bundleName, 61 const std::string &tokenId, const int32_t &userId); 62 bool ShowUsbDialog(const std::string &busDev, const std::string &deviceName, 63 const std::string &bundleName, const std::string &tokenId); 64 bool IsAllDigits(const std::string &bundleName); 65 sptr<AppExecFwk::IBundleMgr> GetBundleMgr(); 66 sptr<AppExecFwk::IBundleResource> GetBundleResMgr(); 67 bool GetAppName(const std::string &bundleName, std::string &appName); 68 bool GetProductName(const std::string &devName, std::string &productName); 69 70 static sem_t waitDialogDisappear_; 71 class UsbAbilityConn : public AAFwk::AbilityConnectionStub { OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int32_t resultCode)72 void OnAbilityConnectDone(const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, 73 int32_t resultCode) override 74 { 75 USB_HILOGI(MODULE_USB_SERVICE, "connect done"); 76 } OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int32_t resultCode)77 void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int32_t resultCode) override 78 { 79 USB_HILOGI(MODULE_USB_SERVICE, "disconnect done"); 80 sem_post(&waitDialogDisappear_); 81 } 82 }; 83 84 std::mutex dialogRunning_; 85 86 bool IsAppInstalled(int32_t uid, const std::string &bundleName); 87 void GetCurrentUserId(int32_t &uid); 88 bool GetBundleInstallAndUpdateTime( 89 int32_t uid, const std::string &bundleName, uint64_t &installTime, uint64_t &updateTime); 90 uint64_t GetCurrentTimestamp(); 91 void StringVectorSortAndUniq(std::vector<std::string> &strings); 92 static bool StringVectorFound(const std::vector<std::string> &strings, const std::string &value, int32_t &index); 93 94 int32_t CleanUpRightAppUninstalled(int32_t uid, int32_t &totalApps, int32_t &deleteApps); 95 int32_t CleanUpRightTemporaryExpired(const std::string &deviceName); 96 int32_t CleanUpRightNormalExpired(int32_t uid); 97 int32_t CleanUpRightAppReinstalled(int32_t uid, uint32_t &totalApps, uint32_t &deleteApps); 98 int32_t TidyUpRight(uint32_t choose); 99 }; 100 101 } // namespace USB 102 } // namespace OHOS 103 104 #endif 105