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 std::string uri = data.ReadString();
150 int32_t wallpaperType = data.ReadInt32();
151 ErrorCode wallpaperErrorCode = SetCustomWallpaper(uri, wallpaperType);
152 if (!reply.WriteInt32(static_cast<int32_t>(wallpaperErrorCode))) {
153 HILOG_ERROR("Write int is failed");
154 return IPC_STUB_WRITE_PARCEL_ERR;
155 }
156 return ERR_NONE;
157 }
158
OnGetPixelMapV9(MessageParcel & data,MessageParcel & reply)159 int32_t WallpaperServiceStub::OnGetPixelMapV9(MessageParcel &data, MessageParcel &reply)
160 {
161 return OnGetPixelMapInner(data, reply, true);
162 }
163
OnGetPixelMapInner(MessageParcel & data,MessageParcel & reply,bool isSystemApi)164 int32_t WallpaperServiceStub::OnGetPixelMapInner(MessageParcel &data, MessageParcel &reply, bool isSystemApi)
165 {
166 HILOG_INFO("WallpaperServiceStub::GetPixelMap start.");
167
168 int32_t wallpaperType = data.ReadInt32();
169 IWallpaperService::FdInfo fdInfo;
170 ErrorCode wallpaperErrorCode = E_UNKNOWN;
171 if (isSystemApi) {
172 wallpaperErrorCode = GetPixelMapV9(wallpaperType, fdInfo);
173 } else {
174 wallpaperErrorCode = GetPixelMap(wallpaperType, fdInfo);
175 }
176 HILOG_INFO(" OnGetPixelMap wallpaperErrorCode = %{public}d", wallpaperErrorCode);
177 if (!reply.WriteInt32(static_cast<int32_t>(wallpaperErrorCode))) {
178 HILOG_ERROR("WriteInt32 fail");
179 close(fdInfo.fd);
180 return IPC_STUB_WRITE_PARCEL_ERR;
181 }
182 if (wallpaperErrorCode == E_OK) {
183 if (!reply.WriteInt32(fdInfo.size)) {
184 HILOG_ERROR("WriteInt32 fail");
185 close(fdInfo.fd);
186 return IPC_STUB_WRITE_PARCEL_ERR;
187 }
188 if (!reply.WriteFileDescriptor(fdInfo.fd)) {
189 HILOG_ERROR("WriteFileDescriptor fail");
190 close(fdInfo.fd);
191 return IPC_STUB_WRITE_PARCEL_ERR;
192 }
193 }
194 close(fdInfo.fd);
195 return ERR_NONE;
196 }
197
OnGetColors(MessageParcel & data,MessageParcel & reply)198 int32_t WallpaperServiceStub::OnGetColors(MessageParcel &data, MessageParcel &reply)
199 {
200 return OnGetColorsInner(data, reply, false);
201 }
202
OnGetColorsV9(MessageParcel & data,MessageParcel & reply)203 int32_t WallpaperServiceStub::OnGetColorsV9(MessageParcel &data, MessageParcel &reply)
204 {
205 return OnGetColorsInner(data, reply, true);
206 }
207
OnGetColorsInner(MessageParcel & data,MessageParcel & reply,bool isSystemApi)208 int32_t WallpaperServiceStub::OnGetColorsInner(MessageParcel &data, MessageParcel &reply, bool isSystemApi)
209 {
210 HILOG_INFO("WallpaperServiceStub::OnGetColors start.");
211 int32_t wallpaperType = data.ReadInt32();
212 std::vector<uint64_t> vecWallpaperColors;
213 ErrorCode wallpaperErrorCode = E_UNKNOWN;
214 if (isSystemApi) {
215 wallpaperErrorCode = GetColorsV9(wallpaperType, vecWallpaperColors);
216 } else {
217 wallpaperErrorCode = GetColors(wallpaperType, vecWallpaperColors);
218 }
219 auto size = vecWallpaperColors.size();
220 if (!reply.WriteInt32(static_cast<int32_t>(wallpaperErrorCode))) {
221 HILOG_ERROR("WriteInt32 fail");
222 return IPC_STUB_WRITE_PARCEL_ERR;
223 }
224 if (wallpaperErrorCode == E_OK) {
225 if (!reply.WriteUInt64Vector(vecWallpaperColors)) {
226 HILOG_ERROR("WallpaperServiceStub::OnGetColors WriteUInt64Vector error.");
227 return IPC_STUB_WRITE_PARCEL_ERR;
228 }
229 }
230 return (size == 0) ? IPC_STUB_INVALID_DATA_ERR : ERR_NONE;
231 }
232
OnGetFile(MessageParcel & data,MessageParcel & reply)233 int32_t WallpaperServiceStub::OnGetFile(MessageParcel &data, MessageParcel &reply)
234 {
235 HILOG_INFO("WallpaperServiceStub::OnGetFile start.");
236
237 int32_t wallpaperType = data.ReadInt32();
238 int32_t wallpaperFd = INVALID_FD;
239 ErrorCode wallpaperErrorCode = GetFile(wallpaperType, wallpaperFd);
240 if (!reply.WriteInt32(static_cast<int32_t>(wallpaperErrorCode))) {
241 HILOG_ERROR("WriteInt32 fail");
242 if (wallpaperFd > INVALID_FD) {
243 close(wallpaperFd);
244 }
245 return IPC_STUB_WRITE_PARCEL_ERR;
246 }
247 if (wallpaperErrorCode == E_OK && !reply.WriteFileDescriptor(wallpaperFd)) {
248 HILOG_ERROR("WriteFileDescriptor fail");
249 close(wallpaperFd);
250 return IPC_STUB_WRITE_PARCEL_ERR;
251 }
252 if (wallpaperFd > INVALID_FD) {
253 close(wallpaperFd);
254 }
255 return ERR_NONE;
256 }
257
OnGetWallpaperId(MessageParcel & data,MessageParcel & reply)258 int32_t WallpaperServiceStub::OnGetWallpaperId(MessageParcel &data, MessageParcel &reply)
259 {
260 HILOG_INFO("WallpaperServiceStub::OnGetWallpaperId start.");
261
262 int32_t wallpaperType = data.ReadInt32();
263 int32_t wallpaperId = GetWallpaperId(wallpaperType);
264 if (!reply.WriteInt32(wallpaperId)) {
265 HILOG_ERROR("Write result data failed");
266 return IPC_STUB_WRITE_PARCEL_ERR;
267 }
268 HILOG_INFO("End. Id[%{public}d]", wallpaperId);
269 return ERR_NONE;
270 }
271
OnIsChangePermitted(MessageParcel & data,MessageParcel & reply)272 int32_t WallpaperServiceStub::OnIsChangePermitted(MessageParcel &data, MessageParcel &reply)
273 {
274 HILOG_INFO("WallpaperServiceStub::OnIsChangePermitted start.");
275 auto bResult = IsChangePermitted();
276 if (!reply.WriteBool(bResult)) {
277 HILOG_ERROR("Write result data failed");
278 return IPC_STUB_WRITE_PARCEL_ERR;
279 }
280 return ERR_NONE;
281 }
282
OnIsOperationAllowed(MessageParcel & data,MessageParcel & reply)283 int32_t WallpaperServiceStub::OnIsOperationAllowed(MessageParcel &data, MessageParcel &reply)
284 {
285 HILOG_INFO("WallpaperServiceStub::OnIsOperationAllowed start.");
286 auto bResult = IsOperationAllowed();
287 if (!reply.WriteBool(bResult)) {
288 HILOG_ERROR("Write result data failed");
289 return IPC_STUB_WRITE_PARCEL_ERR;
290 }
291 return ERR_NONE;
292 }
293
OnResetWallpaper(MessageParcel & data,MessageParcel & reply)294 int32_t WallpaperServiceStub::OnResetWallpaper(MessageParcel &data, MessageParcel &reply)
295 {
296 return OnResetWallpaperInner(data, reply, false);
297 }
298
OnResetWallpaperV9(MessageParcel & data,MessageParcel & reply)299 int32_t WallpaperServiceStub::OnResetWallpaperV9(MessageParcel &data, MessageParcel &reply)
300 {
301 return OnResetWallpaperInner(data, reply, true);
302 }
303
OnResetWallpaperInner(MessageParcel & data,MessageParcel & reply,bool isSystemApi)304 int32_t WallpaperServiceStub::OnResetWallpaperInner(MessageParcel &data, MessageParcel &reply, bool isSystemApi)
305 {
306 HILOG_INFO("WallpaperServiceStub::OnResetWallpaper start.");
307 int32_t wallpaperType = data.ReadInt32();
308 ErrorCode wallpaperErrorCode = E_UNKNOWN;
309 if (isSystemApi) {
310 wallpaperErrorCode = ResetWallpaperV9(wallpaperType);
311 } else {
312 wallpaperErrorCode = ResetWallpaper(wallpaperType);
313 }
314 if (!reply.WriteInt32(static_cast<int32_t>(wallpaperErrorCode))) {
315 HILOG_ERROR("Write result data failed");
316 return IPC_STUB_WRITE_PARCEL_ERR;
317 }
318 return ERR_NONE;
319 }
320
OnWallpaperOn(MessageParcel & data,MessageParcel & reply)321 int32_t WallpaperServiceStub::OnWallpaperOn(MessageParcel &data, MessageParcel &reply)
322 {
323 HILOG_DEBUG("WallpaperServiceStub::OnWallpaperOn in");
324 std::string type = data.ReadString();
325 if (type.empty()) {
326 HILOG_ERROR("OnWallpaperOn type is empty after ipc");
327 return IPC_STUB_INVALID_DATA_ERR;
328 }
329 sptr<IRemoteObject> remote = data.ReadRemoteObject();
330 if (remote == nullptr) {
331 HILOG_ERROR("OnWallpaperOn nullptr after ipc");
332 return IPC_STUB_INVALID_DATA_ERR;
333 }
334
335 sptr<IWallpaperEventListener> wallpaperListenerProxy = iface_cast<IWallpaperEventListener>(remote);
336 ErrorCode errorCode = On(type, std::move(wallpaperListenerProxy));
337 int32_t ret = static_cast<int32_t>(errorCode);
338 if (!reply.WriteInt32(ret)) {
339 HILOG_ERROR("WriteInt32 failed");
340 return IPC_STUB_WRITE_PARCEL_ERR;
341 }
342 HILOG_DEBUG("WallpaperServiceStub::OnWallpaperOn out");
343 return ERR_NONE;
344 }
345
OnWallpaperOff(MessageParcel & data,MessageParcel & reply)346 int32_t WallpaperServiceStub::OnWallpaperOff(MessageParcel &data, MessageParcel &reply)
347 {
348 HILOG_DEBUG("WallpaperServiceStub::OnWallpaperOff in");
349 std::string type = data.ReadString();
350 if (type.empty()) {
351 HILOG_ERROR("OnWallpaperOff type is empty after ipc");
352 return IPC_STUB_INVALID_DATA_ERR;
353 }
354 sptr<IRemoteObject> remote = data.ReadRemoteObject();
355 ErrorCode errorCode = E_UNKNOWN;
356 if (remote == nullptr) {
357 errorCode = Off(type, nullptr);
358 } else {
359 sptr<IWallpaperEventListener> wallpaperListenerProxy = iface_cast<IWallpaperEventListener>(remote);
360 errorCode = Off(type, std::move(wallpaperListenerProxy));
361 }
362 int32_t ret = static_cast<int32_t>(errorCode);
363 if (!reply.WriteInt32(ret)) {
364 HILOG_ERROR("WriteInt32 failed");
365 return IPC_STUB_WRITE_PARCEL_ERR;
366 }
367 HILOG_DEBUG("WallpaperServiceStub::OnWallpaperOff out");
368 return ERR_NONE;
369 }
370
OnSendEvent(MessageParcel & data,MessageParcel & reply)371 int32_t WallpaperServiceStub::OnSendEvent(MessageParcel &data, MessageParcel &reply)
372 {
373 HILOG_DEBUG("WallpaperServiceStub::OnSendEvent start.");
374 std::string eventType = data.ReadString();
375 auto result = SendEvent(eventType);
376 if (!reply.WriteInt32(result)) {
377 HILOG_ERROR("Write int is failed");
378 return IPC_STUB_WRITE_PARCEL_ERR;
379 }
380 return ERR_NONE;
381 }
382
OnRegisterWallpaperCallback(MessageParcel & data,MessageParcel & reply)383 int32_t WallpaperServiceStub::OnRegisterWallpaperCallback(MessageParcel &data, MessageParcel &reply)
384 {
385 HILOG_INFO("WallpaperServiceStub::OnRegisterWallpaperCallback start");
386 sptr<IRemoteObject> object = data.ReadRemoteObject();
387 bool status = false;
388 if (object == nullptr) {
389 HILOG_ERROR("RegisterWallpaperCallback failed");
390 return IPC_STUB_INVALID_DATA_ERR;
391 }
392 sptr<IWallpaperCallback> callbackProxy = iface_cast<IWallpaperCallback>(object);
393
394 status = RegisterWallpaperCallback(callbackProxy);
395 int32_t ret = status ? static_cast<int32_t>(E_OK) : static_cast<int32_t>(E_DEAL_FAILED);
396 if (!reply.WriteInt32(ret)) {
397 HILOG_ERROR("WriteInt32 failed");
398 return IPC_STUB_WRITE_PARCEL_ERR;
399 }
400 return ERR_NONE;
401 }
402 } // namespace WallpaperMgrService
403 } // namespace OHOS