1 /*
2 * Copyright (c) 2023 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_resources/b_constants.h"
20 #include "b_session_restore.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
~BSessionRestoreAsync()28 BSessionRestoreAsync::~BSessionRestoreAsync()
29 {
30 if (!deathRecipient_) {
31 HILOGI("Death Recipient is nullptr");
32 return;
33 }
34 auto proxy = ServiceProxy::GetInstance();
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<BSessionRestoreAsync> BSessionRestoreAsync::Init(Callbacks callbacks)
47 {
48 try {
49 auto restore = make_shared<BSessionRestoreAsync>(callbacks);
50 return restore;
51 } catch (const exception &e) {
52 HILOGE("Failed to Restore because of %{public}s", e.what());
53 }
54 return nullptr;
55 }
56
PublishFile(BFileInfo fileInfo)57 ErrCode BSessionRestoreAsync::PublishFile(BFileInfo fileInfo)
58 {
59 auto proxy = ServiceProxy::GetInstance();
60 if (proxy == nullptr) {
61 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
62 }
63 return proxy->PublishFile(fileInfo);
64 }
65
GetFileHandle(const string & bundleName,const string & fileName)66 ErrCode BSessionRestoreAsync::GetFileHandle(const string &bundleName, const string &fileName)
67 {
68 auto proxy = ServiceProxy::GetInstance();
69 if (proxy == nullptr) {
70 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
71 }
72
73 return proxy->GetFileHandle(bundleName, fileName);
74 }
75
AppendBundles(UniqueFd remoteCap,vector<BundleName> bundlesToRestore,RestoreTypeEnum restoreType,int32_t userId)76 ErrCode BSessionRestoreAsync::AppendBundles(UniqueFd remoteCap,
77 vector<BundleName> bundlesToRestore,
78 RestoreTypeEnum restoreType,
79 int32_t userId)
80 {
81 {
82 std::unique_lock<std::mutex> lock(mutex_);
83 workList_.push({move(remoteCap), move(bundlesToRestore), restoreType, userId});
84 }
85
86 if (isAppend_.exchange(true)) {
87 return ERR_OK;
88 } else {
89 PopBundleInfo();
90 }
91
92 return ERR_OK;
93 }
94
RegisterBackupServiceDied(std::function<void ()> functor)95 void BSessionRestoreAsync::RegisterBackupServiceDied(std::function<void()> functor)
96 {
97 auto proxy = ServiceProxy::GetInstance();
98 if (proxy == nullptr || !functor) {
99 return;
100 }
101 auto remoteObj = proxy->AsObject();
102 if (!remoteObj) {
103 throw BError(BError::Codes::SA_BROKEN_IPC, "Proxy's remote object can't be nullptr");
104 }
105
106 auto callback = [functor](const wptr<IRemoteObject> &obj) { functor(); };
107 deathRecipient_ = sptr(new SvcDeathRecipient(callback));
108 remoteObj->AddDeathRecipient(deathRecipient_);
109 }
110
OnBackupServiceDied()111 void BSessionRestoreAsync::OnBackupServiceDied()
112 {
113 HILOGE("Backup service died");
114 if (callbacks_.onBackupServiceDied) {
115 callbacks_.onBackupServiceDied();
116 }
117 deathRecipient_ = nullptr;
118 ServiceProxy::InvaildInstance();
119 PopBundleInfo();
120 }
121
PopBundleInfo()122 void BSessionRestoreAsync::PopBundleInfo()
123 {
124 AppendBundleInfo info;
125 isAppend_.store(true);
126 {
127 std::unique_lock<std::mutex> lock(mutex_);
128 if (workList_.empty()) {
129 isAppend_.store(false);
130 return;
131 }
132 info = move(workList_.front());
133 workList_.pop();
134 }
135 AppendBundlesImpl(move(info));
136 }
137
AppendBundlesImpl(AppendBundleInfo info)138 void BSessionRestoreAsync::AppendBundlesImpl(AppendBundleInfo info)
139 {
140 auto proxy = ServiceProxy::GetInstance();
141 if (proxy == nullptr) {
142 return OnBundleStarted(BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode(),
143 info.bundlesToRestore);
144 }
145 auto onBackupServiceDied = bind(&BSessionRestoreAsync::OnBackupServiceDied, shared_from_this());
146 RegisterBackupServiceDied(onBackupServiceDied);
147
148 BSessionRestore::Callbacks callbacksTmp {.onFileReady = callbacks_.onFileReady,
149 .onBundleStarted = callbacks_.onBundleStarted,
150 .onBundleFinished = callbacks_.onBundleFinished,
151 .onAllBundlesFinished = callbacks_.onAllBundlesFinished,
152 .onBackupServiceDied = onBackupServiceDied};
153 int32_t res = proxy->InitRestoreSession(new ServiceReverse(callbacksTmp));
154 if (res != 0) {
155 HILOGE("Failed to Restore because of %{public}d", res);
156 BError(BError::Codes::SDK_BROKEN_IPC, "Failed to int restore session").GetCode();
157 return OnBundleStarted(res, info.bundlesToRestore);
158 }
159 res =
160 proxy->AppendBundlesRestoreSession(move(info.remoteCap), info.bundlesToRestore, info.restoreType, info.userId);
161 if (res != 0) {
162 HILOGE("Failed to Restore because of %{public}d", res);
163 BError(BError::Codes::SDK_BROKEN_IPC, "Failed to append bundles").GetCode();
164 return OnBundleStarted(res, info.bundlesToRestore);
165 }
166 }
167
OnBundleStarted(ErrCode errCode,const vector<BundleName> & bundlesToRestore)168 void BSessionRestoreAsync::OnBundleStarted(ErrCode errCode, const vector<BundleName> &bundlesToRestore)
169 {
170 for (auto &bundleName : bundlesToRestore) {
171 if (callbacks_.onBundleStarted) {
172 callbacks_.onBundleStarted(errCode, bundleName);
173 }
174 }
175 }
176 } // namespace OHOS::FileManagement::Backup