142103316Sopenharmony_ci/*
242103316Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
342103316Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
442103316Sopenharmony_ci * you may not use this file except in compliance with the License.
542103316Sopenharmony_ci * You may obtain a copy of the License at
642103316Sopenharmony_ci *
742103316Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
842103316Sopenharmony_ci *
942103316Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1042103316Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1142103316Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1242103316Sopenharmony_ci * See the License for the specific language governing permissions and
1342103316Sopenharmony_ci * limitations under the License.
1442103316Sopenharmony_ci */
1542103316Sopenharmony_ci
1642103316Sopenharmony_ci#ifndef USB_RIGHT_MANAGER_H
1742103316Sopenharmony_ci#define USB_RIGHT_MANAGER_H
1842103316Sopenharmony_ci
1942103316Sopenharmony_ci#include <algorithm>
2042103316Sopenharmony_ci#include <map>
2142103316Sopenharmony_ci#include <mutex>
2242103316Sopenharmony_ci#include <semaphore.h>
2342103316Sopenharmony_ci#include <string>
2442103316Sopenharmony_ci#include <vector>
2542103316Sopenharmony_ci
2642103316Sopenharmony_ci#include "ability_connect_callback_stub.h"
2742103316Sopenharmony_ci#include "bundle_mgr_interface.h"
2842103316Sopenharmony_ci#include "bundle_resource_interface.h"
2942103316Sopenharmony_ci#include "usb_common.h"
3042103316Sopenharmony_ci#include "parameter.h"
3142103316Sopenharmony_ci
3242103316Sopenharmony_cinamespace OHOS {
3342103316Sopenharmony_cinamespace USB {
3442103316Sopenharmony_ci
3542103316Sopenharmony_ciclass UsbRightManager {
3642103316Sopenharmony_cipublic:
3742103316Sopenharmony_ci    int32_t Init();
3842103316Sopenharmony_ci    /* deviceName is in VID-PID format */
3942103316Sopenharmony_ci    bool HasRight(const std::string &deviceName, const std::string &bundleName,
4042103316Sopenharmony_ci        const std::string &tokenId, const int32_t &userId);
4142103316Sopenharmony_ci    /* busDev is in busNum-devAddr format */
4242103316Sopenharmony_ci    int32_t RequestRight(const std::string &busDev, const std::string &deviceName, const std::string &bundleName,
4342103316Sopenharmony_ci        const std::string &tokenId, const int32_t &userId);
4442103316Sopenharmony_ci    bool AddDeviceRight(const std::string &deviceName, const std::string &tokenIdStr);
4542103316Sopenharmony_ci    bool AddDeviceRight(const std::string &deviceName, const std::string &bundleName,
4642103316Sopenharmony_ci        const std::string &tokenId, const int32_t &userId);
4742103316Sopenharmony_ci    bool RemoveDeviceRight(const std::string &deviceName, const std::string &bundleName,
4842103316Sopenharmony_ci        const std::string &tokenId, const int32_t &userId);
4942103316Sopenharmony_ci    bool RemoveDeviceAllRight(const std::string &deviceName);
5042103316Sopenharmony_ci    bool IsSystemAppOrSa();
5142103316Sopenharmony_ci    bool VerifyPermission();
5242103316Sopenharmony_ci    int32_t CleanUpRightExpired(std::vector<std::string> &devices);
5342103316Sopenharmony_ci    static int32_t CleanUpRightUserDeleted(int32_t &totalUsers, int32_t &deleteUsers);
5442103316Sopenharmony_ci    static int32_t CleanUpRightUserStopped(int32_t uid);
5542103316Sopenharmony_ci    static int32_t IsOsAccountExists(int32_t id, bool &isAccountExists);
5642103316Sopenharmony_ci    static int32_t CleanUpRightAppUninstalled(int32_t uid, const std::string &bundleName);
5742103316Sopenharmony_ci    int32_t HasSetFuncRight(int32_t functions);
5842103316Sopenharmony_ci
5942103316Sopenharmony_ciprivate:
6042103316Sopenharmony_ci    bool GetUserAgreementByDiag(const std::string &busDev, const std::string &deviceName, const std::string &bundleName,
6142103316Sopenharmony_ci        const std::string &tokenId, const int32_t &userId);
6242103316Sopenharmony_ci    bool ShowUsbDialog(const std::string &busDev, const std::string &deviceName,
6342103316Sopenharmony_ci        const std::string &bundleName, const std::string &tokenId);
6442103316Sopenharmony_ci    bool IsAllDigits(const std::string &bundleName);
6542103316Sopenharmony_ci    sptr<AppExecFwk::IBundleMgr> GetBundleMgr();
6642103316Sopenharmony_ci    sptr<AppExecFwk::IBundleResource> GetBundleResMgr();
6742103316Sopenharmony_ci    bool GetAppName(const std::string &bundleName, std::string &appName);
6842103316Sopenharmony_ci    bool GetProductName(const std::string &devName, std::string &productName);
6942103316Sopenharmony_ci
7042103316Sopenharmony_ci    static sem_t waitDialogDisappear_;
7142103316Sopenharmony_ci    class UsbAbilityConn : public AAFwk::AbilityConnectionStub {
7242103316Sopenharmony_ci        void OnAbilityConnectDone(const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject,
7342103316Sopenharmony_ci            int32_t resultCode) override
7442103316Sopenharmony_ci        {
7542103316Sopenharmony_ci            USB_HILOGI(MODULE_USB_SERVICE, "connect done");
7642103316Sopenharmony_ci        }
7742103316Sopenharmony_ci        void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int32_t resultCode) override
7842103316Sopenharmony_ci        {
7942103316Sopenharmony_ci            USB_HILOGI(MODULE_USB_SERVICE, "disconnect done");
8042103316Sopenharmony_ci            sem_post(&waitDialogDisappear_);
8142103316Sopenharmony_ci        }
8242103316Sopenharmony_ci    };
8342103316Sopenharmony_ci
8442103316Sopenharmony_ci    std::mutex dialogRunning_;
8542103316Sopenharmony_ci
8642103316Sopenharmony_ci    bool IsAppInstalled(int32_t uid, const std::string &bundleName);
8742103316Sopenharmony_ci    void GetCurrentUserId(int32_t &uid);
8842103316Sopenharmony_ci    bool GetBundleInstallAndUpdateTime(
8942103316Sopenharmony_ci        int32_t uid, const std::string &bundleName, uint64_t &installTime, uint64_t &updateTime);
9042103316Sopenharmony_ci    uint64_t GetCurrentTimestamp();
9142103316Sopenharmony_ci    void StringVectorSortAndUniq(std::vector<std::string> &strings);
9242103316Sopenharmony_ci    static bool StringVectorFound(const std::vector<std::string> &strings, const std::string &value, int32_t &index);
9342103316Sopenharmony_ci
9442103316Sopenharmony_ci    int32_t CleanUpRightAppUninstalled(int32_t uid, int32_t &totalApps, int32_t &deleteApps);
9542103316Sopenharmony_ci    int32_t CleanUpRightTemporaryExpired(const std::string &deviceName);
9642103316Sopenharmony_ci    int32_t CleanUpRightNormalExpired(int32_t uid);
9742103316Sopenharmony_ci    int32_t CleanUpRightAppReinstalled(int32_t uid, uint32_t &totalApps, uint32_t &deleteApps);
9842103316Sopenharmony_ci    int32_t TidyUpRight(uint32_t choose);
9942103316Sopenharmony_ci};
10042103316Sopenharmony_ci
10142103316Sopenharmony_ci} // namespace USB
10242103316Sopenharmony_ci} // namespace OHOS
10342103316Sopenharmony_ci
10442103316Sopenharmony_ci#endif
105