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 "DOMFileSystem.h"
34 #if ENABLE(FILE_SYSTEM)
36 #include "AsyncFileSystem.h"
37 #include "DOMFilePath.h"
38 #include "DirectoryEntry.h"
39 #include "ErrorCallback.h"
41 #include "FileCallback.h"
42 #include "FileEntry.h"
43 #include "FileMetadata.h"
44 #include "FileSystemCallbacks.h"
45 #include "FileWriter.h"
46 #include "FileWriterBaseCallback.h"
47 #include "FileWriterCallback.h"
48 #include "InspectorFileSystemInstrumentation.h"
49 #include "MetadataCallback.h"
50 #include "ScriptExecutionContext.h"
51 #include <wtf/OwnPtr.h>
56 PassRefPtr<DOMFileSystem> DOMFileSystem::create(ScriptExecutionContext* context, const String& name, PassOwnPtr<AsyncFileSystem> asyncFileSystem)
58 RefPtr<DOMFileSystem> fileSystem(adoptRef(new DOMFileSystem(context, name, asyncFileSystem)));
59 InspectorInstrumentation::didOpenFileSystem(fileSystem.get());
63 DOMFileSystem::DOMFileSystem(ScriptExecutionContext* context, const String& name, PassOwnPtr<AsyncFileSystem> asyncFileSystem)
64 : DOMFileSystemBase(context, name, asyncFileSystem)
65 , ActiveDOMObject(context, this)
69 PassRefPtr<DirectoryEntry> DOMFileSystem::root()
71 return DirectoryEntry::create(this, DOMFilePath::root);
74 void DOMFileSystem::stop()
76 m_asyncFileSystem->stop();
79 bool DOMFileSystem::hasPendingActivity() const
81 return m_asyncFileSystem->hasPendingActivity();
84 void DOMFileSystem::contextDestroyed()
86 m_asyncFileSystem->stop();
87 ActiveDOMObject::contextDestroyed();
92 class ConvertToFileWriterCallback : public FileWriterBaseCallback {
94 static PassRefPtr<ConvertToFileWriterCallback> create(PassRefPtr<FileWriterCallback> callback)
96 return adoptRef(new ConvertToFileWriterCallback(callback));
99 bool handleEvent(FileWriterBase* fileWriterBase)
101 return m_callback->handleEvent(static_cast<FileWriter*>(fileWriterBase));
104 ConvertToFileWriterCallback(PassRefPtr<FileWriterCallback> callback)
105 : m_callback(callback)
108 RefPtr<FileWriterCallback> m_callback;
113 void DOMFileSystem::createWriter(const FileEntry* fileEntry, PassRefPtr<FileWriterCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
117 RefPtr<FileWriter> fileWriter = FileWriter::create(scriptExecutionContext());
118 RefPtr<FileWriterBaseCallback> conversionCallback = ConvertToFileWriterCallback::create(successCallback);
119 OwnPtr<FileWriterBaseCallbacks> callbacks = FileWriterBaseCallbacks::create(fileWriter, conversionCallback, errorCallback);
120 m_asyncFileSystem->createWriter(fileWriter.get(), fileEntry->fullPath(), callbacks.release());
125 class GetPathCallback : public FileSystemCallbacksBase {
127 static PassOwnPtr<GetPathCallback> create(PassRefPtr<DOMFileSystem> filesystem, const String& name, PassRefPtr<FileCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
129 return adoptPtr(new GetPathCallback(filesystem, name, successCallback, errorCallback));
132 virtual void didReadMetadata(const FileMetadata& metadata)
134 ASSERT(!metadata.platformPath.isEmpty());
135 m_filesystem->scheduleCallback(m_successCallback.release(), File::createWithName(metadata.platformPath, m_name));
139 GetPathCallback(PassRefPtr<DOMFileSystem> filesystem, const String& name, PassRefPtr<FileCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
140 : FileSystemCallbacksBase(errorCallback)
141 , m_filesystem(filesystem)
143 , m_successCallback(successCallback)
147 RefPtr<DOMFileSystem> m_filesystem;
149 RefPtr<FileCallback> m_successCallback;
154 void DOMFileSystem::createFile(const FileEntry* fileEntry, PassRefPtr<FileCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback)
156 m_asyncFileSystem->readMetadata(fileEntry->fullPath(), GetPathCallback::create(this, fileEntry->name(), successCallback, errorCallback));
159 } // namespace WebCore
161 #endif // ENABLE(FILE_SYSTEM)