176fd607bSopenharmony_ci/*
276fd607bSopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
376fd607bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
476fd607bSopenharmony_ci * you may not use this file except in compliance with the License.
576fd607bSopenharmony_ci * You may obtain a copy of the License at
676fd607bSopenharmony_ci *
776fd607bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
876fd607bSopenharmony_ci *
976fd607bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1076fd607bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1176fd607bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1276fd607bSopenharmony_ci * See the License for the specific language governing permissions and
1376fd607bSopenharmony_ci * limitations under the License.
1476fd607bSopenharmony_ci */
1576fd607bSopenharmony_ci#ifndef MEDIALIBRARY_PERMISSION_UTILS_H
1676fd607bSopenharmony_ci#define MEDIALIBRARY_PERMISSION_UTILS_H
1776fd607bSopenharmony_ci
1876fd607bSopenharmony_ci#include <array>
1976fd607bSopenharmony_ci#include <list>
2076fd607bSopenharmony_ci#include <string>
2176fd607bSopenharmony_ci#include <unordered_map>
2276fd607bSopenharmony_ci#include <vector>
2376fd607bSopenharmony_ci#include <thread>
2476fd607bSopenharmony_ci#include <mutex>
2576fd607bSopenharmony_ci
2676fd607bSopenharmony_ci#include "bundle_mgr_interface.h"
2776fd607bSopenharmony_ci#include "userfile_manager_types.h"
2876fd607bSopenharmony_ci#include "permission_used_type.h"
2976fd607bSopenharmony_ci#include "privacy_kit.h"
3076fd607bSopenharmony_ci#include "tokenid_kit.h"
3176fd607bSopenharmony_ci
3276fd607bSopenharmony_cinamespace OHOS {
3376fd607bSopenharmony_cinamespace Media {
3476fd607bSopenharmony_ci#define COMPILE_HIDDEN __attribute__ ((visibility ("hidden")))
3576fd607bSopenharmony_ciconst std::string PERMISSION_NAME_READ_MEDIA = "ohos.permission.READ_MEDIA";
3676fd607bSopenharmony_ciconst std::string PERMISSION_NAME_WRITE_MEDIA = "ohos.permission.WRITE_MEDIA";
3776fd607bSopenharmony_ciconst std::string PERMISSION_NAME_MEDIA_LOCATION = "ohos.permission.MEDIA_LOCATION";
3876fd607bSopenharmony_ciconst std::string PERM_READ_IMAGEVIDEO = "ohos.permission.READ_IMAGEVIDEO";
3976fd607bSopenharmony_ciconst std::string PERM_READ_AUDIO = "ohos.permission.READ_AUDIO";
4076fd607bSopenharmony_ciconst std::string PERM_READ_DOCUMENT = "ohos.permission.READ_DOCUMENT";
4176fd607bSopenharmony_ciconst std::string PERM_WRITE_IMAGEVIDEO = "ohos.permission.WRITE_IMAGEVIDEO";
4276fd607bSopenharmony_ciconst std::string PERM_WRITE_AUDIO = "ohos.permission.WRITE_AUDIO";
4376fd607bSopenharmony_ciconst std::string PERM_WRITE_DOCUMENT = "ohos.permission.WRITE_DOCUMENT";
4476fd607bSopenharmony_ciconst std::string PERM_MANAGE_PRIVATE_PHOTOS = "ohos.permission.MANAGE_PRIVATE_PHOTOS";
4576fd607bSopenharmony_ciconst std::string PERM_SHORT_TERM_WRITE_IMAGEVIDEO = "ohos.permission.SHORT_TERM_WRITE_IMAGEVIDEO";
4676fd607bSopenharmony_ciconst std::string E_POLICY = "E";
4776fd607bSopenharmony_ciconstexpr int SHORT_TERM_PERMISSION_DURATION_300S = 300;
4876fd607bSopenharmony_ci
4976fd607bSopenharmony_cienum SaveType {
5076fd607bSopenharmony_ci    SHORT_IMAGE_PERM = 0,
5176fd607bSopenharmony_ci};
5276fd607bSopenharmony_ci
5376fd607bSopenharmony_ciconst std::vector<std::string> WRITE_PERMS_V10 = {
5476fd607bSopenharmony_ci    PERM_WRITE_IMAGEVIDEO,
5576fd607bSopenharmony_ci    PERM_WRITE_AUDIO,
5676fd607bSopenharmony_ci    PERM_WRITE_DOCUMENT
5776fd607bSopenharmony_ci};
5876fd607bSopenharmony_ci
5976fd607bSopenharmony_cistruct BundleInfo {
6076fd607bSopenharmony_ci    std::string bundleName;
6176fd607bSopenharmony_ci    std::string packageName;
6276fd607bSopenharmony_ci    std::string appId;
6376fd607bSopenharmony_ci};
6476fd607bSopenharmony_ci
6576fd607bSopenharmony_ciclass PermissionUtils {
6676fd607bSopenharmony_cipublic:
6776fd607bSopenharmony_ci    static bool CheckCallerPermission(const std::string &permission);
6876fd607bSopenharmony_ci    static bool CheckCallerPermission(const std::vector<std::string> &perms);
6976fd607bSopenharmony_ci    static bool CheckHasPermission(const std::vector<std::string> &perms);
7076fd607bSopenharmony_ci    static void GetClientBundle(const int uid, std::string &bundleName);
7176fd607bSopenharmony_ci    static void GetPackageName(const int uid, std::string &packageName);
7276fd607bSopenharmony_ci    static uint32_t GetTokenId();
7376fd607bSopenharmony_ci    static bool IsSystemApp();
7476fd607bSopenharmony_ci    static bool IsNativeSAApp();
7576fd607bSopenharmony_ci    static bool IsRootShell();
7676fd607bSopenharmony_ci    static bool IsHdcShell();
7776fd607bSopenharmony_ci    static bool CheckIsSystemAppByUid();
7876fd607bSopenharmony_ci    static std::string GetPackageNameByBundleName(const std::string &bundleName);
7976fd607bSopenharmony_ci    static std::string GetAppIdByBundleName(const std::string &bundleName);
8076fd607bSopenharmony_ci    static std::string GetAppIdByBundleName(const std::string &bundleName, int32_t uid);
8176fd607bSopenharmony_ci    static bool CheckPhotoCallerPermission(const std::vector<std::string> &perms);
8276fd607bSopenharmony_ci    static bool CheckPhotoCallerPermission(const std::string &permission);
8376fd607bSopenharmony_ci    static void CollectPermissionInfo(const std::string &permission, const bool permGranted,
8476fd607bSopenharmony_ci        const Security::AccessToken::PermissionUsedType type);
8576fd607bSopenharmony_ci    static void ClearBundleInfoInCache();
8676fd607bSopenharmony_ci    static bool SetEPolicy();
8776fd607bSopenharmony_ci
8876fd607bSopenharmony_ciprivate:
8976fd607bSopenharmony_ci    static sptr<AppExecFwk::IBundleMgr> GetSysBundleManager();
9076fd607bSopenharmony_ci    COMPILE_HIDDEN static sptr<AppExecFwk::IBundleMgr> bundleMgr_;
9176fd607bSopenharmony_ci    COMPILE_HIDDEN static std::mutex bundleMgrMutex_;
9276fd607bSopenharmony_ci    static void GetBundleNameFromCache(int uid, std::string &bundleName);
9376fd607bSopenharmony_ci    static void GetPackageNameFromCache(int uid, const std::string &bundleName, std::string &packageName);
9476fd607bSopenharmony_ci    static void GetAppIdFromCache(int uid, const std::string &bundleName, std::string &appId);
9576fd607bSopenharmony_ci    static void UpdateLatestBundleInfo(int uid, const BundleInfo &bundleInfo);
9676fd607bSopenharmony_ci    static void UpdateBundleNameInCache(int uid, const std::string &bundleName);
9776fd607bSopenharmony_ci    static void UpdatePackageNameInCache(int uid, const std::string &packageName);
9876fd607bSopenharmony_ci    static void UpdateAppIdInCache(int uid, const std::string &appId);
9976fd607bSopenharmony_ci
10076fd607bSopenharmony_ci    static std::mutex uninstallMutex_;
10176fd607bSopenharmony_ci    static std::list<std::pair<int32_t, BundleInfo>> bundleInfoList_; // 用来快速获取使用频率最低的uid
10276fd607bSopenharmony_ci    static std::unordered_map<int32_t, std::list<std::pair<int32_t, BundleInfo>>::iterator> bundleInfoMap_;
10376fd607bSopenharmony_ci};
10476fd607bSopenharmony_ci}  // namespace Media
10576fd607bSopenharmony_ci}  // namespace OHOS
10676fd607bSopenharmony_ci#endif // MEDIALIBRARY_PERMISSION_UTILS_H
107