Line data Source code
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 <unotools/extendedsecurityoptions.hxx>
21 : #include <unotools/configmgr.hxx>
22 : #include <unotools/configitem.hxx>
23 : #include <tools/debug.hxx>
24 : #include <com/sun/star/uno/Any.hxx>
25 : #include <com/sun/star/uno/Sequence.hxx>
26 : #include <rtl/ustrbuf.hxx>
27 :
28 : #include <unotools/pathoptions.hxx>
29 :
30 : #include <boost/unordered_map.hpp>
31 :
32 : #include "itemholder1.hxx"
33 :
34 : using namespace ::utl;
35 : using namespace ::osl;
36 : using namespace ::com::sun::star::uno;
37 :
38 : #define ROOTNODE_SECURITY OUString("Office.Security")
39 :
40 : #define SECURE_EXTENSIONS_SET OUString("SecureExtensions")
41 : #define EXTENSION_PROPNAME OUString("/Extension")
42 :
43 : #define PROPERTYNAME_HYPERLINKS_OPEN OUString("Hyperlinks/Open")
44 :
45 : #define PROPERTYHANDLE_HYPERLINKS_OPEN 0
46 :
47 : #define PROPERTYCOUNT 1
48 :
49 : typedef boost::unordered_map<OUString, sal_Int32, OUStringHash>
50 : ExtensionHashMap;
51 :
52 : class SvtExtendedSecurityOptions_Impl : public ConfigItem
53 : {
54 : public:
55 : SvtExtendedSecurityOptions_Impl();
56 : virtual ~SvtExtendedSecurityOptions_Impl();
57 :
58 : /*-****************************************************************************************************
59 : @short called for notify of configmanager
60 : @descr These method is called from the ConfigManager before application ends or from the
61 : PropertyChangeListener if the sub tree broadcasts changes. You must update your
62 : internal values.
63 :
64 : @seealso baseclass ConfigItem
65 :
66 : @param "seqPropertyNames" is the list of properties which should be updated.
67 : *//*-*****************************************************************************************************/
68 :
69 : virtual void Notify( const Sequence< OUString >& seqPropertyNames ) SAL_OVERRIDE;
70 :
71 : /*-****************************************************************************************************
72 : @short write changes to configuration
73 : @descr These method writes the changed values into the sub tree
74 : and should always called in our destructor to guarantee consistency of config data.
75 :
76 : @seealso baseclass ConfigItem
77 : *//*-*****************************************************************************************************/
78 :
79 : virtual void Commit() SAL_OVERRIDE;
80 :
81 0 : SvtExtendedSecurityOptions::OpenHyperlinkMode GetOpenHyperlinkMode() { return m_eOpenHyperlinkMode;}
82 : private:
83 :
84 : /*-****************************************************************************************************
85 : @short return list of key names of our configuration management which represent oue module tree
86 : @descr These methods return a static const list of key names. We need it to get needed values from our
87 : configuration management.
88 : @return A list of needed configuration keys is returned.
89 : *//*-*****************************************************************************************************/
90 :
91 : static Sequence< OUString > GetPropertyNames();
92 :
93 : /*-****************************************************************************************************
94 : @short Fills the hash map with all extensions known to be secure
95 : @descr These methods fills the given hash map object with all extensions known to be secure.
96 : @param aHashMap
97 : A hash map to be filled with secure extension strings.
98 : *//*-*****************************************************************************************************/
99 : void FillExtensionHashMap( ExtensionHashMap& aHashMap );
100 :
101 : OUString m_aSecureExtensionsSetName;
102 : OUString m_aExtensionPropName;
103 :
104 : SvtExtendedSecurityOptions::OpenHyperlinkMode m_eOpenHyperlinkMode;
105 : bool m_bROOpenHyperlinkMode;
106 : ExtensionHashMap m_aExtensionHashMap;
107 : };
108 :
109 : // constructor
110 :
111 0 : SvtExtendedSecurityOptions_Impl::SvtExtendedSecurityOptions_Impl()
112 : // Init baseclasses first
113 : : ConfigItem ( ROOTNODE_SECURITY )
114 : , m_aSecureExtensionsSetName( SECURE_EXTENSIONS_SET )
115 : , m_aExtensionPropName( EXTENSION_PROPNAME )
116 : , m_eOpenHyperlinkMode(SvtExtendedSecurityOptions::OPEN_NEVER)
117 0 : , m_bROOpenHyperlinkMode(false)
118 : // Init member then.
119 : {
120 : // Fill the extension hash map with all secure extension strings
121 0 : FillExtensionHashMap( m_aExtensionHashMap );
122 :
123 0 : Sequence< OUString > seqNames = GetPropertyNames();
124 0 : Sequence< Any > seqValues = GetProperties( seqNames );
125 0 : Sequence< sal_Bool > seqRO = GetReadOnlyStates ( seqNames );
126 :
127 0 : sal_Int32 nPropertyCount = seqValues.getLength();
128 0 : for( sal_Int32 nProperty=0; nProperty<nPropertyCount; ++nProperty )
129 : {
130 : // Safe impossible cases.
131 : // Check any for valid value.
132 : DBG_ASSERT( seqValues[nProperty].hasValue(), "SvtExtendedSecurityOptions_Impl::SvtExtendedSecurityOptions_Impl()\nInvalid property value detected!\n" );
133 0 : switch( nProperty )
134 : {
135 : case PROPERTYHANDLE_HYPERLINKS_OPEN:
136 : {
137 : DBG_ASSERT( ( seqValues[nProperty].getValueTypeClass() == TypeClass_LONG ), "SvtExtendedSecurityOptions_Impl::SvtExtendedSecurityOptions_Impl()\nWho has changed the value type of 'Hyperlink/Open'?" );
138 :
139 0 : sal_Int32 nMode = SvtExtendedSecurityOptions::OPEN_WITHSECURITYCHECK;
140 0 : if ( seqValues[nProperty] >>= nMode )
141 0 : m_eOpenHyperlinkMode = (SvtExtendedSecurityOptions::OpenHyperlinkMode)nMode;
142 : else {
143 : OSL_FAIL("Wrong type for Open mode!");
144 : }
145 0 : m_bROOpenHyperlinkMode = seqRO[nProperty];
146 : }
147 0 : break;
148 : }
149 : }
150 :
151 : // Enable notification mechanism of our baseclass.
152 : // We need it to get information about changes outside these class on our used configuration keys!
153 0 : Sequence< OUString > seqNotifyNames( 1 );
154 0 : seqNotifyNames[0] = m_aSecureExtensionsSetName;
155 0 : EnableNotification( seqNotifyNames );
156 0 : }
157 :
158 : // destructor
159 :
160 0 : SvtExtendedSecurityOptions_Impl::~SvtExtendedSecurityOptions_Impl()
161 : {
162 : // We must save our current values .. if user forget it!
163 0 : if( IsModified() )
164 : {
165 0 : Commit();
166 : }
167 0 : }
168 :
169 : // public method
170 :
171 0 : void SvtExtendedSecurityOptions_Impl::Notify( const Sequence< OUString >& )
172 : {
173 : // Not implemented
174 0 : }
175 :
176 : // public method
177 :
178 0 : void SvtExtendedSecurityOptions_Impl::Commit()
179 : {
180 : // Get names of supported properties, create a list for values and copy current values to it.
181 0 : Sequence< OUString > seqNames = GetPropertyNames ();
182 0 : sal_Int32 nCount = seqNames.getLength();
183 0 : Sequence< Any > seqValues ( nCount );
184 0 : for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
185 : {
186 0 : switch( nProperty )
187 : {
188 : case PROPERTYHANDLE_HYPERLINKS_OPEN: {
189 0 : seqValues[nProperty] <<= (sal_Int32)m_eOpenHyperlinkMode;
190 : }
191 0 : break;
192 : }
193 : }
194 :
195 : // Set properties in configuration.
196 0 : PutProperties( seqNames, seqValues );
197 0 : }
198 :
199 : // public method
200 :
201 :
202 : // private method
203 :
204 0 : void SvtExtendedSecurityOptions_Impl::FillExtensionHashMap( ExtensionHashMap& aHashMap )
205 : {
206 : // Get sequence with secure extensions from configuration
207 0 : Sequence< OUString > seqNodes = GetNodeNames( m_aSecureExtensionsSetName );
208 :
209 0 : OUString aValue;
210 0 : Sequence< Any > aValues;
211 0 : Sequence< OUString > aPropSeq( 1 );
212 0 : for ( int i = 0; i < seqNodes.getLength(); i++ )
213 : {
214 : // Create access name for property
215 0 : OUStringBuffer aExtEntryProp( m_aSecureExtensionsSetName );
216 0 : aExtEntryProp.appendAscii( "/" );
217 0 : aExtEntryProp.append( seqNodes[i] );
218 0 : aExtEntryProp.append( m_aExtensionPropName );
219 :
220 0 : aPropSeq[0] = aExtEntryProp.makeStringAndClear();
221 0 : aValues = GetProperties( aPropSeq );
222 0 : if ( aValues.getLength() == 1 )
223 : {
224 : // Don't use value if sequence has not the correct length
225 0 : if ( aValues[0] >>= aValue )
226 : // Add extension into secure extensions hash map
227 0 : aHashMap.insert( ExtensionHashMap::value_type( aValue.toAsciiLowerCase(), 1 ) );
228 : else
229 : {
230 : SAL_WARN( "unotools.config", "SvtExtendedSecurityOptions_Impl::FillExtensionHashMap(): not string value?" );
231 : }
232 : }
233 0 : }
234 0 : }
235 :
236 : // private method (currently not used)
237 :
238 0 : Sequence< OUString > SvtExtendedSecurityOptions_Impl::GetPropertyNames()
239 : {
240 : // Build list of configuration key names.
241 : const OUString pProperties[] =
242 : {
243 : PROPERTYNAME_HYPERLINKS_OPEN
244 0 : };
245 : // Initialize return sequence with these list ...
246 0 : const Sequence< OUString > seqPropertyNames( pProperties, PROPERTYCOUNT );
247 : // ... and return it.
248 0 : return seqPropertyNames;
249 : }
250 :
251 : // initialize static member
252 : // DON'T DO IT IN YOUR HEADER!
253 : // see definition for further information
254 :
255 : SvtExtendedSecurityOptions_Impl* SvtExtendedSecurityOptions::m_pDataContainer = NULL;
256 : sal_Int32 SvtExtendedSecurityOptions::m_nRefCount = 0;
257 :
258 : // constructor
259 :
260 0 : SvtExtendedSecurityOptions::SvtExtendedSecurityOptions()
261 : {
262 : // Global access, must be guarded (multithreading!).
263 0 : MutexGuard aGuard( GetInitMutex() );
264 : // Increase our refcount ...
265 0 : ++m_nRefCount;
266 : // ... and initialize our data container only if it not already exist!
267 0 : if( m_pDataContainer == NULL )
268 : {
269 0 : m_pDataContainer = new SvtExtendedSecurityOptions_Impl;
270 :
271 0 : ItemHolder1::holdConfigItem(E_EXTENDEDSECURITYOPTIONS);
272 0 : }
273 0 : }
274 :
275 : // destructor
276 :
277 0 : SvtExtendedSecurityOptions::~SvtExtendedSecurityOptions()
278 : {
279 : // Global access, must be guarded (multithreading!)
280 0 : MutexGuard aGuard( GetInitMutex() );
281 : // Decrease our refcount.
282 0 : --m_nRefCount;
283 : // If last instance was deleted ...
284 : // we must destroy our static data container!
285 0 : if( m_nRefCount <= 0 )
286 : {
287 0 : delete m_pDataContainer;
288 0 : m_pDataContainer = NULL;
289 0 : }
290 0 : }
291 :
292 : // public method
293 :
294 0 : SvtExtendedSecurityOptions::OpenHyperlinkMode SvtExtendedSecurityOptions::GetOpenHyperlinkMode()
295 : {
296 0 : MutexGuard aGuard( GetInitMutex() );
297 0 : return m_pDataContainer->GetOpenHyperlinkMode();
298 : }
299 :
300 : namespace
301 : {
302 : class theExtendedSecurityOptionsMutex : public rtl::Static<osl::Mutex, theExtendedSecurityOptionsMutex>{};
303 : }
304 :
305 : // private method
306 :
307 0 : Mutex& SvtExtendedSecurityOptions::GetInitMutex()
308 : {
309 0 : return theExtendedSecurityOptionsMutex::get();
310 : }
311 :
312 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|