• 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_FILEMGMT_BACKUP_SVC_BACKUP_CONNECTION_H
17 #define OHOS_FILEMGMT_BACKUP_SVC_BACKUP_CONNECTION_H
18 
19 #include "ability_connect_callback_stub.h"
20 #include "i_extension.h"
21 
22 namespace OHOS::FileManagement::Backup {
23 class SvcBackupConnection : public AAFwk::AbilityConnectionStub {
24 public:
25     /**
26      * @brief This method is called back to receive the connection result after an ability calls the
27      * ConnectAbility method to connect it to an extension ability.
28      *
29      * @param element: Indicates information about the connected extension ability.
30      * @param remote: Indicates the remote proxy object of the extension ability.
31      * @param resultCode: Indicates the connection result code. The value 0 indicates a successful connection, and any
32      * other value indicates a connection failure.
33      */
34     void OnAbilityConnectDone(const AppExecFwk::ElementName &element,
35                               const sptr<IRemoteObject> &remoteObject,
36                               int resultCode) override;
37 
38     /**
39      * @brief This method is called back to receive the disconnection result after the connected extension ability
40      * crashes or is killed. If the extension ability exits unexpectedly, all its connections are disconnected, and
41      * each ability previously connected to it will call onAbilityDisconnectDone.
42      *
43      * @param element: Indicates information about the disconnected extension ability.
44      * @param resultCode: Indicates the disconnection result code. The value 0 indicates a successful disconnection,
45      * and any other value indicates a disconnection failure.
46      */
47     void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override;
48 
49     /**
50      * @brief connect remote ability of ExtBackup.
51      */
52     ErrCode ConnectBackupExtAbility(AAFwk::Want &want, int32_t userId);
53 
54     /**
55      * @brief disconnect remote ability of ExtBackup.
56      */
57     ErrCode DisconnectBackupExtAbility();
58 
59     /**
60      * @brief check whether connected to remote extension ability.
61      *
62      * @return bool true if connected, otherwise false.
63      */
64     bool IsExtAbilityConnected();
65 
66     /**
67      * @brief get the proxy of backup extension ability.
68      *
69      * @return the proxy of backup extension ability.
70      */
71     sptr<IExtension> GetBackupExtProxy();
72 
73     /**
74      * @brief Set the Callback object
75      *
76      * @param callConnDone
77      */
78     void SetCallback(std::function<void(const std::string &&)> callConnDone);
79 
80 public:
SvcBackupConnection(std::function<void (const std::string &&)> callDied,std::function<void (const std::string &&)> callConnDone)81     SvcBackupConnection(std::function<void(const std::string &&)> callDied,
82                         std::function<void(const std::string &&)> callConnDone)
83         : callDied_(callDied), callConnDone_(callConnDone)
84     {
85     }
~SvcBackupConnection()86     ~SvcBackupConnection() override {};
87 
88 private:
89     std::mutex mutex_;
90     std::condition_variable condition_;
91     std::atomic<bool> isConnected_ = {false};
92     std::atomic<bool> isConnectedDone_ = {false};
93     sptr<IExtension> backupProxy_;
94 
95     std::function<void(const std::string &&)> callDied_;
96     std::function<void(const std::string &&)> callConnDone_;
97 };
98 } // namespace OHOS::FileManagement::Backup
99 
100 #endif // OHOS_FILEMGMT_BACKUP_SVC_BACKUP_CONNECTION_H