1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <osl/diagnose.h>
23 #include <com/sun/star/datatransfer/clipboard/ClipboardEvent.hpp>
24 #include <com/sun/star/lang/DisposedException.hpp>
25 #include <com/sun/star/lang/IllegalArgumentException.hpp>
26 #include <com/sun/star/uno/XComponentContext.hpp>
27 #include <cppuhelper/supportsservice.hxx>
28 #include <cppuhelper/weak.hxx>
30 #include <com/sun/star/datatransfer/clipboard/RenderingCapabilities.hpp>
31 #include "XNotifyingDataObject.hxx"
33 #include <systools/win32/comtools.hxx>
34 #include "DtObjFactory.hxx"
35 #include "APNDataObject.hxx"
36 #include "DOTransferable.hxx"
37 #include "WinClipboard.hxx"
39 #if !defined WIN32_LEAN_AND_MEAN
40 #define WIN32_LEAN_AND_MEAN
46 using namespace com::sun::star
;
48 // definition of static members
49 CWinClipboard
* CWinClipboard::s_pCWinClipbImpl
= nullptr;
50 osl::Mutex
CWinClipboard::s_aMutex
;
53 CWinClipboard::CWinClipboard(const uno::Reference
<uno::XComponentContext
>& rxContext
,
54 const OUString
& aClipboardName
)
55 : WeakComponentImplHelper
<XSystemClipboard
, XFlushableClipboard
, XServiceInfo
>(
57 , m_xContext(rxContext
)
58 , m_itsName(aClipboardName
)
59 , m_pCurrentClipContent(nullptr)
61 // necessary to reassociate from
62 // the static callback function
63 s_pCWinClipbImpl
= this;
64 registerClipboardViewer();
67 CWinClipboard::~CWinClipboard()
70 osl::MutexGuard
aGuard(s_aMutex
);
71 s_pCWinClipbImpl
= nullptr;
74 unregisterClipboardViewer();
79 // to avoid unnecessary traffic we check first if there is a clipboard
80 // content which was set via setContent, in this case we don't need
81 // to query the content from the clipboard, create a new wrapper object
82 // and so on, we simply return the original XTransferable instead of our
85 uno::Reference
<datatransfer::XTransferable
> SAL_CALL
CWinClipboard::getContents()
87 osl::MutexGuard
aGuard(m_aMutex
);
89 if (rBHelper
.bDisposed
)
90 throw lang::DisposedException("object is already disposed",
91 static_cast<XClipboardEx
*>(this));
93 // use the shortcut or create a transferable from
96 osl::MutexGuard
aGuard2(m_ClipContentMutex
);
98 if (nullptr != m_pCurrentClipContent
)
99 return m_pCurrentClipContent
->m_XTransferable
;
102 if (m_foreignContent
.is())
103 return m_foreignContent
;
105 // release the mutex, so that the variable may be
106 // changed by other threads
109 uno::Reference
<datatransfer::XTransferable
> rClipContent
;
111 // get the current format list from clipboard
112 if (UINT nFormats
; !GetUpdatedClipboardFormats(nullptr, 0, &nFormats
)
113 && GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
115 std::vector
<UINT
> aUINTFormats(nFormats
);
116 if (GetUpdatedClipboardFormats(aUINTFormats
.data(), nFormats
, &nFormats
))
118 std::vector
<sal_uInt32
> aFormats(aUINTFormats
.begin(), aUINTFormats
.end());
119 rClipContent
= new CDOTransferable(m_xContext
, this, aFormats
);
121 osl::MutexGuard
aGuard2(m_ClipContentMutex
);
122 m_foreignContent
= rClipContent
;
129 IDataObjectPtr
CWinClipboard::getIDataObject()
131 osl::MutexGuard
aGuard(m_aMutex
);
133 if (rBHelper
.bDisposed
)
134 throw lang::DisposedException("object is already disposed",
135 static_cast<XClipboardEx
*>(this));
137 // get the current dataobject from clipboard
138 IDataObjectPtr pIDataObject
;
139 HRESULT hr
= m_MtaOleClipboard
.getClipboard(&pIDataObject
);
143 // create an apartment neutral dataobject and initialize it with a
144 // com smart pointer to the IDataObject from clipboard
145 pIDataObject
= new CAPNDataObject(pIDataObject
);
151 void SAL_CALL
CWinClipboard::setContents(
152 const uno::Reference
<datatransfer::XTransferable
>& xTransferable
,
153 const uno::Reference
<datatransfer::clipboard::XClipboardOwner
>& xClipboardOwner
)
155 osl::MutexGuard
aGuard(m_aMutex
);
157 if (rBHelper
.bDisposed
)
158 throw lang::DisposedException("object is already disposed",
159 static_cast<XClipboardEx
*>(this));
161 IDataObjectPtr pIDataObj
;
163 if (xTransferable
.is())
166 osl::MutexGuard
aGuard2(m_ClipContentMutex
);
168 m_foreignContent
.clear();
170 m_pCurrentClipContent
= new CXNotifyingDataObject(
171 CDTransObjFactory::createDataObjFromTransferable(m_xContext
, xTransferable
),
172 xTransferable
, xClipboardOwner
, this);
175 pIDataObj
= IDataObjectPtr(m_pCurrentClipContent
);
178 m_MtaOleClipboard
.setClipboard(pIDataObj
.get());
181 OUString SAL_CALL
CWinClipboard::getName()
183 if (rBHelper
.bDisposed
)
184 throw lang::DisposedException("object is already disposed",
185 static_cast<XClipboardEx
*>(this));
190 // XFlushableClipboard
192 void SAL_CALL
CWinClipboard::flushClipboard()
194 osl::MutexGuard
aGuard(m_aMutex
);
196 if (rBHelper
.bDisposed
)
197 throw lang::DisposedException("object is already disposed",
198 static_cast<XClipboardEx
*>(this));
200 // actually it should be ClearableMutexGuard aGuard( m_ClipContentMutex );
201 // but it does not work since FlushClipboard does a callback and frees DataObject
202 // which results in a deadlock in onReleaseDataObject.
203 // FlushClipboard had to be synchron in order to prevent shutdown until all
204 // clipboard-formats are rendered.
205 // The request is needed to prevent flushing if we are not clipboard owner (it is
206 // not known what happens if we flush but aren't clipboard owner).
207 // It may be possible to move the request to the clipboard STA thread by saving the
208 // DataObject and call OleIsCurrentClipboard before flushing.
210 if (nullptr != m_pCurrentClipContent
)
211 m_MtaOleClipboard
.flushClipboard();
216 sal_Int8 SAL_CALL
CWinClipboard::getRenderingCapabilities()
218 if (rBHelper
.bDisposed
)
219 throw lang::DisposedException("object is already disposed",
220 static_cast<XClipboardEx
*>(this));
222 using namespace datatransfer::clipboard::RenderingCapabilities
;
223 return (Delayed
| Persistant
);
226 // XClipboardNotifier
228 void SAL_CALL
CWinClipboard::addClipboardListener(
229 const uno::Reference
<datatransfer::clipboard::XClipboardListener
>& listener
)
231 if (rBHelper
.bDisposed
)
232 throw lang::DisposedException("object is already disposed",
233 static_cast<XClipboardEx
*>(this));
235 // check input parameter
237 throw lang::IllegalArgumentException("empty reference", static_cast<XClipboardEx
*>(this),
240 rBHelper
.aLC
.addInterface(cppu::UnoType
<decltype(listener
)>::get(), listener
);
243 void SAL_CALL
CWinClipboard::removeClipboardListener(
244 const uno::Reference
<datatransfer::clipboard::XClipboardListener
>& listener
)
246 if (rBHelper
.bDisposed
)
247 throw lang::DisposedException("object is already disposed",
248 static_cast<XClipboardEx
*>(this));
250 // check input parameter
252 throw lang::IllegalArgumentException("empty reference", static_cast<XClipboardEx
*>(this),
255 rBHelper
.aLC
.removeInterface(cppu::UnoType
<decltype(listener
)>::get(), listener
);
258 void CWinClipboard::notifyAllClipboardListener()
260 if (rBHelper
.bDisposed
)
263 osl::ClearableMutexGuard
aGuard(rBHelper
.rMutex
);
264 if (rBHelper
.bDisposed
)
268 cppu::OInterfaceContainerHelper
* pICHelper
= rBHelper
.aLC
.getContainer(
269 cppu::UnoType
<datatransfer::clipboard::XClipboardListener
>::get());
275 cppu::OInterfaceIteratorHelper
iter(*pICHelper
);
276 uno::Reference
<datatransfer::XTransferable
> rXTransf(getContents());
277 datatransfer::clipboard::ClipboardEvent
aClipbEvent(static_cast<XClipboard
*>(this),
280 while (iter
.hasMoreElements())
284 uno::Reference
<datatransfer::clipboard::XClipboardListener
> xCBListener(
285 iter
.next(), uno::UNO_QUERY
);
286 if (xCBListener
.is())
287 xCBListener
->changedContents(aClipbEvent
);
289 catch (uno::RuntimeException
&)
291 OSL_FAIL("RuntimeException caught");
295 catch (const lang::DisposedException
&)
297 OSL_FAIL("Service Manager disposed");
299 // no further clipboard changed notifications
300 unregisterClipboardViewer();
306 OUString SAL_CALL
CWinClipboard::getImplementationName()
308 return "com.sun.star.datatransfer.clipboard.ClipboardW32";
311 sal_Bool SAL_CALL
CWinClipboard::supportsService(const OUString
& ServiceName
)
313 return cppu::supportsService(this, ServiceName
);
316 uno::Sequence
<OUString
> SAL_CALL
CWinClipboard::getSupportedServiceNames()
318 return { "com.sun.star.datatransfer.clipboard.SystemClipboard" };
321 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
322 dtrans_CWinClipboard_get_implementation(css::uno::XComponentContext
* context
,
323 css::uno::Sequence
<css::uno::Any
> const&)
325 return cppu::acquire(static_cast<cppu::OWeakObject
*>(new CWinClipboard(context
, "")));
328 void CWinClipboard::onReleaseDataObject(CXNotifyingDataObject
* theCaller
)
330 OSL_ASSERT(nullptr != theCaller
);
333 theCaller
->lostOwnership();
335 // if the current caller is the one we currently hold, then set it to NULL
336 // because an external source must be the clipboardowner now
337 osl::MutexGuard
aGuard(m_ClipContentMutex
);
339 if (m_pCurrentClipContent
== theCaller
)
340 m_pCurrentClipContent
= nullptr;
343 void CWinClipboard::registerClipboardViewer()
345 m_MtaOleClipboard
.registerClipViewer(CWinClipboard::onClipboardContentChanged
);
348 void CWinClipboard::unregisterClipboardViewer() { m_MtaOleClipboard
.registerClipViewer(nullptr); }
350 void WINAPI
CWinClipboard::onClipboardContentChanged()
352 osl::MutexGuard
aGuard(s_aMutex
);
354 // reassociation to instance through static member
355 if (nullptr != s_pCWinClipbImpl
)
357 s_pCWinClipbImpl
->m_foreignContent
.clear();
358 s_pCWinClipbImpl
->notifyAllClipboardListener();
362 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */