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 "wallpaper_service_stub.h"
17
18 #include <unistd.h>
19
20 #include "ashmem.h"
21 #include "hilog_wrapper.h"
22 #include "ipc_skeleton.h"
23 #include "memory_guard.h"
24 #include "parcel.h"
25 #include "pixel_map.h"
26 #include "wallpaper_common.h"
27
28 namespace OHOS {
29 namespace WallpaperMgrService {
30 using namespace OHOS::HiviewDFX;
31 using namespace OHOS::Media;
32
WallpaperServiceStub()33 WallpaperServiceStub::WallpaperServiceStub()
34 {
35 memberFuncMap_[WallpaperServiceIpcInterfaceCode::SET_WALLPAPER] = &WallpaperServiceStub::OnSetWallpaper;
36 memberFuncMap_[WallpaperServiceIpcInterfaceCode::GET_PIXELMAP] = &WallpaperServiceStub::OnGetPixelMap;
37 memberFuncMap_[WallpaperServiceIpcInterfaceCode::GET_COLORS] = &WallpaperServiceStub::OnGetColors;
38 memberFuncMap_[WallpaperServiceIpcInterfaceCode::GET_WALLPAPER_ID] = &WallpaperServiceStub::OnGetWallpaperId;
39 memberFuncMap_[WallpaperServiceIpcInterfaceCode::GET_FILE] = &WallpaperServiceStub::OnGetFile;
40 memberFuncMap_[WallpaperServiceIpcInterfaceCode::ON] = &WallpaperServiceStub::OnWallpaperOn;
41 memberFuncMap_[WallpaperServiceIpcInterfaceCode::OFF] = &WallpaperServiceStub::OnWallpaperOff;
42 memberFuncMap_[WallpaperServiceIpcInterfaceCode::IS_CHANGE_PERMITTED] = &WallpaperServiceStub::OnIsChangePermitted;
43 memberFuncMap_[WallpaperServiceIpcInterfaceCode::IS_OPERATION_ALLOWED] =
44 &WallpaperServiceStub::OnIsOperationAllowed;
45 memberFuncMap_[WallpaperServiceIpcInterfaceCode::RESET_WALLPAPER] = &WallpaperServiceStub::OnResetWallpaper;
46 memberFuncMap_[WallpaperServiceIpcInterfaceCode::REGISTER_CALLBACK] =
47 &WallpaperServiceStub::OnRegisterWallpaperCallback;
48 memberFuncMap_[WallpaperServiceIpcInterfaceCode::SET_WALLPAPER_V9] = &WallpaperServiceStub::OnSetWallpaperV9;
49 memberFuncMap_[WallpaperServiceIpcInterfaceCode::GET_PIXELMAP_V9] = &WallpaperServiceStub::OnGetPixelMapV9;
50 memberFuncMap_[WallpaperServiceIpcInterfaceCode::GET_COLORS_V9] = &WallpaperServiceStub::OnGetColorsV9;
51 memberFuncMap_[WallpaperServiceIpcInterfaceCode::RESET_WALLPAPER_V9] = &WallpaperServiceStub::OnResetWallpaperV9;
52 memberFuncMap_[WallpaperServiceIpcInterfaceCode::SET_VIDEO] = &WallpaperServiceStub::OnSetVideo;
53 memberFuncMap_[WallpaperServiceIpcInterfaceCode::SET_CUSTOM] = &WallpaperServiceStub::OnSetCustomWallpaper;
54 memberFuncMap_[WallpaperServiceIpcInterfaceCode::SEND_EVENT] = &WallpaperServiceStub::OnSendEvent;
55 }
56
~WallpaperServiceStub()57 WallpaperServiceStub::~WallpaperServiceStub()
58 {
59 memberFuncMap_.clear();
60 }
61
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)62 int32_t WallpaperServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
63 MessageOption &option)
64 {
65 MemoryGuard cacheGuard;
66 HILOG_INFO("start##ret = %{public}u", code);
67 std::u16string myDescriptor = WallpaperServiceStub::GetDescriptor();
68 std::u16string remoteDescriptor = data.ReadInterfaceToken();
69 if (myDescriptor != remoteDescriptor) {
70 HILOG_ERROR("end##descriptor checked fail");
71 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
72 }
73 pid_t p = IPCSkeleton::GetCallingPid();
74 pid_t p1 = IPCSkeleton::GetCallingUid();
75 HILOG_INFO("CallingPid = %{public}d, CallingUid = %{public}d, code = %{public}u", p, p1, code);
76 if (code >= static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::SET_WALLPAPER) &&
77 code <= static_cast<uint32_t>(WallpaperServiceIpcInterfaceCode::SEND_EVENT)) {
78 auto itFunc = memberFuncMap_.find(static_cast<WallpaperServiceIpcInterfaceCode>(code));
79 if (itFunc != memberFuncMap_.end()) {
80 auto memberFunc = itFunc->second;
81 if (memberFunc != nullptr) {
82 return (this->*memberFunc)(data, reply);
83 }
84 }
85 }
86 int32_t ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
87 HILOG_INFO("end##ret = %{public}d", ret);
88 return ret;
89 }
90
OnSetWallpaper(MessageParcel & data,MessageParcel & reply)91 int32_t WallpaperServiceStub::OnSetWallpaper(MessageParcel &data, MessageParcel &reply)
92 {
93 return OnSetWallpaperInner(data, reply, false);
94 }
95
OnSetWallpaperV9(MessageParcel & data,MessageParcel & reply)96 int32_t WallpaperServiceStub::OnSetWallpaperV9(MessageParcel &data, MessageParcel &reply)
97 {
98 return OnSetWallpaperInner(data, reply, true);
99 }
100
OnSetWallpaperInner(MessageParcel & data,MessageParcel & reply,bool isSystemApi)101 int32_t WallpaperServiceStub::OnSetWallpaperInner(MessageParcel &data, MessageParcel &reply, bool isSystemApi)
102 {
103 HILOG_INFO("WallpaperServiceStub::SetWallpaper start.");
104
105 int32_t fd = data.ReadFileDescriptor();
106 if (fd < 0) {
107 HILOG_INFO("ReadFileDescriptor fail. fd[%{public}d]", fd);
108 return IPC_STUB_INVALID_DATA_ERR;
109 }
110 int32_t wallpaperType = data.ReadInt32();
111 int32_t length = data.ReadInt32();
112 ErrorCode wallpaperErrorCode = E_UNKNOWN;
113 if (isSystemApi) {
114 wallpaperErrorCode = SetWallpaperV9(fd, wallpaperType, length);
115 } else {
116 wallpaperErrorCode = SetWallpaper(fd, wallpaperType, length);
117 }
118 close(fd);
119 if (!reply.WriteInt32(static_cast<int32_t>(wallpaperErrorCode))) {
120 HILOG_ERROR("WriteInt32 fail");
121 return IPC_STUB_WRITE_PARCEL_ERR;
122 }
123 return ERR_NONE;
124 }
125
OnGetPixelMap(MessageParcel & data,MessageParcel & reply)126 int32_t WallpaperServiceStub::OnGetPixelMap(MessageParcel &data, MessageParcel &reply)
127 {
128 return OnGetPixelMapInner(data, reply, false);
129 }
130
OnSetVideo(MessageParcel & data,MessageParcel & reply)131 int32_t WallpaperServiceStub::OnSetVideo(MessageParcel &data, MessageParcel &reply)
132 {
133 HILOG_DEBUG("WallpaperServiceStub::OnSetVideo start.");
134 int32_t fd = data.ReadFileDescriptor();
135 int32_t wallpaperType = data.ReadInt32();
136 int32_t length = data.ReadInt32();
137 ErrorCode wallpaperErrorCode = SetVideo(fd, wallpaperType, length);
138 close(fd);
139 if (!reply.WriteInt32(static_cast<int32_t>(wallpaperErrorCode))) {
140 HILOG_ERROR("Write int is failed");
141 return IPC_STUB_WRITE_PARCEL_ERR;
142 }
143 return ERR_NONE;
144 }
145
OnSetCustomWallpaper(MessageParcel & data,MessageParcel & reply)146 int32_t WallpaperServiceStub::OnSetCustomWallpaper(MessageParcel &data, MessageParcel &reply)
147 {
148 HILOG_DEBUG("WallpaperServiceStub::SetCustomWallpaper start.");
149 auto fd = data.ReadFileDescriptor();
150 int32_t wallpaperType = data.ReadInt32();
151 int32_t length = data.ReadInt32();
152 ErrorCode wallpaperErrorCode = SetCustomWallpaper(fd, wallpaperType, length);
153 close(fd);
154 if (!reply.WriteInt32(static_cast<int32_t>(wallpaperErrorCode))) {
155 HILOG_ERROR("Write int is failed");
156 return IPC_STUB_WRITE_PARCEL_ERR;
157 }
158 return ERR_NONE;
159 }
160
OnGetPixelMapV9(MessageParcel & data,MessageParcel & reply)161 int32_t WallpaperServiceStub::OnGetPixelMapV9(MessageParcel &data, MessageParcel &reply)
162 {
163 return OnGetPixelMapInner(data, reply, true);
164 }
165
OnGetPixelMapInner(MessageParcel & data,MessageParcel & reply,bool isSystemApi)166 int32_t WallpaperServiceStub::OnGetPixelMapInner(MessageParcel &data, MessageParcel &reply, bool isSystemApi)
167 {
168 HILOG_INFO("WallpaperServiceStub::GetPixelMap start.");
169
170 int32_t wallpaperType = data.ReadInt32();
171 IWallpaperService::FdInfo fdInfo;
172 ErrorCode wallpaperErrorCode = E_UNKNOWN;
173 if (isSystemApi) {
174 wallpaperErrorCode = GetPixelMapV9(wallpaperType, fdInfo);
175 } else {
176 wallpaperErrorCode = GetPixelMap(wallpaperType, fdInfo);
177 }
178 HILOG_INFO(" OnGetPixelMap wallpaperErrorCode = %{public}d", wallpaperErrorCode);
179 if (!reply.WriteInt32(static_cast<int32_t>(wallpaperErrorCode))) {
180 HILOG_ERROR("WriteInt32 fail");
181 close(fdInfo.fd);
182 return IPC_STUB_WRITE_PARCEL_ERR;
183 }
184 if (wallpaperErrorCode == E_OK) {
185 if (!reply.WriteInt32(fdInfo.size)) {
186 HILOG_ERROR("WriteInt32 fail");
187 close(fdInfo.fd);
188 return IPC_STUB_WRITE_PARCEL_ERR;
189 }
190 if (!reply.WriteFileDescriptor(fdInfo.fd)) {
191 HILOG_ERROR("WriteFileDescriptor fail");
192 close(fdInfo.fd);
193 return IPC_STUB_WRITE_PARCEL_ERR;
194 }
195 }
196 close(fdInfo.fd);
197 return ERR_NONE;
198 }
199
OnGetColors(MessageParcel & data,MessageParcel & reply)200 int32_t WallpaperServiceStub::OnGetColors(MessageParcel &data, MessageParcel &reply)
201 {
202 return OnGetColorsInner(data, reply, false);
203 }
204
OnGetColorsV9(MessageParcel & data,MessageParcel & reply)205 int32_t WallpaperServiceStub::OnGetColorsV9(MessageParcel &data, MessageParcel &reply)
206 {
207 return OnGetColorsInner(data, reply, true);
208 }
209
OnGetColorsInner(MessageParcel & data,MessageParcel & reply,bool isSystemApi)210 int32_t WallpaperServiceStub::OnGetColorsInner(MessageParcel &data, MessageParcel &reply, bool isSystemApi)
211 {
212 HILOG_INFO("WallpaperServiceStub::OnGetColors start.");
213 int32_t wallpaperType = data.ReadInt32();
214 std::vector<uint64_t> vecWallpaperColors;
215 ErrorCode wallpaperErrorCode = E_UNKNOWN;
216 if (isSystemApi) {
217 wallpaperErrorCode = GetColorsV9(wallpaperType, vecWallpaperColors);
218 } else {
219 wallpaperErrorCode = GetColors(wallpaperType, vecWallpaperColors);
220 }
221 auto size = vecWallpaperColors.size();
222 if (!reply.WriteInt32(static_cast<int32_t>(wallpaperErrorCode))) {
223 HILOG_ERROR("WriteInt32 fail");
224 return IPC_STUB_WRITE_PARCEL_ERR;
225 }
226 if (wallpaperErrorCode == E_OK) {
227 if (!reply.WriteUInt64Vector(vecWallpaperColors)) {
228 HILOG_ERROR("WallpaperServiceStub::OnGetColors WriteUInt64Vector error.");
229 return IPC_STUB_WRITE_PARCEL_ERR;
230 }
231 }
232 return (size == 0) ? IPC_STUB_INVALID_DATA_ERR : ERR_NONE;
233 }
234
OnGetFile(MessageParcel & data,MessageParcel & reply)235 int32_t WallpaperServiceStub::OnGetFile(MessageParcel &data, MessageParcel &reply)
236 {
237 HILOG_INFO("WallpaperServiceStub::OnGetFile start.");
238
239 int32_t wallpaperType = data.ReadInt32();
240 int32_t wallpaperFd = INVALID_FD;
241 ErrorCode wallpaperErrorCode = GetFile(wallpaperType, wallpaperFd);
242 if (!reply.WriteInt32(static_cast<int32_t>(wallpaperErrorCode))) {
243 HILOG_ERROR("WriteInt32 fail");
244 if (wallpaperFd > INVALID_FD) {
245 close(wallpaperFd);
246 }
247 return IPC_STUB_WRITE_PARCEL_ERR;
248 }
249 if (wallpaperErrorCode == E_OK && !reply.WriteFileDescriptor(wallpaperFd)) {
250 HILOG_ERROR("WriteFileDescriptor fail");
251 close(wallpaperFd);
252 return IPC_STUB_WRITE_PARCEL_ERR;
253 }
254 if (wallpaperFd > INVALID_FD) {
255 close(wallpaperFd);
256 }
257 return ERR_NONE;
258 }
259
OnGetWallpaperId(MessageParcel & data,MessageParcel & reply)260 int32_t WallpaperServiceStub::OnGetWallpaperId(MessageParcel &data, MessageParcel &reply)
261 {
262 HILOG_INFO("WallpaperServiceStub::OnGetWallpaperId start.");
263
264 int32_t wallpaperType = data.ReadInt32();
265 int32_t wallpaperId = GetWallpaperId(wallpaperType);
266 if (!reply.WriteInt32(wallpaperId)) {
267 HILOG_ERROR("Write result data failed");
268 return IPC_STUB_WRITE_PARCEL_ERR;
269 }
270 HILOG_INFO("End. Id[%{public}d]", wallpaperId);
271 return ERR_NONE;
272 }
273
OnIsChangePermitted(MessageParcel & data,MessageParcel & reply)274 int32_t WallpaperServiceStub::OnIsChangePermitted(MessageParcel &data, MessageParcel &reply)
275 {
276 HILOG_INFO("WallpaperServiceStub::OnIsChangePermitted start.");
277 auto bResult = IsChangePermitted();
278 if (!reply.WriteBool(bResult)) {
279 HILOG_ERROR("Write result data failed");
280 return IPC_STUB_WRITE_PARCEL_ERR;
281 }
282 return ERR_NONE;
283 }
284
OnIsOperationAllowed(MessageParcel & data,MessageParcel & reply)285 int32_t WallpaperServiceStub::OnIsOperationAllowed(MessageParcel &data, MessageParcel &reply)
286 {
287 HILOG_INFO("WallpaperServiceStub::OnIsOperationAllowed start.");
288 auto bResult = IsOperationAllowed();
289 if (!reply.WriteBool(bResult)) {
290 HILOG_ERROR("Write result data failed");
291 return IPC_STUB_WRITE_PARCEL_ERR;
292 }
293 return ERR_NONE;
294 }
295
OnResetWallpaper(MessageParcel & data,MessageParcel & reply)296 int32_t WallpaperServiceStub::OnResetWallpaper(MessageParcel &data, MessageParcel &reply)
297 {
298 return OnResetWallpaperInner(data, reply, false);
299 }
300
OnResetWallpaperV9(MessageParcel & data,MessageParcel & reply)301 int32_t WallpaperServiceStub::OnResetWallpaperV9(MessageParcel &data, MessageParcel &reply)
302 {
303 return OnResetWallpaperInner(data, reply, true);
304 }
305
OnResetWallpaperInner(MessageParcel & data,MessageParcel & reply,bool isSystemApi)306 int32_t WallpaperServiceStub::OnResetWallpaperInner(MessageParcel &data, MessageParcel &reply, bool isSystemApi)
307 {
308 HILOG_INFO("WallpaperServiceStub::OnResetWallpaper start.");
309 int32_t wallpaperType = data.ReadInt32();
310 ErrorCode wallpaperErrorCode = E_UNKNOWN;
311 if (isSystemApi) {
312 wallpaperErrorCode = ResetWallpaperV9(wallpaperType);
313 } else {
314 wallpaperErrorCode = ResetWallpaper(wallpaperType);
315 }
316 if (!reply.WriteInt32(static_cast<int32_t>(wallpaperErrorCode))) {
317 HILOG_ERROR("Write result data failed");
318 return IPC_STUB_WRITE_PARCEL_ERR;
319 }
320 return ERR_NONE;
321 }
322
OnWallpaperOn(MessageParcel & data,MessageParcel & reply)323 int32_t WallpaperServiceStub::OnWallpaperOn(MessageParcel &data, MessageParcel &reply)
324 {
325 HILOG_DEBUG("WallpaperServiceStub::OnWallpaperOn in");
326 std::string type = data.ReadString();
327 if (type.empty()) {
328 HILOG_ERROR("OnWallpaperOn type is empty after ipc");
329 return IPC_STUB_INVALID_DATA_ERR;
330 }
331 sptr<IRemoteObject> remote = data.ReadRemoteObject();
332 if (remote == nullptr) {
333 HILOG_ERROR("OnWallpaperOn nullptr after ipc");
334 return IPC_STUB_INVALID_DATA_ERR;
335 }
336
337 sptr<IWallpaperEventListener> wallpaperListenerProxy = iface_cast<IWallpaperEventListener>(remote);
338 ErrorCode errorCode = On(type, std::move(wallpaperListenerProxy));
339 int32_t ret = static_cast<int32_t>(errorCode);
340 if (!reply.WriteInt32(ret)) {
341 HILOG_ERROR("WriteInt32 failed");
342 return IPC_STUB_WRITE_PARCEL_ERR;
343 }
344 HILOG_DEBUG("WallpaperServiceStub::OnWallpaperOn out");
345 return ERR_NONE;
346 }
347
OnWallpaperOff(MessageParcel & data,MessageParcel & reply)348 int32_t WallpaperServiceStub::OnWallpaperOff(MessageParcel &data, MessageParcel &reply)
349 {
350 HILOG_DEBUG("WallpaperServiceStub::OnWallpaperOff in");
351 std::string type = data.ReadString();
352 if (type.empty()) {
353 HILOG_ERROR("OnWallpaperOff type is empty after ipc");
354 return IPC_STUB_INVALID_DATA_ERR;
355 }
356 sptr<IRemoteObject> remote = data.ReadRemoteObject();
357 ErrorCode errorCode = E_UNKNOWN;
358 if (remote == nullptr) {
359 errorCode = Off(type, nullptr);
360 } else {
361 sptr<IWallpaperEventListener> wallpaperListenerProxy = iface_cast<IWallpaperEventListener>(remote);
362 errorCode = Off(type, std::move(wallpaperListenerProxy));
363 }
364 int32_t ret = static_cast<int32_t>(errorCode);
365 if (!reply.WriteInt32(ret)) {
366 HILOG_ERROR("WriteInt32 failed");
367 return IPC_STUB_WRITE_PARCEL_ERR;
368 }
369 HILOG_DEBUG("WallpaperServiceStub::OnWallpaperOff out");
370 return ERR_NONE;
371 }
372
OnSendEvent(MessageParcel & data,MessageParcel & reply)373 int32_t WallpaperServiceStub::OnSendEvent(MessageParcel &data, MessageParcel &reply)
374 {
375 HILOG_DEBUG("WallpaperServiceStub::OnSendEvent start.");
376 std::string eventType = data.ReadString();
377 auto result = SendEvent(eventType);
378 if (!reply.WriteInt32(result)) {
379 HILOG_ERROR("Write int is failed");
380 return IPC_STUB_WRITE_PARCEL_ERR;
381 }
382 return ERR_NONE;
383 }
384
OnRegisterWallpaperCallback(MessageParcel & data,MessageParcel & reply)385 int32_t WallpaperServiceStub::OnRegisterWallpaperCallback(MessageParcel &data, MessageParcel &reply)
386 {
387 HILOG_INFO("WallpaperServiceStub::OnRegisterWallpaperCallback start");
388 sptr<IRemoteObject> object = data.ReadRemoteObject();
389 bool status = false;
390 if (object == nullptr) {
391 HILOG_ERROR("RegisterWallpaperCallback failed");
392 return IPC_STUB_INVALID_DATA_ERR;
393 }
394 sptr<IWallpaperCallback> callbackProxy = iface_cast<IWallpaperCallback>(object);
395
396 status = RegisterWallpaperCallback(callbackProxy);
397 int32_t ret = status ? static_cast<int32_t>(E_OK) : static_cast<int32_t>(E_DEAL_FAILED);
398 if (!reply.WriteInt32(ret)) {
399 HILOG_ERROR("WriteInt32 failed");
400 return IPC_STUB_WRITE_PARCEL_ERR;
401 }
402 return ERR_NONE;
403 }
404 } // namespace WallpaperMgrService
405 } // namespace OHOS