• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_STUB_IMPL_H
17 #define OHOS_ABILITY_RUNTIME_URI_PERMISSION_MANAGER_STUB_IMPL_H
18 
19 #include <functional>
20 #include <map>
21 
22 #include "bundlemgr/bundle_mgr_interface.h"
23 #include "foundation/filemanagement/storage_service/services/storage_manager/include/ipc/storage_manager_proxy.h"
24 #include "istorage_manager.h"
25 #include "uri.h"
26 #include "uri_permission_manager_stub.h"
27 
28 namespace OHOS {
29 namespace AAFwk {
30 using ClearProxyCallback = std::function<void(const wptr<IRemoteObject>&)>;
31 
32 struct GrantInfo {
33     unsigned int flag;
34     const unsigned int fromTokenId;
35     const unsigned int targetTokenId;
36 };
37 class UriPermissionManagerStubImpl : public UriPermissionManagerStub,
38                                      public std::enable_shared_from_this<UriPermissionManagerStubImpl> {
39 public:
40     UriPermissionManagerStubImpl() = default;
41     virtual ~UriPermissionManagerStubImpl() = default;
42 
43     bool GrantUriPermission(const Uri &uri, unsigned int flag, const Security::AccessToken::AccessTokenID fromTokenId,
44         const Security::AccessToken::AccessTokenID targetTokenId) override;
45 
46     bool VerifyUriPermission(const Uri &uri, unsigned int flag,
47         const Security::AccessToken::AccessTokenID tokenId) override;
48 
49     void RemoveUriPermission(const Security::AccessToken::AccessTokenID tokenId) override;
50 
51 private:
52     sptr<AppExecFwk::IBundleMgr> ConnectBundleManager();
53     sptr<StorageManager::IStorageManager> ConnectStorageManager();
54     int GetCurrentAccountId();
55     void ClearBMSProxy();
56     void ClearSMProxy();
57 
58     class BMSOrSMDeathRecipient : public IRemoteObject::DeathRecipient {
59     public:
BMSOrSMDeathRecipient(const ClearProxyCallback & proxy)60         BMSOrSMDeathRecipient(const ClearProxyCallback &proxy) : proxy_(proxy) {}
61         ~BMSOrSMDeathRecipient() = default;
62         virtual void OnRemoteDied([[maybe_unused]] const wptr<IRemoteObject>& remote) override;
63 
64     private:
65         ClearProxyCallback proxy_;
66     };
67 
68 private:
69     std::map<std::string, std::list<GrantInfo>> uriMap_;
70     std::mutex mutex_;
71     std::mutex bmsMutex_;
72     std::mutex storageMutex_;
73     sptr<AppExecFwk::IBundleMgr> bundleManager_ = nullptr;
74     sptr<StorageManager::IStorageManager> storageManager_ = nullptr;
75 };
76 }  // namespace AAFwk
77 }  // namespace OHOS
78 #endif  // OHOS_ABILITY_RUNTIME_URI_PERMISSION_MANAGER_STUB_IMPL_H
79