1 | /* $Id: ClipboardStreamImpl-win.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * ClipboardStreamImpl-win.cpp - Shared Clipboard IStream object implementation (guest and host side).
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2019-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 <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 | SharedClipboardWinStreamImpl::SharedClipboardWinStreamImpl(SharedClipboardWinDataObject *pParent, PSHCLTRANSFER pTransfer,
|
---|
49 | const Utf8Str &strPath, PSHCLFSOBJINFO 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_pTransfer(pTransfer)
|
---|
53 | , m_strPath(strPath)
|
---|
54 | , m_hObj(SHCLOBJHANDLE_INVALID)
|
---|
55 | , m_objInfo(*pObjInfo)
|
---|
56 | , m_cbProcessed(0)
|
---|
57 | , m_fIsComplete(false)
|
---|
58 | {
|
---|
59 | AssertPtr(m_pTransfer);
|
---|
60 |
|
---|
61 | LogFunc(("m_strPath=%s\n", m_strPath.c_str()));
|
---|
62 | }
|
---|
63 |
|
---|
64 | SharedClipboardWinStreamImpl::~SharedClipboardWinStreamImpl(void)
|
---|
65 | {
|
---|
66 | LogFlowThisFuncEnter();
|
---|
67 | }
|
---|
68 |
|
---|
69 | /*
|
---|
70 | * IUnknown methods.
|
---|
71 | */
|
---|
72 |
|
---|
73 | STDMETHODIMP SharedClipboardWinStreamImpl::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) SharedClipboardWinStreamImpl::AddRef(void)
|
---|
103 | {
|
---|
104 | LONG lCount = InterlockedIncrement(&m_lRefCount);
|
---|
105 | LogFlowFunc(("lCount=%RI32\n", lCount));
|
---|
106 | return lCount;
|
---|
107 | }
|
---|
108 |
|
---|
109 | STDMETHODIMP_(ULONG) SharedClipboardWinStreamImpl::Release(void)
|
---|
110 | {
|
---|
111 | LONG lCount = InterlockedDecrement(&m_lRefCount);
|
---|
112 | LogFlowFunc(("lCount=%RI32\n", m_lRefCount));
|
---|
113 | if (lCount == 0)
|
---|
114 | {
|
---|
115 | delete this;
|
---|
116 | return 0;
|
---|
117 | }
|
---|
118 |
|
---|
119 | return lCount;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /*
|
---|
123 | * IStream methods.
|
---|
124 | */
|
---|
125 |
|
---|
126 | STDMETHODIMP SharedClipboardWinStreamImpl::Clone(IStream** ppStream)
|
---|
127 | {
|
---|
128 | RT_NOREF(ppStream);
|
---|
129 |
|
---|
130 | LogFlowFuncEnter();
|
---|
131 | return E_NOTIMPL;
|
---|
132 | }
|
---|
133 |
|
---|
134 | STDMETHODIMP SharedClipboardWinStreamImpl::Commit(DWORD dwFrags)
|
---|
135 | {
|
---|
136 | RT_NOREF(dwFrags);
|
---|
137 |
|
---|
138 | LogFlowThisFuncEnter();
|
---|
139 | return E_NOTIMPL;
|
---|
140 | }
|
---|
141 |
|
---|
142 | STDMETHODIMP SharedClipboardWinStreamImpl::CopyTo(IStream *pDestStream, ULARGE_INTEGER nBytesToCopy, ULARGE_INTEGER *nBytesRead,
|
---|
143 | ULARGE_INTEGER *nBytesWritten)
|
---|
144 | {
|
---|
145 | RT_NOREF(pDestStream, nBytesToCopy, nBytesRead, nBytesWritten);
|
---|
146 |
|
---|
147 | LogFlowThisFuncEnter();
|
---|
148 | return E_NOTIMPL;
|
---|
149 | }
|
---|
150 |
|
---|
151 | STDMETHODIMP SharedClipboardWinStreamImpl::LockRegion(ULARGE_INTEGER nStart, ULARGE_INTEGER nBytes,DWORD dwFlags)
|
---|
152 | {
|
---|
153 | RT_NOREF(nStart, nBytes, dwFlags);
|
---|
154 |
|
---|
155 | LogFlowThisFuncEnter();
|
---|
156 | return STG_E_INVALIDFUNCTION;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /* Note: Windows seems to assume EOF if nBytesRead < nBytesToRead. */
|
---|
160 | STDMETHODIMP SharedClipboardWinStreamImpl::Read(void *pvBuffer, ULONG nBytesToRead, ULONG *nBytesRead)
|
---|
161 | {
|
---|
162 | LogFlowThisFunc(("Enter: m_cbProcessed=%RU64\n", m_cbProcessed));
|
---|
163 |
|
---|
164 | /** @todo Is there any locking required so that parallel reads aren't possible? */
|
---|
165 |
|
---|
166 | if (!pvBuffer)
|
---|
167 | return STG_E_INVALIDPOINTER;
|
---|
168 |
|
---|
169 | if ( nBytesToRead == 0
|
---|
170 | || m_fIsComplete)
|
---|
171 | {
|
---|
172 | if (nBytesRead)
|
---|
173 | *nBytesRead = 0;
|
---|
174 | return S_OK;
|
---|
175 | }
|
---|
176 |
|
---|
177 | int rc;
|
---|
178 |
|
---|
179 | try
|
---|
180 | {
|
---|
181 | if ( m_hObj == SHCLOBJHANDLE_INVALID
|
---|
182 | && m_pTransfer->ProviderIface.pfnObjOpen)
|
---|
183 | {
|
---|
184 | SHCLOBJOPENCREATEPARMS openParms;
|
---|
185 | rc = ShClTransferObjOpenParmsInit(&openParms);
|
---|
186 | if (RT_SUCCESS(rc))
|
---|
187 | {
|
---|
188 | openParms.fCreate = SHCL_OBJ_CF_ACCESS_READ
|
---|
189 | | SHCL_OBJ_CF_ACCESS_DENYWRITE;
|
---|
190 |
|
---|
191 | rc = RTStrCopy(openParms.pszPath, openParms.cbPath, m_strPath.c_str());
|
---|
192 | if (RT_SUCCESS(rc))
|
---|
193 | {
|
---|
194 | rc = m_pTransfer->ProviderIface.pfnObjOpen(&m_pTransfer->ProviderCtx, &openParms, &m_hObj);
|
---|
195 | }
|
---|
196 |
|
---|
197 | ShClTransferObjOpenParmsDestroy(&openParms);
|
---|
198 | }
|
---|
199 | }
|
---|
200 | else
|
---|
201 | rc = VINF_SUCCESS;
|
---|
202 |
|
---|
203 | uint32_t cbRead = 0;
|
---|
204 |
|
---|
205 | const uint64_t cbSize = (uint64_t)m_objInfo.cbObject;
|
---|
206 | const uint32_t cbToRead = RT_MIN(cbSize - m_cbProcessed, nBytesToRead);
|
---|
207 |
|
---|
208 | if (RT_SUCCESS(rc))
|
---|
209 | {
|
---|
210 | if (cbToRead)
|
---|
211 | {
|
---|
212 | rc = m_pTransfer->ProviderIface.pfnObjRead(&m_pTransfer->ProviderCtx, m_hObj,
|
---|
213 | pvBuffer, cbToRead, 0 /* fFlags */, &cbRead);
|
---|
214 | if (RT_SUCCESS(rc))
|
---|
215 | {
|
---|
216 | m_cbProcessed += cbRead;
|
---|
217 | Assert(m_cbProcessed <= cbSize);
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | /* Transfer complete? Make sure to close the object again. */
|
---|
222 | m_fIsComplete = m_cbProcessed == cbSize;
|
---|
223 |
|
---|
224 | if (m_fIsComplete)
|
---|
225 | {
|
---|
226 | if (m_pTransfer->ProviderIface.pfnObjClose)
|
---|
227 | {
|
---|
228 | int rc2 = m_pTransfer->ProviderIface.pfnObjClose(&m_pTransfer->ProviderCtx, m_hObj);
|
---|
229 | AssertRC(rc2);
|
---|
230 | }
|
---|
231 |
|
---|
232 | if (m_pParent)
|
---|
233 | m_pParent->OnTransferComplete();
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | LogFlowThisFunc(("Leave: rc=%Rrc, cbSize=%RU64, cbProcessed=%RU64 -> nBytesToRead=%RU32, cbToRead=%RU32, cbRead=%RU32\n",
|
---|
238 | rc, cbSize, m_cbProcessed, nBytesToRead, cbToRead, cbRead));
|
---|
239 |
|
---|
240 | if (nBytesRead)
|
---|
241 | *nBytesRead = (ULONG)cbRead;
|
---|
242 |
|
---|
243 | if (nBytesToRead != cbRead)
|
---|
244 | return S_FALSE;
|
---|
245 |
|
---|
246 | return S_OK;
|
---|
247 | }
|
---|
248 | catch (...)
|
---|
249 | {
|
---|
250 | LogFunc(("Caught exception\n"));
|
---|
251 | }
|
---|
252 |
|
---|
253 | return E_FAIL;
|
---|
254 | }
|
---|
255 |
|
---|
256 | STDMETHODIMP SharedClipboardWinStreamImpl::Revert(void)
|
---|
257 | {
|
---|
258 | LogFlowThisFuncEnter();
|
---|
259 | return E_NOTIMPL;
|
---|
260 | }
|
---|
261 |
|
---|
262 | STDMETHODIMP SharedClipboardWinStreamImpl::Seek(LARGE_INTEGER nMove, DWORD dwOrigin, ULARGE_INTEGER* nNewPos)
|
---|
263 | {
|
---|
264 | RT_NOREF(nMove, dwOrigin, nNewPos);
|
---|
265 |
|
---|
266 | LogFlowThisFunc(("nMove=%RI64, dwOrigin=%RI32\n", nMove, dwOrigin));
|
---|
267 |
|
---|
268 | return E_NOTIMPL;
|
---|
269 | }
|
---|
270 |
|
---|
271 | STDMETHODIMP SharedClipboardWinStreamImpl::SetSize(ULARGE_INTEGER nNewSize)
|
---|
272 | {
|
---|
273 | RT_NOREF(nNewSize);
|
---|
274 |
|
---|
275 | LogFlowThisFuncEnter();
|
---|
276 | return E_NOTIMPL;
|
---|
277 | }
|
---|
278 |
|
---|
279 | STDMETHODIMP SharedClipboardWinStreamImpl::Stat(STATSTG *pStatStg, DWORD dwFlags)
|
---|
280 | {
|
---|
281 | HRESULT hr = S_OK;
|
---|
282 |
|
---|
283 | if (pStatStg)
|
---|
284 | {
|
---|
285 | RT_ZERO(*pStatStg);
|
---|
286 |
|
---|
287 | switch (dwFlags)
|
---|
288 | {
|
---|
289 | case STATFLAG_NONAME:
|
---|
290 | pStatStg->pwcsName = NULL;
|
---|
291 | break;
|
---|
292 |
|
---|
293 | case STATFLAG_DEFAULT:
|
---|
294 | {
|
---|
295 | /** @todo r=bird: This is using the wrong allocator. According to MSDN the
|
---|
296 | * caller will pass this to CoTaskMemFree, so we should use CoTaskMemAlloc to
|
---|
297 | * allocate it. */
|
---|
298 | int rc2 = RTStrToUtf16(m_strPath.c_str(), &pStatStg->pwcsName);
|
---|
299 | if (RT_FAILURE(rc2))
|
---|
300 | hr = E_FAIL;
|
---|
301 | break;
|
---|
302 | }
|
---|
303 |
|
---|
304 | default:
|
---|
305 | hr = STG_E_INVALIDFLAG;
|
---|
306 | break;
|
---|
307 | }
|
---|
308 |
|
---|
309 | if (SUCCEEDED(hr))
|
---|
310 | {
|
---|
311 | pStatStg->type = STGTY_STREAM;
|
---|
312 | pStatStg->grfMode = STGM_READ;
|
---|
313 | pStatStg->grfLocksSupported = 0;
|
---|
314 | pStatStg->cbSize.QuadPart = (uint64_t)m_objInfo.cbObject;
|
---|
315 | }
|
---|
316 | }
|
---|
317 | else
|
---|
318 | hr = STG_E_INVALIDPOINTER;
|
---|
319 |
|
---|
320 | LogFlowThisFunc(("hr=%Rhrc\n", hr));
|
---|
321 | return hr;
|
---|
322 | }
|
---|
323 |
|
---|
324 | STDMETHODIMP SharedClipboardWinStreamImpl::UnlockRegion(ULARGE_INTEGER nStart, ULARGE_INTEGER nBytes, DWORD dwFlags)
|
---|
325 | {
|
---|
326 | RT_NOREF(nStart, nBytes, dwFlags);
|
---|
327 |
|
---|
328 | LogFlowThisFuncEnter();
|
---|
329 | return E_NOTIMPL;
|
---|
330 | }
|
---|
331 |
|
---|
332 | STDMETHODIMP SharedClipboardWinStreamImpl::Write(const void *pvBuffer, ULONG nBytesToRead, ULONG *nBytesRead)
|
---|
333 | {
|
---|
334 | RT_NOREF(pvBuffer, nBytesToRead, nBytesRead);
|
---|
335 |
|
---|
336 | LogFlowThisFuncEnter();
|
---|
337 | return E_NOTIMPL;
|
---|
338 | }
|
---|
339 |
|
---|
340 | /*
|
---|
341 | * Own stuff.
|
---|
342 | */
|
---|
343 |
|
---|
344 | /**
|
---|
345 | * Factory to create our own IStream implementation.
|
---|
346 | *
|
---|
347 | * @returns HRESULT
|
---|
348 | * @param pParent Pointer to the parent data object.
|
---|
349 | * @param pTransfer Pointer to Shared Clipboard transfer object to use.
|
---|
350 | * @param strPath Path of object to handle for the stream.
|
---|
351 | * @param pObjInfo Pointer to object information.
|
---|
352 | * @param ppStream Where to return the created stream object on success.
|
---|
353 | */
|
---|
354 | /* static */
|
---|
355 | HRESULT SharedClipboardWinStreamImpl::Create(SharedClipboardWinDataObject *pParent, PSHCLTRANSFER pTransfer,
|
---|
356 | const Utf8Str &strPath, PSHCLFSOBJINFO pObjInfo,
|
---|
357 | IStream **ppStream)
|
---|
358 | {
|
---|
359 | AssertPtrReturn(pTransfer, E_POINTER);
|
---|
360 |
|
---|
361 | SharedClipboardWinStreamImpl *pStream = new SharedClipboardWinStreamImpl(pParent, pTransfer, strPath, pObjInfo);
|
---|
362 | if (pStream)
|
---|
363 | {
|
---|
364 | pStream->AddRef();
|
---|
365 |
|
---|
366 | *ppStream = pStream;
|
---|
367 | return S_OK;
|
---|
368 | }
|
---|
369 |
|
---|
370 | return E_FAIL;
|
---|
371 | }
|
---|
372 |
|
---|