VirtualBox

source: vbox/trunk/src/VBox/GuestHost/SharedClipboard/ClipboardStreamImpl-win.cpp@ 80283

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

Shared Clipboard/URI: Update; more work on root list handling.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.7 KB
Line 
1/* $Id: ClipboardStreamImpl-win.cpp 80283 2019-08-15 08:47:23Z 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
48VBoxClipboardWinStreamImpl::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
64VBoxClipboardWinStreamImpl::~VBoxClipboardWinStreamImpl(void)
65{
66 LogFlowThisFuncEnter();
67}
68
69/*
70 * IUnknown methods.
71 */
72
73STDMETHODIMP 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
102STDMETHODIMP_(ULONG) VBoxClipboardWinStreamImpl::AddRef(void)
103{
104 LONG lCount = InterlockedIncrement(&m_lRefCount);
105 LogFlowFunc(("lCount=%RI32\n", lCount));
106 return lCount;
107}
108
109STDMETHODIMP_(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
132STDMETHODIMP VBoxClipboardWinStreamImpl::Clone(IStream** ppStream)
133{
134 RT_NOREF(ppStream);
135
136 LogFlowFuncEnter();
137 return E_NOTIMPL;
138}
139
140STDMETHODIMP VBoxClipboardWinStreamImpl::Commit(DWORD dwFrags)
141{
142 RT_NOREF(dwFrags);
143
144 LogFlowThisFuncEnter();
145 return E_NOTIMPL;
146}
147
148STDMETHODIMP 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
157STDMETHODIMP 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. */
166STDMETHODIMP 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 VBOXCLIPBOARDOBJOPENCREATEPARMS createParms;
190 RT_ZERO(createParms);
191
192 createParms.pszPath = RTStrDup(m_strPath.c_str());
193 if (createParms.pszPath)
194 {
195 rc = m_pURITransfer->ProviderIface.pfnObjOpen(&m_pURITransfer->ProviderCtx, &createParms, &m_hObj);
196
197 RTStrFree(createParms.pszPath);
198 }
199 else
200 rc = VERR_NO_MEMORY;
201 }
202 else
203 rc = VINF_SUCCESS;
204
205 uint32_t cbRead = 0;
206
207 const uint64_t cbSize = (uint64_t)m_objInfo.cbObject;
208 const uint32_t cbToRead = RT_MIN(cbSize - m_cbProcessed, nBytesToRead);
209
210 bool fComplete = false;
211
212 if (RT_SUCCESS(rc))
213 {
214 if (cbToRead)
215 {
216 rc = m_pURITransfer->ProviderIface.pfnObjRead(&m_pURITransfer->ProviderCtx, m_hObj,
217 pvBuffer, cbToRead, 0 /* fFlags */, &cbRead);
218 if (RT_SUCCESS(rc))
219 {
220 m_cbProcessed += cbRead;
221 Assert(m_cbProcessed <= cbSize);
222 }
223 }
224
225 /* Transfer complete? Make sure to close the object again. */
226 fComplete = m_cbProcessed == cbSize;
227
228 if (fComplete)
229 {
230 if (m_pURITransfer->ProviderIface.pfnObjClose)
231 {
232 int rc2 = m_pURITransfer->ProviderIface.pfnObjClose(&m_pURITransfer->ProviderCtx, m_hObj);
233 AssertRC(rc2);
234 }
235
236 if (m_pParent)
237 {
238 m_pParent->OnTransferComplete();
239 m_fNotifiedComplete = true;
240 }
241 }
242 }
243
244 LogFlowThisFunc(("Leave: rc=%Rrc, cbSize=%RU64, cbProcessed=%RU64 -> nBytesToRead=%RU32, cbToRead=%RU32, cbRead=%RU32\n",
245 rc, cbSize, m_cbProcessed, nBytesToRead, cbToRead, cbRead));
246
247 if (nBytesRead)
248 *nBytesRead = (ULONG)cbRead;
249
250 if (nBytesToRead != cbRead)
251 return S_FALSE;
252
253 return S_OK;
254 }
255 catch (...)
256 {
257 LogFunc(("Caught exception\n"));
258 }
259
260 return E_FAIL;
261}
262
263STDMETHODIMP VBoxClipboardWinStreamImpl::Revert(void)
264{
265 LogFlowThisFuncEnter();
266 return E_NOTIMPL;
267}
268
269STDMETHODIMP VBoxClipboardWinStreamImpl::Seek(LARGE_INTEGER nMove, DWORD dwOrigin, ULARGE_INTEGER* nNewPos)
270{
271 RT_NOREF(nMove, dwOrigin, nNewPos);
272
273 LogFlowThisFunc(("nMove=%RI64, dwOrigin=%RI32\n", nMove, dwOrigin));
274
275 return E_NOTIMPL;
276}
277
278STDMETHODIMP VBoxClipboardWinStreamImpl::SetSize(ULARGE_INTEGER nNewSize)
279{
280 RT_NOREF(nNewSize);
281
282 LogFlowThisFuncEnter();
283 return E_NOTIMPL;
284}
285
286STDMETHODIMP VBoxClipboardWinStreamImpl::Stat(STATSTG *pStatStg, DWORD dwFlags)
287{
288 HRESULT hr = S_OK;
289
290 if (pStatStg)
291 {
292 RT_BZERO(pStatStg, sizeof(STATSTG));
293
294 switch (dwFlags)
295 {
296 case STATFLAG_NONAME:
297 pStatStg->pwcsName = NULL;
298 break;
299
300 case STATFLAG_DEFAULT:
301 {
302 int rc2 = RTStrToUtf16(m_strPath.c_str(), &pStatStg->pwcsName);
303 if (RT_FAILURE(rc2))
304 hr = E_FAIL;
305 break;
306 }
307
308 default:
309 hr = STG_E_INVALIDFLAG;
310 break;
311 }
312
313 if (SUCCEEDED(hr))
314 {
315 pStatStg->type = STGTY_STREAM;
316 pStatStg->grfMode = STGM_READ;
317 pStatStg->grfLocksSupported = 0;
318 pStatStg->cbSize.QuadPart = (uint64_t)m_objInfo.cbObject;
319 }
320 }
321 else
322 hr = STG_E_INVALIDPOINTER;
323
324 LogFlowThisFunc(("hr=%Rhrc\n", hr));
325 return hr;
326}
327
328STDMETHODIMP VBoxClipboardWinStreamImpl::UnlockRegion(ULARGE_INTEGER nStart, ULARGE_INTEGER nBytes, DWORD dwFlags)
329{
330 RT_NOREF(nStart, nBytes, dwFlags);
331
332 LogFlowThisFuncEnter();
333 return E_NOTIMPL;
334}
335
336STDMETHODIMP VBoxClipboardWinStreamImpl::Write(const void *pvBuffer, ULONG nBytesToRead, ULONG *nBytesRead)
337{
338 RT_NOREF(pvBuffer, nBytesToRead, nBytesRead);
339
340 LogFlowThisFuncEnter();
341 return E_NOTIMPL;
342}
343
344/*
345 * Own stuff.
346 */
347
348/**
349 * Factory to create our own IStream implementation.
350 *
351 * @returns HRESULT
352 * @param pParent Pointer to the parent data object.
353 * @param pTransfer Pointer to URI transfer object to use.
354 * @param strPath Path of object to handle for the stream.
355 * @param pObjInfo Pointer to object information.
356 * @param ppStream Where to return the created stream object on success.
357 */
358/* static */
359HRESULT VBoxClipboardWinStreamImpl::Create(VBoxClipboardWinDataObject *pParent, PSHAREDCLIPBOARDURITRANSFER pTransfer,
360 const Utf8Str &strPath, PSHAREDCLIPBOARDFSOBJINFO pObjInfo,
361 IStream **ppStream)
362{
363 AssertPtrReturn(pTransfer, E_POINTER);
364
365 VBoxClipboardWinStreamImpl *pStream = new VBoxClipboardWinStreamImpl(pParent, pTransfer, strPath, pObjInfo);
366 if (pStream)
367 {
368 pStream->AddRef();
369
370 *ppStream = pStream;
371 return S_OK;
372 }
373
374 return E_FAIL;
375}
376
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