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