• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_SVC_DISTRIBUTED_CONNECTION_H
17 #define OHOS_SVC_DISTRIBUTED_CONNECTION_H
18 
19 #include "ability_connect_callback_stub.h"
20 #include "i_distributed_extension.h"
21 
22 namespace OHOS {
23 namespace DistributedSchedule {
24 class SvcDistributedConnection : 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, 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 ConnectDExtAbility(AAFwk::Want &want, int32_t userId, bool isCleanCalled);
53 
54     /**
55      * @brief disconnect remote ability of ExtBackup.
56      */
57     ErrCode DisconnectDistributedExtAbility();
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<IDExtension> GetDistributedExtProxy();
72 
73     /**
74      * @brief Set the Callback object
75      *
76      * @param callConnected
77      */
78     void SetCallback(std::function<void(const std::string &&)> callConnected);
79 
80 public:
SvcDistributedConnection(std::string bundleNameIndexInfo)81     SvcDistributedConnection(std::string bundleNameIndexInfo) : bundleNameIndexInfo_(bundleNameIndexInfo)
82     {}
~SvcDistributedConnection()83     ~SvcDistributedConnection() override {};
84 
85 private:
86     std::mutex mutex_;
87     std::condition_variable condition_;
88     std::atomic<bool> isConnected_ = {false};
89     std::atomic<bool> isCleanCalled_ = {false};
90     std::atomic<bool> isConnectCalled_ = {false};
91     sptr<IDExtension> distributedProxy_;
92 
93     std::function<void(const std::string &&)> callConnected_;
94     std::string bundleNameIndexInfo_;
95 };
96 }
97 }
98 
99 #endif // OHOS_SVC_DISTRIBUTED_CONNECTION_H
100