VirtualBox

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

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

Shared Clipboard: Removed shClSvcWinDataObjectTransferEndCallback(), not needed, as ShClSvcTransferDestroy() will be called in shClSvcTransferDestroyAll(). bugref:9437

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 37.7 KB
Line 
1/* $Id: VBoxSharedClipboardSvc-win.cpp 100552 2023-07-12 15:01:19Z 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 && fFormats != VBOX_SHCL_FMT_NONE /** @todo r=bird: BUGBUG: revisit this. */
793 && ShClSvcIsBackendActive())
794 rc = ShClSvcHostReportFormats(pCtx->pClient, fFormats);
795 }
796 else /* If we don't have any client data (yet), bail out. */
797 rc = VINF_NO_CHANGE;
798
799 LogFlowFuncLeaveRC(rc);
800 return rc;
801}
802
803
804/*********************************************************************************************************************************
805* Backend implementation *
806*********************************************************************************************************************************/
807int ShClBackendInit(PSHCLBACKEND pBackend, VBOXHGCMSVCFNTABLE *pTable)
808{
809 RT_NOREF(pBackend, pTable);
810#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
811 HRESULT hr = OleInitialize(NULL);
812 if (FAILED(hr))
813 {
814 LogRel(("Shared Clipboard: Initializing OLE failed (%Rhrc) -- file transfers unavailable\n", hr));
815 /* Not critical, the rest of the clipboard might work. */
816 }
817 else
818 LogRel(("Shared Clipboard: Initialized OLE\n"));
819#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
820
821 return VINF_SUCCESS;
822}
823
824void ShClBackendDestroy(PSHCLBACKEND pBackend)
825{
826 RT_NOREF(pBackend);
827
828#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
829 OleSetClipboard(NULL); /* Make sure to flush the clipboard on destruction. */
830 OleUninitialize();
831#endif
832}
833
834int ShClBackendConnect(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, bool fHeadless)
835{
836 RT_NOREF(pBackend, fHeadless);
837
838 LogFlowFuncEnter();
839
840 int rc;
841
842 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)RTMemAllocZ(sizeof(SHCLCONTEXT));
843 if (pCtx)
844 {
845 rc = SharedClipboardWinCtxInit(&pCtx->Win);
846 if (RT_SUCCESS(rc))
847 {
848 rc = RTThreadCreate(&pCtx->hThread, vboxClipboardSvcWinThread, pCtx /* pvUser */, _64K /* Stack size */,
849 RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "ShClWin");
850 if (RT_SUCCESS(rc))
851 {
852 int rc2 = RTThreadUserWait(pCtx->hThread, RT_MS_30SEC /* Timeout in ms */);
853 AssertRC(rc2);
854 }
855 }
856
857 pClient->State.pCtx = pCtx;
858 pClient->State.pCtx->pClient = pClient;
859
860#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
861 /*
862 * Set callbacks.
863 * Those will be registered within ShClSvcTransferInit() when a new transfer gets initialized.
864 */
865 RT_ZERO(pClient->Transfers.Callbacks);
866
867 pClient->Transfers.Callbacks.pvUser = pCtx; /* Assign context as user-provided callback data. */
868 pClient->Transfers.Callbacks.cbUser = sizeof(SHCLCONTEXT);
869
870 pClient->Transfers.Callbacks.pfnOnCreated = shClSvcWinTransferOnCreatedCallback;
871 pClient->Transfers.Callbacks.pfnOnInitialized = shClSvcWinTransferOnInitializedCallback;
872 pClient->Transfers.Callbacks.pfnOnDestroy = shClSvcWinTransferOnDestroyCallback;
873#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
874 }
875 else
876 rc = VERR_NO_MEMORY;
877
878 LogFlowFuncLeaveRC(rc);
879 return rc;
880}
881
882int ShClBackendSync(PSHCLBACKEND pBackend, PSHCLCLIENT pClient)
883{
884 RT_NOREF(pBackend);
885
886 /* Sync the host clipboard content with the client. */
887 return vboxClipboardSvcWinSyncInternal(pClient->State.pCtx);
888}
889
890int ShClBackendDisconnect(PSHCLBACKEND pBackend, PSHCLCLIENT pClient)
891{
892 RT_NOREF(pBackend);
893
894 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
895
896 LogFlowFuncEnter();
897
898 int rc = VINF_SUCCESS;
899
900 PSHCLCONTEXT pCtx = pClient->State.pCtx;
901 if (pCtx)
902 {
903 if (pCtx->Win.hWnd)
904 PostMessage(pCtx->Win.hWnd, WM_DESTROY, 0 /* wParam */, 0 /* lParam */);
905
906 if (pCtx->hThread != NIL_RTTHREAD)
907 {
908 LogFunc(("Waiting for thread to terminate ...\n"));
909
910 /* Wait for the window thread to terminate. */
911 rc = RTThreadWait(pCtx->hThread, RT_MS_30SEC /* Timeout in ms */, NULL);
912 if (RT_FAILURE(rc))
913 LogRel(("Shared Clipboard: Waiting for window thread termination failed with rc=%Rrc\n", rc));
914
915 pCtx->hThread = NIL_RTTHREAD;
916 }
917
918 SharedClipboardWinCtxDestroy(&pCtx->Win);
919
920 if (RT_SUCCESS(rc))
921 {
922 RTMemFree(pCtx);
923 pCtx = NULL;
924
925 pClient->State.pCtx = NULL;
926 }
927 }
928
929 LogFlowFuncLeaveRC(rc);
930 return rc;
931}
932
933int ShClBackendReportFormats(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, SHCLFORMATS fFormats)
934{
935 RT_NOREF(pBackend);
936
937 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
938
939 PSHCLCONTEXT pCtx = pClient->State.pCtx;
940 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
941
942 LogFlowFunc(("fFormats=0x%x, hWnd=%p\n", fFormats, pCtx->Win.hWnd));
943
944 /*
945 * The guest announced formats. Forward to the window thread.
946 */
947 PostMessage(pCtx->Win.hWnd, SHCL_WIN_WM_REPORT_FORMATS, 0 /* wParam */, fFormats /* lParam */);
948
949 LogFlowFuncLeaveRC(VINF_SUCCESS);
950 return VINF_SUCCESS;
951}
952
953int ShClBackendReadData(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLCLIENTCMDCTX pCmdCtx,
954 SHCLFORMAT uFmt, void *pvData, uint32_t cbData, uint32_t *pcbActual)
955{
956 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
957 AssertPtrReturn(pCmdCtx, VERR_INVALID_POINTER);
958 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
959 AssertPtrReturn(pcbActual, VERR_INVALID_POINTER);
960
961 RT_NOREF(pBackend, pCmdCtx);
962
963 AssertPtrReturn(pClient->State.pCtx, VERR_INVALID_POINTER);
964
965 LogFlowFunc(("uFmt=%#x\n", uFmt));
966
967 HANDLE hClip = NULL;
968
969 const PSHCLWINCTX pWinCtx = &pClient->State.pCtx->Win;
970
971 /*
972 * The guest wants to read data in the given format.
973 */
974 int rc = SharedClipboardWinOpen(pWinCtx->hWnd);
975 if (RT_SUCCESS(rc))
976 {
977 if (uFmt & VBOX_SHCL_FMT_BITMAP)
978 {
979 LogFunc(("CF_DIB\n"));
980 hClip = GetClipboardData(CF_DIB);
981 if (hClip != NULL)
982 {
983 LPVOID lp = GlobalLock(hClip);
984 if (lp != NULL)
985 {
986 rc = vboxClipboardSvcWinDataGet(VBOX_SHCL_FMT_BITMAP, lp, GlobalSize(hClip),
987 pvData, cbData, pcbActual);
988 GlobalUnlock(hClip);
989 }
990 else
991 {
992 hClip = NULL;
993 }
994 }
995 }
996 else if (uFmt & VBOX_SHCL_FMT_UNICODETEXT)
997 {
998 LogFunc(("CF_UNICODETEXT\n"));
999 hClip = GetClipboardData(CF_UNICODETEXT);
1000 if (hClip != NULL)
1001 {
1002 LPWSTR uniString = (LPWSTR)GlobalLock(hClip);
1003 if (uniString != NULL)
1004 {
1005 rc = vboxClipboardSvcWinDataGet(VBOX_SHCL_FMT_UNICODETEXT, uniString, (lstrlenW(uniString) + 1) * 2,
1006 pvData, cbData, pcbActual);
1007 GlobalUnlock(hClip);
1008 }
1009 else
1010 {
1011 hClip = NULL;
1012 }
1013 }
1014 }
1015 else if (uFmt & VBOX_SHCL_FMT_HTML)
1016 {
1017 LogFunc(("SHCL_WIN_REGFMT_HTML\n"));
1018 UINT uRegFmt = RegisterClipboardFormat(SHCL_WIN_REGFMT_HTML);
1019 if (uRegFmt != 0)
1020 {
1021 hClip = GetClipboardData(uRegFmt);
1022 if (hClip != NULL)
1023 {
1024 LPVOID lp = GlobalLock(hClip);
1025 if (lp != NULL)
1026 {
1027 rc = vboxClipboardSvcWinDataGet(VBOX_SHCL_FMT_HTML, lp, GlobalSize(hClip),
1028 pvData, cbData, pcbActual);
1029#ifdef LOG_ENABLED
1030 if (RT_SUCCESS(rc))
1031 {
1032 LogFlowFunc(("Raw HTML clipboard data from host:\n"));
1033 ShClDbgDumpHtml((char *)pvData, cbData);
1034 }
1035#endif
1036 GlobalUnlock(hClip);
1037 }
1038 else
1039 {
1040 hClip = NULL;
1041 }
1042 }
1043 }
1044 }
1045#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1046 else if (uFmt & VBOX_SHCL_FMT_URI_LIST)
1047 {
1048 hClip = hClip = GetClipboardData(CF_HDROP);
1049 if (hClip)
1050 {
1051 HDROP hDrop = (HDROP)GlobalLock(hClip);
1052 if (hDrop)
1053 {
1054 char *pszList = NULL;
1055 uint32_t cbList;
1056 rc = SharedClipboardWinTransferDropFilesToStringList((DROPFILES *)hDrop, &pszList, &cbList);
1057
1058 GlobalUnlock(hClip);
1059
1060 if (RT_SUCCESS(rc))
1061 {
1062 if (cbList <= cbData)
1063 {
1064 memcpy(pvData, pszList, cbList);
1065 *pcbActual = cbList;
1066 }
1067
1068 RTStrFree(pszList);
1069 }
1070 }
1071 else
1072 LogRel(("Shared Clipboard: Unable to lock clipboard data, last error: %ld\n", GetLastError()));
1073 }
1074 else
1075 LogRel(("Shared Clipboard: Unable to retrieve clipboard data from clipboard (CF_HDROP), last error: %ld\n",
1076 GetLastError()));
1077 }
1078#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
1079 SharedClipboardWinClose();
1080 }
1081
1082 if (RT_FAILURE(rc))
1083 LogRel(("Shared Clipboard: Error reading host clipboard data in format %#x from Windows, rc=%Rrc\n", uFmt, rc));
1084
1085 LogFlowFuncLeaveRC(rc);
1086 return rc;
1087}
1088
1089int ShClBackendWriteData(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLCLIENTCMDCTX pCmdCtx,
1090 SHCLFORMAT uFormat, void *pvData, uint32_t cbData)
1091{
1092 RT_NOREF(pBackend, pClient, pCmdCtx, uFormat, pvData, cbData);
1093
1094 LogFlowFuncEnter();
1095
1096 /* Nothing to do here yet. */
1097
1098 LogFlowFuncLeave();
1099 return VINF_SUCCESS;
1100}
1101
1102#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
1103/**
1104 * Handles transfer status replies from the guest.
1105 */
1106int ShClBackendTransferHandleStatusReply(PSHCLBACKEND pBackend, PSHCLCLIENT pClient, PSHCLTRANSFER pTransfer, SHCLSOURCE enmSource, SHCLTRANSFERSTATUS enmStatus, int rcStatus)
1107{
1108 RT_NOREF(pBackend, pClient, pTransfer, enmSource, enmStatus, rcStatus);
1109
1110 return VINF_SUCCESS;
1111}
1112
1113
1114/*********************************************************************************************************************************
1115* Provider interface implementation *
1116*********************************************************************************************************************************/
1117
1118/** @copydoc SHCLTXPROVIDERIFACE::pfnRootListRead */
1119static DECLCALLBACK(int) shClSvcWinTransferIfaceHGRootListRead(PSHCLTXPROVIDERCTX pCtx)
1120{
1121 LogFlowFuncEnter();
1122
1123 PSHCLCLIENT pClient = (PSHCLCLIENT)pCtx->pvUser;
1124 AssertPtr(pClient);
1125
1126 AssertPtr(pClient->State.pCtx);
1127 PSHCLWINCTX pWin = &pClient->State.pCtx->Win;
1128
1129 int rc = SharedClipboardWinTransferGetRootsFromClipboard(pWin, pCtx->pTransfer);
1130
1131 LogFlowFuncLeaveRC(rc);
1132 return rc;
1133}
1134#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
1135
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