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 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 "singleton.h" 22 #include "uri.h" 23 #include "uri_permission_manager_interface.h" 24 25 namespace OHOS { 26 namespace AAFwk { 27 using ClearProxyCallback = std::function<void()>; 28 class UriPermissionManagerClient : public DelayedSingleton<UriPermissionManagerClient>, 29 public std::enable_shared_from_this<UriPermissionManagerClient> { 30 public: 31 UriPermissionManagerClient() = default; 32 ~UriPermissionManagerClient() = default; 33 34 /** 35 * @brief Authorize the uri permission of fromTokenId to targetTokenId. 36 * 37 * @param uri The file uri. 38 * @param flag Want::FLAG_AUTH_READ_URI_PERMISSION or Want::FLAG_AUTH_WRITE_URI_PERMISSION. 39 * @param fromTokenId The owner of uri. 40 * @param targetTokenId The user of uri. 41 */ 42 bool GrantUriPermission(const Uri &uri, unsigned int flag, const Security::AccessToken::AccessTokenID fromTokenId, 43 const Security::AccessToken::AccessTokenID targetTokenId); 44 45 /** 46 * @brief Check whether the tokenId has URI permissions. 47 * 48 * @param uri The file uri. 49 * @param flag Want::FLAG_AUTH_READ_URI_PERMISSION or Want::FLAG_AUTH_WRITE_URI_PERMISSION. 50 * @param tokenId The user of uri. 51 * @return Returns true if the verification is successful, otherwise returns false. 52 */ 53 bool VerifyUriPermission(const Uri &uri, unsigned int flag, const Security::AccessToken::AccessTokenID tokenId); 54 55 /** 56 * @brief Clear user's uri authorization record. 57 * 58 * @param tokenId A tokenId of an application. 59 */ 60 void RemoveUriPermission(const Security::AccessToken::AccessTokenID tokenId); 61 62 private: 63 sptr<IUriPermissionManager> ConnectUriPermService(); 64 void ClearProxy(); 65 DISALLOW_COPY_AND_MOVE(UriPermissionManagerClient); 66 67 class UpmsDeathRecipient : public IRemoteObject::DeathRecipient { 68 public: UpmsDeathRecipient(const ClearProxyCallback & proxy)69 UpmsDeathRecipient(const ClearProxyCallback &proxy) : proxy_(proxy) {} 70 ~UpmsDeathRecipient() = default; 71 virtual void OnRemoteDied([[maybe_unused]] const wptr<IRemoteObject>& remote) override; 72 73 private: 74 ClearProxyCallback proxy_; 75 }; 76 77 private: 78 std::mutex mutex_; 79 sptr<IUriPermissionManager> uriPermMgr_ = nullptr; 80 }; 81 } // namespace AAFwk 82 } // namespace OHOS 83 #endif // OHOS_ABILITY_RUNTIME_URI_PERMISSION_MANAGER_CLIENT_H 84