1 /* 2 * Copyright (c) 2022-2023 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 16 #ifndef OHOS_ABILITY_RUNTIME_URI_PERMISSION_MANAGER_CLIENT_H 17 #define OHOS_ABILITY_RUNTIME_URI_PERMISSION_MANAGER_CLIENT_H 18 19 #include <functional> 20 21 #include "uri.h" 22 #include "uri_permission_manager_interface.h" 23 24 namespace OHOS { 25 namespace AAFwk { 26 using ClearProxyCallback = std::function<void()>; 27 class UriPermissionManagerClient { 28 public: 29 static UriPermissionManagerClient& GetInstance(); 30 ~UriPermissionManagerClient() = default; 31 32 /** 33 * @brief Authorize the uri permission of to targetBundleName. 34 * 35 * @param uri The file uri. 36 * @param flag Want::FLAG_AUTH_READ_URI_PERMISSION or Want::FLAG_AUTH_WRITE_URI_PERMISSION. 37 * @param targetBundleName The user of uri. 38 * @param autoremove the uri is temperarily or not 39 */ 40 int GrantUriPermission(const Uri &uri, unsigned int flag, 41 const std::string targetBundleName, int autoremove, int32_t appIndex = 0); 42 43 /** 44 * @brief Clear user's uri authorization record with auto remove flag. 45 * 46 * @param tokenId A tokenId of an application. 47 */ 48 void RevokeUriPermission(const Security::AccessToken::AccessTokenID tokenId); 49 50 /** 51 * @brief Clear user's all uri authorization record with auto remove flag. 52 * 53 * @param tokenId A tokenId of an application. 54 */ 55 int RevokeAllUriPermissions(const Security::AccessToken::AccessTokenID tokenId); 56 57 /** 58 * @brief Clear user's uri authorization record. 59 * 60 * @param uri The file uri. 61 * @param BundleName A BundleName of an application. 62 */ 63 int RevokeUriPermissionManually(const Uri &uri, const std::string bundleName); 64 65 /** 66 * @brief check if caller can grant persistable uri permission 67 * 68 * @param uri The file uri. 69 * @param flag Want::FLAG_AUTH_READ_URI_PERMISSION or Want::FLAG_AUTH_WRITE_URI_PERMISSION. 70 * @param tokenId A tokenId of an application. 71 */ 72 bool CheckPersistableUriPermissionProxy(const Uri& uri, uint32_t flag, uint32_t tokenId); 73 74 /** 75 * @brief verify if tokenId have uri permission of flag, including temporary permission and persistable permission 76 * 77 * @param uri The file uri. 78 * @param flag Want::FLAG_AUTH_READ_URI_PERMISSION or Want::FLAG_AUTH_WRITE_URI_PERMISSION. 79 * @param tokenId A tokenId of an application. 80 */ 81 bool VerifyUriPermission(const Uri& uri, uint32_t flag, uint32_t tokenId); 82 83 void OnLoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject); 84 void OnLoadSystemAbilityFail(); 85 private: 86 UriPermissionManagerClient() = default; 87 sptr<IUriPermissionManager> ConnectUriPermService(); 88 void ClearProxy(); 89 bool LoadUriPermService(); 90 void SetUriPermMgr(const sptr<IRemoteObject> &remoteObject); 91 sptr<IUriPermissionManager> GetUriPermMgr(); 92 DISALLOW_COPY_AND_MOVE(UriPermissionManagerClient); 93 94 class UpmsDeathRecipient : public IRemoteObject::DeathRecipient { 95 public: UpmsDeathRecipient(const ClearProxyCallback & proxy)96 explicit UpmsDeathRecipient(const ClearProxyCallback &proxy) : proxy_(proxy) {} 97 ~UpmsDeathRecipient() = default; 98 virtual void OnRemoteDied([[maybe_unused]] const wptr<IRemoteObject>& remote) override; 99 100 private: 101 ClearProxyCallback proxy_; 102 }; 103 104 private: 105 std::mutex mutex_; 106 std::mutex saLoadMutex_; 107 std::condition_variable loadSaVariable_; 108 bool saLoadFinished_ = false; 109 sptr<IUriPermissionManager> uriPermMgr_ = nullptr; 110 }; 111 } // namespace AAFwk 112 } // namespace OHOS 113 #endif // OHOS_ABILITY_RUNTIME_URI_PERMISSION_MANAGER_CLIENT_H 114