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 maInputStreamData
[nDataKey
] = tData(pThreadConsumer
);
43 // prepare next data key value
44 if ( snNextKeyValue
< SAL_MAX_UINT64
)
56 void SwRetrievedInputStreamDataManager::PushData(
57 const tDataKey nDataKey
,
58 css::uno::Reference
<css::io::XInputStream
> const & xInputStream
,
59 const bool bIsStreamReadOnly
)
61 std::unique_lock
aGuard(maMutex
);
63 std::map
< tDataKey
, tData
>::iterator aIter
= maInputStreamData
.find( nDataKey
);
65 if ( aIter
== maInputStreamData
.end() )
68 // Fill data container.
69 (*aIter
).second
.mxInputStream
= xInputStream
;
70 (*aIter
).second
.mbIsStreamReadOnly
= bIsStreamReadOnly
;
72 // post user event to process the retrieved input stream data
76 tDataKey
* pDataKey
= new tDataKey
;
78 Application::PostUserEvent( LINK( this, SwRetrievedInputStreamDataManager
, LinkedInputStreamReady
), pDataKey
);
82 // no application available -> discard data
83 maInputStreamData
.erase( aIter
);
87 bool SwRetrievedInputStreamDataManager::PopData( const tDataKey nDataKey
,
90 std::unique_lock
aGuard(maMutex
);
92 bool bDataProvided( false );
94 std::map
< tDataKey
, tData
>::iterator aIter
= maInputStreamData
.find( nDataKey
);
96 if ( aIter
!= maInputStreamData
.end() )
98 rData
.mpThreadConsumer
= (*aIter
).second
.mpThreadConsumer
;
99 rData
.mxInputStream
= (*aIter
).second
.mxInputStream
;
100 rData
.mbIsStreamReadOnly
= (*aIter
).second
.mbIsStreamReadOnly
;
102 maInputStreamData
.erase( aIter
);
104 bDataProvided
= true;
107 return bDataProvided
;
110 /** callback function, which is triggered by input stream data manager on
111 filling of the data container to provide retrieved input stream to the
112 thread Consumer using <Application::PostUserEvent(..)>
115 Note: This method has to be run in the main thread.
117 IMPL_STATIC_LINK( SwRetrievedInputStreamDataManager
, LinkedInputStreamReady
,
120 SwRetrievedInputStreamDataManager::tDataKey
* pDataKey
= static_cast<SwRetrievedInputStreamDataManager::tDataKey
*>(p
);
126 SwRetrievedInputStreamDataManager
& rDataManager
=
127 SwRetrievedInputStreamDataManager::GetManager();
128 SwRetrievedInputStreamDataManager::tData aInputStreamData
;
129 if ( rDataManager
.PopData( *pDataKey
, aInputStreamData
) )
131 std::shared_ptr
< SwAsyncRetrieveInputStreamThreadConsumer
> pThreadConsumer
=
132 aInputStreamData
.mpThreadConsumer
.lock();
133 if ( pThreadConsumer
)
135 pThreadConsumer
->ApplyInputStream( aInputStreamData
.mxInputStream
,
136 aInputStreamData
.mbIsStreamReadOnly
);
142 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */