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 <comphelper/diagnose_ex.hxx>
24 #include <com/sun/star/datatransfer/clipboard/ClipboardEvent.hpp>
25 #include <com/sun/star/lang/DisposedException.hpp>
26 #include <com/sun/star/lang/IllegalArgumentException.hpp>
27 #include <com/sun/star/uno/XComponentContext.hpp>
28 #include <cppuhelper/supportsservice.hxx>
29 #include <cppuhelper/weak.hxx>
30 #include <vcl/svapp.hxx>
32 #include <salinst.hxx>
34 #include <com/sun/star/datatransfer/clipboard/RenderingCapabilities.hpp>
35 #include "XNotifyingDataObject.hxx"
37 #include <systools/win32/comtools.hxx>
38 #include "DtObjFactory.hxx"
39 #include "APNDataObject.hxx"
40 #include "DOTransferable.hxx"
41 #include "WinClipboard.hxx"
43 #if !defined WIN32_LEAN_AND_MEAN
44 #define WIN32_LEAN_AND_MEAN
50 using namespace com::sun::star
;
54 CWinClipboard
* s_pCWinClipbImpl
= nullptr;
55 osl::Mutex s_aClipboardSingletonMutex
;
59 CWinClipboard::CWinClipboard(const uno::Reference
<uno::XComponentContext
>& rxContext
,
60 const OUString
& aClipboardName
)
61 : m_xContext(rxContext
)
62 , m_itsName(aClipboardName
)
63 , m_pCurrentClipContent(nullptr)
65 // necessary to reassociate from
66 // the static callback function
68 osl::MutexGuard
aGuard(s_aClipboardSingletonMutex
);
69 s_pCWinClipbImpl
= this;
72 registerClipboardViewer();
75 CWinClipboard::~CWinClipboard()
78 osl::MutexGuard
aGuard(s_aClipboardSingletonMutex
);
79 s_pCWinClipbImpl
= nullptr;
82 unregisterClipboardViewer();
87 // to avoid unnecessary traffic we check first if there is a clipboard
88 // content which was set via setContent, in this case we don't need
89 // to query the content from the clipboard, create a new wrapper object
90 // and so on, we simply return the original XTransferable instead of our
93 uno::Reference
<datatransfer::XTransferable
> SAL_CALL
CWinClipboard::getContents()
95 osl::MutexGuard
aGuard(m_aContentMutex
);
98 throw lang::DisposedException("object is already disposed",
99 static_cast<XClipboardEx
*>(this));
101 // use the shortcut or create a transferable from
104 osl::MutexGuard
aGuard2(m_aContentCacheMutex
);
106 if (nullptr != m_pCurrentClipContent
)
107 return m_pCurrentClipContent
->m_XTransferable
;
110 if (m_foreignContent
.is())
111 return m_foreignContent
;
113 // release the mutex, so that the variable may be
114 // changed by other threads
117 uno::Reference
<datatransfer::XTransferable
> rClipContent
;
119 // get the current format list from clipboard
120 if (UINT nFormats
; !GetUpdatedClipboardFormats(nullptr, 0, &nFormats
)
121 && GetLastError() == ERROR_INSUFFICIENT_BUFFER
)
123 std::vector
<UINT
> aUINTFormats(nFormats
);
124 if (GetUpdatedClipboardFormats(aUINTFormats
.data(), nFormats
, &nFormats
))
126 std::vector
<sal_uInt32
> aFormats(aUINTFormats
.begin(), aUINTFormats
.end());
127 rClipContent
= new CDOTransferable(m_xContext
, this, aFormats
);
129 osl::MutexGuard
aGuard2(m_aContentCacheMutex
);
130 m_foreignContent
= rClipContent
;
137 IDataObjectPtr
CWinClipboard::getIDataObject()
139 osl::MutexGuard
aGuard(m_aContentMutex
);
142 throw lang::DisposedException("object is already disposed",
143 static_cast<XClipboardEx
*>(this));
145 // get the current dataobject from clipboard
146 IDataObjectPtr pIDataObject
;
147 HRESULT hr
= m_MtaOleClipboard
.getClipboard(&pIDataObject
);
151 // create an apartment neutral dataobject and initialize it with a
152 // com smart pointer to the IDataObject from clipboard
153 pIDataObject
= new CAPNDataObject(pIDataObject
);
159 void SAL_CALL
CWinClipboard::setContents(
160 const uno::Reference
<datatransfer::XTransferable
>& xTransferable
,
161 const uno::Reference
<datatransfer::clipboard::XClipboardOwner
>& xClipboardOwner
)
163 osl::MutexGuard
aGuard(m_aContentMutex
);
166 throw lang::DisposedException("object is already disposed",
167 static_cast<XClipboardEx
*>(this));
169 IDataObjectPtr pIDataObj
;
171 if (xTransferable
.is())
174 osl::MutexGuard
aGuard2(m_aContentCacheMutex
);
176 m_foreignContent
.clear();
178 m_pCurrentClipContent
= new CXNotifyingDataObject(
179 CDTransObjFactory::createDataObjFromTransferable(m_xContext
, xTransferable
),
180 xTransferable
, xClipboardOwner
, this);
183 pIDataObj
= IDataObjectPtr(m_pCurrentClipContent
);
186 m_MtaOleClipboard
.setClipboard(pIDataObj
.get());
189 OUString SAL_CALL
CWinClipboard::getName()
192 throw lang::DisposedException("object is already disposed",
193 static_cast<XClipboardEx
*>(this));
198 // XFlushableClipboard
200 void SAL_CALL
CWinClipboard::flushClipboard()
202 osl::MutexGuard
aGuard(m_aContentMutex
);
205 throw lang::DisposedException("object is already disposed",
206 static_cast<XClipboardEx
*>(this));
208 // actually it should be ClearableMutexGuard aGuard( m_aContentCacheMutex );
209 // but it does not work since FlushClipboard does a callback and frees DataObject
210 // which results in a deadlock in onReleaseDataObject.
211 // FlushClipboard had to be synchron in order to prevent shutdown until all
212 // clipboard-formats are rendered.
213 // The request is needed to prevent flushing if we are not clipboard owner (it is
214 // not known what happens if we flush but aren't clipboard owner).
215 // It may be possible to move the request to the clipboard STA thread by saving the
216 // DataObject and call OleIsCurrentClipboard before flushing.
218 if (nullptr != m_pCurrentClipContent
)
219 m_MtaOleClipboard
.flushClipboard();
224 sal_Int8 SAL_CALL
CWinClipboard::getRenderingCapabilities()
227 throw lang::DisposedException("object is already disposed",
228 static_cast<XClipboardEx
*>(this));
230 using namespace datatransfer::clipboard::RenderingCapabilities
;
231 return (Delayed
| Persistent
);
234 // XClipboardNotifier
236 void SAL_CALL
CWinClipboard::addClipboardListener(
237 const uno::Reference
<datatransfer::clipboard::XClipboardListener
>& listener
)
240 throw lang::DisposedException("object is already disposed",
241 static_cast<XClipboardEx
*>(this));
243 // check input parameter
245 throw lang::IllegalArgumentException("empty reference", static_cast<XClipboardEx
*>(this),
248 std::unique_lock
aGuard(m_aMutex
);
249 maClipboardListeners
.addInterface(aGuard
, listener
);
252 void SAL_CALL
CWinClipboard::removeClipboardListener(
253 const uno::Reference
<datatransfer::clipboard::XClipboardListener
>& listener
)
256 throw lang::DisposedException("object is already disposed",
257 static_cast<XClipboardEx
*>(this));
259 // check input parameter
261 throw lang::IllegalArgumentException("empty reference", static_cast<XClipboardEx
*>(this),
264 std::unique_lock
aGuard(m_aMutex
);
265 maClipboardListeners
.removeInterface(aGuard
, listener
);
268 void CWinClipboard::notifyAllClipboardListener()
273 std::unique_lock
aGuard(m_aMutex
);
277 if (!maClipboardListeners
.getLength(aGuard
))
282 uno::Reference
<datatransfer::XTransferable
> rXTransf(getContents());
283 datatransfer::clipboard::ClipboardEvent
aClipbEvent(static_cast<XClipboard
*>(this),
285 maClipboardListeners
.notifyEach(
286 aGuard
, &datatransfer::clipboard::XClipboardListener::changedContents
, aClipbEvent
);
288 catch (const lang::DisposedException
&)
290 OSL_FAIL("Service Manager disposed");
293 // no further clipboard changed notifications
294 unregisterClipboardViewer();
300 OUString SAL_CALL
CWinClipboard::getImplementationName()
302 return "com.sun.star.datatransfer.clipboard.ClipboardW32";
305 sal_Bool SAL_CALL
CWinClipboard::supportsService(const OUString
& ServiceName
)
307 return cppu::supportsService(this, ServiceName
);
310 uno::Sequence
<OUString
> SAL_CALL
CWinClipboard::getSupportedServiceNames()
312 return { "com.sun.star.datatransfer.clipboard.SystemClipboard" };
315 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
316 dtrans_CWinClipboard_get_implementation(css::uno::XComponentContext
* context
,
317 css::uno::Sequence
<css::uno::Any
> const& args
)
319 // We run unit tests in parallel, which is a problem when touching a shared resource
320 // like the system clipboard, so rather use the dummy GenericClipboard.
321 static const bool bRunningUnitTest
= getenv("LO_TESTNAME");
323 if (bRunningUnitTest
)
325 SolarMutexGuard aGuard
;
326 auto xClipboard
= ImplGetSVData()->mpDefInst
->CreateClipboard(args
);
328 xClipboard
->acquire();
329 return xClipboard
.get();
333 return cppu::acquire(new CWinClipboard(context
, ""));
337 void CWinClipboard::onReleaseDataObject(CXNotifyingDataObject
* theCaller
)
339 OSL_ASSERT(nullptr != theCaller
);
342 theCaller
->lostOwnership();
344 // if the current caller is the one we currently hold, then set it to NULL
345 // because an external source must be the clipboardowner now
346 osl::MutexGuard
aGuard(m_aContentCacheMutex
);
348 if (m_pCurrentClipContent
== theCaller
)
349 m_pCurrentClipContent
= nullptr;
352 void CWinClipboard::registerClipboardViewer()
354 m_MtaOleClipboard
.registerClipViewer(CWinClipboard::onClipboardContentChanged
);
357 void CWinClipboard::unregisterClipboardViewer() { m_MtaOleClipboard
.registerClipViewer(nullptr); }
359 void WINAPI
CWinClipboard::onClipboardContentChanged()
361 osl::MutexGuard
aGuard(s_aClipboardSingletonMutex
);
363 // reassociation to instance through static member
364 if (nullptr != s_pCWinClipbImpl
)
366 s_pCWinClipbImpl
->m_foreignContent
.clear();
367 s_pCWinClipbImpl
->notifyAllClipboardListener();
371 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */