VirtualBox

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

Last change on this file since 100765 was 100655, checked in by vboxsync, 17 months ago

Shared Clipboard/VRDE: Integrated the logic of ShClSvcIsBackendActive() in ShClSvcReportFormats(), so that the specific backends don't need to be in charge for that anymore. Renamed VBOX_CLIPBOARD_EXT_FN_FORMAT_ANNOUNCE -> VBOX_CLIPBOARD_EXT_FN_FORMAT_REPORT_TO_HOST to emphasize its usage. Added VBOX_CLIPBOARD_EXT_FN_FORMAT_REPORT_TO_GUEST; see comment for details.

The whole concept of VRDE being a (one and only) service extension nowadays isn't optimal anymore and needs to be replaced with a more generic mechanism, which is out of this scope for this task. So just adapt this as needed and keep the current way.

bugref:9437

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 37.6 KB
Line 
1/* $Id: VBoxSharedClipboardSvc-win.cpp 100655 2023-07-19 16:06:24Z vboxsync $ */
2/** @file
3 * Shared Clipboard Service - Win32 host.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
33#include <iprt/win/windows.h>
34
35#include <VBox/HostServices/VBoxClipboardSvc.h>
36#include <VBox/GuestHost/clipboard-helper.h>
37#include <VBox/GuestHost/SharedClipboard-win.h>
38#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
39# include <VBox/GuestHost/SharedClipboard-transfers.h>
40#endif
41
42#include <iprt/alloc.h>
43#include <iprt/string.h>
44#include <iprt/asm.h>
45#include <iprt/assert.h>
46#include <iprt/ldr.h>
47#include <iprt/semaphore.h>
48#include <iprt/thread.h>
49#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
50# include <iprt/utf16.h>
51#endif
52
53#include <process.h>
54#include <iprt/win/shlobj.h> /* Needed for shell objects. */
55
56#include "VBoxSharedClipboardSvc-internal.h"
57#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
58# include "VBoxSharedClipboardSvc-transfers.h"
59#endif
60
61
62/*********************************************************************************************************************************
63* Structures and Typedefs *
64*********************************************************************************************************************************/
65/**
66 * Global context information used by the host glue for the X11 clipboard backend.
67 */
68struct SHCLCONTEXT
69{
70 /** Handle for window message handling thread. */
71 RTTHREAD hThread;
72 /** Structure for keeping and communicating with service client. */
73 PSHCLCLIENT pClient;
74 /** Windows-specific context data. */
75 SHCLWINCTX Win;
76};
77
78
79/*********************************************************************************************************************************
80* Prototypes *
81*********************************************************************************************************************************/
82static int vboxClipboardSvcWinSyncInternal(PSHCLCONTEXT pCtx);
83
84#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
85static DECLCALLBACK(int) shClSvcWinTransferIfaceHGRootListRead(PSHCLTXPROVIDERCTX pCtx);
86#endif
87
88
89/**
90 * Copy clipboard data into the guest buffer.
91 *
92 * At first attempt, guest will provide a buffer of default size.
93 * Usually 1K or 4K (see platform specific Guest Additions code around
94 * VbglR3ClipboardReadData calls). If this buffer is not big enough
95 * to fit host clipboard content, this function will return VINF_BUFFER_OVERFLOW
96 * and provide guest with host's clipboard buffer actual size. This will be a
97 * signal for the guest to re-read host clipboard data providing bigger buffer
98 * to store it.
99 *
100 * @returns IPRT status code.
101 * @returns VINF_BUFFER_OVERFLOW returned when guest buffer size if not big
102 * enough to store host clipboard data. This is a signal to the guest
103 * to re-issue host clipboard read request with bigger buffer size
104 * (specified in @a pcbActualDst output parameter).
105 * @param u32Format VBox clipboard format (VBOX_SHCL_FMT_XXX) of copied data.
106 * VBOX_SHCL_FMT_NONE returns 0 data.
107 * @param pvSrc Pointer to host clipboard data.
108 * @param cbSrc Size (in bytes) of actual clipboard data to copy.
109 * @param pvDst Pointer to guest buffer to store clipboard data.
110 * @param cbDst Size (in bytes) of guest buffer.
111 * @param pcbActualDst Actual size (in bytes) of host clipboard data.
112 * Only set if guest buffer size if not big enough
113 * to store host clipboard content. When set,
114 * function returns VINF_BUFFER_OVERFLOW.
115 */
116static int vboxClipboardSvcWinDataGet(SHCLFORMAT u32Format, const void *pvSrc, uint32_t cbSrc,
117 void *pvDst, uint32_t cbDst, uint32_t *pcbActualDst)
118{
119 AssertPtrReturn(pcbActualDst, VERR_INVALID_POINTER);
120 if (u32Format == VBOX_SHCL_FMT_NONE)
121 {
122 *pcbActualDst = 0;
123 return VINF_SUCCESS;
124 }
125
126 AssertPtrReturn(pvSrc, VERR_INVALID_POINTER);
127 AssertReturn (cbSrc, VERR_INVALID_PARAMETER);
128 AssertPtrReturn(pvDst, VERR_INVALID_POINTER);
129 AssertReturn (cbDst, VERR_INVALID_PARAMETER);
130
131 LogFlowFunc(("cbSrc = %d, cbDst = %d\n", cbSrc, cbDst));
132
133 if ( u32Format == VBOX_SHCL_FMT_HTML
134 && SharedClipboardWinIsCFHTML((const char *)pvSrc))
135 {
136 /** @todo r=bird: Why the double conversion? */
137 char *pszBuf = NULL;
138 uint32_t cbBuf = 0;
139 int rc = SharedClipboardWinConvertCFHTMLToMIME((const char *)pvSrc, cbSrc, &pszBuf, &cbBuf);
140 if (RT_SUCCESS(rc))
141 {
142 *pcbActualDst = cbBuf;
143 if (cbBuf > cbDst)
144 {
145 /* Do not copy data. The dst buffer is not enough. */
146 RTMemFree(pszBuf);
147 return VINF_BUFFER_OVERFLOW;
148 }
149 memcpy(pvDst, pszBuf, cbBuf);
150 RTMemFree(pszBuf);
151 }
152 else
153 *pcbActualDst = 0;
154 }
155 else
156 {
157 *pcbActualDst = cbSrc; /* Tell the caller how much space we need. */
158
159 if (cbSrc > cbDst)
160 return VINF_BUFFER_OVERFLOW;
161
162 memcpy(pvDst, pvSrc, cbSrc);
163 }
164
165#ifdef LOG_ENABLED
166 ShClDbgDumpData(pvDst, cbSrc, u32Format);
167#endif
168
169 return VINF_SUCCESS;
170}
171
172/**
173 * Worker for a reading clipboard from the guest.
174 */
175static int vboxClipboardSvcWinReadDataFromGuestWorker(PSHCLCONTEXT pCtx, SHCLFORMAT uFmt, void **ppvData, uint32_t *pcbData)
176{
177 LogFlowFunc(("uFmt=%#x\n", uFmt));
178
179 int rc = ShClSvcReadDataFromGuest(pCtx->pClient, uFmt, ppvData, pcbData);
180 if (RT_FAILURE(rc))
181 LogRel(("Shared Clipboard: Reading guest clipboard data for Windows host failed with %Rrc\n", rc));
182
183 LogFlowFuncLeaveRC(rc);
184 return rc;
185}
186
187static int vboxClipboardSvcWinReadDataFromGuest(PSHCLCONTEXT pCtx, UINT uWinFormat, void **ppvData, uint32_t *pcbData)
188{
189 SHCLFORMAT uVBoxFmt = SharedClipboardWinClipboardFormatToVBox(uWinFormat);
190 if (uVBoxFmt == VBOX_SHCL_FMT_NONE)
191 {
192 LogRel2(("Shared Clipboard: Windows format %u not supported, ignoring\n", uWinFormat));
193 return VERR_NOT_SUPPORTED;
194 }
195
196 int rc = vboxClipboardSvcWinReadDataFromGuestWorker(pCtx, uVBoxFmt, ppvData, pcbData);
197
198 LogFlowFuncLeaveRC(rc);
199 return rc;
200}
201
202/**
203 * @copydoc SHCLCALLBACKS::pfnOnRequestDataFromSource
204 *
205 * Called from the IDataObject implementation to request data from the guest.
206 *
207 * @thread Windows event thread.
208 */
209static DECLCALLBACK(int) vboxClipboardSvcWinRequestDataFromSourceCallback(PSHCLCONTEXT pCtx,
210 SHCLFORMAT uFmt, void **ppv, uint32_t *pcb, void *pvUser)
211{
212 RT_NOREF(pvUser);
213
214 LogFlowFuncEnter();
215
216 int rc = vboxClipboardSvcWinReadDataFromGuestWorker(pCtx, uFmt, ppv, pcb);
217
218 LogFlowFuncLeaveRC(rc);
219
220 return rc;
221}
222
223
224#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
225/**
226 * @copydoc SHCLTRANSFERCALLBACKS::pfnOnCreated
227 *
228 * @thread Service main thread.
229 */
230static DECLCALLBACK(void) shClSvcWinTransferOnCreatedCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx)
231{
232 LogFlowFuncEnter();
233
234 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
235 AssertPtr(pCtx);
236
237 PSHCLTRANSFER pTransfer = pCbCtx->pTransfer;
238 AssertPtr(pTransfer);
239
240 PSHCLCLIENT const pClient = pCtx->pClient;
241 AssertPtr(pClient);
242
243 /*
244 * Set transfer provider.
245 * Those will be registered within ShClSvcTransferInit() when a new transfer gets initialized.
246 */
247
248 /* Set the interface to the local provider by default first. */
249 RT_ZERO(pClient->Transfers.Provider);
250 ShClTransferProviderLocalQueryInterface(&pClient->Transfers.Provider);
251
252 PSHCLTXPROVIDERIFACE pIface = &pClient->Transfers.Provider.Interface;
253
254 pClient->Transfers.Provider.enmSource = pClient->State.enmSource;
255 pClient->Transfers.Provider.pvUser = pClient;
256
257 int rc = VINF_SUCCESS;
258
259 switch (ShClTransferGetDir(pTransfer))
260 {
261 case SHCLTRANSFERDIR_FROM_REMOTE: /* G->H */
262 {
263 pIface->pfnRootListRead = shClSvcTransferIfaceGHRootListRead;
264
265 pIface->pfnListOpen = shClSvcTransferIfaceGHListOpen;
266 pIface->pfnListClose = shClSvcTransferIfaceGHListClose;
267 pIface->pfnListHdrRead = shClSvcTransferIfaceGHListHdrRead;
268 pIface->pfnListEntryRead = shClSvcTransferIfaceGHListEntryRead;
269
270 pIface->pfnObjOpen = shClSvcTransferIfaceGHObjOpen;
271 pIface->pfnObjClose = shClSvcTransferIfaceGHObjClose;
272 pIface->pfnObjRead = shClSvcTransferIfaceGHObjRead;
273 break;
274 }
275
276 case SHCLTRANSFERDIR_TO_REMOTE: /* H->G */
277 {
278 pIface->pfnRootListRead = shClSvcWinTransferIfaceHGRootListRead;
279 break;
280 }
281
282 default:
283 AssertFailedStmt(rc = VERR_NOT_SUPPORTED);
284 break;
285 }
286
287 if (RT_SUCCESS(rc))
288 {
289 rc = ShClTransferSetProvider(pTransfer, &pClient->Transfers.Provider);
290 if (RT_SUCCESS(rc))
291 rc = SharedClipboardWinTransferCreate(&pCtx->Win, pTransfer);
292 }
293
294 LogFlowFuncLeaveRC(rc);
295}
296
297/**
298 * @copydoc SHCLTRANSFERCALLBACKS::pfnOnInitialized
299 *
300 * For G->H: Called on transfer intialization to notify the "in-flight" IDataObject about a data transfer.
301 * For H->G: Called on transfer intialization to populate the transfer's root list.
302 *
303 * @thread Service main thread.
304 */
305static DECLCALLBACK(void) shClSvcWinTransferOnInitializedCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx)
306{
307 LogFlowFuncEnter();
308
309 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
310 AssertPtr(pCtx);
311
312 PSHCLTRANSFER pTransfer = pCbCtx->pTransfer;
313 AssertPtr(pTransfer);
314
315 int rc = VINF_SUCCESS;
316
317 switch (ShClTransferGetDir(pTransfer))
318 {
319 case SHCLTRANSFERDIR_FROM_REMOTE: /* G->H */
320 {
321 rc = RTCritSectEnter(&pCtx->Win.CritSect);
322 if (RT_SUCCESS(rc))
323 {
324 SharedClipboardWinDataObject *pObj = pCtx->Win.pDataObjInFlight;
325 if (pObj)
326 {
327 rc = pObj->SetTransfer(pTransfer);
328 if (RT_SUCCESS(rc))
329 rc = pObj->SetStatus(SharedClipboardWinDataObject::Running);
330
331 pCtx->Win.pDataObjInFlight = NULL; /* Hand off to Windows. */
332 }
333 else
334 AssertMsgFailed(("No data object in flight!\n"));
335
336 int rc2 = RTCritSectLeave(&pCtx->Win.CritSect);
337 AssertRC(rc2);
338 }
339
340 break;
341 }
342
343 case SHCLTRANSFERDIR_TO_REMOTE: /* H->G */
344 {
345 rc = ShClTransferRootListRead(pTransfer); /* Calls shClSvcWinTransferIfaceHGRootListRead(). */
346 break;
347 }
348
349 default:
350 AssertFailedStmt(rc = VERR_NOT_SUPPORTED);
351 break;
352 }
353
354 LogFlowFuncLeaveRC(rc);
355}
356
357/**
358 * @copydoc SHCLTRANSFERCALLBACKS::pfnOnDestroy
359 *
360 * @thread Service main thread.
361 */
362static DECLCALLBACK(void) shClSvcWinTransferOnDestroyCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx)
363{
364 LogFlowFuncEnter();
365
366 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
367 AssertPtr(pCtx);
368
369 PSHCLTRANSFER pTransfer = pCbCtx->pTransfer;
370 AssertPtr(pTransfer);
371
372 SharedClipboardWinTransferDestroy(&pCtx->Win, pTransfer);
373}
374
375/**
376 * @copydoc SharedClipboardWinDataObject::CALLBACKS::pfnTransferBegin
377 *
378 * Called by SharedClipboardWinDataObject::GetData() when the user wants to paste data.
379 * This then creates and initializes a new transfer on the host + lets the guest know about that new transfer.
380 *
381 * @thread Service main thread.
382 */
383static DECLCALLBACK(int) shClSvcWinDataObjectTransferBeginCallback(SharedClipboardWinDataObject::PCALLBACKCTX pCbCtx)
384{
385 LogFlowFuncEnter();
386
387 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
388 AssertPtr(pCtx);
389
390 PSHCLTRANSFER pTransfer;
391 int rc = ShClSvcTransferCreate(pCtx->pClient, SHCLTRANSFERDIR_FROM_REMOTE, SHCLSOURCE_REMOTE,
392 NIL_SHCLTRANSFERID /* Creates a new transfer ID */, &pTransfer);
393 if (RT_SUCCESS(rc))
394 {
395 /* Initialize the transfer on the host side. */
396 rc = ShClSvcTransferInit(pCtx->pClient, pTransfer);
397 if (RT_FAILURE(rc))
398 ShClSvcTransferDestroy(pCtx->pClient, pTransfer);
399 }
400
401 LogFlowFuncLeaveRC(rc);
402 return rc;
403}
404#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
405
406static LRESULT CALLBACK vboxClipboardSvcWinWndProcMain(PSHCLCONTEXT pCtx,
407 HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) RT_NOTHROW_DEF
408{
409 AssertPtr(pCtx);
410
411 LRESULT lresultRc = 0;
412
413 const PSHCLWINCTX pWinCtx = &pCtx->Win;
414
415 switch (uMsg)
416 {
417 case WM_CLIPBOARDUPDATE:
418 {
419 LogFunc(("WM_CLIPBOARDUPDATE\n"));
420
421 int rc = RTCritSectEnter(&pWinCtx->CritSect);
422 if (RT_SUCCESS(rc))
423 {
424 const HWND hWndClipboardOwner = GetClipboardOwner();
425
426 LogFunc(("WM_CLIPBOARDUPDATE: hWndClipboardOwnerUs=%p, hWndNewClipboardOwner=%p\n",
427 pWinCtx->hWndClipboardOwnerUs, hWndClipboardOwner));
428
429 if (pWinCtx->hWndClipboardOwnerUs != hWndClipboardOwner)
430 {
431 int rc2 = RTCritSectLeave(&pWinCtx->CritSect);
432 AssertRC(rc2);
433
434 /* Clipboard was updated by another application, retrieve formats and report back. */
435 rc = vboxClipboardSvcWinSyncInternal(pCtx);
436 }
437 else
438 {
439 int rc2 = RTCritSectLeave(&pWinCtx->CritSect);
440 AssertRC(rc2);
441 }
442 }
443
444 if (RT_FAILURE(rc))
445 LogRel(("Shared Clipboard: WM_CLIPBOARDUPDATE failed with %Rrc\n", rc));
446
447 break;
448 }
449
450 case WM_CHANGECBCHAIN:
451 {
452 LogFunc(("WM_CHANGECBCHAIN\n"));
453 lresultRc = SharedClipboardWinHandleWMChangeCBChain(pWinCtx, hWnd, uMsg, wParam, lParam);
454 break;
455 }
456
457 case WM_DRAWCLIPBOARD:
458 {
459 LogFunc(("WM_DRAWCLIPBOARD\n"));
460
461 int rc = RTCritSectEnter(&pWinCtx->CritSect);
462 if (RT_SUCCESS(rc))
463 {
464 const HWND hWndClipboardOwner = GetClipboardOwner();
465
466 LogFunc(("WM_DRAWCLIPBOARD: hWndClipboardOwnerUs=%p, hWndNewClipboardOwner=%p\n",
467 pWinCtx->hWndClipboardOwnerUs, hWndClipboardOwner));
468
469 if (pWinCtx->hWndClipboardOwnerUs != hWndClipboardOwner)
470 {
471 int rc2 = RTCritSectLeave(&pWinCtx->CritSect);
472 AssertRC(rc2);
473
474 /* Clipboard was updated by another application, retrieve formats and report back. */
475 rc = vboxClipboardSvcWinSyncInternal(pCtx);
476 }
477 else
478 {
479 int rc2 = RTCritSectLeave(&pWinCtx->CritSect);
480 AssertRC(rc2);
481 }
482 }
483
484 lresultRc = SharedClipboardWinChainPassToNext(pWinCtx, uMsg, wParam, lParam);
485 break;
486 }
487
488 case WM_TIMER:
489 {
490 int rc = SharedClipboardWinHandleWMTimer(pWinCtx);
491 AssertRC(rc);
492
493 break;
494 }
495
496 case WM_RENDERFORMAT:
497 {
498 /* Insert the requested clipboard format data into the clipboard. */
499 const UINT uFmtWin = (UINT)wParam;
500 const SHCLFORMAT uFmtVBox = SharedClipboardWinClipboardFormatToVBox(uFmtWin);
501
502 LogFunc(("WM_RENDERFORMAT: uFmtWin=%u -> uFmtVBox=0x%x\n", uFmtWin, uFmtVBox));
503#ifdef LOG_ENABLED
504 char *pszFmts = ShClFormatsToStrA(uFmtVBox);
505 AssertPtrReturn(pszFmts, 0);
506 LogRel(("Shared Clipboard: Rendering Windows format %#x as VBox format '%s'\n", uFmtWin, pszFmts));
507 RTStrFree(pszFmts);
508#endif
509 if ( uFmtVBox == VBOX_SHCL_FMT_NONE
510 || pCtx->pClient == NULL)
511 {
512 /* Unsupported clipboard format is requested. */
513 LogFunc(("WM_RENDERFORMAT unsupported format requested or client is not active\n"));
514 SharedClipboardWinClear();
515 }
516 else
517 {
518 void *pvData = NULL;
519 uint32_t cbData = 0;
520 int rc = ShClSvcReadDataFromGuest(pCtx->pClient, uFmtVBox, &pvData, &cbData);
521 if ( RT_SUCCESS(rc)
522 && pvData
523 && cbData)
524 {
525 /* Wrap HTML clipboard content info CF_HTML format if needed. */
526 if (uFmtVBox == VBOX_SHCL_FMT_HTML
527 && !SharedClipboardWinIsCFHTML((char *)pvData))
528 {
529 char *pszWrapped = NULL;
530 uint32_t cbWrapped = 0;
531 rc = SharedClipboardWinConvertMIMEToCFHTML((char *)pvData, cbData, &pszWrapped, &cbWrapped);
532 if (RT_SUCCESS(rc))
533 {
534 /* Replace buffer with wrapped data content. */
535 RTMemFree(pvData);
536 pvData = (void *)pszWrapped;
537 cbData = cbWrapped;
538 }
539 else
540 LogRel(("Shared Clipboard: cannot convert HTML clipboard into CF_HTML format, rc=%Rrc\n", rc));
541 }
542
543 rc = SharedClipboardWinDataWrite(uFmtWin, pvData, cbData);
544 if (RT_FAILURE(rc))
545 LogRel(("Shared Clipboard: Setting clipboard data for Windows host failed with %Rrc\n", rc));
546
547 RTMemFree(pvData);
548 cbData = 0;
549 }
550
551 if (RT_FAILURE(rc))
552 SharedClipboardWinClear();
553 }
554
555 break;
556 }
557
558 case WM_RENDERALLFORMATS:
559 {
560 LogFunc(("WM_RENDERALLFORMATS\n"));
561
562 int rc = SharedClipboardWinHandleWMRenderAllFormats(pWinCtx, hWnd);
563 AssertRC(rc);
564
565 break;
566 }
567
568 case SHCL_WIN_WM_REPORT_FORMATS: /* Guest reported clipboard formats. */
569 {
570 /* Announce available formats. Do not insert data -- will be inserted in WM_RENDERFORMAT (or via IDataObject). */
571 SHCLFORMATS fFormats = (uint32_t)lParam;
572 LogFunc(("SHCL_WIN_WM_REPORT_FORMATS: fFormats=%#xn", fFormats));
573
574 int rc = SharedClipboardWinClearAndAnnounceFormats(pWinCtx, fFormats, hWnd);
575#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
576 if ( RT_SUCCESS(rc)
577 && fFormats & VBOX_SHCL_FMT_URI_LIST)
578 {
579 /*
580 * Create our IDataObject implementation and push it to the Windows clibpoard.
581 * That way Windows will recognize that there is a data transfer available.
582 */
583 SharedClipboardWinDataObject::CALLBACKS Callbacks;
584 RT_ZERO(Callbacks);
585 Callbacks.pfnTransferBegin = shClSvcWinDataObjectTransferBeginCallback;
586
587 rc = SharedClipboardWinTransferCreateAndSetDataObject(pWinCtx, pCtx, &Callbacks);
588 }
589#else
590 RT_NOREF(rc);
591#endif
592 LogFunc(("SHCL_WIN_WM_REPORT_FORMATS: lastErr=%ld\n", GetLastError()));
593 break;
594 }
595
596 case WM_DESTROY:
597 {
598 LogFunc(("WM_DESTROY\n"));
599
600 int rc = SharedClipboardWinHandleWMDestroy(pWinCtx);
601 AssertRC(rc);
602
603 PostQuitMessage(0);
604 break;
605 }
606
607 default:
608 lresultRc = DefWindowProc(hWnd, uMsg, wParam, lParam);
609 break;
610 }
611
612 LogFlowFunc(("LEAVE hWnd=%p, WM_ %u -> %#zx\n", hWnd, uMsg, lresultRc));
613 return lresultRc;
614}
615
616/**
617 * Static helper function for having a per-client proxy window instances.
618 */
619static LRESULT CALLBACK vboxClipboardSvcWinWndProcInstance(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) RT_NOTHROW_DEF
620{
621 LONG_PTR pUserData = GetWindowLongPtr(hWnd, GWLP_USERDATA);
622 AssertPtrReturn(pUserData, 0);
623
624 PSHCLCONTEXT pCtx = reinterpret_cast<PSHCLCONTEXT>(pUserData);
625 if (pCtx)
626 return vboxClipboardSvcWinWndProcMain(pCtx, hWnd, uMsg, wParam, lParam);
627
628 return 0;
629}
630
631/**
632 * Static helper function for routing Windows messages to a specific
633 * proxy window instance.
634 */
635static LRESULT CALLBACK vboxClipboardSvcWinWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) RT_NOTHROW_DEF
636{
637 /* Note: WM_NCCREATE is not the first ever message which arrives, but
638 * early enough for us. */
639 if (uMsg == WM_NCCREATE)
640 {
641 LogFlowFunc(("WM_NCCREATE\n"));
642
643 LPCREATESTRUCT pCS = (LPCREATESTRUCT)lParam;
644 AssertPtr(pCS);
645 SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)pCS->lpCreateParams);
646 SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)vboxClipboardSvcWinWndProcInstance);
647
648 return vboxClipboardSvcWinWndProcInstance(hWnd, uMsg, wParam, lParam);
649 }
650
651 /* No window associated yet. */
652 return DefWindowProc(hWnd, uMsg, wParam, lParam);
653}
654
655DECLCALLBACK(int) vboxClipboardSvcWinThread(RTTHREAD hThreadSelf, void *pvUser)
656{
657 LogFlowFuncEnter();
658
659 bool fThreadSignalled = false;
660
661 const PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pvUser;
662 AssertPtr(pCtx);
663 const PSHCLWINCTX pWinCtx = &pCtx->Win;
664
665 HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
666
667 /* Register the Window Class. */
668 WNDCLASS wc;
669 RT_ZERO(wc);
670
671 wc.style = CS_NOCLOSE;
672 wc.lpfnWndProc = vboxClipboardSvcWinWndProc;
673 wc.hInstance = hInstance;
674 wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
675
676 /* Register an unique wnd class name. */
677 char szWndClassName[32];
678 RTStrPrintf2(szWndClassName, sizeof(szWndClassName),
679 "%s-%RU64", SHCL_WIN_WNDCLASS_NAME, RTThreadGetNative(hThreadSelf));
680 wc.lpszClassName = szWndClassName;
681
682 int rc;
683
684 ATOM atomWindowClass = RegisterClass(&wc);
685 if (atomWindowClass == 0)
686 {
687 LogFunc(("Failed to register window class\n"));
688 rc = VERR_NOT_SUPPORTED;
689 }
690 else
691 {
692 /* Create a window and make it a clipboard viewer. */
693 pWinCtx->hWnd = CreateWindowEx(WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
694 szWndClassName, szWndClassName,
695 WS_POPUPWINDOW,
696 -200, -200, 100, 100, NULL, NULL, hInstance, pCtx /* lpParam */);
697 if (pWinCtx->hWnd == NULL)
698 {
699 LogFunc(("Failed to create window\n"));
700 rc = VERR_NOT_SUPPORTED;
701 }
702 else
703 {
704 SetWindowPos(pWinCtx->hWnd, HWND_TOPMOST, -200, -200, 0, 0,
705 SWP_NOACTIVATE | SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_NOSIZE);
706
707 rc = SharedClipboardWinChainAdd(&pCtx->Win);
708 if (RT_SUCCESS(rc))
709 {
710 if (!SharedClipboardWinIsNewAPI(&pWinCtx->newAPI))
711 pWinCtx->oldAPI.timerRefresh = SetTimer(pWinCtx->hWnd, 0, 10 * 1000, NULL);
712 }
713
714#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
715 if (RT_SUCCESS(rc))
716 {
717 HRESULT hr = OleInitialize(NULL);
718 if (FAILED(hr))
719 {
720 LogRel(("Shared Clipboard: Initializing window thread OLE failed (%Rhrc) -- file transfers unavailable\n", hr));
721 /* Not critical, the rest of the clipboard might work. */
722 }
723 else
724 LogRel(("Shared Clipboard: Initialized window thread OLE\n"));
725 }
726#endif
727 int rc2 = RTThreadUserSignal(hThreadSelf);
728 AssertRC(rc2);
729
730 fThreadSignalled = true;
731
732 MSG msg;
733 BOOL msgret = 0;
734 while ((msgret = GetMessage(&msg, NULL, 0, 0)) > 0)
735 {
736 TranslateMessage(&msg);
737 DispatchMessage(&msg);
738 }
739
740 /*
741 * Window procedure can return error, * but this is exceptional situation that should be
742 * identified in testing.
743 */
744 Assert(msgret >= 0);
745 LogFunc(("Message loop finished. GetMessage returned %d, message id: %d \n", msgret, msg.message));
746
747#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
748 OleSetClipboard(NULL); /* Make sure to flush the clipboard on destruction. */
749 OleUninitialize();
750#endif
751 }
752 }
753
754 pWinCtx->hWnd = NULL;
755
756 if (atomWindowClass != 0)
757 {
758 UnregisterClass(szWndClassName, hInstance);
759 atomWindowClass = 0;
760 }
761
762 if (!fThreadSignalled)
763 {
764 int rc2 = RTThreadUserSignal(hThreadSelf);
765 AssertRC(rc2);
766 }
767
768 LogFlowFuncLeaveRC(rc);
769 return rc;
770}
771
772/**
773 * Synchronizes the host and the guest clipboard formats by sending all supported host clipboard
774 * formats to the guest.
775 *
776 * @returns VBox status code, VINF_NO_CHANGE if no synchronization was required.
777 * @param pCtx Clipboard context to synchronize.
778 */
779static int vboxClipboardSvcWinSyncInternal(PSHCLCONTEXT pCtx)
780{
781 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
782
783 LogFlowFuncEnter();
784
785 int rc;
786
787 if (pCtx->pClient)
788 {
789 SHCLFORMATS fFormats = 0;
790 rc = SharedClipboardWinGetFormats(&pCtx->Win, &fFormats);
791 if (RT_SUCCESS(rc))
792 rc = ShClSvcReportFormats(pCtx->pClient, fFormats);
793 }
794 else /* If we don't have any client data (yet), bail out. */
795 rc = VINF_NO_CHANGE;
796
797 LogFlowFuncLeaveRC(rc);
798 return rc;
799}
800
801
802/*********************************************************************************************************************************
803* Backend implementation *
804*********************************************************************************************************************************/
805int ShClBackendInit(PSHCLBACKEND pBackend, VBOXHGCMSVCFNTABLE *pTable)
806{
807 RT_NOREF(pBackend, pTable);
808#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
809 HRESULT hr = OleInitialize(NULL);
810 if (FAILED(hr))
811 {
812 LogRel(("Shared Clipboard: Initializing OLE failed (%Rhrc) -- file transfers unavailable\n", hr));
813 /* Not critical, the rest of the clipboard might work. */
814 }
815 else
816 LogRel(("Shared Clipboard: Initialized OLE\n"));
817#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
818
819 return VINF_SUCCESS;
820}
821
822void ShClBackendDestroy(PSHCLBACKEND pBackend)
823{
824 RT_NOREF(pBackend);
825
826#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
827 OleSetClipboard(NULL); /* Make sure to flush the clipboard on destruction. */
828 OleUninitialize();
829#endif
830}
831
832int ShClBackendConnect(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, bool fHeadless)
833{
834 RT_NOREF(pBackend, fHeadless);
835
836 LogFlowFuncEnter();
837
838 int rc;
839
840 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)RTMemAllocZ(sizeof(SHCLCONTEXT));
841 if (pCtx)
842 {
843 rc = SharedClipboardWinCtxInit(&pCtx->Win);
844 if (RT_SUCCESS(rc))
845 {
846 rc = RTThreadCreate(&pCtx->hThread, vboxClipboardSvcWinThread, pCtx /* pvUser */, _64K /* Stack size */,
847 RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "ShClWin");
848 if (RT_SUCCESS(rc))
849 {
850 int rc2 = RTThreadUserWait(pCtx->hThread, RT_MS_30SEC /* Timeout in ms */);
851 AssertRC(rc2);
852 }
853 }
854
855 pClient->State.pCtx = pCtx;
856 pClient->State.pCtx->pClient = pClient;
857
858#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
859 /*
860 * Set callbacks.
861 * Those will be registered within ShClSvcTransferInit() when a new transfer gets initialized.
862 */
863 RT_ZERO(pClient->Transfers.Callbacks);
864
865 pClient->Transfers.Callbacks.pvUser = pCtx; /* Assign context as user-provided callback data. */
866 pClient->Transfers.Callbacks.cbUser = sizeof(SHCLCONTEXT);
867
868 pClient->Transfers.Callbacks.pfnOnCreated = shClSvcWinTransferOnCreatedCallback;
869 pClient->Transfers.Callbacks.pfnOnInitialized = shClSvcWinTransferOnInitializedCallback;
870 pClient->Transfers.Callbacks.pfnOnDestroy = shClSvcWinTransferOnDestroyCallback;
871#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
872 }
873 else
874 rc = VERR_NO_MEMORY;
875
876 LogFlowFuncLeaveRC(rc);
877 return rc;
878}
879
880int ShClBackendSync(PSHCLBACKEND pBackend, PSHCLCLIENT pClient)
881{
882 RT_NOREF(pBackend);
883
884 /* Sync the host clipboard content with the client. */
885 return vboxClipboardSvcWinSyncInternal(pClient->State.pCtx);
886}
887
888int ShClBackendDisconnect(PSHCLBACKEND pBackend, PSHCLCLIENT pClient)
889{
890 RT_NOREF(pBackend);
891
892 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
893
894 LogFlowFuncEnter();
895
896 int rc = VINF_SUCCESS;
897
898 PSHCLCONTEXT pCtx = pClient->State.pCtx;
899 if (pCtx)
900 {
901 if (pCtx->Win.hWnd)
902 PostMessage(pCtx->Win.hWnd, WM_DESTROY, 0 /* wParam */, 0 /* lParam */);
903
904 if (pCtx->hThread != NIL_RTTHREAD)
905 {
906 LogFunc(("Waiting for thread to terminate ...\n"));
907
908 /* Wait for the window thread to terminate. */
909 rc = RTThreadWait(pCtx->hThread, RT_MS_30SEC /* Timeout in ms */, NULL);
910 if (RT_FAILURE(rc))
911 LogRel(("Shared Clipboard: Waiting for window thread termination failed with rc=%Rrc\n", rc));
912
913 pCtx->hThread = NIL_RTTHREAD;
914 }
915
916 SharedClipboardWinCtxDestroy(&pCtx->Win);
917
918 if (RT_SUCCESS(rc))
919 {
920 RTMemFree(pCtx);
921 pCtx = NULL;
922
923 pClient->State.pCtx = NULL;
924 }
925 }
926
927 LogFlowFuncLeaveRC(rc);
928 return rc;
929}
930
931int ShClBackendReportFormats(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, SHCLFORMATS fFormats)
932{
933 RT_NOREF(pBackend);
934
935 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
936
937 PSHCLCONTEXT pCtx = pClient->State.pCtx;
938 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
939
940 LogFlowFunc(("fFormats=0x%x, hWnd=%p\n", fFormats, pCtx->Win.hWnd));
941
942 /*
943 * The guest announced formats. Forward to the window thread.
944 */
945 PostMessage(pCtx->Win.hWnd, SHCL_WIN_WM_REPORT_FORMATS, 0 /* wParam */, fFormats /* lParam */);
946
947 LogFlowFuncLeaveRC(VINF_SUCCESS);
948 return VINF_SUCCESS;
949}
950
951int ShClBackendReadData(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLCLIENTCMDCTX pCmdCtx,
952 SHCLFORMAT uFmt, void *pvData, uint32_t cbData, uint32_t *pcbActual)
953{
954 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
955 AssertPtrReturn(pCmdCtx, VERR_INVALID_POINTER);
956 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
957 AssertPtrReturn(pcbActual, VERR_INVALID_POINTER);
958
959 RT_NOREF(pBackend, pCmdCtx);
960
961 AssertPtrReturn(pClient->State.pCtx, VERR_INVALID_POINTER);
962
963 LogFlowFunc(("uFmt=%#x\n", uFmt));
964
965 HANDLE hClip = NULL;
966
967 const PSHCLWINCTX pWinCtx = &pClient->State.pCtx->Win;
968
969 /*
970 * The guest wants to read data in the given format.
971 */
972 int rc = SharedClipboardWinOpen(pWinCtx->hWnd);
973 if (RT_SUCCESS(rc))
974 {
975 if (uFmt & VBOX_SHCL_FMT_BITMAP)
976 {
977 LogFunc(("CF_DIB\n"));
978 hClip = GetClipboardData(CF_DIB);
979 if (hClip != NULL)
980 {
981 LPVOID lp = GlobalLock(hClip);
982 if (lp != NULL)
983 {
984 rc = vboxClipboardSvcWinDataGet(VBOX_SHCL_FMT_BITMAP, lp, GlobalSize(hClip),
985 pvData, cbData, pcbActual);
986 GlobalUnlock(hClip);
987 }
988 else
989 {
990 hClip = NULL;
991 }
992 }
993 }
994 else if (uFmt & VBOX_SHCL_FMT_UNICODETEXT)
995 {
996 LogFunc(("CF_UNICODETEXT\n"));
997 hClip = GetClipboardData(CF_UNICODETEXT);
998 if (hClip != NULL)
999 {
1000 LPWSTR uniString = (LPWSTR)GlobalLock(hClip);
1001 if (uniString != NULL)
1002 {
1003 rc = vboxClipboardSvcWinDataGet(VBOX_SHCL_FMT_UNICODETEXT, uniString, (lstrlenW(uniString) + 1) * 2,
1004 pvData, cbData, pcbActual);
1005 GlobalUnlock(hClip);
1006 }
1007 else
1008 {
1009 hClip = NULL;
1010 }
1011 }
1012 }
1013 else if (uFmt & VBOX_SHCL_FMT_HTML)
1014 {
1015 LogFunc(("SHCL_WIN_REGFMT_HTML\n"));
1016 UINT uRegFmt = RegisterClipboardFormat(SHCL_WIN_REGFMT_HTML);
1017 if (uRegFmt != 0)
1018 {
1019 hClip = GetClipboardData(uRegFmt);
1020 if (hClip != NULL)
1021 {
1022 LPVOID lp = GlobalLock(hClip);
1023 if (lp != NULL)
1024 {
1025 rc = vboxClipboardSvcWinDataGet(VBOX_SHCL_FMT_HTML, lp, GlobalSize(hClip),
1026 pvData, cbData, pcbActual);
1027#ifdef LOG_ENABLED
1028 if (RT_SUCCESS(rc))
1029 {
1030 LogFlowFunc(("Raw HTML clipboard data from host:\n"));
1031 ShClDbgDumpHtml((char *)pvData, cbData);
1032 }
1033#endif
1034 GlobalUnlock(hClip);
1035 }
1036 else
1037 {
1038 hClip = NULL;
1039 }
1040 }
1041 }
1042 }
1043#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1044 else if (uFmt & VBOX_SHCL_FMT_URI_LIST)
1045 {
1046 hClip = hClip = GetClipboardData(CF_HDROP);
1047 if (hClip)
1048 {
1049 HDROP hDrop = (HDROP)GlobalLock(hClip);
1050 if (hDrop)
1051 {
1052 char *pszList = NULL;
1053 uint32_t cbList;
1054 rc = SharedClipboardWinTransferDropFilesToStringList((DROPFILES *)hDrop, &pszList, &cbList);
1055
1056 GlobalUnlock(hClip);
1057
1058 if (RT_SUCCESS(rc))
1059 {
1060 if (cbList <= cbData)
1061 {
1062 memcpy(pvData, pszList, cbList);
1063 *pcbActual = cbList;
1064 }
1065
1066 RTStrFree(pszList);
1067 }
1068 }
1069 else
1070 LogRel(("Shared Clipboard: Unable to lock clipboard data, last error: %ld\n", GetLastError()));
1071 }
1072 else
1073 LogRel(("Shared Clipboard: Unable to retrieve clipboard data from clipboard (CF_HDROP), last error: %ld\n",
1074 GetLastError()));
1075 }
1076#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
1077 SharedClipboardWinClose();
1078 }
1079
1080 if (RT_FAILURE(rc))
1081 LogRel(("Shared Clipboard: Error reading host clipboard data in format %#x from Windows, rc=%Rrc\n", uFmt, rc));
1082
1083 LogFlowFuncLeaveRC(rc);
1084 return rc;
1085}
1086
1087int ShClBackendWriteData(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLCLIENTCMDCTX pCmdCtx,
1088 SHCLFORMAT uFormat, void *pvData, uint32_t cbData)
1089{
1090 RT_NOREF(pBackend, pClient, pCmdCtx, uFormat, pvData, cbData);
1091
1092 LogFlowFuncEnter();
1093
1094 /* Nothing to do here yet. */
1095
1096 LogFlowFuncLeave();
1097 return VINF_SUCCESS;
1098}
1099
1100#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1101/**
1102 * Handles transfer status replies from the guest.
1103 */
1104int ShClBackendTransferHandleStatusReply(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLTRANSFER pTransfer, SHCLSOURCE enmSource, SHCLTRANSFERSTATUS enmStatus, int rcStatus)
1105{
1106 RT_NOREF(pBackend, pClient, pTransfer, enmSource, enmStatus, rcStatus);
1107
1108 return VINF_SUCCESS;
1109}
1110
1111
1112/*********************************************************************************************************************************
1113* Provider interface implementation *
1114*********************************************************************************************************************************/
1115
1116/** @copydoc SHCLTXPROVIDERIFACE::pfnRootListRead */
1117static DECLCALLBACK(int) shClSvcWinTransferIfaceHGRootListRead(PSHCLTXPROVIDERCTX pCtx)
1118{
1119 LogFlowFuncEnter();
1120
1121 PSHCLCLIENT pClient = (PSHCLCLIENT)pCtx->pvUser;
1122 AssertPtr(pClient);
1123
1124 AssertPtr(pClient->State.pCtx);
1125 PSHCLWINCTX pWin = &pClient->State.pCtx->Win;
1126
1127 int rc = SharedClipboardWinTransferGetRootsFromClipboard(pWin, pCtx->pTransfer);
1128
1129 LogFlowFuncLeaveRC(rc);
1130 return rc;
1131}
1132#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
1133
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