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