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 : #ifndef INCLUDED_SFX2_DOCMACROMODE_HXX
21 : #define INCLUDED_SFX2_DOCMACROMODE_HXX
22 :
23 : #include <sfx2/dllapi.h>
24 : #include <sfx2/signaturestate.hxx>
25 :
26 : #include <com/sun/star/task/XInteractionHandler.hpp>
27 : #include <com/sun/star/embed/XStorage.hpp>
28 : #include <com/sun/star/script/XLibraryContainer.hpp>
29 : #include <com/sun/star/document/XEmbeddedScripts.hpp>
30 :
31 : #include <memory>
32 :
33 :
34 : namespace sfx2
35 : {
36 :
37 :
38 :
39 : //= IMacroDocumentAccess
40 :
41 : /** provides access to several settings of a document, which are needed by ->DocumentMacroMode
42 : to properly determine the current macro execution mode of this document
43 : */
44 5052 : class SAL_NO_VTABLE IMacroDocumentAccess
45 : {
46 : public:
47 : /** retrieves the current MacroExecutionMode.
48 :
49 : Usually, this is initialized from the media descriptor used to load the document,
50 : respectively the one passed into the document's XModel::attachResource call.
51 :
52 : If no such mode was passed there, document implementations should return
53 : MacroExecMode::NEVER_EXECUTE.
54 :
55 : @see ::com::sun::star::document::MediaDescriptor::MacroExecutionMode
56 : @see ::com::sun::star::frame::XComponentLoader::loadComponentFromURL
57 : @see ::com::sun::star::frame::XModel::attachResource
58 :
59 : @see setCurrentMacroExecMode
60 :
61 : @todo
62 : Effectively, this is the MacroExecutionMode of the MediaDescriptor of
63 : the document. Thus, this setting could be obtained from the XModel
64 : directly. We should introduce a getDocumentModel method here, which
65 : can be used for this and other purposes.
66 : */
67 : virtual sal_Int16
68 : getCurrentMacroExecMode() const = 0;
69 :
70 : /** sets the MacroExecutionMode of the document, as calculated by the DocumentMacroMode
71 : class.
72 :
73 : Effectively, the existence of this method means that the responsibility
74 : to store the current macro execution mode is not with the DocumentMacroMode
75 : instance, but with the document instance itself.
76 :
77 : Usually, a document implementation will simply put the macro execution mode
78 : into its media descriptor, as returned by XModel::getArgs.
79 :
80 : @see ::com::sun::star::document::MediaDescriptor::MacroExecutionMode
81 : @see ::com::sun::star::frame::XComponentLoader::loadComponentFromURL
82 : @see ::com::sun::star::frame::XModel::attachResource
83 :
84 : see getCurrentMacroExecMode
85 : */
86 : virtual bool
87 : setCurrentMacroExecMode( sal_uInt16 ) = 0;
88 :
89 : /** returns the origin of the document
90 :
91 : This usually is the document's location, or, if the document has been
92 : newly created from a template, then the location of the template. Location
93 : here means the complete path of the document, including the file name.
94 :
95 : @todo
96 : This probably can also be obtained from the XModel, by calling getURL
97 : or getLocation. If both are empty, then we need an UNO way to obtain
98 : the URL of the underlying template document - if any. If we have this,
99 : we could replace this method with a newly introduced method
100 : getDocumentModel and some internal code.
101 : */
102 : virtual OUString
103 : getDocumentLocation() const = 0;
104 :
105 : /** returns a zip-storage based on the last committed version of the document,
106 : for readonly access
107 :
108 : The storage is intended to be used for signing. An implementation is
109 : allowed to return <NULL/> here if and only if the document
110 : does not support signing the script storages.
111 :
112 : @todo
113 : UNOize this, too. Once we have a getDocumentModel, we should be able to
114 : obtain the "last commit" storage via UNO API, provided it's an
115 : XStorageBasedDocument.
116 : */
117 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >
118 : getZipStorageToSign() = 0;
119 :
120 : /** checks whether the document's storage contains sub storages with macros or scripts
121 :
122 : A default implementation of this method will simply cann DocumentMacroMode::storageHasMacros
123 : with the document's root storage. However, there might be document types where this
124 : isn't sufficient (e.g. database documents which contain sub documents which can also
125 : contain macro/script storages).
126 : */
127 : virtual bool
128 : documentStorageHasMacros() const = 0;
129 :
130 : /** provides access to the XEmbeddedScripts interface of the document
131 :
132 : Implementations are allowed to return <NULL/> here if and only if they
133 : do not (yet) support embedding scripts.
134 :
135 : @todo
136 : can also be replaced with a call to the (to be introduced) getDocumentModel
137 : method, and a queryInterface.
138 : */
139 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::document::XEmbeddedScripts >
140 : getEmbeddedDocumentScripts() const = 0;
141 :
142 : /** returns the state of the signatures for the scripts embedded in the document
143 :
144 : Note: On the medium run, the signature handling of a document should be outsourced
145 : into a dedicated class, instead of being hard-wired into the SfxObjectShell. This
146 : class could then be used outside the SfxObjectShell (e.g. in Base documents), too.
147 : When this happens, this method here becomes should be replaced by a method at this
148 : new class.
149 :
150 : @seealso <sfx2/signaturestate.hxx>
151 : */
152 : virtual SignatureState
153 : getScriptingSignatureState() = 0;
154 :
155 : /** allows to detect whether there is a trusted scripting signature
156 :
157 : Note: On the medium run, the signature handling of a document should be outsourced
158 : into a dedicated class, instead of being hard-wired into the SfxObjectShell. This
159 : class could then be used outside the SfxObjectShell (e.g. in Base documents), too.
160 : When this happens, this method here should be replaced by a method at this
161 : new class.
162 :
163 : @seealso <sfx2/signaturestate.hxx>
164 : */
165 : virtual bool
166 : hasTrustedScriptingSignature( bool bAllowUIToAddAuthor ) = 0;
167 :
168 : /** shows a warning that the document's signature is broken
169 :
170 : Here, a similar note applies as to getScriptingSignatureState: This method doesn't
171 : really belong here. It's just there because SfxObjectShell_Impl::bSignatureErrorIsShown
172 : is not accessible where the method is called.
173 : So, once the signature handling has been oursourced from SfxObjectShell/_Impl, so it
174 : is re-usable in non-SFX contexts as well, this method here is also unneeded, probably.
175 :
176 : @param _rxInteraction
177 : the interaction handler to use for showing the warning. It is exactly the same
178 : as passed to DocumentMacroMode::adjustMacroMode, so it is <NULL/> if and
179 : only if the instance passed to that method was <NULL/>.
180 : */
181 : virtual void
182 : showBrokenSignatureWarning(
183 : const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& _rxInteraction
184 : ) const = 0;
185 :
186 : protected:
187 4991 : ~IMacroDocumentAccess() {}
188 : };
189 :
190 :
191 : //= DocumentMacroMode
192 :
193 : struct DocumentMacroMode_Data;
194 :
195 : /** encapsulates handling the macro mode of a document
196 :
197 : @see com::sun::star::document::MacroExecMode
198 : */
199 108 : class SFX2_DLLPUBLIC DocumentMacroMode
200 : {
201 : public:
202 : /** creates an instance
203 :
204 : @param _rDocumentAccess
205 : access to the document which this instance works for. Must live as long as the
206 : DocumentMacroMode instance lives, at least
207 : */
208 : DocumentMacroMode( IMacroDocumentAccess& _rDocumentAccess );
209 : ~DocumentMacroMode();
210 :
211 : /** allows macro execution in the document
212 :
213 : Effectively, the macro mode is set to MacroExecMode::ALWAYS_EXECUTE_NO_WARN.
214 :
215 : @return
216 : <TRUE/>, always
217 : */
218 : bool allowMacroExecution();
219 :
220 : /** disallows macro execution in the document
221 :
222 : Effectively, the macro mode is set to MacroExecMode::NEVER_EXECUTE.
223 :
224 : @return
225 : <TRUE/>, always
226 : */
227 : bool disallowMacroExecution();
228 :
229 : /** checks whether the document allows executing contained macros.
230 :
231 : The method transforms the current macro execution mode into either
232 : ALWAYS_EXECUTE_NO_WARN or NEVER_EXECUTE, depending on the current value,
233 : possible configuration settings, and possible user interaction.
234 :
235 : @param _rxInteraction
236 : A handler for interactions which might become necessary.
237 : This includes
238 : <ul><li>Asking the user for confirmation for macro execution.</li>
239 : <li>Telling the user that macro execution is disabled.</li>
240 : </ul>
241 :
242 : If the user needs to be asked for macro execution confirmation, and if
243 : this parameter is <NULL/>, the most defensive assumptions will be made,
244 : effectively disabling macro execution.
245 :
246 : @return
247 : <TRUE/> if and only if macro execution in this document is allowed.
248 : */
249 : bool adjustMacroMode(
250 : const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& _rxInteraction
251 : );
252 :
253 : /** determines whether macro execution is disallowed
254 :
255 : There's a number of reasons why macro execution could be disallowed:
256 : <ul><li>Somebody called ->disallowMacroExecution</li>
257 : <li>Macro execution is disabled globally, via the security options</li>
258 : <li>Macro execution mode was not defined initially, and the user denied
259 : executing macros for this particular document.</li>
260 : </ul>
261 :
262 : Note that if this method returns <FALSE/>, then subsequent calls of
263 : ->adjustMacroMode can still return <FALSE/>.
264 : That is, if the current macro execution mode for the document is not yet known
265 : (and inparticular <em>not</em> MacroExecMode::NEVER_EXECUTE), then ->isMacroExecutionDisallowed
266 : will return <FALSE/>.
267 : However, a subsequent call to ->adjustMacroMode can result in the user
268 : denying macro execution, in which ->adjustMacroMode will return <FALSE/>,
269 : and the next call to isMacroExecutionDisallowed will return <TRUE/>.
270 : */
271 : bool isMacroExecutionDisallowed() const;
272 :
273 : /** determines whether the document actually has a macros library
274 :
275 : Effectively, this method checks the Basic library container (as returned by
276 : IMacroDocumentAccess::getEmbeddedDocumentScripts().getBasicLibraries) for
277 : content.
278 : */
279 : bool hasMacroLibrary() const;
280 :
281 : /** determines whether the given document storage has sub storages containing scripts
282 : or macros.
283 :
284 : Effectively, the method checks for the presence of a sub-storage name "Scripts" (where
285 : BeanShell-/JavaScript-/Python-Scripts are stored, and a sub storage named "Basic" (where
286 : Basic scripts are stored).
287 : */
288 : static bool
289 : storageHasMacros( const ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >& _rxStorage );
290 :
291 : static bool containerHasBasicMacros( const ::com::sun::star::uno::Reference< ::com::sun::star::script::XLibraryContainer >& xContainter );
292 : /** checks the macro execution mode while loading the document.
293 :
294 : This must be called when the loading is effectively finished, but before any macro action
295 : happened.
296 :
297 : The method will disallow macro execution for this document if it is disabled
298 : globally (SvtSecurityOptions::IsMacroDisabled). Otherwise, it will check whether
299 : the document contains a macro storage or macro libraries. If so, it will
300 : properly calculate the MacroExecutionMode by calling adjustMacroMode.
301 :
302 : If the document doesn't contain macros, yet, then the macro execution for this
303 : document will be allowed (again: unless disabled globally), since in this case
304 : macros which later are newly created by the user should be allowed, of course.
305 :
306 : @return
307 : <TRUE/> if and only if macro execution is allowed in the document
308 :
309 : @see isMacroExecutionDisallowed
310 : @see IMacroDocumentAccess::documentStorageHasMacros
311 : @see hasMacroLibrary
312 : @see IMacroDocumentAccess::checkForBrokenScriptingSignatures
313 : */
314 : bool
315 : checkMacrosOnLoading(
316 : const ::com::sun::star::uno::Reference< ::com::sun::star::task::XInteractionHandler >& _rxInteraction
317 : );
318 :
319 : private:
320 : std::shared_ptr< DocumentMacroMode_Data > m_xData;
321 : };
322 :
323 :
324 : } // namespace sfx2
325 :
326 :
327 : #endif // INCLUDED_SFX2_DOCMACROMODE_HXX
328 :
329 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|