• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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_anony/b_anony.h"
19 #include "b_error/b_error.h"
20 #include "b_radar/b_radar.h"
21 #include "filemgmt_libhilog.h"
22 #include "service_client.h"
23 #include "service_reverse.h"
24 
25 namespace OHOS::FileManagement::Backup {
26 using namespace std;
27 
~BSessionRestore()28 BSessionRestore::~BSessionRestore()
29 {
30     HILOGE("BSessionRestore Destory");
31     deathRecipient_ = nullptr;
32 }
33 
Init(Callbacks callbacks)34 unique_ptr<BSessionRestore> BSessionRestore::Init(Callbacks callbacks)
35 {
36     try {
37         HILOGI("Init RestoreSession Begin");
38         auto restore = make_unique<BSessionRestore>();
39         ServiceClient::InvaildInstance();
40         auto proxy = ServiceClient::GetInstance();
41         if (proxy == nullptr) {
42             HILOGI("Failed to get backup service");
43             return nullptr;
44         }
45         int32_t res = proxy->InitRestoreSession(new ServiceReverse(callbacks));
46         if (res != ERR_OK) {
47             HILOGE("Failed to Restore because of %{public}d", res);
48             AppRadar::Info info ("", "", "create restore session failed");
49             AppRadar::GetInstance().RecordRestoreFuncRes(info, "BSessionRestore::Init",
50                 AppRadar::GetInstance().GetUserId(), BizStageRestore::BIZ_STAGE_CREATE_RESTORE_SESSION_FAIL, res);
51             return nullptr;
52         }
53 
54         restore->RegisterBackupServiceDied(callbacks.onBackupServiceDied);
55         return restore;
56     } catch (const exception &e) {
57         HILOGE("Failed to Restore because of %{public}s", e.what());
58     }
59     return nullptr;
60 }
61 
GetLocalCapabilities()62 UniqueFd BSessionRestore::GetLocalCapabilities()
63 {
64     HILOGI("GetLocalCapabilities begin");
65     auto proxy = ServiceClient::GetInstance();
66     if (proxy == nullptr) {
67         HILOGE("Failed to get backup service");
68         return UniqueFd(-EPERM);
69     }
70     int fdvalue = INVALID_FD;
71     proxy->GetLocalCapabilitiesForBundleInfos(fdvalue);
72     UniqueFd fd(fdvalue);
73     if (fd < 0) {
74         HILOGE("Failed to get local capabilities for bundleinfos");
75         return UniqueFd(-EPERM);
76     }
77     return fd;
78 }
79 
PublishFile(BFileInfo fileInfo)80 ErrCode BSessionRestore::PublishFile(BFileInfo fileInfo)
81 {
82     auto proxy = ServiceClient::GetInstance();
83     if (proxy == nullptr) {
84         return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
85     }
86     return proxy->PublishFile(fileInfo);
87 }
88 
Start()89 ErrCode BSessionRestore::Start()
90 {
91     auto proxy = ServiceClient::GetInstance();
92     if (proxy == nullptr) {
93         return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
94     }
95 
96     return proxy->Start();
97 }
98 
GetFileHandle(const string & bundleName,const string & fileName)99 ErrCode BSessionRestore::GetFileHandle(const string &bundleName, const string &fileName)
100 {
101     auto proxy = ServiceClient::GetInstance();
102     if (proxy == nullptr) {
103         return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
104     }
105     HILOGI("Begin getFileHandle, bundle:%{public}s, fileName:%{public}s", bundleName.c_str(),
106         GetAnonyPath(fileName).c_str());
107     return proxy->GetFileHandle(bundleName, fileName);
108 }
109 
AppendBundles(UniqueFd remoteCap,vector<BundleName> bundlesToRestore,std::vector<std::string> detailInfos)110 ErrCode BSessionRestore::AppendBundles(UniqueFd remoteCap,
111                                        vector<BundleName> bundlesToRestore,
112                                        std::vector<std::string> detailInfos)
113 {
114     auto proxy = ServiceClient::GetInstance();
115     if (proxy == nullptr) {
116         return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
117     }
118     int32_t remoteCapInt = remoteCap.Get();
119     ErrCode res = proxy->AppendBundlesRestoreSessionDataByDetail(remoteCapInt, bundlesToRestore, detailInfos,
120                                                                  DEFAULT_RESTORE_TYPE, DEFAULT_USER_ID);
121     if (res != ERR_OK) {
122         std::string ss;
123         for (const auto &bundle : bundlesToRestore) {
124             ss += bundle + ", ";
125         }
126         AppRadar::Info info(ss.c_str(), "", "AppendBundles with infos");
127         AppRadar::GetInstance().RecordRestoreFuncRes(info, "BSessionRestore::AppendBundles",
128                                                      AppRadar::GetInstance().GetUserId(),
129                                                      BizStageRestore::BIZ_STAGE_APPEND_BUNDLES_FAIL, res);
130     }
131     return res;
132 }
133 
AppendBundles(UniqueFd remoteCap,vector<BundleName> bundlesToRestore)134 ErrCode BSessionRestore::AppendBundles(UniqueFd remoteCap, vector<BundleName> bundlesToRestore)
135 {
136     auto proxy = ServiceClient::GetInstance();
137     if (proxy == nullptr) {
138         return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
139     }
140     int32_t remoteCapInt = remoteCap.Get();
141     ErrCode res = proxy->AppendBundlesRestoreSessionData(remoteCapInt, bundlesToRestore,
142                                                          DEFAULT_RESTORE_TYPE, DEFAULT_USER_ID);
143     if (res != ERR_OK) {
144         std::string ss;
145         for (const auto &bundle : bundlesToRestore) {
146             ss += bundle + ", ";
147         }
148         AppRadar::Info info(ss.c_str(), "", "");
149         AppRadar::GetInstance().RecordRestoreFuncRes(info, "BSessionRestore::AppendBundles",
150                                                      AppRadar::GetInstance().GetUserId(),
151                                                      BizStageRestore::BIZ_STAGE_APPEND_BUNDLES_FAIL, res);
152     }
153     return res;
154 }
155 
Finish()156 ErrCode BSessionRestore::Finish()
157 {
158     auto proxy = ServiceClient::GetInstance();
159     if (proxy == nullptr) {
160         return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
161     }
162 
163     return proxy->Finish();
164 }
165 
Release()166 ErrCode BSessionRestore::Release()
167 {
168     auto proxy = ServiceClient::GetInstance();
169     if (proxy == nullptr) {
170         return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
171     }
172 
173     return proxy->Release();
174 }
175 
RegisterBackupServiceDied(std::function<void ()> functor)176 void BSessionRestore::RegisterBackupServiceDied(std::function<void()> functor)
177 {
178     auto proxy = ServiceClient::GetInstance();
179     if (proxy == nullptr || !functor) {
180         return;
181     }
182     auto remoteObj = proxy->AsObject();
183     if (!remoteObj) {
184         throw BError(BError::Codes::SA_BROKEN_IPC, "Proxy's remote object can't be nullptr");
185     }
186 
187     auto callback = [functor](const wptr<IRemoteObject> &obj) {
188         HILOGI("Backup service died");
189         ServiceClient::InvaildInstance();
190         functor();
191     };
192     deathRecipient_ = sptr(new SvcDeathRecipient(callback));
193     remoteObj->AddDeathRecipient(deathRecipient_);
194 }
195 
Cancel(std::string bundleName)196 ErrCode BSessionRestore::Cancel(std::string bundleName)
197 {
198     ErrCode result = BError::BackupErrorCode::E_CANCEL_UNSTARTED_TASK;
199     auto proxy = ServiceClient::GetInstance();
200     if (proxy == nullptr) {
201         HILOGE("Called Cancel, failed to get proxy.");
202         return result;
203     }
204 
205     proxy->CancelForResult(bundleName, result);
206     return result;
207 }
208 
CleanBundleTempDir(const std::string & bundleName)209 ErrCode BSessionRestore::CleanBundleTempDir(const std::string &bundleName)
210 {
211     HILOGI("BSessionRestore::CleanBundleTempDir");
212     auto proxy = ServiceClient::GetInstance();
213     if (proxy == nullptr) {
214         return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
215     }
216     return proxy->CleanBundleTempDir(bundleName);
217 }
218 
GetCompatibilityInfo(const std::string & bundleName,const std::string & extInfo,std::string & compatInfo)219 ErrCode BSessionRestore::GetCompatibilityInfo(const std::string &bundleName, const std::string &extInfo,
220     std::string &compatInfo)
221 {
222     HILOGI("BSessionRestore::GetCompatibilityInfo");
223     auto proxy = ServiceClient::GetInstance();
224     if (proxy == nullptr) {
225         return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
226     }
227     return proxy->GetCompatibilityInfo(bundleName, extInfo, compatInfo);
228 }
229 } // namespace OHOS::FileManagement::Backup