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 "modules/filesystem/FileSystemCallbacks.h"
34 #include "core/dom/ExecutionContext.h"
35 #include "core/fileapi/FileError.h"
36 #include "core/html/VoidCallback.h"
37 #include "modules/filesystem/DOMFilePath.h"
38 #include "modules/filesystem/DOMFileSystem.h"
39 #include "modules/filesystem/DOMFileSystemBase.h"
40 #include "modules/filesystem/DirectoryEntry.h"
41 #include "modules/filesystem/DirectoryReader.h"
42 #include "modules/filesystem/Entry.h"
43 #include "modules/filesystem/EntryCallback.h"
44 #include "modules/filesystem/ErrorCallback.h"
45 #include "modules/filesystem/FileEntry.h"
46 #include "modules/filesystem/FileSystemCallback.h"
47 #include "modules/filesystem/FileWriterBase.h"
48 #include "modules/filesystem/FileWriterBaseCallback.h"
49 #include "modules/filesystem/Metadata.h"
50 #include "modules/filesystem/MetadataCallback.h"
51 #include "platform/FileMetadata.h"
52 #include "public/platform/WebFileWriter.h"
56 FileSystemCallbacksBase::FileSystemCallbacksBase(PassOwnPtr<ErrorCallback> errorCallback, DOMFileSystemBase* fileSystem, ExecutionContext* context)
57 : m_errorCallback(errorCallback)
58 , m_fileSystem(fileSystem)
59 , m_executionContext(context)
62 m_fileSystem->addPendingCallbacks();
65 FileSystemCallbacksBase::~FileSystemCallbacksBase()
68 m_fileSystem->removePendingCallbacks();
71 void FileSystemCallbacksBase::didFail(int code)
74 handleEventOrScheduleCallback(m_errorCallback.release(), FileError::create(static_cast<FileError::ErrorCode>(code)));
77 bool FileSystemCallbacksBase::shouldScheduleCallback() const
79 return !shouldBlockUntilCompletion() && m_executionContext && m_executionContext->activeDOMObjectsAreSuspended();
82 template <typename CB, typename CBArg>
83 void FileSystemCallbacksBase::handleEventOrScheduleCallback(PassOwnPtr<CB> callback, CBArg* arg)
85 ASSERT(callback.get());
86 if (shouldScheduleCallback())
87 DOMFileSystem::scheduleCallback(m_executionContext.get(), callback, arg);
89 callback->handleEvent(arg);
90 m_executionContext.clear();
93 template <typename CB, typename CBArg>
94 void FileSystemCallbacksBase::handleEventOrScheduleCallback(PassOwnPtr<CB> callback, PassRefPtrWillBeRawPtr<CBArg> arg)
96 ASSERT(callback.get());
97 if (shouldScheduleCallback())
98 DOMFileSystem::scheduleCallback(m_executionContext.get(), callback, arg);
100 callback->handleEvent(arg.get());
101 m_executionContext.clear();
104 template <typename CB>
105 void FileSystemCallbacksBase::handleEventOrScheduleCallback(PassOwnPtr<CB> callback)
107 ASSERT(callback.get());
108 if (shouldScheduleCallback())
109 DOMFileSystem::scheduleCallback(m_executionContext.get(), callback);
111 callback->handleEvent();
112 m_executionContext.clear();
115 // EntryCallbacks -------------------------------------------------------------
117 PassOwnPtr<AsyncFileSystemCallbacks> EntryCallbacks::create(PassOwnPtr<EntryCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, ExecutionContext* context, DOMFileSystemBase* fileSystem, const String& expectedPath, bool isDirectory)
119 return adoptPtr(new EntryCallbacks(successCallback, errorCallback, context, fileSystem, expectedPath, isDirectory));
122 EntryCallbacks::EntryCallbacks(PassOwnPtr<EntryCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, ExecutionContext* context, DOMFileSystemBase* fileSystem, const String& expectedPath, bool isDirectory)
123 : FileSystemCallbacksBase(errorCallback, fileSystem, context)
124 , m_successCallback(successCallback)
125 , m_expectedPath(expectedPath)
126 , m_isDirectory(isDirectory)
130 void EntryCallbacks::didSucceed()
132 if (m_successCallback) {
134 handleEventOrScheduleCallback(m_successCallback.release(), DirectoryEntry::create(m_fileSystem, m_expectedPath));
136 handleEventOrScheduleCallback(m_successCallback.release(), FileEntry::create(m_fileSystem, m_expectedPath));
140 // EntriesCallbacks -----------------------------------------------------------
142 PassOwnPtr<AsyncFileSystemCallbacks> EntriesCallbacks::create(PassOwnPtr<EntriesCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, ExecutionContext* context, DirectoryReaderBase* directoryReader, const String& basePath)
144 return adoptPtr(new EntriesCallbacks(successCallback, errorCallback, context, directoryReader, basePath));
147 EntriesCallbacks::EntriesCallbacks(PassOwnPtr<EntriesCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, ExecutionContext* context, DirectoryReaderBase* directoryReader, const String& basePath)
148 : FileSystemCallbacksBase(errorCallback, directoryReader->filesystem(), context)
149 , m_successCallback(successCallback)
150 , m_directoryReader(directoryReader)
151 , m_basePath(basePath)
153 ASSERT(m_directoryReader);
156 void EntriesCallbacks::didReadDirectoryEntry(const String& name, bool isDirectory)
159 m_entries.append(DirectoryEntry::create(m_directoryReader->filesystem(), DOMFilePath::append(m_basePath, name)));
161 m_entries.append(FileEntry::create(m_directoryReader->filesystem(), DOMFilePath::append(m_basePath, name)));
164 void EntriesCallbacks::didReadDirectoryEntries(bool hasMore)
166 m_directoryReader->setHasMoreEntries(hasMore);
167 EntryHeapVector entries;
168 entries.swap(m_entries);
169 // FIXME: delay the callback iff shouldScheduleCallback() is true.
170 if (m_successCallback)
171 m_successCallback->handleEvent(entries);
174 // FileSystemCallbacks --------------------------------------------------------
176 PassOwnPtr<AsyncFileSystemCallbacks> FileSystemCallbacks::create(PassOwnPtr<FileSystemCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, ExecutionContext* context, FileSystemType type)
178 return adoptPtr(new FileSystemCallbacks(successCallback, errorCallback, context, type));
181 FileSystemCallbacks::FileSystemCallbacks(PassOwnPtr<FileSystemCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, ExecutionContext* context, FileSystemType type)
182 : FileSystemCallbacksBase(errorCallback, nullptr, context)
183 , m_successCallback(successCallback)
188 void FileSystemCallbacks::didOpenFileSystem(const String& name, const KURL& rootURL)
190 if (m_successCallback)
191 handleEventOrScheduleCallback(m_successCallback.release(), DOMFileSystem::create(m_executionContext.get(), name, m_type, rootURL));
194 // ResolveURICallbacks --------------------------------------------------------
196 PassOwnPtr<AsyncFileSystemCallbacks> ResolveURICallbacks::create(PassOwnPtr<EntryCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, ExecutionContext* context)
198 return adoptPtr(new ResolveURICallbacks(successCallback, errorCallback, context));
201 ResolveURICallbacks::ResolveURICallbacks(PassOwnPtr<EntryCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, ExecutionContext* context)
202 : FileSystemCallbacksBase(errorCallback, nullptr, context)
203 , m_successCallback(successCallback)
207 void ResolveURICallbacks::didResolveURL(const String& name, const KURL& rootURL, FileSystemType type, const String& filePath, bool isDirectory)
209 DOMFileSystem* filesystem = DOMFileSystem::create(m_executionContext.get(), name, type, rootURL);
210 DirectoryEntry* root = filesystem->root();
213 if (!DOMFileSystemBase::pathToAbsolutePath(type, root, filePath, absolutePath)) {
214 handleEventOrScheduleCallback(m_errorCallback.release(), FileError::create(FileError::INVALID_MODIFICATION_ERR));
219 handleEventOrScheduleCallback(m_successCallback.release(), DirectoryEntry::create(filesystem, absolutePath));
221 handleEventOrScheduleCallback(m_successCallback.release(), FileEntry::create(filesystem, absolutePath));
224 // MetadataCallbacks ----------------------------------------------------------
226 PassOwnPtr<AsyncFileSystemCallbacks> MetadataCallbacks::create(PassOwnPtr<MetadataCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, ExecutionContext* context, DOMFileSystemBase* fileSystem)
228 return adoptPtr(new MetadataCallbacks(successCallback, errorCallback, context, fileSystem));
231 MetadataCallbacks::MetadataCallbacks(PassOwnPtr<MetadataCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, ExecutionContext* context, DOMFileSystemBase* fileSystem)
232 : FileSystemCallbacksBase(errorCallback, fileSystem, context)
233 , m_successCallback(successCallback)
237 void MetadataCallbacks::didReadMetadata(const FileMetadata& metadata)
239 if (m_successCallback)
240 handleEventOrScheduleCallback(m_successCallback.release(), Metadata::create(metadata));
243 // FileWriterBaseCallbacks ----------------------------------------------------------
245 PassOwnPtr<AsyncFileSystemCallbacks> FileWriterBaseCallbacks::create(PassRefPtrWillBeRawPtr<FileWriterBase> fileWriter, PassOwnPtr<FileWriterBaseCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, ExecutionContext* context)
247 return adoptPtr(new FileWriterBaseCallbacks(fileWriter, successCallback, errorCallback, context));
250 FileWriterBaseCallbacks::FileWriterBaseCallbacks(PassRefPtrWillBeRawPtr<FileWriterBase> fileWriter, PassOwnPtr<FileWriterBaseCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, ExecutionContext* context)
251 : FileSystemCallbacksBase(errorCallback, nullptr, context)
252 , m_fileWriter(fileWriter.get())
253 , m_successCallback(successCallback)
257 void FileWriterBaseCallbacks::didCreateFileWriter(PassOwnPtr<blink::WebFileWriter> fileWriter, long long length)
259 m_fileWriter->initialize(fileWriter, length);
260 if (m_successCallback)
261 handleEventOrScheduleCallback(m_successCallback.release(), m_fileWriter.release());
264 // VoidCallbacks --------------------------------------------------------------
266 PassOwnPtr<AsyncFileSystemCallbacks> VoidCallbacks::create(PassOwnPtr<VoidCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, ExecutionContext* context, DOMFileSystemBase* fileSystem)
268 return adoptPtr(new VoidCallbacks(successCallback, errorCallback, context, fileSystem));
271 VoidCallbacks::VoidCallbacks(PassOwnPtr<VoidCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, ExecutionContext* context, DOMFileSystemBase* fileSystem)
272 : FileSystemCallbacksBase(errorCallback, fileSystem, context)
273 , m_successCallback(successCallback)
277 void VoidCallbacks::didSucceed()
279 if (m_successCallback)
280 handleEventOrScheduleCallback(m_successCallback.release());