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