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::vector<std::string> WRITE_PERMS_V10 = { 46 PERM_WRITE_IMAGEVIDEO, 47 PERM_WRITE_AUDIO, 48 PERM_WRITE_DOCUMENT 49 }; 50 51 class PermissionUtils { 52 public: 53 static bool CheckCallerPermission(const std::string &permission); 54 static bool CheckNapiCallerPermission(const std::string &permission); 55 static bool CheckCallerPermission(const std::vector<std::string> &perms); 56 static bool CheckHasPermission(const std::vector<std::string> &perms); 57 static void GetClientBundle(const int uid, std::string &bundleName); 58 static uint32_t GetTokenId(); 59 static bool IsSystemApp(); 60 static bool IsNativeSAApp(); 61 static bool CheckIsSystemAppByUid(); 62 static std::string GetPackageNameByBundleName(const std::string &bundleName); 63 64 private: 65 static sptr<AppExecFwk::IBundleMgr> GetSysBundleManager(); 66 static sptr<AppExecFwk::IBundleMgr> bundleMgr_; 67 static std::mutex bundleMgrMutex_; 68 }; 69 } // namespace Media 70 } // namespace OHOS 71 #endif // MEDIALIBRARY_PERMISSION_UTILS_H 72