VirtualBox

source: vbox/trunk/src/VBox/HostServices/SharedClipboard/VBoxSharedClipboardSvc-win.cpp@ 79449

Last change on this file since 79449 was 79366, checked in by vboxsync, 6 years ago

Shared Clipboard/URI: Update.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 26.4 KB
Line 
1/* $Id: VBoxSharedClipboardSvc-win.cpp 79366 2019-06-26 15:59:30Z vboxsync $ */
2/** @file
3 * Shared Clipboard Service - Win32 host.
4 */
5
6/*
7 * Copyright (C) 2006-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
23#include <iprt/win/windows.h>
24
25#include <VBox/HostServices/VBoxClipboardSvc.h>
26#include <VBox/GuestHost/clipboard-helper.h>
27#include <VBox/GuestHost/SharedClipboard-win.h>
28#ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
29# include <VBox/GuestHost/SharedClipboard-uri.h>
30#endif
31
32#include <iprt/alloc.h>
33#include <iprt/string.h>
34#include <iprt/asm.h>
35#include <iprt/assert.h>
36#include <iprt/ldr.h>
37#include <iprt/semaphore.h>
38#include <iprt/thread.h>
39#ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
40# include <iprt/utf16.h>
41#endif
42
43#include <process.h>
44#include <shlobj.h> /* Needed for shell objects. */
45
46#include "VBoxSharedClipboardSvc-internal.h"
47#ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
48# include "VBoxSharedClipboardSvc-uri.h"
49#endif
50
51/** Static window class name. */
52static char s_szClipWndClassName[] = VBOX_CLIPBOARD_WNDCLASS_NAME;
53
54
55/*********************************************************************************************************************************
56* Internal Functions *
57*********************************************************************************************************************************/
58static int vboxClipboardSvcWinSyncInternal(PVBOXCLIPBOARDCONTEXT pCtx);
59
60struct _VBOXCLIPBOARDCONTEXT
61{
62 /** Handle for window message handling thread. */
63 RTTHREAD hThread;
64 /** Event which gets triggered if the host clipboard needs to render its data. */
65 RTSEMEVENT hRenderEvent;
66 /** Structure for keeping and communicating with client data (from the guest). */
67 PVBOXCLIPBOARDCLIENTDATA pClientData;
68 /** Windows-specific context data. */
69 VBOXCLIPBOARDWINCTX Win;
70};
71
72/* Only one client is supported. There seems to be no need for more clients. */
73static VBOXCLIPBOARDCONTEXT g_ctx;
74
75
76/** @todo Someone please explain the protocol wrt overflows... */
77static void vboxClipboardGetData(uint32_t u32Format, const void *pvSrc, uint32_t cbSrc,
78 void *pvDst, uint32_t cbDst, uint32_t *pcbActualDst)
79{
80 LogFlowFunc(("cbSrc = %d, cbDst = %d\n", cbSrc, cbDst));
81
82 if ( u32Format == VBOX_SHARED_CLIPBOARD_FMT_HTML
83 && VBoxClipboardWinIsCFHTML((const char *)pvSrc))
84 {
85 /** @todo r=bird: Why the double conversion? */
86 char *pszBuf = NULL;
87 uint32_t cbBuf = 0;
88 int rc = VBoxClipboardWinConvertCFHTMLToMIME((const char *)pvSrc, cbSrc, &pszBuf, &cbBuf);
89 if (RT_SUCCESS(rc))
90 {
91 *pcbActualDst = cbBuf;
92 if (cbBuf > cbDst)
93 {
94 /* Do not copy data. The dst buffer is not enough. */
95 RTMemFree(pszBuf);
96 return;
97 }
98 memcpy(pvDst, pszBuf, cbBuf);
99 RTMemFree(pszBuf);
100 }
101 else
102 *pcbActualDst = 0;
103 }
104 else
105 {
106 *pcbActualDst = cbSrc;
107
108 if (cbSrc > cbDst)
109 {
110 /* Do not copy data. The dst buffer is not enough. */
111 return;
112 }
113
114 memcpy(pvDst, pvSrc, cbSrc);
115 }
116
117#ifdef LOG_ENABLED
118 VBoxClipboardDbgDumpData(pvDst, cbSrc, u32Format);
119#endif
120
121 return;
122}
123
124/**
125 * Requests data of a specific format from the guest, optionally waiting for its arrival via VBoxClipboardSvcImplWriteData().
126 *
127 * @returns VBox status code.
128 * @param pCtx Clipboard context to use.
129 * @param fFormat Format to receive data in.
130 * @param uTimeoutMs Timeout (in ms) to wait until the render event has been triggered.
131 * Specify 0 if no waiting is required.
132 */
133static int vboxClipboardSvcWinRequestData(PVBOXCLIPBOARDCONTEXT pCtx, VBOXCLIPBOARDFORMAT fFormat,
134 RTMSINTERVAL uTimeoutMs)
135{
136 AssertPtr(pCtx->pClientData);
137 Assert(pCtx->hRenderEvent);
138 Assert(pCtx->pClientData->State.data.pv == NULL && pCtx->pClientData->State.data.cb == 0 && pCtx->pClientData->State.data.u32Format == 0);
139
140 LogFlowFunc(("fFormat=%02X, uTimeoutMs=%RU32\n", fFormat, uTimeoutMs));
141
142 int rc = vboxSvcClipboardReportMsg(pCtx->pClientData, VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA, fFormat);
143 if ( RT_SUCCESS(rc)
144 && uTimeoutMs)
145 {
146 rc = RTSemEventWait(pCtx->hRenderEvent, uTimeoutMs /* Timeout in ms */);
147 }
148
149 LogFlowFuncLeaveRC(rc);
150 return rc;
151}
152
153static LRESULT CALLBACK vboxClipboardSvcWinWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
154{
155 LRESULT lresultRc = 0;
156
157 const PVBOXCLIPBOARDCONTEXT pCtx = &g_ctx;
158 const PVBOXCLIPBOARDWINCTX pWinCtx = &pCtx->Win;
159
160 switch (msg)
161 {
162 case WM_CLIPBOARDUPDATE:
163 {
164 const HWND hWndClipboardOwner = GetClipboardOwner();
165 if (pWinCtx->hWndClipboardOwnerUs != hWndClipboardOwner)
166 {
167 LogFunc(("WM_CLIPBOARDUPDATE: hWndOldClipboardOwner=%p, hWndNewClipboardOwner=%p\n",
168 pWinCtx->hWndClipboardOwnerUs, hWndClipboardOwner));
169
170 /* Clipboard was updated by another application, retrieve formats and report back. */
171 int rc = vboxClipboardSvcWinSyncInternal(pCtx);
172 AssertRC(rc);
173
174 vboxSvcClipboardSetSource(pCtx->pClientData, SHAREDCLIPBOARDSOURCE_LOCAL);
175 }
176 } break;
177
178 case WM_CHANGECBCHAIN:
179 {
180 LogFunc(("WM_CHANGECBCHAIN\n"));
181 lresultRc = VBoxClipboardWinHandleWMChangeCBChain(pWinCtx, hwnd, msg, wParam, lParam);
182 } break;
183
184 case WM_DRAWCLIPBOARD:
185 {
186 LogFunc(("WM_DRAWCLIPBOARD\n"));
187
188 if (GetClipboardOwner() != hwnd)
189 {
190 /* Clipboard was updated by another application, retrieve formats and report back. */
191 int rc = vboxClipboardSvcWinSyncInternal(pCtx);
192 if (RT_SUCCESS(rc))
193 vboxSvcClipboardSetSource(pCtx->pClientData, SHAREDCLIPBOARDSOURCE_LOCAL);
194 }
195
196 lresultRc = VBoxClipboardWinChainPassToNext(pWinCtx, msg, wParam, lParam);
197
198 } break;
199
200 case WM_TIMER:
201 {
202 int rc = VBoxClipboardWinHandleWMTimer(pWinCtx);
203 AssertRC(rc);
204 } break;
205
206 case WM_RENDERFORMAT:
207 {
208 LogFunc(("WM_RENDERFORMAT\n"));
209
210 /* Insert the requested clipboard format data into the clipboard. */
211 const UINT cfFormat = (UINT)wParam;
212
213 const VBOXCLIPBOARDFORMAT fFormat = VBoxClipboardWinClipboardFormatToVBox(cfFormat);
214
215 LogFunc(("WM_RENDERFORMAT: cfFormat=%u -> fFormat=0x%x\n", cfFormat, fFormat));
216
217 if ( fFormat == VBOX_SHARED_CLIPBOARD_FMT_NONE
218 || pCtx->pClientData == NULL)
219 {
220 /* Unsupported clipboard format is requested. */
221 LogFunc(("WM_RENDERFORMAT unsupported format requested or client is not active\n"));
222 VBoxClipboardWinClear();
223 }
224 else
225 {
226 int rc = vboxClipboardSvcWinRequestData(pCtx, fFormat, 30 * 1000 /* 30s timeout */);
227
228 LogFunc(("vboxClipboardReadDataFromClient rc = %Rrc, pv %p, cb %d, u32Format %d\n",
229 rc, pCtx->pClientData->State.data.pv, pCtx->pClientData->State.data.cb,
230 pCtx->pClientData->State.data.u32Format));
231
232 if ( RT_SUCCESS (rc)
233 && pCtx->pClientData->State.data.pv != NULL
234 && pCtx->pClientData->State.data.cb > 0
235 && pCtx->pClientData->State.data.u32Format == fFormat)
236 {
237 HANDLE hMem = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, pCtx->pClientData->State.data.cb);
238
239 LogFunc(("hMem %p\n", hMem));
240
241 if (hMem)
242 {
243 void *pMem = GlobalLock(hMem);
244
245 LogFunc(("pMem %p, GlobalSize %d\n", pMem, GlobalSize(hMem)));
246
247 if (pMem)
248 {
249 LogFunc(("WM_RENDERFORMAT setting data\n"));
250
251 if (pCtx->pClientData->State.data.pv)
252 {
253 memcpy(pMem, pCtx->pClientData->State.data.pv, pCtx->pClientData->State.data.cb);
254
255 RTMemFree(pCtx->pClientData->State.data.pv);
256 pCtx->pClientData->State.data.pv = NULL;
257 }
258
259 pCtx->pClientData->State.data.cb = 0;
260 pCtx->pClientData->State.data.u32Format = 0;
261
262 /* The memory must be unlocked before inserting to the Clipboard. */
263 GlobalUnlock(hMem);
264
265 /* 'hMem' contains the host clipboard data.
266 * size is 'cb' and format is 'format'.
267 */
268 HANDLE hClip = SetClipboardData(cfFormat, hMem);
269
270 LogFunc(("vboxClipboardHostEvent hClip %p\n", hClip));
271
272 if (hClip)
273 {
274 /* The hMem ownership has gone to the system. Nothing to do. */
275 break;
276 }
277 }
278
279 GlobalFree(hMem);
280 }
281 }
282
283 RTMemFree(pCtx->pClientData->State.data.pv);
284 pCtx->pClientData->State.data.pv = NULL;
285 pCtx->pClientData->State.data.cb = 0;
286 pCtx->pClientData->State.data.u32Format = 0;
287
288 /* Something went wrong. */
289 VBoxClipboardWinClear();
290 }
291 } break;
292
293 case WM_RENDERALLFORMATS:
294 {
295 LogFunc(("WM_RENDERALLFORMATS\n"));
296
297 int rc = VBoxClipboardWinHandleWMRenderAllFormats(pWinCtx, hwnd);
298 AssertRC(rc);
299 } break;
300
301 case VBOX_CLIPBOARD_WM_SET_FORMATS:
302 {
303 LogFunc(("VBOX_CLIPBOARD_WM_SET_FORMATS\n"));
304
305 if ( pCtx->pClientData == NULL
306 || pCtx->pClientData->State.fHostMsgFormats)
307 {
308 /* Host has pending formats message. Ignore the guest announcement,
309 * because host clipboard has more priority.
310 */
311 LogFunc(("VBOX_CLIPBOARD_WM_SET_FORMATS ignored\n"));
312 break;
313 }
314
315 /* Announce available formats. Do not insert data -- will be inserted in WM_RENDERFORMAT. */
316 VBOXCLIPBOARDFORMATS fFormats = (uint32_t)lParam;
317 if (fFormats != VBOX_SHARED_CLIPBOARD_FMT_NONE) /* Could arrive with some older GA versions. */
318 {
319 int rc = VBoxClipboardWinOpen(hwnd);
320 if (RT_SUCCESS(rc))
321 {
322 VBoxClipboardWinClear();
323
324#ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
325 if (fFormats & VBOX_SHARED_CLIPBOARD_FMT_URI_LIST)
326 {
327 LogFunc(("VBOX_SHARED_CLIPBOARD_FMT_URI_LIST\n"));
328
329 PSHAREDCLIPBOARDURITRANSFER pTransfer = SharedClipboardURICtxGetTransfer(&pCtx->pClientData->URI,
330 0 /* uIdx */);
331 if (pTransfer)
332 {
333 rc = VBoxClipboardWinURITransferCreate(pWinCtx, pTransfer);
334
335 /* Note: The actual requesting + retrieving of data will be done in the IDataObject implementation
336 (ClipboardDataObjectImpl::GetData()). */
337 }
338 else
339 AssertFailedStmt(rc = VERR_NOT_FOUND);
340
341 /* Note: VBoxClipboardWinURITransferCreate() takes care of closing the clipboard. */
342 }
343 else
344 {
345#endif
346 rc = VBoxClipboardWinAnnounceFormats(pWinCtx, fFormats);
347
348 VBoxClipboardWinClose();
349#ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
350 }
351#endif
352 }
353
354 if (RT_FAILURE(rc))
355 LogFunc(("Failed with rc=%Rrc\n", rc));
356 }
357 LogFunc(("VBOX_CLIPBOARD_WM_SET_FORMATS: fFormats=0x%x, lastErr=%ld\n", fFormats, GetLastError()));
358 } break;
359
360 case WM_DESTROY:
361 {
362 LogFunc(("WM_DESTROY\n"));
363
364 int rc = VBoxClipboardWinHandleWMDestroy(pWinCtx);
365 AssertRC(rc);
366
367 PostQuitMessage(0);
368 } break;
369
370 default:
371 {
372 LogFunc(("WM_ %p\n", msg));
373 lresultRc = DefWindowProc(hwnd, msg, wParam, lParam);
374 }
375 }
376
377 LogFunc(("WM_ rc %d\n", lresultRc));
378 return lresultRc;
379}
380
381DECLCALLBACK(int) vboxClipboardSvcWinThread(RTTHREAD hThreadSelf, void *pvUser)
382{
383 RT_NOREF(hThreadSelf, pvUser);
384
385 /* Create a window and make it a clipboard viewer. */
386 int rc = VINF_SUCCESS;
387
388 LogFlowFuncEnter();
389
390 const PVBOXCLIPBOARDCONTEXT pCtx = &g_ctx;
391 const PVBOXCLIPBOARDWINCTX pWinCtx = &pCtx->Win;
392
393 HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
394
395 /* Register the Window Class. */
396 WNDCLASS wc;
397 RT_ZERO(wc);
398
399 wc.style = CS_NOCLOSE;
400 wc.lpfnWndProc = vboxClipboardSvcWinWndProc;
401 wc.hInstance = hInstance;
402 wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
403 wc.lpszClassName = s_szClipWndClassName;
404
405 ATOM atomWindowClass = RegisterClass(&wc);
406
407 if (atomWindowClass == 0)
408 {
409 LogFunc(("Failed to register window class\n"));
410 rc = VERR_NOT_SUPPORTED;
411 }
412 else
413 {
414 /* Create the window. */
415 pWinCtx->hWnd = CreateWindowEx(WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
416 s_szClipWndClassName, s_szClipWndClassName,
417 WS_POPUPWINDOW,
418 -200, -200, 100, 100, NULL, NULL, hInstance, NULL);
419 if (pWinCtx->hWnd == NULL)
420 {
421 LogFunc(("Failed to create window\n"));
422 rc = VERR_NOT_SUPPORTED;
423 }
424 else
425 {
426 SetWindowPos(pWinCtx->hWnd, HWND_TOPMOST, -200, -200, 0, 0,
427 SWP_NOACTIVATE | SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_NOSIZE);
428
429 rc = VBoxClipboardWinChainAdd(&pCtx->Win);
430 if (RT_SUCCESS(rc))
431 {
432 if (!VBoxClipboardWinIsNewAPI(&pWinCtx->newAPI))
433 pWinCtx->oldAPI.timerRefresh = SetTimer(pWinCtx->hWnd, 0, 10 * 1000, NULL);
434 }
435
436#ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
437 if (RT_SUCCESS(rc))
438 {
439 HRESULT hr = OleInitialize(NULL);
440 if (FAILED(hr))
441 {
442 LogRel(("Clipboard: Initializing OLE failed (%Rhrc) -- file transfers unavailable\n"));
443 /* Not critical, the rest of the clipboard might work. */
444 }
445 else
446 LogRel(("Clipboard: Initialized OLE\n"));
447 }
448#endif
449 MSG msg;
450 BOOL msgret = 0;
451 while ((msgret = GetMessage(&msg, NULL, 0, 0)) > 0)
452 {
453 TranslateMessage(&msg);
454 DispatchMessage(&msg);
455 }
456 /*
457 * Window procedure can return error,
458 * but this is exceptional situation
459 * that should be identified in testing
460 */
461 Assert(msgret >= 0);
462 LogFunc(("Message loop finished. GetMessage returned %d, message id: %d \n", msgret, msg.message));
463
464#ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
465 OleSetClipboard(NULL); /* Make sure to flush the clipboard on destruction. */
466 OleUninitialize();
467#endif
468 }
469 }
470
471 pWinCtx->hWnd = NULL;
472
473 if (atomWindowClass != 0)
474 {
475 UnregisterClass(s_szClipWndClassName, hInstance);
476 atomWindowClass = 0;
477 }
478
479 LogFlowFuncLeaveRC(rc);
480 return rc;
481}
482
483/**
484 * Synchronizes the host and the guest clipboard formats by sending all supported host clipboard
485 * formats to the guest.
486 *
487 * @returns VBox status code, VINF_NO_CHANGE if no synchronization was required.
488 * @param pCtx Clipboard context to synchronize.
489 */
490static int vboxClipboardSvcWinSyncInternal(PVBOXCLIPBOARDCONTEXT pCtx)
491{
492 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
493
494 LogFlowFuncEnter();
495
496 int rc;
497
498 if (pCtx->pClientData)
499 {
500 uint32_t uFormats;
501 rc = VBoxClipboardWinGetFormats(&pCtx->Win, &uFormats);
502 if (RT_SUCCESS(rc))
503 vboxSvcClipboardReportMsg(pCtx->pClientData, VBOX_SHARED_CLIPBOARD_HOST_MSG_REPORT_FORMATS, uFormats);
504 }
505 else /* If we don't have any client data (yet), bail out. */
506 rc = VINF_NO_CHANGE;
507
508 LogFlowFuncLeaveRC(rc);
509 return rc;
510}
511
512/*
513 * Public platform dependent functions.
514 */
515
516int VBoxClipboardSvcImplInit(void)
517{
518 RT_ZERO(g_ctx); /* Be careful not messing up non-POD types! */
519
520 /* Check that new Clipboard API is available. */
521 VBoxClipboardWinCheckAndInitNewAPI(&g_ctx.Win.newAPI);
522
523 int rc = RTSemEventCreate(&g_ctx.hRenderEvent);
524 if (RT_SUCCESS(rc))
525 {
526 rc = RTThreadCreate(&g_ctx.hThread, vboxClipboardSvcWinThread, NULL, _64K /* Stack size */,
527 RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "SHCLIP");
528 }
529
530 if (RT_FAILURE(rc))
531 RTSemEventDestroy(g_ctx.hRenderEvent);
532
533 LogFlowFuncLeaveRC(rc);
534 return rc;
535}
536
537void VBoxClipboardSvcImplDestroy(void)
538{
539 LogFlowFuncEnter();
540
541 if (g_ctx.Win.hWnd)
542 {
543 PostMessage(g_ctx.Win.hWnd, WM_CLOSE, 0, 0);
544 }
545
546 int rc = RTSemEventDestroy(g_ctx.hRenderEvent);
547 AssertRC(rc);
548
549 /* Wait for the window thread to terminate. */
550 rc = RTThreadWait(g_ctx.hThread, 30 * 1000 /* Timeout in ms */, NULL);
551 if (RT_FAILURE(rc))
552 LogRel(("Shared Clipboard: Waiting for window thread termination failed with rc=%Rrc\n", rc));
553
554 g_ctx.hThread = NIL_RTTHREAD;
555}
556
557int VBoxClipboardSvcImplConnect(PVBOXCLIPBOARDCLIENTDATA pClientData, bool fHeadless)
558{
559 RT_NOREF(fHeadless);
560
561 LogFlowFuncEnter();
562
563 if (g_ctx.pClientData != NULL)
564 {
565 /* One client only. */
566 return VERR_NOT_SUPPORTED;
567 }
568
569 pClientData->State.pCtx = &g_ctx;
570
571 pClientData->State.pCtx->pClientData = pClientData;
572
573 /* Sync the host clipboard content with the client. */
574 VBoxClipboardSvcImplSync(pClientData);
575
576 return VINF_SUCCESS;
577}
578
579int VBoxClipboardSvcImplSync(PVBOXCLIPBOARDCLIENTDATA pClientData)
580{
581 /* Sync the host clipboard content with the client. */
582 return vboxClipboardSvcWinSyncInternal(pClientData->State.pCtx);
583}
584
585int VBoxClipboardSvcImplDisconnect(PVBOXCLIPBOARDCLIENTDATA pClientData)
586{
587 RT_NOREF(pClientData);
588
589 LogFlowFuncEnter();
590
591 g_ctx.pClientData = NULL;
592
593 return VINF_SUCCESS;
594}
595
596int VBoxClipboardSvcImplFormatAnnounce(PVBOXCLIPBOARDCLIENTDATA pClientData, uint32_t u32Formats)
597{
598 AssertPtrReturn(pClientData, VERR_INVALID_POINTER);
599 AssertPtrReturn(pClientData->State.pCtx, VERR_INVALID_POINTER);
600
601 /*
602 * The guest announces formats. Forward to the window thread.
603 */
604 PostMessage(pClientData->State.pCtx->Win.hWnd, VBOX_CLIPBOARD_WM_SET_FORMATS, 0, u32Formats);
605
606 return VINF_SUCCESS;
607}
608
609int VBoxClipboardSvcImplReadData(PVBOXCLIPBOARDCLIENTDATA pClientData, uint32_t u32Format, void *pv, uint32_t cb,
610 uint32_t *pcbActual)
611{
612 AssertPtrReturn(pClientData, VERR_INVALID_POINTER);
613 AssertPtrReturn(pClientData->State.pCtx, VERR_INVALID_POINTER);
614
615 LogFlowFunc(("u32Format=%02X\n", u32Format));
616
617 HANDLE hClip = NULL;
618
619 const PVBOXCLIPBOARDWINCTX pWinCtx = &pClientData->State.pCtx->Win;
620
621 /*
622 * The guest wants to read data in the given format.
623 */
624 int rc = VBoxClipboardWinOpen(pWinCtx->hWnd);
625 if (RT_SUCCESS(rc))
626 {
627 LogFunc(("Clipboard opened\n"));
628
629 if (u32Format & VBOX_SHARED_CLIPBOARD_FMT_BITMAP)
630 {
631 hClip = GetClipboardData(CF_DIB);
632 if (hClip != NULL)
633 {
634 LPVOID lp = GlobalLock(hClip);
635
636 if (lp != NULL)
637 {
638 LogFunc(("CF_DIB\n"));
639
640 vboxClipboardGetData(VBOX_SHARED_CLIPBOARD_FMT_BITMAP, lp, GlobalSize(hClip),
641 pv, cb, pcbActual);
642
643 GlobalUnlock(hClip);
644 }
645 else
646 {
647 hClip = NULL;
648 }
649 }
650 }
651 else if (u32Format & VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT)
652 {
653 hClip = GetClipboardData(CF_UNICODETEXT);
654 if (hClip != NULL)
655 {
656 LPWSTR uniString = (LPWSTR)GlobalLock(hClip);
657
658 if (uniString != NULL)
659 {
660 LogFunc(("CF_UNICODETEXT\n"));
661
662 vboxClipboardGetData(VBOX_SHARED_CLIPBOARD_FMT_UNICODETEXT, uniString, (lstrlenW(uniString) + 1) * 2,
663 pv, cb, pcbActual);
664
665 GlobalUnlock(hClip);
666 }
667 else
668 {
669 hClip = NULL;
670 }
671 }
672 }
673 else if (u32Format & VBOX_SHARED_CLIPBOARD_FMT_HTML)
674 {
675 UINT format = RegisterClipboardFormat(VBOX_CLIPBOARD_WIN_REGFMT_HTML);
676 if (format != 0)
677 {
678 hClip = GetClipboardData(format);
679 if (hClip != NULL)
680 {
681 LPVOID lp = GlobalLock(hClip);
682 if (lp != NULL)
683 {
684 /** @todo r=andy Add data overflow handling. */
685 vboxClipboardGetData(VBOX_SHARED_CLIPBOARD_FMT_HTML, lp, GlobalSize(hClip),
686 pv, cb, pcbActual);
687#ifdef VBOX_STRICT
688 LogFlowFunc(("Raw HTML clipboard data from host:"));
689 VBoxClipboardDbgDumpHtml((char *)pv, cb);
690#endif
691 GlobalUnlock(hClip);
692 }
693 else
694 {
695 hClip = NULL;
696 }
697 }
698 }
699 }
700#ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
701 else if (u32Format & VBOX_SHARED_CLIPBOARD_FMT_URI_LIST)
702 {
703 AssertFailed(); /** @todo */
704 }
705#endif /* VBOX_WITH_SHARED_CLIPBOARD_URI_LIST */
706 VBoxClipboardWinClose();
707 }
708
709 if (hClip == NULL)
710 {
711 /* Reply with empty data. */
712 vboxClipboardGetData(0, NULL, 0, pv, cb, pcbActual);
713 }
714
715 LogFlowFuncLeaveRC(rc);
716 return rc;
717}
718
719int VBoxClipboardSvcImplWriteData(PVBOXCLIPBOARDCLIENTDATA pClientData, void *pv, uint32_t cb, uint32_t u32Format)
720{
721 LogFlowFuncEnter();
722
723 /*
724 * The guest returns data that was requested in the WM_RENDERFORMAT handler.
725 */
726 Assert(pClientData->State.data.pv == NULL && pClientData->State.data.cb == 0 && pClientData->State.data.u32Format == 0);
727
728#ifdef LOG_ENABLED
729 VBoxClipboardDbgDumpData(pv, cb, u32Format);
730#endif
731
732 if (cb > 0)
733 {
734 char *pszResult = NULL;
735
736 if ( u32Format == VBOX_SHARED_CLIPBOARD_FMT_HTML
737 && !VBoxClipboardWinIsCFHTML((const char*)pv))
738 {
739 /* check that this is not already CF_HTML */
740 uint32_t cbResult;
741 int rc = VBoxClipboardWinConvertMIMEToCFHTML((const char *)pv, cb, &pszResult, &cbResult);
742 if (RT_SUCCESS(rc))
743 {
744 if (pszResult != NULL && cbResult != 0)
745 {
746 pClientData->State.data.pv = pszResult;
747 pClientData->State.data.cb = cbResult;
748 pClientData->State.data.u32Format = u32Format;
749 }
750 }
751 }
752 else
753 {
754 pClientData->State.data.pv = RTMemDup(pv, cb);
755 if (pClientData->State.data.pv)
756 {
757 pClientData->State.data.cb = cb;
758 pClientData->State.data.u32Format = u32Format;
759 }
760 }
761 }
762
763 AssertPtr(pClientData->State.pCtx);
764 int rc = RTSemEventSignal(pClientData->State.pCtx->hRenderEvent);
765 AssertRC(rc);
766
767 LogFlowFuncLeaveRC(rc);
768 return rc;
769}
770
771#ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
772void VBoxClipboardSvcImplURIOnDataHeaderComplete(PSHAREDCLIPBOARDURITRANSFERCALLBACKDATA pData)
773{
774 LogFlowFuncEnter();
775
776 AssertPtrReturnVoid(pData);
777
778 PSHAREDCLIPBOARDURITRANSFER pTransfer = pData->pTransfer;
779 AssertPtrReturnVoid(pTransfer);
780
781 SharedClipboardWinURITransferCtx *pTransferCtx = (SharedClipboardWinURITransferCtx *)pTransfer->pvUser;
782 AssertPtrReturnVoid(pTransferCtx);
783
784 /* Notify the Windows implementation's data object that the meta data has been read successfully. */
785 if (pTransferCtx->pDataObj)
786 pTransferCtx->pDataObj->OnMetaDataComplete(pTransfer);
787}
788
789int VBoxClipboardSvcImplURITransferCreate(PVBOXCLIPBOARDCLIENTDATA pClientData, PSHAREDCLIPBOARDURITRANSFER pTransfer)
790{
791 RT_NOREF(pClientData, pTransfer);
792
793 LogFlowFuncEnter();
794
795 return VINF_SUCCESS;
796}
797
798int VBoxClipboardSvcImplURITransferDestroy(PVBOXCLIPBOARDCLIENTDATA pClientData, PSHAREDCLIPBOARDURITRANSFER pTransfer)
799{
800 LogFlowFuncEnter();
801
802 VBoxClipboardWinURITransferDestroy(&pClientData->State.pCtx->Win, pTransfer);
803
804 return VINF_SUCCESS;
805}
806#endif /* VBOX_WITH_SHARED_CLIPBOARD_URI_LIST */
807
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette