Debian Codegraph @

ucb/source/ucp/webdav-curl/SerfLockStore.cxx

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20 #include <chrono>
21 #include <rtl/ustring.hxx>
22 #include <sal/log.hxx>
23 #include <osl/time.h>
24 #include <osl/thread.hxx>
25 #include <salhelper/thread.hxx>
26 #include <condition_variable>
27
28 #include <com/sun/star/ucb/LockScope.hpp>
29 #include <thread>
30
31 #include "CurlSession.hxx"
32 #include "SerfLockStore.hxx"
33
34 using namespace http_dav_ucp;
35
36 namespace http_dav_ucp {
37
38 class TickerThread : public salhelper::Thread
39 {
40 bool m_bFinish;
41 SerfLockStore & m_rLockStore;
42
43 public:
44
45 explicit TickerThread( SerfLockStore & rLockStore )
46 : Thread( "WebDavTickerThread" ), m_bFinish( false ),
47 m_rLockStore( rLockStore ) {}
48
49 void finish()
50 {
51 m_bFinish = true;
52 m_rLockStore.m_aCondition.notify_all(); // Wake up the TickerThread
53 }
54
55 private:
56
57 virtual void execute();
58 };
59
60 } // namespace http_dav_ucp
61
62
63 void TickerThread::execute()
64 {
65 osl_setThreadName("http_dav_ucp::TickerThread");
66 SAL_INFO("ucb.ucp.webdav", "TickerThread: start.");
67
68 std::unique_lock aGuard(m_rLockStore.m_aMutex);
69
70 while (!m_bFinish)
71 {
72 auto sleep_duration = m_rLockStore.refreshLocks(aGuard);
73
74 if (sleep_duration == std::chrono::milliseconds::max())
75 {
76 // Wait until a lock is added or shutdown
77 m_rLockStore.m_aCondition.wait(aGuard,
78 [this] { return !m_rLockStore.m_aLockInfoMap.empty() || m_bFinish; });
79 }
80 else
81 {
82 // Wait until the next deadline or a notification
83 // coverity[wait_not_in_locked_loop : SUPPRESS] - a spurious wakeup is ok
84 m_rLockStore.m_aCondition.wait_for(aGuard, sleep_duration);
85 }
86 }
87
88 SAL_INFO("ucb.ucp.webdav", "TickerThread: stop.");
89 }
90
91
92 SerfLockStore::SerfLockStore()
93 {
94 }
95
96
97 SerfLockStore::~SerfLockStore()
98 {
99 std::unique_lock aGuard(m_aMutex);
100 stopTicker(aGuard);
101 assert(aGuard.owns_lock()); // actually no threads should even try to access members now
102
103 // release active locks, if any.
104 SAL_WARN_IF( !m_aLockInfoMap.empty(), "ucb.ucp.webdav",
105 "SerfLockStore::~SerfLockStore - Releasing active locks!" );
106
107 for ( auto& rLockInfo : m_aLockInfoMap )
108 {
109 rLockInfo.second.m_xSession->NonInteractive_UNLOCK(rLockInfo.first);
110 }
111 }
112
113 void SerfLockStore::startTicker(std::unique_lock<std::mutex> & /* rGuard is held */)
114 {
115 if ( !m_pTickerThread.is() )
116 {
117 m_pTickerThread = new TickerThread( *this );
118 m_pTickerThread->launch();
119 }
120 }
121
122 void SerfLockStore::stopTicker(std::unique_lock<std::mutex> & rGuard)
123 {
124 rtl::Reference<TickerThread> pTickerThread;
125
126 if (m_pTickerThread.is())
127 {
128 m_pTickerThread->finish(); // needs mutex
129 // the TickerThread may run refreshLocks() at most once after this
130 pTickerThread = m_pTickerThread;
131
132 m_pTickerThread.clear();
133 }
134
135 rGuard.unlock();
136
137 if (pTickerThread.is() && pTickerThread->getIdentifier() != osl::Thread::getCurrentIdentifier())
138 {
139 pTickerThread->join(); // without m_aMutex locked (to prevent deadlock)
140 }
141
142 rGuard.lock();
143 }
144
145 bool SerfLockStore::joinThreads()
146 {
147 std::unique_lock aGuard(m_aMutex);
148 // FIXME: cure could be worse than the problem; we don't
149 // want to block on a long-standing webdav lock refresh request.
150 // perhaps we should timeout on a condition instead if a request
151 // is in progress.
152 if (m_pTickerThread.is())
153 stopTicker(aGuard);
154 return true;
155 }
156
157 void SerfLockStore::startThreads()
158 {
159 std::unique_lock aGuard( m_aMutex );
160 if (!m_aLockInfoMap.empty())
161 startTicker(aGuard);
162 }
163
164 OUString const*
165 SerfLockStore::getLockTokenForURI(OUString const& rURI, css::ucb::Lock const*const pLock)
166 {
167 assert(rURI.startsWith("http://") || rURI.startsWith("https://"));
168
169 std::unique_lock aGuard( m_aMutex );
170
171 auto const it(m_aLockInfoMap.find(rURI));
172
173 if (it == m_aLockInfoMap.end())
174 {
175 return nullptr;
176 }
177 if (!pLock) // any lock will do
178 {
179 return &it->second.m_sToken;
180 }
181 // 0: EXCLUSIVE 1: SHARED
182 if (it->second.m_Lock.Scope == ucb::LockScope_SHARED && pLock->Scope == ucb::LockScope_EXCLUSIVE)
183 {
184 return nullptr;
185 }
186 assert(it->second.m_Lock.Type == pLock->Type); // only WRITE possible
187 if (it->second.m_Lock.Depth < pLock->Depth)
188 {
189 return nullptr;
190 }
191 // Only own locks are expected in the lock store, but depending on the
192 // server it->second.m_Lock.Owner may contain the string this UCP passed in
193 // the LOCK request, or a user identifier generated by the server (happens
194 // with Sharepoint), so just ignore it here.
195 // ignore Timeout ?
196 return &it->second.m_sToken;
197 }
198
199 void SerfLockStore::addLock(const OUString& rURI, ucb::Lock const& rLock, const OUString& sToken,
200 rtl::Reference<CurlSession> const& xSession,
201 sal_Int32 nLastChanceToSendRefreshRequest)
202 {
203 assert(rURI.startsWith("http://") || rURI.startsWith("https://"));
204 std::unique_lock aGuard(m_aMutex);
205
206 m_aLockInfoMap[rURI] = LockInfo(sToken, rLock, xSession, nLastChanceToSendRefreshRequest);
207 m_aCondition.notify_all(); // Wake up the TickerThread
208
209 startTicker(aGuard);
210 }
211
212
213 void SerfLockStore::removeLock(const OUString& rURI)
214 {
215 std::unique_lock aGuard( m_aMutex );
216
217 removeLockImpl(aGuard, rURI);
218 }
219
220 void SerfLockStore::removeLockImpl(std::unique_lock<std::mutex> & rGuard, const OUString& rURI)
221 {
222 assert(rURI.startsWith("http://") || rURI.startsWith("https://"));
223
224 m_aLockInfoMap.erase(rURI);
225
226 if ( m_aLockInfoMap.empty() )
227 {
228 stopTicker(rGuard);
229 }
230 }
231
232 std::chrono::milliseconds SerfLockStore::refreshLocks(std::unique_lock<std::mutex>& rGuard)
233 {
234 assert(rGuard.owns_lock());
235 (void)rGuard;
236
237 TimeValue currentTimeVal;
238 osl_getSystemTime(&currentTimeVal);
239 sal_Int32 currentTime = currentTimeVal.Seconds;
240
241 ::std::vector<OUString> authFailedLocks;
242 std::chrono::milliseconds min_remaining = std::chrono::milliseconds::max();
243
244 for ( auto& rLockInfo : m_aLockInfoMap )
245 {
246 LockInfo & rInfo = rLockInfo.second;
247 if ( rInfo.m_nLastChanceToSendRefreshRequest != -1 )
248 {
249 // 30 seconds or less remaining until lock expires?
250 sal_Int32 deadline = rInfo.m_nLastChanceToSendRefreshRequest - 30;
251 if ( deadline <= currentTime )
252 {
253 // refresh the lock.
254 sal_Int32 nlastChanceToSendRefreshRequest = -1;
255 bool isAuthFailed(false);
256 if (rInfo.m_xSession->NonInteractive_LOCK(
257 rLockInfo.first, rLockInfo.second.m_sToken,
258 nlastChanceToSendRefreshRequest,
259 isAuthFailed))
260 {
261 rInfo.m_nLastChanceToSendRefreshRequest
262 = nlastChanceToSendRefreshRequest;
263 }
264 else
265 {
266 if (isAuthFailed)
267 {
268 authFailedLocks.push_back(rLockInfo.first);
269 }
270 // refresh failed. stop auto-refresh.
271 rInfo.m_nLastChanceToSendRefreshRequest = -1;
272 }
273 }
274 if (rInfo.m_nLastChanceToSendRefreshRequest != -1)
275 {
276 sal_Int32 remaining = (rInfo.m_nLastChanceToSendRefreshRequest - 30) - currentTime;
277 if (remaining > 0)
278 {
279 auto remaining_ms = std::chrono::seconds(remaining);
280 if ( remaining_ms < min_remaining )
281 min_remaining
282 = std::chrono::duration_cast<std::chrono::milliseconds>(remaining_ms);
283 }
284 }
285 }
286 }
287
288 for (auto const& rLock : authFailedLocks)
289 {
290 removeLockImpl(rGuard, rLock);
291 }
292
293 return min_remaining;
294 }
295
296 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */