1 /*
2 * Copyright (c) 2022-2023 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 /*
17 * 注意:
18 * - 注意点1:本文件原则上只处理与IPC无关的业务逻辑
19 * - 注意点2:This document, in principle, captures all exceptions.
20 * Prevent exceptions from spreading to insecure modules.
21 */
22 #include "module_ipc/service.h"
23
24 #include <algorithm>
25 #include <cerrno>
26 #include <cstddef>
27 #include <cstdint>
28 #include <cstring>
29 #include <regex>
30
31 #include <fcntl.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <sys/vfs.h>
35
36 #include <directory_ex.h>
37
38 #include "ability_manager_client.h"
39 #include "accesstoken_kit.h"
40 #include "b_error/b_error.h"
41 #include "b_error/b_excep_utils.h"
42 #include "b_file_info.h"
43 #include "b_json/b_json_cached_entity.h"
44 #include "b_json/b_json_entity_caps.h"
45 #include "b_process/b_multiuser.h"
46 #include "b_resources/b_constants.h"
47 #include "bundle_mgr_client.h"
48 #include "filemgmt_libhilog.h"
49 #include "ipc_skeleton.h"
50 #include "module_external/bms_adapter.h"
51 #include "module_ipc/svc_backup_connection.h"
52 #include "parameter.h"
53 #include "system_ability_definition.h"
54
55 namespace OHOS::FileManagement::Backup {
56 using namespace std;
57
58 REGISTER_SYSTEM_ABILITY_BY_ID(Service, FILEMANAGEMENT_BACKUP_SERVICE_SA_ID, false);
59
60 /* Shell/Xts user id equal to 0/1, we need set default 100 */
GetUserIdDefault()61 static inline int32_t GetUserIdDefault()
62 {
63 auto multiuser = BMultiuser::ParseUid(IPCSkeleton::GetCallingUid());
64 if ((multiuser.userId == BConstants::SYSTEM_UID) || (multiuser.userId == BConstants::XTS_UID)) {
65 return BConstants::DEFAULT_USER_ID;
66 }
67 return multiuser.userId;
68 }
69
OnStart()70 void Service::OnStart()
71 {
72 bool res = SystemAbility::Publish(sptr(this));
73 sched_ = sptr(new SchedScheduler(wptr(this), wptr(session_)));
74 sched_->StartTimer();
75 HILOGI("End, res = %{public}d", res);
76 }
77
OnStop()78 void Service::OnStop()
79 {
80 HILOGI("Called");
81 sched_ = nullptr;
82 session_ = nullptr;
83 }
84
GetLocalCapabilities()85 UniqueFd Service::GetLocalCapabilities()
86 {
87 try {
88 HILOGI("Begin");
89 /*
90 Only called by restore app before InitBackupSession,
91 so there must be set init userId.
92 */
93 session_->SetSessionUserId(GetUserIdDefault());
94 VerifyCaller();
95 string path = BConstants::GetSaBundleBackupRootDir(session_->GetSessionUserId());
96 BExcepUltils::VerifyPath(path, false);
97 BJsonCachedEntity<BJsonEntityCaps> cachedEntity(
98 UniqueFd(open(path.data(), O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR)));
99
100 auto cache = cachedEntity.Structuralize();
101
102 cache.SetSystemFullName(GetOSFullName());
103 cache.SetDeviceType(GetDeviceType());
104 auto bundleInfos = BundleMgrAdapter::GetBundleInfos(session_->GetSessionUserId());
105 cache.SetBundleInfos(bundleInfos);
106 cachedEntity.Persist();
107
108 return move(cachedEntity.GetFd());
109 } catch (const BError &e) {
110 return UniqueFd(-e.GetCode());
111 } catch (const exception &e) {
112 HILOGI("Catched an unexpected low-level exception %{public}s", e.what());
113 return UniqueFd(-EPERM);
114 } catch (...) {
115 HILOGI("Unexpected exception");
116 return UniqueFd(-EPERM);
117 }
118 }
119
StopAll(const wptr<IRemoteObject> & obj,bool force)120 void Service::StopAll(const wptr<IRemoteObject> &obj, bool force)
121 {
122 session_->Deactive(obj, force);
123 }
124
VerifyCallerAndGetCallerName()125 string Service::VerifyCallerAndGetCallerName()
126 {
127 uint32_t tokenCaller = IPCSkeleton::GetCallingTokenID();
128 int tokenType = Security::AccessToken::AccessTokenKit::GetTokenType(tokenCaller);
129 if (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_HAP) {
130 Security::AccessToken::HapTokenInfo hapTokenInfo;
131 if (Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenCaller, hapTokenInfo) != 0) {
132 throw BError(BError::Codes::SA_INVAL_ARG, "Get hap token info failed");
133 }
134 session_->VerifyBundleName(hapTokenInfo.bundleName);
135 return hapTokenInfo.bundleName;
136 } else {
137 throw BError(BError::Codes::SA_INVAL_ARG, string("Invalid token type ").append(to_string(tokenType)));
138 }
139 }
140
VerifyCaller()141 void Service::VerifyCaller()
142 {
143 uint32_t tokenCaller = IPCSkeleton::GetCallingTokenID();
144 int tokenType = Security::AccessToken::AccessTokenKit::GetTokenType(tokenCaller);
145 switch (tokenType) {
146 case Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE: /* Update Service */
147 case Security::AccessToken::ATokenTypeEnum::TOKEN_HAP: {
148 const string permission = "ohos.permission.BACKUP";
149 if (Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, permission) ==
150 Security::AccessToken::TypePermissionState::PERMISSION_DENIED) {
151 throw BError(BError::Codes::SA_INVAL_ARG,
152 string("Permission denied, token type is ").append(to_string(tokenType)));
153 }
154 break;
155 }
156 case Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL:
157 if (IPCSkeleton::GetCallingUid() != BConstants::SYSTEM_UID) {
158 throw BError(BError::Codes::SA_INVAL_ARG, "Calling uid is invalid");
159 }
160 break;
161 default:
162 throw BError(BError::Codes::SA_INVAL_ARG, string("Invalid token type ").append(to_string(tokenType)));
163 break;
164 }
165 }
166
VerifyCaller(IServiceReverse::Scenario scenario)167 void Service::VerifyCaller(IServiceReverse::Scenario scenario)
168 {
169 session_->VerifyCallerAndScenario(IPCSkeleton::GetCallingTokenID(), scenario);
170 VerifyCaller();
171 }
172
InitRestoreSession(sptr<IServiceReverse> remote)173 ErrCode Service::InitRestoreSession(sptr<IServiceReverse> remote)
174 {
175 try {
176 VerifyCaller();
177 session_->Active({
178 .clientToken = IPCSkeleton::GetCallingTokenID(),
179 .scenario = IServiceReverse::Scenario::RESTORE,
180 .clientProxy = remote,
181 .userId = GetUserIdDefault(),
182 });
183 return BError(BError::Codes::OK);
184 } catch (const BError &e) {
185 StopAll(nullptr, true);
186 return e.GetCode();
187 } catch (const exception &e) {
188 HILOGI("Catched an unexpected low-level exception %{public}s", e.what());
189 return EPERM;
190 } catch (...) {
191 HILOGI("Unexpected exception");
192 return EPERM;
193 }
194 }
195
InitBackupSession(sptr<IServiceReverse> remote)196 ErrCode Service::InitBackupSession(sptr<IServiceReverse> remote)
197 {
198 try {
199 VerifyCaller();
200 session_->Active({
201 .clientToken = IPCSkeleton::GetCallingTokenID(),
202 .scenario = IServiceReverse::Scenario::BACKUP,
203 .clientProxy = remote,
204 .userId = GetUserIdDefault(),
205 });
206 return BError(BError::Codes::OK);
207 } catch (const BError &e) {
208 StopAll(nullptr, true);
209 return e.GetCode();
210 }
211 }
212
Start()213 ErrCode Service::Start()
214 {
215 HILOGI("Begin");
216 VerifyCaller(session_->GetScenario());
217 session_->Start();
218 OnStartSched();
219 return BError(BError::Codes::OK);
220 }
221
AppendBundlesRestoreSession(UniqueFd fd,const vector<BundleName> & bundleNames,RestoreTypeEnum restoreType,int32_t userId)222 ErrCode Service::AppendBundlesRestoreSession(UniqueFd fd,
223 const vector<BundleName> &bundleNames,
224 RestoreTypeEnum restoreType,
225 int32_t userId)
226 {
227 HILOGI("Begin");
228 if (userId != DEFAULT_INVAL_VALUE) { /* multi user scenario */
229 session_->SetSessionUserId(userId);
230 }
231 VerifyCaller(IServiceReverse::Scenario::RESTORE);
232 BJsonCachedEntity<BJsonEntityCaps> cachedEntity(move(fd));
233 auto cache = cachedEntity.Structuralize();
234 auto bundleInfos = cache.GetBundleInfos();
235 if (!bundleInfos.size()) {
236 throw BError(BError::Codes::SA_INVAL_ARG, "Json entity caps is empty");
237 }
238 session_->AppendBundles(bundleNames);
239 for (auto bundleName : bundleNames) {
240 for (auto &&bundleInfo : bundleInfos) {
241 if (bundleInfo.name != bundleName) {
242 continue;
243 }
244 session_->SetNeedToInstall(bundleInfo.name, bundleInfo.needToInstall);
245 session_->SetBundleRestoreType(bundleInfo.name, restoreType);
246 session_->SetBundleVersionCode(bundleInfo.name, bundleInfo.versionCode);
247 session_->SetBundleVersionName(bundleInfo.name, bundleInfo.versionName);
248 }
249 }
250 Start();
251 Finish();
252 OnStartSched();
253 return BError(BError::Codes::OK);
254 }
255
AppendBundlesBackupSession(const vector<BundleName> & bundleNames)256 ErrCode Service::AppendBundlesBackupSession(const vector<BundleName> &bundleNames)
257 {
258 HILOGI("Begin");
259 VerifyCaller(IServiceReverse::Scenario::BACKUP);
260 session_->AppendBundles(bundleNames);
261 Start();
262 Finish();
263 OnStartSched();
264 return BError(BError::Codes::OK);
265 }
266
Finish()267 ErrCode Service::Finish()
268 {
269 HILOGI("Begin");
270 VerifyCaller(session_->GetScenario());
271 session_->Finish();
272 OnAllBundlesFinished(BError(BError::Codes::OK));
273 return BError(BError::Codes::OK);
274 }
275
PublishFile(const BFileInfo & fileInfo)276 ErrCode Service::PublishFile(const BFileInfo &fileInfo)
277 {
278 try {
279 HILOGI("Begin");
280 VerifyCaller(IServiceReverse::Scenario::RESTORE);
281
282 if (fileInfo.fileName == BConstants::RESTORE_INSTALL_PATH) {
283 session_->SetInstallState(fileInfo.owner, "OK");
284 sched_->Sched(fileInfo.owner);
285 return BError(BError::Codes::OK);
286 }
287 if (!regex_match(fileInfo.fileName, regex("^[0-9a-zA-Z_.]+$"))) {
288 throw BError(BError::Codes::SA_INVAL_ARG, "Filename is not alphanumeric");
289 }
290
291 auto backUpConnection = session_->GetExtConnection(fileInfo.owner);
292
293 auto proxy = backUpConnection->GetBackupExtProxy();
294 if (!proxy) {
295 throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty");
296 }
297 ErrCode res = proxy->PublishFile(fileInfo.fileName);
298 if (res) {
299 HILOGE("Failed to publish file for backup extension");
300 }
301
302 return res;
303 } catch (const BError &e) {
304 return e.GetCode();
305 } catch (const exception &e) {
306 HILOGI("Catched an unexpected low-level exception %{public}s", e.what());
307 return EPERM;
308 } catch (...) {
309 HILOGI("Unexpected exception");
310 return EPERM;
311 }
312 }
313
AppFileReady(const string & fileName,UniqueFd fd)314 ErrCode Service::AppFileReady(const string &fileName, UniqueFd fd)
315 {
316 try {
317 HILOGI("Begin");
318 string callerName = VerifyCallerAndGetCallerName();
319 if (!regex_match(fileName, regex("^[0-9a-zA-Z_.]+$"))) {
320 throw BError(BError::Codes::SA_INVAL_ARG, "Filename is not alphanumeric");
321 }
322 if (fileName == BConstants::EXT_BACKUP_MANAGE) {
323 fd = session_->OnBunleExtManageInfo(callerName, move(fd));
324 }
325
326 session_->GetServiceReverseProxy()->BackupOnFileReady(callerName, fileName, move(fd));
327
328 if (session_->OnBunleFileReady(callerName, fileName)) {
329 auto backUpConnection = session_->GetExtConnection(callerName);
330 auto proxy = backUpConnection->GetBackupExtProxy();
331 if (!proxy) {
332 throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty");
333 }
334 // 通知extension清空缓存
335 proxy->HandleClear();
336 // 通知TOOL 备份完成
337 session_->GetServiceReverseProxy()->BackupOnBundleFinished(BError(BError::Codes::OK), callerName);
338 // 断开extension
339 backUpConnection->DisconnectBackupExtAbility();
340 ClearSessionAndSchedInfo(callerName);
341 }
342 OnAllBundlesFinished(BError(BError::Codes::OK));
343 return BError(BError::Codes::OK);
344 } catch (const BError &e) {
345 return e.GetCode(); // 任意异常产生,终止监听该任务
346 } catch (const exception &e) {
347 HILOGI("Catched an unexpected low-level exception %{public}s", e.what());
348 return EPERM;
349 } catch (...) {
350 HILOGI("Unexpected exception");
351 return EPERM;
352 }
353 }
354
AppDone(ErrCode errCode)355 ErrCode Service::AppDone(ErrCode errCode)
356 {
357 try {
358 HILOGI("Begin");
359 string callerName = VerifyCallerAndGetCallerName();
360 if (session_->OnBunleFileReady(callerName)) {
361 auto backUpConnection = session_->GetExtConnection(callerName);
362 auto proxy = backUpConnection->GetBackupExtProxy();
363 if (!proxy) {
364 throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty");
365 }
366 proxy->HandleClear();
367 IServiceReverse::Scenario scenario = session_->GetScenario();
368 if (scenario == IServiceReverse::Scenario::BACKUP) {
369 session_->GetServiceReverseProxy()->BackupOnBundleFinished(errCode, callerName);
370 } else if (scenario == IServiceReverse::Scenario::RESTORE) {
371 session_->GetServiceReverseProxy()->RestoreOnBundleFinished(errCode, callerName);
372 }
373 backUpConnection->DisconnectBackupExtAbility();
374 ClearSessionAndSchedInfo(callerName);
375 }
376 OnAllBundlesFinished(BError(BError::Codes::OK));
377 return BError(BError::Codes::OK);
378 } catch (const BError &e) {
379 return e.GetCode(); // 任意异常产生,终止监听该任务
380 } catch (const exception &e) {
381 HILOGI("Catched an unexpected low-level exception %{public}s", e.what());
382 return EPERM;
383 } catch (...) {
384 HILOGI("Unexpected exception");
385 return EPERM;
386 }
387 }
388
LaunchBackupExtension(const BundleName & bundleName)389 ErrCode Service::LaunchBackupExtension(const BundleName &bundleName)
390 {
391 try {
392 HILOGE("begin %{public}s", bundleName.data());
393 IServiceReverse::Scenario scenario = session_->GetScenario();
394 BConstants::ExtensionAction action;
395 if (scenario == IServiceReverse::Scenario::BACKUP) {
396 action = BConstants::ExtensionAction::BACKUP;
397 } else if (scenario == IServiceReverse::Scenario::RESTORE) {
398 action = BConstants::ExtensionAction::RESTORE;
399 } else {
400 throw BError(BError::Codes::SA_INVAL_ARG, "Failed to scenario");
401 }
402
403 AAFwk::Want want;
404 string backupExtName = session_->GetBackupExtName(bundleName); /* new device app ext name */
405 string versionName = session_->GetBundleVersionName(bundleName); /* old device app version name */
406 uint32_t versionCode = session_->GetBundleVersionCode(bundleName); /* old device app version code */
407 RestoreTypeEnum restoreType = session_->GetBundleRestoreType(bundleName); /* app restore type */
408
409 want.SetElementName(bundleName, backupExtName);
410 want.SetParam(BConstants::EXTENSION_ACTION_PARA, static_cast<int>(action));
411 want.SetParam(BConstants::EXTENSION_VERSION_CODE_PARA, static_cast<int>(versionCode));
412 want.SetParam(BConstants::EXTENSION_RESTORE_TYPE_PARA, static_cast<int>(restoreType));
413 want.SetParam(BConstants::EXTENSION_VERSION_NAME_PARA, versionName);
414
415 auto backUpConnection = session_->GetExtConnection(bundleName);
416
417 ErrCode ret = backUpConnection->ConnectBackupExtAbility(want, session_->GetSessionUserId());
418 return ret;
419 } catch (const BError &e) {
420 return e.GetCode();
421 } catch (const exception &e) {
422 HILOGI("Catched an unexpected low-level exception %{public}s", e.what());
423 return EPERM;
424 } catch (...) {
425 HILOGI("Unexpected exception");
426 return EPERM;
427 }
428 }
429
GetFileHandle(const string & bundleName,const string & fileName)430 ErrCode Service::GetFileHandle(const string &bundleName, const string &fileName)
431 {
432 try {
433 HILOGI("Begin");
434 VerifyCaller(IServiceReverse::Scenario::RESTORE);
435 if (fileName == BConstants::RESTORE_INSTALL_PATH && regex_match(bundleName, regex("^[0-9a-zA-Z_.]+$"))) {
436 session_->SetInstallState(bundleName, string(BConstants::RESTORE_INSTALL_PATH));
437 auto action = session_->GetServiceSchedAction(bundleName);
438 if (action == BConstants::ServiceSchedAction::INSTALLING) {
439 sched_->Sched(bundleName);
440 }
441 return BError(BError::Codes::OK);
442 }
443 if (!regex_match(fileName, regex("^[0-9a-zA-Z_.]+$"))) {
444 throw BError(BError::Codes::SA_INVAL_ARG, "Filename is not alphanumeric");
445 }
446 session_->SetExtFileNameRequest(bundleName, fileName);
447 auto action = session_->GetServiceSchedAction(bundleName);
448 if (action == BConstants::ServiceSchedAction::RUNNING) {
449 sched_->Sched(bundleName);
450 }
451 return BError(BError::Codes::OK);
452 } catch (const BError &e) {
453 return e.GetCode();
454 } catch (const exception &e) {
455 HILOGI("Catched an unexpected low-level exception %{public}s", e.what());
456 return EPERM;
457 } catch (...) {
458 HILOGI("Unexpected exception");
459 return EPERM;
460 }
461 }
462
OnBackupExtensionDied(const string && bundleName,ErrCode ret)463 void Service::OnBackupExtensionDied(const string &&bundleName, ErrCode ret)
464 {
465 try {
466 HILOGI("extension died. Died bundleName = %{public}s", bundleName.data());
467 string callName = move(bundleName);
468 session_->VerifyBundleName(callName);
469 auto scenario = session_->GetScenario();
470 if (scenario == IServiceReverse::Scenario::BACKUP) {
471 session_->GetServiceReverseProxy()->BackupOnBundleFinished(ret, callName);
472 } else if (scenario == IServiceReverse::Scenario::RESTORE) {
473 session_->GetServiceReverseProxy()->RestoreOnBundleFinished(ret, callName);
474 }
475 auto backUpConnection = session_->GetExtConnection(callName);
476 if (backUpConnection->IsExtAbilityConnected()) {
477 backUpConnection->DisconnectBackupExtAbility();
478 }
479 ClearSessionAndSchedInfo(bundleName);
480 } catch (const BError &e) {
481 return;
482 } catch (const exception &e) {
483 HILOGI("Catched an unexpected low-level exception %{public}s", e.what());
484 return;
485 } catch (...) {
486 HILOGI("Unexpected exception");
487 return;
488 }
489 }
490
ExtStart(const string & bundleName)491 void Service::ExtStart(const string &bundleName)
492 {
493 try {
494 HILOGE("begin %{public}s", bundleName.data());
495 IServiceReverse::Scenario scenario = session_->GetScenario();
496 auto backUpConnection = session_->GetExtConnection(bundleName);
497 auto proxy = backUpConnection->GetBackupExtProxy();
498 if (!proxy) {
499 throw BError(BError::Codes::SA_INVAL_ARG, "Extension backup Proxy is empty");
500 }
501 if (session_->GetBundleVersionCode(bundleName) != BConstants::DEFAULT_VERSION_CODE &&
502 session_->GetBundleVersionName(bundleName) != BConstants::DEFAULT_VERSION_NAME &&
503 session_->GetBundleRestoreType(bundleName) != RestoreTypeEnum::RESTORE_DATA_READDY) {
504 proxy->HandleClear();
505 }
506 if (scenario == IServiceReverse::Scenario::BACKUP) {
507 auto ret = proxy->HandleBackup();
508 session_->GetServiceReverseProxy()->BackupOnBundleStarted(ret, bundleName);
509 if (ret) {
510 ClearSessionAndSchedInfo(bundleName);
511 }
512 return;
513 }
514 if (scenario != IServiceReverse::Scenario::RESTORE) {
515 throw BError(BError::Codes::SA_INVAL_ARG, "Failed to scenario");
516 }
517 session_->GetServiceReverseProxy()->RestoreOnBundleStarted(BError(BError::Codes::OK), bundleName);
518 auto fileNameVec = session_->GetExtFileNameRequest(bundleName);
519 for (auto &fileName : fileNameVec) {
520 UniqueFd fd = proxy->GetFileHandle(fileName);
521 if (fd < 0) {
522 HILOGE("Failed to extension file handle");
523 OnBackupExtensionDied(move(bundleName), fd);
524 return;
525 }
526 session_->GetServiceReverseProxy()->RestoreOnFileReady(bundleName, fileName, move(fd));
527 }
528 return;
529 } catch (const BError &e) {
530 return;
531 } catch (const exception &e) {
532 HILOGI("Catched an unexpected low-level exception %{public}s", e.what());
533 return;
534 } catch (...) {
535 HILOGI("Unexpected exception");
536 return;
537 }
538 }
539
Dump(int fd,const vector<u16string> & args)540 int Service::Dump(int fd, const vector<u16string> &args)
541 {
542 if (fd < 0) {
543 HILOGI("HiDumper handle invalid");
544 return -1;
545 }
546
547 session_->DumpInfo(fd, args);
548 return 0;
549 }
550
ExtConnectFailed(const string & bundleName,ErrCode ret)551 void Service::ExtConnectFailed(const string &bundleName, ErrCode ret)
552 {
553 try {
554 HILOGE("begin %{public}s", bundleName.data());
555 IServiceReverse::Scenario scenario = session_->GetScenario();
556 if (scenario == IServiceReverse::Scenario::BACKUP) {
557 session_->GetServiceReverseProxy()->BackupOnBundleStarted(ret, bundleName);
558 } else if (scenario == IServiceReverse::Scenario::RESTORE) {
559 session_->GetServiceReverseProxy()->RestoreOnBundleStarted(ret, bundleName);
560 }
561 ClearSessionAndSchedInfo(bundleName);
562 return;
563 } catch (const BError &e) {
564 return;
565 } catch (const exception &e) {
566 HILOGI("Catched an unexpected low-level exception %{public}s", e.what());
567 return;
568 } catch (...) {
569 HILOGI("Unexpected exception");
570 return;
571 }
572 }
573
ExtConnectDone(string bundleName)574 void Service::ExtConnectDone(string bundleName)
575 {
576 try {
577 HILOGE("begin %{public}s", bundleName.data());
578 session_->SetServiceSchedAction(bundleName, BConstants::ServiceSchedAction::RUNNING);
579 sched_->Sched(bundleName);
580 } catch (const BError &e) {
581 return;
582 } catch (const exception &e) {
583 HILOGI("Catched an unexpected low-level exception %{public}s", e.what());
584 return;
585 } catch (...) {
586 HILOGI("Unexpected exception");
587 return;
588 }
589 }
590
ClearSessionAndSchedInfo(const string & bundleName)591 void Service::ClearSessionAndSchedInfo(const string &bundleName)
592 {
593 try {
594 session_->RemoveExtInfo(bundleName);
595 sched_->RemoveExtConn(bundleName);
596 sched_->Sched();
597 } catch (const BError &e) {
598 return;
599 } catch (const exception &e) {
600 HILOGI("Catched an unexpected low-level exception %{public}s", e.what());
601 return;
602 } catch (...) {
603 HILOGI("Unexpected exception");
604 return;
605 }
606 }
607
OnAllBundlesFinished(ErrCode errCode)608 void Service::OnAllBundlesFinished(ErrCode errCode)
609 {
610 if (session_->IsOnAllBundlesFinished()) {
611 IServiceReverse::Scenario scenario = session_->GetScenario();
612 if (scenario == IServiceReverse::Scenario::BACKUP) {
613 session_->GetServiceReverseProxy()->BackupOnAllBundlesFinished(errCode);
614 } else if (scenario == IServiceReverse::Scenario::RESTORE) {
615 session_->GetServiceReverseProxy()->RestoreOnAllBundlesFinished(errCode);
616 }
617 sched_->TryUnloadServiceTimer(true);
618 }
619 }
620
OnStartSched()621 void Service::OnStartSched()
622 {
623 if (session_->IsOnOnStartSched()) {
624 for (int num = 0; num < BConstants::EXT_CONNECT_MAX_COUNT; num++) {
625 sched_->Sched();
626 }
627 }
628 }
629 } // namespace OHOS::FileManagement::Backup
630