2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "LocalFileSystem.h"
34 #if ENABLE(FILE_SYSTEM)
36 #include "AsyncFileSystem.h"
37 #include "CrossThreadTask.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>
56 using namespace WebKit;
60 LocalFileSystem& LocalFileSystem::localFileSystem()
62 AtomicallyInitializedStatic(LocalFileSystem*, localFileSystem = new LocalFileSystem(""));
63 return *localFileSystem;
75 static const char allowFileSystemMode[] = "allowFileSystemMode";
76 static const char openFileSystemMode[] = "openFileSystemMode";
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> {
82 static PassRefPtr<AllowFileSystemMainThreadBridge> create(WebCore::WorkerLoaderProxy* workerLoaderProxy, const WTF::String& mode, NewWebCommonWorkerClient* commonClient)
84 return adoptRef(new AllowFileSystemMainThreadBridge(workerLoaderProxy, mode, commonClient));
87 // These methods are invoked on the worker context.
90 MutexLocker locker(m_mutex);
91 m_workerLoaderProxy = 0;
99 // This method is invoked on the main thread.
100 void signalCompleted(bool result)
102 MutexLocker locker(m_mutex);
103 if (m_workerLoaderProxy)
104 m_workerLoaderProxy->postTaskForModeToWorkerContext(
105 createCallbackTask(&didComplete, AllowCrossThreadAccess(this), result), m_mode);
109 AllowFileSystemMainThreadBridge(WebCore::WorkerLoaderProxy* workerLoaderProxy, const WTF::String& mode, NewWebCommonWorkerClient* commonClient)
110 : m_workerLoaderProxy(workerLoaderProxy)
113 WebWorkerBase::dispatchTaskToMainThread(
114 createCallbackTask(&allowFileSystemTask, AllowCrossThreadAccess(commonClient),
115 AllowCrossThreadAccess(this)));
118 static void allowFileSystemTask(WebCore::ScriptExecutionContext* context, NewWebCommonWorkerClient* commonClient, PassRefPtr<AllowFileSystemMainThreadBridge> bridge)
121 bridge->signalCompleted(commonClient->allowFileSystem());
123 bridge->signalCompleted(false);
126 static void didComplete(WebCore::ScriptExecutionContext* context, PassRefPtr<AllowFileSystemMainThreadBridge> bridge, bool result)
128 bridge->m_result = result;
133 WebCore::WorkerLoaderProxy* m_workerLoaderProxy;
137 bool allowFileSystemForWorker(NewWebCommonWorkerClient* commonClient)
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();
145 // Create a unique mode just for this synchronous call.
146 String mode = allowFileSystemMode;
147 mode.append(String::number(runLoop.createUniqueId()));
149 RefPtr<AllowFileSystemMainThreadBridge> bridge = AllowFileSystemMainThreadBridge::create(workerLoaderProxy, mode, commonClient);
151 // Either the bridge returns, or the queue gets terminated.
152 if (runLoop.runInMode(workerContext, mode) == MessageQueueTerminated) {
157 return bridge->result();
160 void openFileSystemForWorker(NewWebCommonWorkerClient* commonClient, WebFileSystem::Type type, long long size, bool create, WebFileSystemCallbacks* callbacks, bool synchronous)
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();
168 // Create a unique mode for this openFileSystem call.
169 String mode = openFileSystemMode;
170 mode.append(String::number(runLoop.createUniqueId()));
172 RefPtr<WorkerFileSystemCallbacksBridge> bridge = WorkerFileSystemCallbacksBridge::create(workerLoaderProxy, workerContext, callbacks);
173 bridge->postOpenFileSystemToMainThread(commonClient, type, size, create, mode);
176 if (runLoop.runInMode(workerContext, mode) == MessageQueueTerminated)
181 #endif // ENABLE(WORKERS)
185 static void openFileSystemNotAllowed(ScriptExecutionContext*, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
187 callbacks->didFail(WebKit::WebFileErrorAbort);
190 static void openFileSystemHelper(ScriptExecutionContext* context, AsyncFileSystem::Type type, PassOwnPtr<AsyncFileSystemCallbacks> callbacks, bool synchronous, long long size, CreationFlag create)
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))
201 webFrame->client()->openFileSystem(webFrame, static_cast<WebFileSystem::Type>(type), size, create == CreateIfNotPresent, new WebFileSystemCallbacksImpl(callbacks, type));
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()))
210 openFileSystemForWorker(webWorker->newCommonClient(), static_cast<WebFileSystem::Type>(type), size, create == CreateIfNotPresent, new WebFileSystemCallbacksImpl(callbacks, type, context, synchronous), synchronous);
213 ASSERT_NOT_REACHED();
218 // The tasks are expected to be called asynchronously.
219 context->postTask(createCallbackTask(&openFileSystemNotAllowed, callbacks));
223 void LocalFileSystem::readFileSystem(ScriptExecutionContext* context, AsyncFileSystem::Type type, PassOwnPtr<AsyncFileSystemCallbacks> callbacks, bool synchronous)
225 openFileSystemHelper(context, type, callbacks, synchronous, 0, OpenExisting);
228 void LocalFileSystem::requestFileSystem(ScriptExecutionContext* context, AsyncFileSystem::Type type, long long size, PassOwnPtr<AsyncFileSystemCallbacks> callbacks, bool synchronous)
230 openFileSystemHelper(context, type, callbacks, synchronous, size, CreateIfNotPresent);
233 } // namespace WebCore
235 #endif // ENABLE(FILE_SYSTEM)