1 /* 2 * Copyright (C) 2022 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 #ifndef MEDIALIBRARY_PERMISSION_UTILS_H 16 #define MEDIALIBRARY_PERMISSION_UTILS_H 17 18 #include <array> 19 #include <string> 20 #include <unordered_map> 21 #include <vector> 22 23 #include "bundle_mgr_interface.h" 24 #include "userfile_manager_types.h" 25 26 namespace OHOS { 27 namespace Media { 28 const std::string PERMISSION_NAME_READ_MEDIA = "ohos.permission.READ_MEDIA"; 29 const std::string PERMISSION_NAME_WRITE_MEDIA = "ohos.permission.WRITE_MEDIA"; 30 const std::string PERMISSION_NAME_MEDIA_LOCATION = "ohos.permission.MEDIA_LOCATION"; 31 const std::string PERM_READ_IMAGEVIDEO = "ohos.permission.READ_IMAGEVIDEO"; 32 const std::string PERM_READ_AUDIO = "ohos.permission.READ_AUDIO"; 33 const std::string PERM_READ_DOCUMENT = "ohos.permission.READ_DOCUMENT"; 34 const std::string PERM_WRITE_IMAGEVIDEO = "ohos.permission.WRITE_IMAGEVIDEO"; 35 const std::string PERM_WRITE_AUDIO = "ohos.permission.WRITE_AUDIO"; 36 const std::string PERM_WRITE_DOCUMENT = "ohos.permission.WRITE_DOCUMENT"; 37 38 constexpr int PERM_GRP_SIZE = 3; 39 const std::array<std::string, PERM_GRP_SIZE> READ_PERMS = { 40 PERM_READ_IMAGEVIDEO, 41 PERM_READ_AUDIO, 42 PERM_READ_DOCUMENT 43 }; 44 45 const std::array<std::string, PERM_GRP_SIZE> WRITE_PERMS = { 46 PERM_WRITE_IMAGEVIDEO, 47 PERM_WRITE_AUDIO, 48 PERM_WRITE_DOCUMENT 49 }; 50 51 const std::unordered_map<std::string, MediaTypeMaskInteger> PERM_MASK_MAP = { 52 { PERM_READ_IMAGEVIDEO, MediaTypeMaskInteger::BIT_IMAGEVIDEO }, 53 { PERM_READ_AUDIO, MediaTypeMaskInteger::BIT_AUDIO }, 54 { PERM_READ_DOCUMENT, MediaTypeMaskInteger::BIT_DOCUMENT }, 55 { PERM_WRITE_IMAGEVIDEO, MediaTypeMaskInteger::BIT_IMAGEVIDEO }, 56 { PERM_WRITE_AUDIO, MediaTypeMaskInteger::BIT_AUDIO }, 57 { PERM_WRITE_DOCUMENT, MediaTypeMaskInteger::BIT_DOCUMENT }, 58 }; 59 class PermissionUtils { 60 public: 61 static bool CheckCallerPermission(const std::string &permission); 62 static bool CheckCallerPermission(const std::array<std::string, PERM_GRP_SIZE> &perms, const uint32_t permMask); 63 static void GetClientBundle(const int uid, std::string &bundleName, bool &isSystemApp); 64 65 private: 66 static sptr<AppExecFwk::IBundleMgr> GetSysBundleManager(); 67 static sptr<AppExecFwk::IBundleMgr> bundleMgr_; 68 static std::mutex bundleMgrMutex_; 69 }; 70 } // namespace Media 71 } // namespace OHOS 72 #endif // MEDIALIBRARY_PERMISSION_UTILS_H 73