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 "module_ipc/service_reverse_proxy.h"
17 
18 #include "b_error/b_error.h"
19 #include "b_error/b_excep_utils.h"
20 #include "filemgmt_libhilog.h"
21 #include "module_app_gallery/app_gallery_dispose_proxy.h"
22 
23 namespace OHOS::FileManagement::Backup {
24 using namespace std;
25 
IncrementalBackupOnFileReady(string bundleName,string fileName,int fd,int manifestFd,int32_t errCode)26 void ServiceReverseProxy::IncrementalBackupOnFileReady(string bundleName, string fileName, int fd, int manifestFd,
27     int32_t errCode)
28 {
29     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
30     MessageParcel data;
31     bool fdFlag = true;
32     if (fd < 0 || manifestFd < 0) {
33         fdFlag = false;
34     }
35     if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteString(bundleName) ||
36         !data.WriteString(fileName) || !data.WriteBool(fdFlag) ||
37         (fdFlag == true && (!data.WriteFileDescriptor(fd) || !data.WriteFileDescriptor(manifestFd))) ||
38         !data.WriteInt32(errCode)) {
39         throw BError(BError::Codes::SA_BROKEN_IPC);
40     }
41 
42     MessageParcel reply;
43     MessageOption option;
44     if (int err = Remote()->SendRequest(
45         static_cast<uint32_t>(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_BACKUP_ON_FILE_READY), data, reply,
46         option);
47         err != ERR_OK) {
48         throw BError(BError::Codes::SA_BROKEN_IPC, to_string(err));
49     }
50 }
51 
IncrementalBackupOnBundleStarted(int32_t errCode,string bundleName)52 void ServiceReverseProxy::IncrementalBackupOnBundleStarted(int32_t errCode, string bundleName)
53 {
54     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
55     MessageParcel data;
56     if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteInt32(errCode) || !data.WriteString(bundleName)) {
57         throw BError(BError::Codes::SA_BROKEN_IPC);
58     };
59 
60     MessageParcel reply;
61     MessageOption option;
62     if (int err = Remote()->SendRequest(
63         static_cast<uint32_t>(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_BACKUP_ON_SUB_TASK_STARTED), data,
64         reply, option);
65         err != ERR_OK) {
66         throw BError(BError::Codes::SA_BROKEN_IPC, to_string(err));
67     }
68 }
69 
IncrementalBackupOnResultReport(std::string result,std::string bundleName)70 void ServiceReverseProxy::IncrementalBackupOnResultReport(std::string result, std::string bundleName)
71 {
72     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
73     MessageParcel data;
74     if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteString(result) || !data.WriteString(bundleName)) {
75         throw BError(BError::Codes::SA_BROKEN_IPC);
76     };
77 
78     MessageParcel reply;
79     MessageOption option;
80     if (int err = Remote()->SendRequest(
81         static_cast<uint32_t>(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_BACKUP_ON_RESULT_REPORT), data,
82         reply, option);
83         err != ERR_OK) {
84         throw BError(BError::Codes::SA_BROKEN_IPC, to_string(err));
85     }
86 }
87 
IncrementalBackupOnBundleFinished(int32_t errCode,string bundleName)88 void ServiceReverseProxy::IncrementalBackupOnBundleFinished(int32_t errCode, string bundleName)
89 {
90     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
91     MessageParcel data;
92     if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteInt32(errCode) || !data.WriteString(bundleName)) {
93         throw BError(BError::Codes::SA_BROKEN_IPC);
94     }
95 
96     MessageParcel reply;
97     MessageOption option;
98     if (int err = Remote()->SendRequest(
99         static_cast<uint32_t>(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_BACKUP_ON_SUB_TASK_FINISHED), data,
100         reply, option);
101         err != ERR_OK) {
102         throw BError(BError::Codes::SA_BROKEN_IPC, to_string(err));
103     }
104 }
105 
IncrementalBackupOnAllBundlesFinished(int32_t errCode)106 void ServiceReverseProxy::IncrementalBackupOnAllBundlesFinished(int32_t errCode)
107 {
108     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
109     MessageParcel data;
110     if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteInt32(errCode)) {
111         throw BError(BError::Codes::SA_BROKEN_IPC);
112     }
113 
114     MessageParcel reply;
115     MessageOption option;
116     if (int err = Remote()->SendRequest(
117         static_cast<uint32_t>(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_BACKUP_ON_TASK_FINISHED), data,
118         reply, option);
119         err != ERR_OK) {
120         throw BError(BError::Codes::SA_BROKEN_IPC, to_string(err));
121     }
122 }
123 
IncrementalBackupOnProcessInfo(std::string bundleName,std::string processInfo)124 void ServiceReverseProxy::IncrementalBackupOnProcessInfo(std::string bundleName, std::string processInfo)
125 {
126     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
127     MessageParcel data;
128     if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteString(bundleName) || !data.WriteString(processInfo)) {
129         throw BError(BError::Codes::SA_BROKEN_IPC);
130     }
131 
132     MessageParcel reply;
133     MessageOption option;
134     if (int err = Remote()->SendRequest(
135         static_cast<uint32_t>(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_BACKUP_ON_PROCESS_INFO), data,
136         reply, option);
137         err != ERR_OK) {
138         throw BError(BError::Codes::SA_BROKEN_IPC, to_string(err));
139     }
140 }
141 
IncrementalRestoreOnBundleStarted(int32_t errCode,string bundleName)142 void ServiceReverseProxy::IncrementalRestoreOnBundleStarted(int32_t errCode, string bundleName)
143 {
144     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
145     MessageParcel data;
146     if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteInt32(errCode) || !data.WriteString(bundleName)) {
147         throw BError(BError::Codes::SA_BROKEN_IPC);
148     }
149 
150     MessageParcel reply;
151     MessageOption option;
152     if (int err = Remote()->SendRequest(
153         static_cast<uint32_t>(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_RESTORE_ON_SUB_TASK_STARTED), data,
154         reply, option);
155         err != ERR_OK) {
156         throw BError(BError::Codes::SA_BROKEN_IPC, to_string(err));
157     }
158 }
159 
IncrementalRestoreOnBundleFinished(int32_t errCode,string bundleName)160 void ServiceReverseProxy::IncrementalRestoreOnBundleFinished(int32_t errCode, string bundleName)
161 {
162     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
163     MessageParcel data;
164     if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteInt32(errCode) || !data.WriteString(bundleName)) {
165         throw BError(BError::Codes::SA_BROKEN_IPC);
166     }
167 
168     MessageParcel reply;
169     MessageOption option;
170     if (int err = Remote()->SendRequest(
171         static_cast<uint32_t>(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_RESTORE_ON_SUB_TASK_FINISHED),
172         data, reply, option);
173         err != ERR_OK) {
174         throw BError(BError::Codes::SA_BROKEN_IPC, to_string(err));
175     }
176 }
177 
IncrementalRestoreOnAllBundlesFinished(int32_t errCode)178 void ServiceReverseProxy::IncrementalRestoreOnAllBundlesFinished(int32_t errCode)
179 {
180     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
181     MessageParcel data;
182     if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteInt32(errCode)) {
183         throw BError(BError::Codes::SA_BROKEN_IPC);
184     }
185 
186     MessageParcel reply;
187     MessageOption option;
188     if (int err = Remote()->SendRequest(
189         static_cast<uint32_t>(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_RESTORE_ON_TASK_FINISHED), data,
190         reply, option);
191         err != ERR_OK) {
192         throw BError(BError::Codes::SA_BROKEN_IPC, to_string(err));
193     }
194 }
195 
IncrementalRestoreOnResultReport(std::string result,std::string bundleName,ErrCode errCode)196 void ServiceReverseProxy::IncrementalRestoreOnResultReport(std::string result,
197     std::string bundleName, ErrCode errCode)
198 {
199     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
200     MessageParcel data;
201     if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteString(result) ||
202         !data.WriteString(bundleName) || !data.WriteInt32(errCode)) {
203         throw BError(BError::Codes::SA_BROKEN_IPC);
204     }
205 
206     MessageParcel reply;
207     MessageOption option;
208     if (int err = Remote()->SendRequest(
209         static_cast<uint32_t>(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_RESTORE_ON_RESULT_REPORT), data,
210         reply, option);
211         err != ERR_OK) {
212         throw BError(BError::Codes::SA_BROKEN_IPC, to_string(err));
213     }
214 }
215 
IncrementalRestoreOnFileReady(string bundleName,string fileName,int fd,int manifestFd,int32_t errCode)216 void ServiceReverseProxy::IncrementalRestoreOnFileReady(string bundleName, string fileName, int fd, int manifestFd,
217     int32_t errCode)
218 {
219     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
220     MessageParcel data;
221     bool fdFlag = (fd < 0 || manifestFd < 0) ? false : true;
222     if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteString(bundleName) ||
223         !data.WriteString(fileName) || !data.WriteBool(fdFlag) ||
224         (fdFlag == true && (!data.WriteFileDescriptor(fd) || !data.WriteFileDescriptor(manifestFd))) ||
225         !data.WriteInt32(errCode)) {
226         throw BError(BError::Codes::SA_BROKEN_IPC);
227     }
228 
229     MessageParcel reply;
230     MessageOption option;
231     if (int err = Remote()->SendRequest(
232         static_cast<uint32_t>(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_RESTORE_ON_FILE_READY), data,
233         reply, option);
234         err != ERR_OK) {
235         throw BError(BError::Codes::SA_BROKEN_IPC, to_string(err));
236     }
237 }
238 
IncrementalRestoreOnProcessInfo(std::string bundleName,std::string processInfo)239 void ServiceReverseProxy::IncrementalRestoreOnProcessInfo(std::string bundleName, std::string processInfo)
240 {
241     BExcepUltils::BAssert(Remote(), BError::Codes::SDK_INVAL_ARG, "Remote is nullptr");
242     MessageParcel data;
243     if (!data.WriteInterfaceToken(GetDescriptor()) || !data.WriteString(bundleName) || !data.WriteString(processInfo)) {
244         throw BError(BError::Codes::SA_BROKEN_IPC);
245     }
246 
247     MessageParcel reply;
248     MessageOption option;
249     if (int err = Remote()->SendRequest(
250         static_cast<uint32_t>(IServiceReverseInterfaceCode::SERVICER_INCREMENTAL_RESTORE_ON_PROCESS_INFO), data,
251         reply, option);
252         err != ERR_OK) {
253         throw BError(BError::Codes::SA_BROKEN_IPC, to_string(err));
254     }
255 }
256 } // namespace OHOS::FileManagement::Backup