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 #include "b_session_backup.h"
17
18 #include "b_error/b_error.h"
19 #include "b_radar/b_radar.h"
20 #include "filemgmt_libhilog.h"
21 #include "service_proxy.h"
22 #include "service_reverse.h"
23
24 namespace OHOS::FileManagement::Backup {
25 using namespace std;
26
~BSessionBackup()27 BSessionBackup::~BSessionBackup()
28 {
29 if (!deathRecipient_) {
30 HILOGI("Death Recipient is nullptr");
31 return;
32 }
33 auto proxy = ServiceProxy::GetServiceProxyPointer();
34 if (proxy == nullptr) {
35 return;
36 }
37 auto remoteObject = proxy->AsObject();
38 if (remoteObject != nullptr) {
39 remoteObject->RemoveDeathRecipient(deathRecipient_);
40 }
41 deathRecipient_ = nullptr;
42 }
43
Init(Callbacks callbacks)44 unique_ptr<BSessionBackup> BSessionBackup::Init(Callbacks callbacks)
45 {
46 try {
47 HILOGI("Init BackupSession Begin");
48 auto backup = make_unique<BSessionBackup>();
49 ServiceProxy::InvaildInstance();
50 auto proxy = ServiceProxy::GetInstance();
51 if (proxy == nullptr) {
52 HILOGI("Failed to get backup service");
53 return nullptr;
54 }
55
56 int32_t res = proxy->InitBackupSession(sptr(new ServiceReverse(callbacks)));
57 if (res != ERR_OK) {
58 HILOGE("Failed to Backup because of %{public}d", res);
59 AppRadar::Info info("", "", "");
60 AppRadar::GetInstance().RecordBackupFuncRes(info, "BSessionBackup::Init",
61 AppRadar::GetInstance().GetUserId(), BizStageBackup::BIZ_STAGE_CREATE_BACKUP_SESSION_FAIL, res);
62 return nullptr;
63 }
64
65 backup->RegisterBackupServiceDied(callbacks.onBackupServiceDied);
66 return backup;
67 } catch (const exception &e) {
68 HILOGE("Failed to Backup because of %{public}s", e.what());
69 }
70 return nullptr;
71 }
72
Init(Callbacks callbacks,std::string & errMsg,ErrCode & errCode)73 unique_ptr<BSessionBackup> BSessionBackup::Init(Callbacks callbacks,
74 std::string &errMsg, ErrCode &errCode)
75 {
76 try {
77 HILOGI("Init BackupSession Begin");
78 auto backup = make_unique<BSessionBackup>();
79 ServiceProxy::InvaildInstance();
80 auto proxy = ServiceProxy::GetInstance();
81 if (proxy == nullptr) {
82 HILOGI("Failed to get backup service");
83 return nullptr;
84 }
85 errCode = proxy->InitBackupSession(sptr(new ServiceReverse(callbacks)), errMsg);
86 if (errCode != ERR_OK) {
87 HILOGE("Failed to Backup because of %{public}d", errCode);
88 AppRadar::Info info("", "", "");
89 AppRadar::GetInstance().RecordBackupFuncRes(info, "BSessionBackup::Init",
90 AppRadar::GetInstance().GetUserId(), BizStageBackup::BIZ_STAGE_CREATE_BACKUP_SESSION_FAIL, errCode);
91 return nullptr;
92 }
93 backup->RegisterBackupServiceDied(callbacks.onBackupServiceDied);
94 return backup;
95 } catch (const exception &e) {
96 HILOGE("Failed to Backup because of %{public}s", e.what());
97 errCode = BError(BError::Codes::SDK_INVAL_ARG);
98 }
99 return nullptr;
100 }
101
RegisterBackupServiceDied(std::function<void ()> functor)102 void BSessionBackup::RegisterBackupServiceDied(std::function<void()> functor)
103 {
104 auto proxy = ServiceProxy::GetInstance();
105 if (proxy == nullptr || !functor) {
106 return;
107 }
108 auto remoteObj = proxy->AsObject();
109 if (!remoteObj) {
110 throw BError(BError::Codes::SA_BROKEN_IPC, "Proxy's remote object can't be nullptr");
111 }
112
113 auto callback = [functor](const wptr<IRemoteObject> &obj) {
114 ServiceProxy::InvaildInstance();
115 HILOGI("Backup service died");
116 functor();
117 };
118 deathRecipient_ = sptr(new SvcDeathRecipient(callback));
119 remoteObj->AddDeathRecipient(deathRecipient_);
120 }
121
Start()122 ErrCode BSessionBackup::Start()
123 {
124 auto proxy = ServiceProxy::GetInstance();
125 if (proxy == nullptr) {
126 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
127 }
128
129 return proxy->Start();
130 }
131
GetLocalCapabilities()132 UniqueFd BSessionBackup::GetLocalCapabilities()
133 {
134 HILOGI("GetLocalCapabilities begin");
135 auto proxy = ServiceProxy::GetInstance();
136 if (proxy == nullptr) {
137 HILOGE("Failed to get backup service");
138 return UniqueFd(-EPERM);
139 }
140 UniqueFd fd = proxy->GetLocalCapabilitiesForBundleInfos();
141 if (fd < 0) {
142 HILOGE("Failed to get local capabilities for bundleinfos");
143 return UniqueFd(-EPERM);
144 }
145 return fd;
146 }
147
GetBackupDataSize(bool isPreciseScan,vector<BIncrementalData> bundleNameList)148 ErrCode BSessionBackup::GetBackupDataSize(bool isPreciseScan, vector<BIncrementalData> bundleNameList)
149 {
150 HILOGI("GetBackupDataSize Begin");
151 auto proxy = ServiceProxy::GetInstance();
152 if (proxy == nullptr) {
153 HILOGE("Failed to get backup service");
154 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
155 }
156 ErrCode err = proxy->GetBackupDataSize(isPreciseScan, bundleNameList);
157 if (err != ERR_OK) {
158 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to GetBackupDataSize").GetCode();
159 }
160 HILOGI("GetBackupDataSize end");
161 return ERR_OK;
162 }
163
AppendBundles(vector<BundleName> bundlesToBackup)164 ErrCode BSessionBackup::AppendBundles(vector<BundleName> bundlesToBackup)
165 {
166 auto proxy = ServiceProxy::GetInstance();
167 if (proxy == nullptr) {
168 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
169 }
170
171 int32_t res = proxy->AppendBundlesBackupSession(bundlesToBackup);
172 if (res != ERR_OK) {
173 std::string ss;
174 for (const auto &bundleName:bundlesToBackup) {
175 ss += bundleName + ", ";
176 }
177 AppRadar::Info info(ss.c_str(), "", "");
178 AppRadar::GetInstance().RecordBackupFuncRes(info, "BSessionBackup::AppendBundles",
179 AppRadar::GetInstance().GetUserId(), BizStageBackup::BIZ_STAGE_APPEND_BUNDLES_FAIL, res);
180 }
181 return res;
182 }
183
AppendBundles(vector<BundleName> bundlesToBackup,vector<std::string> detailInfos)184 ErrCode BSessionBackup::AppendBundles(vector<BundleName> bundlesToBackup, vector<std::string> detailInfos)
185 {
186 auto proxy = ServiceProxy::GetInstance();
187 if (proxy == nullptr) {
188 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
189 }
190
191 int32_t res = proxy->AppendBundlesDetailsBackupSession(bundlesToBackup, detailInfos);
192 if (res != ERR_OK) {
193 std::string ss;
194 for (const auto &bundleName:bundlesToBackup) {
195 ss += bundleName + ", ";
196 }
197 AppRadar::Info info(ss.c_str(), "", "AppendBundles with infos");
198 AppRadar::GetInstance().RecordBackupFuncRes(info, "BSessionBackup::AppendBundles",
199 AppRadar::GetInstance().GetUserId(), BizStageBackup::BIZ_STAGE_APPEND_BUNDLES_FAIL, res);
200 }
201 return res;
202 }
203
Finish()204 ErrCode BSessionBackup::Finish()
205 {
206 auto proxy = ServiceProxy::GetInstance();
207 if (proxy == nullptr) {
208 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
209 }
210
211 return proxy->Finish();
212 }
213
Release()214 ErrCode BSessionBackup::Release()
215 {
216 auto proxy = ServiceProxy::GetInstance();
217 if (proxy == nullptr) {
218 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
219 }
220
221 return proxy->Release();
222 }
223
Cancel(std::string bundleName)224 ErrCode BSessionBackup::Cancel(std::string bundleName)
225 {
226 ErrCode result = BError::BackupErrorCode::E_CANCEL_UNSTARTED_TASK;
227 auto proxy = ServiceProxy::GetInstance();
228 if (proxy == nullptr) {
229 HILOGE("Called Cancel, failed to get proxy.");
230 return result;
231 }
232
233 ErrCode errCode = proxy->Cancel(bundleName, result);
234 if (errCode != 0) {
235 HILOGE("proxy->Cancel failed, errCode:%{public}d.", errCode);
236 return result;
237 }
238 return result;
239 }
240 } // namespace OHOS::FileManagement::Backup