• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "service_reverse.h"
17 
18 #include "b_error/b_error.h"
19 #include "filemgmt_libhilog.h"
20 
21 namespace OHOS::FileManagement::Backup {
22 using namespace std;
23 
BackupOnFileReady(const std::string & bundleName,const std::string & fileName,int fd,int32_t errCode)24 ErrCode ServiceReverse::BackupOnFileReady(const std::string &bundleName,
25                                           const std::string &fileName,
26                                           int fd,
27                                           int32_t errCode)
28 {
29     if (scenario_ != Scenario::BACKUP || !callbacksBackup_.onFileReady) {
30         HILOGE("Error scenario or callback is nullptr, scenario = %{public}d", scenario_);
31         return BError(BError::Codes::OK);
32     }
33     BFileInfo bFileInfo(bundleName, fileName, 0);
34     callbacksBackup_.onFileReady(bFileInfo, UniqueFd(fd), errCode);
35     return BError(BError::Codes::OK);
36 }
37 
BackupOnFileReadyWithoutFd(const std::string & bundleName,const std::string & fileName,int32_t errCode)38 ErrCode ServiceReverse::BackupOnFileReadyWithoutFd(const std::string &bundleName,
39                                                    const std::string &fileName,
40                                                    int32_t errCode)
41 {
42     if (scenario_ != Scenario::BACKUP || !callbacksBackup_.onFileReady) {
43         HILOGE("Error scenario or callback is nullptr, scenario = %{public}d", scenario_);
44         return BError(BError::Codes::OK);
45     }
46     BFileInfo bFileInfo(bundleName, fileName, 0);
47     callbacksBackup_.onFileReady(bFileInfo, UniqueFd(INVALID_FD), errCode);
48     return BError(BError::Codes::OK);
49 }
50 
BackupOnBundleStarted(int32_t errCode,const std::string & bundleName)51 ErrCode ServiceReverse::BackupOnBundleStarted(int32_t errCode, const std::string &bundleName)
52 {
53     if (scenario_ != Scenario::BACKUP || !callbacksBackup_.onBundleStarted) {
54         HILOGE("Error scenario or callback is nullptr, scenario = %{public}d", scenario_);
55         return BError(BError::Codes::OK);
56     }
57     callbacksBackup_.onBundleStarted(errCode, bundleName);
58     return BError(BError::Codes::OK);
59 }
60 
BackupOnResultReport(const std::string & result,const std::string & bundleName)61 ErrCode ServiceReverse::BackupOnResultReport(const std::string &result, const std::string &bundleName)
62 {
63     if (scenario_ != Scenario::BACKUP || !callbacksBackup_.onResultReport) {
64         HILOGE("Error scenario or callback is nullptr, scenario = %{public}d", scenario_);
65         return BError(BError::Codes::OK);
66     }
67     callbacksBackup_.onResultReport(bundleName, result);
68     return BError(BError::Codes::OK);
69 }
70 
BackupOnBundleFinished(int32_t errCode,const std::string & bundleName)71 ErrCode ServiceReverse::BackupOnBundleFinished(int32_t errCode, const std::string &bundleName)
72 {
73     if (scenario_ != Scenario::BACKUP || !callbacksBackup_.onBundleFinished) {
74         HILOGE("Error scenario or callback is nullptr, scenario = %{public}d", scenario_);
75         return BError(BError::Codes::OK);
76     }
77     HILOGI("errCode = %{public}d, bundleName = %{public}s", errCode, bundleName.c_str());
78     callbacksBackup_.onBundleFinished(errCode, bundleName);
79     return BError(BError::Codes::OK);
80 }
81 
BackupOnAllBundlesFinished(int32_t errCode)82 ErrCode ServiceReverse::BackupOnAllBundlesFinished(int32_t errCode)
83 {
84     HILOGI("errCode = %{public}d", errCode);
85     if (scenario_ != Scenario::BACKUP || !callbacksBackup_.onAllBundlesFinished) {
86         HILOGE("Error scenario or callback is nullptr, scenario = %{public}d", scenario_);
87         return BError(BError::Codes::OK);
88     }
89     callbacksBackup_.onAllBundlesFinished(errCode);
90     return BError(BError::Codes::OK);
91 }
92 
BackupOnProcessInfo(const std::string & bundleName,const std::string & processInfo)93 ErrCode ServiceReverse::BackupOnProcessInfo(const std::string &bundleName, const std::string &processInfo)
94 {
95     HILOGI("bundleName = %{public}s", bundleName.c_str());
96     if (scenario_ != Scenario::BACKUP || !callbacksBackup_.onProcess) {
97         HILOGI("Error scenario or callback is nullptr");
98         return BError(BError::Codes::OK);
99     }
100     callbacksBackup_.onProcess(bundleName, processInfo);
101     return BError(BError::Codes::OK);
102 }
103 
BackupOnScanningInfo(const std::string & scannedInfo)104 ErrCode ServiceReverse::BackupOnScanningInfo(const std::string &scannedInfo)
105 {
106     if (scenario_ != Scenario::BACKUP || !callbacksBackup_.onBackupSizeReport) {
107         HILOGE("Error scenario or callback is nullptr");
108         return BError(BError::Codes::OK);
109     }
110     callbacksBackup_.onBackupSizeReport(scannedInfo);
111     return BError(BError::Codes::OK);
112 }
113 
RestoreOnBundleStarted(int32_t errCode,const std::string & bundleName)114 ErrCode ServiceReverse::RestoreOnBundleStarted(int32_t errCode, const std::string &bundleName)
115 {
116     if (scenario_ != Scenario::RESTORE || !callbacksRestore_.onBundleStarted) {
117         HILOGE("Error scenario or callback is nullptr, scenario = %{public}d", scenario_);
118         return BError(BError::Codes::OK);
119     }
120     callbacksRestore_.onBundleStarted(errCode, bundleName);
121     return BError(BError::Codes::OK);
122 }
123 
RestoreOnBundleFinished(int32_t errCode,const std::string & bundleName)124 ErrCode ServiceReverse::RestoreOnBundleFinished(int32_t errCode, const std::string &bundleName)
125 {
126     if (scenario_ != Scenario::RESTORE || !callbacksRestore_.onBundleFinished) {
127         HILOGE("Error scenario or callback is nullptr, scenario = %{public}d", scenario_);
128         return BError(BError::Codes::OK);
129     }
130     HILOGI("errCode = %{public}d, bundleName = %{public}s", errCode, bundleName.c_str());
131     callbacksRestore_.onBundleFinished(errCode, bundleName);
132     return BError(BError::Codes::OK);
133 }
134 
RestoreOnAllBundlesFinished(int32_t errCode)135 ErrCode ServiceReverse::RestoreOnAllBundlesFinished(int32_t errCode)
136 {
137     HILOGI("errCode = %{public}d", errCode);
138     if (scenario_ != Scenario::RESTORE || !callbacksRestore_.onAllBundlesFinished) {
139         HILOGE("Error scenario or callback is nullptr, scenario = %{public}d", scenario_);
140         return BError(BError::Codes::OK);
141     }
142     callbacksRestore_.onAllBundlesFinished(errCode);
143     return BError(BError::Codes::OK);
144 }
145 
RestoreOnFileReady(const std::string & bundleName,const std::string & fileName,int fd,int32_t errCode)146 ErrCode ServiceReverse::RestoreOnFileReady(const std::string &bundleName,
147                                            const std::string &fileName,
148                                            int fd,
149                                            int32_t errCode)
150 {
151     HILOGD("begin, bundleName is:%{public}s", bundleName.c_str());
152     if (scenario_ != Scenario::RESTORE || !callbacksRestore_.onFileReady) {
153         HILOGE("Error scenario or callback is nullptr, scenario = %{public}d", scenario_);
154         return BError(BError::Codes::OK);
155     }
156     BFileInfo bFileInfo(bundleName, fileName, 0);
157     callbacksRestore_.onFileReady(bFileInfo, UniqueFd(fd), errCode);
158     return BError(BError::Codes::OK);
159 }
160 
RestoreOnFileReadyWithoutFd(const std::string & bundleName,const std::string & fileName,int32_t errCode)161 ErrCode ServiceReverse::RestoreOnFileReadyWithoutFd(const std::string &bundleName,
162                                                     const std::string &fileName,
163                                                     int32_t errCode)
164 {
165     HILOGD("begin, bundleName is:%{public}s", bundleName.c_str());
166     if (scenario_ != Scenario::RESTORE || !callbacksRestore_.onFileReady) {
167         HILOGE("Error scenario or callback is nullptr, scenario = %{public}d", scenario_);
168         return BError(BError::Codes::OK);
169     }
170     BFileInfo bFileInfo(bundleName, fileName, 0);
171     callbacksRestore_.onFileReady(bFileInfo, UniqueFd(INVALID_FD), errCode);
172     return BError(BError::Codes::OK);
173 }
174 
RestoreOnResultReport(const std::string & result,const std::string & bundleName,ErrCode errCode)175 ErrCode ServiceReverse::RestoreOnResultReport(const std::string &result, const std::string &bundleName, ErrCode errCode)
176 {
177     HILOGI("ServiceReverse RestoreOnResultReport bundle %{public}s begin with result: %{public}s", bundleName.c_str(),
178            result.c_str());
179     if (scenario_ != Scenario::RESTORE || !callbacksRestore_.onResultReport) {
180         HILOGE("Error scenario or callback is nullptr, scenario = %{public}d", scenario_);
181         return BError(BError::Codes::OK);
182     }
183     callbacksRestore_.onResultReport(bundleName, result);
184     if (scenario_ != Scenario::RESTORE || !callbacksRestore_.onBundleFinished) {
185         HILOGE("Error scenario or callback is nullptr, scenario = %{public}d", scenario_);
186         return BError(BError::Codes::OK);
187     }
188     callbacksRestore_.onBundleFinished(errCode, bundleName);
189     return BError(BError::Codes::OK);
190 }
191 
RestoreOnProcessInfo(const std::string & bundleName,const std::string & processInfo)192 ErrCode ServiceReverse::RestoreOnProcessInfo(const std::string &bundleName, const std::string &processInfo)
193 {
194     HILOGI("bundleName = %{public}s", bundleName.c_str());
195     if (scenario_ != Scenario::RESTORE || !callbacksRestore_.onProcess) {
196         HILOGI("Error scenario or callback is nullptr");
197         return BError(BError::Codes::OK);
198     }
199     callbacksRestore_.onProcess(bundleName, processInfo);
200     return BError(BError::Codes::OK);
201 }
202 
ServiceReverse(BSessionBackup::Callbacks callbacks)203 ServiceReverse::ServiceReverse(BSessionBackup::Callbacks callbacks)
204     : scenario_(Scenario::BACKUP), callbacksBackup_(callbacks)
205 {
206 }
207 
ServiceReverse(BSessionRestore::Callbacks callbacks)208 ServiceReverse::ServiceReverse(BSessionRestore::Callbacks callbacks)
209     : scenario_(Scenario::RESTORE), callbacksRestore_(callbacks)
210 {
211 }
212 } // namespace OHOS::FileManagement::Backup