1 /*
2 * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "b_session_restore.h"
17
18 #include "b_error/b_error.h"
19 #include "b_radar/b_radar.h"
20 #include "filemgmt_libhilog.h"
21 #include "service_proxy.h"
22 #include "service_reverse.h"
23
24 namespace OHOS::FileManagement::Backup {
25 using namespace std;
26
~BSessionRestore()27 BSessionRestore::~BSessionRestore()
28 {
29 if (!deathRecipient_) {
30 HILOGI("Death Recipient is nullptr");
31 return;
32 }
33 auto proxy = ServiceProxy::GetServiceProxyPointer();
34 if (proxy == nullptr) {
35 return;
36 }
37 auto remoteObject = proxy->AsObject();
38 if (remoteObject != nullptr) {
39 remoteObject->RemoveDeathRecipient(deathRecipient_);
40 }
41 deathRecipient_ = nullptr;
42 }
43
Init(Callbacks callbacks)44 unique_ptr<BSessionRestore> BSessionRestore::Init(Callbacks callbacks)
45 {
46 try {
47 HILOGI("Init RestoreSession Begin");
48 auto restore = make_unique<BSessionRestore>();
49 ServiceProxy::InvaildInstance();
50 auto proxy = ServiceProxy::GetInstance();
51 if (proxy == nullptr) {
52 HILOGI("Failed to get backup service");
53 return nullptr;
54 }
55 int32_t res = proxy->InitRestoreSession(new ServiceReverse(callbacks));
56 if (res != ERR_OK) {
57 HILOGE("Failed to Restore because of %{public}d", res);
58 AppRadar::Info info ("", "", "create restore session failed");
59 AppRadar::GetInstance().RecordRestoreFuncRes(info, "BSessionRestore::Init",
60 AppRadar::GetInstance().GetUserId(), BizStageRestore::BIZ_STAGE_CREATE_RESTORE_SESSION_FAIL, res);
61 return nullptr;
62 }
63
64 restore->RegisterBackupServiceDied(callbacks.onBackupServiceDied);
65 return restore;
66 } catch (const exception &e) {
67 HILOGE("Failed to Restore because of %{public}s", e.what());
68 }
69 return nullptr;
70 }
71
GetLocalCapabilities()72 UniqueFd BSessionRestore::GetLocalCapabilities()
73 {
74 HILOGI("GetLocalCapabilities begin");
75 auto proxy = ServiceProxy::GetInstance();
76 if (proxy == nullptr) {
77 HILOGE("Failed to get backup service");
78 return UniqueFd(-EPERM);
79 }
80 UniqueFd fd = proxy->GetLocalCapabilitiesForBundleInfos();
81 if (fd < 0) {
82 HILOGE("Failed to get local capabilities for bundleinfos");
83 return UniqueFd(-EPERM);
84 }
85 return fd;
86 }
87
PublishFile(BFileInfo fileInfo)88 ErrCode BSessionRestore::PublishFile(BFileInfo fileInfo)
89 {
90 auto proxy = ServiceProxy::GetInstance();
91 if (proxy == nullptr) {
92 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
93 }
94 return proxy->PublishFile(fileInfo);
95 }
96
Start()97 ErrCode BSessionRestore::Start()
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->Start();
105 }
106
GetFileHandle(const string & bundleName,const string & fileName)107 ErrCode BSessionRestore::GetFileHandle(const string &bundleName, const string &fileName)
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->GetFileHandle(bundleName, fileName);
115 }
116
AppendBundles(UniqueFd remoteCap,vector<BundleName> bundlesToRestore,std::vector<std::string> detailInfos)117 ErrCode BSessionRestore::AppendBundles(UniqueFd remoteCap, vector<BundleName> bundlesToRestore,
118 std::vector<std::string> detailInfos)
119 {
120 auto proxy = ServiceProxy::GetInstance();
121 if (proxy == nullptr) {
122 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
123 }
124 ErrCode res = proxy->AppendBundlesRestoreSession(move(remoteCap), bundlesToRestore, detailInfos);
125 if (res != ERR_OK) {
126 std::string ss;
127 for (const auto &bundle : bundlesToRestore) {
128 ss += bundle + ", ";
129 }
130 AppRadar::Info info(ss.c_str(), "", "AppendBundles with infos");
131 AppRadar::GetInstance().RecordRestoreFuncRes(info, "BSessionRestore::AppendBundles",
132 AppRadar::GetInstance().GetUserId(), BizStageRestore::BIZ_STAGE_APPEND_BUNDLES_FAIL, res);
133 }
134 return res;
135 }
136
AppendBundles(UniqueFd remoteCap,vector<BundleName> bundlesToRestore)137 ErrCode BSessionRestore::AppendBundles(UniqueFd remoteCap, vector<BundleName> bundlesToRestore)
138 {
139 auto proxy = ServiceProxy::GetInstance();
140 if (proxy == nullptr) {
141 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
142 }
143 ErrCode res = proxy->AppendBundlesRestoreSession(move(remoteCap), bundlesToRestore);
144 if (res != ERR_OK) {
145 std::string ss;
146 for (const auto &bundle : bundlesToRestore) {
147 ss += bundle + ", ";
148 }
149 AppRadar::Info info(ss.c_str(), "", "");
150 AppRadar::GetInstance().RecordRestoreFuncRes(info, "BSessionRestore::AppendBundles",
151 AppRadar::GetInstance().GetUserId(), BizStageRestore::BIZ_STAGE_APPEND_BUNDLES_FAIL, res);
152 }
153 return res;
154 }
155
Finish()156 ErrCode BSessionRestore::Finish()
157 {
158 auto proxy = ServiceProxy::GetInstance();
159 if (proxy == nullptr) {
160 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
161 }
162
163 return proxy->Finish();
164 }
165
Release()166 ErrCode BSessionRestore::Release()
167 {
168 auto proxy = ServiceProxy::GetInstance();
169 if (proxy == nullptr) {
170 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
171 }
172
173 return proxy->Release();
174 }
175
RegisterBackupServiceDied(std::function<void ()> functor)176 void BSessionRestore::RegisterBackupServiceDied(std::function<void()> functor)
177 {
178 auto proxy = ServiceProxy::GetInstance();
179 if (proxy == nullptr || !functor) {
180 return;
181 }
182 auto remoteObj = proxy->AsObject();
183 if (!remoteObj) {
184 throw BError(BError::Codes::SA_BROKEN_IPC, "Proxy's remote object can't be nullptr");
185 }
186
187 auto callback = [functor](const wptr<IRemoteObject> &obj) {
188 HILOGI("Backup service died");
189 ServiceProxy::InvaildInstance();
190 functor();
191 };
192 deathRecipient_ = sptr(new SvcDeathRecipient(callback));
193 remoteObj->AddDeathRecipient(deathRecipient_);
194 }
195
Cancel(std::string bundleName)196 ErrCode BSessionRestore::Cancel(std::string bundleName)
197 {
198 ErrCode result = BError::BackupErrorCode::E_CANCEL_UNSTARTED_TASK;
199 auto proxy = ServiceProxy::GetInstance();
200 if (proxy == nullptr) {
201 HILOGE("Called Cancel, failed to get proxy.");
202 return result;
203 }
204
205 ErrCode errCode = proxy->Cancel(bundleName, result);
206 if (errCode != 0) {
207 HILOGE("proxy->Cancel failed, errCode:%{public}d.", errCode);
208 return result;
209 }
210 return result;
211 }
212 } // namespace OHOS::FileManagement::Backup