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