1 | /* $Id: ClipboardStreamImpl-win.cpp 79702 2019-07-11 19:34:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * ClipboardStreamImpl-win.cpp - Shared Clipboard IStream object implementation (guest and host side).
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
|
---|
23 | #include <VBox/GuestHost/SharedClipboard-win.h>
|
---|
24 |
|
---|
25 | #include <iprt/asm.h>
|
---|
26 | #include <iprt/ldr.h>
|
---|
27 | #include <iprt/thread.h>
|
---|
28 |
|
---|
29 | #include <VBox/GuestHost/SharedClipboard.h>
|
---|
30 | #include <VBox/GuestHost/SharedClipboard-win.h>
|
---|
31 | #include <strsafe.h>
|
---|
32 |
|
---|
33 | #include <VBox/log.h>
|
---|
34 |
|
---|
35 |
|
---|
36 | /*********************************************************************************************************************************
|
---|
37 | * Structures and Typedefs *
|
---|
38 | *********************************************************************************************************************************/
|
---|
39 |
|
---|
40 |
|
---|
41 |
|
---|
42 | /*********************************************************************************************************************************
|
---|
43 | * Static variables *
|
---|
44 | *********************************************************************************************************************************/
|
---|
45 |
|
---|
46 |
|
---|
47 |
|
---|
48 | VBoxClipboardWinStreamImpl::VBoxClipboardWinStreamImpl(VBoxClipboardWinDataObject *pParent, PSHAREDCLIPBOARDURITRANSFER pTransfer,
|
---|
49 | const Utf8Str &strPath, PSHAREDCLIPBOARDFSOBJINFO pObjInfo)
|
---|
50 | : m_pParent(pParent)
|
---|
51 | , m_lRefCount(1) /* Our IDataObjct *always* holds the last reference to this object; needed for the callbacks. */
|
---|
52 | , m_pURITransfer(pTransfer)
|
---|
53 | , m_strPath(strPath)
|
---|
54 | , m_hObj(SHAREDCLIPBOARDOBJHANDLE_INVALID)
|
---|
55 | , m_objInfo(*pObjInfo)
|
---|
56 | , m_cbProcessed(0)
|
---|
57 | , m_fNotifiedComplete(false)
|
---|
58 | {
|
---|
59 | AssertPtr(m_pURITransfer);
|
---|
60 |
|
---|
61 | LogFunc(("m_strPath=%s\n", m_strPath.c_str()));
|
---|
62 | }
|
---|
63 |
|
---|
64 | VBoxClipboardWinStreamImpl::~VBoxClipboardWinStreamImpl(void)
|
---|
65 | {
|
---|
66 | LogFlowThisFuncEnter();
|
---|
67 | }
|
---|
68 |
|
---|
69 | /*
|
---|
70 | * IUnknown methods.
|
---|
71 | */
|
---|
72 |
|
---|
73 | STDMETHODIMP VBoxClipboardWinStreamImpl::QueryInterface(REFIID iid, void **ppvObject)
|
---|
74 | {
|
---|
75 | AssertPtrReturn(ppvObject, E_INVALIDARG);
|
---|
76 |
|
---|
77 | if (iid == IID_IUnknown)
|
---|
78 | {
|
---|
79 | LogFlowFunc(("IID_IUnknown\n"));
|
---|
80 | *ppvObject = (IUnknown *)(ISequentialStream *)this;
|
---|
81 | }
|
---|
82 | else if (iid == IID_ISequentialStream)
|
---|
83 | {
|
---|
84 | LogFlowFunc(("IID_ISequentialStream\n"));
|
---|
85 | *ppvObject = (ISequentialStream *)this;
|
---|
86 | }
|
---|
87 | else if (iid == IID_IStream)
|
---|
88 | {
|
---|
89 | LogFlowFunc(("IID_IStream\n"));
|
---|
90 | *ppvObject = (IStream *)this;
|
---|
91 | }
|
---|
92 | else
|
---|
93 | {
|
---|
94 | *ppvObject = NULL;
|
---|
95 | return E_NOINTERFACE;
|
---|
96 | }
|
---|
97 |
|
---|
98 | AddRef();
|
---|
99 | return S_OK;
|
---|
100 | }
|
---|
101 |
|
---|
102 | STDMETHODIMP_(ULONG) VBoxClipboardWinStreamImpl::AddRef(void)
|
---|
103 | {
|
---|
104 | LONG lCount = InterlockedIncrement(&m_lRefCount);
|
---|
105 | LogFlowFunc(("lCount=%RI32\n", lCount));
|
---|
106 | return lCount;
|
---|
107 | }
|
---|
108 |
|
---|
109 | STDMETHODIMP_(ULONG) VBoxClipboardWinStreamImpl::Release(void)
|
---|
110 | {
|
---|
111 | LONG lCount = InterlockedDecrement(&m_lRefCount);
|
---|
112 | LogFlowFunc(("lCount=%RI32\n", m_lRefCount));
|
---|
113 | if (lCount == 0)
|
---|
114 | {
|
---|
115 | if ( !m_fNotifiedComplete
|
---|
116 | && m_pParent)
|
---|
117 | {
|
---|
118 | m_pParent->OnTransferComplete();
|
---|
119 | }
|
---|
120 |
|
---|
121 | delete this;
|
---|
122 | return 0;
|
---|
123 | }
|
---|
124 |
|
---|
125 | return lCount;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /*
|
---|
129 | * IStream methods.
|
---|
130 | */
|
---|
131 |
|
---|
132 | STDMETHODIMP VBoxClipboardWinStreamImpl::Clone(IStream** ppStream)
|
---|
133 | {
|
---|
134 | RT_NOREF(ppStream);
|
---|
135 |
|
---|
136 | LogFlowFuncEnter();
|
---|
137 | return E_NOTIMPL;
|
---|
138 | }
|
---|
139 |
|
---|
140 | STDMETHODIMP VBoxClipboardWinStreamImpl::Commit(DWORD dwFrags)
|
---|
141 | {
|
---|
142 | RT_NOREF(dwFrags);
|
---|
143 |
|
---|
144 | LogFlowThisFuncEnter();
|
---|
145 | return E_NOTIMPL;
|
---|
146 | }
|
---|
147 |
|
---|
148 | STDMETHODIMP VBoxClipboardWinStreamImpl::CopyTo(IStream *pDestStream, ULARGE_INTEGER nBytesToCopy, ULARGE_INTEGER *nBytesRead,
|
---|
149 | ULARGE_INTEGER *nBytesWritten)
|
---|
150 | {
|
---|
151 | RT_NOREF(pDestStream, nBytesToCopy, nBytesRead, nBytesWritten);
|
---|
152 |
|
---|
153 | LogFlowThisFuncEnter();
|
---|
154 | return E_NOTIMPL;
|
---|
155 | }
|
---|
156 |
|
---|
157 | STDMETHODIMP VBoxClipboardWinStreamImpl::LockRegion(ULARGE_INTEGER nStart, ULARGE_INTEGER nBytes,DWORD dwFlags)
|
---|
158 | {
|
---|
159 | RT_NOREF(nStart, nBytes, dwFlags);
|
---|
160 |
|
---|
161 | LogFlowThisFuncEnter();
|
---|
162 | return STG_E_INVALIDFUNCTION;
|
---|
163 | }
|
---|
164 |
|
---|
165 | /* Note: Windows seems to assume EOF if nBytesRead < nBytesToRead. */
|
---|
166 | STDMETHODIMP VBoxClipboardWinStreamImpl::Read(void *pvBuffer, ULONG nBytesToRead, ULONG *nBytesRead)
|
---|
167 | {
|
---|
168 | LogFlowThisFunc(("Enter: m_cbProcessed=%RU64\n", m_cbProcessed));
|
---|
169 |
|
---|
170 | /** @todo Is there any locking required so that parallel reads aren't possible? */
|
---|
171 |
|
---|
172 | if (!pvBuffer)
|
---|
173 | return STG_E_INVALIDPOINTER;
|
---|
174 |
|
---|
175 | if (nBytesToRead == 0)
|
---|
176 | {
|
---|
177 | if (nBytesRead)
|
---|
178 | *nBytesRead = 0;
|
---|
179 | return S_OK;
|
---|
180 | }
|
---|
181 |
|
---|
182 | int rc;
|
---|
183 |
|
---|
184 | try
|
---|
185 | {
|
---|
186 | if ( m_hObj == SHAREDCLIPBOARDOBJHANDLE_INVALID
|
---|
187 | && m_pURITransfer->ProviderIface.pfnObjOpen)
|
---|
188 | {
|
---|
189 | VBOXCLIPBOARDCREATEPARMS createParms;
|
---|
190 | RT_ZERO(createParms);
|
---|
191 |
|
---|
192 | rc = m_pURITransfer->ProviderIface.pfnObjOpen(&m_pURITransfer->ProviderCtx, m_strPath.c_str(), &createParms, &m_hObj);
|
---|
193 | }
|
---|
194 | else
|
---|
195 | rc = VINF_SUCCESS;
|
---|
196 |
|
---|
197 | uint32_t cbRead = 0;
|
---|
198 |
|
---|
199 | const uint64_t cbSize = (uint64_t)m_objInfo.cbObject;
|
---|
200 | const uint32_t cbToRead = RT_MIN(cbSize - m_cbProcessed, nBytesToRead);
|
---|
201 |
|
---|
202 | bool fComplete = false;
|
---|
203 |
|
---|
204 | if (RT_SUCCESS(rc))
|
---|
205 | {
|
---|
206 | if (cbToRead)
|
---|
207 | {
|
---|
208 | rc = m_pURITransfer->ProviderIface.pfnObjRead(&m_pURITransfer->ProviderCtx, m_hObj,
|
---|
209 | pvBuffer, cbToRead, 0 /* fFlags */, &cbRead);
|
---|
210 | if (RT_SUCCESS(rc))
|
---|
211 | {
|
---|
212 | m_cbProcessed += cbRead;
|
---|
213 | Assert(m_cbProcessed <= cbSize);
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | /* Transfer complete? Make sure to close the object again. */
|
---|
218 | fComplete = m_cbProcessed == cbSize;
|
---|
219 |
|
---|
220 | if (fComplete)
|
---|
221 | {
|
---|
222 | if (m_pURITransfer->ProviderIface.pfnObjClose)
|
---|
223 | {
|
---|
224 | int rc2 = m_pURITransfer->ProviderIface.pfnObjClose(&m_pURITransfer->ProviderCtx, m_hObj);
|
---|
225 | AssertRC(rc2);
|
---|
226 | }
|
---|
227 |
|
---|
228 | if (m_pParent)
|
---|
229 | {
|
---|
230 | m_pParent->OnTransferComplete();
|
---|
231 | m_fNotifiedComplete = true;
|
---|
232 | }
|
---|
233 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 | LogFlowThisFunc(("Leave: rc=%Rrc, cbSize=%RU64, cbProcessed=%RU64 -> nBytesToRead=%RU32, cbToRead=%RU32, cbRead=%RU32\n",
|
---|
237 | rc, cbSize, m_cbProcessed, nBytesToRead, cbToRead, cbRead));
|
---|
238 |
|
---|
239 | if (nBytesRead)
|
---|
240 | *nBytesRead = (ULONG)cbRead;
|
---|
241 |
|
---|
242 | if (nBytesToRead != cbRead)
|
---|
243 | return S_FALSE;
|
---|
244 |
|
---|
245 | return S_OK;
|
---|
246 | }
|
---|
247 | catch (...)
|
---|
248 | {
|
---|
249 | LogFunc(("Caught exception\n"));
|
---|
250 | }
|
---|
251 |
|
---|
252 | return E_FAIL;
|
---|
253 | }
|
---|
254 |
|
---|
255 | STDMETHODIMP VBoxClipboardWinStreamImpl::Revert(void)
|
---|
256 | {
|
---|
257 | LogFlowThisFuncEnter();
|
---|
258 | return E_NOTIMPL;
|
---|
259 | }
|
---|
260 |
|
---|
261 | STDMETHODIMP VBoxClipboardWinStreamImpl::Seek(LARGE_INTEGER nMove, DWORD dwOrigin, ULARGE_INTEGER* nNewPos)
|
---|
262 | {
|
---|
263 | RT_NOREF(nMove, dwOrigin, nNewPos);
|
---|
264 |
|
---|
265 | LogFlowThisFunc(("nMove=%RI64, dwOrigin=%RI32\n", nMove, dwOrigin));
|
---|
266 |
|
---|
267 | return E_NOTIMPL;
|
---|
268 | }
|
---|
269 |
|
---|
270 | STDMETHODIMP VBoxClipboardWinStreamImpl::SetSize(ULARGE_INTEGER nNewSize)
|
---|
271 | {
|
---|
272 | RT_NOREF(nNewSize);
|
---|
273 |
|
---|
274 | LogFlowThisFuncEnter();
|
---|
275 | return E_NOTIMPL;
|
---|
276 | }
|
---|
277 |
|
---|
278 | STDMETHODIMP VBoxClipboardWinStreamImpl::Stat(STATSTG *pStatStg, DWORD dwFlags)
|
---|
279 | {
|
---|
280 | HRESULT hr = S_OK;
|
---|
281 |
|
---|
282 | if (pStatStg)
|
---|
283 | {
|
---|
284 | RT_BZERO(pStatStg, sizeof(STATSTG));
|
---|
285 |
|
---|
286 | switch (dwFlags)
|
---|
287 | {
|
---|
288 | case STATFLAG_NONAME:
|
---|
289 | pStatStg->pwcsName = NULL;
|
---|
290 | break;
|
---|
291 |
|
---|
292 | case STATFLAG_DEFAULT:
|
---|
293 | {
|
---|
294 | int rc2 = RTStrToUtf16(m_strPath.c_str(), &pStatStg->pwcsName);
|
---|
295 | if (RT_FAILURE(rc2))
|
---|
296 | hr = E_FAIL;
|
---|
297 | break;
|
---|
298 | }
|
---|
299 |
|
---|
300 | default:
|
---|
301 | hr = STG_E_INVALIDFLAG;
|
---|
302 | break;
|
---|
303 | }
|
---|
304 |
|
---|
305 | if (SUCCEEDED(hr))
|
---|
306 | {
|
---|
307 | pStatStg->type = STGTY_STREAM;
|
---|
308 | pStatStg->grfMode = STGM_READ;
|
---|
309 | pStatStg->grfLocksSupported = 0;
|
---|
310 | pStatStg->cbSize.QuadPart = (uint64_t)m_objInfo.cbObject;
|
---|
311 | }
|
---|
312 | }
|
---|
313 | else
|
---|
314 | hr = STG_E_INVALIDPOINTER;
|
---|
315 |
|
---|
316 | LogFlowThisFunc(("hr=%Rhrc\n", hr));
|
---|
317 | return hr;
|
---|
318 | }
|
---|
319 |
|
---|
320 | STDMETHODIMP VBoxClipboardWinStreamImpl::UnlockRegion(ULARGE_INTEGER nStart, ULARGE_INTEGER nBytes, DWORD dwFlags)
|
---|
321 | {
|
---|
322 | RT_NOREF(nStart, nBytes, dwFlags);
|
---|
323 |
|
---|
324 | LogFlowThisFuncEnter();
|
---|
325 | return E_NOTIMPL;
|
---|
326 | }
|
---|
327 |
|
---|
328 | STDMETHODIMP VBoxClipboardWinStreamImpl::Write(const void *pvBuffer, ULONG nBytesToRead, ULONG *nBytesRead)
|
---|
329 | {
|
---|
330 | RT_NOREF(pvBuffer, nBytesToRead, nBytesRead);
|
---|
331 |
|
---|
332 | LogFlowThisFuncEnter();
|
---|
333 | return E_NOTIMPL;
|
---|
334 | }
|
---|
335 |
|
---|
336 | /*
|
---|
337 | * Own stuff.
|
---|
338 | */
|
---|
339 |
|
---|
340 | /**
|
---|
341 | * Factory to create our own IStream implementation.
|
---|
342 | *
|
---|
343 | * @returns HRESULT
|
---|
344 | * @param pParent Pointer to the parent data object.
|
---|
345 | * @param pTransfer Pointer to URI transfer object to use.
|
---|
346 | * @param strPath Path of object to handle for the stream.
|
---|
347 | * @param pObjInfo Pointer to object information.
|
---|
348 | * @param ppStream Where to return the created stream object on success.
|
---|
349 | */
|
---|
350 | /* static */
|
---|
351 | HRESULT VBoxClipboardWinStreamImpl::Create(VBoxClipboardWinDataObject *pParent, PSHAREDCLIPBOARDURITRANSFER pTransfer,
|
---|
352 | const Utf8Str &strPath, PSHAREDCLIPBOARDFSOBJINFO pObjInfo,
|
---|
353 | IStream **ppStream)
|
---|
354 | {
|
---|
355 | AssertPtrReturn(pTransfer, E_POINTER);
|
---|
356 |
|
---|
357 | VBoxClipboardWinStreamImpl *pStream = new VBoxClipboardWinStreamImpl(pParent, pTransfer, strPath, pObjInfo);
|
---|
358 | if (pStream)
|
---|
359 | {
|
---|
360 | pStream->AddRef();
|
---|
361 |
|
---|
362 | *ppStream = pStream;
|
---|
363 | return S_OK;
|
---|
364 | }
|
---|
365 |
|
---|
366 | return E_FAIL;
|
---|
367 | }
|
---|
368 |
|
---|