• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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 "tokenid_kit.h"
19 
20 #include "accesstoken_kit.h"
21 #include "drag_params.h"
22 #include "devicestatus_define.h"
23 
24 #undef LOG_TAG
25 #define LOG_TAG "DragServer"
26 
27 namespace OHOS {
28 namespace Msdp {
29 namespace DeviceStatus {
30 
31 #ifdef OHOS_BUILD_UNIVERSAL_DRAG
DragServer(IContext * env)32 DragServer::DragServer(IContext *env)
33     : env_(env), universalDragWrapper_(env)
34 #else
35 DragServer::DragServer(IContext *env)
36     : env_(env)
37 #endif // OHOS_BUILD_UNIVERSAL_DRAG
38 {
39 #ifdef OHOS_BUILD_UNIVERSAL_DRAG
40     bool ret = universalDragWrapper_.InitUniversalDrag();
41     if (!ret) {
42         FI_HILOGW("Init universal drag failed");
43     }
44 #endif // OHOS_BUILD_UNIVERSAL_DRAG
45 }
46 
Enable(CallingContext & context,MessageParcel & data,MessageParcel & reply)47 int32_t DragServer::Enable(CallingContext &context, MessageParcel &data, MessageParcel &reply)
48 {
49     CALL_DEBUG_ENTER;
50     return RET_ERR;
51 }
52 
Disable(CallingContext & context,MessageParcel & data,MessageParcel & reply)53 int32_t DragServer::Disable(CallingContext &context, MessageParcel &data, MessageParcel &reply)
54 {
55     CALL_DEBUG_ENTER;
56     return RET_ERR;
57 }
58 
Start(CallingContext & context,MessageParcel & data,MessageParcel & reply)59 int32_t DragServer::Start(CallingContext &context, MessageParcel &data, MessageParcel &reply)
60 {
61     CALL_DEBUG_ENTER;
62     DragData dragData {};
63     StartDragParam param { dragData };
64 
65     if (!param.Unmarshalling(data)) {
66         FI_HILOGE("Failed to unmarshalling param");
67         return RET_ERR;
68     }
69     CHKPR(env_, RET_ERR);
70     auto session = env_->GetSocketSessionManager().FindSessionByPid(context.pid);
71     CHKPR(session, RET_ERR);
72     session->SetProgramName(GetPackageName(context.tokenId));
73     return env_->GetDragManager().StartDrag(dragData, context.pid);
74 }
75 
Stop(CallingContext & context,MessageParcel & data,MessageParcel & reply)76 int32_t DragServer::Stop(CallingContext &context, MessageParcel &data, MessageParcel &reply)
77 {
78     CALL_DEBUG_ENTER;
79     StopDragParam param {};
80 
81     if (!param.Unmarshalling(data)) {
82         FI_HILOGE("Failed to unmarshalling param");
83         return RET_ERR;
84     }
85 #ifdef OHOS_BUILD_UNIVERSAL_DRAG
86     universalDragWrapper_.StopLongPressDrag();
87 #endif // OHOS_BUILD_UNIVERSAL_DRAG
88     CHKPR(env_, RET_ERR);
89     return env_->GetDragManager().StopDrag(param.dropResult_, GetPackageName(context.tokenId), context.pid);
90 }
91 
AddWatch(CallingContext & context,uint32_t id,MessageParcel & data,MessageParcel & reply)92 int32_t DragServer::AddWatch(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
93 {
94     CALL_DEBUG_ENTER;
95     switch (id) {
96         case DragRequestID::ADD_DRAG_LISTENER: {
97             return AddListener(context, data);
98         }
99         case DragRequestID::ADD_SUBSCRIPT_LISTENER: {
100             FI_HILOGD("Add subscript listener, from:%{public}d", context.pid);
101             return env_->GetDragManager().AddSubscriptListener(context.pid);
102         }
103         default: {
104             FI_HILOGE("Unexpected request ID (%{public}u)", id);
105             return RET_ERR;
106         }
107     }
108 }
109 
RemoveWatch(CallingContext & context,uint32_t id,MessageParcel & data,MessageParcel & reply)110 int32_t DragServer::RemoveWatch(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
111 {
112     CALL_DEBUG_ENTER;
113     switch (id) {
114         case DragRequestID::REMOVE_DRAG_LISTENER: {
115             return RemoveListener(context, data);
116         }
117         case DragRequestID::REMOVE_SUBSCRIPT_LISTENER: {
118             FI_HILOGD("Remove subscript listener, from:%{public}d", context.pid);
119             return env_->GetDragManager().RemoveSubscriptListener(context.pid);
120         }
121         default: {
122             FI_HILOGE("Unexpected request ID (%{public}u)", id);
123             return RET_ERR;
124         }
125     }
126 }
127 
SetParam(CallingContext & context,uint32_t id,MessageParcel & data,MessageParcel & reply)128 int32_t DragServer::SetParam(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
129 {
130     CALL_DEBUG_ENTER;
131     switch (id) {
132         case DragRequestID::SET_DRAG_WINDOW_VISIBLE: {
133             return SetDragWindowVisible(context, data, reply);
134         }
135         case DragRequestID::UPDATE_DRAG_STYLE: {
136             return UpdateDragStyle(context, data, reply);
137         }
138         case DragRequestID::UPDATE_SHADOW_PIC: {
139             return UpdateShadowPic(context, data, reply);
140         }
141         case DragRequestID::UPDATE_PREVIEW_STYLE: {
142             return UpdatePreviewStyle(context, data, reply);
143         }
144         case DragRequestID::UPDATE_PREVIEW_STYLE_WITH_ANIMATION: {
145             return UpdatePreviewAnimation(context, data, reply);
146         }
147         case DragRequestID::SET_DRAG_WINDOW_SCREEN_ID: {
148             return SetDragWindowScreenId(context, data, reply);
149         }
150         case DragRequestID::ADD_SELECTED_PIXELMAP: {
151             return AddSelectedPixelMap(context, data, reply);
152         }
153         case DragRequestID::SET_DRAG_SWITCH_STATE: {
154             return SetDragSwitchState(context, data, reply);
155         }
156         case DragRequestID::SET_APP_DRAG_SWITCH_STATE: {
157             return SetAppDragSwitchState(context, data, reply);
158         }
159         case DragRequestID::SET_DRAGGABLE_STATE: {
160             return SetDraggableState(context, data, reply);
161         }
162         case DragRequestID::SET_DRAGABLE_STATE_ASYNC: {
163             return SetDraggableStateAsync(context, data);
164         }
165         default: {
166             FI_HILOGE("Unexpected request ID (%{public}u)", id);
167             return RET_ERR;
168         }
169     }
170 }
171 
GetParam(CallingContext & context,uint32_t id,MessageParcel & data,MessageParcel & reply)172 int32_t DragServer::GetParam(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
173 {
174     CALL_DEBUG_ENTER;
175     switch (id) {
176         case DragRequestID::GET_DRAG_TARGET_PID: {
177             FI_HILOGI("Get drag target pid, from:%{public}d", context.pid);
178             return GetDragTargetPid(context, data, reply);
179         }
180         case DragRequestID::GET_UDKEY: {
181             FI_HILOGI("Get udkey, from:%{public}d", context.pid);
182             return GetUdKey(context, data, reply);
183         }
184         case DragRequestID::GET_SHADOW_OFFSET: {
185             FI_HILOGD("Get shadow offset, from:%{public}d", context.pid);
186             return GetShadowOffset(context, data, reply);
187         }
188         case DragRequestID::GET_DRAG_DATA: {
189             FI_HILOGD("Get drag data, from:%{public}d", context.pid);
190             return GetDragData(context, data, reply);
191         }
192         case DragRequestID::GET_DRAG_STATE: {
193             FI_HILOGD("Get drag state, from:%{public}d", context.pid);
194             return GetDragState(context, data, reply);
195         }
196         case DragRequestID::GET_DRAG_SUMMARY: {
197             FI_HILOGD("Get drag summary, from:%{public}d", context.pid);
198             return GetDragSummary(context, data, reply);
199         }
200         case DragRequestID::GET_DRAG_ACTION: {
201             FI_HILOGI("Get drag action, from:%{public}d", context.pid);
202             return GetDragAction(context, data, reply);
203         }
204         case DragRequestID::GET_EXTRA_INFO: {
205             FI_HILOGI("Get extra info, from:%{public}d", context.pid);
206             return GetExtraInfo(context, data, reply);
207         }
208         case DragRequestID::GET_UNIVERSAL_DRAG_APP_STATE: {
209             FI_HILOGI("Get universal drag app state, from:%{public}d", context.pid);
210             return GetAppDragSwitchState(context, data, reply);
211         }
212         default: {
213             FI_HILOGE("Unexpected request ID (%{public}u)", id);
214             return RET_ERR;
215         }
216     }
217 }
218 
Control(CallingContext & context,uint32_t id,MessageParcel & data,MessageParcel & reply)219 int32_t DragServer::Control(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
220 {
221     CALL_DEBUG_ENTER;
222     switch (id) {
223         case DragRequestID::ADD_PRIVILEGE: {
224             FI_HILOGI("Add privilege, from:%{public}d", context.pid);
225             return env_->GetDragManager().AddPrivilege(context.tokenId);
226         }
227         case DragRequestID::ENTER_TEXT_EDITOR_AREA: {
228             FI_HILOGI("Enter text editor area, from:%{public}d", context.pid);
229             return EnterTextEditorArea(context, data, reply);
230         }
231         case DragRequestID::ROTATE_DRAG_WINDOW_SYNC: {
232             FI_HILOGI("Rotate drag window sync, from:%{public}d", context.pid);
233             return RotateDragWindowSync(context, data, reply);
234         }
235         case DragRequestID::ERASE_MOUSE_ICON: {
236             FI_HILOGI("Erase mouse, from:%{public}d", context.pid);
237             return env_->GetDragManager().EraseMouseIcon();
238         }
239         case DragRequestID::SET_MOUSE_DRAG_MONITOR_STATE: {
240             FI_HILOGI("Set mouse drag monitor state, from:%{public}d", context.pid);
241             return SetMouseDragMonitorState(context, data, reply);
242         }
243         default: {
244             FI_HILOGE("Unexpected request ID (%{public}u)", id);
245             return RET_ERR;
246         }
247     }
248 }
249 
AddListener(CallingContext & context,MessageParcel & data)250 int32_t DragServer::AddListener(CallingContext &context, MessageParcel &data)
251 {
252     AddDraglistenerParam param {};
253     if (!param.Unmarshalling(data)) {
254         FI_HILOGE("AddDraglistenerParam::Unmarshalling fail");
255         return RET_ERR;
256     }
257 
258     if (param.isJsCaller_ && !IsSystemHAPCalling(context)) {
259         FI_HILOGE("The caller is not system hap");
260         return COMMON_NOT_SYSTEM_APP;
261     }
262     FI_HILOGI("Add drag listener, from:%{public}d", context.pid);
263     return env_->GetDragManager().AddListener(context.pid);
264 }
265 
RemoveListener(CallingContext & context,MessageParcel & data)266 int32_t DragServer::RemoveListener(CallingContext &context, MessageParcel &data)
267 {
268     RemoveDraglistenerParam param {};
269     if (!param.Unmarshalling(data)) {
270         FI_HILOGE("RemoveDraglistenerParam::Unmarshalling fail");
271         return RET_ERR;
272     }
273 
274     if (param.isJsCaller_ && !IsSystemHAPCalling(context)) {
275         FI_HILOGE("The caller is not system hap");
276         return COMMON_NOT_SYSTEM_APP;
277     }
278     FI_HILOGD("Remove drag listener, from:%{public}d", context.pid);
279     return env_->GetDragManager().RemoveListener(context.pid);
280 }
281 
SetDragWindowVisible(CallingContext & context,MessageParcel & data,MessageParcel & reply)282 int32_t DragServer::SetDragWindowVisible(CallingContext &context, MessageParcel &data, MessageParcel &reply)
283 {
284     SetDragWindowVisibleParam param {};
285 
286     if (!param.Unmarshalling(data)) {
287         FI_HILOGE("SetDragWindowVisibleParam::Unmarshalling fail");
288         return RET_ERR;
289     }
290     FI_HILOGI("SetDragWindowVisible(%{public}d, %{public}d)", param.visible_, param.isForce_);
291     return env_->GetDragManager().OnSetDragWindowVisible(param.visible_, param.isForce_);
292 }
293 
UpdateDragStyle(CallingContext & context,MessageParcel & data,MessageParcel & reply)294 int32_t DragServer::UpdateDragStyle(CallingContext &context, MessageParcel &data, MessageParcel &reply)
295 {
296     UpdateDragStyleParam param {};
297 
298     if (!param.Unmarshalling(data)) {
299         FI_HILOGE("UpdateDragStyleParam::Unmarshalling fail");
300         return RET_ERR;
301     }
302     FI_HILOGI("UpdateDragStyle(%{public}d)", static_cast<int32_t>(param.cursorStyle_));
303     return env_->GetDragManager().UpdateDragStyle(param.cursorStyle_, context.pid, context.tokenId, param.eventId_);
304 }
305 
UpdateShadowPic(CallingContext & context,MessageParcel & data,MessageParcel & reply)306 int32_t DragServer::UpdateShadowPic(CallingContext &context, MessageParcel &data, MessageParcel &reply)
307 {
308     UpdateShadowPicParam param {};
309 
310     if (!param.Unmarshalling(data)) {
311         FI_HILOGE("UpdateShadowPicParam::Unmarshalling fail");
312         return RET_ERR;
313     }
314     FI_HILOGD("Updata shadow pic");
315     return env_->GetDragManager().UpdateShadowPic(param.shadowInfo_);
316 }
317 
AddSelectedPixelMap(CallingContext & context,MessageParcel & data,MessageParcel & reply)318 int32_t DragServer::AddSelectedPixelMap(CallingContext &context, MessageParcel &data, MessageParcel &reply)
319 {
320     AddSelectedPixelMapParam param {};
321 
322     if (!param.Unmarshalling(data)) {
323         FI_HILOGE("AddSelectedPixelMap::Unmarshalling fail");
324         return RET_ERR;
325     }
326     return env_->GetDragManager().AddSelectedPixelMap(param.pixelMap_);
327 }
328 
UpdatePreviewStyle(CallingContext & context,MessageParcel & data,MessageParcel & reply)329 int32_t DragServer::UpdatePreviewStyle(CallingContext &context, MessageParcel &data, MessageParcel &reply)
330 {
331     UpdatePreviewStyleParam param {};
332 
333     if (!param.Unmarshalling(data)) {
334         FI_HILOGE("UpdatePreviewStyleParam::Unmarshalling fail");
335         return RET_ERR;
336     }
337     return env_->GetDragManager().UpdatePreviewStyle(param.previewStyle_);
338 }
339 
UpdatePreviewAnimation(CallingContext & context,MessageParcel & data,MessageParcel & reply)340 int32_t DragServer::UpdatePreviewAnimation(CallingContext &context, MessageParcel &data, MessageParcel &reply)
341 {
342     UpdatePreviewAnimationParam param {};
343 
344     if (!param.Unmarshalling(data)) {
345         FI_HILOGE("UpdatePreviewAnimationParam::Unmarshalling fail");
346         return RET_ERR;
347     }
348     return env_->GetDragManager().UpdatePreviewStyleWithAnimation(param.previewStyle_, param.previewAnimation_);
349 }
350 
RotateDragWindowSync(CallingContext & context,MessageParcel & data,MessageParcel & reply)351 int32_t DragServer::RotateDragWindowSync(CallingContext &context, MessageParcel &data, MessageParcel &reply)
352 {
353     RotateDragWindowSyncParam param {};
354 
355     if (!param.Unmarshalling(data)) {
356         FI_HILOGE("RotateDragWindowSync::Unmarshalling fail");
357         return RET_ERR;
358     }
359     return env_->GetDragManager().RotateDragWindowSync(param.rsTransaction_);
360 }
361 
SetDragWindowScreenId(CallingContext & context,MessageParcel & data,MessageParcel & reply)362 int32_t DragServer::SetDragWindowScreenId(CallingContext &context, MessageParcel &data, MessageParcel &reply)
363 {
364     SetDragWindowScreenIdParam param {};
365 
366     if (!param.Unmarshalling(data)) {
367         FI_HILOGE("SetDragWindowScreenId::Unmarshalling fail");
368         return RET_ERR;
369     }
370     env_->GetDragManager().SetDragWindowScreenId(param.displayId_, param.screenId_);
371     return RET_OK;
372 }
373 
GetDragTargetPid(CallingContext & context,MessageParcel & data,MessageParcel & reply)374 int32_t DragServer::GetDragTargetPid(CallingContext &context, MessageParcel &data, MessageParcel &reply)
375 {
376     int32_t targetPid = env_->GetDragManager().GetDragTargetPid();
377     GetDragTargetPidReply targetPidReply { targetPid };
378 
379     if (!targetPidReply.Marshalling(reply)) {
380         FI_HILOGE("GetDragTargetPidReply::Marshalling fail");
381         return RET_ERR;
382     }
383     return RET_OK;
384 }
385 
GetUdKey(CallingContext & context,MessageParcel & data,MessageParcel & reply)386 int32_t DragServer::GetUdKey(CallingContext &context, MessageParcel &data, MessageParcel &reply)
387 {
388     std::string udKey;
389 
390     int32_t ret = env_->GetDragManager().GetUdKey(udKey);
391     if (ret != RET_OK) {
392         FI_HILOGE("IDragManager::GetUdKey fail, error:%{public}d", ret);
393         return ret;
394     }
395     GetUdKeyReply udKeyReply { std::move(udKey) };
396 
397     if (!udKeyReply.Marshalling(reply)) {
398         FI_HILOGE("GetUdKeyReply::Marshalling fail");
399         return RET_ERR;
400     }
401     return RET_OK;
402 }
403 
GetShadowOffset(CallingContext & context,MessageParcel & data,MessageParcel & reply)404 int32_t DragServer::GetShadowOffset(CallingContext &context, MessageParcel &data, MessageParcel &reply)
405 {
406     ShadowOffset shadowOffset {};
407 
408     int32_t ret = env_->GetDragManager().OnGetShadowOffset(shadowOffset);
409     if (ret != RET_OK) {
410         FI_HILOGE("IDragManager::GetShadowOffset fail, error:%{public}d", ret);
411         return ret;
412     }
413     GetShadowOffsetReply shadowOffsetReply { shadowOffset };
414 
415     if (!shadowOffsetReply.Marshalling(reply)) {
416         FI_HILOGE("GetShadowOffsetReply::Marshalling fail");
417         return RET_ERR;
418     }
419     return RET_OK;
420 }
421 
GetDragData(CallingContext & context,MessageParcel & data,MessageParcel & reply)422 int32_t DragServer::GetDragData(CallingContext &context, MessageParcel &data, MessageParcel &reply)
423 {
424     DragData dragData {};
425 
426     int32_t ret = env_->GetDragManager().GetDragData(dragData);
427     if (ret != RET_OK) {
428         FI_HILOGE("IDragManager::GetDragData fail, error:%{public}d", ret);
429         return ret;
430     }
431     GetDragDataReply dragDataReply { static_cast<const DragData&>(dragData) };
432 
433     if (!dragDataReply.Marshalling(reply)) {
434         FI_HILOGE("GetDragDataReply::Marshalling fail");
435         return RET_ERR;
436     }
437     return RET_OK;
438 }
439 
GetDragState(CallingContext & context,MessageParcel & data,MessageParcel & reply)440 int32_t DragServer::GetDragState(CallingContext &context, MessageParcel &data, MessageParcel &reply)
441 {
442     DragState dragState {};
443 
444     int32_t ret = env_->GetDragManager().GetDragState(dragState);
445     if (ret != RET_OK) {
446         FI_HILOGE("IDragManager::GetDragState fail, error:%{public}d", ret);
447         return ret;
448     }
449     GetDragStateReply dragStateReply { dragState };
450 
451     if (!dragStateReply.Marshalling(reply)) {
452         FI_HILOGE("GetDragStateReply::Marshalling fail");
453         return RET_ERR;
454     }
455     return RET_OK;
456 }
457 
GetDragSummary(CallingContext & context,MessageParcel & data,MessageParcel & reply)458 int32_t DragServer::GetDragSummary(CallingContext &context, MessageParcel &data, MessageParcel &reply)
459 {
460     GetDragSummaryParam param {};
461 
462     if (!param.Unmarshalling(data)) {
463         FI_HILOGE("GetDragSummary::Unmarshalling fail");
464         return RET_ERR;
465     }
466     if (param.isJsCaller_ && !IsSystemHAPCalling(context)) {
467         FI_HILOGE("The caller is not system hap");
468         return COMMON_NOT_SYSTEM_APP;
469     }
470     std::map<std::string, int64_t> summaries;
471 
472     int32_t ret = env_->GetDragManager().GetDragSummary(summaries);
473     if (ret != RET_OK) {
474         FI_HILOGE("IDragManager::GetDragSummary fail, error:%{public}d", ret);
475         return ret;
476     }
477     GetDragSummaryReply summaryReply { std::move(summaries) };
478 
479     if (!summaryReply.Marshalling(reply)) {
480         FI_HILOGE("GetDragSummaryReply::Marshalling fail");
481         return RET_ERR;
482     }
483     return RET_OK;
484 }
485 
SetDragSwitchState(CallingContext & context,MessageParcel & data,MessageParcel & reply)486 int32_t DragServer::SetDragSwitchState(CallingContext &context, MessageParcel &data, MessageParcel &reply)
487 {
488     SetDragSwitchStateParam param {};
489 
490     if (!param.Unmarshalling(data)) {
491         FI_HILOGE("SetDragSwitchStateParam::Unmarshalling fail");
492         return RET_ERR;
493     }
494     if (param.isJsCaller_ && !IsSystemHAPCalling(context)) {
495         FI_HILOGE("The caller is not system hap");
496         return COMMON_NOT_SYSTEM_APP;
497     }
498 #ifdef OHOS_BUILD_UNIVERSAL_DRAG
499     universalDragWrapper_.SetDragSwitchState(param.enable_);
500 #endif // OHOS_BUILD_UNIVERSAL_DRAG
501     return RET_OK;
502 }
503 
SetAppDragSwitchState(CallingContext & context,MessageParcel & data,MessageParcel & reply)504 int32_t DragServer::SetAppDragSwitchState(CallingContext &context, MessageParcel &data, MessageParcel &reply)
505 {
506     SetAppDragSwitchStateParam param {};
507 
508     if (!param.Unmarshalling(data)) {
509         FI_HILOGE("SetAppDragSwitchStateParam::Unmarshalling fail");
510         return RET_ERR;
511     }
512     if (param.isJsCaller_ && !IsSystemHAPCalling(context)) {
513         FI_HILOGE("The caller is not system hap");
514         return COMMON_NOT_SYSTEM_APP;
515     }
516 #ifdef OHOS_BUILD_UNIVERSAL_DRAG
517     universalDragWrapper_.SetAppDragSwitchState(param.pkgName_, param.enable_);
518 #endif // OHOS_BUILD_UNIVERSAL_DRAG
519     return RET_OK;
520 }
521 
GetDragAction(CallingContext & context,MessageParcel & data,MessageParcel & reply)522 int32_t DragServer::GetDragAction(CallingContext &context, MessageParcel &data, MessageParcel &reply)
523 {
524     DragAction dragAction {};
525 
526     int32_t ret = env_->GetDragManager().GetDragAction(dragAction);
527     if (ret != RET_OK) {
528         FI_HILOGE("IDragManager::GetDragSummary fail, error:%{public}d", ret);
529         return ret;
530     }
531     GetDragActionReply dragActionReply { dragAction };
532 
533     if (!dragActionReply.Marshalling(reply)) {
534         FI_HILOGE("GetDragActionReply::Marshalling fail");
535         return RET_ERR;
536     }
537     return RET_OK;
538 }
539 
GetExtraInfo(CallingContext & context,MessageParcel & data,MessageParcel & reply)540 int32_t DragServer::GetExtraInfo(CallingContext &context, MessageParcel &data, MessageParcel &reply)
541 {
542     std::string extraInfo;
543 
544     int32_t ret = env_->GetDragManager().GetExtraInfo(extraInfo);
545     if (ret != RET_OK) {
546         FI_HILOGE("IDragManager::GetExtraInfo fail, error:%{public}d", ret);
547         return ret;
548     }
549     GetExtraInfoReply extraInfoReply { std::move(extraInfo) };
550 
551     if (!extraInfoReply.Marshalling(reply)) {
552         FI_HILOGE("GetExtraInfoReply::Marshalling fail");
553         return RET_ERR;
554     }
555     return RET_OK;
556 }
557 
EnterTextEditorArea(CallingContext & context,MessageParcel & data,MessageParcel & reply)558 int32_t DragServer::EnterTextEditorArea(CallingContext &context, MessageParcel &data, MessageParcel &reply)
559 {
560     EnterTextEditorAreaParam param {};
561 
562     if (!param.Unmarshalling(data)) {
563         FI_HILOGE("EnterTextEditorAreaParam::Unmarshalling fail");
564         return RET_ERR;
565     }
566     return env_->GetDragManager().EnterTextEditorArea(param.state);
567 }
568 
SetMouseDragMonitorState(CallingContext & context,MessageParcel & data,MessageParcel & reply)569 int32_t DragServer::SetMouseDragMonitorState(CallingContext &context, MessageParcel &data, MessageParcel &reply)
570 {
571     SetMouseDragMonitorStateParam param {};
572 
573     if (!param.Unmarshalling(data)) {
574         FI_HILOGE("SetMouseDragMonitorStateParam::Unmarshalling fail");
575         return RET_ERR;
576     }
577     return env_->GetDragManager().SetMouseDragMonitorState(param.state);
578 }
579 
GetPackageName(Security::AccessToken::AccessTokenID tokenId)580 std::string DragServer::GetPackageName(Security::AccessToken::AccessTokenID tokenId)
581 {
582     std::string packageName = std::string();
583     int32_t tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId);
584     switch (tokenType) {
585         case Security::AccessToken::ATokenTypeEnum::TOKEN_HAP: {
586             Security::AccessToken::HapTokenInfo hapInfo;
587             if (Security::AccessToken::AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo) != 0) {
588                 FI_HILOGE("Get hap token info failed");
589             } else {
590                 packageName = hapInfo.bundleName;
591             }
592             break;
593         }
594         case Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE:
595         case Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL: {
596             Security::AccessToken::NativeTokenInfo tokenInfo;
597             if (Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(tokenId, tokenInfo) != 0) {
598                 FI_HILOGE("Get native token info failed");
599             } else {
600                 packageName = tokenInfo.processName;
601             }
602             break;
603         }
604         default: {
605             FI_HILOGW("token type not match");
606             break;
607         }
608     }
609     return packageName;
610 }
611 
IsSystemServiceCalling(CallingContext & context)612 bool DragServer::IsSystemServiceCalling(CallingContext &context)
613 {
614     auto flag = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(context.tokenId);
615     if ((flag == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE) ||
616         (flag == Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL)) {
617         FI_HILOGI("system service calling, flag:%{public}u", flag);
618         return true;
619     }
620     return false;
621 }
622 
IsSystemHAPCalling(CallingContext & context)623 bool DragServer::IsSystemHAPCalling(CallingContext &context)
624 {
625     if (IsSystemServiceCalling(context)) {
626         return true;
627     }
628     return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(context.fullTokenId);
629 }
630 
SetDraggableState(CallingContext & context,MessageParcel & data,MessageParcel & reply)631 int32_t DragServer::SetDraggableState(CallingContext &context, MessageParcel &data, MessageParcel &reply)
632 {
633     SetDraggableStateParam param {};
634 
635     if (!param.Unmarshalling(data)) {
636         FI_HILOGE("SetDraggableStateParam::Unmarshalling fail");
637         return RET_ERR;
638     }
639 #ifdef OHOS_BUILD_UNIVERSAL_DRAG
640     universalDragWrapper_.SetDragableState(param.state_);
641 #endif // OHOS_BUILD_UNIVERSAL_DRAG
642     return RET_OK;
643 }
644 
GetAppDragSwitchState(CallingContext & context,MessageParcel & data,MessageParcel & reply)645 int32_t DragServer::GetAppDragSwitchState(CallingContext &context, MessageParcel &data, MessageParcel &reply)
646 {
647 #ifdef OHOS_BUILD_UNIVERSAL_DRAG
648     bool state = false;
649     int32_t ret = universalDragWrapper_.GetAppDragSwitchState(GetPackageName(context.tokenId), state);
650     if (ret != RET_OK) {
651         FI_HILOGE("universalDragWrapper GetAppDragSwitchState fail, error:%{public}d", ret);
652         return ret;
653     }
654     GetUniversalDragAppStateReply getUniversalDragAppStateReply { state };
655 
656     if (!getUniversalDragAppStateReply.Marshalling(reply)) {
657         FI_HILOGE("GetUniversalDragAppStateReply::Marshalling fail");
658         return RET_ERR;
659     }
660 #endif // OHOS_BUILD_UNIVERSAL_DRAG
661     return RET_OK;
662 }
663 
SetDraggableStateAsync(CallingContext & context,MessageParcel & data)664 int32_t DragServer::SetDraggableStateAsync(CallingContext &context, MessageParcel &data)
665 {
666 #ifdef OHOS_BUILD_UNIVERSAL_DRAG
667     CHKPR(env_, RET_ERR);
668     SetDraggableStateAsyncParam param {};
669     if (!param.Unmarshalling(data)) {
670         FI_HILOGE("SetDraggableStateAsync::Unmarshalling fail");
671         return RET_ERR;
672     }
673     env_->GetDelegateTasks().PostAsyncTask([this, param] {
674         this->universalDragWrapper_.SetDraggableStateAsync(param.state_, param.downTime_);
675         return RET_OK;
676     });
677 #endif // OHOS_BUILD_UNIVERSAL_DRAG
678     return RET_OK;
679 }
680 } // namespace DeviceStatus
681 } // namespace Msdp
682 } // namespace OHOS