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