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 
InitBackupSession(sptr<IServiceReverse> remote)59 ErrCode ServiceProxy::InitBackupSession(sptr<IServiceReverse> remote)
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(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_INIT_BACKUP_SESSION),
78                                         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     return reply.ReadInt32();
84 }
85 
Start()86 ErrCode ServiceProxy::Start()
87 {
88     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
89     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
90     MessageParcel data;
91     if (!data.WriteInterfaceToken(GetDescriptor())) {
92         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
93     }
94 
95     MessageParcel reply;
96     MessageOption option;
97     int32_t ret =
98         Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_START), data, reply, option);
99     if (ret != NO_ERROR) {
100         string str = "Failed to send out the request because of " + to_string(ret);
101         return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
102     }
103     return reply.ReadInt32();
104 }
105 
GetLocalCapabilities()106 UniqueFd ServiceProxy::GetLocalCapabilities()
107 {
108     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
109     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
110     MessageParcel data;
111     if (!data.WriteInterfaceToken(GetDescriptor())) {
112         HILOGE("Failed to write descriptor");
113         return UniqueFd(-EPERM);
114     }
115 
116     MessageParcel reply;
117     MessageOption option;
118     option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
119     int32_t ret = Remote()->SendRequest(
120         static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_LOCAL_CAPABILITIES), data, reply, option);
121     if (ret != NO_ERROR) {
122         HILOGE("Received error %{public}d when doing IPC", ret);
123         AppRadar::Info resInfo("", "", "");
124         AppRadar::GetInstance().RecordDefaultFuncRes(resInfo, "ServiceProxy::GetLocalCapabilities",
125             AppRadar::GetInstance().GetUserId(), BizStageBackup::BIZ_STAGE_GET_LOCAL_CAPABILITIES_FAIL, ret);
126         return UniqueFd(-ret);
127     }
128     UniqueFd fd(reply.ReadFileDescriptor());
129     return UniqueFd(fd.Release());
130 }
131 
PublishFile(const BFileInfo & fileInfo)132 ErrCode ServiceProxy::PublishFile(const BFileInfo &fileInfo)
133 {
134     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
135     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
136     MessageParcel data;
137     if (!data.WriteInterfaceToken(GetDescriptor())) {
138         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
139     }
140 
141     if (!data.WriteParcelable(&fileInfo)) {
142         HILOGE("Failed to send the fileInfo");
143         return -EPIPE;
144     }
145 
146     MessageParcel reply;
147     MessageOption option;
148     int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_PUBLISH_FILE), data,
149                                         reply, option);
150     if (ret != NO_ERROR) {
151         string str = "Failed to send out the request because of " + to_string(ret);
152         return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
153     }
154     return reply.ReadInt32();
155 }
156 
AppFileReady(const string & fileName,UniqueFd fd,int32_t errCode)157 ErrCode ServiceProxy::AppFileReady(const string &fileName, UniqueFd fd, int32_t errCode)
158 {
159     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
160     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
161     MessageParcel data;
162     if (!data.WriteInterfaceToken(GetDescriptor())) {
163         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
164     }
165 
166     if (!data.WriteString(fileName)) {
167         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the filename").GetCode();
168     }
169     bool fdFlag = fd < 0 ? false : true;
170     data.WriteBool(fdFlag);
171     if (fdFlag == true && !data.WriteFileDescriptor(fd)) {
172         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the fd").GetCode();
173     }
174     if (!data.WriteInt32(errCode)) {
175         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the errCode").GetCode();
176     }
177 
178     MessageParcel reply;
179     MessageOption option;
180     int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APP_FILE_READY), data,
181                                         reply, option);
182     if (ret != NO_ERROR) {
183         string str = "Failed to send out the request because of " + to_string(ret);
184         return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
185     }
186     return reply.ReadInt32();
187 }
188 
AppDone(ErrCode errCode)189 ErrCode ServiceProxy::AppDone(ErrCode errCode)
190 {
191     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
192     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
193     MessageParcel data;
194     if (!data.WriteInterfaceToken(GetDescriptor())) {
195         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
196     }
197 
198     if (!data.WriteInt32(errCode)) {
199         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the errCode").GetCode();
200     }
201 
202     MessageParcel reply;
203     MessageOption option;
204     int32_t ret =
205         Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APP_DONE), data, reply, option);
206     if (ret != NO_ERROR) {
207         string str = "Failed to send out the request because of " + to_string(ret);
208         return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
209     }
210     return reply.ReadInt32();
211 }
212 
ServiceResultReport(const std::string restoreRetInfo,BackupRestoreScenario scenario,ErrCode errCode)213 ErrCode ServiceProxy::ServiceResultReport(const std::string restoreRetInfo,
214     BackupRestoreScenario scenario, ErrCode errCode)
215 {
216     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
217     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
218     MessageParcel data;
219     if (!data.WriteInterfaceToken(GetDescriptor())) {
220         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
221     }
222     if (!data.WriteString(restoreRetInfo)) {
223         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the restoreRetInfo").GetCode();
224     }
225     if (!data.WriteInt32(static_cast<int32_t>(scenario))) {
226         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the scenario").GetCode();
227     }
228     if (!data.WriteInt32(errCode)) {
229         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the errCode").GetCode();
230     }
231     MessageParcel reply;
232     MessageOption option;
233     int32_t ret =
234         Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_RESULT_REPORT), data, reply,
235             option);
236     if (ret != NO_ERROR) {
237         string str = "Failed to send out the request because of " + to_string(ret);
238         return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
239     }
240     return reply.ReadInt32();
241 }
242 
GetFileHandle(const string & bundleName,const string & fileName)243 ErrCode ServiceProxy::GetFileHandle(const string &bundleName, const string &fileName)
244 {
245     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
246     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
247     MessageParcel data;
248     if (!data.WriteInterfaceToken(GetDescriptor())) {
249         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
250     }
251 
252     if (!data.WriteString(bundleName)) {
253         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the bundleName").GetCode();
254     }
255     if (!data.WriteString(fileName)) {
256         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the fileName").GetCode();
257     }
258 
259     MessageParcel reply;
260     MessageOption option;
261     option.SetFlags(MessageOption::TF_ASYNC);
262     int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_FILE_NAME), data,
263                                         reply, option);
264     if (ret != NO_ERROR) {
265         string str = "Failed to send out the request because of " + to_string(ret);
266         return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
267     }
268     return ret;
269 }
270 
AppendBundlesRestoreSession(UniqueFd fd,const vector<BundleName> & bundleNames,const std::vector<std::string> & detailInfos,RestoreTypeEnum restoreType,int32_t userId)271 ErrCode ServiceProxy::AppendBundlesRestoreSession(UniqueFd fd, const vector<BundleName> &bundleNames,
272     const std::vector<std::string> &detailInfos, RestoreTypeEnum restoreType, int32_t userId)
273 {
274     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
275     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
276     MessageParcel data;
277     if (!data.WriteInterfaceToken(GetDescriptor())) {
278         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
279     }
280     MessageParcel reply;
281     MessageOption option;
282     option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
283 
284     if (!data.WriteFileDescriptor(fd)) {
285         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the fd").GetCode();
286     }
287     if (!data.WriteStringVector(bundleNames)) {
288         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send bundleNames").GetCode();
289     }
290     if (!detailInfos.empty() && !data.WriteStringVector(detailInfos)) {
291         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send detailInfos").GetCode();
292     }
293     if (!data.WriteInt32(static_cast<int32_t>(restoreType))) {
294         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send restoreType").GetCode();
295     }
296     if (!data.WriteInt32(userId)) {
297         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send userId").GetCode();
298     }
299     int32_t ret = Remote()->SendRequest(
300         static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_RESTORE_SESSION_DETAIL), data, reply,
301         option);
302     if (ret != NO_ERROR) {
303         string str = "Failed to send out the request because of " + to_string(ret);
304         return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
305     }
306     return reply.ReadInt32();
307 }
308 
AppendBundlesRestoreSession(UniqueFd fd,const vector<BundleName> & bundleNames,RestoreTypeEnum restoreType,int32_t userId)309 ErrCode ServiceProxy::AppendBundlesRestoreSession(UniqueFd fd,
310                                                   const vector<BundleName> &bundleNames,
311                                                   RestoreTypeEnum restoreType,
312                                                   int32_t userId)
313 {
314     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
315     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
316     MessageParcel data;
317     if (!data.WriteInterfaceToken(GetDescriptor())) {
318         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
319     }
320     MessageParcel reply;
321     MessageOption option;
322     option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
323 
324     if (!data.WriteFileDescriptor(fd)) {
325         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the fd").GetCode();
326     }
327     if (!data.WriteStringVector(bundleNames)) {
328         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send bundleNames").GetCode();
329     }
330     if (!data.WriteInt32(static_cast<int32_t>(restoreType))) {
331         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send restoreType").GetCode();
332     }
333     if (!data.WriteInt32(userId)) {
334         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send userId").GetCode();
335     }
336 
337     int32_t ret = Remote()->SendRequest(
338         static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_RESTORE_SESSION), data, reply, option);
339     if (ret != NO_ERROR) {
340         string str = "Failed to send out the request because of " + to_string(ret);
341         return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
342     }
343     return reply.ReadInt32();
344 }
345 
AppendBundlesBackupSession(const vector<BundleName> & bundleNames)346 ErrCode ServiceProxy::AppendBundlesBackupSession(const vector<BundleName> &bundleNames)
347 {
348     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
349     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
350     MessageParcel data;
351     if (!data.WriteInterfaceToken(GetDescriptor())) {
352         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
353     }
354     MessageParcel reply;
355     MessageOption option;
356     option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
357 
358     if (!data.WriteStringVector(bundleNames)) {
359         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send bundleNames").GetCode();
360     }
361 
362     int32_t ret = Remote()->SendRequest(
363         static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_BACKUP_SESSION), data, reply, option);
364     if (ret != NO_ERROR) {
365         string str = "Failed to send out the request because of " + to_string(ret);
366         return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
367     }
368     return reply.ReadInt32();
369 }
370 
AppendBundlesDetailsBackupSession(const vector<BundleName> & bundleNames,const vector<std::string> & detailInfos)371 ErrCode ServiceProxy::AppendBundlesDetailsBackupSession(const vector<BundleName> &bundleNames,
372                                                         const vector<std::string> &detailInfos)
373 {
374     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
375     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
376     MessageParcel data;
377     if (!data.WriteInterfaceToken(GetDescriptor())) {
378         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
379     }
380     MessageParcel reply;
381     MessageOption option;
382     option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
383 
384     if (!data.WriteStringVector(bundleNames)) {
385         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send bundleNames").GetCode();
386     }
387 
388     if (!data.WriteStringVector(detailInfos)) {
389         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send detailInfos").GetCode();
390     }
391 
392     int32_t ret = Remote()->SendRequest(
393         static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_APPEND_BUNDLES_BACKUP_SESSION_DETAILS),
394         data, reply, option);
395     if (ret != NO_ERROR) {
396         string str = "Failed to send out the request because of " + to_string(ret);
397         return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
398     }
399     return reply.ReadInt32();
400 }
401 
Finish()402 ErrCode ServiceProxy::Finish()
403 {
404     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
405     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
406     MessageParcel data;
407     if (!data.WriteInterfaceToken(GetDescriptor())) {
408         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
409     }
410 
411     MessageParcel reply;
412     MessageOption option;
413     int32_t ret =
414         Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_FINISH), data, reply, option);
415     if (ret != NO_ERROR) {
416         string str = "Failed to send out the request because of " + to_string(ret);
417         return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
418     }
419     return reply.ReadInt32();
420 }
421 
GetServiceProxyPointer()422 sptr<IService> ServiceProxy::GetServiceProxyPointer()
423 {
424     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
425     return serviceProxy_;
426 }
427 
GetInstance()428 sptr<IService> ServiceProxy::GetInstance()
429 {
430     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
431     unique_lock<mutex> getInstanceLock(getInstanceMutex_);
432     unique_lock<mutex> lock(proxyMutex_);
433     if (serviceProxy_ != nullptr) {
434         return serviceProxy_;
435     }
436 
437     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
438     if (!samgr) {
439         HILOGE("Get an empty samgr");
440         return nullptr;
441     }
442     sptr<ServiceProxyLoadCallback> loadCallback = new ServiceProxyLoadCallback();
443     if (loadCallback == nullptr) {
444         HILOGE("loadCallback is nullptr.");
445         return nullptr;
446     }
447     int32_t ret = samgr->LoadSystemAbility(FILEMANAGEMENT_BACKUP_SERVICE_SA_ID, loadCallback);
448     if (ret != ERR_OK) {
449         HILOGE("Failed to Load systemAbility, systemAbilityId:%{private}d, ret code:%{public}d",
450                FILEMANAGEMENT_BACKUP_SERVICE_SA_ID, ret);
451         return nullptr;
452     }
453 
454     auto waitStatus =
455         loadCallback->proxyConVar_.wait_for(lock, std::chrono::milliseconds(BConstants::BACKUP_LOADSA_TIMEOUT_MS),
456                                             [loadCallback]() { return loadCallback->isLoadSuccess_.load(); });
457     if (!waitStatus) {
458         HILOGE("Load backup sa timeout");
459         AppRadar::Info info("", "", "\"reason\":\"Load backup sa timeout\"");
460         AppRadar::GetInstance().RecordBackupFuncRes(info, "ServiceProxy::GetInstance",
461             AppRadar::GetInstance().GetUserId(), BizStageBackup::BIZ_STAGE_BOOT_BACKUP_SA_FAIL,
462             BError(BError::Codes::SA_INVAL_ARG).GetCode());
463         return nullptr;
464     }
465     return serviceProxy_;
466 }
467 
InvaildInstance()468 void ServiceProxy::InvaildInstance()
469 {
470     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
471     HILOGD("invalid instance");
472     unique_lock<mutex> lock(proxyMutex_);
473     serviceProxy_ = nullptr;
474 }
475 
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const OHOS::sptr<IRemoteObject> & remoteObject)476 void ServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId,
477                                                                         const OHOS::sptr<IRemoteObject> &remoteObject)
478 {
479     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
480     HILOGI("Load backup sa success, systemAbilityId: %{private}d, remoteObject result:%{private}s", systemAbilityId,
481            (remoteObject != nullptr) ? "true" : "false");
482     if (systemAbilityId != FILEMANAGEMENT_BACKUP_SERVICE_SA_ID || remoteObject == nullptr) {
483         isLoadSuccess_.store(false);
484         proxyConVar_.notify_one();
485         return;
486     }
487     unique_lock<mutex> lock(proxyMutex_);
488     serviceProxy_ = iface_cast<IService>(remoteObject);
489     if (serviceProxy_ == nullptr) {
490         HILOGD("serviceProxy_ is nullptr");
491         return;
492     }
493     auto remoteObj = serviceProxy_->AsObject();
494     if (!remoteObj) {
495         HILOGE("Failed to get remote object");
496         serviceProxy_ = nullptr;
497         isLoadSuccess_.store(false);
498         proxyConVar_.notify_one();
499         return;
500     }
501 
502     auto callback = [](const wptr<IRemoteObject> &obj) {
503         ServiceProxy::InvaildInstance();
504         HILOGE("Backup service died");
505     };
506     sptr<SvcDeathRecipient> deathRecipient = sptr(new SvcDeathRecipient(callback));
507     remoteObj->AddDeathRecipient(deathRecipient);
508     isLoadSuccess_.store(true);
509     proxyConVar_.notify_one();
510 }
511 
OnLoadSystemAbilityFail(int32_t systemAbilityId)512 void ServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
513 {
514     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
515     HILOGE("Load backup sa failed, systemAbilityId:%{private}d", systemAbilityId);
516     unique_lock<mutex> lock(proxyMutex_);
517     serviceProxy_ = nullptr;
518     isLoadSuccess_.store(false);
519     AppRadar::Info info("", "", "\"reason\":\"Load backup sa fail\"");
520     AppRadar::GetInstance().RecordBackupFuncRes(info, "ServiceProxyLoadCallback::OnLoadSystemAbilityFail",
521         AppRadar::GetInstance().GetUserId(), BizStageBackup::BIZ_STAGE_BOOT_BACKUP_SA_FAIL,
522         static_cast<int32_t>(BError::Codes::SA_INVAL_ARG));
523     proxyConVar_.notify_one();
524 }
525 
GetBackupInfo(BundleName & bundleName,std::string & result)526 ErrCode ServiceProxy::GetBackupInfo(BundleName &bundleName, std::string &result)
527 {
528     HILOGI("ServiceProxy GetBackupInfo Begin.");
529     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
530     MessageParcel data;
531     if (!data.WriteInterfaceToken(GetDescriptor())) {
532         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
533     }
534     if (!data.WriteString(bundleName)) {
535         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send bundleName").GetCode();
536     }
537     MessageParcel reply;
538     MessageOption option;
539     option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
540     int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_GET_BACKUP_INFO),
541                                         data, reply, option);
542     if (ret != NO_ERROR) {
543         string str = "Failed to send out the request because of " + to_string(ret);
544         return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
545     }
546     reply.ReadString(result);
547     HILOGI("ServiceProxy GetBackupInfo end. result = %s", result.c_str());
548     return BError(BError::Codes::OK, "success");
549 }
550 
UpdateTimer(BundleName & bundleName,uint32_t timeout,bool & result)551 ErrCode ServiceProxy::UpdateTimer(BundleName &bundleName, uint32_t timeout, bool &result)
552 {
553     HILOGI("ServiceProxy UpdateTimer Begin.");
554     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
555     MessageParcel data;
556     if (!data.WriteInterfaceToken(GetDescriptor())) {
557         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
558     }
559     if (!data.WriteString(bundleName)) {
560         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send bundleName").GetCode();
561     }
562     if (!data.WriteUint32(timeout)) {
563         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send timeout").GetCode();
564     }
565     MessageParcel reply;
566     MessageOption option;
567     option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
568     int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_UPDATE_TIMER),
569                                         data, reply, option);
570     if (ret != NO_ERROR) {
571         string str = "Failed to send out the request because of " + to_string(ret);
572         return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
573     }
574     reply.ReadBool(result);
575     HILOGI("ServiceProxy UpdateTimer end. result = %d", result);
576     return BError(BError::Codes::OK, "success");
577 }
578 
UpdateSendRate(std::string & bundleName,int32_t sendRate,bool & result)579 ErrCode ServiceProxy::UpdateSendRate(std::string &bundleName, int32_t sendRate, bool &result)
580 {
581     HILOGD("ServiceProxy UpdateSendRate Begin.");
582     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
583     MessageParcel data;
584     if (!data.WriteInterfaceToken(GetDescriptor())) {
585         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
586     }
587     if (!data.WriteString(bundleName)) {
588         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send bundleName").GetCode();
589     }
590     if (!data.WriteInt32(sendRate)) {
591         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send sendRate").GetCode();
592     }
593     MessageParcel reply;
594     MessageOption option;
595     option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
596     int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_UPDATE_SENDRATE),
597                                         data, reply, option);
598     if (ret != NO_ERROR) {
599         string str = "Failed to send out the request because of " + to_string(ret);
600         return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
601     }
602     reply.ReadBool(result);
603     HILOGI("ServiceProxy UpdateSendRate end. ret = %{public}d", ret);
604     return BError(BError::Codes::OK, "success");
605 }
606 
ReportAppProcessInfo(const std::string processInfo,const BackupRestoreScenario sennario)607 ErrCode ServiceProxy::ReportAppProcessInfo(const std::string processInfo, const BackupRestoreScenario sennario)
608 {
609     HILOGD("ServiceProxy NotifyBundleProcessInfo Begin.");
610     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
611     MessageParcel data;
612     if (!data.WriteInterfaceToken(GetDescriptor())) {
613         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
614     }
615     if (!data.WriteString(processInfo)) {
616         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send bundleName").GetCode();
617     }
618     if (!data.WriteInt32(static_cast<int32_t>(sennario))) {
619         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to send the scenario").GetCode();
620     }
621     MessageParcel reply;
622     MessageOption option;
623     option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
624     int32_t ret = Remote()-> SendRequest(static_cast<uint32_t>(
625         IServiceInterfaceCode::SERVICE_CMD_REPORT_APP_PROCESS_INFO), data, reply, option);
626     if (ret != NO_ERROR) {
627         string str = "Failed to send out the request because of " + to_string(ret);
628         return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
629     }
630     HILOGI("ServiceProxy NotifyBundleProcessInfo end. ret = %{public}d", ret);
631     return BError(BError::Codes::OK, "success");
632 }
633 
StartExtTimer(bool & isExtStart)634 ErrCode ServiceProxy::StartExtTimer(bool &isExtStart)
635 {
636     HILOGI("ServiceProxy StartExtTimer Begin.");
637     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
638     MessageParcel data;
639     if (!data.WriteInterfaceToken(GetDescriptor())) {
640         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
641     }
642     MessageParcel reply;
643     MessageOption option;
644     option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
645     int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_START_EXT_TIMER),
646                                         data, reply, option);
647     if (ret != NO_ERROR) {
648         string str = "Failed to send out the request because of " + to_string(ret);
649         return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
650     }
651     reply.ReadBool(isExtStart);
652     HILOGI("ServiceProxy StartExtTimer end. isExtStart = %d", isExtStart);
653     return BError(BError::Codes::OK, "success");
654 }
655 
StartFwkTimer(bool & isFwkStart)656 ErrCode ServiceProxy::StartFwkTimer(bool &isFwkStart)
657 {
658     HILOGI("ServiceProxy StartFwkTimer Begin.");
659     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
660     MessageParcel data;
661     if (!data.WriteInterfaceToken(GetDescriptor())) {
662         return BError(BError::Codes::SDK_INVAL_ARG, "Failed to write descriptor").GetCode();
663     }
664     MessageParcel reply;
665     MessageOption option;
666     option.SetWaitTime(BConstants::IPC_MAX_WAIT_TIME);
667     int32_t ret = Remote()->SendRequest(static_cast<uint32_t>(IServiceInterfaceCode::SERVICE_CMD_START_FWK_TIMER),
668                                         data, reply, option);
669     if (ret != NO_ERROR) {
670         string str = "Failed to send out the request because of " + to_string(ret);
671         return BError(BError::Codes::SDK_INVAL_ARG, str.data()).GetCode();
672     }
673     reply.ReadBool(isFwkStart);
674     HILOGI("ServiceProxy StartFwkTimer end. isFwkStart = %d", isFwkStart);
675     return BError(BError::Codes::OK, "success");
676 }
677 } // namespace OHOS::FileManagement::Backup
678