1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 
25 #ifndef _DRAFTS_COM_SUN_STAR_SCRIPT_FRAMEWORK_STORAGE_SCRIPTSTORAGEMANAGER_HXX_
26 #define _DRAFTS_COM_SUN_STAR_SCRIPT_FRAMEWORK_STORAGE_SCRIPTSTORAGEMANAGER_HXX_
27 
28 #include <hash_map>
29 #include <map>
30 
31 #include <osl/mutex.hxx>
32 #include <cppuhelper/implbase4.hxx>
33 
34 #include <com/sun/star/lang/XServiceInfo.hpp>
35 #include <com/sun/star/lang/XEventListener.hpp>
36 #include <com/sun/star/lang/IllegalArgumentException.hpp>
37 #include <com/sun/star/security/AccessControlException.hpp>
38 #include <com/sun/star/uno/RuntimeException.hpp>
39 
40 #include <drafts/com/sun/star/script/framework/storage/XScriptStorageManager.hpp>
41 #include <drafts/com/sun/star/script/framework/storage/XScriptStorageRefresh.hpp>
42 #include <drafts/com/sun/star/script/framework/storage/XScriptInfo.hpp>
43 #include <drafts/com/sun/star/script/framework/security/XScriptSecurity.hpp>
44 #include "ScriptSecurityManager.hxx"
45 
46 
47 namespace scripting_impl
48 {
49 // for simplification
50 #define css ::com::sun::star
51 #define dcsssf ::drafts::com::sun::star::script::framework
52 
53 // Define a map used to store the ScriptingStorages key;d by ID
54 typedef ::std::map < sal_Int32, css::uno::Reference < css::uno::XInterface > >
55     ScriptStorage_map;
56 
57 typedef ::std::hash_map < ::rtl::OUString, sal_Int32, ::rtl::OUStringHash>
58     StorageId_hash;
59 
60 class ScriptStorageManager : public
61     ::cppu::WeakImplHelper4 < dcsssf::storage::XScriptStorageManager,
62     dcsssf::security::XScriptSecurity, css::lang::XServiceInfo,
63     css::lang::XEventListener >
64 {
65 public:
66     explicit ScriptStorageManager(
67         const css::uno::Reference< css::uno::XComponentContext > & xContext )
68         SAL_THROW ( ( css::uno::RuntimeException ) );
69 
70 
71     ~ScriptStorageManager() SAL_THROW ( () );
72 
73     // XServiceInfo implementation
74     //======================================================================
75     virtual ::rtl::OUString SAL_CALL getImplementationName()
76         throw( css::uno::RuntimeException );
77     //----------------------------------------------------------------------
78     virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName )
79         throw( css::uno::RuntimeException );
80     //----------------------------------------------------------------------
81     virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames()
82         throw( css::uno::RuntimeException );
83     //======================================================================
84 
85     //XScriptStorageManager
86     //======================================================================
87     /**
88         create a ScriptStorage using the XSimpleFileAccess passed as an
89         argument, and return a ID for getting the associated ScriptStorage
90 
91         @params xSFA
92         an implementation of XSimpleFileAccess that knows its own base URL
93         and can thus take URLs relative to that base.
94 
95         @returns an unsigned short ScriptStorage ID, which can be used in the
96         getScriptStorage method
97     */
98     virtual sal_Int32 SAL_CALL createScriptStorage(
99             const css::uno::Reference< css::ucb::XSimpleFileAccess > & xSFA )
100             throw ( css::uno::RuntimeException );
101     //----------------------------------------------------------------------
102     /**
103         create a ScriptStorage using the XSimpleFileAccess, and a string URL
104         and return a ID for getting the associated ScriptStorage
105 
106         @params xSFA
107         a standard implementation of XSimpleFileAccess
108 
109         @params stringURI
110         a string URI to the head of the script storage
111 
112         @returns an unsigned short ScriptStorage ID, which can be used in the
113         getScriptStorage method
114     */
115     virtual sal_Int32 SAL_CALL
116         createScriptStorageWithURI(
117             const css::uno::Reference< css::ucb::XSimpleFileAccess >& xSFA,
118             const ::rtl::OUString& stringURI )
119             throw ( css::uno::RuntimeException );
120     //----------------------------------------------------------------------
121     /**
122         get a ScriptStorage component using its scriptStorageID
123 
124         @params scriptStorageID
125         the usigned short returned by one of the methods above. ID=0 is
126         reserved for the application/share scripts, and ID=1 is reserved
127         for the application/user scripts
128 
129         @returns an XInterface to a component that implements the ScriptStorage
130         service
131     */
132     virtual css::uno::Reference< css::uno::XInterface > SAL_CALL getScriptStorage(
133         sal_Int32 scriptStorageID )
134         throw ( css::uno::RuntimeException );
135 
136     /**
137         get a ScriptStorage ID using storage URI
138 
139         @param scriptStorageURI
140         the file URL for the document will retrieve storage id for the document,        special treatment is reserved for the strings "USER"and "SHARE" which
141         retrieve storage id for application/user application/share areas respectively.
142 
143 
144         @returns as long ScriptStorage ID (-1 if no storage exists), which can be used in getScriptStorage method.
145 
146     */
147 
148     virtual sal_Int32 SAL_CALL getScriptStorageID( const ::rtl::OUString& scriptStorageURI )
149         throw (::com::sun::star::uno::RuntimeException);
150 
151     /**
152         refresh a storage component using its URI
153 
154         @param StringURI
155         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.
156 
157     */
158 
159     virtual void SAL_CALL refreshScriptStorage(const ::rtl::OUString & stringURI)
160 	throw ( css::uno::RuntimeException );
161     //======================================================================
162 
163     //XScriptSecurity
164     //======================================================================
165     //----------------------------------------------------------------------
166     /**
167         the language independent interface for invocation
168 
169         @param scriptURI
170             a string containing the script URI
171 
172         @returns
173             the value returned from the function being invoked
174 
175         @throws IllegalArgumentException
176             if there is no matching script name
177 
178     */
179     virtual void SAL_CALL ScriptStorageManager::checkPermission(
180         const rtl::OUString & scriptStorageURI,
181         const rtl::OUString & permissionRequest )
182         throw ( css::uno::RuntimeException, css::lang::IllegalArgumentException,
183             css::security::AccessControlException );
184     //======================================================================
185 
186     //XEventListener
187     //======================================================================
188 
189     virtual void SAL_CALL disposing( const css::lang::EventObject& Source )
190         throw ( css::uno::RuntimeException );
191 
192 private:
193     ScriptStorageManager( const ScriptStorageManager & );
194     ScriptStorageManager& operator= ( const ScriptStorageManager & );
195 
196     void removeScriptDocURIHashEntry( const ::rtl::OUString & origURI );
197 
198     // to obtain other services if needed
199     css::uno::Reference< css::uno::XComponentContext > m_xContext;
200     css::uno::Reference< css::lang::XMultiComponentFactory > m_xMgr;
201     ::osl::Mutex m_mutex;
202     ScriptStorage_map m_ScriptStorageMap;
203     StorageId_hash m_StorageIdOrigURIHash;
204     sal_Int32 m_count;
205     scripting_securitymgr::ScriptSecurityManager m_securityMgr;
206 
207     void setupAppStorage( const css::uno::Reference< css::util::XMacroExpander > & xME,
208         const ::rtl::OUString & storageStr,
209         const ::rtl::OUString & appStr)
210         SAL_THROW ( ( css::uno::RuntimeException ) );
211 
212     sal_Int32 setupAnyStorage(
213         const css::uno::Reference< css::ucb::XSimpleFileAccess> & xSFA,
214         const ::rtl::OUString & storageStr,
215         const ::rtl::OUString & origStringURI )
216         SAL_THROW ( ( css::uno::RuntimeException ) );
217 
218 };
219 } // scripting_impl
220 
221 #endif //_COM_SUN_STAR_SCRIPTING_STORAGE_SCRIPTSTORAGEMANAGER_HXX_
222