xref: /trunk/main/scripting/source/storage/ScriptStorageManager.hxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 
69 
70     ~ScriptStorageManager() SAL_THROW ( () );
71 
72     // XServiceInfo implementation
73     //======================================================================
74     virtual ::rtl::OUString SAL_CALL getImplementationName();
75     //----------------------------------------------------------------------
76     virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName );
77     //----------------------------------------------------------------------
78     virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames();
79     //======================================================================
80 
81     //XScriptStorageManager
82     //======================================================================
83     /**
84         create a ScriptStorage using the XSimpleFileAccess passed as an
85         argument, and return a ID for getting the associated ScriptStorage
86 
87         @params xSFA
88         an implementation of XSimpleFileAccess that knows its own base URL
89         and can thus take URLs relative to that base.
90 
91         @returns an unsigned short ScriptStorage ID, which can be used in the
92         getScriptStorage method
93     */
94     virtual sal_Int32 SAL_CALL createScriptStorage(
95             const css::uno::Reference< css::ucb::XSimpleFileAccess > & xSFA );
96     //----------------------------------------------------------------------
97     /**
98         create a ScriptStorage using the XSimpleFileAccess, and a string URL
99         and return a ID for getting the associated ScriptStorage
100 
101         @params xSFA
102         a standard implementation of XSimpleFileAccess
103 
104         @params stringURI
105         a string URI to the head of the script storage
106 
107         @returns an unsigned short ScriptStorage ID, which can be used in the
108         getScriptStorage method
109     */
110     virtual sal_Int32 SAL_CALL
111         createScriptStorageWithURI(
112             const css::uno::Reference< css::ucb::XSimpleFileAccess >& xSFA,
113             const ::rtl::OUString& stringURI );
114     //----------------------------------------------------------------------
115     /**
116         get a ScriptStorage component using its scriptStorageID
117 
118         @params scriptStorageID
119         the usigned short returned by one of the methods above. ID=0 is
120         reserved for the application/share scripts, and ID=1 is reserved
121         for the application/user scripts
122 
123         @returns an XInterface to a component that implements the ScriptStorage
124         service
125     */
126     virtual css::uno::Reference< css::uno::XInterface > SAL_CALL getScriptStorage(
127         sal_Int32 scriptStorageID );
128 
129     /**
130         get a ScriptStorage ID using storage URI
131 
132         @param scriptStorageURI
133         the file URL for the document will retrieve storage id for the document,        special treatment is reserved for the strings "USER"and "SHARE" which
134         retrieve storage id for application/user application/share areas respectively.
135 
136 
137         @returns as long ScriptStorage ID (-1 if no storage exists), which can be used in getScriptStorage method.
138 
139     */
140 
141     virtual sal_Int32 SAL_CALL getScriptStorageID( const ::rtl::OUString& scriptStorageURI );
142 
143     /**
144         refresh a storage component using its URI
145 
146         @param StringURI
147         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.
148 
149     */
150 
151     virtual void SAL_CALL refreshScriptStorage(const ::rtl::OUString & stringURI);
152     //======================================================================
153 
154     //XScriptSecurity
155     //======================================================================
156     //----------------------------------------------------------------------
157     /**
158         the language independent interface for invocation
159 
160         @param scriptURI
161             a string containing the script URI
162 
163         @returns
164             the value returned from the function being invoked
165 
166         @throws IllegalArgumentException
167             if there is no matching script name
168 
169     */
170     virtual void SAL_CALL ScriptStorageManager::checkPermission(
171         const rtl::OUString & scriptStorageURI,
172         const rtl::OUString & permissionRequest );
173     //======================================================================
174 
175     //XEventListener
176     //======================================================================
177 
178     virtual void SAL_CALL disposing( const css::lang::EventObject& Source );
179 
180 private:
181     ScriptStorageManager( const ScriptStorageManager & );
182     ScriptStorageManager& operator= ( const ScriptStorageManager & );
183 
184     void removeScriptDocURIHashEntry( const ::rtl::OUString & origURI );
185 
186     // to obtain other services if needed
187     css::uno::Reference< css::uno::XComponentContext > m_xContext;
188     css::uno::Reference< css::lang::XMultiComponentFactory > m_xMgr;
189     ::osl::Mutex m_mutex;
190     ScriptStorage_map m_ScriptStorageMap;
191     StorageId_hash m_StorageIdOrigURIHash;
192     sal_Int32 m_count;
193     scripting_securitymgr::ScriptSecurityManager m_securityMgr;
194 
195     void setupAppStorage( const css::uno::Reference< css::util::XMacroExpander > & xME,
196         const ::rtl::OUString & storageStr,
197         const ::rtl::OUString & appStr);
198 
199     sal_Int32 setupAnyStorage(
200         const css::uno::Reference< css::ucb::XSimpleFileAccess> & xSFA,
201         const ::rtl::OUString & storageStr,
202         const ::rtl::OUString & origStringURI );
203 
204 };
205 } // scripting_impl
206 
207 #endif //_COM_SUN_STAR_SCRIPTING_STORAGE_SCRIPTSTORAGEMANAGER_HXX_
208