1 /*
2 * Copyright (c) 2023-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_async.h"
17
18 #include "b_error/b_error.h"
19 #include "b_radar/b_radar.h"
20 #include "b_resources/b_constants.h"
21 #include "b_session_restore.h"
22 #include "filemgmt_libhilog.h"
23 #include "service_proxy.h"
24 #include "service_reverse.h"
25
26 namespace OHOS::FileManagement::Backup {
27 using namespace std;
28
~BSessionRestoreAsync()29 BSessionRestoreAsync::~BSessionRestoreAsync()
30 {
31 if (!deathRecipient_) {
32 HILOGI("Death Recipient is nullptr");
33 return;
34 }
35 auto proxy = ServiceProxy::GetServiceProxyPointer();
36 if (proxy == nullptr) {
37 return;
38 }
39 auto remoteObject = proxy->AsObject();
40 if (remoteObject != nullptr) {
41 remoteObject->RemoveDeathRecipient(deathRecipient_);
42 }
43 callbacks_ = {};
44 deathRecipient_ = nullptr;
45 }
46
Init(Callbacks callbacks)47 shared_ptr<BSessionRestoreAsync> BSessionRestoreAsync::Init(Callbacks callbacks)
48 {
49 try {
50 auto restore = make_shared<BSessionRestoreAsync>(callbacks);
51 ServiceProxy::InvaildInstance();
52 auto proxy = ServiceProxy::GetInstance();
53 if (proxy == nullptr) {
54 HILOGI("Failed to get backup service");
55 return nullptr;
56 }
57 BSessionRestore::Callbacks callbacksTmp {.onFileReady = callbacks.onFileReady,
58 .onBundleStarted = callbacks.onBundleStarted,
59 .onBundleFinished = callbacks.onBundleFinished,
60 .onAllBundlesFinished = callbacks.onAllBundlesFinished,
61 .onResultReport = callbacks.onResultReport,
62 .onBackupServiceDied = callbacks.onBackupServiceDied,
63 .onProcess = callbacks.onProcess};
64 int32_t res = proxy->InitRestoreSession(sptr(new ServiceReverse(callbacksTmp)));
65 if (res != ERR_OK) {
66 HILOGE("Failed to Restore because of %{public}d", res);
67 AppRadar::Info info ("", "", "create restore session failed");
68 AppRadar::GetInstance().RecordRestoreFuncRes(info, "BSessionRestoreAsync::Init",
69 AppRadar::GetInstance().GetUserId(), BizStageRestore::BIZ_STAGE_CREATE_RESTORE_SESSION_FAIL, res);
70 return nullptr;
71 }
72
73 restore->RegisterBackupServiceDied(callbacks.onBackupServiceDied);
74 return restore;
75 } catch (const exception &e) {
76 HILOGE("Failed to Restore because of %{public}s", e.what());
77 }
78 return nullptr;
79 }
80
PublishFile(BFileInfo fileInfo)81 ErrCode BSessionRestoreAsync::PublishFile(BFileInfo fileInfo)
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->PublishFile(fileInfo);
88 }
89
GetFileHandle(const string & bundleName,const string & fileName)90 ErrCode BSessionRestoreAsync::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->GetFileHandle(bundleName, fileName);
98 }
99
AppendBundles(UniqueFd remoteCap,vector<BundleName> bundlesToRestore,std::vector<std::string> detailInfos,RestoreTypeEnum restoreType,int32_t userId)100 ErrCode BSessionRestoreAsync::AppendBundles(UniqueFd remoteCap,
101 vector<BundleName> bundlesToRestore,
102 std::vector<std::string> detailInfos,
103 RestoreTypeEnum restoreType,
104 int32_t userId)
105 {
106 auto proxy = ServiceProxy::GetInstance();
107 if (proxy == nullptr) {
108 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
109 }
110 ErrCode res = proxy->AppendBundlesRestoreSession(move(remoteCap), bundlesToRestore, detailInfos, restoreType,
111 userId);
112 if (res != ERR_OK) {
113 std::string ss;
114 for (const auto &bundle : bundlesToRestore) {
115 ss += bundle + ", ";
116 }
117 AppRadar::Info info(ss.c_str(), "", "AppendBundles with infos");
118 AppRadar::GetInstance().RecordRestoreFuncRes(info, "BSessionRestoreAsync::AppendBundles",
119 AppRadar::GetInstance().GetUserId(), BizStageRestore::BIZ_STAGE_APPEND_BUNDLES_FAIL, res);
120 }
121 return res;
122 }
123
AppendBundles(UniqueFd remoteCap,vector<BundleName> bundlesToRestore,RestoreTypeEnum restoreType,int32_t userId)124 ErrCode BSessionRestoreAsync::AppendBundles(UniqueFd remoteCap,
125 vector<BundleName> bundlesToRestore,
126 RestoreTypeEnum restoreType,
127 int32_t userId)
128 {
129 auto proxy = ServiceProxy::GetInstance();
130 if (proxy == nullptr) {
131 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
132 }
133
134 ErrCode res = proxy->AppendBundlesRestoreSession(move(remoteCap), bundlesToRestore, restoreType, userId);
135 if (res != ERR_OK) {
136 std::string ss;
137 for (const auto &bundle : bundlesToRestore) {
138 ss += bundle + ", ";
139 }
140 AppRadar::Info info(ss.c_str(), "", "");
141 AppRadar::GetInstance().RecordRestoreFuncRes(info, "BSessionRestoreAsync::AppendBundles",
142 AppRadar::GetInstance().GetUserId(), BizStageRestore::BIZ_STAGE_APPEND_BUNDLES_FAIL, res);
143 }
144 return res;
145 }
146
Release()147 ErrCode BSessionRestoreAsync::Release()
148 {
149 auto proxy = ServiceProxy::GetInstance();
150 if (proxy == nullptr) {
151 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
152 }
153
154 return proxy->Release();
155 }
156
RegisterBackupServiceDied(std::function<void ()> functor)157 void BSessionRestoreAsync::RegisterBackupServiceDied(std::function<void()> functor)
158 {
159 auto proxy = ServiceProxy::GetInstance();
160 if (proxy == nullptr || !functor) {
161 return;
162 }
163 auto remoteObj = proxy->AsObject();
164 if (!remoteObj) {
165 throw BError(BError::Codes::SA_BROKEN_IPC, "Proxy's remote object can't be nullptr");
166 }
167
168 auto callback = [functor](const wptr<IRemoteObject> &obj) { functor(); };
169 deathRecipient_ = sptr(new SvcDeathRecipient(callback));
170 remoteObj->AddDeathRecipient(deathRecipient_);
171 }
172 } // namespace OHOS::FileManagement::Backup