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 "WorkerAsyncFileSystemChromium.h"
34 #if ENABLE(FILE_SYSTEM)
36 #include "AsyncFileSystemCallbacks.h"
37 #include "FileMetadata.h"
38 #include "FileSystem.h"
39 #include "NotImplemented.h"
40 #include "WebFileSystem.h"
41 #include "WebFileSystemCallbacksImpl.h"
42 #include "WebFileWriter.h"
44 #include "WebKitClient.h"
45 #include "WebWorkerBase.h"
46 #include "WorkerAsyncFileWriterChromium.h"
47 #include "WorkerContext.h"
48 #include "WorkerFileSystemCallbacksBridge.h"
49 #include "WorkerScriptController.h"
50 #include "WorkerThread.h"
51 #include <wtf/text/CString.h>
53 using namespace WebKit;
57 static const char fileSystemOperationsMode[] = "fileSystemOperationsMode";
59 WorkerAsyncFileSystemChromium::WorkerAsyncFileSystemChromium(ScriptExecutionContext* context, AsyncFileSystem::Type type, const String& rootPath, bool synchronous)
60 : AsyncFileSystem(type, rootPath)
61 , m_scriptExecutionContext(context)
62 , m_webFileSystem(WebKit::webKitClient()->fileSystem())
63 , m_workerContext(static_cast<WorkerContext*>(context))
64 , m_synchronous(synchronous)
66 ASSERT(m_webFileSystem);
67 ASSERT(m_scriptExecutionContext->isWorkerContext());
69 WorkerLoaderProxy* workerLoaderProxy = &m_workerContext->thread()->workerLoaderProxy();
70 m_worker = static_cast<WebWorkerBase*>(workerLoaderProxy);
73 WorkerAsyncFileSystemChromium::~WorkerAsyncFileSystemChromium()
77 bool WorkerAsyncFileSystemChromium::waitForOperationToComplete()
79 if (!m_bridgeForCurrentOperation.get())
82 RefPtr<WorkerFileSystemCallbacksBridge> bridge = m_bridgeForCurrentOperation.release();
83 if (m_workerContext->thread()->runLoop().runInMode(m_workerContext, m_modeForCurrentOperation) == MessageQueueTerminated) {
90 void WorkerAsyncFileSystemChromium::move(const String& sourcePath, const String& destinationPath, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
92 createWorkerFileSystemCallbacksBridge(callbacks)->postMoveToMainThread(m_webFileSystem, sourcePath, destinationPath, m_modeForCurrentOperation);
95 void WorkerAsyncFileSystemChromium::copy(const String& sourcePath, const String& destinationPath, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
97 createWorkerFileSystemCallbacksBridge(callbacks)->postCopyToMainThread(m_webFileSystem, sourcePath, destinationPath, m_modeForCurrentOperation);
100 void WorkerAsyncFileSystemChromium::remove(const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
102 createWorkerFileSystemCallbacksBridge(callbacks)->postRemoveToMainThread(m_webFileSystem, path, m_modeForCurrentOperation);
105 void WorkerAsyncFileSystemChromium::removeRecursively(const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
107 createWorkerFileSystemCallbacksBridge(callbacks)->postRemoveRecursivelyToMainThread(m_webFileSystem, path, m_modeForCurrentOperation);
110 void WorkerAsyncFileSystemChromium::readMetadata(const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
112 createWorkerFileSystemCallbacksBridge(callbacks)->postReadMetadataToMainThread(m_webFileSystem, path, m_modeForCurrentOperation);
115 void WorkerAsyncFileSystemChromium::createFile(const String& path, bool exclusive, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
117 createWorkerFileSystemCallbacksBridge(callbacks)->postCreateFileToMainThread(m_webFileSystem, path, exclusive, m_modeForCurrentOperation);
120 void WorkerAsyncFileSystemChromium::createDirectory(const String& path, bool exclusive, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
122 createWorkerFileSystemCallbacksBridge(callbacks)->postCreateDirectoryToMainThread(m_webFileSystem, path, exclusive, m_modeForCurrentOperation);
125 void WorkerAsyncFileSystemChromium::fileExists(const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
127 createWorkerFileSystemCallbacksBridge(callbacks)->postFileExistsToMainThread(m_webFileSystem, path, m_modeForCurrentOperation);
130 void WorkerAsyncFileSystemChromium::directoryExists(const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
132 createWorkerFileSystemCallbacksBridge(callbacks)->postDirectoryExistsToMainThread(m_webFileSystem, path, m_modeForCurrentOperation);
135 void WorkerAsyncFileSystemChromium::readDirectory(const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
137 createWorkerFileSystemCallbacksBridge(callbacks)->postReadDirectoryToMainThread(m_webFileSystem, path, m_modeForCurrentOperation);
140 class WorkerFileWriterHelperCallbacks : public AsyncFileSystemCallbacks {
142 static PassOwnPtr<WorkerFileWriterHelperCallbacks> create(AsyncFileWriterClient* client, const String& path, WebKit::WebFileSystem* webFileSystem, PassOwnPtr<WebCore::AsyncFileSystemCallbacks> callbacks, WorkerContext* workerContext)
144 return adoptPtr(new WorkerFileWriterHelperCallbacks(client, path, webFileSystem, callbacks, workerContext));
147 virtual void didSucceed()
149 ASSERT_NOT_REACHED();
152 virtual void didReadMetadata(const FileMetadata& metadata)
155 if (metadata.type != FileMetadata::TypeFile || metadata.length < 0)
156 m_callbacks->didFail(WebKit::WebFileErrorInvalidState);
158 OwnPtr<WorkerAsyncFileWriterChromium> asyncFileWriterChromium = WorkerAsyncFileWriterChromium::create(m_webFileSystem, m_path, m_workerContext, m_client, WorkerAsyncFileWriterChromium::Asynchronous);
159 m_callbacks->didCreateFileWriter(asyncFileWriterChromium.release(), metadata.length);
163 virtual void didReadDirectoryEntry(const String& name, bool isDirectory)
165 ASSERT_NOT_REACHED();
168 virtual void didReadDirectoryEntries(bool hasMore)
170 ASSERT_NOT_REACHED();
173 virtual void didOpenFileSystem(const String&, PassOwnPtr<AsyncFileSystem>)
175 ASSERT_NOT_REACHED();
178 // Called when an AsyncFileWrter has been created successfully.
179 virtual void didCreateFileWriter(PassOwnPtr<AsyncFileWriter>, long long)
181 ASSERT_NOT_REACHED();
184 virtual void didFail(int code)
187 m_callbacks->didFail(code);
191 WorkerFileWriterHelperCallbacks(AsyncFileWriterClient* client, const String& path, WebKit::WebFileSystem* webFileSystem, PassOwnPtr<WebCore::AsyncFileSystemCallbacks> callbacks, WorkerContext* workerContext)
194 , m_webFileSystem(webFileSystem)
195 , m_callbacks(callbacks)
196 , m_workerContext(workerContext)
200 AsyncFileWriterClient* m_client;
202 WebKit::WebFileSystem* m_webFileSystem;
203 OwnPtr<WebCore::AsyncFileSystemCallbacks> m_callbacks;
204 WorkerContext* m_workerContext;
207 void WorkerAsyncFileSystemChromium::createWriter(AsyncFileWriterClient* client, const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
209 createWorkerFileSystemCallbacksBridge(WorkerFileWriterHelperCallbacks::create(client, path, m_webFileSystem, callbacks, m_workerContext))->postReadMetadataToMainThread(m_webFileSystem, path, m_modeForCurrentOperation);
212 PassRefPtr<WorkerFileSystemCallbacksBridge> WorkerAsyncFileSystemChromium::createWorkerFileSystemCallbacksBridge(PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
214 ASSERT(!m_synchronous || !m_bridgeForCurrentOperation.get());
216 m_modeForCurrentOperation = fileSystemOperationsMode;
217 m_modeForCurrentOperation.append(String::number(m_workerContext->thread()->runLoop().createUniqueId()));
219 m_bridgeForCurrentOperation = WorkerFileSystemCallbacksBridge::create(m_worker, m_scriptExecutionContext, new WebKit::WebFileSystemCallbacksImpl(callbacks));
220 return m_bridgeForCurrentOperation;
223 } // namespace WebCore
225 #endif // ENABLE(FILE_SYSTEM)