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