LibreOffice Module svl (master) 1
syscreds.cxx
Go to the documentation of this file.
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 "syscreds.hxx"
21#include <osl/diagnose.h>
23
24using namespace com::sun::star;
25
27 SysCredentialsConfig * pOwner )
28: utl::ConfigItem( "Office.Common/Passwords", ConfigItemMode::NONE ),
29 m_bInited( false ),
31{
32 uno::Sequence<OUString> aNode { "Office.Common/Passwords/AuthenticateUsingSystemCredentials" };
33 EnableNotification( aNode );
34}
35
36//virtual
38 const uno::Sequence< OUString > & /*seqPropertyNames*/ )
39{
40 {
41 std::unique_lock aGuard( m_aMutex );
42 m_bInited = false;
43 // rebuild m_seqURLs
45 }
47}
48
50{
51 // does nothing
52}
53
54uno::Sequence< OUString >
56{
57 std::unique_lock aGuard(m_aMutex);
58 return getSystemCredentialsURLs(aGuard);
59}
60
61uno::Sequence< OUString >
62SysCredentialsConfigItem::getSystemCredentialsURLs(std::unique_lock<std::mutex>& /*rGuard*/)
63{
64 if ( !m_bInited )
65 {
66 // read config item
67 uno::Sequence<OUString> aPropNames { "AuthenticateUsingSystemCredentials" };
68 uno::Sequence< uno::Any > aAnyValues(
70
71 OSL_ENSURE(
72 aAnyValues.getLength() == 1,
73 "SysCredentialsConfigItem::getSystemCredentialsURLs: "
74 "Error reading config item!" );
75
76 uno::Sequence< OUString > aValues;
77 if ( ( aAnyValues[ 0 ] >>= aValues ) ||
78 ( !aAnyValues[ 0 ].hasValue() ) )
79 {
80 m_seqURLs = aValues;
81 m_bInited = true;
82 }
83 }
84 return m_seqURLs;
85}
86
88 const uno::Sequence< OUString > & seqURLList )
89{
90 // write config item.
91 uno::Sequence< OUString > aPropNames{ "AuthenticateUsingSystemCredentials" };
92 uno::Sequence< uno::Any > aPropValues{ uno::Any(seqURLList) };
93
96
97 std::unique_lock aGuard( m_aMutex );
98
99 m_seqURLs = seqURLList;
100 m_bInited = true;
101}
102
103
104namespace
105{
106 // TODO: This code is actually copied from svl/source/passwordcontainer.cxx
107 bool removeLastSegment( OUString & aURL )
108 {
109 sal_Int32 aInd = aURL.lastIndexOf( '/' );
110
111 if( aInd > 0 )
112 {
113 sal_Int32 aPrevInd = aURL.lastIndexOf( '/', aInd );
114 if ( aURL.indexOf( "://" ) != aPrevInd - 2 ||
115 aInd != aURL.getLength() - 1 )
116 {
117 aURL = aURL.copy( 0, aInd );
118 return true;
119 }
120 }
121
122 return false;
123 }
124
125 bool findURL( std::set<OUString> const & rContainer, OUString const & aURL, OUString & aResult )
126 {
127 // TODO: This code is actually copied from svl/source/passwordcontainer.cxx
128 if( !rContainer.empty() && !aURL.isEmpty() )
129 {
130 OUString aUrl( aURL );
131
132 // each iteration remove last '/...' section from the aUrl
133 // while it's possible, up to the most left '://'
134 do
135 {
136 // first look for <url>/somename and then look for <url>/somename/...
137 auto aIter = rContainer.find( aUrl );
138 if( aIter != rContainer.end() )
139 {
140 aResult = *aIter;
141 return true;
142 }
143 else
144 {
145 OUString tmpUrl( aUrl );
146 if ( !tmpUrl.endsWith("/") )
147 tmpUrl += "/";
148
149 aIter = rContainer.lower_bound( tmpUrl );
150 if( aIter != rContainer.end() && aIter->match( tmpUrl ) )
151 {
152 aResult = *aIter;
153 return true;
154 }
155 }
156 }
157 while( removeLastSegment( aUrl ) && !aUrl.isEmpty() );
158 }
159 aResult.clear();
160 return false;
161 }
162
163} // namespace
164
166: m_aConfigItem( this ),
167 m_bCfgInited( false )
168{
169}
170
171void SysCredentialsConfig::initCfg(std::unique_lock<std::mutex>& /*rGuard*/)
172{
173 if ( !m_bCfgInited )
174 {
175 const uno::Sequence< OUString > aURLs(
177 m_aCfgContainer.insert( aURLs.begin(), aURLs.end() );
178 m_bCfgInited = true;
179 }
180}
181
182void SysCredentialsConfig::writeCfg(std::unique_lock<std::mutex>& /*rGuard*/)
183{
184 OSL_ENSURE( m_bCfgInited, "SysCredentialsConfig::writeCfg : not initialized!" );
185
187}
188
189OUString SysCredentialsConfig::find( OUString const & aURL )
190{
191 std::unique_lock aGuard( m_aMutex );
192 OUString aResult;
193 if ( findURL( m_aMemContainer, aURL, aResult ) )
194 return aResult;
195
196 initCfg(aGuard);
197 if ( findURL( m_aCfgContainer, aURL, aResult ) )
198 return aResult;
199
200 return OUString();
201}
202
203void SysCredentialsConfig::add( OUString const & rURL, bool bPersistent )
204{
205 std::unique_lock aGuard( m_aMutex );
206
207 if ( bPersistent )
208 {
209 m_aMemContainer.erase( rURL );
210
211 initCfg(aGuard);
212 m_aCfgContainer.insert( rURL );
213 writeCfg(aGuard);
214 }
215 else
216 {
217 initCfg(aGuard);
218 if ( m_aCfgContainer.erase( rURL ) > 0 )
219 writeCfg(aGuard);
220
221 m_aMemContainer.insert( rURL );
222 }
223}
224
225void SysCredentialsConfig::remove( OUString const & rURL )
226{
227 std::unique_lock aGuard(m_aMutex);
228
229 m_aMemContainer.erase( rURL );
230
231 initCfg(aGuard);
232 if ( m_aCfgContainer.erase( rURL ) > 0 )
233 writeCfg(aGuard);
234}
235
236uno::Sequence< OUString > SysCredentialsConfig::list( bool bOnlyPersistent )
237{
238 std::unique_lock aGuard(m_aMutex);
239 initCfg(aGuard);
240 sal_Int32 nCount = m_aCfgContainer.size()
241 + ( bOnlyPersistent ? 0 : m_aMemContainer.size() );
242 uno::Sequence< OUString > aResult( nCount );
243 auto aResultRange = asNonConstRange(aResult);
244 sal_Int32 n = 0;
245
246 for ( const auto& rItem : m_aCfgContainer )
247 {
248 aResultRange[ n ] = rItem;
249 ++n;
250 }
251
252 if ( !bOnlyPersistent )
253 {
254 for ( const auto& rItem : m_aMemContainer )
255 {
256 aResultRange[ n ] = rItem;
257 ++n;
258 }
259 }
260 return aResult;
261}
262
264{
265 std::unique_lock aGuard( m_aMutex );
266 m_bCfgInited = false; // re-init on demand.
267}
268
269/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Sequence< OUString > m_seqURLs
Definition: syscreds.hxx:52
virtual void Notify(const css::uno::Sequence< OUString > &seqPropertyNames) override
Definition: syscreds.cxx:37
css::uno::Sequence< OUString > getSystemCredentialsURLs()
Definition: syscreds.cxx:55
void setSystemCredentialsURLs(const css::uno::Sequence< OUString > &seqURLList)
Definition: syscreds.cxx:87
virtual void ImplCommit() override
Definition: syscreds.cxx:49
SysCredentialsConfig * m_pOwner
Definition: syscreds.hxx:53
SysCredentialsConfigItem(SysCredentialsConfig *pOwner)
Definition: syscreds.cxx:26
OUString find(OUString const &rURL)
Definition: syscreds.cxx:189
std::set< OUString > m_aCfgContainer
Definition: syscreds.hxx:74
css::uno::Sequence< OUString > list(bool bOnlyPersistent)
Definition: syscreds.cxx:236
void writeCfg(std::unique_lock< std::mutex > &rGuard)
Definition: syscreds.cxx:182
std::mutex m_aMutex
Definition: syscreds.hxx:72
std::set< OUString > m_aMemContainer
Definition: syscreds.hxx:73
void remove(OUString const &rURL)
Definition: syscreds.cxx:225
void persistentConfigChanged()
Definition: syscreds.cxx:263
void add(OUString const &rURL, bool bPersistent)
Definition: syscreds.cxx:203
void initCfg(std::unique_lock< std::mutex > &rGuard)
Definition: syscreds.cxx:171
SysCredentialsConfigItem m_aConfigItem
Definition: syscreds.hxx:75
static bool PutProperties(css::uno::Reference< css::container::XHierarchicalNameAccess > const &xHierarchyAccess, const css::uno::Sequence< OUString > &rNames, const css::uno::Sequence< css::uno::Any > &rValues, bool bAllLocales)
bool EnableNotification(const css::uno::Sequence< OUString > &rNames, bool bEnableInternalNotification=false)
static css::uno::Sequence< css::uno::Any > GetProperties(css::uno::Reference< css::container::XHierarchicalNameAccess > const &xHierarchyAccess, const css::uno::Sequence< OUString > &rNames, bool bAllLocales)
ConfigItemMode
int nCount
URL aURL
sal_Int64 n
SvLinkSource * pOwner
NONE
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
const PropertyStruct aPropNames[]
ContentProvider * m_pOwner