VirtualBox

source: vbox/trunk/include/VBox/GuestHost/SharedClipboard-win.h@ 97039

Last change on this file since 97039 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.9 KB
Line 
1/** @file
2 * Shared Clipboard - Common Guest and Host Code, for Windows OSes.
3 */
4
5/*
6 * Copyright (C) 2006-2022 Oracle and/or its affiliates.
7 *
8 * This file is part of VirtualBox base platform packages, as
9 * available from https://www.virtualbox.org.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation, in version 3 of the
14 * License.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <https://www.gnu.org/licenses>.
23 *
24 * The contents of this file may alternatively be used under the terms
25 * of the Common Development and Distribution License Version 1.0
26 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
27 * in the VirtualBox distribution, in which case the provisions of the
28 * CDDL are applicable instead of those of the GPL.
29 *
30 * You may elect to license modified versions of this file under the
31 * terms and conditions of either the GPL or the CDDL or both.
32 *
33 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
34 */
35
36#ifndef VBOX_INCLUDED_GuestHost_SharedClipboard_win_h
37#define VBOX_INCLUDED_GuestHost_SharedClipboard_win_h
38#ifndef RT_WITHOUT_PRAGMA_ONCE
39# pragma once
40#endif
41
42#include <iprt/critsect.h>
43#include <iprt/types.h>
44#include <iprt/win/windows.h>
45
46#include <VBox/GuestHost/SharedClipboard.h>
47
48# ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
49# include <vector>
50
51# include <iprt/cpp/ministring.h> /* For RTCString. */
52# include <iprt/win/shlobj.h> /* For DROPFILES and friends. */
53# include <VBox/com/string.h> /* For Utf8Str. */
54# include <oleidl.h>
55
56# include <VBox/GuestHost/SharedClipboard-transfers.h>
57
58using namespace com;
59# endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
60
61#ifndef WM_CLIPBOARDUPDATE
62# define WM_CLIPBOARDUPDATE 0x031D
63#endif
64
65#define SHCL_WIN_WNDCLASS_NAME "VBoxSharedClipboardClass"
66
67/** See: https://docs.microsoft.com/en-us/windows/desktop/dataxchg/html-clipboard-format
68 * Do *not* change the name, as this will break compatbility with other (legacy) applications! */
69#define SHCL_WIN_REGFMT_HTML "HTML Format"
70
71/** Default timeout (in ms) for passing down messages down the clipboard chain. */
72#define SHCL_WIN_CBCHAIN_TIMEOUT_MS 5000
73
74/** Reports clipboard formats. */
75#define SHCL_WIN_WM_REPORT_FORMATS WM_USER
76/** Reads data from the clipboard and sends it to the destination. */
77#define SHCL_WIN_WM_READ_DATA WM_USER + 1
78#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
79/** Starts a transfer on the guest.
80 * This creates the necessary IDataObject in the matching window thread. */
81# define SHCL_WIN_WM_TRANSFER_START WM_USER + 2
82#endif
83
84/* Dynamically load clipboard functions from User32.dll. */
85typedef BOOL WINAPI FNADDCLIPBOARDFORMATLISTENER(HWND);
86typedef FNADDCLIPBOARDFORMATLISTENER *PFNADDCLIPBOARDFORMATLISTENER;
87
88typedef BOOL WINAPI FNREMOVECLIPBOARDFORMATLISTENER(HWND);
89typedef FNREMOVECLIPBOARDFORMATLISTENER *PFNREMOVECLIPBOARDFORMATLISTENER;
90
91/**
92 * Structure for keeping function pointers for the new clipboard API.
93 * If the new API is not available, those function pointer are NULL.
94 */
95typedef struct _SHCLWINAPINEW
96{
97 PFNADDCLIPBOARDFORMATLISTENER pfnAddClipboardFormatListener;
98 PFNREMOVECLIPBOARDFORMATLISTENER pfnRemoveClipboardFormatListener;
99} SHCLWINAPINEW, *PSHCLWINAPINEW;
100
101/**
102 * Structure for keeping variables which are needed to drive the old clipboard API.
103 */
104typedef struct _SHCLWINAPIOLD
105{
106 /** Timer ID for the refresh timer. */
107 UINT timerRefresh;
108 /** Whether "pinging" the clipboard chain currently is in progress or not. */
109 bool fCBChainPingInProcess;
110} SHCLWINAPIOLD, *PSHCLWINAPIOLD;
111
112/**
113 * Structure for maintaining a Shared Clipboard context on Windows platforms.
114 */
115typedef struct _SHCLWINCTX
116{
117 /** Critical section to serialize access. */
118 RTCRITSECT CritSect;
119 /** Window handle of our (invisible) clipbaord window. */
120 HWND hWnd;
121 /** Window handle which is next to us in the clipboard chain. */
122 HWND hWndNextInChain;
123 /** Window handle of the clipboard owner *if* we are the owner.
124 * @todo r=bird: Ignore the misleading statement above. This is only set to
125 * NULL by the initialization code and then it's set to the clipboard owner
126 * after we announce data to the clipboard. So, essentially this will be our
127 * windows handle or NULL. End of story. */
128 HWND hWndClipboardOwnerUs;
129 /** Structure for maintaining the new clipboard API. */
130 SHCLWINAPINEW newAPI;
131 /** Structure for maintaining the old clipboard API. */
132 SHCLWINAPIOLD oldAPI;
133} SHCLWINCTX, *PSHCLWINCTX;
134
135int SharedClipboardWinOpen(HWND hWnd);
136int SharedClipboardWinClose(void);
137int SharedClipboardWinClear(void);
138
139int SharedClipboardWinCtxInit(PSHCLWINCTX pWinCtx);
140void SharedClipboardWinCtxDestroy(PSHCLWINCTX pWinCtx);
141
142int SharedClipboardWinCheckAndInitNewAPI(PSHCLWINAPINEW pAPI);
143bool SharedClipboardWinIsNewAPI(PSHCLWINAPINEW pAPI);
144
145int SharedClipboardWinChainAdd(PSHCLWINCTX pCtx);
146int SharedClipboardWinChainRemove(PSHCLWINCTX pCtx);
147VOID CALLBACK SharedClipboardWinChainPingProc(HWND hWnd, UINT uMsg, ULONG_PTR dwData, LRESULT lResult) RT_NOTHROW_DEF;
148LRESULT SharedClipboardWinChainPassToNext(PSHCLWINCTX pWinCtx, UINT msg, WPARAM wParam, LPARAM lParam);
149
150SHCLFORMAT SharedClipboardWinClipboardFormatToVBox(UINT uFormat);
151int SharedClipboardWinGetFormats(PSHCLWINCTX pCtx, PSHCLFORMATS pfFormats);
152
153#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
154int SharedClipboardWinGetRoots(PSHCLWINCTX pWinCtx, PSHCLTRANSFER pTransfer);
155int SharedClipboardWinDropFilesToStringList(DROPFILES *pDropFiles, char **papszList, uint32_t *pcbList);
156#endif
157
158int SharedClipboardWinGetCFHTMLHeaderValue(const char *pszSrc, const char *pszOption, uint32_t *puValue);
159bool SharedClipboardWinIsCFHTML(const char *pszSource);
160int SharedClipboardWinConvertCFHTMLToMIME(const char *pszSource, const uint32_t cch, char **ppszOutput, uint32_t *pcbOutput);
161int SharedClipboardWinConvertMIMEToCFHTML(const char *pszSource, size_t cb, char **ppszOutput, uint32_t *pcbOutput);
162
163LRESULT SharedClipboardWinHandleWMChangeCBChain(PSHCLWINCTX pWinCtx, HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
164int SharedClipboardWinHandleWMDestroy(PSHCLWINCTX pWinCtx);
165int SharedClipboardWinHandleWMRenderAllFormats(PSHCLWINCTX pWinCtx, HWND hWnd);
166int SharedClipboardWinHandleWMTimer(PSHCLWINCTX pWinCtx);
167
168int SharedClipboardWinClearAndAnnounceFormats(PSHCLWINCTX pWinCtx, SHCLFORMATS fFormats, HWND hWnd);
169#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
170int SharedClipboardWinTransferCreate(PSHCLWINCTX pWinCtx, PSHCLTRANSFER pTransfer);
171void SharedClipboardWinTransferDestroy(PSHCLWINCTX pWinCtx, PSHCLTRANSFER pTransfer);
172#endif
173
174# ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
175class SharedClipboardTransferList;
176# ifndef FILEGROUPDESCRIPTOR
177class FILEGROUPDESCRIPTOR;
178# endif
179
180class SharedClipboardWinDataObject : public IDataObject //, public IDataObjectAsyncCapability
181{
182public:
183
184 enum Status
185 {
186 /** The object is uninitialized (not ready). */
187 Uninitialized = 0,
188 /** The object is initialized and ready to use. */
189 Initialized,
190 /** The operation has been successfully completed. */
191 Completed,
192 /** The operation has been canceled. */
193 Canceled,
194 /** An (unrecoverable) error occurred. */
195 Error
196 };
197
198public:
199
200 SharedClipboardWinDataObject(PSHCLTRANSFER pTransfer,
201 LPFORMATETC pFormatEtc = NULL, LPSTGMEDIUM pStgMed = NULL, ULONG cFormats = 0);
202 virtual ~SharedClipboardWinDataObject(void);
203
204public: /* IUnknown methods. */
205
206 STDMETHOD(QueryInterface)(REFIID iid, void ** ppvObject);
207 STDMETHOD_(ULONG, AddRef)(void);
208 STDMETHOD_(ULONG, Release)(void);
209
210public: /* IDataObject methods. */
211
212 STDMETHOD(GetData)(LPFORMATETC pFormatEtc, LPSTGMEDIUM pMedium);
213 STDMETHOD(GetDataHere)(LPFORMATETC pFormatEtc, LPSTGMEDIUM pMedium);
214 STDMETHOD(QueryGetData)(LPFORMATETC pFormatEtc);
215 STDMETHOD(GetCanonicalFormatEtc)(LPFORMATETC pFormatEct, LPFORMATETC pFormatEtcOut);
216 STDMETHOD(SetData)(LPFORMATETC pFormatEtc, LPSTGMEDIUM pMedium, BOOL fRelease);
217 STDMETHOD(EnumFormatEtc)(DWORD dwDirection, IEnumFORMATETC **ppEnumFormatEtc);
218 STDMETHOD(DAdvise)(LPFORMATETC pFormatEtc, DWORD advf, IAdviseSink *pAdvSink, DWORD *pdwConnection);
219 STDMETHOD(DUnadvise)(DWORD dwConnection);
220 STDMETHOD(EnumDAdvise)(IEnumSTATDATA **ppEnumAdvise);
221
222#ifdef VBOX_WITH_SHARED_CLIPBOARD_WIN_ASYNC
223public: /* IDataObjectAsyncCapability methods. */
224
225 STDMETHOD(EndOperation)(HRESULT hResult, IBindCtx* pbcReserved, DWORD dwEffects);
226 STDMETHOD(GetAsyncMode)(BOOL* pfIsOpAsync);
227 STDMETHOD(InOperation)(BOOL* pfInAsyncOp);
228 STDMETHOD(SetAsyncMode)(BOOL fDoOpAsync);
229 STDMETHOD(StartOperation)(IBindCtx* pbcReserved);
230#endif /* VBOX_WITH_SHARED_CLIPBOARD_WIN_ASYNC */
231
232public:
233
234 int Init(void);
235 void OnTransferComplete(int rc = VINF_SUCCESS);
236 void OnTransferCanceled();
237
238public:
239
240 static DECLCALLBACK(int) readThread(RTTHREAD ThreadSelf, void *pvUser);
241
242 static void logFormat(CLIPFORMAT fmt);
243
244protected:
245
246 static int Thread(RTTHREAD hThread, void *pvUser);
247
248 int readDir(PSHCLTRANSFER pTransfer, const Utf8Str &strPath);
249
250 int copyToHGlobal(const void *pvData, size_t cbData, UINT fFlags, HGLOBAL *phGlobal);
251 int createFileGroupDescriptorFromTransfer(PSHCLTRANSFER pTransfer,
252 bool fUnicode, HGLOBAL *phGlobal);
253
254 bool lookupFormatEtc(LPFORMATETC pFormatEtc, ULONG *puIndex);
255 void registerFormat(LPFORMATETC pFormatEtc, CLIPFORMAT clipFormat, TYMED tyMed = TYMED_HGLOBAL,
256 LONG lindex = -1, DWORD dwAspect = DVASPECT_CONTENT, DVTARGETDEVICE *pTargetDevice = NULL);
257protected:
258
259 /**
260 * Structure for keeping a single file system object entry.
261 */
262 struct FSOBJENTRY
263 {
264 /** Relative path of the object. */
265 Utf8Str strPath;
266 /** Related (cached) object information. */
267 SHCLFSOBJINFO objInfo;
268 };
269
270 /** Vector containing file system objects with its (cached) objection information. */
271 typedef std::vector<FSOBJENTRY> FsObjEntryList;
272
273 /** The object's current status. */
274 Status m_enmStatus;
275 /** The object's current reference count. */
276 LONG m_lRefCount;
277 /** How many formats have been registered. */
278 ULONG m_cFormats;
279 LPFORMATETC m_pFormatEtc;
280 LPSTGMEDIUM m_pStgMedium;
281 /** Pointer to the associated transfer object being handled. */
282 PSHCLTRANSFER m_pTransfer;
283 /** Current stream object being used. */
284 IStream *m_pStream;
285 /** Current object index being handled by the data object.
286 * This is needed to create the next IStream object for e.g. the next upcoming file/dir/++ in the transfer. */
287 ULONG m_uObjIdx;
288 /** List of (cached) file system objects. */
289 FsObjEntryList m_lstEntries;
290 /** Whether the transfer thread is running. */
291 bool m_fRunning;
292 /** Event being triggered when reading the transfer list been completed. */
293 RTSEMEVENT m_EventListComplete;
294 /** Event being triggered when the transfer has been completed. */
295 RTSEMEVENT m_EventTransferComplete;
296 /** Registered format for CFSTR_FILEDESCRIPTORA. */
297 UINT m_cfFileDescriptorA;
298 /** Registered format for CFSTR_FILEDESCRIPTORW. */
299 UINT m_cfFileDescriptorW;
300 /** Registered format for CFSTR_FILECONTENTS. */
301 UINT m_cfFileContents;
302 /** Registered format for CFSTR_PERFORMEDDROPEFFECT. */
303 UINT m_cfPerformedDropEffect;
304};
305
306class SharedClipboardWinEnumFormatEtc : public IEnumFORMATETC
307{
308public:
309
310 SharedClipboardWinEnumFormatEtc(LPFORMATETC pFormatEtc, ULONG cFormats);
311 virtual ~SharedClipboardWinEnumFormatEtc(void);
312
313public: /* IUnknown methods. */
314
315 STDMETHOD(QueryInterface)(REFIID iid, void ** ppvObject);
316 STDMETHOD_(ULONG, AddRef)(void);
317 STDMETHOD_(ULONG, Release)(void);
318
319public: /* IEnumFORMATETC methods. */
320
321 STDMETHOD(Next)(ULONG cFormats, LPFORMATETC pFormatEtc, ULONG *pcFetched);
322 STDMETHOD(Skip)(ULONG cFormats);
323 STDMETHOD(Reset)(void);
324 STDMETHOD(Clone)(IEnumFORMATETC **ppEnumFormatEtc);
325
326public:
327
328 static void CopyFormat(LPFORMATETC pFormatDest, LPFORMATETC pFormatSource);
329 static HRESULT CreateEnumFormatEtc(UINT cFormats, LPFORMATETC pFormatEtc, IEnumFORMATETC **ppEnumFormatEtc);
330
331private:
332
333 LONG m_lRefCount;
334 ULONG m_nIndex;
335 ULONG m_nNumFormats;
336 LPFORMATETC m_pFormatEtc;
337};
338
339/**
340 * Own IStream implementation to implement file-based clipboard operations
341 * through HGCM. Needed on Windows hosts and guests.
342 */
343class SharedClipboardWinStreamImpl : public IStream
344{
345public:
346
347 SharedClipboardWinStreamImpl(SharedClipboardWinDataObject *pParent, PSHCLTRANSFER pTransfer,
348 const Utf8Str &strPath, PSHCLFSOBJINFO pObjInfo);
349 virtual ~SharedClipboardWinStreamImpl(void);
350
351public: /* IUnknown methods. */
352
353 STDMETHOD(QueryInterface)(REFIID iid, void ** ppvObject);
354 STDMETHOD_(ULONG, AddRef)(void);
355 STDMETHOD_(ULONG, Release)(void);
356
357public: /* IStream methods. */
358
359 STDMETHOD(Clone)(IStream** ppStream);
360 STDMETHOD(Commit)(DWORD dwFrags);
361 STDMETHOD(CopyTo)(IStream* pDestStream, ULARGE_INTEGER nBytesToCopy, ULARGE_INTEGER* nBytesRead, ULARGE_INTEGER* nBytesWritten);
362 STDMETHOD(LockRegion)(ULARGE_INTEGER nStart, ULARGE_INTEGER nBytes,DWORD dwFlags);
363 STDMETHOD(Read)(void* pvBuffer, ULONG nBytesToRead, ULONG* nBytesRead);
364 STDMETHOD(Revert)(void);
365 STDMETHOD(Seek)(LARGE_INTEGER nMove, DWORD dwOrigin, ULARGE_INTEGER* nNewPos);
366 STDMETHOD(SetSize)(ULARGE_INTEGER nNewSize);
367 STDMETHOD(Stat)(STATSTG* statstg, DWORD dwFlags);
368 STDMETHOD(UnlockRegion)(ULARGE_INTEGER nStart, ULARGE_INTEGER nBytes, DWORD dwFlags);
369 STDMETHOD(Write)(const void* pvBuffer, ULONG nBytesToRead, ULONG* nBytesRead);
370
371public: /* Own methods. */
372
373 static HRESULT Create(SharedClipboardWinDataObject *pParent, PSHCLTRANSFER pTransfer, const Utf8Str &strPath,
374 PSHCLFSOBJINFO pObjInfo, IStream **ppStream);
375private:
376
377 /** Pointer to the parent data object. */
378 SharedClipboardWinDataObject *m_pParent;
379 /** The stream object's current reference count. */
380 LONG m_lRefCount;
381 /** Pointer to the associated Shared Clipboard transfer. */
382 PSHCLTRANSFER m_pTransfer;
383 /** The object handle to use. */
384 SHCLOBJHANDLE m_hObj;
385 /** Object path. */
386 Utf8Str m_strPath;
387 /** (Cached) object information. */
388 SHCLFSOBJINFO m_objInfo;
389 /** Number of bytes already processed. */
390 uint64_t m_cbProcessed;
391 /** Whether this object already is in completed state or not. */
392 bool m_fIsComplete;
393};
394
395/**
396 * Class for Windows-specifics for maintaining a single Shared Clipboard transfer.
397 * Set as pvUser / cbUser in SHCLTRANSFERCTX.
398 */
399class SharedClipboardWinTransferCtx
400{
401public:
402 SharedClipboardWinTransferCtx()
403 : pDataObj(NULL) { }
404
405 virtual ~SharedClipboardWinTransferCtx()
406 {
407 if (pDataObj)
408 delete pDataObj;
409 }
410
411 /** Pointer to data object to use for this transfer.
412 * Can be NULL if not being used. */
413 SharedClipboardWinDataObject *pDataObj;
414};
415# endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
416#endif /* !VBOX_INCLUDED_GuestHost_SharedClipboard_win_h */
417
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