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 #define COMPILE_HIDDEN __attribute__ ((visibility ("hidden"))) 29 const std::string PERMISSION_NAME_READ_MEDIA = "ohos.permission.READ_MEDIA"; 30 const std::string PERMISSION_NAME_WRITE_MEDIA = "ohos.permission.WRITE_MEDIA"; 31 const std::string PERMISSION_NAME_MEDIA_LOCATION = "ohos.permission.MEDIA_LOCATION"; 32 const std::string PERM_READ_IMAGEVIDEO = "ohos.permission.READ_IMAGEVIDEO"; 33 const std::string PERM_READ_AUDIO = "ohos.permission.READ_AUDIO"; 34 const std::string PERM_READ_DOCUMENT = "ohos.permission.READ_DOCUMENT"; 35 const std::string PERM_WRITE_IMAGEVIDEO = "ohos.permission.WRITE_IMAGEVIDEO"; 36 const std::string PERM_WRITE_AUDIO = "ohos.permission.WRITE_AUDIO"; 37 const std::string PERM_WRITE_DOCUMENT = "ohos.permission.WRITE_DOCUMENT"; 38 const std::string PERM_MANAGE_PRIVATE_PHOTOS = "ohos.permission.MANAGE_PRIVATE_PHOTOS"; 39 40 const std::vector<std::string> WRITE_PERMS_V10 = { 41 PERM_WRITE_IMAGEVIDEO, 42 PERM_WRITE_AUDIO, 43 PERM_WRITE_DOCUMENT 44 }; 45 46 class PermissionUtils { 47 public: 48 static bool CheckCallerPermission(const std::string &permission); 49 static bool CheckCallerPermission(const std::vector<std::string> &perms); 50 static bool CheckHasPermission(const std::vector<std::string> &perms); 51 static void GetClientBundle(const int uid, std::string &bundleName); 52 static uint32_t GetTokenId(); 53 static bool IsSystemApp(); 54 static bool IsNativeSAApp(); 55 static bool CheckIsSystemAppByUid(); 56 static std::string GetPackageNameByBundleName(const std::string &bundleName); 57 58 private: 59 static sptr<AppExecFwk::IBundleMgr> GetSysBundleManager(); 60 COMPILE_HIDDEN static sptr<AppExecFwk::IBundleMgr> bundleMgr_; 61 COMPILE_HIDDEN static std::mutex bundleMgrMutex_; 62 }; 63 } // namespace Media 64 } // namespace OHOS 65 #endif // MEDIALIBRARY_PERMISSION_UTILS_H 66