1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 
29 #ifndef _DRAFTS_COM_SUN_STAR_SCRIPT_FRAMEWORK_STORAGE_SCRIPTSTORAGEMANAGER_HXX_
30 #define _DRAFTS_COM_SUN_STAR_SCRIPT_FRAMEWORK_STORAGE_SCRIPTSTORAGEMANAGER_HXX_
31 
32 #include <hash_map>
33 #include <map>
34 
35 #include <osl/mutex.hxx>
36 #include <cppuhelper/implbase4.hxx>
37 
38 #include <com/sun/star/lang/XServiceInfo.hpp>
39 #include <com/sun/star/lang/XEventListener.hpp>
40 #include <com/sun/star/lang/IllegalArgumentException.hpp>
41 #include <com/sun/star/security/AccessControlException.hpp>
42 #include <com/sun/star/uno/RuntimeException.hpp>
43 
44 #include <drafts/com/sun/star/script/framework/storage/XScriptStorageManager.hpp>
45 #include <drafts/com/sun/star/script/framework/storage/XScriptStorageRefresh.hpp>
46 #include <drafts/com/sun/star/script/framework/storage/XScriptInfo.hpp>
47 #include <drafts/com/sun/star/script/framework/security/XScriptSecurity.hpp>
48 #include "ScriptSecurityManager.hxx"
49 
50 
51 namespace scripting_impl
52 {
53 // for simplification
54 #define css ::com::sun::star
55 #define dcsssf ::drafts::com::sun::star::script::framework
56 
57 // Define a map used to store the ScriptingStorages key;d by ID
58 typedef ::std::map < sal_Int32, css::uno::Reference < css::uno::XInterface > >
59     ScriptStorage_map;
60 
61 typedef ::std::hash_map < ::rtl::OUString, sal_Int32, ::rtl::OUStringHash>
62     StorageId_hash;
63 
64 class ScriptStorageManager : public
65     ::cppu::WeakImplHelper4 < dcsssf::storage::XScriptStorageManager,
66     dcsssf::security::XScriptSecurity, css::lang::XServiceInfo,
67     css::lang::XEventListener >
68 {
69 public:
70     explicit ScriptStorageManager(
71         const css::uno::Reference< css::uno::XComponentContext > & xContext )
72         SAL_THROW ( ( css::uno::RuntimeException ) );
73 
74 
75     ~ScriptStorageManager() SAL_THROW ( () );
76 
77     // XServiceInfo implementation
78     //======================================================================
79     virtual ::rtl::OUString SAL_CALL getImplementationName()
80         throw( css::uno::RuntimeException );
81     //----------------------------------------------------------------------
82     virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName )
83         throw( css::uno::RuntimeException );
84     //----------------------------------------------------------------------
85     virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames()
86         throw( css::uno::RuntimeException );
87     //======================================================================
88 
89     //XScriptStorageManager
90     //======================================================================
91     /**
92         create a ScriptStorage using the XSimpleFileAccess passed as an
93         argument, and return a ID for getting the associated ScriptStorage
94 
95         @params xSFA
96         an implementation of XSimpleFileAccess that knows its own base URL
97         and can thus take URLs relative to that base.
98 
99         @returns an unsigned short ScriptStorage ID, which can be used in the
100         getScriptStorage method
101     */
102     virtual sal_Int32 SAL_CALL createScriptStorage(
103             const css::uno::Reference< css::ucb::XSimpleFileAccess > & xSFA )
104             throw ( css::uno::RuntimeException );
105     //----------------------------------------------------------------------
106     /**
107         create a ScriptStorage using the XSimpleFileAccess, and a string URL
108         and return a ID for getting the associated ScriptStorage
109 
110         @params xSFA
111         a standard implementation of XSimpleFileAccess
112 
113         @params stringURI
114         a string URI to the head of the script storage
115 
116         @returns an unsigned short ScriptStorage ID, which can be used in the
117         getScriptStorage method
118     */
119     virtual sal_Int32 SAL_CALL
120         createScriptStorageWithURI(
121             const css::uno::Reference< css::ucb::XSimpleFileAccess >& xSFA,
122             const ::rtl::OUString& stringURI )
123             throw ( css::uno::RuntimeException );
124     //----------------------------------------------------------------------
125     /**
126         get a ScriptStorage component using its scriptStorageID
127 
128         @params scriptStorageID
129         the usigned short returned by one of the methods above. ID=0 is
130         reserved for the application/share scripts, and ID=1 is reserved
131         for the application/user scripts
132 
133         @returns an XInterface to a component that implements the ScriptStorage
134         service
135     */
136     virtual css::uno::Reference< css::uno::XInterface > SAL_CALL getScriptStorage(
137         sal_Int32 scriptStorageID )
138         throw ( css::uno::RuntimeException );
139 
140     /**
141         get a ScriptStorage ID using storage URI
142 
143         @param scriptStorageURI
144         the file URL for the document will retrieve storage id for the document,        special treatment is reserved for the strings "USER"and "SHARE" which
145         retrieve storage id for application/user application/share areas respectively.
146 
147 
148         @returns as long ScriptStorage ID (-1 if no storage exists), which can be used in getScriptStorage method.
149 
150     */
151 
152     virtual sal_Int32 SAL_CALL getScriptStorageID( const ::rtl::OUString& scriptStorageURI )
153         throw (::com::sun::star::uno::RuntimeException);
154 
155     /**
156         refresh a storage component using its URI
157 
158         @param StringURI
159         URI of storage area, for documents, url to document eg, file:///home/user/ADocument.sxw To refresh the storage for user or share area, use USER or SHARE respectively instead or a url.
160 
161     */
162 
163     virtual void SAL_CALL refreshScriptStorage(const ::rtl::OUString & stringURI)
164 	throw ( css::uno::RuntimeException );
165     //======================================================================
166 
167     //XScriptSecurity
168     //======================================================================
169     //----------------------------------------------------------------------
170     /**
171         the language independent interface for invocation
172 
173         @param scriptURI
174             a string containing the script URI
175 
176         @returns
177             the value returned from the function being invoked
178 
179         @throws IllegalArgumentException
180             if there is no matching script name
181 
182     */
183     virtual void SAL_CALL ScriptStorageManager::checkPermission(
184         const rtl::OUString & scriptStorageURI,
185         const rtl::OUString & permissionRequest )
186         throw ( css::uno::RuntimeException, css::lang::IllegalArgumentException,
187             css::security::AccessControlException );
188     //======================================================================
189 
190     //XEventListener
191     //======================================================================
192 
193     virtual void SAL_CALL disposing( const css::lang::EventObject& Source )
194         throw ( css::uno::RuntimeException );
195 
196 private:
197     ScriptStorageManager( const ScriptStorageManager & );
198     ScriptStorageManager& operator= ( const ScriptStorageManager & );
199 
200     void removeScriptDocURIHashEntry( const ::rtl::OUString & origURI );
201 
202     // to obtain other services if needed
203     css::uno::Reference< css::uno::XComponentContext > m_xContext;
204     css::uno::Reference< css::lang::XMultiComponentFactory > m_xMgr;
205     ::osl::Mutex m_mutex;
206     ScriptStorage_map m_ScriptStorageMap;
207     StorageId_hash m_StorageIdOrigURIHash;
208     sal_Int32 m_count;
209     scripting_securitymgr::ScriptSecurityManager m_securityMgr;
210 
211     void setupAppStorage( const css::uno::Reference< css::util::XMacroExpander > & xME,
212         const ::rtl::OUString & storageStr,
213         const ::rtl::OUString & appStr)
214         SAL_THROW ( ( css::uno::RuntimeException ) );
215 
216     sal_Int32 setupAnyStorage(
217         const css::uno::Reference< css::ucb::XSimpleFileAccess> & xSFA,
218         const ::rtl::OUString & storageStr,
219         const ::rtl::OUString & origStringURI )
220         SAL_THROW ( ( css::uno::RuntimeException ) );
221 
222 };
223 } // scripting_impl
224 
225 #endif //_COM_SUN_STAR_SCRIPTING_STORAGE_SCRIPTSTORAGEMANAGER_HXX_
226