VirtualBox

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

Last change on this file since 83487 was 82893, checked in by vboxsync, 5 years ago

Shared Clipboard: Dealt with event retention for ShClSvcDataReadRequest(). bugref:9437

  • 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 82893 2020-01-28 16:53:51Z vboxsync $ */
2/** @file
3 * Shared Clipboard Service - Win32 host.
4 */
5
6/*
7 * Copyright (C) 2006-2020 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_TRANSFERS
29# include <VBox/GuestHost/SharedClipboard-transfers.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_TRANSFERS
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_TRANSFERS
48# include "VBoxSharedClipboardSvc-transfers.h"
49#endif
50
51
52/*********************************************************************************************************************************
53* Internal Functions *
54*********************************************************************************************************************************/
55static int vboxClipboardSvcWinSyncInternal(PSHCLCONTEXT pCtx);
56
57struct SHCLCONTEXT
58{
59 /** Handle for window message handling thread. */
60 RTTHREAD hThread;
61 /** Structure for keeping and communicating with service client. */
62 PSHCLCLIENT pClient;
63 /** Windows-specific context data. */
64 SHCLWINCTX Win;
65};
66
67
68/** @todo Someone please explain the protocol wrt overflows... */
69static void vboxClipboardSvcWinGetData(uint32_t u32Format, const void *pvSrc, uint32_t cbSrc,
70 void *pvDst, uint32_t cbDst, uint32_t *pcbActualDst)
71{
72 LogFlowFunc(("cbSrc = %d, cbDst = %d\n", cbSrc, cbDst));
73
74 if ( u32Format == VBOX_SHCL_FMT_HTML
75 && SharedClipboardWinIsCFHTML((const char *)pvSrc))
76 {
77 /** @todo r=bird: Why the double conversion? */
78 char *pszBuf = NULL;
79 uint32_t cbBuf = 0;
80 int rc = SharedClipboardWinConvertCFHTMLToMIME((const char *)pvSrc, cbSrc, &pszBuf, &cbBuf);
81 if (RT_SUCCESS(rc))
82 {
83 *pcbActualDst = cbBuf;
84 if (cbBuf > cbDst)
85 {
86 /* Do not copy data. The dst buffer is not enough. */
87 RTMemFree(pszBuf);
88 return;
89 }
90 memcpy(pvDst, pszBuf, cbBuf);
91 RTMemFree(pszBuf);
92 }
93 else
94 *pcbActualDst = 0;
95 }
96 else
97 {
98 *pcbActualDst = cbSrc;
99
100 if (cbSrc > cbDst)
101 {
102 /* Do not copy data. The dst buffer is not enough. */
103 return;
104 }
105
106 memcpy(pvDst, pvSrc, cbSrc);
107 }
108
109#ifdef LOG_ENABLED
110 ShClDbgDumpData(pvDst, cbSrc, u32Format);
111#endif
112
113 return;
114}
115
116static int vboxClipboardSvcWinDataSet(PSHCLCONTEXT pCtx, UINT cfFormat, void *pvData, uint32_t cbData)
117{
118 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
119 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
120 AssertReturn (cbData, VERR_INVALID_PARAMETER);
121
122 int rc = VINF_SUCCESS;
123
124 HANDLE hMem = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, cbData);
125
126 LogFlowFunc(("hMem=%p\n", hMem));
127
128 if (hMem)
129 {
130 void *pMem = GlobalLock(hMem);
131
132 LogFlowFunc(("pMem=%p, GlobalSize=%zu\n", pMem, GlobalSize(hMem)));
133
134 if (pMem)
135 {
136 LogFlowFunc(("Setting data\n"));
137
138 memcpy(pMem, pvData, cbData);
139
140 /* The memory must be unlocked before inserting to the Clipboard. */
141 GlobalUnlock(hMem);
142
143 /* 'hMem' contains the host clipboard data.
144 * size is 'cb' and format is 'format'.
145 */
146 HANDLE hClip = SetClipboardData(cfFormat, hMem);
147
148 LogFlowFunc(("hClip=%p\n", hClip));
149
150 if (hClip)
151 {
152 /* The hMem ownership has gone to the system. Nothing to do. */
153 }
154 else
155 rc = RTErrConvertFromWin32(GetLastError());
156 }
157 else
158 rc = VERR_ACCESS_DENIED;
159
160 GlobalFree(hMem);
161 }
162 else
163 rc = RTErrConvertFromWin32(GetLastError());
164
165 LogFlowFuncLeaveRC(rc);
166 return rc;
167}
168
169static int vboxClipboardSvcWinDataRead(PSHCLCONTEXT pCtx, UINT uFormat, void **ppvData, uint32_t *pcbData)
170{
171 SHCLFORMAT fFormat = SharedClipboardWinClipboardFormatToVBox(uFormat);
172 LogFlowFunc(("uFormat=%u -> uFmt=0x%x\n", uFormat, fFormat));
173
174 if (fFormat == VBOX_SHCL_FMT_NONE)
175 {
176 LogRel2(("Shared Clipbaord: Windows format %u not supported, ingoring\n", uFormat));
177 return VERR_NOT_SUPPORTED;
178 }
179
180 SHCLEVENTID idEvent = 0;
181 int rc = ShClSvcDataReadRequest(pCtx->pClient, fFormat, &idEvent);
182 if (RT_SUCCESS(rc))
183 {
184 PSHCLEVENTPAYLOAD pPayload;
185 rc = ShClEventWait(&pCtx->pClient->EventSrc, idEvent, 30 * 1000, &pPayload);
186 if (RT_SUCCESS(rc))
187 {
188 *ppvData = pPayload ? pPayload->pvData : NULL;
189 *pcbData = pPayload ? pPayload->cbData : 0;
190 }
191
192 ShClEventRelease(&pCtx->pClient->EventSrc, idEvent);
193 ShClEventUnregister(&pCtx->pClient->EventSrc, idEvent);
194 }
195
196 LogFlowFuncLeaveRC(rc);
197 return rc;
198}
199
200static LRESULT CALLBACK vboxClipboardSvcWinWndProcMain(PSHCLCONTEXT pCtx,
201 HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
202{
203 AssertPtr(pCtx);
204
205 LRESULT lresultRc = 0;
206
207 const PSHCLWINCTX pWinCtx = &pCtx->Win;
208
209 switch (uMsg)
210 {
211 case WM_CLIPBOARDUPDATE:
212 {
213 LogFunc(("WM_CLIPBOARDUPDATE\n"));
214
215 int rc = RTCritSectEnter(&pWinCtx->CritSect);
216 if (RT_SUCCESS(rc))
217 {
218 const HWND hWndClipboardOwner = GetClipboardOwner();
219
220 LogFunc(("WM_CLIPBOARDUPDATE: hWndClipboardOwnerUs=%p, hWndNewClipboardOwner=%p\n",
221 pWinCtx->hWndClipboardOwnerUs, hWndClipboardOwner));
222
223 if (pWinCtx->hWndClipboardOwnerUs != hWndClipboardOwner)
224 {
225 int rc2 = RTCritSectLeave(&pWinCtx->CritSect);
226 AssertRC(rc2);
227
228 /* Clipboard was updated by another application, retrieve formats and report back. */
229 rc = vboxClipboardSvcWinSyncInternal(pCtx);
230 }
231 else
232 {
233 int rc2 = RTCritSectLeave(&pWinCtx->CritSect);
234 AssertRC(rc2);
235 }
236 }
237
238 if (RT_FAILURE(rc))
239 LogRel(("Shared Clipboard: WM_CLIPBOARDUPDATE failed with %Rrc\n", rc));
240
241 break;
242 }
243
244 case WM_CHANGECBCHAIN:
245 {
246 LogFunc(("WM_CHANGECBCHAIN\n"));
247 lresultRc = SharedClipboardWinHandleWMChangeCBChain(pWinCtx, hWnd, uMsg, wParam, lParam);
248 break;
249 }
250
251 case WM_DRAWCLIPBOARD:
252 {
253 LogFunc(("WM_DRAWCLIPBOARD\n"));
254
255 int rc = RTCritSectEnter(&pWinCtx->CritSect);
256 if (RT_SUCCESS(rc))
257 {
258 const HWND hWndClipboardOwner = GetClipboardOwner();
259
260 LogFunc(("WM_DRAWCLIPBOARD: hWndClipboardOwnerUs=%p, hWndNewClipboardOwner=%p\n",
261 pWinCtx->hWndClipboardOwnerUs, hWndClipboardOwner));
262
263 if (pWinCtx->hWndClipboardOwnerUs != hWndClipboardOwner)
264 {
265 int rc2 = RTCritSectLeave(&pWinCtx->CritSect);
266 AssertRC(rc2);
267
268 /* Clipboard was updated by another application, retrieve formats and report back. */
269 rc = vboxClipboardSvcWinSyncInternal(pCtx);
270 }
271 else
272 {
273 int rc2 = RTCritSectLeave(&pWinCtx->CritSect);
274 AssertRC(rc2);
275 }
276 }
277
278 lresultRc = SharedClipboardWinChainPassToNext(pWinCtx, uMsg, wParam, lParam);
279 break;
280 }
281
282 case WM_TIMER:
283 {
284 int rc = SharedClipboardWinHandleWMTimer(pWinCtx);
285 AssertRC(rc);
286
287 break;
288 }
289
290 case WM_RENDERFORMAT:
291 {
292 LogFunc(("WM_RENDERFORMAT\n"));
293
294 /* Insert the requested clipboard format data into the clipboard. */
295 const UINT uFormat = (UINT)wParam;
296 const SHCLFORMAT fFormat = SharedClipboardWinClipboardFormatToVBox(uFormat);
297 LogFunc(("WM_RENDERFORMAT: uFormat=%u -> fFormat=0x%x\n", uFormat, fFormat));
298
299 if ( fFormat == VBOX_SHCL_FMT_NONE
300 || pCtx->pClient == NULL)
301 {
302 /* Unsupported clipboard format is requested. */
303 LogFunc(("WM_RENDERFORMAT unsupported format requested or client is not active\n"));
304 SharedClipboardWinClear();
305 }
306 else
307 {
308 void *pvData = NULL;
309 uint32_t cbData = 0;
310 int rc = vboxClipboardSvcWinDataRead(pCtx, uFormat, &pvData, &cbData);
311 if ( RT_SUCCESS(rc)
312 && pvData
313 && cbData)
314 {
315 rc = vboxClipboardSvcWinDataSet(pCtx, uFormat, pvData, cbData);
316
317 RTMemFree(pvData);
318 cbData = 0;
319 }
320
321 if (RT_FAILURE(rc))
322 SharedClipboardWinClear();
323 }
324
325 break;
326 }
327
328 case WM_RENDERALLFORMATS:
329 {
330 LogFunc(("WM_RENDERALLFORMATS\n"));
331
332 int rc = SharedClipboardWinHandleWMRenderAllFormats(pWinCtx, hWnd);
333 AssertRC(rc);
334
335 break;
336 }
337
338 case SHCL_WIN_WM_REPORT_FORMATS:
339 {
340 /* Announce available formats. Do not insert data -- will be inserted in WM_RENDERFORMAT (or via IDataObject). */
341 SHCLFORMATS fFormats = (uint32_t)lParam;
342 LogFunc(("SHCL_WIN_WM_REPORT_FORMATS: fFormats=0x%x\n", fFormats));
343
344#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
345 if (fFormats & VBOX_SHCL_FMT_URI_LIST)
346 {
347 PSHCLTRANSFER pTransfer;
348 int rc = shClSvcTransferStart(pCtx->pClient,
349 SHCLTRANSFERDIR_FROM_REMOTE, SHCLSOURCE_REMOTE,
350 &pTransfer);
351 if (RT_SUCCESS(rc))
352 {
353 /* Create the IDataObject implementation the host OS needs and assign
354 * the newly created transfer to this object. */
355 rc = SharedClipboardWinTransferCreate(&pCtx->Win, pTransfer);
356
357 /* Note: The actual requesting + retrieving of data will be done in the IDataObject implementation
358 (ClipboardDataObjectImpl::GetData()). */
359 }
360 else
361 LogRel(("Shared Clipboard: Initializing read transfer failed with %Rrc\n", rc));
362 }
363 else
364 {
365#endif
366 int rc = SharedClipboardWinOpen(hWnd);
367 if (RT_SUCCESS(rc))
368 {
369 SharedClipboardWinClear();
370
371 rc = SharedClipboardWinAnnounceFormats(pWinCtx, fFormats);
372
373 SharedClipboardWinClose();
374 }
375#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
376 }
377#endif
378 LogFunc(("SHCL_WIN_WM_REPORT_FORMATS: lastErr=%ld\n", GetLastError()));
379 break;
380 }
381
382 case WM_DESTROY:
383 {
384 LogFunc(("WM_DESTROY\n"));
385
386 int rc = SharedClipboardWinHandleWMDestroy(pWinCtx);
387 AssertRC(rc);
388
389 PostQuitMessage(0);
390 break;
391 }
392
393 default:
394 break;
395 }
396
397 LogFlowFunc(("LEAVE hWnd=%p, WM_ %u\n", hWnd, uMsg));
398 return DefWindowProc(hWnd, uMsg, wParam, lParam);
399}
400
401/**
402 * Static helper function for having a per-client proxy window instances.
403 */
404static LRESULT CALLBACK vboxClipboardSvcWinWndProcInstance(HWND hWnd, UINT uMsg,
405 WPARAM wParam, LPARAM lParam)
406{
407 LONG_PTR pUserData = GetWindowLongPtr(hWnd, GWLP_USERDATA);
408 AssertPtrReturn(pUserData, 0);
409
410 PSHCLCONTEXT pCtx = reinterpret_cast<PSHCLCONTEXT>(pUserData);
411 if (pCtx)
412 return vboxClipboardSvcWinWndProcMain(pCtx, hWnd, uMsg, wParam, lParam);
413
414 return 0;
415}
416
417/**
418 * Static helper function for routing Windows messages to a specific
419 * proxy window instance.
420 */
421static LRESULT CALLBACK vboxClipboardSvcWinWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
422{
423 /* Note: WM_NCCREATE is not the first ever message which arrives, but
424 * early enough for us. */
425 if (uMsg == WM_NCCREATE)
426 {
427 LogFlowFunc(("WM_NCCREATE\n"));
428
429 LPCREATESTRUCT pCS = (LPCREATESTRUCT)lParam;
430 AssertPtr(pCS);
431 SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)pCS->lpCreateParams);
432 SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)vboxClipboardSvcWinWndProcInstance);
433
434 return vboxClipboardSvcWinWndProcInstance(hWnd, uMsg, wParam, lParam);
435 }
436
437 /* No window associated yet. */
438 return DefWindowProc(hWnd, uMsg, wParam, lParam);
439}
440
441DECLCALLBACK(int) vboxClipboardSvcWinThread(RTTHREAD hThreadSelf, void *pvUser)
442{
443 LogFlowFuncEnter();
444
445 bool fThreadSignalled = false;
446
447 const PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pvUser;
448 AssertPtr(pCtx);
449 const PSHCLWINCTX pWinCtx = &pCtx->Win;
450
451 HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
452
453 /* Register the Window Class. */
454 WNDCLASS wc;
455 RT_ZERO(wc);
456
457 wc.style = CS_NOCLOSE;
458 wc.lpfnWndProc = vboxClipboardSvcWinWndProc;
459 wc.hInstance = hInstance;
460 wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
461
462 /* Register an unique wnd class name. */
463 char szWndClassName[32];
464 RTStrPrintf2(szWndClassName, sizeof(szWndClassName),
465 "%s-%RU64", SHCL_WIN_WNDCLASS_NAME, RTThreadGetNative(hThreadSelf));
466 wc.lpszClassName = szWndClassName;
467
468 int rc;
469
470 ATOM atomWindowClass = RegisterClass(&wc);
471 if (atomWindowClass == 0)
472 {
473 LogFunc(("Failed to register window class\n"));
474 rc = VERR_NOT_SUPPORTED;
475 }
476 else
477 {
478 /* Create a window and make it a clipboard viewer. */
479 pWinCtx->hWnd = CreateWindowEx(WS_EX_TOOLWINDOW | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
480 szWndClassName, szWndClassName,
481 WS_POPUPWINDOW,
482 -200, -200, 100, 100, NULL, NULL, hInstance, pCtx /* lpParam */);
483 if (pWinCtx->hWnd == NULL)
484 {
485 LogFunc(("Failed to create window\n"));
486 rc = VERR_NOT_SUPPORTED;
487 }
488 else
489 {
490 SetWindowPos(pWinCtx->hWnd, HWND_TOPMOST, -200, -200, 0, 0,
491 SWP_NOACTIVATE | SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_NOSIZE);
492
493 rc = SharedClipboardWinChainAdd(&pCtx->Win);
494 if (RT_SUCCESS(rc))
495 {
496 if (!SharedClipboardWinIsNewAPI(&pWinCtx->newAPI))
497 pWinCtx->oldAPI.timerRefresh = SetTimer(pWinCtx->hWnd, 0, 10 * 1000, NULL);
498 }
499
500#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
501 if (RT_SUCCESS(rc))
502 {
503 HRESULT hr = OleInitialize(NULL);
504 if (FAILED(hr))
505 {
506 LogRel(("Shared Clipboard: Initializing window thread OLE failed (%Rhrc) -- file transfers unavailable\n", hr));
507 /* Not critical, the rest of the clipboard might work. */
508 }
509 else
510 LogRel(("Shared Clipboard: Initialized window thread OLE\n"));
511 }
512#endif
513 int rc2 = RTThreadUserSignal(hThreadSelf);
514 AssertRC(rc2);
515
516 fThreadSignalled = true;
517
518 MSG msg;
519 BOOL msgret = 0;
520 while ((msgret = GetMessage(&msg, NULL, 0, 0)) > 0)
521 {
522 TranslateMessage(&msg);
523 DispatchMessage(&msg);
524 }
525
526 /*
527 * Window procedure can return error, * but this is exceptional situation that should be
528 * identified in testing.
529 */
530 Assert(msgret >= 0);
531 LogFunc(("Message loop finished. GetMessage returned %d, message id: %d \n", msgret, msg.message));
532
533#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
534 OleSetClipboard(NULL); /* Make sure to flush the clipboard on destruction. */
535 OleUninitialize();
536#endif
537 }
538 }
539
540 pWinCtx->hWnd = NULL;
541
542 if (atomWindowClass != 0)
543 {
544 UnregisterClass(szWndClassName, hInstance);
545 atomWindowClass = 0;
546 }
547
548 if (!fThreadSignalled)
549 {
550 int rc2 = RTThreadUserSignal(hThreadSelf);
551 AssertRC(rc2);
552 }
553
554 LogFlowFuncLeaveRC(rc);
555 return rc;
556}
557
558/**
559 * Synchronizes the host and the guest clipboard formats by sending all supported host clipboard
560 * formats to the guest.
561 *
562 * @returns VBox status code, VINF_NO_CHANGE if no synchronization was required.
563 * @param pCtx Clipboard context to synchronize.
564 */
565static int vboxClipboardSvcWinSyncInternal(PSHCLCONTEXT pCtx)
566{
567 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
568
569 LogFlowFuncEnter();
570
571 int rc;
572
573 if (pCtx->pClient)
574 {
575 SHCLFORMATDATA Formats;
576 RT_ZERO(Formats);
577
578 rc = SharedClipboardWinGetFormats(&pCtx->Win, &Formats);
579 if ( RT_SUCCESS(rc)
580 && Formats.Formats != VBOX_SHCL_FMT_NONE) /** @todo r=bird: BUGBUG: revisit this. */
581 rc = ShClSvcHostReportFormats(pCtx->pClient, Formats.Formats);
582 }
583 else /* If we don't have any client data (yet), bail out. */
584 rc = VINF_NO_CHANGE;
585
586 LogFlowFuncLeaveRC(rc);
587 return rc;
588}
589
590/*
591 * Public platform dependent functions.
592 */
593
594int ShClSvcImplInit(void)
595{
596#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
597 HRESULT hr = OleInitialize(NULL);
598 if (FAILED(hr))
599 {
600 LogRel(("Shared Clipboard: Initializing OLE failed (%Rhrc) -- file transfers unavailable\n", hr));
601 /* Not critical, the rest of the clipboard might work. */
602 }
603 else
604 LogRel(("Shared Clipboard: Initialized OLE\n"));
605#endif
606
607 return VINF_SUCCESS;
608}
609
610void ShClSvcImplDestroy(void)
611{
612#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
613 OleSetClipboard(NULL); /* Make sure to flush the clipboard on destruction. */
614 OleUninitialize();
615#endif
616}
617
618int ShClSvcImplConnect(PSHCLCLIENT pClient, bool fHeadless)
619{
620 RT_NOREF(fHeadless);
621
622 LogFlowFuncEnter();
623
624 int rc;
625
626 PSHCLCONTEXT pCtx = (PSHCLCONTEXT)RTMemAllocZ(sizeof(SHCLCONTEXT));
627 if (pCtx)
628 {
629 rc = SharedClipboardWinCtxInit(&pCtx->Win);
630 if (RT_SUCCESS(rc))
631 {
632 rc = RTThreadCreate(&pCtx->hThread, vboxClipboardSvcWinThread, pCtx /* pvUser */, _64K /* Stack size */,
633 RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "SHCLIP");
634 if (RT_SUCCESS(rc))
635 {
636 int rc2 = RTThreadUserWait(pCtx->hThread, 30 * 1000 /* Timeout in ms */);
637 AssertRC(rc2);
638 }
639 }
640
641 pClient->State.pCtx = pCtx;
642 pClient->State.pCtx->pClient = pClient;
643 }
644 else
645 rc = VERR_NO_MEMORY;
646
647 LogFlowFuncLeaveRC(rc);
648 return rc;
649}
650
651int ShClSvcImplSync(PSHCLCLIENT pClient)
652{
653 /* Sync the host clipboard content with the client. */
654 return vboxClipboardSvcWinSyncInternal(pClient->State.pCtx);
655}
656
657int ShClSvcImplDisconnect(PSHCLCLIENT pClient)
658{
659 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
660
661 LogFlowFuncEnter();
662
663 int rc = VINF_SUCCESS;
664
665 PSHCLCONTEXT pCtx = pClient->State.pCtx;
666 if (pCtx)
667 {
668 if (pCtx->Win.hWnd)
669 PostMessage(pCtx->Win.hWnd, WM_DESTROY, 0 /* wParam */, 0 /* lParam */);
670
671 if (pCtx->hThread != NIL_RTTHREAD)
672 {
673 LogFunc(("Waiting for thread to terminate ...\n"));
674
675 /* Wait for the window thread to terminate. */
676 rc = RTThreadWait(pCtx->hThread, 30 * 1000 /* Timeout in ms */, NULL);
677 if (RT_FAILURE(rc))
678 LogRel(("Shared Clipboard: Waiting for window thread termination failed with rc=%Rrc\n", rc));
679
680 pCtx->hThread = NIL_RTTHREAD;
681 }
682
683 SharedClipboardWinCtxDestroy(&pCtx->Win);
684
685 if (RT_SUCCESS(rc))
686 {
687 RTMemFree(pCtx);
688 pCtx = NULL;
689
690 pClient->State.pCtx = NULL;
691 }
692 }
693
694 LogFlowFuncLeaveRC(rc);
695 return rc;
696}
697
698int ShClSvcImplFormatAnnounce(PSHCLCLIENT pClient, PSHCLCLIENTCMDCTX pCmdCtx,
699 PSHCLFORMATDATA pFormats)
700{
701 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
702 RT_NOREF(pCmdCtx);
703
704 int rc;
705
706 PSHCLCONTEXT pCtx = pClient->State.pCtx;
707 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
708
709 LogFlowFunc(("uFormats=0x%x, hWnd=%p\n", pFormats->Formats, pCtx->Win.hWnd));
710
711 /*
712 * The guest announced formats. Forward to the window thread.
713 */
714 PostMessage(pCtx->Win.hWnd, SHCL_WIN_WM_REPORT_FORMATS,
715 0 /* wParam */, pFormats->Formats /* lParam */);
716
717 rc = VINF_SUCCESS;
718
719 LogFlowFuncLeaveRC(rc);
720 return rc;
721}
722
723int ShClSvcImplReadData(PSHCLCLIENT pClient, PSHCLCLIENTCMDCTX pCmdCtx,
724 SHCLFORMAT uFormat, void *pvData, uint32_t cbData, uint32_t *pcbActual)
725{
726 AssertPtrReturn(pClient, VERR_INVALID_POINTER);
727 RT_NOREF(pCmdCtx);
728 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
729 AssertPtrReturn(pClient->State.pCtx, VERR_INVALID_POINTER);
730
731 LogFlowFunc(("uFormat=%02X\n", uFormat));
732
733 HANDLE hClip = NULL;
734
735 const PSHCLWINCTX pWinCtx = &pClient->State.pCtx->Win;
736
737 /*
738 * The guest wants to read data in the given format.
739 */
740 int rc = SharedClipboardWinOpen(pWinCtx->hWnd);
741 if (RT_SUCCESS(rc))
742 {
743 LogFunc(("Clipboard opened\n"));
744
745 if (uFormat & VBOX_SHCL_FMT_BITMAP)
746 {
747 hClip = GetClipboardData(CF_DIB);
748 if (hClip != NULL)
749 {
750 LPVOID lp = GlobalLock(hClip);
751
752 if (lp != NULL)
753 {
754 LogFunc(("CF_DIB\n"));
755
756 vboxClipboardSvcWinGetData(VBOX_SHCL_FMT_BITMAP, lp, GlobalSize(hClip),
757 pvData, cbData, pcbActual);
758
759 GlobalUnlock(hClip);
760 }
761 else
762 {
763 hClip = NULL;
764 }
765 }
766 }
767 else if (uFormat & VBOX_SHCL_FMT_UNICODETEXT)
768 {
769 hClip = GetClipboardData(CF_UNICODETEXT);
770 if (hClip != NULL)
771 {
772 LPWSTR uniString = (LPWSTR)GlobalLock(hClip);
773
774 if (uniString != NULL)
775 {
776 LogFunc(("CF_UNICODETEXT\n"));
777
778 vboxClipboardSvcWinGetData(VBOX_SHCL_FMT_UNICODETEXT, uniString, (lstrlenW(uniString) + 1) * 2,
779 pvData, cbData, pcbActual);
780
781 GlobalUnlock(hClip);
782 }
783 else
784 {
785 hClip = NULL;
786 }
787 }
788 }
789 else if (uFormat & VBOX_SHCL_FMT_HTML)
790 {
791 UINT format = RegisterClipboardFormat(SHCL_WIN_REGFMT_HTML);
792 if (format != 0)
793 {
794 hClip = GetClipboardData(format);
795 if (hClip != NULL)
796 {
797 LPVOID lp = GlobalLock(hClip);
798 if (lp != NULL)
799 {
800 /** @todo r=andy Add data overflow handling. */
801 vboxClipboardSvcWinGetData(VBOX_SHCL_FMT_HTML, lp, GlobalSize(hClip),
802 pvData, cbData, pcbActual);
803#ifdef VBOX_STRICT
804 LogFlowFunc(("Raw HTML clipboard data from host:"));
805 ShClDbgDumpHtml((char *)pvData, cbData);
806#endif
807 GlobalUnlock(hClip);
808 }
809 else
810 {
811 hClip = NULL;
812 }
813 }
814 }
815 }
816#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
817 else if (uFormat & VBOX_SHCL_FMT_URI_LIST)
818 {
819 AssertFailed(); /** @todo */
820 }
821#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
822 SharedClipboardWinClose();
823 }
824
825 if (hClip == NULL)
826 {
827 /* Reply with empty data. */
828 vboxClipboardSvcWinGetData(0, NULL, 0, pvData, cbData, pcbActual);
829 }
830
831 LogFlowFuncLeaveRC(rc);
832 return rc;
833}
834
835int ShClSvcImplWriteData(PSHCLCLIENT pClient, PSHCLCLIENTCMDCTX pCmdCtx,
836 SHCLFORMAT uFormat, void *pvData, uint32_t cbData)
837{
838 LogFlowFuncEnter();
839
840 int rc = ShClSvcDataReadSignal(pClient, pCmdCtx, uFormat, pvData, cbData);
841
842 LogFlowFuncLeaveRC(rc);
843 return rc;
844}
845
846#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
847int ShClSvcImplTransferCreate(PSHCLCLIENT pClient, PSHCLTRANSFER pTransfer)
848{
849 RT_NOREF(pClient, pTransfer);
850
851 LogFlowFuncEnter();
852
853 return VINF_SUCCESS;
854}
855
856int ShClSvcImplTransferDestroy(PSHCLCLIENT pClient, PSHCLTRANSFER pTransfer)
857{
858 LogFlowFuncEnter();
859
860 SharedClipboardWinTransferDestroy(&pClient->State.pCtx->Win, pTransfer);
861
862 return VINF_SUCCESS;
863}
864
865int ShClSvcImplTransferGetRoots(PSHCLCLIENT pClient, PSHCLTRANSFER pTransfer)
866{
867 LogFlowFuncEnter();
868
869 const PSHCLWINCTX pWinCtx = &pClient->State.pCtx->Win;
870
871 int rc = SharedClipboardWinGetRoots(pWinCtx, pTransfer);
872
873 LogFlowFuncLeaveRC(rc);
874 return rc;
875}
876#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
877
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