1 /*
2 * Copyright (c) 2022-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.h"
17
18 #include "b_error/b_error.h"
19 #include "filemgmt_libhilog.h"
20 #include "service_proxy.h"
21 #include "service_reverse.h"
22
23 namespace OHOS::FileManagement::Backup {
24 using namespace std;
25
~BSessionRestore()26 BSessionRestore::~BSessionRestore()
27 {
28 if (!deathRecipient_) {
29 HILOGI("Death Recipient is nullptr");
30 return;
31 }
32 auto proxy = ServiceProxy::GetInstance();
33 if (proxy == nullptr) {
34 return;
35 }
36 auto remoteObject = proxy->AsObject();
37 if (remoteObject != nullptr) {
38 remoteObject->RemoveDeathRecipient(deathRecipient_);
39 }
40 deathRecipient_ = nullptr;
41 }
42
Init(Callbacks callbacks)43 unique_ptr<BSessionRestore> BSessionRestore::Init(Callbacks callbacks)
44 {
45 try {
46 auto restore = make_unique<BSessionRestore>();
47 auto proxy = ServiceProxy::GetInstance();
48 if (proxy == nullptr) {
49 HILOGI("Failed to get backup service");
50 return nullptr;
51 }
52 int32_t res = proxy->InitRestoreSession(new ServiceReverse(callbacks));
53 if (res != 0) {
54 HILOGE("Failed to Restore because of %{public}d", res);
55 return nullptr;
56 }
57
58 restore->RegisterBackupServiceDied(callbacks.onBackupServiceDied);
59 return restore;
60 } catch (const exception &e) {
61 HILOGE("Failed to Restore because of %{public}s", e.what());
62 }
63 return nullptr;
64 }
65
PublishFile(BFileInfo fileInfo)66 ErrCode BSessionRestore::PublishFile(BFileInfo fileInfo)
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 return proxy->PublishFile(fileInfo);
73 }
74
Start()75 ErrCode BSessionRestore::Start()
76 {
77 auto proxy = ServiceProxy::GetInstance();
78 if (proxy == nullptr) {
79 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
80 }
81
82 return proxy->Start();
83 }
84
GetFileHandle(const string & bundleName,const string & fileName)85 ErrCode BSessionRestore::GetFileHandle(const string &bundleName, const string &fileName)
86 {
87 auto proxy = ServiceProxy::GetInstance();
88 if (proxy == nullptr) {
89 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
90 }
91
92 return proxy->GetFileHandle(bundleName, fileName);
93 }
94
AppendBundles(UniqueFd remoteCap,vector<BundleName> bundlesToRestore)95 ErrCode BSessionRestore::AppendBundles(UniqueFd remoteCap, vector<BundleName> bundlesToRestore)
96 {
97 auto proxy = ServiceProxy::GetInstance();
98 if (proxy == nullptr) {
99 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
100 }
101
102 return proxy->AppendBundlesRestoreSession(move(remoteCap), bundlesToRestore);
103 }
104
Finish()105 ErrCode BSessionRestore::Finish()
106 {
107 auto proxy = ServiceProxy::GetInstance();
108 if (proxy == nullptr) {
109 return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
110 }
111
112 return proxy->Finish();
113 }
114
RegisterBackupServiceDied(std::function<void ()> functor)115 void BSessionRestore::RegisterBackupServiceDied(std::function<void()> functor)
116 {
117 auto proxy = ServiceProxy::GetInstance();
118 if (proxy == nullptr || !functor) {
119 return;
120 }
121 auto remoteObj = proxy->AsObject();
122 if (!remoteObj) {
123 throw BError(BError::Codes::SA_BROKEN_IPC, "Proxy's remote object can't be nullptr");
124 }
125
126 auto callback = [functor](const wptr<IRemoteObject> &obj) {
127 HILOGI("Backup service died");
128 ServiceProxy::InvaildInstance();
129 functor();
130 };
131 deathRecipient_ = sptr(new SvcDeathRecipient(callback));
132 remoteObj->AddDeathRecipient(deathRecipient_);
133 }
134 } // namespace OHOS::FileManagement::Backup