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 FILE_ACCESS_EXT_CONNECTION_H
17 #define FILE_ACCESS_EXT_CONNECTION_H
18 
19 #include <atomic>
20 #include <mutex>
21 
22 #include "ability_connect_callback_stub.h"
23 #include "element_name.h"
24 #include "ifile_access_ext_base.h"
25 #include "file_access_ext_proxy.h"
26 #include "file_access_service.h"
27 #include "iremote_object.h"
28 #include "refbase.h"
29 #include "want.h"
30 
31 namespace OHOS {
32 namespace FileAccessFwk {
33 class FileAccessService;
34 
35 class FileAccessExtConnection : public AAFwk::AbilityConnectionStub {
36 public:
37     FileAccessExtConnection() = default;
38     virtual ~FileAccessExtConnection() = default;
39 
40     void OnAbilityConnectDone(
41         const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode) override;
42     void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override;
43     void ConnectFileExtAbility(const AAFwk::Want &want, const sptr<IRemoteObject> &token);
44     void DisconnectFileExtAbility();
45     bool IsExtAbilityConnected();
46     sptr<IFileAccessExtBase> GetFileExtProxy();
47 
48 private:
49     struct ThreadLockInfo {
50         std::condition_variable condition;
51         std::mutex mutex;
52         bool isReady = false;
53     };
54     ThreadLockInfo connectLockInfo_;
55 
56     static std::mutex mutex_;
57     std::atomic<bool> isConnected_ = {false};
58     sptr<IFileAccessExtBase> fileExtProxy_;
59 };
60 
61 
62 class AgentFileAccessExtConnection : public AAFwk::AbilityConnectionStub {
63 public:
AgentFileAccessExtConnection(const sptr<AAFwk::IAbilityConnection> & connection)64     AgentFileAccessExtConnection(const sptr<AAFwk::IAbilityConnection>& connection) : connection_(connection){};
65     virtual ~AgentFileAccessExtConnection();
66     void OnAbilityConnectDone(
67         const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode) override;
68     void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override;
69     void ConnectFileExtAbility(const AAFwk::Want &want);
70     void DisconnectFileExtAbility();
71 private:
72     const sptr<AAFwk::IAbilityConnection> connection_ = nullptr;
73 };
74 
75 } // namespace FileAccessFwk
76 } // namespace OHOS
77 #endif // FILE_ACCESS_EXT_CONNECTION_H
78