tizen beta release
[framework/web/webkit-efl.git] / Source / WebKit / chromium / src / LocalFileSystemChromium.cpp
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "LocalFileSystem.h"
33
34 #if ENABLE(FILE_SYSTEM)
35
36 #include "AsyncFileSystem.h"
37 #include "CrossThreadTask.h"
38 #include "Document.h"
39 #include "ErrorCallback.h"
40 #include "FileSystemCallback.h"
41 #include "FileSystemCallbacks.h"
42 #include "PlatformString.h"
43 #include "WebFileError.h"
44 #include "WebFileSystem.h"
45 #include "WebFileSystemCallbacksImpl.h"
46 #include "WebFrameClient.h"
47 #include "WebFrameImpl.h"
48 #include "WebPermissionClient.h"
49 #include "WebViewImpl.h"
50 #include "WebWorkerBase.h"
51 #include "WorkerContext.h"
52 #include "WorkerFileSystemCallbacksBridge.h"
53 #include "WorkerThread.h"
54 #include <wtf/Threading.h>
55
56 using namespace WebKit;
57
58 namespace WebCore {
59
60 LocalFileSystem& LocalFileSystem::localFileSystem()
61 {
62     AtomicallyInitializedStatic(LocalFileSystem*, localFileSystem = new LocalFileSystem(""));
63     return *localFileSystem;
64 }
65
66 namespace {
67
68 enum CreationFlag {
69     OpenExisting,
70     CreateIfNotPresent
71 };
72
73 #if ENABLE(WORKERS)
74
75 static const char allowFileSystemMode[] = "allowFileSystemMode";
76 static const char openFileSystemMode[] = "openFileSystemMode";
77
78 // This class is used to route the result of the WebWorkerBase::allowFileSystem
79 // call back to the worker context.
80 class AllowFileSystemMainThreadBridge : public ThreadSafeRefCounted<AllowFileSystemMainThreadBridge> {
81 public:
82     static PassRefPtr<AllowFileSystemMainThreadBridge> create(WebCore::WorkerLoaderProxy* workerLoaderProxy, const WTF::String& mode, NewWebCommonWorkerClient* commonClient)
83     {
84         return adoptRef(new AllowFileSystemMainThreadBridge(workerLoaderProxy, mode, commonClient));
85     }
86
87     // These methods are invoked on the worker context.
88     void cancel()
89     {
90         MutexLocker locker(m_mutex);
91         m_workerLoaderProxy = 0;
92     }
93
94     bool result()
95     {
96         return m_result;
97     }
98
99     // This method is invoked on the main thread.
100     void signalCompleted(bool result)
101     {
102         MutexLocker locker(m_mutex);
103         if (m_workerLoaderProxy)
104             m_workerLoaderProxy->postTaskForModeToWorkerContext(
105                 createCallbackTask(&didComplete, AllowCrossThreadAccess(this), result), m_mode);
106     }
107
108 private:
109     AllowFileSystemMainThreadBridge(WebCore::WorkerLoaderProxy* workerLoaderProxy, const WTF::String& mode, NewWebCommonWorkerClient* commonClient)
110         : m_workerLoaderProxy(workerLoaderProxy)
111         , m_mode(mode)
112     {
113         WebWorkerBase::dispatchTaskToMainThread(
114             createCallbackTask(&allowFileSystemTask, AllowCrossThreadAccess(commonClient),
115                                AllowCrossThreadAccess(this)));
116     }
117
118     static void allowFileSystemTask(WebCore::ScriptExecutionContext* context, NewWebCommonWorkerClient* commonClient, PassRefPtr<AllowFileSystemMainThreadBridge> bridge)
119     {
120         if (commonClient)
121             bridge->signalCompleted(commonClient->allowFileSystem());
122         else
123             bridge->signalCompleted(false);
124     }
125
126     static void didComplete(WebCore::ScriptExecutionContext* context, PassRefPtr<AllowFileSystemMainThreadBridge> bridge, bool result)
127     {
128         bridge->m_result = result;
129     }
130
131     bool m_result;
132     Mutex m_mutex;
133     WebCore::WorkerLoaderProxy* m_workerLoaderProxy;
134     WTF::String m_mode;
135 };
136
137 bool allowFileSystemForWorker(NewWebCommonWorkerClient* commonClient)
138 {
139     WorkerScriptController* controller = WorkerScriptController::controllerForContext();
140     WorkerContext* workerContext = controller->workerContext();
141     WebCore::WorkerThread* workerThread = workerContext->thread();
142     WorkerRunLoop& runLoop = workerThread->runLoop();
143     WebCore::WorkerLoaderProxy* workerLoaderProxy =  &workerThread->workerLoaderProxy();
144
145     // Create a unique mode just for this synchronous call.
146     String mode = allowFileSystemMode;
147     mode.append(String::number(runLoop.createUniqueId()));
148
149     RefPtr<AllowFileSystemMainThreadBridge> bridge = AllowFileSystemMainThreadBridge::create(workerLoaderProxy, mode, commonClient);
150
151     // Either the bridge returns, or the queue gets terminated.
152     if (runLoop.runInMode(workerContext, mode) == MessageQueueTerminated) {
153         bridge->cancel();
154         return false;
155     }
156
157     return bridge->result();
158 }
159
160 void openFileSystemForWorker(NewWebCommonWorkerClient* commonClient, WebFileSystem::Type type, long long size, bool create, WebFileSystemCallbacks* callbacks, bool synchronous)
161 {
162     WorkerScriptController* controller = WorkerScriptController::controllerForContext();
163     WorkerContext* workerContext = controller->workerContext();
164     WebCore::WorkerThread* workerThread = workerContext->thread();
165     WorkerRunLoop& runLoop = workerThread->runLoop();
166     WebCore::WorkerLoaderProxy* workerLoaderProxy =  &workerThread->workerLoaderProxy();
167
168     // Create a unique mode for this openFileSystem call.
169     String mode = openFileSystemMode;
170     mode.append(String::number(runLoop.createUniqueId()));
171
172     RefPtr<WorkerFileSystemCallbacksBridge> bridge = WorkerFileSystemCallbacksBridge::create(workerLoaderProxy, workerContext, callbacks);
173     bridge->postOpenFileSystemToMainThread(commonClient, type, size, create, mode);
174
175     if (synchronous) {
176         if (runLoop.runInMode(workerContext, mode) == MessageQueueTerminated)
177             bridge->stop();
178     }
179 }
180
181 #endif // ENABLE(WORKERS)
182
183 } // namespace
184
185 static void openFileSystemNotAllowed(ScriptExecutionContext*, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
186 {
187     callbacks->didFail(WebKit::WebFileErrorAbort);
188 }
189
190 static void openFileSystemHelper(ScriptExecutionContext* context, AsyncFileSystem::Type type, PassOwnPtr<AsyncFileSystemCallbacks> callbacks, bool synchronous, long long size, CreationFlag create)
191 {
192     bool allowed = true;
193     ASSERT(context);
194     if (context->isDocument()) {
195         Document* document = static_cast<Document*>(context);
196         WebFrameImpl* webFrame = WebFrameImpl::fromFrame(document->frame());
197         WebKit::WebViewImpl* webView = webFrame->viewImpl();
198         if (webView->permissionClient() && !webView->permissionClient()->allowFileSystem(webFrame))
199             allowed = false;
200         else
201             webFrame->client()->openFileSystem(webFrame, static_cast<WebFileSystem::Type>(type), size, create == CreateIfNotPresent, new WebFileSystemCallbacksImpl(callbacks, type));
202     } else {
203 #if ENABLE(WORKERS)
204         WorkerContext* workerContext = static_cast<WorkerContext*>(context);
205         WorkerLoaderProxy* workerLoaderProxy = &workerContext->thread()->workerLoaderProxy();
206         NewWebWorkerBase* webWorker = static_cast<NewWebWorkerBase*>(workerLoaderProxy);
207         if (!allowFileSystemForWorker(webWorker->newCommonClient()))
208             allowed = false;
209         else
210             openFileSystemForWorker(webWorker->newCommonClient(), static_cast<WebFileSystem::Type>(type), size, create == CreateIfNotPresent, new WebFileSystemCallbacksImpl(callbacks, type, context, synchronous), synchronous);
211
212 #else
213         ASSERT_NOT_REACHED();
214 #endif
215     }
216
217     if (!allowed) {
218         // The tasks are expected to be called asynchronously.
219         context->postTask(createCallbackTask(&openFileSystemNotAllowed, callbacks));
220     }
221 }
222
223 void LocalFileSystem::readFileSystem(ScriptExecutionContext* context, AsyncFileSystem::Type type, PassOwnPtr<AsyncFileSystemCallbacks> callbacks, bool synchronous)
224 {
225     openFileSystemHelper(context, type, callbacks, synchronous, 0, OpenExisting);
226 }
227
228 void LocalFileSystem::requestFileSystem(ScriptExecutionContext* context, AsyncFileSystem::Type type, long long size, PassOwnPtr<AsyncFileSystemCallbacks> callbacks, bool synchronous)
229 {
230     openFileSystemHelper(context, type, callbacks, synchronous, size, CreateIfNotPresent);
231 }
232
233 } // namespace WebCore
234
235 #endif // ENABLE(FILE_SYSTEM)