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