• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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 "b_radar/radar_const.h"
21 #include "iextension.h"
22 
23 namespace OHOS::FileManagement::Backup {
24 class SvcBackupConnection : public AAFwk::AbilityConnectionStub {
25 public:
26     /**
27      * @brief This method is called back to receive the connection result after an ability calls the
28      * ConnectAbility method to connect it to an extension ability.
29      *
30      * @param element: Indicates information about the connected extension ability.
31      * @param remote: Indicates the remote proxy object of the extension ability.
32      * @param resultCode: Indicates the connection result code. The value 0 indicates a successful connection, and any
33      * other value indicates a connection failure.
34      */
35     void OnAbilityConnectDone(const AppExecFwk::ElementName &element,
36                               const sptr<IRemoteObject> &remoteObject,
37                               int resultCode) override;
38 
39     /**
40      * @brief This method is called back to receive the disconnection result after the connected extension ability
41      * crashes or is killed. If the extension ability exits unexpectedly, all its connections are disconnected, and
42      * each ability previously connected to it will call onAbilityDisconnectDone.
43      *
44      * @param element: Indicates information about the disconnected extension ability.
45      * @param resultCode: Indicates the disconnection result code. The value 0 indicates a successful disconnection,
46      * and any other value indicates a disconnection failure.
47      */
48     void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override;
49 
50     /**
51      * @brief connect remote ability of ExtBackup.
52      */
53     ErrCode ConnectBackupExtAbility(AAFwk::Want &want, int32_t userId, bool isCleanCalled);
54 
55     /**
56      * @brief disconnect remote ability of ExtBackup.
57      */
58     ErrCode DisconnectBackupExtAbility();
59 
60     /**
61      * @brief check whether connected to remote extension ability.
62      *
63      * @return bool true if connected, otherwise false.
64      */
65     bool IsExtAbilityConnected();
66 
67     /**
68      * @brief get the proxy of backup extension ability.
69      *
70      * @return the proxy of backup extension ability.
71      */
72     sptr<IExtension> GetBackupExtProxy();
73 
74     /**
75      * @brief Set the Callback object
76      *
77      * @param callConnected
78      */
79     void SetCallback(std::function<void(const std::string &&)> callConnected);
80 
81     /**
82      * @brief Set the CallDied object
83      *
84      * @param callDied
85      */
86     void SetCallDied(std::function<void(const std::string &&, bool)> callDied);
87 
88     /**
89      * @brief wait disconnect done
90      */
91     bool WaitDisconnectDone();
92 
GetConnectSpan()93     uint32_t GetConnectSpan() { return connectSpend_.GetSpan(); }
94 
GetError()95     BError& GetError() { return error_; }
96 public:
SvcBackupConnection(std::function<void (const std::string &&,bool)> callDied,std::function<void (const std::string &&)> callConnected,std::string bundleNameIndexInfo)97     SvcBackupConnection(std::function<void(const std::string &&, bool)> callDied,
98                         std::function<void(const std::string &&)> callConnected,
99                         std::string bundleNameIndexInfo)
100         : callDied_(callDied), callConnected_(callConnected), bundleNameIndexInfo_(bundleNameIndexInfo)
101     {
102     }
~SvcBackupConnection()103     ~SvcBackupConnection() override {};
104 
105 private:
106     std::mutex mutex_;
107     std::mutex waitMutex_;
108     std::condition_variable condition_;
109     std::condition_variable waitCondition_;
110     std::atomic<bool> isConnected_ = {false};
111     std::atomic<bool> isCleanCalled_ = {false};
112     std::atomic<bool> isConnectCalled_ = {false};
113     sptr<IExtension> backupProxy_;
114 
115     std::function<void(const std::string &&, bool)> callDied_;
116     std::function<void(const std::string &&)> callConnected_;
117     std::string bundleNameIndexInfo_;
118     Duration connectSpend_;
119     BError error_;
120 };
121 } // namespace OHOS::FileManagement::Backup
122 
123 #endif // OHOS_FILEMGMT_BACKUP_SVC_BACKUP_CONNECTION_H