1 /*
2 * Copyright (c) 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_incremental_restore_session.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
~BIncrementalRestoreSession()27 BIncrementalRestoreSession::~BIncrementalRestoreSession()
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<BIncrementalRestoreSession> BIncrementalRestoreSession::Init(Callbacks callbacks)
45 {
46 try {
47 HILOGI("Init IncrementalRestoreSession Begin");
48 auto restore = make_unique<BIncrementalRestoreSession>();
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(sptr(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, "BIncrementalRestoreSession::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 BIncrementalRestoreSession::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->PublishIncrementalFile(fileInfo);
79 }
80
PublishSAFile(BFileInfo fileInfo,UniqueFd fd)81 ErrCode BIncrementalRestoreSession::PublishSAFile(BFileInfo fileInfo, UniqueFd fd)
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 return proxy->PublishSAIncrementalFile(fileInfo, move(fd));
88 }
89
GetFileHandle(const string & bundleName,const string & fileName)90 ErrCode BIncrementalRestoreSession::GetFileHandle(const string &bundleName, const string &fileName)
91 {
92 auto proxy = ServiceProxy::GetInstance();
93 if (proxy == nullptr) {
94 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
95 }
96
97 return proxy->GetIncrementalFileHandle(bundleName, fileName);
98 }
99
AppendBundles(UniqueFd remoteCap,vector<BundleName> bundlesToRestore)100 ErrCode BIncrementalRestoreSession::AppendBundles(UniqueFd remoteCap, vector<BundleName> bundlesToRestore)
101 {
102 auto proxy = ServiceProxy::GetInstance();
103 if (proxy == nullptr) {
104 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
105 }
106 ErrCode res = proxy->AppendBundlesRestoreSession(move(remoteCap), bundlesToRestore);
107 if (res != ERR_OK) {
108 std::string ss;
109 for (const auto &bundle : bundlesToRestore) {
110 ss += bundle + ", ";
111 }
112 AppRadar::Info info(ss.c_str(), "", "");
113 AppRadar::GetInstance().RecordRestoreFuncRes(info, "BIncrementalRestoreSession::AppendBundles",
114 AppRadar::GetInstance().GetUserId(), BizStageRestore::BIZ_STAGE_APPEND_BUNDLES_FAIL, res);
115 }
116 return res;
117 }
118
AppendBundles(UniqueFd remoteCap,vector<BundleName> bundlesToRestore,std::vector<std::string> detailInfos)119 ErrCode BIncrementalRestoreSession::AppendBundles(UniqueFd remoteCap, vector<BundleName> bundlesToRestore,
120 std::vector<std::string> detailInfos)
121 {
122 auto proxy = ServiceProxy::GetInstance();
123 if (proxy == nullptr) {
124 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
125 }
126 ErrCode res = proxy->AppendBundlesRestoreSession(move(remoteCap), bundlesToRestore, detailInfos);
127 if (res != ERR_OK) {
128 std::string ss;
129 for (const auto &bundle : bundlesToRestore) {
130 ss += bundle + ", ";
131 }
132 AppRadar::Info info(ss.c_str(), "", "AppendBundles with infos");
133 AppRadar::GetInstance().RecordRestoreFuncRes(info, "BIncrementalRestoreSession::AppendBundles",
134 AppRadar::GetInstance().GetUserId(), BizStageRestore::BIZ_STAGE_APPEND_BUNDLES_FAIL, res);
135 }
136 return res;
137 }
138
Release()139 ErrCode BIncrementalRestoreSession::Release()
140 {
141 auto proxy = ServiceProxy::GetInstance();
142 if (proxy == nullptr) {
143 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
144 }
145
146 return proxy->Release();
147 }
148
RegisterBackupServiceDied(function<void ()> functor)149 void BIncrementalRestoreSession::RegisterBackupServiceDied(function<void()> functor)
150 {
151 auto proxy = ServiceProxy::GetInstance();
152 if (proxy == nullptr || !functor) {
153 return;
154 }
155 auto remoteObj = proxy->AsObject();
156 if (!remoteObj) {
157 throw BError(BError::Codes::SA_BROKEN_IPC, "Proxy's remote object can't be nullptr");
158 }
159
160 auto callback = [functor](const wptr<IRemoteObject> &obj) {
161 HILOGI("Backup service died");
162 ServiceProxy::InvaildInstance();
163 functor();
164 };
165 deathRecipient_ = sptr(new SvcDeathRecipient(callback));
166 remoteObj->AddDeathRecipient(deathRecipient_);
167 }
168 } // namespace OHOS::FileManagement::Backup