1/*
2 * Copyright (c) 2023 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 "drag_server.h"
17
18#include "drag_params.h"
19#include "devicestatus_define.h"
20
21#undef LOG_TAG
22#define LOG_TAG "DragServer"
23
24namespace OHOS {
25namespace Msdp {
26namespace DeviceStatus {
27
28DragServer::DragServer(IContext *env)
29    : env_(env)
30{}
31
32int32_t DragServer::Enable(CallingContext &context, MessageParcel &data, MessageParcel &reply)
33{
34    CALL_DEBUG_ENTER;
35    return RET_ERR;
36}
37
38int32_t DragServer::Disable(CallingContext &context, MessageParcel &data, MessageParcel &reply)
39{
40    CALL_DEBUG_ENTER;
41    return RET_ERR;
42}
43
44int32_t DragServer::Start(CallingContext &context, MessageParcel &data, MessageParcel &reply)
45{
46    CALL_DEBUG_ENTER;
47    DragData dragData {};
48    StartDragParam param { dragData };
49
50    if (!param.Unmarshalling(data)) {
51        FI_HILOGE("Failed to unmarshalling param");
52        return RET_ERR;
53    }
54    CHKPR(env_, RET_ERR);
55    auto session = env_->GetSocketSessionManager().FindSessionByPid(context.pid);
56    CHKPR(session, RET_ERR);
57    session->SetProgramName(GetPackageName(context.tokenId));
58    return env_->GetDragManager().StartDrag(dragData, context.pid);
59}
60
61int32_t DragServer::Stop(CallingContext &context, MessageParcel &data, MessageParcel &reply)
62{
63    CALL_DEBUG_ENTER;
64    StopDragParam param {};
65
66    if (!param.Unmarshalling(data)) {
67        FI_HILOGE("Failed to unmarshalling param");
68        return RET_ERR;
69    }
70    CHKPR(env_, RET_ERR);
71    return env_->GetDragManager().StopDrag(param.dropResult_, GetPackageName(context.tokenId));
72}
73
74int32_t DragServer::AddWatch(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
75{
76    CALL_DEBUG_ENTER;
77    switch (id) {
78        case DragRequestID::ADD_DRAG_LISTENER: {
79            FI_HILOGI("Add drag listener, from:%{public}d", context.pid);
80            return env_->GetDragManager().AddListener(context.pid);
81        }
82        case DragRequestID::ADD_SUBSCRIPT_LISTENER: {
83            FI_HILOGD("Add subscript listener, from:%{public}d", context.pid);
84            return env_->GetDragManager().AddSubscriptListener(context.pid);
85        }
86        default: {
87            FI_HILOGE("Unexpected request ID (%{public}u)", id);
88            return RET_ERR;
89        }
90    }
91}
92
93int32_t DragServer::RemoveWatch(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
94{
95    CALL_DEBUG_ENTER;
96    switch (id) {
97        case DragRequestID::REMOVE_DRAG_LISTENER: {
98            FI_HILOGD("Remove drag listener, from:%{public}d", context.pid);
99            return env_->GetDragManager().RemoveListener(context.pid);
100        }
101        case DragRequestID::REMOVE_SUBSCRIPT_LISTENER: {
102            FI_HILOGD("Remove subscript listener, from:%{public}d", context.pid);
103            return env_->GetDragManager().RemoveSubscriptListener(context.pid);
104        }
105        default: {
106            FI_HILOGE("Unexpected request ID (%{public}u)", id);
107            return RET_ERR;
108        }
109    }
110}
111
112int32_t DragServer::SetParam(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
113{
114    CALL_DEBUG_ENTER;
115    switch (id) {
116        case DragRequestID::SET_DRAG_WINDOW_VISIBLE: {
117            return SetDragWindowVisible(context, data, reply);
118        }
119        case DragRequestID::UPDATE_DRAG_STYLE: {
120            return UpdateDragStyle(context, data, reply);
121        }
122        case DragRequestID::UPDATE_SHADOW_PIC: {
123            return UpdateShadowPic(context, data, reply);
124        }
125        case DragRequestID::UPDATE_PREVIEW_STYLE: {
126            return UpdatePreviewStyle(context, data, reply);
127        }
128        case DragRequestID::UPDATE_PREVIEW_STYLE_WITH_ANIMATION: {
129            return UpdatePreviewAnimation(context, data, reply);
130        }
131        case DragRequestID::SET_DRAG_WINDOW_SCREEN_ID: {
132            return SetDragWindowScreenId(context, data, reply);
133        }
134        case DragRequestID::ADD_SELECTED_PIXELMAP: {
135            return AddSelectedPixelMap(context, data, reply);
136        }
137        default: {
138            FI_HILOGE("Unexpected request ID (%{public}u)", id);
139            return RET_ERR;
140        }
141    }
142}
143
144int32_t DragServer::GetParam(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
145{
146    CALL_DEBUG_ENTER;
147    switch (id) {
148        case DragRequestID::GET_DRAG_TARGET_PID: {
149            FI_HILOGI("Get drag target pid, from:%{public}d", context.pid);
150            return GetDragTargetPid(context, data, reply);
151        }
152        case DragRequestID::GET_UDKEY: {
153            FI_HILOGI("Get udkey, from:%{public}d", context.pid);
154            return GetUdKey(context, data, reply);
155        }
156        case DragRequestID::GET_SHADOW_OFFSET: {
157            FI_HILOGI("Get shadow offset, from:%{public}d", context.pid);
158            return GetShadowOffset(context, data, reply);
159        }
160        case DragRequestID::GET_DRAG_DATA: {
161            FI_HILOGI("Get drag data, from:%{public}d", context.pid);
162            return GetDragData(context, data, reply);
163        }
164        case DragRequestID::GET_DRAG_STATE: {
165            FI_HILOGI("Get drag state, from:%{public}d", context.pid);
166            return GetDragState(context, data, reply);
167        }
168        case DragRequestID::GET_DRAG_SUMMARY: {
169            FI_HILOGI("Get drag summary, from:%{public}d", context.pid);
170            return GetDragSummary(context, data, reply);
171        }
172        case DragRequestID::GET_DRAG_ACTION: {
173            FI_HILOGI("Get drag action, from:%{public}d", context.pid);
174            return GetDragAction(context, data, reply);
175        }
176        case DragRequestID::GET_EXTRA_INFO: {
177            FI_HILOGI("Get extra info, from:%{public}d", context.pid);
178            return GetExtraInfo(context, data, reply);
179        }
180        default: {
181            FI_HILOGE("Unexpected request ID (%{public}u)", id);
182            return RET_ERR;
183        }
184    }
185}
186
187int32_t DragServer::Control(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
188{
189    CALL_DEBUG_ENTER;
190    switch (id) {
191        case DragRequestID::ADD_PRIVILEGE: {
192            FI_HILOGI("Add privilege, from:%{public}d", context.pid);
193            return env_->GetDragManager().AddPrivilege(context.tokenId);
194        }
195        case DragRequestID::ENTER_TEXT_EDITOR_AREA: {
196            FI_HILOGI("Enter text editor area, from:%{public}d", context.pid);
197            return EnterTextEditorArea(context, data, reply);
198        }
199        case DragRequestID::ROTATE_DRAG_WINDOW_SYNC: {
200            FI_HILOGI("Rotate drag window sync, from:%{public}d", context.pid);
201            return RotateDragWindowSync(context, data, reply);
202        }
203        case DragRequestID::ERASE_MOUSE_ICON: {
204            FI_HILOGI("Erase mouse, from:%{public}d", context.pid);
205            return env_->GetDragManager().EraseMouseIcon();
206        }
207        default: {
208            FI_HILOGE("Unexpected request ID (%{public}u)", id);
209            return RET_ERR;
210        }
211    }
212}
213
214int32_t DragServer::SetDragWindowVisible(CallingContext &context, MessageParcel &data, MessageParcel &reply)
215{
216    SetDragWindowVisibleParam param {};
217
218    if (!param.Unmarshalling(data)) {
219        FI_HILOGE("SetDragWindowVisibleParam::Unmarshalling fail");
220        return RET_ERR;
221    }
222    FI_HILOGI("SetDragWindowVisible(%{public}d, %{public}d)", param.visible_, param.isForce_);
223    return env_->GetDragManager().OnSetDragWindowVisible(param.visible_, param.isForce_);
224}
225
226int32_t DragServer::UpdateDragStyle(CallingContext &context, MessageParcel &data, MessageParcel &reply)
227{
228    UpdateDragStyleParam param {};
229
230    if (!param.Unmarshalling(data)) {
231        FI_HILOGE("UpdateDragStyleParam::Unmarshalling fail");
232        return RET_ERR;
233    }
234    FI_HILOGI("UpdateDragStyle(%{public}d)", static_cast<int32_t>(param.cursorStyle_));
235    return env_->GetDragManager().UpdateDragStyle(param.cursorStyle_, context.pid, context.tokenId);
236}
237
238int32_t DragServer::UpdateShadowPic(CallingContext &context, MessageParcel &data, MessageParcel &reply)
239{
240    UpdateShadowPicParam param {};
241
242    if (!param.Unmarshalling(data)) {
243        FI_HILOGE("UpdateShadowPicParam::Unmarshalling fail");
244        return RET_ERR;
245    }
246    FI_HILOGD("Updata shadow pic");
247    return env_->GetDragManager().UpdateShadowPic(param.shadowInfo_);
248}
249
250int32_t DragServer::AddSelectedPixelMap(CallingContext &context, MessageParcel &data, MessageParcel &reply)
251{
252    AddSelectedPixelMapParam param {};
253
254    if (!param.Unmarshalling(data)) {
255        FI_HILOGE("AddSelectedPixelMap::Unmarshalling fail");
256        return RET_ERR;
257    }
258    return env_->GetDragManager().AddSelectedPixelMap(param.pixelMap_);
259}
260
261int32_t DragServer::UpdatePreviewStyle(CallingContext &context, MessageParcel &data, MessageParcel &reply)
262{
263    UpdatePreviewStyleParam param {};
264
265    if (!param.Unmarshalling(data)) {
266        FI_HILOGE("UpdatePreviewStyleParam::Unmarshalling fail");
267        return RET_ERR;
268    }
269    return env_->GetDragManager().UpdatePreviewStyle(param.previewStyle_);
270}
271
272int32_t DragServer::UpdatePreviewAnimation(CallingContext &context, MessageParcel &data, MessageParcel &reply)
273{
274    UpdatePreviewAnimationParam param {};
275
276    if (!param.Unmarshalling(data)) {
277        FI_HILOGE("UpdatePreviewAnimationParam::Unmarshalling fail");
278        return RET_ERR;
279    }
280    return env_->GetDragManager().UpdatePreviewStyleWithAnimation(param.previewStyle_, param.previewAnimation_);
281}
282
283int32_t DragServer::RotateDragWindowSync(CallingContext &context, MessageParcel &data, MessageParcel &reply)
284{
285    RotateDragWindowSyncParam param {};
286
287    if (!param.Unmarshalling(data)) {
288        FI_HILOGE("RotateDragWindowSync::Unmarshalling fail");
289        return RET_ERR;
290    }
291    return env_->GetDragManager().RotateDragWindowSync(param.rsTransaction_);
292}
293
294int32_t DragServer::SetDragWindowScreenId(CallingContext &context, MessageParcel &data, MessageParcel &reply)
295{
296    SetDragWindowScreenIdParam param {};
297
298    if (!param.Unmarshalling(data)) {
299        FI_HILOGE("SetDragWindowScreenId::Unmarshalling fail");
300        return RET_ERR;
301    }
302    env_->GetDragManager().SetDragWindowScreenId(param.displayId_, param.screenId_);
303    return RET_OK;
304}
305
306int32_t DragServer::GetDragTargetPid(CallingContext &context, MessageParcel &data, MessageParcel &reply)
307{
308    int32_t targetPid = env_->GetDragManager().GetDragTargetPid();
309    GetDragTargetPidReply targetPidReply { targetPid };
310
311    if (!targetPidReply.Marshalling(reply)) {
312        FI_HILOGE("GetDragTargetPidReply::Marshalling fail");
313        return RET_ERR;
314    }
315    return RET_OK;
316}
317
318int32_t DragServer::GetUdKey(CallingContext &context, MessageParcel &data, MessageParcel &reply)
319{
320    std::string udKey;
321
322    int32_t ret = env_->GetDragManager().GetUdKey(udKey);
323    if (ret != RET_OK) {
324        FI_HILOGE("IDragManager::GetUdKey fail, error:%{public}d", ret);
325        return ret;
326    }
327    GetUdKeyReply udKeyReply { std::move(udKey) };
328
329    if (!udKeyReply.Marshalling(reply)) {
330        FI_HILOGE("GetUdKeyReply::Marshalling fail");
331        return RET_ERR;
332    }
333    return RET_OK;
334}
335
336int32_t DragServer::GetShadowOffset(CallingContext &context, MessageParcel &data, MessageParcel &reply)
337{
338    ShadowOffset shadowOffset {};
339
340    int32_t ret = env_->GetDragManager().OnGetShadowOffset(shadowOffset);
341    if (ret != RET_OK) {
342        FI_HILOGE("IDragManager::GetShadowOffset fail, error:%{public}d", ret);
343        return ret;
344    }
345    GetShadowOffsetReply shadowOffsetReply { shadowOffset };
346
347    if (!shadowOffsetReply.Marshalling(reply)) {
348        FI_HILOGE("GetShadowOffsetReply::Marshalling fail");
349        return RET_ERR;
350    }
351    return RET_OK;
352}
353
354int32_t DragServer::GetDragData(CallingContext &context, MessageParcel &data, MessageParcel &reply)
355{
356    DragData dragData {};
357
358    int32_t ret = env_->GetDragManager().GetDragData(dragData);
359    if (ret != RET_OK) {
360        FI_HILOGE("IDragManager::GetDragData fail, error:%{public}d", ret);
361        return ret;
362    }
363    GetDragDataReply dragDataReply { static_cast<const DragData&>(dragData) };
364
365    if (!dragDataReply.Marshalling(reply)) {
366        FI_HILOGE("GetDragDataReply::Marshalling fail");
367        return RET_ERR;
368    }
369    return RET_OK;
370}
371
372int32_t DragServer::GetDragState(CallingContext &context, MessageParcel &data, MessageParcel &reply)
373{
374    DragState dragState {};
375
376    int32_t ret = env_->GetDragManager().GetDragState(dragState);
377    if (ret != RET_OK) {
378        FI_HILOGE("IDragManager::GetDragState fail, error:%{public}d", ret);
379        return ret;
380    }
381    GetDragStateReply dragStateReply { dragState };
382
383    if (!dragStateReply.Marshalling(reply)) {
384        FI_HILOGE("GetDragStateReply::Marshalling fail");
385        return RET_ERR;
386    }
387    return RET_OK;
388}
389
390int32_t DragServer::GetDragSummary(CallingContext &context, MessageParcel &data, MessageParcel &reply)
391{
392    std::map<std::string, int64_t> summaries;
393
394    int32_t ret = env_->GetDragManager().GetDragSummary(summaries);
395    if (ret != RET_OK) {
396        FI_HILOGE("IDragManager::GetDragSummary fail, error:%{public}d", ret);
397        return ret;
398    }
399    GetDragSummaryReply summaryReply { std::move(summaries) };
400
401    if (!summaryReply.Marshalling(reply)) {
402        FI_HILOGE("GetDragSummaryReply::Marshalling fail");
403        return RET_ERR;
404    }
405    return RET_OK;
406}
407
408int32_t DragServer::GetDragAction(CallingContext &context, MessageParcel &data, MessageParcel &reply)
409{
410    DragAction dragAction {};
411
412    int32_t ret = env_->GetDragManager().GetDragAction(dragAction);
413    if (ret != RET_OK) {
414        FI_HILOGE("IDragManager::GetDragSummary fail, error:%{public}d", ret);
415        return ret;
416    }
417    GetDragActionReply dragActionReply { dragAction };
418
419    if (!dragActionReply.Marshalling(reply)) {
420        FI_HILOGE("GetDragActionReply::Marshalling fail");
421        return RET_ERR;
422    }
423    return RET_OK;
424}
425
426int32_t DragServer::GetExtraInfo(CallingContext &context, MessageParcel &data, MessageParcel &reply)
427{
428    std::string extraInfo;
429
430    int32_t ret = env_->GetDragManager().GetExtraInfo(extraInfo);
431    if (ret != RET_OK) {
432        FI_HILOGE("IDragManager::GetExtraInfo fail, error:%{public}d", ret);
433        return ret;
434    }
435    GetExtraInfoReply extraInfoReply { std::move(extraInfo) };
436
437    if (!extraInfoReply.Marshalling(reply)) {
438        FI_HILOGE("GetExtraInfoReply::Marshalling fail");
439        return RET_ERR;
440    }
441    return RET_OK;
442}
443
444int32_t DragServer::EnterTextEditorArea(CallingContext &context, MessageParcel &data, MessageParcel &reply)
445{
446    EnterTextEditorAreaParam param {};
447
448    if (!param.Unmarshalling(data)) {
449        FI_HILOGE("EnterTextEditorAreaParam::Unmarshalling fail");
450        return RET_ERR;
451    }
452    return env_->GetDragManager().EnterTextEditorArea(param.state);
453}
454
455std::string DragServer::GetPackageName(Security::AccessToken::AccessTokenID tokenId)
456{
457    std::string packageName = std::string();
458    int32_t tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId);
459    switch (tokenType) {
460        case Security::AccessToken::ATokenTypeEnum::TOKEN_HAP: {
461            Security::AccessToken::HapTokenInfo hapInfo;
462            if (Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo) != 0) {
463                FI_HILOGE("Get hap token info failed");
464            } else {
465                packageName = hapInfo.bundleName;
466            }
467            break;
468        }
469        case Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE:
470        case Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL: {
471            Security::AccessToken::NativeTokenInfo tokenInfo;
472            if (Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(tokenId, tokenInfo) != 0) {
473                FI_HILOGE("Get native token info failed");
474            } else {
475                packageName = tokenInfo.processName;
476            }
477            break;
478        }
479        default: {
480            FI_HILOGW("token type not match");
481            break;
482        }
483    }
484    return packageName;
485}
486} // namespace DeviceStatus
487} // namespace Msdp
488} // namespace OHOS