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 "parcel.h"
24 #include "pixel_map.h"
25 #include "wallpaper_common.h"
26 #include "memory_guard.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_[SET_WALLPAPER_URI_FD] = &WallpaperServiceStub::OnSetWallpaperUriByFD;
36 memberFuncMap_[SET_WALLPAPER_MAP] = &WallpaperServiceStub::OnSetWallpaperByMap;
37 memberFuncMap_[GET_PIXELMAP] = &WallpaperServiceStub::OnGetPixelMap;
38 memberFuncMap_[GET_COLORS] = &WallpaperServiceStub::OnGetColors;
39 memberFuncMap_[GET_WALLPAPER_ID] = &WallpaperServiceStub::OnGetWallpaperId;
40 memberFuncMap_[GET_FILE] = &WallpaperServiceStub::OnGetFile;
41 memberFuncMap_[GET_WALLPAPER_MIN_HEIGHT] = &WallpaperServiceStub::OnGetWallpaperMinHeight;
42 memberFuncMap_[GET_WALLPAPER_MIN_WIDTH] = &WallpaperServiceStub::OnGetWallpaperMinWidth;
43 memberFuncMap_[ON] = &WallpaperServiceStub::OnWallpaperOn;
44 memberFuncMap_[OFF] = &WallpaperServiceStub::OnWallpaperOff;
45 memberFuncMap_[IS_CHANGE_PERMITTED] = &WallpaperServiceStub::OnIsChangePermitted;
46 memberFuncMap_[IS_OPERATION_ALLOWED] = &WallpaperServiceStub::OnIsOperationAllowed;
47 memberFuncMap_[RESET_WALLPAPER] = &WallpaperServiceStub::OnResetWallpaper;
48 memberFuncMap_[REGISTER_CALLBACK] = &WallpaperServiceStub::OnRegisterWallpaperCallback;
49 }
50
~WallpaperServiceStub()51 WallpaperServiceStub::~WallpaperServiceStub()
52 {
53 memberFuncMap_.clear();
54 }
55
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)56 int32_t WallpaperServiceStub::OnRemoteRequest(
57 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
58 {
59 MemoryGuard cacheGuard;
60 HILOG_INFO(" start##ret = %{public}u", code);
61 std::u16string myDescripter = WallpaperServiceStub::GetDescriptor();
62 std::u16string remoteDescripter = data.ReadInterfaceToken();
63 if (myDescripter != remoteDescripter) {
64 HILOG_ERROR(" end##descriptor checked fail");
65 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
66 }
67 pid_t p = IPCSkeleton::GetCallingPid();
68 pid_t p1 = IPCSkeleton::GetCallingUid();
69 HILOG_INFO("CallingPid = %{public}d, CallingUid = %{public}d, code = %{public}u", p, p1, code);
70 auto itFunc = memberFuncMap_.find(code);
71 if (itFunc != memberFuncMap_.end()) {
72 auto memberFunc = itFunc->second;
73 if (memberFunc != nullptr) {
74 return (this->*memberFunc)(data, reply);
75 }
76 }
77 int ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
78 HILOG_INFO(" end##ret = %{public}d", ret);
79 return ret;
80 }
OnSetWallpaperByMap(MessageParcel & data,MessageParcel & reply)81 int32_t WallpaperServiceStub::OnSetWallpaperByMap(MessageParcel &data, MessageParcel &reply)
82 {
83 HILOG_INFO("WallpaperServiceStub::SetWallpaperMap start.");
84
85 int fd = data.ReadFileDescriptor();
86 int wallpaperType = data.ReadInt32();
87 int length = data.ReadInt32();
88 int32_t wallpaperErrorCode = SetWallpaperByMap(fd, wallpaperType, length);
89 close(fd);
90 if (!reply.WriteInt32(wallpaperErrorCode)) {
91 HILOG_ERROR("WriteInt32 fail");
92 return IPC_STUB_WRITE_PARCEL_ERR;
93 }
94 return ERR_NONE;
95 }
OnSetWallpaperUriByFD(MessageParcel & data,MessageParcel & reply)96 int32_t WallpaperServiceStub::OnSetWallpaperUriByFD(MessageParcel &data, MessageParcel &reply)
97 {
98 HILOG_INFO("WallpaperServiceStub::SetWallpaperUri start.");
99
100 int fd = data.ReadFileDescriptor();
101 if (fd < 0) {
102 HILOG_INFO("ReadFileDescriptor fail. fd[%{public}d]", fd);
103 return IPC_STUB_INVALID_DATA_ERR;
104 }
105 int wallpaperType = data.ReadInt32();
106 HILOG_INFO("wallpaperType= %{public}d", wallpaperType);
107 int length = data.ReadInt32();
108 HILOG_INFO("SetWallpaperByFD start");
109 int32_t wallpaperErrorCode = SetWallpaperByFD(fd, wallpaperType, length);
110 close(fd);
111 if (!reply.WriteInt32(wallpaperErrorCode)) {
112 HILOG_ERROR("WriteInt32 fail");
113 return IPC_STUB_WRITE_PARCEL_ERR;
114 }
115 return ERR_NONE;
116 }
117
OnGetPixelMap(MessageParcel & data,MessageParcel & reply)118 int32_t WallpaperServiceStub::OnGetPixelMap(MessageParcel &data, MessageParcel &reply)
119 {
120 HILOG_INFO("WallpaperServiceStub::GetPixelMap start.");
121
122 int wallpaperType = data.ReadInt32();
123 IWallpaperService::FdInfo fdInfo;
124 int wallpaperErrorCode = GetPixelMap(wallpaperType, fdInfo);
125 HILOG_INFO(" OnGetPixelMap wallpaperErrorCode = %{public}d", wallpaperErrorCode);
126 if (!reply.WriteInt32(wallpaperErrorCode)) {
127 HILOG_ERROR("WriteInt32 fail");
128 return IPC_STUB_WRITE_PARCEL_ERR;
129 }
130 if (wallpaperErrorCode == static_cast<int32_t>(E_OK)) {
131 if (!reply.WriteInt32(fdInfo.size)) {
132 HILOG_ERROR("WriteInt32 fail");
133 return IPC_STUB_WRITE_PARCEL_ERR;
134 }
135 if (!reply.WriteFileDescriptor(fdInfo.fd)) {
136 HILOG_ERROR("WriteFileDescriptor fail");
137 close(fdInfo.fd);
138 return IPC_STUB_WRITE_PARCEL_ERR;
139 }
140 close(fdInfo.fd);
141 }
142 return ERR_NONE;
143 }
144
OnGetColors(MessageParcel & data,MessageParcel & reply)145 int32_t WallpaperServiceStub::OnGetColors(MessageParcel &data, MessageParcel &reply)
146 {
147 HILOG_INFO("WallpaperServiceStub::OnGetColors start.");
148 int wallpaperType = data.ReadInt32();
149 std::vector<uint64_t> vecWallpaperColors = GetColors(wallpaperType);
150 auto size = vecWallpaperColors.size();
151 if (!reply.WriteUInt64Vector(vecWallpaperColors)) {
152 HILOG_ERROR("WallpaperServiceStub::OnGetColors WriteUInt64Vector error.");
153 return IPC_STUB_WRITE_PARCEL_ERR;
154 }
155 return (size == 0) ? IPC_STUB_INVALID_DATA_ERR : ERR_NONE;
156 }
157
OnGetFile(MessageParcel & data,MessageParcel & reply)158 int32_t WallpaperServiceStub::OnGetFile(MessageParcel &data, MessageParcel &reply)
159 {
160 HILOG_INFO("WallpaperServiceStub::OnGetFile start.");
161
162 int32_t wallpaperType = data.ReadInt32();
163 int32_t wallpaperFd = INVALID_FD;
164 int32_t wallpaperErrorCode = GetFile(wallpaperType, wallpaperFd);
165 if (!reply.WriteInt32(wallpaperErrorCode)) {
166 HILOG_ERROR("WriteInt32 fail");
167 if (wallpaperFd > INVALID_FD) {
168 close(wallpaperFd);
169 }
170 return IPC_STUB_WRITE_PARCEL_ERR;
171 }
172 if (wallpaperErrorCode == static_cast<int32_t>(E_OK) && !reply.WriteFileDescriptor(wallpaperFd)) {
173 HILOG_ERROR("WriteFileDescriptor fail");
174 close(wallpaperFd);
175 return IPC_STUB_WRITE_PARCEL_ERR;
176 }
177 if (wallpaperFd > INVALID_FD) {
178 close(wallpaperFd);
179 }
180 return ERR_NONE;
181 }
182
OnGetWallpaperId(MessageParcel & data,MessageParcel & reply)183 int32_t WallpaperServiceStub::OnGetWallpaperId(MessageParcel &data, MessageParcel &reply)
184 {
185 HILOG_INFO("WallpaperServiceStub::OnGetWallpaperId start.");
186
187 int wallpaperType = data.ReadInt32();
188 int wallpaerid = GetWallpaperId(wallpaperType);
189 if (!reply.WriteInt32(wallpaerid)) {
190 HILOG_ERROR("Write result data failed");
191 return IPC_STUB_WRITE_PARCEL_ERR;
192 }
193 HILOG_INFO("End. Id[%{public}d]", wallpaerid);
194 return ERR_NONE;
195 }
196
OnGetWallpaperMinHeight(MessageParcel & data,MessageParcel & reply)197 int32_t WallpaperServiceStub::OnGetWallpaperMinHeight(MessageParcel &data, MessageParcel &reply)
198 {
199 HILOG_INFO("WallpaperServiceStub::OnGetWallpaperMinHeight start.");
200 int wallpaerMinHeight = GetWallpaperMinHeight();
201 if (!reply.WriteInt32(wallpaerMinHeight)) {
202 HILOG_ERROR("Write result data failed");
203 return IPC_STUB_WRITE_PARCEL_ERR;
204 }
205 HILOG_INFO("End. height[%{public}d]", wallpaerMinHeight);
206 return ERR_NONE;
207 }
208
OnGetWallpaperMinWidth(MessageParcel & data,MessageParcel & reply)209 int32_t WallpaperServiceStub::OnGetWallpaperMinWidth(MessageParcel &data, MessageParcel &reply)
210 {
211 HILOG_INFO("WallpaperServiceStub::OnGetWallpaperMinWidth start.");
212
213 int wallpaperMinWidth = GetWallpaperMinWidth();
214 if (!reply.WriteInt32(wallpaperMinWidth)) {
215 HILOG_ERROR("Write result data failed");
216 return IPC_STUB_WRITE_PARCEL_ERR;
217 }
218 HILOG_INFO("End. width[%{public}d]", wallpaperMinWidth);
219 return ERR_NONE;
220 }
221
OnIsChangePermitted(MessageParcel & data,MessageParcel & reply)222 int32_t WallpaperServiceStub::OnIsChangePermitted(MessageParcel &data, MessageParcel &reply)
223 {
224 HILOG_INFO("WallpaperServiceStub::OnIsChangePermitted start.");
225 auto bResult = IsChangePermitted();
226 if (!reply.WriteBool(bResult)) {
227 HILOG_ERROR("Write result data failed");
228 return IPC_STUB_WRITE_PARCEL_ERR;
229 }
230 return ERR_NONE;
231 }
232
OnIsOperationAllowed(MessageParcel & data,MessageParcel & reply)233 int32_t WallpaperServiceStub::OnIsOperationAllowed(MessageParcel &data, MessageParcel &reply)
234 {
235 HILOG_INFO("WallpaperServiceStub::OnIsOperationAllowed start.");
236 auto bResult = IsOperationAllowed();
237 if (!reply.WriteBool(bResult)) {
238 HILOG_ERROR("Write result data failed");
239 return IPC_STUB_WRITE_PARCEL_ERR;
240 }
241 return ERR_NONE;
242 }
243
OnResetWallpaper(MessageParcel & data,MessageParcel & reply)244 int32_t WallpaperServiceStub::OnResetWallpaper(MessageParcel &data, MessageParcel &reply)
245 {
246 HILOG_INFO("WallpaperServiceStub::OnResetWallpaper start.");
247 int wallpaperType = data.ReadInt32();
248 auto wallpaperErrorCode = ResetWallpaper(wallpaperType);
249 if (!reply.WriteInt32(wallpaperErrorCode)) {
250 HILOG_ERROR("Write result data failed");
251 return IPC_STUB_WRITE_PARCEL_ERR;
252 }
253 return ERR_NONE;
254 }
255
OnWallpaperOn(MessageParcel & data,MessageParcel & reply)256 int32_t WallpaperServiceStub::OnWallpaperOn(MessageParcel &data, MessageParcel &reply)
257 {
258 HILOG_DEBUG("WallpaperServiceStub::OnWallpaperOn in");
259 sptr<IRemoteObject> remote = data.ReadRemoteObject();
260 if (remote == nullptr) {
261 HILOG_ERROR("OnWallpaperOn nullptr after ipc");
262 if (!reply.WriteInt32(static_cast<int32_t>(E_READ_PARCEL_ERROR))) {
263 return IPC_STUB_WRITE_PARCEL_ERR;
264 }
265 return IPC_STUB_INVALID_DATA_ERR;
266 }
267 sptr<IWallpaperColorChangeListener> WallpaperListenerProxy = iface_cast<IWallpaperColorChangeListener>(remote);
268
269 bool status = On(std::move(WallpaperListenerProxy));
270 int32_t ret = status ? static_cast<int32_t>(E_OK) : static_cast<int32_t>(E_DEAL_FAILED);
271 if (!reply.WriteInt32(ret)) {
272 HILOG_ERROR("WriteInt32 failed");
273 return IPC_STUB_WRITE_PARCEL_ERR;
274 }
275 HILOG_DEBUG("WallpaperServiceStub::OnWallpaperOn out");
276 return ERR_NONE;
277 }
278
OnWallpaperOff(MessageParcel & data,MessageParcel & reply)279 int32_t WallpaperServiceStub::OnWallpaperOff(MessageParcel &data, MessageParcel &reply)
280 {
281 HILOG_DEBUG("WallpaperServiceStub::OnWallpaperOff in");
282 sptr<IRemoteObject> remote = data.ReadRemoteObject();
283 bool status = false;
284 if (remote == nullptr) {
285 status = Off(nullptr);
286 } else {
287 sptr<IWallpaperColorChangeListener> WallpaperListenerProxy = iface_cast<IWallpaperColorChangeListener>(remote);
288 status = Off(std::move(WallpaperListenerProxy));
289 }
290 int32_t ret = status ? static_cast<int32_t>(E_OK) : static_cast<int32_t>(E_DEAL_FAILED);
291 if (!reply.WriteInt32(ret)) {
292 HILOG_ERROR("WriteInt32 failed");
293 return IPC_STUB_WRITE_PARCEL_ERR;
294 }
295 HILOG_DEBUG("WallpaperServiceStub::OnWallpaperOff out");
296 return ERR_NONE;
297 }
298
OnRegisterWallpaperCallback(MessageParcel & data,MessageParcel & reply)299 int32_t WallpaperServiceStub::OnRegisterWallpaperCallback(MessageParcel &data, MessageParcel &reply)
300 {
301 HILOG_INFO(" WallpaperServiceStub::OnRegisterWallpaperCallback start");
302 sptr<IRemoteObject> object = data.ReadRemoteObject();
303 if (object == nullptr) {
304 HILOG_ERROR("RegisterWallpaperCallback failed");
305 reply.WriteInt32(static_cast<int32_t>(E_READ_PARCEL_ERROR));
306 return IPC_STUB_INVALID_DATA_ERR;
307 }
308 sptr<IWallpaperCallback> callbackProxy = iface_cast<IWallpaperCallback>(object);
309
310 RegisterWallpaperCallback(callbackProxy);
311 if (!reply.WriteInt32(static_cast<int32_t>(E_OK))) {
312 HILOG_ERROR("WriteInt32 failed");
313 return IPC_STUB_WRITE_PARCEL_ERR;
314 }
315 return ERR_NONE;
316 }
317 } // namespace WallpaperMgrService
318 } // namespace OHOS