• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_incremental_session_restore_async.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 "b_resources/b_constants.h"
22 #include "filemgmt_libhilog.h"
23 #include "service_client.h"
24 #include "service_reverse.h"
25 
26 namespace OHOS::FileManagement::Backup {
27 using namespace std;
28 
~BIncrementalSessionRestoreAsync()29 BIncrementalSessionRestoreAsync::~BIncrementalSessionRestoreAsync()
30 {
31     HILOGE("BIncrementalSessionRestoreAsync Destory");
32     callbacks_ = {};
33     deathRecipient_ = nullptr;
34 }
35 
Init(Callbacks callbacks)36 shared_ptr<BIncrementalSessionRestoreAsync> BIncrementalSessionRestoreAsync::Init(Callbacks callbacks)
37 {
38     try {
39         auto restore = make_shared<BIncrementalSessionRestoreAsync>(callbacks);
40         ServiceClient::InvaildInstance();
41         auto proxy = ServiceClient::GetInstance();
42         if (proxy == nullptr) {
43             HILOGE("Failed to get backup service");
44             return nullptr;
45         }
46         BIncrementalRestoreSession::Callbacks callbacksTmp {
47             .onFileReady = callbacks.onFileReady,
48             .onBundleStarted = callbacks.onBundleStarted,
49             .onBundleFinished = callbacks.onBundleFinished,
50             .onAllBundlesFinished = callbacks.onAllBundlesFinished,
51             .onResultReport = callbacks.onResultReport,
52             .onBackupServiceDied = callbacks.onBackupServiceDied};
53         int32_t res = proxy->InitRestoreSession(sptr(new ServiceReverse(callbacksTmp)));
54         if (res != ERR_OK) {
55             HILOGE("Failed to Restore because of %{public}d", res);
56             AppRadar::Info info ("", "", "create restore session failed");
57             AppRadar::GetInstance().RecordRestoreFuncRes(info, "BIncrementalSessionRestoreAsync::Init",
58                 AppRadar::GetInstance().GetUserId(), BizStageRestore::BIZ_STAGE_CREATE_RESTORE_SESSION_FAIL, res);
59             return nullptr;
60         }
61 
62         restore->RegisterBackupServiceDied(callbacks.onBackupServiceDied);
63         return restore;
64     } catch (const exception &e) {
65         HILOGE("Failed to Restore because of %{public}s", e.what());
66     }
67     return nullptr;
68 }
69 
PublishFile(BFileInfo fileInfo)70 ErrCode BIncrementalSessionRestoreAsync::PublishFile(BFileInfo fileInfo)
71 {
72     auto proxy = ServiceClient::GetInstance();
73     if (proxy == nullptr) {
74         return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
75     }
76     return proxy->PublishIncrementalFile(fileInfo);
77 }
78 
GetFileHandle(const string & bundleName,const string & fileName)79 ErrCode BIncrementalSessionRestoreAsync::GetFileHandle(const string &bundleName, const string &fileName)
80 {
81     auto proxy = ServiceClient::GetInstance();
82     if (proxy == nullptr) {
83         return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
84     }
85     HILOGI("Begin getFileHandle, bundle:%{public}s, fileName:%{public}s", bundleName.c_str(),
86         GetAnonyPath(fileName).c_str());
87     return proxy->GetIncrementalFileHandle(bundleName, fileName);
88 }
89 
AppendBundles(UniqueFd remoteCap,vector<BundleName> bundlesToRestore,std::vector<std::string> detailInfos,RestoreTypeEnum restoreType,int32_t userId)90 ErrCode BIncrementalSessionRestoreAsync::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     int fdCode = remoteCap.Get();
101     int32_t restoreTypeInt = static_cast<int32_t>(restoreType);
102     ErrCode res =
103         proxy->AppendBundlesRestoreSessionDataByDetail(fdCode, bundlesToRestore, detailInfos, 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, "BIncrementalSessionRestoreAsync::AppendBundles",
111                                                      AppRadar::GetInstance().GetUserId(),
112                                                      BizStageRestore::BIZ_STAGE_APPEND_BUNDLES_FAIL, res);
113     }
114     return res;
115 }
116 
AppendBundles(UniqueFd remoteCap,vector<BundleName> bundlesToRestore,RestoreTypeEnum restoreType,int32_t userId)117 ErrCode BIncrementalSessionRestoreAsync::AppendBundles(UniqueFd remoteCap,
118                                                        vector<BundleName> bundlesToRestore,
119                                                        RestoreTypeEnum restoreType,
120                                                        int32_t userId)
121 {
122     auto proxy = ServiceClient::GetInstance();
123     if (proxy == nullptr) {
124         return BError(BError::Codes::SDK_BROKEN_IPC, "Failed to get backup service").GetCode();
125     }
126     int fdCode = remoteCap.Get();
127     int32_t restoreTypeInt = static_cast<int32_t>(restoreType);
128     ErrCode res =
129         proxy->AppendBundlesRestoreSessionData(fdCode, 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, "BIncrementalSessionRestoreAsync::AppendBundles",
137                                                      AppRadar::GetInstance().GetUserId(),
138                                                      BizStageRestore::BIZ_STAGE_APPEND_BUNDLES_FAIL, res);
139     }
140     return res;
141 }
142 
Release()143 ErrCode BIncrementalSessionRestoreAsync::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 BIncrementalSessionRestoreAsync::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 BIncrementalSessionRestoreAsync::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