1 /*
2  * Copyright (C) 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 FILE_ACCESS_SERVICE_PROXY_H
17 #define FILE_ACCESS_SERVICE_PROXY_H
18 
19 #include "ifile_access_service_base.h"
20 #include "iremote_proxy.h"
21 #include "system_ability_load_callback_stub.h"
22 
23 namespace OHOS {
24 namespace FileAccessFwk {
25 class FileAccessServiceProxy : public IRemoteProxy<IFileAccessServiceBase> {
26 public:
FileAccessServiceProxy(const sptr<IRemoteObject> & impl)27     explicit FileAccessServiceProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IFileAccessServiceBase>(impl) {}
28     ~FileAccessServiceProxy() = default;
29     static sptr<FileAccessServiceProxy> GetInstance();
30     static void InvaildInstance();
31     int32_t OnChange(Uri uri, NotifyType notifyType) override;
32     int32_t RegisterNotify(Uri uri, bool notifyForDescendants, const sptr<IFileAccessObserver> &observer,
33         const std::shared_ptr<ConnectExtensionInfo> &info) override;
34     int32_t UnregisterNotify(Uri uri, const sptr<IFileAccessObserver> &observer,
35         const std::shared_ptr<ConnectExtensionInfo> &info) override;
36     int32_t GetExtensionProxy(const std::shared_ptr<ConnectExtensionInfo> &info,
37                              sptr<IFileAccessExtBase> &extensionProxy) override;
38     int32_t ConnectFileExtAbility(const AAFwk::Want &want,
39         const sptr<AAFwk::IAbilityConnection>& connection) override;
40     int32_t DisConnectFileExtAbility(const sptr<AAFwk::IAbilityConnection>& connection) override;
41 
42     class ServiceProxyLoadCallback : public SystemAbilityLoadCallbackStub {
43     public:
44         void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject) override;
45         void OnLoadSystemAbilityFail(int32_t systemAbilityId) override;
46         std::condition_variable proxyConVar_;
47         std::atomic<bool> isLoadSuccess_{false};
48     };
49 
50 private:
51     class ProxyDeathRecipient : public IRemoteObject::DeathRecipient {
52     public:
ProxyDeathRecipient(std::function<void (const wptr<IRemoteObject> &)> functor)53         explicit ProxyDeathRecipient(std::function<void(const wptr<IRemoteObject> &)> functor) : functor_(functor) {};
54         virtual ~ProxyDeathRecipient() = default;
55     public:
OnRemoteDied(const wptr<IRemoteObject> & object)56         void OnRemoteDied(const wptr<IRemoteObject> &object) override
57         {
58             auto ptr = object.promote();
59             if (!ptr) {
60                 HILOG_ERROR("remote object is nullptr");
61                 return;
62             }
63             ptr->RemoveDeathRecipient(this);
64             functor_(ptr);
65         };
66     private:
67         std::function<void(const wptr<IRemoteObject> &)> functor_;
68     };
69     int32_t UnregisterNotifyInternal(MessageParcel &data);
70     static inline std::mutex proxyMutex_;
71     static inline sptr<FileAccessServiceProxy> serviceProxy_;
72     static inline BrokerDelegator<FileAccessServiceProxy> delegator_;
73 };
74 } // namespace FileAccessFwk
75 } // namespace OHOS
76 #endif // FILE_ACCESS_SERVICE_PROXY_H
77