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 callConnected 77 */ 78 void SetCallback(std::function<void(const std::string &&)> callConnected); 79 80 /** 81 * @brief Set the CallDied object 82 * 83 * @param callDied 84 */ 85 void SetCallDied(std::function<void(const std::string &&, bool)> callDied); 86 87 /** 88 * @brief wait disconnect done 89 */ 90 bool WaitDisconnectDone(); 91 92 public: SvcBackupConnection(std::function<void (const std::string &&,bool)> callDied,std::function<void (const std::string &&)> callConnected,std::string bundleNameIndexInfo)93 SvcBackupConnection(std::function<void(const std::string &&, bool)> callDied, 94 std::function<void(const std::string &&)> callConnected, 95 std::string bundleNameIndexInfo) 96 : callDied_(callDied), callConnected_(callConnected), bundleNameIndexInfo_(bundleNameIndexInfo) 97 { 98 } ~SvcBackupConnection()99 ~SvcBackupConnection() override {}; 100 101 private: 102 std::mutex mutex_; 103 std::mutex waitMutex_; 104 std::condition_variable condition_; 105 std::condition_variable waitCondition_; 106 std::atomic<bool> isConnected_ = {false}; 107 std::atomic<bool> isConnectedDone_ = {false}; 108 std::atomic<bool> isSecondOnDisCon_ = {false}; 109 sptr<IExtension> backupProxy_; 110 111 std::function<void(const std::string &&, bool)> callDied_; 112 std::function<void(const std::string &&)> callConnected_; 113 std::string bundleNameIndexInfo_; 114 }; 115 } // namespace OHOS::FileManagement::Backup 116 117 #endif // OHOS_FILEMGMT_BACKUP_SVC_BACKUP_CONNECTION_H