1 /* 2 * Copyright (c) 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_SA_BACKUP_CONNECTION_H 17 #define OHOS_SA_BACKUP_CONNECTION_H 18 19 #include "if_local_ability_manager.h" 20 #include "if_system_ability_manager.h" 21 #include "iservice_registry.h" 22 #include "system_ability_load_callback_stub.h" 23 #include <memory> 24 #include "unique_fd.h" 25 #include "thread_pool.h" 26 #include "b_resources/b_constants.h" 27 28 namespace OHOS::FileManagement::Backup { 29 class SABackupConnection : public SystemAbilityExtensionPara { 30 public: 31 bool InputParaSet(MessageParcel& data) override; 32 bool OutputParaGet(MessageParcel& reply) override; 33 34 /** 35 * @brief connect remote BackupSAExt. 36 * 37 * @return ErrCode OK if connect, otherwise connect failed. 38 */ 39 ErrCode ConnectBackupSAExt(std::string bundleName, std::string extension, std::string extInfo); 40 41 /** 42 * @brief disconnect remote BackupSAExt. 43 * 44 * @return ErrCode OK if disconnect, otherwise disconnect failed. 45 */ 46 ErrCode DisconnectBackupSAExt(); 47 48 /** 49 * @brief check whether connected to remote sa extension. 50 * 51 * @return bool true if connected, otherwise false. 52 */ 53 bool IsSAExtConnected(); 54 55 /** 56 * @brief get the proxy of BackupSAExt. 57 * 58 * @return the proxy of BackupSAExt. 59 */ 60 sptr<ILocalAbilityManager> GetBackupSAExtProxy(); 61 62 /** 63 * @brief load remote BackupSAExt. 64 * 65 * @return ErrCode OK if load, otherwise load failed. 66 */ 67 ErrCode LoadBackupSAExt(); 68 69 /** 70 * @brief load remote BackupSAExt by samgr. 71 * 72 * @return ErrCode OK if load, otherwise load failed. 73 */ 74 ErrCode LoadBackupSAExtInner(); 75 76 /** 77 * @brief call backup BackupSAExt. 78 * 79 * @return ErrCode OK if backup, otherwise backupErr. 80 */ 81 ErrCode CallBackupSA(); 82 83 /** 84 * @brief call restore BackupSAExt. 85 * 86 * @param fd restore文件描述符 87 * @return ErrCode OK if restore, otherwise restoreErr. 88 */ 89 ErrCode CallRestoreSA(UniqueFd fd); 90 91 public: SABackupConnection(std::function<void (const std::string &&)> callDied,std::function<void (const std::string &&)> callConnect,std::function<void (const std::string &&,const int &&,const std::string &&,const ErrCode &&)> callBackup,std::function<void (const std::string &&,const std::string &&,const ErrCode &&)> callRestore)92 SABackupConnection( 93 std::function<void(const std::string &&)> callDied, 94 std::function<void(const std::string &&)> callConnect, 95 std::function<void(const std::string &&, const int &&, const std::string &&, const ErrCode &&)> callBackup, 96 std::function<void(const std::string &&, const std::string &&, const ErrCode &&)> callRestore) 97 : callDied_(callDied), callConnected_(callConnect), callBackup_(callBackup), callRestore_(callRestore) 98 { 99 threadPool_.Start(BConstants::EXTENSION_THREAD_POOL_COUNT); 100 } ~SABackupConnection()101 virtual ~SABackupConnection() 102 { 103 threadPool_.Stop(); 104 }; 105 106 public: 107 class SALoadCallback : public SystemAbilityLoadCallbackStub { 108 public: 109 void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject) override; 110 void OnLoadSystemAbilityFail(int32_t systemAbilityId) override; 111 112 public: 113 std::condition_variable proxyConVar_; 114 std::atomic<bool> isLoadSuccess_ = {false}; 115 }; 116 117 private: 118 std::function<void(const std::string &&)> callDied_; 119 std::function<void(const std::string &&)> callConnected_; 120 std::function<void(const std::string &&, const int &&, const std::string &&, const ErrCode &&)> callBackup_; 121 std::function<void(const std::string &&, const std::string &&, const ErrCode &&)> callRestore_; 122 std::mutex mutex_; 123 std::condition_variable condition_; 124 std::atomic<bool> isConnected_ = {false}; 125 std::atomic<bool> isLoaded_ = {false}; 126 sptr<ILocalAbilityManager> proxy_; 127 OHOS::ThreadPool threadPool_; 128 std::atomic<int> reloadNum_ = 0; 129 std::string extension_; 130 std::string bundleName_; 131 int32_t saId_ = BConstants::BACKUP_DEFAULT_SA_ID; 132 MessageParcel parcel_; 133 std::string extInfo_; 134 UniqueFd fd_; 135 }; 136 } // namespace OHOS::FileManagement::Backup 137 138 #endif // OHOS_SA_BACKUP_CONNECTION_H