• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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      */
39     int GrantUriPermission(const Uri &uri, unsigned int flag, const std::string targetBundleName, int32_t appIndex = 0,
40         uint32_t initiatorTokenId = 0);
41 
42     /**
43      * @brief Authorize the uri permission of to targetBundleName.
44      *
45      * @param uriVec The file uri list.
46      * @param flag Want::FLAG_AUTH_READ_URI_PERMISSION or Want::FLAG_AUTH_WRITE_URI_PERMISSION.
47      * @param targetBundleName The user of uri.
48      */
49     int GrantUriPermission(const std::vector<Uri> &uriVec, unsigned int flag,
50         const std::string targetBundleName, int32_t appIndex = 0, uint32_t initiatorTokenId = 0);
51 
52     /**
53      * @brief Authorize the uri permission to targetBundleName for 2in1, only supports AbilityManagerService calls.
54      *
55      * @param uriVec The file urilist.
56      * @param flag Want::FLAG_AUTH_READ_URI_PERMISSION or Want::FLAG_AUTH_WRITE_URI_PERMISSION.
57      * @param targetBundleName The user of uri.
58      * @param appIndex The index of application in sandbox.
59      * @param isSystemAppCall The flag of system application called.
60      * @return Returns true if the authorization is successful, otherwise returns false.
61      */
62     int GrantUriPermissionFor2In1(const std::vector<Uri> &uriVec, unsigned int flag,
63         const std::string &targetBundleName, int32_t appIndex = 0, bool isSystemAppCall = false);
64 
65     /**
66      * @brief Clear user's uri authorization record with auto remove flag.
67      *
68      * @param tokenId A tokenId of an application.
69      */
70     void RevokeUriPermission(const Security::AccessToken::AccessTokenID tokenId);
71 
72     /**
73      * @brief Clear user's all uri authorization record with auto remove flag.
74      *
75      * @param tokenId A tokenId of an application.
76      */
77     int RevokeAllUriPermissions(const Security::AccessToken::AccessTokenID tokenId);
78 
79     /**
80      * @brief Clear user's uri authorization record.
81      *
82      * @param uri The file uri.
83      * @param BundleName A BundleName of an application.
84      */
85     int RevokeUriPermissionManually(const Uri &uri, const std::string bundleName);
86 
87     /**
88      * @brief check if caller can grant persistable uri permission
89      *
90      * @param uri The file uri.
91      * @param flag Want::FLAG_AUTH_READ_URI_PERMISSION or Want::FLAG_AUTH_WRITE_URI_PERMISSION.
92      * @param tokenId A tokenId of an application.
93      */
94     bool CheckPersistableUriPermissionProxy(const Uri& uri, uint32_t flag, uint32_t tokenId);
95 
96     /**
97      * @brief verify if tokenId have uri permission of flag, including temporary permission and persistable permission
98      *
99      * @param uri The file uri.
100      * @param flag Want::FLAG_AUTH_READ_URI_PERMISSION or Want::FLAG_AUTH_WRITE_URI_PERMISSION.
101      * @param tokenId A tokenId of an application.
102      */
103     bool VerifyUriPermission(const Uri& uri, uint32_t flag, uint32_t tokenId);
104 
105     bool IsAuthorizationUriAllowed(uint32_t fromTokenId);
106 
107     void OnLoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject);
108     void OnLoadSystemAbilityFail();
109 private:
110     UriPermissionManagerClient() = default;
111     sptr<IUriPermissionManager> ConnectUriPermService();
112     void ClearProxy();
113     bool LoadUriPermService();
114     void SetUriPermMgr(const sptr<IRemoteObject> &remoteObject);
115     sptr<IUriPermissionManager> GetUriPermMgr();
116     DISALLOW_COPY_AND_MOVE(UriPermissionManagerClient);
117 
118     class UpmsDeathRecipient : public IRemoteObject::DeathRecipient {
119     public:
UpmsDeathRecipient(const ClearProxyCallback & proxy)120         explicit UpmsDeathRecipient(const ClearProxyCallback &proxy) : proxy_(proxy) {}
121         ~UpmsDeathRecipient() = default;
122         virtual void OnRemoteDied([[maybe_unused]] const wptr<IRemoteObject>& remote) override;
123 
124     private:
125         ClearProxyCallback proxy_;
126     };
127 
128 private:
129     std::mutex mutex_;
130     std::mutex saLoadMutex_;
131     std::condition_variable loadSaVariable_;
132     bool saLoadFinished_ = false;
133     sptr<IUriPermissionManager> uriPermMgr_ = nullptr;
134 };
135 }  // namespace AAFwk
136 }  // namespace OHOS
137 #endif  // OHOS_ABILITY_RUNTIME_URI_PERMISSION_MANAGER_CLIENT_H
138