summaryrefslogtreecommitdiffstats
path: root/src/VBox/Main/src-client/GuestDnDPrivate.cpp
blob: 3007e181304ad01382687efc614dec05be15aa56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
/* $Id: GuestDnDPrivate.cpp $ */
/** @file
 * Private guest drag and drop code, used by GuestDnDTarget + GuestDnDSource.
 */

/*
 * Copyright (C) 2011-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#define LOG_GROUP LOG_GROUP_GUEST_DND
#include "LoggingNew.h"

#include "GuestImpl.h"
#include "AutoCaller.h"

#ifdef VBOX_WITH_DRAG_AND_DROP
# include "ConsoleImpl.h"
# include "ProgressImpl.h"
# include "GuestDnDPrivate.h"

# include <algorithm>

# include <iprt/dir.h>
# include <iprt/path.h>
# include <iprt/stream.h>
# include <iprt/semaphore.h>
# include <iprt/cpp/utils.h>

# include <VMMDev.h>

# include <VBox/GuestHost/DragAndDrop.h>
# include <VBox/HostServices/DragAndDropSvc.h>
# include <VBox/version.h>

/** @page pg_main_dnd  Dungeons & Dragons - Overview
 * Overview:
 *
 * Drag and Drop is handled over the internal HGCM service for the host <->
 * guest communication. Beside that we need to map the Drag and Drop protocols
 * of the various OS's we support to our internal channels, this is also highly
 * communicative in both directions. Unfortunately HGCM isn't really designed
 * for that. Next we have to foul some of the components. This includes to
 * trick X11 on the guest side, but also Qt needs to be tricked on the host
 * side a little bit.
 *
 * The following components are involved:
 *
 * 1. GUI: Uses the Qt classes for Drag and Drop and mainly forward the content
 *    of it to the Main IGuest / IGuestDnDSource / IGuestDnDTarget interfaces.
 * 2. Main: Public interface for doing Drag and Drop. Also manage the IProgress
 *    interfaces for blocking the caller by showing a progress dialog (see
 *    this file).
 * 3. HGCM service: Handle all messages from the host to the guest at once and
 *    encapsulate the internal communication details (see dndmanager.cpp and
 *    friends).
 * 4. Guest Additions: Split into the platform neutral part (see
 *    VBoxGuestR3LibDragAndDrop.cpp) and the guest OS specific parts.
 *    Receive/send message from/to the HGCM service and does all guest specific
 *    operations. For Windows guests VBoxTray is in charge, whereas on UNIX-y guests
 *    VBoxClient will be used.
 *
 * Terminology:
 *
 * All transfers contain a MIME format and according meta data. This meta data then can
 * be interpreted either as raw meta data or something else. When raw meta data is
 * being handled, this gets passed through to the destination (guest / host) without
 * modification. Other meta data (like URI lists) can and will be modified by the
 * receiving side before passing to OS. How and when modifications will be applied
 * depends on the MIME format.
 *
 * Host -> Guest:
 * 1. There are DnD Enter, Move, Leave events which are send exactly like this
 *    to the guest. The info includes the position, MIME types and allowed actions.
 *    The guest has to respond with an action it would accept, so the GUI could
 *    change the cursor accordingly.
 * 2. On drop, first a drop event is sent. If this is accepted a drop data
 *    event follows. This blocks the GUI and shows some progress indicator.
 *
 * Guest -> Host:
 * 1. The GUI is asking the guest if a DnD event is pending when the user moves
 *    the cursor out of the view window. If so, this returns the mimetypes and
 *    allowed actions.
 * (2. On every mouse move this is asked again, to make sure the DnD event is
 *     still valid.)
 * 3. On drop the host request the data from the guest. This blocks the GUI and
 *    shows some progress indicator.
 *
 * Implementation hints:
 * m_strSupportedFormats here in this file defines the allowed mime-types.
 * This is necessary because we need special handling for some of the
 * mime-types. E.g. for URI lists we need to transfer the actual dirs and
 * files. Text EOL may to be changed. Also unknown mime-types may need special
 * handling as well, which may lead to undefined behavior in the host/guest, if
 * not done.
 *
 * Dropping of a directory, means recursively transferring _all_ the content.
 *
 * Directories and files are placed into the user's temporary directory on the
 * guest (e.g. /tmp/VirtualBox Dropped Files). We can't delete them after the
 * DnD operation, because we didn't know what the DnD target does with it. E.g.
 * it could just be opened in place. This could lead ofc to filling up the disk
 * within the guest. To inform the user about this, a small app could be
 * developed which scans this directory regularly and inform the user with a
 * tray icon hint (and maybe the possibility to clean this up instantly). The
 * same has to be done in the G->H direction when it is implemented.
 *
 * Only regular files are supported; symlinks are not allowed.
 *
 * Transfers currently are an all-succeed or all-fail operation (see todos).
 *
 * On MacOS hosts we had to implement own DnD "promises" support for file transfers,
 * as Qt does not support this out-of-the-box.
 *
 * The code tries to preserve the file modes of the transfered directories / files.
 * This is useful (and maybe necessary) for two things:
 * 1. If a file is executable, it should be also after the transfer, so the
 *    user can just execute it, without manually tweaking the modes first.
 * 2. If a dir/file is not accessible by group/others in the host, it shouldn't
 *    be in the guest.
 * In any case, the user mode is always set to rwx (so that we can access it
 * ourself, in e.g. for a cleanup case after cancel).
 *
 * ACEs / ACLs currently are not supported.
 *
 * Cancelling ongoing transfers is supported in both directions by the guest
 * and/or host side and cleans up all previous steps. This also involves
 * removing partially transferred directories / files in the temporary directory.
 *
 ** @todo
 * - ESC doesn't really work (on Windows guests it's already implemented)
 *   ... in any case it seems a little bit difficult to handle from the Qt side.
 * - Transfers currently do not have any interactive (UI) callbacks / hooks which
 *   e.g. would allow to skip / replace / rename and entry, or abort the operation on failure.
 * - Add support for more MIME types (especially images, csv)
 * - Test unusual behavior:
 *   - DnD service crash in the guest during a DnD op (e.g. crash of VBoxClient or X11)
 *   - Not expected order of the events between HGCM and the guest
 * - Security considerations: We transfer a lot of memory between the guest and
 *   the host and even allow the creation of dirs/files. Maybe there should be
 *   limits introduced to preventing DoS attacks or filling up all the memory
 *   (both in the host and the guest).
 */


/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/

/** Tries locking the GuestDnD object and returns on failure. */
#define GUESTDND_LOCK() do { \
        int const vrcLock = RTCritSectEnter(&m_CritSect); \
        if (RT_SUCCESS(vrcLock)) { /* likely */ } else return vrcLock; \
    } while (0)

/** Tries locking the GuestDnD object and returns a_Ret failure. */
#define GUESTDND_LOCK_RET(a_Ret) do { \
        int const vrcLock = RTCritSectEnter(&m_CritSect); \
        if (RT_SUCCESS(vrcLock)) { /* likely */ } else return vrcLock; \
    } while (0)

/** Unlocks a formerly locked GuestDnD object. */
#define GUESTDND_UNLOCK() do { \
        int const vrcUnlock = RTCritSectLeave(&m_CritSect); \
        AssertRC(vrcUnlock); \
    } while (0)


/*********************************************************************************************************************************
*   GuestDnDSendCtx implementation.                                                                                              *
*********************************************************************************************************************************/

GuestDnDSendCtx::GuestDnDSendCtx(void)
    : pTarget(NULL)
    , pState(NULL)
{
    reset();
}

/**
 * Resets a GuestDnDSendCtx object.
 */
void GuestDnDSendCtx::reset(void)
{
    uScreenID  = 0;

    Transfer.reset();

    int vrc2 = EventCallback.Reset();
    AssertRC(vrc2);

    GuestDnDData::reset();
}


/*********************************************************************************************************************************
*   GuestDnDRecvCtx implementation.                                                                                              *
*********************************************************************************************************************************/

GuestDnDRecvCtx::GuestDnDRecvCtx(void)
    : pSource(NULL)
    , pState(NULL)
{
    reset();
}

/**
 * Resets a GuestDnDRecvCtx object.
 */
void GuestDnDRecvCtx::reset(void)
{
    lstFmtOffered.clear();
    strFmtReq  = "";
    strFmtRecv = "";
    enmAction  = 0;

    Transfer.reset();

    int vrc2 = EventCallback.Reset();
    AssertRC(vrc2);

    GuestDnDData::reset();
}


/*********************************************************************************************************************************
*   GuestDnDCallbackEvent implementation.                                                                                        *
*********************************************************************************************************************************/

GuestDnDCallbackEvent::~GuestDnDCallbackEvent(void)
{
    if (NIL_RTSEMEVENT != m_SemEvent)
        RTSemEventDestroy(m_SemEvent);
}

/**
 * Resets a GuestDnDCallbackEvent object.
 */
int GuestDnDCallbackEvent::Reset(void)
{
    int vrc = VINF_SUCCESS;

    if (m_SemEvent == NIL_RTSEMEVENT)
        vrc = RTSemEventCreate(&m_SemEvent);

    m_vrc = VINF_SUCCESS;
    return vrc;
}

/**
 * Completes a callback event by notifying the waiting side.
 *
 * @returns VBox status code.
 * @param   vrc                 Result code to use for the event completion.
 */
int GuestDnDCallbackEvent::Notify(int vrc /* = VINF_SUCCESS */)
{
    m_vrc = vrc;
    return RTSemEventSignal(m_SemEvent);
}

/**
 * Waits on a callback event for being notified.
 *
 * @returns VBox status code.
 * @param   msTimeout           Timeout (in ms) to wait for callback event.
 */
int GuestDnDCallbackEvent::Wait(RTMSINTERVAL msTimeout)
{
    return RTSemEventWait(m_SemEvent, msTimeout);
}


/*********************************************************************************************************************************
*   GuestDnDState implementation                                                                                                 *
*********************************************************************************************************************************/

GuestDnDState::GuestDnDState(const ComObjPtr<Guest>& pGuest)
    : m_uProtocolVersion(0)
    , m_fGuestFeatures0(VBOX_DND_GF_NONE)
    , m_EventSem(NIL_RTSEMEVENT)
    , m_pParent(pGuest)
{
    reset();

    int vrc = RTCritSectInit(&m_CritSect);
    if (RT_FAILURE(vrc))
        throw vrc;
    vrc = RTSemEventCreate(&m_EventSem);
    if (RT_FAILURE(vrc))
        throw vrc;
}

GuestDnDState::~GuestDnDState(void)
{
    int vrc = RTSemEventDestroy(m_EventSem);
    AssertRC(vrc);
    vrc = RTCritSectDelete(&m_CritSect);
    AssertRC(vrc);
}

/**
 * Notifies the waiting side about a guest notification response.
 *
 * @returns VBox status code.
 * @param   vrcGuest    Guest VBox status code to set for the response.
 *                      Defaults to VINF_SUCCESS (for success).
 */
int GuestDnDState::notifyAboutGuestResponse(int vrcGuest /* = VINF_SUCCESS */)
{
    m_vrcGuest = vrcGuest;
    return RTSemEventSignal(m_EventSem);
}

/**
 * Resets a guest drag'n drop state.
 */
void GuestDnDState::reset(void)
{
    LogRel2(("DnD: Reset\n"));

    m_enmState =  VBOXDNDSTATE_UNKNOWN;

    m_dndActionDefault     = VBOX_DND_ACTION_IGNORE;
    m_dndLstActionsAllowed = VBOX_DND_ACTION_IGNORE;

    m_lstFormats.clear();
    m_mapCallbacks.clear();

    m_vrcGuest = VERR_IPE_UNINITIALIZED_STATUS;
}

/**
 * Default callback handler for guest callbacks.
 *
 * This handler acts as a fallback in case important callback messages are not being handled
 * by the specific callers.
 *
 * @returns VBox status code. Will get sent back to the host service.
 * @retval  VERR_NO_DATA if no new messages from the host side are available at the moment.
 * @retval  VERR_CANCELLED for indicating that the current operation was cancelled.
 * @param   uMsg                HGCM message ID (function number).
 * @param   pvParms             Pointer to additional message data. Optional and can be NULL.
 * @param   cbParms             Size (in bytes) additional message data. Optional and can be 0.
 * @param   pvUser              User-supplied pointer on callback registration.
 */
/* static */
DECLCALLBACK(int) GuestDnDState::i_defaultCallback(uint32_t uMsg, void *pvParms, size_t cbParms, void *pvUser)
{
    GuestDnDState *pThis = (GuestDnDState *)pvUser;
    AssertPtrReturn(pThis, VERR_INVALID_POINTER);

    LogFlowFunc(("uMsg=%RU32 (%#x)\n", uMsg, uMsg));

    int vrc = VERR_IPE_UNINITIALIZED_STATUS;

    switch (uMsg)
    {
        case GUEST_DND_FN_EVT_ERROR:
        {
            PVBOXDNDCBEVTERRORDATA pCBData = reinterpret_cast<PVBOXDNDCBEVTERRORDATA>(pvParms);
            AssertPtr(pCBData);
            AssertReturn(sizeof(VBOXDNDCBEVTERRORDATA) == cbParms, VERR_INVALID_PARAMETER);
            AssertReturn(CB_MAGIC_DND_EVT_ERROR == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);

            if (RT_SUCCESS(pCBData->rc))
            {
                AssertMsgFailed(("Guest has sent an error event but did not specify an actual error code\n"));
                pCBData->rc = VERR_GENERAL_FAILURE; /* Make sure some error is set. */
            }

            vrc = pThis->setProgress(100, DND_PROGRESS_ERROR, pCBData->rc,
                                     Utf8StrFmt("Received error from guest: %Rrc", pCBData->rc));
            AssertRCBreak(vrc);
            vrc = pThis->notifyAboutGuestResponse(pCBData->rc);
            AssertRCBreak(vrc);
            break;
        }

        case GUEST_DND_FN_GET_NEXT_HOST_MSG:
            vrc = VERR_NO_DATA; /* Indicate back to the host service that there are no new messages. */
            break;

        default:
            AssertMsgBreakStmt(pThis->isProgressRunning() == false,
                               ("Progress object not completed / canceld yet! State is '%s' (%#x)\n",
                                DnDStateToStr(pThis->m_enmState), pThis->m_enmState),
                               vrc = VERR_INVALID_STATE); /* Please report this! */
            vrc = VERR_CANCELLED;
            break;
    }

    LogFlowFunc(("Returning vrc=%Rrc\n", vrc));
    return vrc;
}

/**
 * Resets the progress object.
 *
 * @returns HRESULT
 * @param   pParent             Parent to set for the progress object.
 * @param   strDesc             Description of the progress.
 */
HRESULT GuestDnDState::resetProgress(const ComObjPtr<Guest>& pParent, const Utf8Str &strDesc)
{
    AssertReturn(strDesc.isNotEmpty(), E_INVALIDARG);

    m_pProgress.setNull();

    HRESULT hrc = m_pProgress.createObject();
    if (SUCCEEDED(hrc))
        hrc = m_pProgress->init(static_cast<IGuest *>(pParent), Bstr(strDesc).raw(), TRUE /* aCancelable */);

    return hrc;
}

/**
 * Returns whether the progress object has been canceled or not.
 *
 * @returns \c true if canceled or progress does not exist, \c false if not.
 */
bool GuestDnDState::isProgressCanceled(void) const
{
    if (m_pProgress.isNull())
        return true;

    BOOL fCanceled;
    HRESULT hrc = m_pProgress->COMGETTER(Canceled)(&fCanceled);
    AssertComRCReturn(hrc, false);
    return RT_BOOL(fCanceled);
}

/**
 * Returns whether the progress object still is in a running state or not.
 *
 * @returns \c true if running, \c false if not.
 */
bool GuestDnDState::isProgressRunning(void) const
{
    if (m_pProgress.isNull())
        return false;

    BOOL fCompleted;
    HRESULT const hrc = m_pProgress->COMGETTER(Completed)(&fCompleted);
    AssertComRCReturn(hrc, false);
    return !RT_BOOL(fCompleted);
}

/**
 * Sets (registers or unregisters) a callback for a specific HGCM message.
 *
 * @returns VBox status code.
 * @param   uMsg                HGCM message ID to set callback for.
 * @param   pfnCallback         Callback function pointer to use. Pass NULL to unregister.
 * @param   pvUser              User-provided arguments for the callback function. Optional and can be NULL.
 */
int GuestDnDState::setCallback(uint32_t uMsg, PFNGUESTDNDCALLBACK pfnCallback, void *pvUser /* = NULL */)
{
    GuestDnDCallbackMap::iterator it = m_mapCallbacks.find(uMsg);

    /* Register. */
    if (pfnCallback)
    {
        try
        {
            m_mapCallbacks[uMsg] = GuestDnDCallback(pfnCallback, uMsg, pvUser);
        }
        catch (std::bad_alloc &)
        {
            return VERR_NO_MEMORY;
        }
        return VINF_SUCCESS;
    }

    /* Unregister. */
    if (it != m_mapCallbacks.end())
        m_mapCallbacks.erase(it);

    return VINF_SUCCESS;
}

/**
 * Sets the progress object to a new state.
 *
 * @returns VBox status code.
 * @param   uPercentage         Percentage (0-100) to set.
 * @param   uStatus             Status (of type DND_PROGRESS_XXX) to set.
 * @param   vrcOp               VBox status code to set. Optional.
 * @param   strMsg              Message to set. Optional.
 */
int GuestDnDState::setProgress(unsigned uPercentage, uint32_t uStatus,
                               int vrcOp /* = VINF_SUCCESS */, const Utf8Str &strMsg /* = Utf8Str::Empty */)
{
    LogFlowFunc(("uPercentage=%u, uStatus=%RU32, , vrcOp=%Rrc, strMsg=%s\n",
                 uPercentage, uStatus, vrcOp, strMsg.c_str()));

    if (m_pProgress.isNull())
        return VINF_SUCCESS;

    BOOL fCompleted = FALSE;
    HRESULT hrc = m_pProgress->COMGETTER(Completed)(&fCompleted);
    AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);

    BOOL fCanceled  = FALSE;
    hrc = m_pProgress->COMGETTER(Canceled)(&fCanceled);
    AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);

    LogFlowFunc(("Progress fCompleted=%RTbool, fCanceled=%RTbool\n", fCompleted, fCanceled));

    int vrc = VINF_SUCCESS;

    switch (uStatus)
    {
        case DragAndDropSvc::DND_PROGRESS_ERROR:
        {
            LogRel(("DnD: Guest reported error %Rrc\n", vrcOp));

            if (!fCompleted)
                hrc = m_pProgress->i_notifyComplete(VBOX_E_DND_ERROR, COM_IIDOF(IGuest),
                                                    m_pParent->getComponentName(), strMsg.c_str());
            break;
        }

        case DragAndDropSvc::DND_PROGRESS_CANCELLED:
        {
            LogRel2(("DnD: Guest cancelled operation\n"));

            if (!fCanceled)
            {
                hrc = m_pProgress->Cancel();
                AssertComRC(hrc);
            }

            if (!fCompleted)
            {
                hrc = m_pProgress->i_notifyComplete(S_OK);
                AssertComRC(hrc);
            }
            break;
        }

        case DragAndDropSvc::DND_PROGRESS_RUNNING:
            RT_FALL_THROUGH();
        case DragAndDropSvc::DND_PROGRESS_COMPLETE:
        {
            LogRel2(("DnD: Guest reporting running/completion status with %u%%\n", uPercentage));

            if (   !fCompleted
                && !fCanceled)
            {
                hrc = m_pProgress->SetCurrentOperationProgress(uPercentage);
                AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
                if (   uStatus     == DragAndDropSvc::DND_PROGRESS_COMPLETE
                    || uPercentage >= 100)
                {
                    hrc = m_pProgress->i_notifyComplete(S_OK);
                    AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
                }
            }
            break;
        }

        default:
            break;
    }

    LogFlowFuncLeaveRC(vrc);
    return vrc;
}

/**
 * Dispatching function for handling the host service service callback.
 *
 * @returns VBox status code.
 * @param   u32Function         HGCM message ID to handle.
 * @param   pvParms             Pointer to optional data provided for a particular message. Optional.
 * @param   cbParms             Size (in bytes) of \a pvParms.
 */
int GuestDnDState::onDispatch(uint32_t u32Function, void *pvParms, uint32_t cbParms)
{
    LogFlowFunc(("u32Function=%RU32, pvParms=%p, cbParms=%RU32\n", u32Function, pvParms, cbParms));

    int vrc = VERR_WRONG_ORDER; /* Play safe. */

    /* Whether or not to try calling host-installed callbacks after successfully processing the message. */
    bool fTryCallbacks = false;

    switch (u32Function)
    {
        case DragAndDropSvc::GUEST_DND_FN_CONNECT:
        {
            DragAndDropSvc::PVBOXDNDCBCONNECTDATA pCBData = reinterpret_cast<DragAndDropSvc::PVBOXDNDCBCONNECTDATA>(pvParms);
            AssertPtr(pCBData);
            AssertReturn(sizeof(DragAndDropSvc::VBOXDNDCBCONNECTDATA) == cbParms, VERR_INVALID_PARAMETER);
            AssertReturn(DragAndDropSvc::CB_MAGIC_DND_CONNECT == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);

            m_uProtocolVersion = pCBData->uProtocolVersion;
            /** @todo Handle flags. */

            LogThisFunc(("Client connected, using protocol v%RU32\n", m_uProtocolVersion));

            vrc = VINF_SUCCESS;
            break;
        }

        case DragAndDropSvc::GUEST_DND_FN_REPORT_FEATURES:
        {
            DragAndDropSvc::PVBOXDNDCBREPORTFEATURESDATA pCBData = reinterpret_cast<DragAndDropSvc::PVBOXDNDCBREPORTFEATURESDATA>(pvParms);
            AssertPtr(pCBData);
            AssertReturn(sizeof(DragAndDropSvc::VBOXDNDCBREPORTFEATURESDATA) == cbParms, VERR_INVALID_PARAMETER);
            AssertReturn(DragAndDropSvc::CB_MAGIC_DND_REPORT_FEATURES == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);

            m_fGuestFeatures0 = pCBData->fGuestFeatures0;

            LogThisFunc(("Client reported features: %#RX64\n", m_fGuestFeatures0));

            vrc = VINF_SUCCESS;
            break;
        }

        /* Note: GUEST_DND_FN_EVT_ERROR is handled in either the state's default callback or in specific
         *       (overriden) callbacks (e.g. GuestDnDSendCtx / GuestDnDRecvCtx). */

        case DragAndDropSvc::GUEST_DND_FN_DISCONNECT:
        {
            LogThisFunc(("Client disconnected\n"));
            vrc = setProgress(100, DND_PROGRESS_CANCELLED, VINF_SUCCESS);
            break;
        }

        case DragAndDropSvc::GUEST_DND_FN_HG_ACK_OP:
        {
            DragAndDropSvc::PVBOXDNDCBHGACKOPDATA pCBData = reinterpret_cast<DragAndDropSvc::PVBOXDNDCBHGACKOPDATA>(pvParms);
            AssertPtr(pCBData);
            AssertReturn(sizeof(DragAndDropSvc::VBOXDNDCBHGACKOPDATA) == cbParms, VERR_INVALID_PARAMETER);
            AssertReturn(DragAndDropSvc::CB_MAGIC_DND_HG_ACK_OP == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);

            LogRel2(("DnD: Guest responded with action '%s' for host->guest drag event\n", DnDActionToStr(pCBData->uAction)));

            setActionDefault(pCBData->uAction);
            vrc = notifyAboutGuestResponse();
            break;
        }

        case DragAndDropSvc::GUEST_DND_FN_HG_REQ_DATA:
        {
            DragAndDropSvc::PVBOXDNDCBHGREQDATADATA pCBData = reinterpret_cast<DragAndDropSvc::PVBOXDNDCBHGREQDATADATA>(pvParms);
            AssertPtr(pCBData);
            AssertReturn(sizeof(DragAndDropSvc::VBOXDNDCBHGREQDATADATA) == cbParms, VERR_INVALID_PARAMETER);
            AssertReturn(DragAndDropSvc::CB_MAGIC_DND_HG_REQ_DATA == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);

            if (   pCBData->cbFormat  == 0
                || pCBData->cbFormat  > _64K /** @todo Make this configurable? */
                || pCBData->pszFormat == NULL)
                vrc = VERR_INVALID_PARAMETER;
            else if (!RTStrIsValidEncoding(pCBData->pszFormat))
                vrc = VERR_INVALID_PARAMETER;
            else
            {
                setFormats(GuestDnD::toFormatList(pCBData->pszFormat));
                vrc = VINF_SUCCESS;
            }

            int vrc2 = notifyAboutGuestResponse();
            if (RT_SUCCESS(vrc))
                vrc = vrc2;
            break;
        }

        case DragAndDropSvc::GUEST_DND_FN_HG_EVT_PROGRESS:
        {
            DragAndDropSvc::PVBOXDNDCBHGEVTPROGRESSDATA pCBData =
               reinterpret_cast<DragAndDropSvc::PVBOXDNDCBHGEVTPROGRESSDATA>(pvParms);
            AssertPtr(pCBData);
            AssertReturn(sizeof(DragAndDropSvc::VBOXDNDCBHGEVTPROGRESSDATA) == cbParms, VERR_INVALID_PARAMETER);
            AssertReturn(DragAndDropSvc::CB_MAGIC_DND_HG_EVT_PROGRESS == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);

            vrc = setProgress(pCBData->uPercentage, pCBData->uStatus, pCBData->rc);
            if (RT_SUCCESS(vrc))
                vrc = notifyAboutGuestResponse(pCBData->rc);
            break;
        }
#ifdef VBOX_WITH_DRAG_AND_DROP_GH
        case DragAndDropSvc::GUEST_DND_FN_GH_ACK_PENDING:
        {
            DragAndDropSvc::PVBOXDNDCBGHACKPENDINGDATA pCBData =
               reinterpret_cast<DragAndDropSvc::PVBOXDNDCBGHACKPENDINGDATA>(pvParms);
            AssertPtr(pCBData);
            AssertReturn(sizeof(DragAndDropSvc::VBOXDNDCBGHACKPENDINGDATA) == cbParms, VERR_INVALID_PARAMETER);
            AssertReturn(DragAndDropSvc::CB_MAGIC_DND_GH_ACK_PENDING == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);

            LogRel2(("DnD: Guest responded with pending action '%s' (%RU32 bytes format data) to guest->host drag event\n",
                     DnDActionToStr(pCBData->uDefAction), pCBData->cbFormat));

            if (   pCBData->cbFormat  == 0
                || pCBData->cbFormat  > _64K /** @todo Make the maximum size configurable? */
                || pCBData->pszFormat == NULL)
                vrc = VERR_INVALID_PARAMETER;
            else if (!RTStrIsValidEncoding(pCBData->pszFormat))
                vrc = VERR_INVALID_PARAMETER;
            else
            {
                setFormats   (GuestDnD::toFormatList(pCBData->pszFormat));
                setActionDefault (pCBData->uDefAction);
                setActionsAllowed(pCBData->uAllActions);

                vrc = VINF_SUCCESS;
            }

            int vrc2 = notifyAboutGuestResponse();
            if (RT_SUCCESS(vrc))
                vrc = vrc2;
            break;
        }
#endif /* VBOX_WITH_DRAG_AND_DROP_GH */
        default:
            /* * Try if the event is covered by a registered callback. */
            fTryCallbacks = true;
            break;
    }

    /*
     * Try the host's installed callbacks (if any).
     */
    if (fTryCallbacks)
    {
        GuestDnDCallbackMap::const_iterator it = m_mapCallbacks.find(u32Function);
        if (it != m_mapCallbacks.end())
        {
            AssertPtr(it->second.pfnCallback);
            vrc = it->second.pfnCallback(u32Function, pvParms, cbParms, it->second.pvUser);
        }
        else
        {
            /* Invoke the default callback handler in case we don't have any registered callback above. */
            vrc = i_defaultCallback(u32Function, pvParms, cbParms, this /* pvUser */);
        }
    }

    LogFlowFunc(("Returning vrc=%Rrc\n", vrc));
    return vrc;
}

/**
 * Helper function to query the internal progress object to an IProgress interface.
 *
 * @returns HRESULT
 * @param   ppProgress          Where to query the progress object to.
 */
HRESULT GuestDnDState::queryProgressTo(IProgress **ppProgress)
{
    return m_pProgress.queryInterfaceTo(ppProgress);
}

/**
 * Waits for a guest response to happen, extended version.
 *
 * @returns VBox status code.
 * @retval  VERR_TIMEOUT when waiting has timed out.
 * @retval  VERR_DND_GUEST_ERROR on an error reported back from the guest.
 * @param   msTimeout           Timeout (in ms) for waiting. Optional, waits 3000 ms if not specified.
 * @param   pvrcGuest           Where to return the guest error when
 *                              VERR_DND_GUEST_ERROR is returned. Optional.
 */
int GuestDnDState::waitForGuestResponseEx(RTMSINTERVAL msTimeout /* = 3000 */, int *pvrcGuest /* = NULL */)
{
    int vrc = RTSemEventWait(m_EventSem, msTimeout);
    if (RT_SUCCESS(vrc))
    {
        if (RT_FAILURE(m_vrcGuest))
            vrc = VERR_DND_GUEST_ERROR;
        if (pvrcGuest)
            *pvrcGuest = m_vrcGuest;
    }
    return vrc;
}

/**
 * Waits for a guest response to happen.
 *
 * @returns VBox status code.
 * @retval  VERR_TIMEOUT when waiting has timed out.
 * @retval  VERR_DND_GUEST_ERROR on an error reported back from the guest.
 * @param   pvrcGuest            Where to return the guest error when
 *                               VERR_DND_GUEST_ERROR is returned. Optional.
 *
 * @note    Uses the default timeout of 3000 ms.
 */
int GuestDnDState::waitForGuestResponse(int *pvrcGuest /* = NULL */)
{
    return waitForGuestResponseEx(3000 /* ms */, pvrcGuest);
}

/*********************************************************************************************************************************
 * GuestDnD implementation.                                                                                                      *
 ********************************************************************************************************************************/

/** Static (Singleton) instance of the GuestDnD object. */
GuestDnD* GuestDnD::s_pInstance = NULL;

GuestDnD::GuestDnD(const ComObjPtr<Guest> &pGuest)
    : m_pGuest(pGuest)
    , m_cTransfersPending(0)
{
    LogFlowFuncEnter();

    try
    {
        m_pState = new GuestDnDState(pGuest);
    }
    catch (std::bad_alloc &)
    {
        throw VERR_NO_MEMORY;
    }

    int vrc = RTCritSectInit(&m_CritSect);
    if (RT_FAILURE(vrc))
        throw vrc;

    /* List of supported default MIME types. */
    LogRel2(("DnD: Supported default host formats:\n"));
    const com::Utf8Str arrEntries[] = { VBOX_DND_FORMATS_DEFAULT };
    for (size_t i = 0; i < RT_ELEMENTS(arrEntries); i++)
    {
        m_strDefaultFormats.push_back(arrEntries[i]);
        LogRel2(("DnD: \t%s\n", arrEntries[i].c_str()));
    }
}

GuestDnD::~GuestDnD(void)
{
    LogFlowFuncEnter();

    Assert(m_cTransfersPending == 0); /* Sanity. */

    RTCritSectDelete(&m_CritSect);

    if (m_pState)
        delete m_pState;
}

/**
 * Adjusts coordinations to a given screen.
 *
 * @returns HRESULT
 * @param   uScreenId           ID of screen to adjust coordinates to.
 * @param   puX                 Pointer to X coordinate to adjust. Will return the adjusted value on success.
 * @param   puY                 Pointer to Y coordinate to adjust. Will return the adjusted value on success.
 */
HRESULT GuestDnD::adjustScreenCoordinates(ULONG uScreenId, ULONG *puX, ULONG *puY) const
{
    /** @todo r=andy Save the current screen's shifting coordinates to speed things up.
     *               Only query for new offsets when the screen ID or the screen's resolution has changed. */

    /* For multi-monitor support we need to add shift values to the coordinates
     * (depending on the screen number). */
    ComObjPtr<Console> pConsole = m_pGuest->i_getConsole();
    ComPtr<IDisplay> pDisplay;
    HRESULT hrc = pConsole->COMGETTER(Display)(pDisplay.asOutParam());
    if (FAILED(hrc))
        return hrc;

    ULONG dummy;
    LONG xShift, yShift;
    GuestMonitorStatus_T monitorStatus;
    hrc = pDisplay->GetScreenResolution(uScreenId, &dummy, &dummy, &dummy, &xShift, &yShift, &monitorStatus);
    if (FAILED(hrc))
        return hrc;

    if (puX)
        *puX += xShift;
    if (puY)
        *puY += yShift;

    LogFlowFunc(("uScreenId=%RU32, x=%RU32, y=%RU32\n", uScreenId, puX ? *puX : 0, puY ? *puY : 0));
    return S_OK;
}

/**
 * Returns a DnD guest state.
 *
 * @returns Pointer to DnD guest state, or NULL if not found / invalid.
 * @param   uID                 ID of DnD guest state to return.
 */
GuestDnDState *GuestDnD::getState(uint32_t uID /* = 0 */) const
{
    AssertMsgReturn(uID == 0, ("Only one state (0) is supported at the moment\n"), NULL);

    return m_pState;
}

/**
 * Sends a (blocking) message to the host side of the host service.
 *
 * @returns VBox status code.
 * @param   u32Function         HGCM message ID to send.
 * @param   cParms              Number of parameters to send.
 * @param   paParms             Array of parameters to send. Must match \c cParms.
 */
int GuestDnD::hostCall(uint32_t u32Function, uint32_t cParms, PVBOXHGCMSVCPARM paParms) const
{
    Assert(!m_pGuest.isNull());
    ComObjPtr<Console> pConsole = m_pGuest->i_getConsole();

    /* Forward the information to the VMM device. */
    Assert(!pConsole.isNull());
    VMMDev *pVMMDev = pConsole->i_getVMMDev();
    if (!pVMMDev)
        return VERR_COM_OBJECT_NOT_FOUND;

    return pVMMDev->hgcmHostCall("VBoxDragAndDropSvc", u32Function, cParms, paParms);
}

/**
 * Registers a GuestDnDSource object with the GuestDnD manager.
 *
 * Currently only one source is supported at a time.
 *
 * @returns VBox status code.
 * @param   Source              Source to register.
 */
int GuestDnD::registerSource(const ComObjPtr<GuestDnDSource> &Source)
{
    GUESTDND_LOCK();

    Assert(m_lstSrc.size() == 0); /* We only support one source at a time at the moment. */
    m_lstSrc.push_back(Source);

    GUESTDND_UNLOCK();
    return VINF_SUCCESS;
}

/**
 * Unregisters a GuestDnDSource object from the GuestDnD manager.
 *
 * @returns VBox status code.
 * @param   Source              Source to unregister.
 */
int GuestDnD::unregisterSource(const ComObjPtr<GuestDnDSource> &Source)
{
    GUESTDND_LOCK();

    GuestDnDSrcList::iterator itSrc = std::find(m_lstSrc.begin(), m_lstSrc.end(), Source);
    if (itSrc != m_lstSrc.end())
        m_lstSrc.erase(itSrc);

    GUESTDND_UNLOCK();
    return VINF_SUCCESS;
}

/**
 * Returns the current number of registered sources.
 *
 * @returns Current number of registered sources.
 */
size_t GuestDnD::getSourceCount(void)
{
    GUESTDND_LOCK_RET(0);

    size_t cSources = m_lstSrc.size();

    GUESTDND_UNLOCK();
    return cSources;
}

/**
 * Registers a GuestDnDTarget object with the GuestDnD manager.
 *
 * Currently only one target is supported at a time.
 *
 * @returns VBox status code.
 * @param   Target              Target to register.
 */
int GuestDnD::registerTarget(const ComObjPtr<GuestDnDTarget> &Target)
{
    GUESTDND_LOCK();

    Assert(m_lstTgt.size() == 0); /* We only support one target at a time at the moment. */
    m_lstTgt.push_back(Target);

    GUESTDND_UNLOCK();
    return VINF_SUCCESS;
}

/**
 * Unregisters a GuestDnDTarget object from the GuestDnD manager.
 *
 * @returns VBox status code.
 * @param   Target              Target to unregister.
 */
int GuestDnD::unregisterTarget(const ComObjPtr<GuestDnDTarget> &Target)
{
    GUESTDND_LOCK();

    GuestDnDTgtList::iterator itTgt = std::find(m_lstTgt.begin(), m_lstTgt.end(), Target);
    if (itTgt != m_lstTgt.end())
        m_lstTgt.erase(itTgt);

    GUESTDND_UNLOCK();
    return VINF_SUCCESS;
}

/**
 * Returns the current number of registered targets.
 *
 * @returns Current number of registered targets.
 */
size_t GuestDnD::getTargetCount(void)
{
    GUESTDND_LOCK_RET(0);

    size_t cTargets = m_lstTgt.size();

    GUESTDND_UNLOCK();
    return cTargets;
}

/**
 * Static main dispatcher function to handle callbacks from the DnD host service.
 *
 * @returns VBox status code.
 * @param   pvExtension         Pointer to service extension.
 * @param   u32Function         Callback HGCM message ID.
 * @param   pvParms             Pointer to optional data provided for a particular message. Optional.
 * @param   cbParms             Size (in bytes) of \a pvParms.
 */
/* static */
DECLCALLBACK(int) GuestDnD::notifyDnDDispatcher(void *pvExtension, uint32_t u32Function,
                                                void *pvParms, uint32_t cbParms)
{
    LogFlowFunc(("pvExtension=%p, u32Function=%RU32, pvParms=%p, cbParms=%RU32\n",
                 pvExtension, u32Function, pvParms, cbParms));

    GuestDnD *pGuestDnD = reinterpret_cast<GuestDnD*>(pvExtension);
    AssertPtrReturn(pGuestDnD, VERR_INVALID_POINTER);

    /** @todo In case we need to handle multiple guest DnD responses at a time this
     *        would be the place to lookup and dispatch to those. For the moment we
     *        only have one response -- simple. */
    if (pGuestDnD->m_pState)
        return pGuestDnD->m_pState->onDispatch(u32Function, pvParms, cbParms);

    return VERR_NOT_SUPPORTED;
}

/**
 * Static helper function to determine whether a format is part of a given MIME list.
 *
 * @returns \c true if found, \c false if not.
 * @param   strFormat           Format to search for.
 * @param   lstFormats          MIME list to search in.
 */
/* static */
bool GuestDnD::isFormatInFormatList(const com::Utf8Str &strFormat, const GuestDnDMIMEList &lstFormats)
{
    return std::find(lstFormats.begin(), lstFormats.end(), strFormat) != lstFormats.end();
}

/**
 * Static helper function to create a GuestDnDMIMEList out of a format list string.
 *
 * @returns MIME list object.
 * @param   strFormats          List of formats to convert.
 * @param   strSep              Separator to use. If not specified, DND_FORMATS_SEPARATOR_STR will be used.
 */
/* static */
GuestDnDMIMEList GuestDnD::toFormatList(const com::Utf8Str &strFormats, const com::Utf8Str &strSep /* = DND_FORMATS_SEPARATOR_STR */)
{
    GuestDnDMIMEList lstFormats;
    RTCList<RTCString> lstFormatsTmp = strFormats.split(strSep);

    for (size_t i = 0; i < lstFormatsTmp.size(); i++)
        lstFormats.push_back(com::Utf8Str(lstFormatsTmp.at(i)));

    return lstFormats;
}

/**
 * Static helper function to create a format list string from a given GuestDnDMIMEList object.
 *
 * @returns Format list string.
 * @param   lstFormats          GuestDnDMIMEList to convert.
 * @param   strSep              Separator to use between formats.
 *                              Uses DND_FORMATS_SEPARATOR_STR as default.
 */
/* static */
com::Utf8Str GuestDnD::toFormatString(const GuestDnDMIMEList &lstFormats, const com::Utf8Str &strSep /* = DND_FORMATS_SEPARATOR_STR */)
{
    com::Utf8Str strFormat;
    for (size_t i = 0; i < lstFormats.size(); i++)
    {
        const com::Utf8Str &f = lstFormats.at(i);
        strFormat += f + strSep;
    }

    return strFormat;
}

/**
 * Static helper function to create a filtered GuestDnDMIMEList object from supported and wanted formats.
 *
 * @returns Filtered MIME list object.
 * @param   lstFormatsSupported     MIME list of supported formats.
 * @param   lstFormatsWanted        MIME list of wanted formats in returned object.
 */
/* static */
GuestDnDMIMEList GuestDnD::toFilteredFormatList(const GuestDnDMIMEList &lstFormatsSupported, const GuestDnDMIMEList &lstFormatsWanted)
{
    GuestDnDMIMEList lstFormats;

    for (size_t i = 0; i < lstFormatsWanted.size(); i++)
    {
        /* Only keep supported format types. */
        if (std::find(lstFormatsSupported.begin(),
                      lstFormatsSupported.end(), lstFormatsWanted.at(i)) != lstFormatsSupported.end())
        {
            lstFormats.push_back(lstFormatsWanted[i]);
        }
    }

    return lstFormats;
}

/**
 * Static helper function to create a filtered GuestDnDMIMEList object from supported and wanted formats.
 *
 * @returns Filtered MIME list object.
 * @param   lstFormatsSupported     MIME list of supported formats.
 * @param   strFormatsWanted        Format list string of wanted formats in returned object.
 */
/* static */
GuestDnDMIMEList GuestDnD::toFilteredFormatList(const GuestDnDMIMEList &lstFormatsSupported, const com::Utf8Str &strFormatsWanted)
{
    GuestDnDMIMEList lstFmt;

    RTCList<RTCString> lstFormats = strFormatsWanted.split(DND_FORMATS_SEPARATOR_STR);
    size_t i = 0;
    while (i < lstFormats.size())
    {
        /* Only keep allowed format types. */
        if (std::find(lstFormatsSupported.begin(),
                      lstFormatsSupported.end(), lstFormats.at(i)) != lstFormatsSupported.end())
        {
            lstFmt.push_back(lstFormats[i]);
        }
        i++;
    }

    return lstFmt;
}

/**
 * Static helper function to convert a Main DnD action an internal DnD action.
 *
 * @returns Internal DnD action, or VBOX_DND_ACTION_IGNORE if not found / supported.
 * @param   enmAction               Main DnD action to convert.
 */
/* static */
VBOXDNDACTION GuestDnD::toHGCMAction(DnDAction_T enmAction)
{
    VBOXDNDACTION dndAction = VBOX_DND_ACTION_IGNORE;
    switch (enmAction)
    {
        case DnDAction_Copy:
            dndAction = VBOX_DND_ACTION_COPY;
            break;
        case DnDAction_Move:
            dndAction = VBOX_DND_ACTION_MOVE;
            break;
        case DnDAction_Link:
            /* For now it doesn't seems useful to allow a link
               action between host & guest. Later? */
        case DnDAction_Ignore:
            /* Ignored. */
            break;
        default:
            AssertMsgFailed(("Action %RU32 not recognized!\n", enmAction));
            break;
    }

    return dndAction;
}

/**
 * Static helper function to convert a Main DnD default action and allowed Main actions to their
 * corresponding internal representations.
 *
 * @param   enmDnDActionDefault     Default Main action to convert.
 * @param   pDnDActionDefault       Where to store the converted default action.
 * @param   vecDnDActionsAllowed    Allowed Main actions to convert.
 * @param   pDnDLstActionsAllowed   Where to store the converted allowed actions.
 */
/* static */
void GuestDnD::toHGCMActions(DnDAction_T                    enmDnDActionDefault,
                             VBOXDNDACTION                 *pDnDActionDefault,
                             const std::vector<DnDAction_T> vecDnDActionsAllowed,
                             VBOXDNDACTIONLIST             *pDnDLstActionsAllowed)
{
    VBOXDNDACTIONLIST dndLstActionsAllowed = VBOX_DND_ACTION_IGNORE;
    VBOXDNDACTION     dndActionDefault     = toHGCMAction(enmDnDActionDefault);

    if (!vecDnDActionsAllowed.empty())
    {
        /* First convert the allowed actions to a bit array. */
        for (size_t i = 0; i < vecDnDActionsAllowed.size(); i++)
            dndLstActionsAllowed |= toHGCMAction(vecDnDActionsAllowed[i]);

        /*
         * If no default action is set (ignoring), try one of the
         * set allowed actions, preferring copy, move (in that order).
         */
        if (isDnDIgnoreAction(dndActionDefault))
        {
            if (hasDnDCopyAction(dndLstActionsAllowed))
                dndActionDefault = VBOX_DND_ACTION_COPY;
            else if (hasDnDMoveAction(dndLstActionsAllowed))
                dndActionDefault = VBOX_DND_ACTION_MOVE;
        }
    }

    if (pDnDActionDefault)
        *pDnDActionDefault     = dndActionDefault;
    if (pDnDLstActionsAllowed)
        *pDnDLstActionsAllowed = dndLstActionsAllowed;
}

/**
 * Static helper function to convert an internal DnD action to its Main representation.
 *
 * @returns Converted Main DnD action.
 * @param   dndAction           DnD action to convert.
 */
/* static */
DnDAction_T GuestDnD::toMainAction(VBOXDNDACTION dndAction)
{
    /* For now it doesn't seems useful to allow a
     * link action between host & guest. Maybe later! */
    return isDnDCopyAction(dndAction) ? DnDAction_Copy
         : isDnDMoveAction(dndAction) ? DnDAction_Move
         :                              DnDAction_Ignore;
}

/**
 * Static helper function to convert an internal DnD action list to its Main representation.
 *
 * @returns Converted Main DnD action list.
 * @param   dndActionList       DnD action list to convert.
 */
/* static */
std::vector<DnDAction_T> GuestDnD::toMainActions(VBOXDNDACTIONLIST dndActionList)
{
    std::vector<DnDAction_T> vecActions;

    /* For now it doesn't seems useful to allow a
     * link action between host & guest. Maybe later! */
    RTCList<DnDAction_T> lstActions;
    if (hasDnDCopyAction(dndActionList))
        lstActions.append(DnDAction_Copy);
    if (hasDnDMoveAction(dndActionList))
        lstActions.append(DnDAction_Move);

    for (size_t i = 0; i < lstActions.size(); ++i)
        vecActions.push_back(lstActions.at(i));

    return vecActions;
}

/*********************************************************************************************************************************
 * GuestDnDBase implementation.                                                                                                  *
 ********************************************************************************************************************************/

GuestDnDBase::GuestDnDBase(VirtualBoxBase *pBase)
    : m_pBase(pBase)
    , m_fIsPending(false)
{
    /* Initialize public attributes. */
    m_lstFmtSupported = GuestDnDInst()->defaultFormats();
}

GuestDnDBase::~GuestDnDBase(void)
{
}

/**
 * Checks whether a given DnD format is supported or not.
 *
 * @returns \c true if supported, \c false if not.
 * @param   aFormat             DnD format to check.
 */
bool GuestDnDBase::i_isFormatSupported(const com::Utf8Str &aFormat) const
{
    return std::find(m_lstFmtSupported.begin(), m_lstFmtSupported.end(), aFormat) != m_lstFmtSupported.end();
}

/**
 * Returns the currently supported DnD formats.
 *
 * @returns List of the supported DnD formats.
 */
const GuestDnDMIMEList &GuestDnDBase::i_getFormats(void) const
{
    return m_lstFmtSupported;
}

/**
 * Adds DnD formats to the supported formats list.
 *
 * @returns HRESULT
 * @param   aFormats            List of DnD formats to add.
 */
HRESULT GuestDnDBase::i_addFormats(const GuestDnDMIMEList &aFormats)
{
    for (size_t i = 0; i < aFormats.size(); ++i)
    {
        Utf8Str strFormat = aFormats.at(i);
        if (std::find(m_lstFmtSupported.begin(),
                      m_lstFmtSupported.end(), strFormat) == m_lstFmtSupported.end())
        {
            m_lstFmtSupported.push_back(strFormat);
        }
    }

    return S_OK;
}

/**
 * Removes DnD formats from tehh supported formats list.
 *
 * @returns HRESULT
 * @param   aFormats            List of DnD formats to remove.
 */
HRESULT GuestDnDBase::i_removeFormats(const GuestDnDMIMEList &aFormats)
{
    for (size_t i = 0; i < aFormats.size(); ++i)
    {
        Utf8Str strFormat = aFormats.at(i);
        GuestDnDMIMEList::iterator itFormat = std::find(m_lstFmtSupported.begin(),
                                                        m_lstFmtSupported.end(), strFormat);
        if (itFormat != m_lstFmtSupported.end())
            m_lstFmtSupported.erase(itFormat);
    }

    return S_OK;
}

/**
 * Prints an error in the release log and sets the COM error info.
 *
 * @returns HRESULT
 * @param   vrc                 IPRT-style error code to print in addition.
 *                              Will not be printed if set to a non-error (e.g. VINF _ / VWRN_) code.
 * @param   pcszMsgFmt          Format string.
 * @param   va                  Format arguments.
 * @note
 */
HRESULT GuestDnDBase::i_setErrorV(int vrc, const char *pcszMsgFmt, va_list va)
{
    char *psz = NULL;
    if (RTStrAPrintfV(&psz, pcszMsgFmt, va) < 0)
        return E_OUTOFMEMORY;
    AssertPtrReturn(psz, E_OUTOFMEMORY);

    HRESULT hrc;
    if (RT_FAILURE(vrc))
    {
        LogRel(("DnD: Error: %s (%Rrc)\n", psz, vrc));
        hrc = m_pBase->setErrorBoth(VBOX_E_DND_ERROR, vrc, "DnD: Error: %s (%Rrc)", psz, vrc);
    }
    else
    {
        LogRel(("DnD: Error: %s\n", psz));
        hrc = m_pBase->setErrorBoth(VBOX_E_DND_ERROR, vrc, "DnD: Error: %s", psz);
    }

    RTStrFree(psz);
    return hrc;
}

/**
 * Prints an error in the release log and sets the COM error info.
 *
 * @returns HRESULT
 * @param   vrc                 IPRT-style error code to print in addition.
 *                              Will not be printed if set to a non-error (e.g. VINF _ / VWRN_) code.
 * @param   pcszMsgFmt          Format string.
 * @param   ...                 Format arguments.
 * @note
 */
HRESULT GuestDnDBase::i_setError(int vrc, const char *pcszMsgFmt, ...)
{
    va_list va;
    va_start(va, pcszMsgFmt);
    HRESULT const hrc = i_setErrorV(vrc, pcszMsgFmt, va);
    va_end(va);

    return hrc;
}

/**
 * Prints an error in the release log, sets the COM error info and calls the object's reset function.
 *
 * @returns HRESULT
 * @param   pcszMsgFmt          Format string.
 * @param   va                  Format arguments.
 * @note
 */
HRESULT GuestDnDBase::i_setErrorAndReset(const char *pcszMsgFmt, ...)
{
    va_list va;
    va_start(va, pcszMsgFmt);
    HRESULT const hrc = i_setErrorV(VINF_SUCCESS, pcszMsgFmt, va);
    va_end(va);

    i_reset();

    return hrc;
}

/**
 * Prints an error in the release log, sets the COM error info and calls the object's reset function.
 *
 * @returns HRESULT
 * @param   vrc                 IPRT-style error code to print in addition.
 *                              Will not be printed if set to a non-error (e.g. VINF _ / VWRN_) code.
 * @param   pcszMsgFmt          Format string.
 * @param   ...                 Format arguments.
 * @note
 */
HRESULT GuestDnDBase::i_setErrorAndReset(int vrc, const char *pcszMsgFmt, ...)
{
    va_list va;
    va_start(va, pcszMsgFmt);
    HRESULT const hrc = i_setErrorV(vrc, pcszMsgFmt, va);
    va_end(va);

    i_reset();

    return hrc;
}

/**
 * Adds a new guest DnD message to the internal message queue.
 *
 * @returns VBox status code.
 * @param   pMsg                Pointer to message to add.
 */
int GuestDnDBase::msgQueueAdd(GuestDnDMsg *pMsg)
{
    m_DataBase.lstMsgOut.push_back(pMsg);
    return VINF_SUCCESS;
}

/**
 * Returns the next guest DnD message in the internal message queue (FIFO).
 *
 * @returns Pointer to guest DnD message, or NULL if none found.
 */
GuestDnDMsg *GuestDnDBase::msgQueueGetNext(void)
{
    if (m_DataBase.lstMsgOut.empty())
        return NULL;
    return m_DataBase.lstMsgOut.front();
}

/**
 * Removes the next guest DnD message from the internal message queue.
 */
void GuestDnDBase::msgQueueRemoveNext(void)
{
    if (!m_DataBase.lstMsgOut.empty())
    {
        GuestDnDMsg *pMsg = m_DataBase.lstMsgOut.front();
        if (pMsg)
            delete pMsg;
        m_DataBase.lstMsgOut.pop_front();
    }
}

/**
 * Clears the internal message queue.
 */
void GuestDnDBase::msgQueueClear(void)
{
    LogFlowFunc(("cMsg=%zu\n", m_DataBase.lstMsgOut.size()));

    GuestDnDMsgList::iterator itMsg = m_DataBase.lstMsgOut.begin();
    while (itMsg != m_DataBase.lstMsgOut.end())
    {
        GuestDnDMsg *pMsg = *itMsg;
        if (pMsg)
            delete pMsg;

        itMsg++;
    }

    m_DataBase.lstMsgOut.clear();
}

/**
 * Sends a request to the guest side to cancel the current DnD operation.
 *
 * @returns VBox status code.
 */
int GuestDnDBase::sendCancel(void)
{
    GuestDnDMsg Msg;
    Msg.setType(HOST_DND_FN_CANCEL);
    if (m_pState->m_uProtocolVersion >= 3)
        Msg.appendUInt32(0); /** @todo ContextID not used yet. */

    LogRel2(("DnD: Cancelling operation on guest ...\n"));

    int vrc = GuestDnDInst()->hostCall(Msg.getType(), Msg.getCount(), Msg.getParms());
    if (RT_FAILURE(vrc))
        LogRel(("DnD: Cancelling operation on guest failed with %Rrc\n", vrc));

    return vrc;
}

/**
 * Helper function to update the progress based on given a GuestDnDData object.
 *
 * @returns VBox status code.
 * @param   pData               GuestDnDData object to use for accounting.
 * @param   pState              Guest state to update its progress object for.
 * @param   cbDataAdd           By how much data (in bytes) to update the progress.
 */
int GuestDnDBase::updateProgress(GuestDnDData *pData, GuestDnDState *pState,
                                 size_t cbDataAdd /* = 0 */)
{
    AssertPtrReturn(pData, VERR_INVALID_POINTER);
    AssertPtrReturn(pState, VERR_INVALID_POINTER);
    /* cbDataAdd is optional. */

    LogFlowFunc(("cbExtra=%zu, cbProcessed=%zu, cbRemaining=%zu, cbDataAdd=%zu\n",
                 pData->cbExtra, pData->cbProcessed, pData->getRemaining(), cbDataAdd));

    if (   !pState
        || !cbDataAdd) /* Only update if something really changes. */
        return VINF_SUCCESS;

    if (cbDataAdd)
        pData->addProcessed(cbDataAdd);

    const uint8_t uPercent = pData->getPercentComplete();

    LogRel2(("DnD: Transfer %RU8%% complete\n", uPercent));

    int vrc = pState->setProgress(uPercent, pData->isComplete() ? DND_PROGRESS_COMPLETE : DND_PROGRESS_RUNNING);
    LogFlowFuncLeaveRC(vrc);
    return vrc;
}

/**
 * Waits for a specific guest callback event to get signalled.
 *
 * @returns VBox status code. Will return VERR_CANCELLED if the user has cancelled the progress object.
 * @param   pEvent                  Callback event to wait for.
 * @param   pState                  Guest state to update.
 * @param   msTimeout               Timeout (in ms) to wait.
 */
int GuestDnDBase::waitForEvent(GuestDnDCallbackEvent *pEvent, GuestDnDState *pState, RTMSINTERVAL msTimeout)
{
    AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
    AssertPtrReturn(pState, VERR_INVALID_POINTER);

    int vrc;

    LogFlowFunc(("msTimeout=%RU32\n", msTimeout));

    uint64_t tsStart = RTTimeMilliTS();
    do
    {
        /*
         * Wait until our desired callback triggered the
         * wait event. As we don't want to block if the guest does not
         * respond, do busy waiting here.
         */
        vrc = pEvent->Wait(500 /* ms */);
        if (RT_SUCCESS(vrc))
        {
            vrc = pEvent->Result();
            LogFlowFunc(("Callback done, result is %Rrc\n", vrc));
            break;
        }
        if (vrc == VERR_TIMEOUT) /* Continue waiting. */
            vrc = VINF_SUCCESS;

        if (   msTimeout != RT_INDEFINITE_WAIT
            && RTTimeMilliTS() - tsStart > msTimeout)
        {
            vrc = VERR_TIMEOUT;
            LogRel2(("DnD: Error: Guest did not respond within time\n"));
        }
        else if (pState->isProgressCanceled())
        {
            LogRel2(("DnD: Operation was canceled by user\n"));
            vrc = VERR_CANCELLED;
        }

    } while (RT_SUCCESS(vrc));

    LogFlowFuncLeaveRC(vrc);
    return vrc;
}

#endif /* VBOX_WITH_DRAG_AND_DROP */