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) && ENABLE(WORKERS)
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 "WebKitPlatformSupport.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 WebKit::WebURL& rootURL, bool synchronous)
60 : AsyncFileSystem(type)
61 , m_scriptExecutionContext(context)
62 , m_webFileSystem(webKitPlatformSupport()->fileSystem())
63 , m_workerContext(static_cast<WorkerContext*>(context))
64 , m_synchronous(synchronous)
65 , m_filesystemRootURL(rootURL)
67 ASSERT(m_webFileSystem);
68 ASSERT(m_scriptExecutionContext->isWorkerContext());
70 WorkerLoaderProxy* workerLoaderProxy = &m_workerContext->thread()->workerLoaderProxy();
71 m_worker = static_cast<WebWorkerBase*>(workerLoaderProxy);
74 WorkerAsyncFileSystemChromium::~WorkerAsyncFileSystemChromium()
78 bool WorkerAsyncFileSystemChromium::waitForOperationToComplete()
80 if (!m_bridgeForCurrentOperation.get())
83 RefPtr<WorkerFileSystemCallbacksBridge> bridge = m_bridgeForCurrentOperation.release();
84 if (m_workerContext->thread()->runLoop().runInMode(m_workerContext, m_modeForCurrentOperation) == MessageQueueTerminated) {
91 void WorkerAsyncFileSystemChromium::move(const String& sourcePath, const String& destinationPath, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
93 createWorkerFileSystemCallbacksBridge(callbacks)->postMoveToMainThread(m_webFileSystem, virtualPathToFileSystemURL(sourcePath), virtualPathToFileSystemURL(destinationPath), m_modeForCurrentOperation);
96 void WorkerAsyncFileSystemChromium::copy(const String& sourcePath, const String& destinationPath, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
98 createWorkerFileSystemCallbacksBridge(callbacks)->postCopyToMainThread(m_webFileSystem, virtualPathToFileSystemURL(sourcePath), virtualPathToFileSystemURL(destinationPath), m_modeForCurrentOperation);
101 void WorkerAsyncFileSystemChromium::remove(const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
103 createWorkerFileSystemCallbacksBridge(callbacks)->postRemoveToMainThread(m_webFileSystem, virtualPathToFileSystemURL(path), m_modeForCurrentOperation);
106 void WorkerAsyncFileSystemChromium::removeRecursively(const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
108 createWorkerFileSystemCallbacksBridge(callbacks)->postRemoveRecursivelyToMainThread(m_webFileSystem, virtualPathToFileSystemURL(path), m_modeForCurrentOperation);
111 void WorkerAsyncFileSystemChromium::readMetadata(const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
113 createWorkerFileSystemCallbacksBridge(callbacks)->postReadMetadataToMainThread(m_webFileSystem, virtualPathToFileSystemURL(path), m_modeForCurrentOperation);
116 void WorkerAsyncFileSystemChromium::createFile(const String& path, bool exclusive, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
118 createWorkerFileSystemCallbacksBridge(callbacks)->postCreateFileToMainThread(m_webFileSystem, virtualPathToFileSystemURL(path), exclusive, m_modeForCurrentOperation);
121 void WorkerAsyncFileSystemChromium::createDirectory(const String& path, bool exclusive, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
123 createWorkerFileSystemCallbacksBridge(callbacks)->postCreateDirectoryToMainThread(m_webFileSystem, virtualPathToFileSystemURL(path), exclusive, m_modeForCurrentOperation);
126 void WorkerAsyncFileSystemChromium::fileExists(const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
128 createWorkerFileSystemCallbacksBridge(callbacks)->postFileExistsToMainThread(m_webFileSystem, virtualPathToFileSystemURL(path), m_modeForCurrentOperation);
131 void WorkerAsyncFileSystemChromium::directoryExists(const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
133 createWorkerFileSystemCallbacksBridge(callbacks)->postDirectoryExistsToMainThread(m_webFileSystem, virtualPathToFileSystemURL(path), m_modeForCurrentOperation);
136 void WorkerAsyncFileSystemChromium::readDirectory(const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
138 createWorkerFileSystemCallbacksBridge(callbacks)->postReadDirectoryToMainThread(m_webFileSystem, virtualPathToFileSystemURL(path), m_modeForCurrentOperation);
141 class WorkerFileWriterHelperCallbacks : public AsyncFileSystemCallbacks {
143 static PassOwnPtr<WorkerFileWriterHelperCallbacks> create(AsyncFileWriterClient* client, const WebURL& path, WebKit::WebFileSystem* webFileSystem, PassOwnPtr<WebCore::AsyncFileSystemCallbacks> callbacks, WorkerContext* workerContext)
145 return adoptPtr(new WorkerFileWriterHelperCallbacks(client, path, webFileSystem, callbacks, workerContext));
148 virtual void didSucceed()
150 ASSERT_NOT_REACHED();
153 virtual void didReadMetadata(const FileMetadata& metadata)
156 if (metadata.type != FileMetadata::TypeFile || metadata.length < 0)
157 m_callbacks->didFail(WebKit::WebFileErrorInvalidState);
159 OwnPtr<WorkerAsyncFileWriterChromium> asyncFileWriterChromium = WorkerAsyncFileWriterChromium::create(m_webFileSystem, m_path, m_workerContext, m_client, WorkerAsyncFileWriterChromium::Asynchronous);
160 m_callbacks->didCreateFileWriter(asyncFileWriterChromium.release(), metadata.length);
164 virtual void didReadDirectoryEntry(const String& name, bool isDirectory)
166 ASSERT_NOT_REACHED();
169 virtual void didReadDirectoryEntries(bool hasMore)
171 ASSERT_NOT_REACHED();
174 virtual void didOpenFileSystem(const String&, PassOwnPtr<AsyncFileSystem>)
176 ASSERT_NOT_REACHED();
179 // Called when an AsyncFileWrter has been created successfully.
180 virtual void didCreateFileWriter(PassOwnPtr<AsyncFileWriter>, long long)
182 ASSERT_NOT_REACHED();
185 virtual void didFail(int code)
188 m_callbacks->didFail(code);
192 WorkerFileWriterHelperCallbacks(AsyncFileWriterClient* client, const WebURL& path, WebKit::WebFileSystem* webFileSystem, PassOwnPtr<WebCore::AsyncFileSystemCallbacks> callbacks, WorkerContext* workerContext)
195 , m_webFileSystem(webFileSystem)
196 , m_callbacks(callbacks)
197 , m_workerContext(workerContext)
201 AsyncFileWriterClient* m_client;
203 WebKit::WebFileSystem* m_webFileSystem;
204 OwnPtr<WebCore::AsyncFileSystemCallbacks> m_callbacks;
205 WorkerContext* m_workerContext;
208 void WorkerAsyncFileSystemChromium::createWriter(AsyncFileWriterClient* client, const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
210 KURL pathAsURL = virtualPathToFileSystemURL(path);
211 createWorkerFileSystemCallbacksBridge(WorkerFileWriterHelperCallbacks::create(client, pathAsURL, m_webFileSystem, callbacks, m_workerContext))->postReadMetadataToMainThread(m_webFileSystem, pathAsURL, m_modeForCurrentOperation);
214 PassRefPtr<WorkerFileSystemCallbacksBridge> WorkerAsyncFileSystemChromium::createWorkerFileSystemCallbacksBridge(PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
216 ASSERT(!m_synchronous || !m_bridgeForCurrentOperation.get());
218 m_modeForCurrentOperation = fileSystemOperationsMode;
219 m_modeForCurrentOperation.append(String::number(m_workerContext->thread()->runLoop().createUniqueId()));
221 m_bridgeForCurrentOperation = WorkerFileSystemCallbacksBridge::create(m_worker, m_scriptExecutionContext, new WebKit::WebFileSystemCallbacksImpl(callbacks));
222 return m_bridgeForCurrentOperation;
225 KURL WorkerAsyncFileSystemChromium::virtualPathToFileSystemURL(const String& virtualPath) const
227 ASSERT(!m_filesystemRootURL.isEmpty());
228 KURL url = m_filesystemRootURL;
229 // Remove the extra leading slash.
230 url.setPath(url.path() + encodeWithURLEscapeSequences(virtualPath.substring(1)));
234 } // namespace WebCore
236 #endif // ENABLE(FILE_SYSTEM)