initial import
[vuplus_webkit] / Source / WebCore / fileapi / DOMFileSystemSync.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 "DOMFileSystemSync.h"
33
34 #if ENABLE(FILE_SYSTEM)
35
36 #include "AsyncFileSystem.h"
37 #include "AsyncFileWriter.h"
38 #include "DOMFilePath.h"
39 #include "DirectoryEntrySync.h"
40 #include "ErrorCallback.h"
41 #include "File.h"
42 #include "FileEntrySync.h"
43 #include "FileError.h"
44 #include "FileException.h"
45 #include "FileMetadata.h"
46 #include "FileSystemCallbacks.h"
47 #include "FileWriterBaseCallback.h"
48 #include "FileWriterSync.h"
49
50 namespace WebCore {
51
52 class FileWriterBase;
53
54 PassRefPtr<DOMFileSystemSync> DOMFileSystemSync::create(DOMFileSystemBase* fileSystem)
55 {
56     return adoptRef(new DOMFileSystemSync(fileSystem->m_context, fileSystem->m_name, fileSystem->m_asyncFileSystem.release()));
57 }
58
59 DOMFileSystemSync::DOMFileSystemSync(ScriptExecutionContext* context, const String& name, PassOwnPtr<AsyncFileSystem> asyncFileSystem)
60     : DOMFileSystemBase(context, name, asyncFileSystem)
61 {
62 }
63
64 DOMFileSystemSync::~DOMFileSystemSync()
65 {
66 }
67
68 PassRefPtr<DirectoryEntrySync> DOMFileSystemSync::root()
69 {
70     return DirectoryEntrySync::create(this, DOMFilePath::root);
71 }
72
73 namespace {
74
75 class GetPathHelper : public AsyncFileSystemCallbacks {
76 public:
77     class GetPathResult : public RefCounted<GetPathResult> {
78       public:
79         static PassRefPtr<GetPathResult> create()
80         {
81             return adoptRef(new GetPathResult());
82         }
83
84         bool m_failed;
85         int m_code;
86         String m_path;
87
88       private:
89         GetPathResult()
90             : m_failed(false)
91             , m_code(0)
92         {
93         }
94
95         ~GetPathResult()
96         {
97         }
98         friend class WTF::RefCounted<GetPathResult>;
99     };
100
101     static PassOwnPtr<GetPathHelper> create(PassRefPtr<GetPathResult> result)
102     {
103         return adoptPtr(new GetPathHelper(result));
104     }
105
106     virtual void didSucceed()
107     {
108         ASSERT_NOT_REACHED();
109     }
110
111     virtual void didOpenFileSystem(const String&, PassOwnPtr<AsyncFileSystem>)
112     {
113         ASSERT_NOT_REACHED();
114     }
115
116     virtual void didReadDirectoryEntry(const String&, bool)
117     {
118         ASSERT_NOT_REACHED();
119     }
120
121     virtual void didReadDirectoryEntries(bool)
122     {
123         ASSERT_NOT_REACHED();
124     }
125
126     virtual void didCreateFileWriter(PassOwnPtr<AsyncFileWriter>, long long)
127     {
128         ASSERT_NOT_REACHED();
129     }
130
131     virtual void didFail(int code)
132     {
133         m_result->m_failed = true;
134         m_result->m_code = code;
135     }
136
137     virtual ~GetPathHelper()
138     {
139     }
140
141     void didReadMetadata(const FileMetadata& metadata)
142     {
143         m_result->m_path = metadata.platformPath;
144     }
145 private:
146     GetPathHelper(PassRefPtr<GetPathResult> result)
147         : m_result(result)
148     {
149     }
150
151     RefPtr<GetPathResult> m_result;
152 };
153
154 } // namespace
155
156 PassRefPtr<File> DOMFileSystemSync::createFile(const FileEntrySync* fileEntry, ExceptionCode& ec)
157 {
158     ec = 0;
159     RefPtr<GetPathHelper::GetPathResult> result(GetPathHelper::GetPathResult::create());
160     m_asyncFileSystem->readMetadata(fileEntry->fullPath(), GetPathHelper::create(result));
161     if (!m_asyncFileSystem->waitForOperationToComplete()) {
162         ec = FileException::ABORT_ERR;
163         return 0;
164     }
165     if (result->m_failed) {
166         ec = result->m_code;
167         return 0;
168     }
169     ASSERT(!result->m_path.isEmpty());
170     return File::createWithName(result->m_path, fileEntry->name());
171 }
172
173 namespace {
174
175 class ReceiveFileWriterCallback : public FileWriterBaseCallback {
176 public:
177     static PassRefPtr<ReceiveFileWriterCallback> create()
178     {
179         return adoptRef(new ReceiveFileWriterCallback());
180     }
181
182     bool handleEvent(FileWriterBase* fileWriterBase)
183     {
184 #ifndef NDEBUG
185         m_fileWriterBase = fileWriterBase;
186 #else
187         ASSERT_UNUSED(fileWriterBase, fileWriterBase);
188 #endif
189         return true;
190     }
191
192 #ifndef NDEBUG
193     FileWriterBase* fileWriterBase()
194     {
195         return m_fileWriterBase;
196     }
197 #endif
198
199 private:
200     ReceiveFileWriterCallback()
201 #ifndef NDEBUG
202         : m_fileWriterBase(0)
203 #endif
204     {
205     }
206
207 #ifndef NDEBUG
208     FileWriterBase* m_fileWriterBase;
209 #endif
210 };
211
212 class LocalErrorCallback : public ErrorCallback {
213 public:
214     static PassRefPtr<LocalErrorCallback> create()
215     {
216         return adoptRef(new LocalErrorCallback());
217     }
218
219     bool handleEvent(FileError* error)
220     {
221         m_error = error;
222         return true;
223     }
224
225     FileError* error()
226     {
227         return m_error.get();
228     }
229
230 private:
231     LocalErrorCallback()
232     {
233     }
234     RefPtr<FileError> m_error;
235 };
236
237 }
238
239 PassRefPtr<FileWriterSync> DOMFileSystemSync::createWriter(const FileEntrySync* fileEntry, ExceptionCode& ec)
240 {
241     ASSERT(fileEntry);
242     ec = 0;
243
244
245     RefPtr<FileWriterSync> fileWriter = FileWriterSync::create();
246     RefPtr<ReceiveFileWriterCallback> successCallback = ReceiveFileWriterCallback::create();
247     RefPtr<LocalErrorCallback> errorCallback = LocalErrorCallback::create();
248
249     OwnPtr<FileWriterBaseCallbacks> callbacks = FileWriterBaseCallbacks::create(fileWriter, successCallback, errorCallback);
250     m_asyncFileSystem->createWriter(fileWriter.get(), fileEntry->fullPath(), callbacks.release());
251     if (!m_asyncFileSystem->waitForOperationToComplete()) {
252         ec = FileException::ABORT_ERR;
253         return 0;
254     }
255     if (errorCallback->error()) {
256         ASSERT(!successCallback->fileWriterBase());
257         ec = FileException::ErrorCodeToExceptionCode(errorCallback->error()->code());
258         return 0;
259     }
260     ASSERT(successCallback->fileWriterBase());
261     ASSERT(static_cast<FileWriterSync*>(successCallback->fileWriterBase()) == fileWriter.get());
262     return fileWriter;
263 }
264
265 }
266
267 #endif // ENABLE(FILE_SYSTEM)