initial import
[vuplus_webkit] / Source / WebKit / chromium / src / WorkerAsyncFileSystemChromium.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 "WorkerAsyncFileSystemChromium.h"
33
34 #if ENABLE(FILE_SYSTEM) && ENABLE(WORKERS)
35
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"
43 #include "WebKit.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>
52
53 using namespace WebKit;
54
55 namespace WebCore {
56
57 static const char fileSystemOperationsMode[] = "fileSystemOperationsMode";
58
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)
66 {
67     ASSERT(m_webFileSystem);
68     ASSERT(m_scriptExecutionContext->isWorkerContext());
69
70     WorkerLoaderProxy* workerLoaderProxy = &m_workerContext->thread()->workerLoaderProxy();
71     m_worker = static_cast<WebWorkerBase*>(workerLoaderProxy);
72 }
73
74 WorkerAsyncFileSystemChromium::~WorkerAsyncFileSystemChromium()
75 {
76 }
77
78 bool WorkerAsyncFileSystemChromium::waitForOperationToComplete()
79 {
80     if (!m_bridgeForCurrentOperation.get())
81         return false;
82
83     RefPtr<WorkerFileSystemCallbacksBridge> bridge = m_bridgeForCurrentOperation.release();
84     if (m_workerContext->thread()->runLoop().runInMode(m_workerContext, m_modeForCurrentOperation) == MessageQueueTerminated) {
85         bridge->stop();
86         return false;
87     }
88     return true;
89 }
90
91 void WorkerAsyncFileSystemChromium::move(const String& sourcePath, const String& destinationPath, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
92 {
93     createWorkerFileSystemCallbacksBridge(callbacks)->postMoveToMainThread(m_webFileSystem, virtualPathToFileSystemURL(sourcePath), virtualPathToFileSystemURL(destinationPath), m_modeForCurrentOperation);
94 }
95
96 void WorkerAsyncFileSystemChromium::copy(const String& sourcePath, const String& destinationPath, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
97 {
98     createWorkerFileSystemCallbacksBridge(callbacks)->postCopyToMainThread(m_webFileSystem, virtualPathToFileSystemURL(sourcePath), virtualPathToFileSystemURL(destinationPath), m_modeForCurrentOperation);
99 }
100
101 void WorkerAsyncFileSystemChromium::remove(const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
102 {
103     createWorkerFileSystemCallbacksBridge(callbacks)->postRemoveToMainThread(m_webFileSystem, virtualPathToFileSystemURL(path), m_modeForCurrentOperation);
104 }
105
106 void WorkerAsyncFileSystemChromium::removeRecursively(const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
107 {
108     createWorkerFileSystemCallbacksBridge(callbacks)->postRemoveRecursivelyToMainThread(m_webFileSystem, virtualPathToFileSystemURL(path), m_modeForCurrentOperation);
109 }
110
111 void WorkerAsyncFileSystemChromium::readMetadata(const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
112 {
113     createWorkerFileSystemCallbacksBridge(callbacks)->postReadMetadataToMainThread(m_webFileSystem, virtualPathToFileSystemURL(path), m_modeForCurrentOperation);
114 }
115
116 void WorkerAsyncFileSystemChromium::createFile(const String& path, bool exclusive, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
117 {
118     createWorkerFileSystemCallbacksBridge(callbacks)->postCreateFileToMainThread(m_webFileSystem, virtualPathToFileSystemURL(path), exclusive, m_modeForCurrentOperation);
119 }
120
121 void WorkerAsyncFileSystemChromium::createDirectory(const String& path, bool exclusive, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
122 {
123     createWorkerFileSystemCallbacksBridge(callbacks)->postCreateDirectoryToMainThread(m_webFileSystem, virtualPathToFileSystemURL(path), exclusive, m_modeForCurrentOperation);
124 }
125
126 void WorkerAsyncFileSystemChromium::fileExists(const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
127 {
128     createWorkerFileSystemCallbacksBridge(callbacks)->postFileExistsToMainThread(m_webFileSystem, virtualPathToFileSystemURL(path), m_modeForCurrentOperation);
129 }
130
131 void WorkerAsyncFileSystemChromium::directoryExists(const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
132 {
133     createWorkerFileSystemCallbacksBridge(callbacks)->postDirectoryExistsToMainThread(m_webFileSystem, virtualPathToFileSystemURL(path), m_modeForCurrentOperation);
134 }
135
136 void WorkerAsyncFileSystemChromium::readDirectory(const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
137 {
138     createWorkerFileSystemCallbacksBridge(callbacks)->postReadDirectoryToMainThread(m_webFileSystem, virtualPathToFileSystemURL(path), m_modeForCurrentOperation);
139 }
140
141 class WorkerFileWriterHelperCallbacks : public AsyncFileSystemCallbacks {
142 public:
143     static PassOwnPtr<WorkerFileWriterHelperCallbacks> create(AsyncFileWriterClient* client, const WebURL& path, WebKit::WebFileSystem* webFileSystem, PassOwnPtr<WebCore::AsyncFileSystemCallbacks> callbacks, WorkerContext* workerContext)
144     {
145         return adoptPtr(new WorkerFileWriterHelperCallbacks(client, path, webFileSystem, callbacks, workerContext));
146     }
147
148     virtual void didSucceed()
149     {
150         ASSERT_NOT_REACHED();
151     }
152
153     virtual void didReadMetadata(const FileMetadata& metadata)
154     {
155         ASSERT(m_callbacks);
156         if (metadata.type != FileMetadata::TypeFile || metadata.length < 0)
157             m_callbacks->didFail(WebKit::WebFileErrorInvalidState);
158         else {
159             OwnPtr<WorkerAsyncFileWriterChromium> asyncFileWriterChromium = WorkerAsyncFileWriterChromium::create(m_webFileSystem, m_path, m_workerContext, m_client, WorkerAsyncFileWriterChromium::Asynchronous);
160             m_callbacks->didCreateFileWriter(asyncFileWriterChromium.release(), metadata.length);
161         }
162     }
163
164     virtual void didReadDirectoryEntry(const String& name, bool isDirectory)
165     {
166         ASSERT_NOT_REACHED();
167     }
168
169     virtual void didReadDirectoryEntries(bool hasMore)
170     {
171         ASSERT_NOT_REACHED();
172     }
173
174     virtual void didOpenFileSystem(const String&, PassOwnPtr<AsyncFileSystem>)
175     {
176         ASSERT_NOT_REACHED();
177     }
178
179     // Called when an AsyncFileWrter has been created successfully.
180     virtual void didCreateFileWriter(PassOwnPtr<AsyncFileWriter>, long long)
181     {
182         ASSERT_NOT_REACHED();
183     }
184
185     virtual void didFail(int code)
186     {
187         ASSERT(m_callbacks);
188         m_callbacks->didFail(code);
189     }
190
191 private:
192     WorkerFileWriterHelperCallbacks(AsyncFileWriterClient* client, const WebURL& path, WebKit::WebFileSystem* webFileSystem, PassOwnPtr<WebCore::AsyncFileSystemCallbacks> callbacks, WorkerContext* workerContext)
193         : m_client(client)
194         , m_path(path)
195         , m_webFileSystem(webFileSystem)
196         , m_callbacks(callbacks)
197         , m_workerContext(workerContext)
198     {
199     }
200
201     AsyncFileWriterClient* m_client;
202     WebURL m_path;
203     WebKit::WebFileSystem* m_webFileSystem;
204     OwnPtr<WebCore::AsyncFileSystemCallbacks> m_callbacks;
205     WorkerContext* m_workerContext;
206 };
207
208 void WorkerAsyncFileSystemChromium::createWriter(AsyncFileWriterClient* client, const String& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
209 {
210     KURL pathAsURL = virtualPathToFileSystemURL(path);
211     createWorkerFileSystemCallbacksBridge(WorkerFileWriterHelperCallbacks::create(client, pathAsURL, m_webFileSystem, callbacks, m_workerContext))->postReadMetadataToMainThread(m_webFileSystem, pathAsURL, m_modeForCurrentOperation);
212 }
213
214 PassRefPtr<WorkerFileSystemCallbacksBridge> WorkerAsyncFileSystemChromium::createWorkerFileSystemCallbacksBridge(PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
215 {
216     ASSERT(!m_synchronous || !m_bridgeForCurrentOperation.get());
217
218     m_modeForCurrentOperation = fileSystemOperationsMode;
219     m_modeForCurrentOperation.append(String::number(m_workerContext->thread()->runLoop().createUniqueId()));
220
221     m_bridgeForCurrentOperation = WorkerFileSystemCallbacksBridge::create(m_worker, m_scriptExecutionContext, new WebKit::WebFileSystemCallbacksImpl(callbacks));
222     return m_bridgeForCurrentOperation;
223 }
224
225 KURL WorkerAsyncFileSystemChromium::virtualPathToFileSystemURL(const String& virtualPath) const
226 {
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)));
231     return url;
232 }
233
234 } // namespace WebCore
235
236 #endif // ENABLE(FILE_SYSTEM)