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 #ifndef USB_RIGHT_MANAGER_H 17 #define USB_RIGHT_MANAGER_H 18 #include <algorithm> 19 #include <map> 20 #include <mutex> 21 #include <semaphore.h> 22 #include <string> 23 #include <vector> 24 25 #include "ability_connect_callback_stub.h" 26 #include "bundle_mgr_interface.h" 27 #include "usb_common.h" 28 29 namespace OHOS { 30 namespace USB { 31 class UsbRightManager { 32 public: 33 typedef std::vector<std::string> BundleNameList; 34 typedef std::map<std::string, BundleNameList> RightMap; 35 RightMap rightMap; 36 37 void Init(); 38 bool HasRight(const std::string &deviceName, const std::string &bundleName); 39 int32_t RequestRight(const std::string &deviceName, const std::string &bundleName); 40 bool AddDeviceRight(const std::string &deviceName, const std::string &bundleName); 41 bool RemoveDeviceRight(const std::string &deviceName, const std::string &bundleName); 42 bool RemoveDeviceAllRight(const std::string &deviceName); 43 bool IsSystemHap(); 44 45 private: 46 bool GetUserAgreementByDiag(const std::string &deviceName, const std::string &bundleName); 47 bool ShowUsbDialog(const std::string &deviceName, const std::string &bundleName); 48 sptr<AppExecFwk::IBundleMgr> GetBundleMgr(); 49 50 static sem_t waitDialogDisappear_; 51 class UsbAbilityConn : public AAFwk::AbilityConnectionStub { OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)52 void OnAbilityConnectDone( 53 const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode) override 54 { 55 USB_HILOGI(MODULE_USB_SERVICE, "connect done"); 56 } OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)57 void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override 58 { 59 USB_HILOGI(MODULE_USB_SERVICE, "disconnect done"); 60 sem_post(&waitDialogDisappear_); 61 } 62 }; 63 64 std::mutex dialogRunning_; 65 }; 66 } // namespace USB 67 } // namespace OHOS 68 69 #endif 70