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 <retrievedinputstreamdata.hxx>
21 #include <retrieveinputstreamconsumer.hxx>
22 #include <vcl/svapp.hxx>
26 SwRetrievedInputStreamDataManager::tDataKey
SwRetrievedInputStreamDataManager::snNextKeyValue
= 1;
28 SwRetrievedInputStreamDataManager
& SwRetrievedInputStreamDataManager::GetManager()
30 static SwRetrievedInputStreamDataManager theSwRetrievedInputStreamDataManager
;
31 return theSwRetrievedInputStreamDataManager
;
34 SwRetrievedInputStreamDataManager::tDataKey
SwRetrievedInputStreamDataManager::ReserveData(
35 std::weak_ptr
< SwAsyncRetrieveInputStreamThreadConsumer
> const & pThreadConsumer
)
37 std::unique_lock
aGuard(maMutex
);
39 // create empty data container for given thread Consumer
40 tDataKey
nDataKey( snNextKeyValue
);
41 tData
aNewEntry( pThreadConsumer
);
42 maInputStreamData
[ nDataKey
] = aNewEntry
;
44 // prepare next data key value
45 if ( snNextKeyValue
< SAL_MAX_UINT64
)
57 void SwRetrievedInputStreamDataManager::PushData(
58 const tDataKey nDataKey
,
59 css::uno::Reference
<css::io::XInputStream
> const & xInputStream
,
60 const bool bIsStreamReadOnly
)
62 std::unique_lock
aGuard(maMutex
);
64 std::map
< tDataKey
, tData
>::iterator aIter
= maInputStreamData
.find( nDataKey
);
66 if ( aIter
== maInputStreamData
.end() )
69 // Fill data container.
70 (*aIter
).second
.mxInputStream
= xInputStream
;
71 (*aIter
).second
.mbIsStreamReadOnly
= bIsStreamReadOnly
;
73 // post user event to process the retrieved input stream data
77 tDataKey
* pDataKey
= new tDataKey
;
79 Application::PostUserEvent( LINK( this, SwRetrievedInputStreamDataManager
, LinkedInputStreamReady
), pDataKey
);
83 // no application available -> discard data
84 maInputStreamData
.erase( aIter
);
88 bool SwRetrievedInputStreamDataManager::PopData( const tDataKey nDataKey
,
91 std::unique_lock
aGuard(maMutex
);
93 bool bDataProvided( false );
95 std::map
< tDataKey
, tData
>::iterator aIter
= maInputStreamData
.find( nDataKey
);
97 if ( aIter
!= maInputStreamData
.end() )
99 rData
.mpThreadConsumer
= (*aIter
).second
.mpThreadConsumer
;
100 rData
.mxInputStream
= (*aIter
).second
.mxInputStream
;
101 rData
.mbIsStreamReadOnly
= (*aIter
).second
.mbIsStreamReadOnly
;
103 maInputStreamData
.erase( aIter
);
105 bDataProvided
= true;
108 return bDataProvided
;
111 /** callback function, which is triggered by input stream data manager on
112 filling of the data container to provide retrieved input stream to the
113 thread Consumer using <Application::PostUserEvent(..)>
116 Note: This method has to be run in the main thread.
118 IMPL_STATIC_LINK( SwRetrievedInputStreamDataManager
, LinkedInputStreamReady
,
121 SwRetrievedInputStreamDataManager::tDataKey
* pDataKey
= static_cast<SwRetrievedInputStreamDataManager::tDataKey
*>(p
);
127 SwRetrievedInputStreamDataManager
& rDataManager
=
128 SwRetrievedInputStreamDataManager::GetManager();
129 SwRetrievedInputStreamDataManager::tData aInputStreamData
;
130 if ( rDataManager
.PopData( *pDataKey
, aInputStreamData
) )
132 std::shared_ptr
< SwAsyncRetrieveInputStreamThreadConsumer
> pThreadConsumer
=
133 aInputStreamData
.mpThreadConsumer
.lock();
134 if ( pThreadConsumer
)
136 pThreadConsumer
->ApplyInputStream( aInputStreamData
.mxInputStream
,
137 aInputStreamData
.mbIsStreamReadOnly
);
143 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */