1 /*
2  * Copyright (C) 2022 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 <unistd.h>
17 
18 #include "ashmem.h"
19 #include "hilog_wrapper.h"
20 #include "ipc_skeleton.h"
21 #include "memory_guard.h"
22 #include "parcel.h"
23 #include "pixel_map.h"
24 #include "wallpaper_common.h"
25 #include "wallpaper_service_ipc_interface_code.h"
26 #include "wallpaper_service_stub.h"
27 
28 namespace OHOS {
29 namespace WallpaperMgrService {
30 using namespace OHOS::HiviewDFX;
31 using namespace OHOS::Media;
32 
WallpaperServiceStub(bool serialInvokeFlag)33 WallpaperServiceStub::WallpaperServiceStub(bool serialInvokeFlag) : IRemoteStub(serialInvokeFlag)
34 {
35     requestHandlers = {
36         { static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::SET_WALLPAPER),
37             [this](MessageParcel &data, MessageParcel &reply) { return OnSetWallpaper(data, reply); } },
38         { static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::GET_PIXELMAP),
39             [this](MessageParcel &data, MessageParcel &reply) { return OnGetPixelMap(data, reply); } },
40         { static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::GET_COLORS),
41             [this](MessageParcel &data, MessageParcel &reply) { return OnGetColors(data, reply); } },
42         { static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::GET_WALLPAPER_ID),
43             [this](MessageParcel &data, MessageParcel &reply) { return OnGetWallpaperId(data, reply); } },
44         { static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::GET_FILE),
45             [this](MessageParcel &data, MessageParcel &reply) { return OnGetFile(data, reply); } },
46         { static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::ON),
47             [this](MessageParcel &data, MessageParcel &reply) { return OnWallpaperOn(data, reply); } },
48         { static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::OFF),
49             [this](MessageParcel &data, MessageParcel &reply) { return OnWallpaperOff(data, reply); } },
50         { static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::IS_CHANGE_PERMITTED),
51             [this](MessageParcel &data, MessageParcel &reply) { return OnIsChangePermitted(data, reply); } },
52         { static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::IS_OPERATION_ALLOWED),
53             [this](MessageParcel &data, MessageParcel &reply) { return OnIsOperationAllowed(data, reply); } },
54         { static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::RESET_WALLPAPER),
55             [this](MessageParcel &data, MessageParcel &reply) { return OnResetWallpaper(data, reply); } },
56         { static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::REGISTER_CALLBACK),
57             [this](MessageParcel &data, MessageParcel &reply) { return OnRegisterWallpaperCallback(data, reply); } },
58         { static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::SET_WALLPAPER_V9),
59             [this](MessageParcel &data, MessageParcel &reply) { return OnSetWallpaperV9(data, reply); } },
60         { static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::GET_PIXELMAP_V9),
61             [this](MessageParcel &data, MessageParcel &reply) { return OnGetPixelMapV9(data, reply); } },
62         { static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::GET_COLORS_V9),
63             [this](MessageParcel &data, MessageParcel &reply) { return OnGetColorsV9(data, reply); } },
64         { static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::RESET_WALLPAPER_V9),
65             [this](MessageParcel &data, MessageParcel &reply) { return OnResetWallpaperV9(data, reply); } },
66         { static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::SET_VIDEO),
67             [this](MessageParcel &data, MessageParcel &reply) { return OnSetVideo(data, reply); } },
68         { static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::SET_CUSTOM),
69             [this](MessageParcel &data, MessageParcel &reply) { return OnSetCustomWallpaper(data, reply); } },
70         { static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::SEND_EVENT),
71             [this](MessageParcel &data, MessageParcel &reply) { return OnSendEvent(data, reply); } },
72         { static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::SET_WALLPAPER_PIXELMAP),
73             [this](MessageParcel &data, MessageParcel &reply) { return OnSetWallpaperByPixelMap(data, reply); } },
74         { static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::SET_WALLPAPER_PIXELMAP_V9),
75             [this](MessageParcel &data, MessageParcel &reply) { return OnSetWallpaperV9ByPixelMap(data, reply); } },
76         { static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::SET_ALL_WALLPAPERS),
77             [this](MessageParcel &data, MessageParcel &reply) { return OnSetAllWallpapers(data, reply); } },
78         { static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::GET_CORRESPOND_WALLPAPER),
79             [this](MessageParcel &data, MessageParcel &reply) { return OnGetCorrespondWallpaper(data, reply); } },
80     };
81 }
82 
~WallpaperServiceStub()83 WallpaperServiceStub::~WallpaperServiceStub()
84 {
85 }
86 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)87 int32_t WallpaperServiceStub::OnRemoteRequest(
88     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
89 {
90     MemoryGuard cacheGuard;
91     std::u16string myDescriptor = WallpaperServiceStub::GetDescriptor();
92     std::u16string remoteDescriptor = data.ReadInterfaceToken();
93     if (myDescriptor != remoteDescriptor) {
94         HILOG_ERROR("Remote descriptor not the same as local descriptor.");
95         return E_CHECK_DESCRIPTOR_ERROR;
96     }
97     return HandleWallpaperRequest(code, data, reply, option);
98 }
99 
GetWallpaperRequestHandler(uint32_t code)100 WallpaperRequestHandler WallpaperServiceStub::GetWallpaperRequestHandler(uint32_t code)
101 {
102     auto it = requestHandlers.find(code);
103     if (it != requestHandlers.end()) {
104         return it->second;
105     }
106     return nullptr;
107 }
108 
HandleWallpaperRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)109 int32_t WallpaperServiceStub::HandleWallpaperRequest(
110     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
111 {
112     WallpaperRequestHandler handler = GetWallpaperRequestHandler(code);
113     if (handler) {
114         return handler(data, reply);
115     }
116     HILOG_ERROR("remote request unhandled: %{public}d", code);
117     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
118 }
119 
OnSetWallpaper(MessageParcel & data,MessageParcel & reply)120 int32_t WallpaperServiceStub::OnSetWallpaper(MessageParcel &data, MessageParcel &reply)
121 {
122     return OnSetWallpaperInner(data, reply, false);
123 }
124 
OnSetWallpaperV9(MessageParcel & data,MessageParcel & reply)125 int32_t WallpaperServiceStub::OnSetWallpaperV9(MessageParcel &data, MessageParcel &reply)
126 {
127     return OnSetWallpaperInner(data, reply, true);
128 }
129 
OnSetWallpaperInner(MessageParcel & data,MessageParcel & reply,bool isSystemApi)130 int32_t WallpaperServiceStub::OnSetWallpaperInner(MessageParcel &data, MessageParcel &reply, bool isSystemApi)
131 {
132     HILOG_DEBUG("WallpaperServiceStub::SetWallpaper start.");
133     int32_t fd = data.ReadFileDescriptor();
134     if (fd < 0) {
135         HILOG_ERROR("ReadFileDescriptor fail. fd[%{public}d]", fd);
136         return IPC_STUB_INVALID_DATA_ERR;
137     }
138     int32_t wallpaperType = data.ReadInt32();
139     int32_t length = data.ReadInt32();
140     ErrorCode wallpaperErrorCode = E_UNKNOWN;
141     if (isSystemApi) {
142         wallpaperErrorCode = SetWallpaperV9(fd, wallpaperType, length);
143     } else {
144         wallpaperErrorCode = SetWallpaper(fd, wallpaperType, length);
145     }
146     close(fd);
147     if (!reply.WriteInt32(static_cast<int32_t>(wallpaperErrorCode))) {
148         HILOG_ERROR("WriteInt32 fail!");
149         return IPC_STUB_WRITE_PARCEL_ERR;
150     }
151     return ERR_NONE;
152 }
153 
OnSetWallpaperByPixelMap(MessageParcel & data,MessageParcel & reply)154 int32_t WallpaperServiceStub::OnSetWallpaperByPixelMap(MessageParcel &data, MessageParcel &reply)
155 {
156     return OnSetWallpaperInnerByPixelMap(data, reply, false);
157 }
158 
OnSetWallpaperV9ByPixelMap(MessageParcel & data,MessageParcel & reply)159 int32_t WallpaperServiceStub::OnSetWallpaperV9ByPixelMap(MessageParcel &data, MessageParcel &reply)
160 {
161     return OnSetWallpaperInnerByPixelMap(data, reply, true);
162 }
163 
OnSetWallpaperInnerByPixelMap(MessageParcel & data,MessageParcel & reply,bool isSystemApi)164 int32_t WallpaperServiceStub::OnSetWallpaperInnerByPixelMap(MessageParcel &data, MessageParcel &reply, bool isSystemApi)
165 {
166     HILOG_DEBUG("WallpaperServiceStub::SetWallpaper start.");
167     int32_t vectorPixelMapSize = data.ReadInt32();
168     if (vectorPixelMapSize <= 0) {
169         HILOG_ERROR("ReadInt32 fail!");
170         return IPC_STUB_INVALID_DATA_ERR;
171     }
172     auto *rawData = (uint8_t *)data.ReadRawData(vectorPixelMapSize);
173     if (rawData == nullptr) {
174         HILOG_ERROR("ReadRawData fail!");
175         return IPC_STUB_INVALID_DATA_ERR;
176     }
177     std::vector<uint8_t> VectorPixelMap(rawData, rawData + vectorPixelMapSize);
178     int32_t wallpaperType = data.ReadInt32();
179     std::shared_ptr<OHOS::Media::PixelMap> pixelMap = VectorToPixelMap(VectorPixelMap);
180     if (pixelMap == nullptr) {
181         HILOG_ERROR("VectorToPixelMap fail!");
182         return IPC_STUB_INVALID_DATA_ERR;
183     }
184     ErrorCode wallpaperErrorCode = E_UNKNOWN;
185     if (isSystemApi) {
186         wallpaperErrorCode = SetWallpaperV9ByPixelMap(pixelMap, wallpaperType);
187     } else {
188         wallpaperErrorCode = SetWallpaperByPixelMap(pixelMap, wallpaperType);
189     }
190     if (!reply.WriteInt32(static_cast<int32_t>(wallpaperErrorCode))) {
191         HILOG_ERROR("WriteInt32 fail!");
192         return IPC_STUB_WRITE_PARCEL_ERR;
193     }
194     return ERR_NONE;
195 }
196 
OnGetPixelMap(MessageParcel & data,MessageParcel & reply)197 int32_t WallpaperServiceStub::OnGetPixelMap(MessageParcel &data, MessageParcel &reply)
198 {
199     return OnGetPixelMapInner(data, reply, false);
200 }
201 
OnSetVideo(MessageParcel & data,MessageParcel & reply)202 int32_t WallpaperServiceStub::OnSetVideo(MessageParcel &data, MessageParcel &reply)
203 {
204     HILOG_DEBUG("WallpaperServiceStub::OnSetVideo start.");
205     int32_t fd = data.ReadFileDescriptor();
206     int32_t wallpaperType = data.ReadInt32();
207     int32_t length = data.ReadInt32();
208     ErrorCode wallpaperErrorCode = SetVideo(fd, wallpaperType, length);
209     close(fd);
210     if (!reply.WriteInt32(static_cast<int32_t>(wallpaperErrorCode))) {
211         HILOG_ERROR("Write int is failed!");
212         return IPC_STUB_WRITE_PARCEL_ERR;
213     }
214     return ERR_NONE;
215 }
216 
OnSetCustomWallpaper(MessageParcel & data,MessageParcel & reply)217 int32_t WallpaperServiceStub::OnSetCustomWallpaper(MessageParcel &data, MessageParcel &reply)
218 {
219     HILOG_DEBUG("WallpaperServiceStub::SetCustomWallpaper start.");
220     auto fd = data.ReadFileDescriptor();
221     int32_t wallpaperType = data.ReadInt32();
222     int32_t length = data.ReadInt32();
223     ErrorCode wallpaperErrorCode = SetCustomWallpaper(fd, wallpaperType, length);
224     close(fd);
225     if (!reply.WriteInt32(static_cast<int32_t>(wallpaperErrorCode))) {
226         HILOG_ERROR("Write int is failed!");
227         return IPC_STUB_WRITE_PARCEL_ERR;
228     }
229     return ERR_NONE;
230 }
231 
OnGetPixelMapV9(MessageParcel & data,MessageParcel & reply)232 int32_t WallpaperServiceStub::OnGetPixelMapV9(MessageParcel &data, MessageParcel &reply)
233 {
234     return OnGetPixelMapInner(data, reply, true);
235 }
236 
OnGetPixelMapInner(MessageParcel & data,MessageParcel & reply,bool isSystemApi)237 int32_t WallpaperServiceStub::OnGetPixelMapInner(MessageParcel &data, MessageParcel &reply, bool isSystemApi)
238 {
239     HILOG_DEBUG("WallpaperServiceStub::GetPixelMap start.");
240     int32_t wallpaperType = data.ReadInt32();
241     IWallpaperService::FdInfo fdInfo;
242     ErrorCode wallpaperErrorCode = E_UNKNOWN;
243     if (isSystemApi) {
244         wallpaperErrorCode = GetPixelMapV9(wallpaperType, fdInfo);
245     } else {
246         wallpaperErrorCode = GetPixelMap(wallpaperType, fdInfo);
247     }
248     HILOG_INFO(" OnGetPixelMap wallpaperErrorCode = %{public}d", wallpaperErrorCode);
249     if (!reply.WriteInt32(static_cast<int32_t>(wallpaperErrorCode))) {
250         HILOG_ERROR("WriteInt32 fail!");
251         close(fdInfo.fd);
252         return IPC_STUB_WRITE_PARCEL_ERR;
253     }
254     if (wallpaperErrorCode == E_OK) {
255         if (!reply.WriteInt32(fdInfo.size)) {
256             HILOG_ERROR("WriteInt32 fail!");
257             close(fdInfo.fd);
258             return IPC_STUB_WRITE_PARCEL_ERR;
259         }
260         if (!reply.WriteFileDescriptor(fdInfo.fd)) {
261             HILOG_ERROR("WriteFileDescriptor fail!");
262             close(fdInfo.fd);
263             return IPC_STUB_WRITE_PARCEL_ERR;
264         }
265     }
266     close(fdInfo.fd);
267     return ERR_NONE;
268 }
269 
OnGetColors(MessageParcel & data,MessageParcel & reply)270 int32_t WallpaperServiceStub::OnGetColors(MessageParcel &data, MessageParcel &reply)
271 {
272     return OnGetColorsInner(data, reply, false);
273 }
274 
OnGetColorsV9(MessageParcel & data,MessageParcel & reply)275 int32_t WallpaperServiceStub::OnGetColorsV9(MessageParcel &data, MessageParcel &reply)
276 {
277     return OnGetColorsInner(data, reply, true);
278 }
279 
OnGetColorsInner(MessageParcel & data,MessageParcel & reply,bool isSystemApi)280 int32_t WallpaperServiceStub::OnGetColorsInner(MessageParcel &data, MessageParcel &reply, bool isSystemApi)
281 {
282     HILOG_DEBUG("WallpaperServiceStub::OnGetColors start.");
283     int32_t wallpaperType = data.ReadInt32();
284     std::vector<uint64_t> vecWallpaperColors;
285     ErrorCode wallpaperErrorCode = E_UNKNOWN;
286     if (isSystemApi) {
287         wallpaperErrorCode = GetColorsV9(wallpaperType, vecWallpaperColors);
288     } else {
289         wallpaperErrorCode = GetColors(wallpaperType, vecWallpaperColors);
290     }
291     auto size = vecWallpaperColors.size();
292     if (!reply.WriteInt32(static_cast<int32_t>(wallpaperErrorCode))) {
293         HILOG_ERROR("WriteInt32 fail!");
294         return IPC_STUB_WRITE_PARCEL_ERR;
295     }
296     if (wallpaperErrorCode == E_OK) {
297         if (!reply.WriteUInt64Vector(vecWallpaperColors)) {
298             HILOG_ERROR("WallpaperServiceStub::OnGetColors WriteUInt64Vector error!");
299             return IPC_STUB_WRITE_PARCEL_ERR;
300         }
301     }
302     return (size == 0) ? IPC_STUB_INVALID_DATA_ERR : ERR_NONE;
303 }
304 
OnGetFile(MessageParcel & data,MessageParcel & reply)305 int32_t WallpaperServiceStub::OnGetFile(MessageParcel &data, MessageParcel &reply)
306 {
307     HILOG_DEBUG("WallpaperServiceStub::OnGetFile start.");
308     int32_t wallpaperType = data.ReadInt32();
309     int32_t wallpaperFd = INVALID_FD;
310     ErrorCode wallpaperErrorCode = GetFile(wallpaperType, wallpaperFd);
311     if (!reply.WriteInt32(static_cast<int32_t>(wallpaperErrorCode))) {
312         HILOG_ERROR("WriteInt32 fail!");
313         if (wallpaperFd > INVALID_FD) {
314             close(wallpaperFd);
315         }
316         return IPC_STUB_WRITE_PARCEL_ERR;
317     }
318     if (wallpaperErrorCode == E_OK && !reply.WriteFileDescriptor(wallpaperFd)) {
319         HILOG_ERROR("WriteFileDescriptor fail!");
320         close(wallpaperFd);
321         return IPC_STUB_WRITE_PARCEL_ERR;
322     }
323     if (wallpaperFd > INVALID_FD) {
324         close(wallpaperFd);
325     }
326     return ERR_NONE;
327 }
328 
OnGetWallpaperId(MessageParcel & data,MessageParcel & reply)329 int32_t WallpaperServiceStub::OnGetWallpaperId(MessageParcel &data, MessageParcel &reply)
330 {
331     HILOG_DEBUG("WallpaperServiceStub::OnGetWallpaperId start.");
332     int32_t wallpaperType = data.ReadInt32();
333     int32_t wallpaperId = GetWallpaperId(wallpaperType);
334     if (!reply.WriteInt32(wallpaperId)) {
335         HILOG_ERROR("Write result data failed!");
336         return IPC_STUB_WRITE_PARCEL_ERR;
337     }
338     HILOG_INFO("End. Id[%{public}d]", wallpaperId);
339     return ERR_NONE;
340 }
341 
OnIsChangePermitted(MessageParcel & data,MessageParcel & reply)342 int32_t WallpaperServiceStub::OnIsChangePermitted(MessageParcel &data, MessageParcel &reply)
343 {
344     HILOG_DEBUG("WallpaperServiceStub::OnIsChangePermitted start.");
345     auto bResult = IsChangePermitted();
346     if (!reply.WriteBool(bResult)) {
347         HILOG_ERROR("Write result data failed!");
348         return IPC_STUB_WRITE_PARCEL_ERR;
349     }
350     return ERR_NONE;
351 }
352 
OnIsOperationAllowed(MessageParcel & data,MessageParcel & reply)353 int32_t WallpaperServiceStub::OnIsOperationAllowed(MessageParcel &data, MessageParcel &reply)
354 {
355     HILOG_DEBUG("WallpaperServiceStub::OnIsOperationAllowed start.");
356     auto bResult = IsOperationAllowed();
357     if (!reply.WriteBool(bResult)) {
358         HILOG_ERROR("Write result data failed!");
359         return IPC_STUB_WRITE_PARCEL_ERR;
360     }
361     return ERR_NONE;
362 }
363 
OnResetWallpaper(MessageParcel & data,MessageParcel & reply)364 int32_t WallpaperServiceStub::OnResetWallpaper(MessageParcel &data, MessageParcel &reply)
365 {
366     return OnResetWallpaperInner(data, reply, false);
367 }
368 
OnResetWallpaperV9(MessageParcel & data,MessageParcel & reply)369 int32_t WallpaperServiceStub::OnResetWallpaperV9(MessageParcel &data, MessageParcel &reply)
370 {
371     return OnResetWallpaperInner(data, reply, true);
372 }
373 
OnResetWallpaperInner(MessageParcel & data,MessageParcel & reply,bool isSystemApi)374 int32_t WallpaperServiceStub::OnResetWallpaperInner(MessageParcel &data, MessageParcel &reply, bool isSystemApi)
375 {
376     HILOG_DEBUG("WallpaperServiceStub::OnResetWallpaper start.");
377     int32_t wallpaperType = data.ReadInt32();
378     ErrorCode wallpaperErrorCode = E_UNKNOWN;
379     if (isSystemApi) {
380         wallpaperErrorCode = ResetWallpaperV9(wallpaperType);
381     } else {
382         wallpaperErrorCode = ResetWallpaper(wallpaperType);
383     }
384     if (!reply.WriteInt32(static_cast<int32_t>(wallpaperErrorCode))) {
385         HILOG_ERROR("Write result data failed!");
386         return IPC_STUB_WRITE_PARCEL_ERR;
387     }
388     return ERR_NONE;
389 }
390 
OnWallpaperOn(MessageParcel & data,MessageParcel & reply)391 int32_t WallpaperServiceStub::OnWallpaperOn(MessageParcel &data, MessageParcel &reply)
392 {
393     HILOG_DEBUG("WallpaperServiceStub::OnWallpaperOn in.");
394     std::string type = data.ReadString();
395     if (type.empty()) {
396         HILOG_ERROR("OnWallpaperOn type is empty after ipc.");
397         return IPC_STUB_INVALID_DATA_ERR;
398     }
399     sptr<IRemoteObject> remote = data.ReadRemoteObject();
400     if (remote == nullptr) {
401         HILOG_ERROR("OnWallpaperOn nullptr after ipc.");
402         return IPC_STUB_INVALID_DATA_ERR;
403     }
404 
405     sptr<IWallpaperEventListener> wallpaperListenerProxy = iface_cast<IWallpaperEventListener>(remote);
406     ErrorCode errorCode = On(type, std::move(wallpaperListenerProxy));
407     int32_t ret = static_cast<int32_t>(errorCode);
408     if (!reply.WriteInt32(ret)) {
409         HILOG_ERROR("WriteInt32 failed!");
410         return IPC_STUB_WRITE_PARCEL_ERR;
411     }
412     return ERR_NONE;
413 }
414 
OnWallpaperOff(MessageParcel & data,MessageParcel & reply)415 int32_t WallpaperServiceStub::OnWallpaperOff(MessageParcel &data, MessageParcel &reply)
416 {
417     HILOG_DEBUG("WallpaperServiceStub::OnWallpaperOff in.");
418     std::string type = data.ReadString();
419     if (type.empty()) {
420         HILOG_ERROR("OnWallpaperOff type is empty after ipc.");
421         return IPC_STUB_INVALID_DATA_ERR;
422     }
423     sptr<IRemoteObject> remote = data.ReadRemoteObject();
424     ErrorCode errorCode = E_UNKNOWN;
425     if (remote == nullptr) {
426         errorCode = Off(type, nullptr);
427     } else {
428         sptr<IWallpaperEventListener> wallpaperListenerProxy = iface_cast<IWallpaperEventListener>(remote);
429         errorCode = Off(type, std::move(wallpaperListenerProxy));
430     }
431     int32_t ret = static_cast<int32_t>(errorCode);
432     if (!reply.WriteInt32(ret)) {
433         HILOG_ERROR("WriteInt32 failed!");
434         return IPC_STUB_WRITE_PARCEL_ERR;
435     }
436     return ERR_NONE;
437 }
438 
OnSendEvent(MessageParcel & data,MessageParcel & reply)439 int32_t WallpaperServiceStub::OnSendEvent(MessageParcel &data, MessageParcel &reply)
440 {
441     HILOG_DEBUG("WallpaperServiceStub::OnSendEvent start.");
442     std::string eventType = data.ReadString();
443     auto result = SendEvent(eventType);
444     if (!reply.WriteInt32(result)) {
445         HILOG_ERROR("Write int is failed!");
446         return IPC_STUB_WRITE_PARCEL_ERR;
447     }
448     return ERR_NONE;
449 }
450 
OnRegisterWallpaperCallback(MessageParcel & data,MessageParcel & reply)451 int32_t WallpaperServiceStub::OnRegisterWallpaperCallback(MessageParcel &data, MessageParcel &reply)
452 {
453     HILOG_DEBUG("WallpaperServiceStub::OnRegisterWallpaperCallback start.");
454     sptr<IRemoteObject> object = data.ReadRemoteObject();
455     bool status = false;
456     if (object == nullptr) {
457         HILOG_ERROR("RegisterWallpaperCallback failed!");
458         return IPC_STUB_INVALID_DATA_ERR;
459     }
460     sptr<IWallpaperCallback> callbackProxy = iface_cast<IWallpaperCallback>(object);
461 
462     status = RegisterWallpaperCallback(callbackProxy);
463     int32_t ret = status ? static_cast<int32_t>(E_OK) : static_cast<int32_t>(E_DEAL_FAILED);
464     if (!reply.WriteInt32(ret)) {
465         HILOG_ERROR("WriteInt32 failed!");
466         return IPC_STUB_WRITE_PARCEL_ERR;
467     }
468     return ERR_NONE;
469 }
470 
VectorToPixelMap(std::vector<std::uint8_t> value)471 std::shared_ptr<OHOS::Media::PixelMap> WallpaperServiceStub::VectorToPixelMap(std::vector<std::uint8_t> value)
472 {
473     HILOG_DEBUG("VectorToPixelMap start.!");
474     if (value.size() == 0) {
475         return nullptr;
476     }
477     return std::shared_ptr<PixelMap>(PixelMap::DecodeTlv(value));
478 }
479 
OnSetAllWallpapers(MessageParcel & data,MessageParcel & reply)480 int32_t WallpaperServiceStub::OnSetAllWallpapers(MessageParcel &data, MessageParcel &reply)
481 {
482     HILOG_DEBUG("WallpaperServiceStub::OnSetAllWallpapers start.");
483     int32_t vectorSize = data.ReadInt32();
484     std::vector<WallpaperPictureInfo> wallpaperPictureInfos;
485     for (int32_t i = 0; i < vectorSize; i++) {
486         WallpaperPictureInfo wallpaperInfo;
487         int32_t foldStateVale = data.ReadInt32();
488         int32_t rotateStateVale = data.ReadInt32();
489         if (foldStateVale == static_cast<int32_t>(FoldState::NORMAL)) {
490             wallpaperInfo.foldState = NORMAL;
491         } else if (foldStateVale == static_cast<int32_t>(FoldState::UNFOLD_1)) {
492             wallpaperInfo.foldState = UNFOLD_1;
493         } else if (foldStateVale == static_cast<int32_t>(FoldState::UNFOLD_2)) {
494             wallpaperInfo.foldState = UNFOLD_2;
495         }
496         if (rotateStateVale == static_cast<int32_t>(RotateState::PORT)) {
497             wallpaperInfo.rotateState = PORT;
498         } else if (rotateStateVale == static_cast<int32_t>(RotateState::LAND)) {
499             wallpaperInfo.rotateState = LAND;
500         }
501         wallpaperInfo.fd = data.ReadFileDescriptor();
502         if (wallpaperInfo.fd < 0) {
503             CloseWallpaperInfoFd(wallpaperPictureInfos);
504             HILOG_ERROR("ReadFileDescriptor fail. fd[%{public}d]", wallpaperInfo.fd);
505             return IPC_STUB_INVALID_DATA_ERR;
506         }
507         wallpaperInfo.length = data.ReadInt32();
508         wallpaperPictureInfos.push_back(wallpaperInfo);
509     }
510     int32_t wallpaperType = data.ReadInt32();
511     ErrorCode wallpaperErrorCode = E_UNKNOWN;
512     wallpaperErrorCode = SetAllWallpapers(wallpaperPictureInfos, wallpaperType);
513     CloseWallpaperInfoFd(wallpaperPictureInfos);
514     if (!reply.WriteInt32(static_cast<int32_t>(wallpaperErrorCode))) {
515         HILOG_ERROR("WriteInt32 fail!");
516         return IPC_STUB_WRITE_PARCEL_ERR;
517     }
518     return ERR_NONE;
519 }
520 
OnGetCorrespondWallpaper(MessageParcel & data,MessageParcel & reply)521 int32_t WallpaperServiceStub::OnGetCorrespondWallpaper(MessageParcel &data, MessageParcel &reply)
522 {
523     HILOG_DEBUG("WallpaperServiceStub::GetPixelMap start.");
524     int32_t wallpaperType = data.ReadInt32();
525     int32_t foldState = data.ReadInt32();
526     int32_t rotateState = data.ReadInt32();
527     IWallpaperService::FdInfo fdInfo;
528     ErrorCode wallpaperErrorCode = E_UNKNOWN;
529     wallpaperErrorCode = GetCorrespondWallpaper(wallpaperType, foldState, rotateState, fdInfo);
530     HILOG_INFO(" OnGetCorrespondWallpaper wallpaperErrorCode = %{public}d", wallpaperErrorCode);
531     if (!reply.WriteInt32(static_cast<int32_t>(wallpaperErrorCode))) {
532         HILOG_ERROR("WriteInt32 fail!");
533         close(fdInfo.fd);
534         return IPC_STUB_WRITE_PARCEL_ERR;
535     }
536     if (wallpaperErrorCode == E_OK) {
537         if (!reply.WriteInt32(fdInfo.size)) {
538             HILOG_ERROR("WriteInt32 fail!");
539             close(fdInfo.fd);
540             return IPC_STUB_WRITE_PARCEL_ERR;
541         }
542         if (!reply.WriteFileDescriptor(fdInfo.fd)) {
543             HILOG_ERROR("WriteFileDescriptor fail!");
544             close(fdInfo.fd);
545             return IPC_STUB_WRITE_PARCEL_ERR;
546         }
547     }
548     close(fdInfo.fd);
549     return ERR_NONE;
550 }
551 
CloseWallpaperInfoFd(std::vector<WallpaperPictureInfo> wallpaperPictureInfos)552 void WallpaperServiceStub::CloseWallpaperInfoFd(std::vector<WallpaperPictureInfo> wallpaperPictureInfos)
553 {
554     for (auto &wallpaperInfo : wallpaperPictureInfos) {
555         if (wallpaperInfo.fd >= 0) {
556             close(wallpaperInfo.fd);
557         }
558     }
559 }
560 } // namespace WallpaperMgrService
561 } // namespace OHOS