1 /*
2  * Copyright (c) 2021-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 "zidl/window_manager_stub.h"
17 #include <ipc_skeleton.h>
18 #include <key_event.h>
19 #include <rs_iwindow_animation_controller.h>
20 #include <rs_window_animation_target.h>
21 
22 #include "marshalling_helper.h"
23 #include "memory_guard.h"
24 #include "window_manager_hilog.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 namespace {
29 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowManagerStub"};
30 }
31 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int32_t WindowManagerStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
33     MessageOption& option)
34 {
35     MemoryGuard cacheGuard;
36     if (data.ReadInterfaceToken() != GetDescriptor()) {
37         WLOGFE("InterfaceToken check failed");
38         return ERR_TRANSACTION_FAILED;
39     }
40     auto msgId = static_cast<WindowManagerMessage>(code);
41     switch (msgId) {
42         case WindowManagerMessage::TRANS_ID_CREATE_WINDOW: {
43             sptr<IRemoteObject> windowObject = data.ReadRemoteObject();
44             sptr<IWindow> windowProxy = iface_cast<IWindow>(windowObject);
45             sptr<WindowProperty> windowProperty = data.ReadStrongParcelable<WindowProperty>();
46             std::shared_ptr<RSSurfaceNode> surfaceNode = RSSurfaceNode::Unmarshalling(data);
47             uint32_t windowId;
48             sptr<IRemoteObject> token = nullptr;
49             if (windowProperty && windowProperty->GetTokenState()) {
50                 token = data.ReadRemoteObject();
51             } else {
52                 WLOGI("accept token is nullptr");
53             }
54             WMError errCode = CreateWindow(windowProxy, windowProperty, surfaceNode, windowId, token);
55             reply.WriteUint32(windowId);
56             reply.WriteInt32(static_cast<int32_t>(errCode));
57             if (windowProperty) {
58                 reply.WriteUint32(windowProperty->GetWindowFlags());
59                 reply.WriteUint32(windowProperty->GetApiCompatibleVersion());
60             }
61             break;
62         }
63         case WindowManagerMessage::TRANS_ID_ADD_WINDOW: {
64             sptr<WindowProperty> windowProperty = data.ReadStrongParcelable<WindowProperty>();
65             WMError errCode = AddWindow(windowProperty);
66             reply.WriteInt32(static_cast<int32_t>(errCode));
67             break;
68         }
69         case WindowManagerMessage::TRANS_ID_REMOVE_WINDOW: {
70             uint32_t windowId = data.ReadUint32();
71             bool isFromInnerkits = data.ReadBool();
72             WMError errCode = RemoveWindow(windowId, isFromInnerkits);
73             reply.WriteInt32(static_cast<int32_t>(errCode));
74             break;
75         }
76         case WindowManagerMessage::TRANS_ID_DESTROY_WINDOW: {
77             uint32_t windowId = data.ReadUint32();
78             WMError errCode = DestroyWindow(windowId);
79             reply.WriteInt32(static_cast<int32_t>(errCode));
80             break;
81         }
82         case WindowManagerMessage::TRANS_ID_REQUEST_FOCUS: {
83             uint32_t windowId = data.ReadUint32();
84             WMError errCode = RequestFocus(windowId);
85             reply.WriteInt32(static_cast<int32_t>(errCode));
86             break;
87         }
88         case WindowManagerMessage::TRANS_ID_GET_AVOID_AREA: {
89             uint32_t windowId = data.ReadUint32();
90             auto avoidAreaType = static_cast<AvoidAreaType>(data.ReadUint32());
91             AvoidArea avoidArea = GetAvoidAreaByType(windowId, avoidAreaType);
92             reply.WriteParcelable(&avoidArea);
93 
94             break;
95         }
96         case WindowManagerMessage::TRANS_ID_REGISTER_WINDOW_MANAGER_AGENT: {
97             auto type = static_cast<WindowManagerAgentType>(data.ReadUint32());
98             sptr<IRemoteObject> windowManagerAgentObject = data.ReadRemoteObject();
99             sptr<IWindowManagerAgent> windowManagerAgentProxy =
100                 iface_cast<IWindowManagerAgent>(windowManagerAgentObject);
101             WMError errCode = RegisterWindowManagerAgent(type, windowManagerAgentProxy);
102             reply.WriteInt32(static_cast<int32_t>(errCode));
103             break;
104         }
105         case WindowManagerMessage::TRANS_ID_UNREGISTER_WINDOW_MANAGER_AGENT: {
106             auto type = static_cast<WindowManagerAgentType>(data.ReadUint32());
107             sptr<IRemoteObject> windowManagerAgentObject = data.ReadRemoteObject();
108             sptr<IWindowManagerAgent> windowManagerAgentProxy =
109                 iface_cast<IWindowManagerAgent>(windowManagerAgentObject);
110             WMError errCode = UnregisterWindowManagerAgent(type, windowManagerAgentProxy);
111             reply.WriteInt32(static_cast<int32_t>(errCode));
112             break;
113         }
114         case WindowManagerMessage::TRANS_ID_NOTIFY_READY_MOVE_OR_DRAG: {
115             uint32_t windowId = data.ReadUint32();
116             sptr<WindowProperty> windowProperty = data.ReadStrongParcelable<WindowProperty>();
117             sptr<MoveDragProperty> moveDragProperty = data.ReadStrongParcelable<MoveDragProperty>();
118             NotifyServerReadyToMoveOrDrag(windowId, windowProperty, moveDragProperty);
119             break;
120         }
121         case WindowManagerMessage::TRANS_ID_PROCESS_POINT_DOWN: {
122             uint32_t windowId = data.ReadUint32();
123             bool isPointDown = data.ReadBool();
124             ProcessPointDown(windowId, isPointDown);
125             break;
126         }
127         case WindowManagerMessage::TRANS_ID_PROCESS_POINT_UP: {
128             uint32_t windowId = data.ReadUint32();
129             ProcessPointUp(windowId);
130             break;
131         }
132         case WindowManagerMessage::TRANS_ID_GET_TOP_WINDOW_ID: {
133             uint32_t mainWinId = data.ReadUint32();
134             uint32_t topWinId;
135             WMError errCode = GetTopWindowId(mainWinId, topWinId);
136             reply.WriteUint32(topWinId);
137             reply.WriteInt32(static_cast<int32_t>(errCode));
138             break;
139         }
140         case WindowManagerMessage::TRANS_ID_MINIMIZE_ALL_APP_WINDOWS: {
141             WMError errCode = MinimizeAllAppWindows(data.ReadUint64());
142             reply.WriteInt32(static_cast<int32_t>(errCode));
143             break;
144         }
145         case WindowManagerMessage::TRANS_ID_TOGGLE_SHOWN_STATE_FOR_ALL_APP_WINDOWS: {
146             WMError errCode = ToggleShownStateForAllAppWindows();
147             reply.WriteInt32(static_cast<int32_t>(errCode));
148             break;
149         }
150         case WindowManagerMessage::TRANS_ID_UPDATE_LAYOUT_MODE: {
151             auto mode = static_cast<WindowLayoutMode>(data.ReadUint32());
152             WMError errCode = SetWindowLayoutMode(mode);
153             reply.WriteInt32(static_cast<int32_t>(errCode));
154             break;
155         }
156         case WindowManagerMessage::TRANS_ID_UPDATE_PROPERTY: {
157             auto action = static_cast<PropertyChangeAction>(data.ReadUint32());
158             sptr<WindowProperty> windowProperty = new WindowProperty();
159             windowProperty->Read(data, action);
160             WMError errCode = UpdateProperty(windowProperty, action);
161             reply.WriteInt32(static_cast<int32_t>(errCode));
162             break;
163         }
164         case WindowManagerMessage::TRANS_ID_GET_ACCESSIBILITY_WINDOW_INFO_ID: {
165             std::vector<sptr<AccessibilityWindowInfo>> infos;
166             WMError errCode = GetAccessibilityWindowInfo(infos);
167             if (!MarshallingHelper::MarshallingVectorParcelableObj<AccessibilityWindowInfo>(reply, infos)) {
168                 WLOGFE("Write accessibility window infos failed");
169                 return -1;
170             }
171             reply.WriteInt32(static_cast<int32_t>(errCode));
172             break;
173         }
174         case WindowManagerMessage::TRANS_ID_GET_UNRELIABLE_WINDOW_INFO_ID: {
175             int32_t windowId = 0;
176             if (!data.ReadInt32(windowId)) {
177                 WLOGFE("Failed to readInt32 windowId");
178                 return ERR_INVALID_DATA;
179             }
180             std::vector<sptr<UnreliableWindowInfo>> infos;
181             WMError errCode = GetUnreliableWindowInfo(windowId, infos);
182             if (!MarshallingHelper::MarshallingVectorParcelableObj<UnreliableWindowInfo>(reply, infos)) {
183                 WLOGFE("Write unreliable window infos failed");
184                 return ERR_INVALID_DATA;
185             }
186             reply.WriteInt32(static_cast<int32_t>(errCode));
187             break;
188         }
189         case WindowManagerMessage::TRANS_ID_GET_VISIBILITY_WINDOW_INFO_ID: {
190             std::vector<sptr<WindowVisibilityInfo>> infos;
191             WMError errCode = GetVisibilityWindowInfo(infos);
192             if (!MarshallingHelper::MarshallingVectorParcelableObj<WindowVisibilityInfo>(reply, infos)) {
193                 WLOGFE("Write visibility window infos failed");
194                 return -1;
195             }
196             reply.WriteInt32(static_cast<int32_t>(errCode));
197             break;
198         }
199         case WindowManagerMessage::TRANS_ID_ANIMATION_SET_CONTROLLER: {
200             sptr<IRemoteObject> controllerObject = data.ReadRemoteObject();
201             sptr<RSIWindowAnimationController> controller = iface_cast<RSIWindowAnimationController>(controllerObject);
202             WMError errCode = SetWindowAnimationController(controller);
203             reply.WriteInt32(static_cast<int32_t>(errCode));
204             break;
205         }
206         case WindowManagerMessage::TRANS_ID_GET_SYSTEM_CONFIG: {
207             SystemConfig config;
208             WMError errCode = GetSystemConfig(config);
209             reply.WriteParcelable(&config);
210             reply.WriteInt32(static_cast<int32_t>(errCode));
211             break;
212         }
213         case WindowManagerMessage::TRANS_ID_NOTIFY_WINDOW_TRANSITION: {
214             sptr<WindowTransitionInfo> from = data.ReadParcelable<WindowTransitionInfo>();
215             sptr<WindowTransitionInfo> to = data.ReadParcelable<WindowTransitionInfo>();
216             bool isFromClient = data.ReadBool();
217             WMError errCode = NotifyWindowTransition(from, to, isFromClient);
218             reply.WriteInt32(static_cast<int32_t>(errCode));
219             break;
220         }
221         case WindowManagerMessage::TRANS_ID_GET_FULLSCREEN_AND_SPLIT_HOT_ZONE: {
222             DisplayId displayId = data.ReadUint64();
223             ModeChangeHotZones hotZones = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
224             WMError errCode = GetModeChangeHotZones(displayId, hotZones);
225             reply.WriteInt32(static_cast<int32_t>(errCode));
226 
227             reply.WriteInt32(hotZones.fullscreen_.posX_);
228             reply.WriteInt32(hotZones.fullscreen_.posY_);
229             reply.WriteUint32(hotZones.fullscreen_.width_);
230             reply.WriteUint32(hotZones.fullscreen_.height_);
231 
232             reply.WriteInt32(hotZones.primary_.posX_);
233             reply.WriteInt32(hotZones.primary_.posY_);
234             reply.WriteUint32(hotZones.primary_.width_);
235             reply.WriteUint32(hotZones.primary_.height_);
236 
237             reply.WriteInt32(hotZones.secondary_.posX_);
238             reply.WriteInt32(hotZones.secondary_.posY_);
239             reply.WriteUint32(hotZones.secondary_.width_);
240             reply.WriteUint32(hotZones.secondary_.height_);
241             break;
242         }
243         case WindowManagerMessage::TRANS_ID_GET_ANIMATION_CALLBACK: {
244             std::vector<uint32_t> windowIds;
245             data.ReadUInt32Vector(&windowIds);
246             bool isAnimated = data.ReadBool();
247             sptr<RSIWindowAnimationFinishedCallback> finishedCallback = nullptr;
248             MinimizeWindowsByLauncher(windowIds, isAnimated, finishedCallback);
249             if (finishedCallback == nullptr) {
250                 if (!reply.WriteBool(false)) {
251                     WLOGFE("finishedCallback is nullptr and failed to write!");
252                     return ERR_INVALID_DATA;
253                 }
254             } else {
255                 if (!reply.WriteBool(true) || !reply.WriteRemoteObject(finishedCallback->AsObject())) {
256                     WLOGFE("finishedCallback is not nullptr and failed to write!");
257                     return ERR_INVALID_DATA;
258                 }
259             }
260             break;
261         }
262         case WindowManagerMessage::TRANS_ID_UPDATE_AVOIDAREA_LISTENER: {
263             uint32_t windowId = data.ReadUint32();
264             bool haveAvoidAreaListener = data.ReadBool();
265             WMError errCode = UpdateAvoidAreaListener(windowId, haveAvoidAreaListener);
266             reply.WriteInt32(static_cast<int32_t>(errCode));
267             break;
268         }
269         case WindowManagerMessage::TRANS_ID_UPDATE_RS_TREE: {
270             uint32_t windowId = data.ReadUint32();
271             bool isAdd = data.ReadBool();
272             WMError errCode = UpdateRsTree(windowId, isAdd);
273             reply.WriteInt32(static_cast<int32_t>(errCode));
274             break;
275         }
276         case WindowManagerMessage::TRANS_ID_BIND_DIALOG_TARGET: {
277             uint32_t windowId = data.ReadUint32();
278             sptr<IRemoteObject> targetToken = data.ReadRemoteObject();
279             WMError errCode = BindDialogTarget(windowId, targetToken);
280             reply.WriteInt32(static_cast<int32_t>(errCode));
281             break;
282         }
283         case WindowManagerMessage::TRANS_ID_SET_ANCHOR_AND_SCALE : {
284             int32_t x = data.ReadInt32();
285             int32_t y = data.ReadInt32();
286             float scale = data.ReadFloat();
287             SetAnchorAndScale(x, y, scale);
288             break;
289         }
290         case WindowManagerMessage::TRANS_ID_SET_ANCHOR_OFFSET: {
291             int32_t deltaX = data.ReadInt32();
292             int32_t deltaY = data.ReadInt32();
293             SetAnchorOffset(deltaX, deltaY);
294             break;
295         }
296         case WindowManagerMessage::TRANS_ID_OFF_WINDOW_ZOOM: {
297             OffWindowZoom();
298             break;
299         }
300         case WindowManagerMessage::TRANS_ID_RAISE_WINDOW_Z_ORDER: {
301             uint32_t windowId = data.ReadUint32();
302             WMError errCode = RaiseToAppTop(windowId);
303             reply.WriteInt32(static_cast<int32_t>(errCode));
304             break;
305         }
306         case WindowManagerMessage::TRANS_ID_GET_SNAPSHOT: {
307             uint32_t windowId = data.ReadUint32();
308             std::shared_ptr<Media::PixelMap> pixelMap = GetSnapshot(windowId);
309             reply.WriteParcelable(pixelMap.get());
310             break;
311         }
312         case WindowManagerMessage::TRANS_ID_GESTURE_NAVIGATION_ENABLED: {
313             bool enable = data.ReadBool();
314             WMError errCode = SetGestureNavigaionEnabled(enable);
315             reply.WriteInt32(static_cast<int32_t>(errCode));
316             break;
317         }
318         case WindowManagerMessage::TRANS_ID_SET_WINDOW_GRAVITY: {
319             uint32_t windowId = data.ReadUint32();
320             WindowGravity gravity = static_cast<WindowGravity>(data.ReadUint32());
321             uint32_t percent = data.ReadUint32();
322             WMError errCode = SetWindowGravity(windowId, gravity, percent);
323             reply.WriteInt32(static_cast<int32_t>(errCode));
324             break;
325         }
326         case WindowManagerMessage::TRANS_ID_DISPATCH_KEY_EVENT: {
327             uint32_t windowId = data.ReadUint32();
328             std::shared_ptr<MMI::KeyEvent> event = MMI::KeyEvent::Create();
329             if (event == nullptr) {
330                 WLOGFE("event is null");
331                 return ERR_INVALID_DATA;
332             }
333             event->ReadFromParcel(data);
334             DispatchKeyEvent(windowId, event);
335             break;
336         }
337         case WindowManagerMessage::TRANS_ID_NOTIFY_DUMP_INFO_RESULT: {
338             std::vector<std::string> info;
339             data.ReadStringVector(&info);
340             NotifyDumpInfoResult(info);
341             break;
342         }
343         case WindowManagerMessage::TRANS_ID_GET_WINDOW_ANIMATION_TARGETS: {
344             std::vector<uint32_t> missionIds;
345             data.ReadUInt32Vector(&missionIds);
346             std::vector<sptr<RSWindowAnimationTarget>> targets;
347             WMError errCode = GetWindowAnimationTargets(missionIds, targets);
348             if (!MarshallingHelper::MarshallingVectorParcelableObj<RSWindowAnimationTarget>(reply, targets)) {
349                 WLOGFE("Write window animation targets failed");
350                 return ERR_INVALID_DATA;
351             }
352             reply.WriteInt32(static_cast<int32_t>(errCode));
353             break;
354         }
355         case WindowManagerMessage::TRANS_ID_SET_MAXIMIZE_MODE: {
356             MaximizeMode maximizeMode = static_cast<MaximizeMode>(data.ReadUint32());
357             SetMaximizeMode(maximizeMode);
358             break;
359         }
360         case WindowManagerMessage::TRANS_ID_GET_MAXIMIZE_MODE: {
361             MaximizeMode maximizeMode = GetMaximizeMode();
362             reply.WriteInt32(static_cast<int32_t>(maximizeMode));
363             break;
364         }
365         case WindowManagerMessage::TRANS_ID_GET_FOCUS_WINDOW_INFO: {
366             FocusChangeInfo focusInfo;
367             GetFocusWindowInfo(focusInfo);
368             reply.WriteParcelable(&focusInfo);
369             break;
370         }
371         default:
372             WLOGFW("unknown transaction code %{public}d", code);
373             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
374     }
375     return ERR_NONE;
376 }
377 } // namespace Rosen
378 } // namespace OHOS
379