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 <sstream>
17
18 #include "b_error/b_error.h"
19 #include "b_error/b_excep_utils.h"
20 #include "b_radar/b_radar.h"
21 #include "b_resources/b_constants.h"
22 #include "filemgmt_libhilog.h"
23 #include "iservice_registry.h"
24 #include "service_proxy.h"
25 #include "system_ability_definition.h"
26 #include "svc_death_recipient.h"
27 #include "hitrace_meter.h"
28
29 namespace OHOS::FileManagement::Backup {
30 using namespace std;
31
InitRestoreSession(sptr<IServiceReverse> remote)32 ErrCode ServiceProxy::InitRestoreSession(sptr<IServiceReverse> remote)
33 {
34 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
35 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
36 MessageParcel data;
37 if (!data.WriteInterfaceToken(GetDescriptor())) {
38 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
39 }
40 MessageParcel reply;
41 MessageOption option;
42
43 if (!remote) {
44 return BError(BError::Codes::SDK_INVAL_ARG, "Empty reverse stub").GetCode();
45 }
46 if (!data.WriteRemoteObject(remote->AsObject().GetRefPtr())) {
47 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the reverse stub").GetCode();
48 }
49
50 int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_RESTORE_SESSION),
51 data, reply, option);
52 if (ret != NO_ERROR) {
53 string str = "Failed to send out the request because of " + to_string(ret);
54 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
55 }
56 return reply.ReadInt32();
57 }
58
InitRestoreSession(sptr<IServiceReverse> remote,std::string & errMsg)59 ErrCode ServiceProxy::InitRestoreSession(sptr<IServiceReverse> remote, std::string &errMsg)
60 {
61 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
62 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
63 MessageParcel data;
64 if (!data.WriteInterfaceToken(GetDescriptor())) {
65 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
66 }
67 MessageParcel reply;
68 MessageOption option;
69
70 if (!remote) {
71 return BError(BError::Codes::SDK_INVAL_ARG, "Empty reverse stub").GetCode();
72 }
73 if (!data.WriteRemoteObject(remote->AsObject().GetRefPtr())) {
74 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the reverse stub").GetCode();
75 }
76
77 int32_t ret = Remote()->SendRequest(
78 static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_RESTORE_SESSION_MSG), data, reply, option);
79 if (ret != NO_ERROR) {
80 string str = "Failed to send out the request because of " + to_string(ret);
81 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
82 }
83 if (!reply.ReadString(errMsg)) {
84 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to receive the errMsg").GetCode();
85 }
86 return reply.ReadInt32();
87 }
88
InitBackupSession(sptr<IServiceReverse> remote)89 ErrCode ServiceProxy::InitBackupSession(sptr<IServiceReverse> remote)
90 {
91 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
92 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
93 MessageParcel data;
94 if (!data.WriteInterfaceToken(GetDescriptor())) {
95 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
96 }
97 MessageParcel reply;
98 MessageOption option;
99
100 if (!remote) {
101 return BError(BError::Codes::SDK_INVAL_ARG, "Empty reverse stub").GetCode();
102 }
103 if (!data.WriteRemoteObject(remote->AsObject().GetRefPtr())) {
104 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the reverse stub").GetCode();
105 }
106
107 int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_BACKUP_SESSION),
108 data, reply, option);
109 if (ret != NO_ERROR) {
110 string str = "Failed to send out the request because of " + to_string(ret);
111 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
112 }
113 return reply.ReadInt32();
114 }
115
InitBackupSession(sptr<IServiceReverse> remote,std::string & errMsg)116 ErrCode ServiceProxy::InitBackupSession(sptr<IServiceReverse> remote, std::string &errMsg)
117 {
118 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
119 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
120 MessageParcel data;
121 if (!data.WriteInterfaceToken(GetDescriptor())) {
122 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
123 }
124 MessageParcel reply;
125 MessageOption option;
126 if (!remote) {
127 return BError(BError::Codes::SDK_INVAL_ARG, "Empty reverse stub").GetCode();
128 }
129 if (!data.WriteRemoteObject(remote->AsObject().GetRefPtr())) {
130 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the reverse stub").GetCode();
131 }
132
133 int32_t ret = Remote()->SendRequest(
134 static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_BACKUP_SESSION_MSG), data, reply, option);
135 if (ret != NO_ERROR) {
136 string str = "Failed to send out the request because of " + to_string(ret);
137 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
138 }
139 if (!reply.ReadString(errMsg)) {
140 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to receive the errMsg").GetCode();
141 }
142 return reply.ReadInt32();
143 }
144
Start()145 ErrCode ServiceProxy::Start()
146 {
147 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
148 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
149 MessageParcel data;
150 if (!data.WriteInterfaceToken(GetDescriptor())) {
151 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
152 }
153
154 MessageParcel reply;
155 MessageOption option;
156 int32_t ret =
157 Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_START), data, reply, option);
158 if (ret != NO_ERROR) {
159 string str = "Failed to send out the request because of " + to_string(ret);
160 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
161 }
162 return reply.ReadInt32();
163 }
164
GetLocalCapabilities()165 UniqueFd ServiceProxy::GetLocalCapabilities()
166 {
167 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
168 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
169 MessageParcel data;
170 if (!data.WriteInterfaceToken(GetDescriptor())) {
171 HILOGE("Failed to write descriptor");
172 return UniqueFd(-EPERM);
173 }
174
175 MessageParcel reply;
176 MessageOption option;
177 option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
178 int32_t ret = Remote()->SendRequest(
179 static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_LOCAL_CAPABILITIES), data, reply, option);
180 if (ret != NO_ERROR) {
181 HILOGE("Received error %{public}d when doing IPC", ret);
182 AppRadar::Info resInfo("", "", "");
183 AppRadar::GetInstance().RecordDefaultFuncRes(resInfo, "ServiceProxy::GetLocalCapabilities",
184 AppRadar::GetInstance().GetUserId(), BizStageBackup::BIZ_STAGE_GET_LOCAL_CAPABILITIES_FAIL, ret);
185 return UniqueFd(-ret);
186 }
187 UniqueFd fd(reply.ReadFileDescriptor());
188 return UniqueFd(fd.Release());
189 }
190
PublishFile(const BFileInfo & fileInfo)191 ErrCode ServiceProxy::PublishFile(const BFileInfo &fileInfo)
192 {
193 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
194 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
195 MessageParcel data;
196 if (!data.WriteInterfaceToken(GetDescriptor())) {
197 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
198 }
199
200 if (!data.WriteParcelable(&fileInfo)) {
201 HILOGE("Failed to send the fileInfo");
202 return -EPIPE;
203 }
204
205 MessageParcel reply;
206 MessageOption option;
207 int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_PUBLISH_FILE), data,
208 reply, option);
209 if (ret != NO_ERROR) {
210 string str = "Failed to send out the request because of " + to_string(ret);
211 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
212 }
213 return reply.ReadInt32();
214 }
215
AppFileReady(const string & fileName,UniqueFd fd,int32_t errCode)216 ErrCode ServiceProxy::AppFileReady(const string &fileName, UniqueFd fd, int32_t errCode)
217 {
218 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
219 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
220 MessageParcel data;
221 if (!data.WriteInterfaceToken(GetDescriptor())) {
222 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
223 }
224
225 if (!data.WriteString(fileName)) {
226 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the filename").GetCode();
227 }
228 bool fdFlag = fd < 0 ? false : true;
229 data.WriteBool(fdFlag);
230 if (fdFlag == true && !data.WriteFileDescriptor(fd)) {
231 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the fd").GetCode();
232 }
233 if (!data.WriteInt32(errCode)) {
234 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the errCode").GetCode();
235 }
236
237 MessageParcel reply;
238 MessageOption option;
239 int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APP_FILE_READY), data,
240 reply, option);
241 if (ret != NO_ERROR) {
242 string str = "Failed to send out the request because of " + to_string(ret);
243 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
244 }
245 return reply.ReadInt32();
246 }
247
AppDone(ErrCode errCode)248 ErrCode ServiceProxy::AppDone(ErrCode errCode)
249 {
250 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
251 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
252 MessageParcel data;
253 if (!data.WriteInterfaceToken(GetDescriptor())) {
254 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
255 }
256
257 if (!data.WriteInt32(errCode)) {
258 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the errCode").GetCode();
259 }
260
261 MessageParcel reply;
262 MessageOption option;
263 int32_t ret =
264 Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APP_DONE), data, reply, option);
265 if (ret != NO_ERROR) {
266 string str = "Failed to send out the request because of " + to_string(ret);
267 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
268 }
269 return reply.ReadInt32();
270 }
271
ServiceResultReport(const std::string restoreRetInfo,BackupRestoreScenario scenario,ErrCode errCode)272 ErrCode ServiceProxy::ServiceResultReport(const std::string restoreRetInfo,
273 BackupRestoreScenario scenario, ErrCode errCode)
274 {
275 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
276 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
277 MessageParcel data;
278 if (!data.WriteInterfaceToken(GetDescriptor())) {
279 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
280 }
281 if (!data.WriteString(restoreRetInfo)) {
282 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the restoreRetInfo").GetCode();
283 }
284 if (!data.WriteInt32(static_cast<int32_t>(scenario))) {
285 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the scenario").GetCode();
286 }
287 if (!data.WriteInt32(errCode)) {
288 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the errCode").GetCode();
289 }
290 MessageParcel reply;
291 MessageOption option;
292 int32_t ret =
293 Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_RESULT_REPORT), data, reply,
294 option);
295 if (ret != NO_ERROR) {
296 string str = "Failed to send out the request because of " + to_string(ret);
297 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
298 }
299 return reply.ReadInt32();
300 }
301
GetFileHandle(const string & bundleName,const string & fileName)302 ErrCode ServiceProxy::GetFileHandle(const string &bundleName, const string &fileName)
303 {
304 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
305 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
306 MessageParcel data;
307 if (!data.WriteInterfaceToken(GetDescriptor())) {
308 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
309 }
310
311 if (!data.WriteString(bundleName)) {
312 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the bundleName").GetCode();
313 }
314 if (!data.WriteString(fileName)) {
315 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the fileName").GetCode();
316 }
317
318 MessageParcel reply;
319 MessageOption option;
320 option.SetFlags(MessageOption::TF_ASYNC);
321 int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_FILE_NAME), data,
322 reply, option);
323 if (ret != NO_ERROR) {
324 string str = "Failed to send out the request because of " + to_string(ret);
325 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
326 }
327 return ret;
328 }
329
AppendBundlesRestoreSession(UniqueFd fd,const vector<BundleName> & bundleNames,const std::vector<std::string> & detailInfos,RestoreTypeEnum restoreType,int32_t userId)330 ErrCode ServiceProxy::AppendBundlesRestoreSession(UniqueFd fd, const vector<BundleName> &bundleNames,
331 const std::vector<std::string> &detailInfos, RestoreTypeEnum restoreType, int32_t userId)
332 {
333 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
334 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
335 MessageParcel data;
336 if (!data.WriteInterfaceToken(GetDescriptor())) {
337 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
338 }
339 MessageParcel reply;
340 MessageOption option;
341 option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
342
343 if (!data.WriteFileDescriptor(fd)) {
344 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the fd").GetCode();
345 }
346 if (!data.WriteStringVector(bundleNames)) {
347 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send bundleNames").GetCode();
348 }
349 if (!detailInfos.empty() && !data.WriteStringVector(detailInfos)) {
350 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send detailInfos").GetCode();
351 }
352 if (!data.WriteInt32(static_cast<int32_t>(restoreType))) {
353 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send restoreType").GetCode();
354 }
355 if (!data.WriteInt32(userId)) {
356 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send userId").GetCode();
357 }
358 int32_t ret = Remote()->SendRequest(
359 static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_RESTORE_SESSION_DETAIL), data, reply,
360 option);
361 if (ret != NO_ERROR) {
362 string str = "Failed to send out the request because of " + to_string(ret);
363 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
364 }
365 return reply.ReadInt32();
366 }
367
AppendBundlesRestoreSession(UniqueFd fd,const vector<BundleName> & bundleNames,RestoreTypeEnum restoreType,int32_t userId)368 ErrCode ServiceProxy::AppendBundlesRestoreSession(UniqueFd fd,
369 const vector<BundleName> &bundleNames,
370 RestoreTypeEnum restoreType,
371 int32_t userId)
372 {
373 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
374 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
375 MessageParcel data;
376 if (!data.WriteInterfaceToken(GetDescriptor())) {
377 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
378 }
379 MessageParcel reply;
380 MessageOption option;
381 option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
382
383 if (!data.WriteFileDescriptor(fd)) {
384 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the fd").GetCode();
385 }
386 if (!data.WriteStringVector(bundleNames)) {
387 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send bundleNames").GetCode();
388 }
389 if (!data.WriteInt32(static_cast<int32_t>(restoreType))) {
390 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send restoreType").GetCode();
391 }
392 if (!data.WriteInt32(userId)) {
393 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send userId").GetCode();
394 }
395
396 int32_t ret = Remote()->SendRequest(
397 static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_RESTORE_SESSION), data, reply, option);
398 if (ret != NO_ERROR) {
399 string str = "Failed to send out the request because of " + to_string(ret);
400 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
401 }
402 return reply.ReadInt32();
403 }
404
AppendBundlesBackupSession(const vector<BundleName> & bundleNames)405 ErrCode ServiceProxy::AppendBundlesBackupSession(const vector<BundleName> &bundleNames)
406 {
407 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
408 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
409 MessageParcel data;
410 if (!data.WriteInterfaceToken(GetDescriptor())) {
411 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
412 }
413 MessageParcel reply;
414 MessageOption option;
415 option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
416
417 if (!data.WriteStringVector(bundleNames)) {
418 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send bundleNames").GetCode();
419 }
420
421 int32_t ret = Remote()->SendRequest(
422 static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_BACKUP_SESSION), data, reply, option);
423 if (ret != NO_ERROR) {
424 string str = "Failed to send out the request because of " + to_string(ret);
425 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
426 }
427 return reply.ReadInt32();
428 }
429
AppendBundlesDetailsBackupSession(const vector<BundleName> & bundleNames,const vector<std::string> & detailInfos)430 ErrCode ServiceProxy::AppendBundlesDetailsBackupSession(const vector<BundleName> &bundleNames,
431 const vector<std::string> &detailInfos)
432 {
433 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
434 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
435 MessageParcel data;
436 if (!data.WriteInterfaceToken(GetDescriptor())) {
437 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
438 }
439 MessageParcel reply;
440 MessageOption option;
441 option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
442
443 if (!data.WriteStringVector(bundleNames)) {
444 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send bundleNames").GetCode();
445 }
446
447 if (!data.WriteStringVector(detailInfos)) {
448 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send detailInfos").GetCode();
449 }
450
451 int32_t ret = Remote()->SendRequest(
452 static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_BACKUP_SESSION_DETAILS),
453 data, reply, option);
454 if (ret != NO_ERROR) {
455 string str = "Failed to send out the request because of " + to_string(ret);
456 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
457 }
458 return reply.ReadInt32();
459 }
460
Finish()461 ErrCode ServiceProxy::Finish()
462 {
463 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
464 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
465 MessageParcel data;
466 if (!data.WriteInterfaceToken(GetDescriptor())) {
467 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
468 }
469
470 MessageParcel reply;
471 MessageOption option;
472 int32_t ret =
473 Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_FINISH), data, reply, option);
474 if (ret != NO_ERROR) {
475 string str = "Failed to send out the request because of " + to_string(ret);
476 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
477 }
478 return reply.ReadInt32();
479 }
480
GetServiceProxyPointer()481 sptr<IService> ServiceProxy::GetServiceProxyPointer()
482 {
483 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
484 return serviceProxy_;
485 }
486
GetInstance()487 sptr<IService> ServiceProxy::GetInstance()
488 {
489 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
490 unique_lock<mutex> getInstanceLock(getInstanceMutex_);
491 unique_lock<mutex> lock(proxyMutex_);
492 if (serviceProxy_ != nullptr) {
493 return serviceProxy_;
494 }
495
496 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
497 if (!samgr) {
498 HILOGE("Get an empty samgr");
499 return nullptr;
500 }
501 sptr<ServiceProxyLoadCallback> loadCallback = sptr(new ServiceProxyLoadCallback());
502 if (loadCallback == nullptr) {
503 HILOGE("loadCallback is nullptr.");
504 return nullptr;
505 }
506 int32_t ret = samgr->LoadSystemAbility(FILEMANAGEMENT_BACKUP_SERVICE_SA_ID, loadCallback);
507 if (ret != ERR_OK) {
508 HILOGE("Failed to Load systemAbility, systemAbilityId:%{private}d, ret code:%{public}d",
509 FILEMANAGEMENT_BACKUP_SERVICE_SA_ID, ret);
510 return nullptr;
511 }
512
513 auto waitStatus =
514 loadCallback->proxyConVar_.wait_for(lock, std::chrono::milliseconds(BConstants::BACKUP_LOADSA_TIMEOUT_MS),
515 [loadCallback]() { return loadCallback->isLoadSuccess_.load(); });
516 if (!waitStatus) {
517 HILOGE("Load backup sa timeout");
518 AppRadar::Info info("", "", "\"reason\":\"Load backup sa timeout\"");
519 AppRadar::GetInstance().RecordBackupFuncRes(info, "ServiceProxy::GetInstance",
520 AppRadar::GetInstance().GetUserId(), BizStageBackup::BIZ_STAGE_BOOT_BACKUP_SA_FAIL,
521 BError(BError::Codes::SA_INVAL_ARG).GetCode());
522 return nullptr;
523 }
524 return serviceProxy_;
525 }
526
InvaildInstance()527 void ServiceProxy::InvaildInstance()
528 {
529 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
530 HILOGD("invalid instance");
531 unique_lock<mutex> lock(proxyMutex_);
532 serviceProxy_ = nullptr;
533 }
534
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const OHOS::sptr<IRemoteObject> & remoteObject)535 void ServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId,
536 const OHOS::sptr<IRemoteObject> &remoteObject)
537 {
538 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
539 HILOGI("Load backup sa success, systemAbilityId: %{private}d, remoteObject result:%{private}s", systemAbilityId,
540 (remoteObject != nullptr) ? "true" : "false");
541 if (systemAbilityId != FILEMANAGEMENT_BACKUP_SERVICE_SA_ID || remoteObject == nullptr) {
542 isLoadSuccess_.store(false);
543 proxyConVar_.notify_one();
544 return;
545 }
546 unique_lock<mutex> lock(proxyMutex_);
547 serviceProxy_ = iface_cast<IService>(remoteObject);
548 if (serviceProxy_ == nullptr) {
549 HILOGD("serviceProxy_ is nullptr");
550 return;
551 }
552 auto remoteObj = serviceProxy_->AsObject();
553 if (!remoteObj) {
554 HILOGE("Failed to get remote object");
555 serviceProxy_ = nullptr;
556 isLoadSuccess_.store(false);
557 proxyConVar_.notify_one();
558 return;
559 }
560
561 auto callback = [](const wptr<IRemoteObject> &obj) {
562 ServiceProxy::InvaildInstance();
563 HILOGE("Backup service died");
564 };
565 sptr<SvcDeathRecipient> deathRecipient = sptr(new SvcDeathRecipient(callback));
566 remoteObj->AddDeathRecipient(deathRecipient);
567 isLoadSuccess_.store(true);
568 proxyConVar_.notify_one();
569 }
570
OnLoadSystemAbilityFail(int32_t systemAbilityId)571 void ServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
572 {
573 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
574 HILOGE("Load backup sa failed, systemAbilityId:%{private}d", systemAbilityId);
575 unique_lock<mutex> lock(proxyMutex_);
576 serviceProxy_ = nullptr;
577 isLoadSuccess_.store(false);
578 AppRadar::Info info("", "", "\"reason\":\"Load backup sa fail\"");
579 AppRadar::GetInstance().RecordBackupFuncRes(info, "ServiceProxyLoadCallback::OnLoadSystemAbilityFail",
580 AppRadar::GetInstance().GetUserId(), BizStageBackup::BIZ_STAGE_BOOT_BACKUP_SA_FAIL,
581 static_cast<int32_t>(BError::Codes::SA_INVAL_ARG));
582 proxyConVar_.notify_one();
583 }
584
GetBackupInfo(BundleName & bundleName,std::string & result)585 ErrCode ServiceProxy::GetBackupInfo(BundleName &bundleName, std::string &result)
586 {
587 HILOGI("ServiceProxy GetBackupInfo Begin.");
588 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
589 MessageParcel data;
590 if (!data.WriteInterfaceToken(GetDescriptor())) {
591 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
592 }
593 if (!data.WriteString(bundleName)) {
594 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send bundleName").GetCode();
595 }
596 MessageParcel reply;
597 MessageOption option;
598 option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
599 int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_BACKUP_INFO),
600 data, reply, option);
601 if (ret != NO_ERROR) {
602 string str = "Failed to send out the request because of " + to_string(ret);
603 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
604 }
605 reply.ReadString(result);
606 HILOGI("ServiceProxy GetBackupInfo end. result = %s", result.c_str());
607 return BError(BError::Codes::OK, "success");
608 }
609
UpdateTimer(BundleName & bundleName,uint32_t timeout,bool & result)610 ErrCode ServiceProxy::UpdateTimer(BundleName &bundleName, uint32_t timeout, bool &result)
611 {
612 HILOGI("ServiceProxy UpdateTimer Begin.");
613 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
614 MessageParcel data;
615 if (!data.WriteInterfaceToken(GetDescriptor())) {
616 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
617 }
618 if (!data.WriteString(bundleName)) {
619 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send bundleName").GetCode();
620 }
621 if (!data.WriteUint32(timeout)) {
622 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send timeout").GetCode();
623 }
624 MessageParcel reply;
625 MessageOption option;
626 option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
627 int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_UPDATE_TIMER),
628 data, reply, option);
629 if (ret != NO_ERROR) {
630 string str = "Failed to send out the request because of " + to_string(ret);
631 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
632 }
633 reply.ReadBool(result);
634 HILOGI("ServiceProxy UpdateTimer end. result = %d", result);
635 return BError(BError::Codes::OK, "success");
636 }
637
UpdateSendRate(std::string & bundleName,int32_t sendRate,bool & result)638 ErrCode ServiceProxy::UpdateSendRate(std::string &bundleName, int32_t sendRate, bool &result)
639 {
640 HILOGD("ServiceProxy UpdateSendRate Begin.");
641 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
642 MessageParcel data;
643 if (!data.WriteInterfaceToken(GetDescriptor())) {
644 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
645 }
646 if (!data.WriteString(bundleName)) {
647 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send bundleName").GetCode();
648 }
649 if (!data.WriteInt32(sendRate)) {
650 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send sendRate").GetCode();
651 }
652 MessageParcel reply;
653 MessageOption option;
654 option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
655 int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_UPDATE_SENDRATE),
656 data, reply, option);
657 if (ret != NO_ERROR) {
658 string str = "Failed to send out the request because of " + to_string(ret);
659 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
660 }
661 reply.ReadBool(result);
662 HILOGI("ServiceProxy UpdateSendRate end. ret = %{public}d", ret);
663 return BError(BError::Codes::OK, "success");
664 }
665
ReportAppProcessInfo(const std::string processInfo,const BackupRestoreScenario sennario)666 ErrCode ServiceProxy::ReportAppProcessInfo(const std::string processInfo, const BackupRestoreScenario sennario)
667 {
668 HILOGD("ServiceProxy NotifyBundleProcessInfo Begin.");
669 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
670 MessageParcel data;
671 if (!data.WriteInterfaceToken(GetDescriptor())) {
672 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
673 }
674 if (!data.WriteString(processInfo)) {
675 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send bundleName").GetCode();
676 }
677 if (!data.WriteInt32(static_cast<int32_t>(sennario))) {
678 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the scenario").GetCode();
679 }
680 MessageParcel reply;
681 MessageOption option;
682 option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
683 int32_t ret = Remote()-> SendRequest(static_cast<uint32_t>(
684 IServiceInterfaceCode::SERVICE_CMD_REPORT_APP_PROCESS_INFO), data, reply, option);
685 if (ret != NO_ERROR) {
686 string str = "Failed to send out the request because of " + to_string(ret);
687 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
688 }
689 HILOGI("ServiceProxy NotifyBundleProcessInfo end. ret = %{public}d", ret);
690 return BError(BError::Codes::OK, "success");
691 }
692
StartExtTimer(bool & isExtStart)693 ErrCode ServiceProxy::StartExtTimer(bool &isExtStart)
694 {
695 HILOGI("ServiceProxy StartExtTimer Begin.");
696 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
697 MessageParcel data;
698 if (!data.WriteInterfaceToken(GetDescriptor())) {
699 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
700 }
701 MessageParcel reply;
702 MessageOption option;
703 option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
704 int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_START_EXT_TIMER),
705 data, reply, option);
706 if (ret != NO_ERROR) {
707 string str = "Failed to send out the request because of " + to_string(ret);
708 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
709 }
710 reply.ReadBool(isExtStart);
711 HILOGI("ServiceProxy StartExtTimer end. isExtStart = %d", isExtStart);
712 return BError(BError::Codes::OK, "success");
713 }
714
StartFwkTimer(bool & isFwkStart)715 ErrCode ServiceProxy::StartFwkTimer(bool &isFwkStart)
716 {
717 HILOGI("ServiceProxy StartFwkTimer Begin.");
718 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
719 MessageParcel data;
720 if (!data.WriteInterfaceToken(GetDescriptor())) {
721 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
722 }
723 MessageParcel reply;
724 MessageOption option;
725 option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
726 int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_START_FWK_TIMER),
727 data, reply, option);
728 if (ret != NO_ERROR) {
729 string str = "Failed to send out the request because of " + to_string(ret);
730 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
731 }
732 reply.ReadBool(isFwkStart);
733 HILOGI("ServiceProxy StartFwkTimer end. isFwkStart = %d", isFwkStart);
734 return BError(BError::Codes::OK, "success");
735 }
736
StopExtTimer(bool & isExtStop)737 ErrCode ServiceProxy::StopExtTimer(bool &isExtStop)
738 {
739 HILOGI("ServiceProxy StopExtTimer Begin.");
740 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
741 MessageParcel data;
742 if (!data.WriteInterfaceToken(GetDescriptor())) {
743 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
744 }
745 MessageParcel reply;
746 MessageOption option;
747 option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
748 int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_STOP_EXT_TIMER),
749 data, reply, option);
750 if (ret != NO_ERROR) {
751 string str = "Failed to send out the request because of " + to_string(ret);
752 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
753 }
754 reply.ReadBool(isExtStop);
755 HILOGI("ServiceProxy StopExtTimer end. isExtStop = %{public}d", isExtStop);
756 return BError(BError::Codes::OK, "success");
757 }
758
RefreshDataSize(int64_t totalSize)759 ErrCode ServiceProxy::RefreshDataSize(int64_t totalSize)
760 {
761 HILOGI("ServiceProxy RefreshDatasize Begin.");
762 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
763 MessageParcel data;
764 if (!data.WriteInterfaceToken(GetDescriptor())) {
765 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
766 }
767 if (!data.WriteInt64(totalSize)) {
768 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write totalSize").GetCode();
769 }
770 MessageParcel reply;
771 MessageOption option;
772 option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
773 int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_REFRESH_DATA_SIZE),
774 data, reply, option);
775 if (ret != NO_ERROR) {
776 string str = "Failed to send out the request because of " + to_string(ret);
777 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
778 }
779 bool result = false;
780 reply.ReadBool(result);
781 if (!result) {
782 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to Refresh Datasize").GetCode();
783 }
784 return BError(BError::Codes::OK, "success");
785 }
786
GetLocalCapabilitiesForBundleInfos()787 UniqueFd ServiceProxy::GetLocalCapabilitiesForBundleInfos()
788 {
789 HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
790 HILOGI("ServiceProxy, start GetLocalCapabilitiesForBundleInfos");
791 if (Remote() == nullptr) {
792 HILOGE("Remote is nullptr");
793 return UniqueFd(-EPERM);
794 }
795 MessageParcel data;
796 if (!data.WriteInterfaceToken(GetDescriptor())) {
797 HILOGE("Failed to write descriptor");
798 return UniqueFd(-EPERM);
799 }
800
801 MessageParcel reply;
802 MessageOption option;
803 option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
804 int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(
805 IServiceInterfaceCode::SERVICE_CMD_GET_LOCAL_CAPABILITIES_FOR_BUNDLE_INFOS), data, reply, option);
806 if (ret != NO_ERROR) {
807 HILOGE("Received error %{public}d when doing IPC", ret);
808 }
809 UniqueFd fd(reply.ReadFileDescriptor());
810 HILOGI("ServiceProxy, end GetLocalCapabilitiesForBundleInfos");
811 return fd;
812 }
813
GetBackupDataSize(bool isPreciseScan,vector<BIncrementalData> bundleNameList)814 ErrCode ServiceProxy::GetBackupDataSize(bool isPreciseScan, vector<BIncrementalData> bundleNameList)
815 {
816 HILOGI("ServiceProxy GetBackupDataSize Begin.");
817 BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
818 MessageParcel data;
819 if (!data.WriteInterfaceToken(GetDescriptor())) {
820 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
821 }
822 if (!data.WriteBool(isPreciseScan)) {
823 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write isPreciseScan").GetCode();
824 }
825 if (!WriteParcelableVector(bundleNameList, data)) {
826 return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write bundleNameList").GetCode();
827 }
828 MessageParcel reply;
829 MessageOption option;
830 option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
831 int32_t ret = Remote()->SendRequest(
832 static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_BACKUP_DATA_SIZE), data, reply, option);
833 if (ret != NO_ERROR) {
834 string str = "Failed to send out the request because of " + to_string(ret);
835 return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
836 }
837 return BError(BError::Codes::OK, "success");
838 }
839 } // namespace OHOS::FileManagement::Backup
840