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 #include "b_session_backup.h"
17
18 #include "b_error/b_error.h"
19 #include "filemgmt_libhilog.h"
20 #include "service_proxy.h"
21 #include "service_reverse.h"
22
23 namespace OHOS::FileManagement::Backup {
24 using namespace std;
25
~BSessionBackup()26 BSessionBackup::~BSessionBackup()
27 {
28 if (!deathRecipient_) {
29 HILOGI("Death Recipient is nullptr");
30 return;
31 }
32 auto proxy = ServiceProxy::GetInstance();
33 if (proxy == nullptr) {
34 return;
35 }
36 auto remoteObject = proxy->AsObject();
37 if (remoteObject != nullptr) {
38 remoteObject->RemoveDeathRecipient(deathRecipient_);
39 }
40 deathRecipient_ = nullptr;
41 }
42
Init(Callbacks callbacks)43 unique_ptr<BSessionBackup> BSessionBackup::Init(Callbacks callbacks)
44 {
45 try {
46 auto backup = make_unique<BSessionBackup>();
47 auto proxy = ServiceProxy::GetInstance();
48 if (proxy == nullptr) {
49 HILOGI("Failed to get backup service");
50 return nullptr;
51 }
52
53 int32_t res = proxy->InitBackupSession(sptr(new ServiceReverse(callbacks)));
54 if (res != 0) {
55 HILOGE("Failed to Backup because of %{public}d", res);
56 return nullptr;
57 }
58
59 backup->RegisterBackupServiceDied(callbacks.onBackupServiceDied);
60 return backup;
61 } catch (const exception &e) {
62 HILOGE("Failed to Backup because of %{public}s", e.what());
63 }
64 return nullptr;
65 }
66
RegisterBackupServiceDied(std::function<void ()> functor)67 void BSessionBackup::RegisterBackupServiceDied(std::function<void()> functor)
68 {
69 auto proxy = ServiceProxy::GetInstance();
70 if (proxy == nullptr || !functor) {
71 return;
72 }
73 auto remoteObj = proxy->AsObject();
74 if (!remoteObj) {
75 throw BError(BError::Codes::SA_BROKEN_IPC, "Proxy's remote object can't be nullptr");
76 }
77
78 auto callback = [functor](const wptr<IRemoteObject> &obj) {
79 ServiceProxy::InvaildInstance();
80 HILOGI("Backup service died");
81 functor();
82 };
83 deathRecipient_ = sptr(new SvcDeathRecipient(callback));
84 remoteObj->AddDeathRecipient(deathRecipient_);
85 }
86
Start()87 ErrCode BSessionBackup::Start()
88 {
89 auto proxy = ServiceProxy::GetInstance();
90 if (proxy == nullptr) {
91 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
92 }
93
94 return proxy->Start();
95 }
96
AppendBundles(vector<BundleName> bundlesToBackup)97 ErrCode BSessionBackup::AppendBundles(vector<BundleName> bundlesToBackup)
98 {
99 auto proxy = ServiceProxy::GetInstance();
100 if (proxy == nullptr) {
101 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
102 }
103
104 return proxy->AppendBundlesBackupSession(bundlesToBackup);
105 }
106
Finish()107 ErrCode BSessionBackup::Finish()
108 {
109 auto proxy = ServiceProxy::GetInstance();
110 if (proxy == nullptr) {
111 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
112 }
113
114 return proxy->Finish();
115 }
116 } // namespace OHOS::FileManagement::Backup