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
PublishFile(BFileInfo fileInfo)72 ErrCode BSessionRestore::PublishFile(BFileInfo fileInfo)
73 {
74 auto proxy = ServiceProxy::GetInstance();
75 if (proxy == nullptr) {
76 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
77 }
78 return proxy->PublishFile(fileInfo);
79 }
80
Start()81 ErrCode BSessionRestore::Start()
82 {
83 auto proxy = ServiceProxy::GetInstance();
84 if (proxy == nullptr) {
85 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
86 }
87
88 return proxy->Start();
89 }
90
GetFileHandle(const string & bundleName,const string & fileName)91 ErrCode BSessionRestore::GetFileHandle(const string &bundleName, const string &fileName)
92 {
93 auto proxy = ServiceProxy::GetInstance();
94 if (proxy == nullptr) {
95 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
96 }
97
98 return proxy->GetFileHandle(bundleName, fileName);
99 }
100
AppendBundles(UniqueFd remoteCap,vector<BundleName> bundlesToRestore,std::vector<std::string> detailInfos)101 ErrCode BSessionRestore::AppendBundles(UniqueFd remoteCap, vector<BundleName> bundlesToRestore,
102 std::vector<std::string> detailInfos)
103 {
104 auto proxy = ServiceProxy::GetInstance();
105 if (proxy == nullptr) {
106 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
107 }
108 ErrCode res = proxy->AppendBundlesRestoreSession(move(remoteCap), bundlesToRestore, detailInfos);
109 if (res != ERR_OK) {
110 std::string ss;
111 for (const auto &bundle : bundlesToRestore) {
112 ss += bundle + ", ";
113 }
114 AppRadar::Info info(ss.c_str(), "", "AppendBundles with infos");
115 AppRadar::GetInstance().RecordRestoreFuncRes(info, "BSessionRestore::AppendBundles",
116 AppRadar::GetInstance().GetUserId(), BizStageRestore::BIZ_STAGE_APPEND_BUNDLES_FAIL, res);
117 }
118 return res;
119 }
120
AppendBundles(UniqueFd remoteCap,vector<BundleName> bundlesToRestore)121 ErrCode BSessionRestore::AppendBundles(UniqueFd remoteCap, vector<BundleName> bundlesToRestore)
122 {
123 auto proxy = ServiceProxy::GetInstance();
124 if (proxy == nullptr) {
125 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
126 }
127 ErrCode res = proxy->AppendBundlesRestoreSession(move(remoteCap), bundlesToRestore);
128 if (res != ERR_OK) {
129 std::string ss;
130 for (const auto &bundle : bundlesToRestore) {
131 ss += bundle + ", ";
132 }
133 AppRadar::Info info(ss.c_str(), "", "");
134 AppRadar::GetInstance().RecordRestoreFuncRes(info, "BSessionRestore::AppendBundles",
135 AppRadar::GetInstance().GetUserId(), BizStageRestore::BIZ_STAGE_APPEND_BUNDLES_FAIL, res);
136 }
137 return res;
138 }
139
Finish()140 ErrCode BSessionRestore::Finish()
141 {
142 auto proxy = ServiceProxy::GetInstance();
143 if (proxy == nullptr) {
144 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
145 }
146
147 return proxy->Finish();
148 }
149
Release()150 ErrCode BSessionRestore::Release()
151 {
152 auto proxy = ServiceProxy::GetInstance();
153 if (proxy == nullptr) {
154 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
155 }
156
157 return proxy->Release();
158 }
159
RegisterBackupServiceDied(std::function<void ()> functor)160 void BSessionRestore::RegisterBackupServiceDied(std::function<void()> functor)
161 {
162 auto proxy = ServiceProxy::GetInstance();
163 if (proxy == nullptr || !functor) {
164 return;
165 }
166 auto remoteObj = proxy->AsObject();
167 if (!remoteObj) {
168 throw BError(BError::Codes::SA_BROKEN_IPC, "Proxy's remote object can't be nullptr");
169 }
170
171 auto callback = [functor](const wptr<IRemoteObject> &obj) {
172 HILOGI("Backup service died");
173 ServiceProxy::InvaildInstance();
174 functor();
175 };
176 deathRecipient_ = sptr(new SvcDeathRecipient(callback));
177 remoteObj->AddDeathRecipient(deathRecipient_);
178 }
179 } // namespace OHOS::FileManagement::Backup