diff options
author | Kristian Monsen <kristianm@google.com> | 2010-09-08 12:18:00 +0100 |
---|---|---|
committer | Kristian Monsen <kristianm@google.com> | 2010-09-11 12:08:58 +0100 |
commit | 5ddde30071f639962dd557c453f2ad01f8f0fd00 (patch) | |
tree | 775803c4ab35af50aa5f5472cd1fb95fe9d5152d /WebCore/fileapi | |
parent | 3e63d9b33b753ca86d0765d1b3d711114ba9e34f (diff) | |
download | external_webkit-5ddde30071f639962dd557c453f2ad01f8f0fd00.zip external_webkit-5ddde30071f639962dd557c453f2ad01f8f0fd00.tar.gz external_webkit-5ddde30071f639962dd557c453f2ad01f8f0fd00.tar.bz2 |
Merge WebKit at r66666 : Initial merge by git.
Change-Id: I57dedeb49859adc9c539e760f0e749768c66626f
Diffstat (limited to 'WebCore/fileapi')
74 files changed, 6199 insertions, 0 deletions
diff --git a/WebCore/fileapi/AsyncFileWriter.h b/WebCore/fileapi/AsyncFileWriter.h new file mode 100644 index 0000000..cdb815c --- /dev/null +++ b/WebCore/fileapi/AsyncFileWriter.h @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef AsyncFileWriter_h +#define AsyncFileWriter_h + +#if ENABLE(FILE_WRITER) + +#include "PlatformString.h" +#include <wtf/RefCounted.h> + +namespace WebCore { + +class Blob; +class FileWriterClient; + +class AsyncFileWriter { +public: + virtual ~AsyncFileWriter() {} + + virtual void setFileWriterClient(FileWriterClient* client) = 0; + + virtual void write(long long position, Blob* data) = 0; + virtual void truncate(long long length) = 0; + virtual void abort() = 0; +}; + +} // namespace + +#endif // ENABLE(FILE_WRITER) + +#endif // AsyncFileWriter_h + diff --git a/WebCore/fileapi/Blob.cpp b/WebCore/fileapi/Blob.cpp new file mode 100644 index 0000000..5fed327 --- /dev/null +++ b/WebCore/fileapi/Blob.cpp @@ -0,0 +1,123 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "Blob.h" + +#include "BlobURL.h" +#include "File.h" +#include "ScriptExecutionContext.h" +#include "ThreadableBlobRegistry.h" + +namespace WebCore { + +Blob::Blob(ScriptExecutionContext* scriptExecutionContext, PassOwnPtr<BlobData> blobData, long long size) + : m_scriptExecutionContext(scriptExecutionContext) + , m_type(blobData->contentType()) + , m_size(size) +{ + ASSERT(blobData); + + m_scriptExecutionContext->addBlob(this); + + // Create a new internal URL and register it with the provided blob data. + m_url = BlobURL::createURL(scriptExecutionContext); + ThreadableBlobRegistry::registerBlobURL(scriptExecutionContext, m_url, blobData); +} + +Blob::Blob(ScriptExecutionContext* scriptExecutionContext, const KURL& srcURL, const String& type, long long size) + : m_scriptExecutionContext(scriptExecutionContext) + , m_type(type) + , m_size(size) +{ + m_scriptExecutionContext->addBlob(this); + + // Create a new internal URL and register it with the same blob data as the source URL. + m_url = BlobURL::createURL(scriptExecutionContext); + ThreadableBlobRegistry::registerBlobURL(scriptExecutionContext, m_url, srcURL); +} + +Blob::~Blob() +{ + // The internal URL is only used to refer to the Blob object. So we need to unregister the URL when the object is GC-ed. + if (m_scriptExecutionContext) { + m_scriptExecutionContext->removeBlob(this); + ThreadableBlobRegistry::unregisterBlobURL(m_scriptExecutionContext, m_url); + } +} + +void Blob::contextDestroyed() +{ + ASSERT(m_scriptExecutionContext); + + // Unregister the internal URL before the context is gone. + ThreadableBlobRegistry::unregisterBlobURL(m_scriptExecutionContext, m_url); + m_scriptExecutionContext = 0; +} + +#if ENABLE(BLOB) +PassRefPtr<Blob> Blob::slice(ScriptExecutionContext* scriptExecutionContext, long long start, long long length, const String& contentType) const +{ + // When we slice a file for the first time, we obtain a snapshot of the file by capturing its current size and modification time. + // The modification time will be used to verify if the file has been changed or not, when the underlying data are accessed. + long long size; + double modificationTime; + if (isFile()) + // FIXME: This involves synchronous file operation. We need to figure out how to make it asynchronous. + static_cast<const File*>(this)->captureSnapshot(size, modificationTime); + else { + ASSERT(m_size != -1); + size = m_size; + } + + // Clamp the range if it exceeds the size limit. + if (start < 0) + start = 0; + if (length < 0) + length = 0; + + if (start >= size) { + start = 0; + length = 0; + } else if (start + length > size) + length = size - start; + + OwnPtr<BlobData> blobData = BlobData::create(); + blobData->setContentType(contentType); + if (isFile()) + blobData->appendFile(static_cast<const File*>(this)->path(), start, length, modificationTime); + else + blobData->appendBlob(m_url, start, length); + + return Blob::create(scriptExecutionContext, blobData.release(), length); +} +#endif + +} // namespace WebCore diff --git a/WebCore/fileapi/Blob.h b/WebCore/fileapi/Blob.h new file mode 100644 index 0000000..0d5649c --- /dev/null +++ b/WebCore/fileapi/Blob.h @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef Blob_h +#define Blob_h + +#include "BlobData.h" +#include "KURL.h" +#include "PlatformString.h" +#include <wtf/PassOwnPtr.h> +#include <wtf/PassRefPtr.h> +#include <wtf/RefCounted.h> +#include <wtf/Vector.h> + +namespace WebCore { + +class ScriptExecutionContext; + +class Blob : public RefCounted<Blob> { +public: + static PassRefPtr<Blob> create(ScriptExecutionContext* scriptExecutionContext, PassOwnPtr<BlobData> blobData, long long size) + { + return adoptRef(new Blob(scriptExecutionContext, blobData, size)); + } + + // For deserialization. + static PassRefPtr<Blob> create(ScriptExecutionContext* scriptExecutionContext, const KURL& srcURL, const String& type, long long size) + { + return adoptRef(new Blob(scriptExecutionContext, srcURL, type, size)); + } + + virtual ~Blob(); + + void contextDestroyed(); + + const KURL& url() const { return m_url; } + const String& type() const { return m_type; } + + virtual unsigned long long size() const { return static_cast<unsigned long long>(m_size); } + virtual bool isFile() const { return false; } + +#if ENABLE(BLOB) + PassRefPtr<Blob> slice(ScriptExecutionContext*, long long start, long long length, const String& contentType = String()) const; +#endif + +protected: + Blob(ScriptExecutionContext*, PassOwnPtr<BlobData>, long long size); + + // For deserialization. + Blob(ScriptExecutionContext*, const KURL& srcURL, const String& type, long long size); + + // This is an internal URL referring to the blob data associated with this object. + // It is only used by FileReader to read the blob data via loading from the blob URL resource. + KURL m_url; + + ScriptExecutionContext* m_scriptExecutionContext; + String m_type; + long long m_size; +}; + +} // namespace WebCore + +#endif // Blob_h diff --git a/WebCore/fileapi/Blob.idl b/WebCore/fileapi/Blob.idl new file mode 100644 index 0000000..b220233 --- /dev/null +++ b/WebCore/fileapi/Blob.idl @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +module html { + + interface [ + NoStaticTables + ] Blob { + readonly attribute unsigned long long size; + readonly attribute DOMString type; + +#if !defined(LANGUAGE_OBJECTIVE_C) +#if defined(ENABLE_BLOB) && ENABLE_BLOB + [CallWith=ScriptExecutionContext] Blob slice(in long long start, in long long length, in [Optional, ConvertUndefinedOrNullToNullString] DOMString contentType); +#endif +#endif + }; + +} diff --git a/WebCore/fileapi/BlobBuilder.cpp b/WebCore/fileapi/BlobBuilder.cpp new file mode 100644 index 0000000..e10df6b --- /dev/null +++ b/WebCore/fileapi/BlobBuilder.cpp @@ -0,0 +1,135 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" + +#include "BlobBuilder.h" + +#include "Blob.h" +#include "ExceptionCode.h" +#include "File.h" +#include "LineEnding.h" +#include "TextEncoding.h" +#include <wtf/text/AtomicString.h> + +namespace WebCore { + +static CString convertToCString(const String& text, const String& endingType, ExceptionCode& ec) +{ + DEFINE_STATIC_LOCAL(AtomicString, transparent, ("transparent")); + DEFINE_STATIC_LOCAL(AtomicString, native, ("native")); + + ec = 0; + + if (endingType.isEmpty() || endingType == transparent) + return UTF8Encoding().encode(text.characters(), text.length(), EntitiesForUnencodables); + if (endingType == native) + return normalizeLineEndingsToNative(UTF8Encoding().encode(text.characters(), text.length(), EntitiesForUnencodables)); + + ec = SYNTAX_ERR; + return CString(); +} + +static CString concatenateTwoCStrings(const CString& a, const CString& b) +{ + if (a.isNull() && b.isNull()) + return CString(); + + char* q; + CString result = CString::newUninitialized(a.length() + b.length(), q); + if (a.length()) + memcpy(q, a.data(), a.length()); + if (b.length()) + memcpy(q + a.length(), b.data(), b.length()); + return result; +} + +BlobBuilder::BlobBuilder() + : m_size(0) +{ +} + +bool BlobBuilder::append(const String& text, const String& endingType, ExceptionCode& ec) +{ + CString cstr = convertToCString(text, endingType, ec); + if (ec) + return false; + + m_size += cstr.length(); + + // If the last item is a string, concatenate it with current string. + if (!m_items.isEmpty() && m_items[m_items.size() - 1].type == BlobDataItem::Data) + m_items[m_items.size() - 1].data = concatenateTwoCStrings(m_items[m_items.size() - 1].data, cstr); + else + m_items.append(BlobDataItem(cstr)); + return true; +} + +bool BlobBuilder::append(const String& text, ExceptionCode& ec) +{ + return append(text, String(), ec); +} + +bool BlobBuilder::append(PassRefPtr<Blob> blob) +{ + if (blob->isFile()) { + // If the blob is file that is not snapshoted, capture the snapshot now. + // FIXME: This involves synchronous file operation. We need to figure out how to make it asynchronous. + File* file = static_cast<File*>(blob.get()); + long long snapshotSize; + double snapshotModificationTime; + file->captureSnapshot(snapshotSize, snapshotModificationTime); + + m_size += snapshotSize; + m_items.append(BlobDataItem(file->path(), 0, snapshotSize, snapshotModificationTime)); + } else { + long long blobSize = static_cast<long long>(blob->size()); + m_size += blobSize; + m_items.append(BlobDataItem(blob->url(), 0, blobSize)); + } + return true; +} + +PassRefPtr<Blob> BlobBuilder::getBlob(ScriptExecutionContext* scriptExecutionContext, const String& contentType) +{ + OwnPtr<BlobData> blobData = BlobData::create(); + blobData->setContentType(contentType); + blobData->swapItems(m_items); + + RefPtr<Blob> blob = Blob::create(scriptExecutionContext, blobData.release(), m_size); + + // After creating a blob from the current blob data, we do not need to keep the data around any more. Instead, we only + // need to keep a reference to the URL of the blob just created. + m_items.append(BlobDataItem(blob->url(), 0, m_size)); + + return blob; +} + +} // namespace WebCore diff --git a/WebCore/fileapi/BlobBuilder.h b/WebCore/fileapi/BlobBuilder.h new file mode 100644 index 0000000..20e510a --- /dev/null +++ b/WebCore/fileapi/BlobBuilder.h @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef BlobBuilder_h +#define BlobBuilder_h + +#include "BlobData.h" +#include "PlatformString.h" +#include <wtf/PassRefPtr.h> +#include <wtf/RefCounted.h> +#include <wtf/Vector.h> +#include <wtf/text/CString.h> + +namespace WebCore { + +class Blob; +class ScriptExecutionContext; +class TextEncoding; + +typedef int ExceptionCode; + +class BlobBuilder : public RefCounted<BlobBuilder> { +public: + static PassRefPtr<BlobBuilder> create() { return adoptRef(new BlobBuilder()); } + + bool append(PassRefPtr<Blob>); + bool append(const String& text, ExceptionCode&); + bool append(const String& text, const String& ending, ExceptionCode&); + + PassRefPtr<Blob> getBlob(ScriptExecutionContext*, const String& contentType = String()); + +private: + BlobBuilder(); + + long long m_size; + BlobDataItemList m_items; +}; + +} // namespace WebCore + +#endif // BlobBuilder_h diff --git a/WebCore/fileapi/BlobBuilder.idl b/WebCore/fileapi/BlobBuilder.idl new file mode 100644 index 0000000..53c7add --- /dev/null +++ b/WebCore/fileapi/BlobBuilder.idl @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +module html { + interface [ + CanBeConstructed, + GenerateNativeConverter, + NoStaticTables + ] BlobBuilder { +#if !defined(LANGUAGE_OBJECTIVE_C) + [CallWith=ScriptExecutionContext] Blob getBlob(in [Optional, ConvertUndefinedOrNullToNullString] DOMString contentType); +#endif + void append(in Blob blob); + void append(in DOMString value, in [Optional, ConvertUndefinedOrNullToNullString] DOMString endings) raises (DOMException); + }; + +} + diff --git a/WebCore/fileapi/BlobURL.cpp b/WebCore/fileapi/BlobURL.cpp new file mode 100644 index 0000000..c5571a7 --- /dev/null +++ b/WebCore/fileapi/BlobURL.cpp @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" + +#include "BlobURL.h" + +#include "KURL.h" +#include "PlatformString.h" +#include "ScriptExecutionContext.h" +#include "SecurityOrigin.h" +#include "UUID.h" + +namespace WebCore { + +KURL BlobURL::createURL(ScriptExecutionContext* scriptExecutionContext) +{ + // Create the blob URL in the following format: + // blob:%escaped_origin%/%UUID% + // The origin of the host page is encoded in the URL value to allow easy lookup of the origin when the security check needs + // to be performed. + String urlString = "blob:"; + urlString += encodeWithURLEscapeSequences(scriptExecutionContext->securityOrigin()->toString()); + urlString += "/"; + urlString += createCanonicalUUIDString(); + return KURL(ParsedURLString, urlString); +} + +KURL BlobURL::getOrigin(const KURL& url) +{ + ASSERT(url.protocolIs("blob")); + + unsigned startIndex = url.pathStart(); + unsigned afterEndIndex = url.pathAfterLastSlash(); + String origin = url.string().substring(startIndex, afterEndIndex - startIndex); + return KURL(ParsedURLString, decodeURLEscapeSequences(origin)); +} + +String BlobURL::getIdentifier(const KURL& url) +{ + ASSERT(url.protocolIs("blob")); + + unsigned startIndex = url.pathAfterLastSlash(); + return url.string().substring(startIndex); +} + +} // namespace WebCore diff --git a/WebCore/fileapi/BlobURL.h b/WebCore/fileapi/BlobURL.h new file mode 100644 index 0000000..e73b771 --- /dev/null +++ b/WebCore/fileapi/BlobURL.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef BlobURL_h +#define BlobURL_h + +#include "KURL.h" + +namespace WebCore { + +class ScriptExecutionContext; + +class BlobURL { +public: + static KURL createURL(ScriptExecutionContext*); + static KURL getOrigin(const KURL&); + static String getIdentifier(const KURL& url); +}; + +} + +#endif // BlobURL_h diff --git a/WebCore/fileapi/DOMFilePath.cpp b/WebCore/fileapi/DOMFilePath.cpp new file mode 100644 index 0000000..2da057c --- /dev/null +++ b/WebCore/fileapi/DOMFilePath.cpp @@ -0,0 +1,166 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "DOMFilePath.h" + +#if ENABLE(FILE_SYSTEM) + +#include "RegularExpression.h" +#include <wtf/Vector.h> +#include <wtf/text/CString.h> + +namespace WebCore { + +const char DOMFilePath::separator = '/'; +const char DOMFilePath::root[] = "/"; + +String DOMFilePath::append(const String& base, const String& components) +{ + return ensureDirectoryPath(base) + components; +} + +String DOMFilePath::ensureDirectoryPath(const String& path) +{ + if (!DOMFilePath::endsWithSeparator(path)) { + String newPath = path; + newPath.append(DOMFilePath::separator); + return newPath; + } + return path; +} + +String DOMFilePath::getName(const String& path) +{ + int index = path.reverseFind(DOMFilePath::separator); + if (index != -1) + return path.substring(index); + return path; +} + +String DOMFilePath::getDirectory(const String& path) +{ + int index = path.reverseFind(DOMFilePath::separator); + if (index == 0) + return DOMFilePath::root; + if (index != -1) + return path.substring(0, index); + return "."; +} + +bool DOMFilePath::isParentOf(const String& parent, const String& mayBeChild) +{ + ASSERT(DOMFilePath::isAbsolute(parent)); + ASSERT(DOMFilePath::isAbsolute(mayBeChild)); + if (parent == DOMFilePath::root && mayBeChild != DOMFilePath::root) + return true; + if (parent.length() >= mayBeChild.length() || !mayBeChild.startsWith(parent, false)) + return false; + if (mayBeChild[parent.length()] != DOMFilePath::separator) + return false; + return true; +} + +String DOMFilePath::removeExtraParentReferences(const String& path) +{ + ASSERT(DOMFilePath::isAbsolute(path)); + Vector<String> components; + Vector<String> canonicalized; + path.split(DOMFilePath::separator, components); + for (size_t i = 0; i < components.size(); ++i) { + if (components[i] == ".") + continue; + if (components[i] == "..") { + if (canonicalized.size() > 0) + canonicalized.removeLast(); + continue; + } + canonicalized.append(components[i]); + } + if (canonicalized.isEmpty()) + return DOMFilePath::root; + String result; + for (size_t i = 0; i < canonicalized.size(); ++i) { + result.append(DOMFilePath::separator); + result.append(canonicalized[i]); + } + return result; +} + +// Check the naming restrictions defined in FileSystem API 8.3. +// http://dev.w3.org/2009/dap/file-system/file-dir-sys.html#naming-restrictions +bool DOMFilePath::isValidPath(const String& path) +{ + if (path.isEmpty() || path == DOMFilePath::root) + return true; + + // Chars 0-31 in UTF-8 prepresentation are not allowed. + for (size_t i = 0; i < path.length(); ++i) + if (path[i] < 32) + return false; + + // Unallowed names. + DEFINE_STATIC_LOCAL(RegularExpression, unallowedNamesRegExp1, ("(/|^)(CON|PRN|AUX|NUL)([\\./]|$)", TextCaseInsensitive)); + DEFINE_STATIC_LOCAL(RegularExpression, unallowedNamesRegExp2, ("(/|^)(COM|LPT)[1-9]([\\./]|$)", TextCaseInsensitive)); + + if (unallowedNamesRegExp1.match(path) >= 0) + return false; + if (unallowedNamesRegExp2.match(path) >= 0) + return false; + + // Names must not end with period or whitespace. + DEFINE_STATIC_LOCAL(RegularExpression, endingRegExp, ("[\\.\\s](/|$)", TextCaseInsensitive)); + + if (endingRegExp.match(path) >= 0) + return false; + + // Unallowed chars: '\', '<', '>', ':', '?', '*', '"', '|' + // (We don't check '/' here as this method takes paths as its argument.) + DEFINE_STATIC_LOCAL(RegularExpression, unallowedCharsRegExp, ("[\\\\<>:\\?\\*\"|]", TextCaseInsensitive)); + + if (unallowedCharsRegExp.match(path) >= 0) + return false; + + return true; +} + +bool DOMFilePath::isValidName(const String& name) +{ + if (name.isEmpty()) + return true; + // '/' is not allowed in name. + if (name.contains('/')) + return false; + return isValidPath(name); +} + +} // namespace WebCore + +#endif // ENABLE(FILE_SYSTEM) diff --git a/WebCore/fileapi/DOMFilePath.h b/WebCore/fileapi/DOMFilePath.h new file mode 100644 index 0000000..2f2bb23 --- /dev/null +++ b/WebCore/fileapi/DOMFilePath.h @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef DOMFilePath_h +#define DOMFilePath_h + +#include "PlatformString.h" +#include <wtf/PassRefPtr.h> +#include <wtf/RefCounted.h> + +namespace WebCore { + +// DOMFileSystem path utilities. All methods in this class are static. +class DOMFilePath { +public: + static const char separator; + static const char root[]; + + // Returns the name part from the given path. + static String getName(const String& path); + + // Returns the parent directory path of the given path. + static String getDirectory(const String& path); + + // Checks if a given path is a parent of mayBeChild. This method assumes given paths are absolute and do not have extra references to a parent (i.e. "../"). + static bool isParentOf(const String& path, const String& mayBeChild); + + // Appends the separator at the end of the path if it's not there already. + static String ensureDirectoryPath(const String& path); + + // Returns a new path by appending a separator and the supplied path component to the path. + static String append(const String& path, const String& component); + + static bool isAbsolute(const String& path) + { + return path.startsWith(DOMFilePath::root); + } + + static bool endsWithSeparator(const String& path) + { + return path[path.length() - 1] == DOMFilePath::separator; + } + + // Evaluates all "../" and "./" segments. Note that "/../" expands to "/", so you can't ever refer to anything above the root directory. + static String removeExtraParentReferences(const String& path); + + // Checks if the given path follows the FileSystem API naming restrictions. + static bool isValidPath(const String& path); + + // Checks if the given name follows the FileSystem API naming restrictions. + static bool isValidName(const String& name); + +private: + DOMFilePath() { } +}; + +} // namespace WebCore + +#endif // DOMFilePath_h diff --git a/WebCore/fileapi/DOMFileSystem.cpp b/WebCore/fileapi/DOMFileSystem.cpp new file mode 100644 index 0000000..f1b9342 --- /dev/null +++ b/WebCore/fileapi/DOMFileSystem.cpp @@ -0,0 +1,210 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "DOMFileSystem.h" + +#if ENABLE(FILE_SYSTEM) + +#include "AsyncFileSystem.h" +#include "DOMFilePath.h" +#include "DirectoryEntry.h" +#include "EntriesCallback.h" +#include "Entry.h" +#include "EntryCallback.h" +#include "ErrorCallback.h" +#include "FileError.h" +#include "FileSystemCallbacks.h" +#include "MetadataCallback.h" +#include "ScriptExecutionContext.h" +#include "VoidCallback.h" +#include <wtf/OwnPtr.h> + +namespace WebCore { + +DOMFileSystem::DOMFileSystem(ScriptExecutionContext* context, const String& name, PassOwnPtr<AsyncFileSystem> asyncFileSystem) + : ActiveDOMObject(context, this) + , m_name(name) + , m_asyncFileSystem(asyncFileSystem) +{ +} + +DOMFileSystem::~DOMFileSystem() +{ +} + +PassRefPtr<DirectoryEntry> DOMFileSystem::root() +{ + return DirectoryEntry::create(this, DOMFilePath::root); +} + +void DOMFileSystem::stop() +{ + m_asyncFileSystem->stop(); +} + +bool DOMFileSystem::hasPendingActivity() const +{ + return m_asyncFileSystem->hasPendingActivity(); +} + +void DOMFileSystem::contextDestroyed() +{ + m_asyncFileSystem->stop(); +} + +void DOMFileSystem::getMetadata(const Entry* entry, PassRefPtr<MetadataCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback) +{ + String platformPath = m_asyncFileSystem->virtualToPlatformPath(entry->fullPath()); + m_asyncFileSystem->readMetadata(platformPath, new MetadataCallbacks(successCallback, errorCallback)); +} + +static bool checkValidityForForCopyOrMove(const Entry* src, Entry* parent, const String& newName) +{ + ASSERT(src); + + if (!parent || !parent->isDirectory()) + return false; + + if (!newName.isEmpty() && !DOMFilePath::isValidName(newName)) + return false; + + // It is an error to try to copy or move an entry inside itself at any depth if it is a directory. + if (src->isDirectory() && DOMFilePath::isParentOf(src->fullPath(), parent->fullPath())) + return false; + + // It is an error to copy or move an entry into its parent if a name different from its current one isn't provided. + if ((newName.isEmpty() || src->name() == newName) && DOMFilePath::getDirectory(src->fullPath()) == parent->fullPath()) + return false; + + return true; +} + +void DOMFileSystem::move(const Entry* src, PassRefPtr<Entry> parent, const String& newName, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback) +{ + if (!checkValidityForForCopyOrMove(src, parent.get(), newName)) { + scheduleCallback(scriptExecutionContext(), errorCallback, FileError::create(INVALID_MODIFICATION_ERR)); + return; + } + + String destPath = parent->fullPath(); + if (!newName.isEmpty()) + destPath = DOMFilePath::append(destPath, newName); + else + destPath = DOMFilePath::append(destPath, src->name()); + + String srcPlatformPath = m_asyncFileSystem->virtualToPlatformPath(src->fullPath()); + String destPlatformPath = parent->filesystem()->asyncFileSystem()->virtualToPlatformPath(destPath); + m_asyncFileSystem->move(srcPlatformPath, destPlatformPath, new EntryCallbacks(successCallback, errorCallback, this, destPath, src->isDirectory())); +} + +void DOMFileSystem::copy(const Entry* src, PassRefPtr<Entry> parent, const String& newName, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback) +{ + if (!checkValidityForForCopyOrMove(src, parent.get(), newName)) { + scheduleCallback(scriptExecutionContext(), errorCallback, FileError::create(INVALID_MODIFICATION_ERR)); + return; + } + + String destPath = parent->fullPath(); + if (!newName.isEmpty()) + destPath = DOMFilePath::append(destPath, newName); + else + destPath = DOMFilePath::append(destPath, src->name()); + + String srcPlatformPath = m_asyncFileSystem->virtualToPlatformPath(src->fullPath()); + String destPlatformPath = parent->filesystem()->asyncFileSystem()->virtualToPlatformPath(destPath); + m_asyncFileSystem->copy(srcPlatformPath, destPlatformPath, new EntryCallbacks(successCallback, errorCallback, this, destPath, src->isDirectory())); +} + +void DOMFileSystem::remove(const Entry* entry, PassRefPtr<VoidCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback) +{ + ASSERT(entry); + String platformPath = m_asyncFileSystem->virtualToPlatformPath(entry->fullPath()); + m_asyncFileSystem->remove(platformPath, new VoidCallbacks(successCallback, errorCallback)); +} + +void DOMFileSystem::getParent(const Entry* entry, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback) +{ + ASSERT(entry); + String path = DOMFilePath::getDirectory(entry->fullPath()); + String platformPath = m_asyncFileSystem->virtualToPlatformPath(path); + m_asyncFileSystem->directoryExists(platformPath, new EntryCallbacks(successCallback, errorCallback, this, path, true)); +} + +void DOMFileSystem::getFile(const Entry* base, const String& path, PassRefPtr<Flags> flags, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback) +{ + ASSERT(base); + if (!DOMFilePath::isValidPath(path)) { + scheduleCallback(scriptExecutionContext(), errorCallback, FileError::create(INVALID_MODIFICATION_ERR)); + return; + } + + String absolutePath = path; + if (!DOMFilePath::isAbsolute(path)) + absolutePath = DOMFilePath::removeExtraParentReferences(DOMFilePath::append(base->fullPath(), path)); + String platformPath = m_asyncFileSystem->virtualToPlatformPath(absolutePath); + + OwnPtr<EntryCallbacks> callbacks = adoptPtr(new EntryCallbacks(successCallback, errorCallback, this, absolutePath, false)); + if (flags && flags->isCreate()) + m_asyncFileSystem->createFile(platformPath, flags->isExclusive(), callbacks.release()); + else + m_asyncFileSystem->fileExists(platformPath, callbacks.release()); +} + +void DOMFileSystem::getDirectory(const Entry* base, const String& path, PassRefPtr<Flags> flags, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback) +{ + ASSERT(base); + if (!DOMFilePath::isValidPath(path)) { + scheduleCallback(scriptExecutionContext(), errorCallback, FileError::create(INVALID_MODIFICATION_ERR)); + return; + } + + String absolutePath = path; + if (!DOMFilePath::isAbsolute(path)) + absolutePath = DOMFilePath::removeExtraParentReferences(DOMFilePath::append(base->fullPath(), path)); + String platformPath = m_asyncFileSystem->virtualToPlatformPath(absolutePath); + + OwnPtr<EntryCallbacks> callbacks = adoptPtr(new EntryCallbacks(successCallback, errorCallback, this, absolutePath, true)); + if (flags && flags->isCreate()) + m_asyncFileSystem->createDirectory(platformPath, flags->isExclusive(), callbacks.release()); + else + m_asyncFileSystem->directoryExists(platformPath, callbacks.release()); +} + +void DOMFileSystem::readDirectory(const String& path, PassRefPtr<EntriesCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback) +{ + ASSERT(DOMFilePath::isAbsolute(path)); + String platformPath = m_asyncFileSystem->virtualToPlatformPath(path); + m_asyncFileSystem->readDirectory(platformPath, new EntriesCallbacks(successCallback, errorCallback, this, path)); +} + +} // namespace + +#endif // ENABLE(FILE_SYSTEM) diff --git a/WebCore/fileapi/DOMFileSystem.h b/WebCore/fileapi/DOMFileSystem.h new file mode 100644 index 0000000..f779aba --- /dev/null +++ b/WebCore/fileapi/DOMFileSystem.h @@ -0,0 +1,126 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef DOMFileSystem_h +#define DOMFileSystem_h + +#if ENABLE(FILE_SYSTEM) + +#include "ActiveDOMObject.h" +#include "AsyncFileSystem.h" +#include "Flags.h" +#include "PlatformString.h" +#include "ScriptExecutionContext.h" +#include <wtf/PassRefPtr.h> +#include <wtf/RefCounted.h> + +namespace WebCore { + +class DirectoryEntry; +class Entry; +class EntryCallback; +class EntriesCallback; +class ErrorCallback; +class MetadataCallback; +class VoidCallback; + +class DOMFileSystem : public RefCounted<DOMFileSystem>, public ActiveDOMObject { +public: + static PassRefPtr<DOMFileSystem> create(ScriptExecutionContext* context, const String& name, PassOwnPtr<AsyncFileSystem> asyncFileSystem) + { + return adoptRef(new DOMFileSystem(context, name, asyncFileSystem)); + } + + virtual ~DOMFileSystem(); + + const String& name() const { return m_name; } + PassRefPtr<DirectoryEntry> root(); + + // ActiveDOMObject methods. + virtual void stop(); + virtual bool hasPendingActivity() const; + virtual void contextDestroyed(); + + // Actual FileSystem API implementations. All the validity checks on virtual paths are done at DOMFileSystem level. + void getMetadata(const Entry* entry, PassRefPtr<MetadataCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback); + void move(const Entry* src, PassRefPtr<Entry> parent, const String& name, PassRefPtr<EntryCallback>, PassRefPtr<ErrorCallback>); + void copy(const Entry* src, PassRefPtr<Entry> parent, const String& name, PassRefPtr<EntryCallback>, PassRefPtr<ErrorCallback>); + void remove(const Entry* entry, PassRefPtr<VoidCallback>, PassRefPtr<ErrorCallback>); + void getParent(const Entry* entry, PassRefPtr<EntryCallback>, PassRefPtr<ErrorCallback>); + void getFile(const Entry* base, const String& path, PassRefPtr<Flags>, PassRefPtr<EntryCallback>, PassRefPtr<ErrorCallback>); + void getDirectory(const Entry* base, const String& path, PassRefPtr<Flags>, PassRefPtr<EntryCallback>, PassRefPtr<ErrorCallback>); + void readDirectory(const String& path, PassRefPtr<EntriesCallback>, PassRefPtr<ErrorCallback>); + + // Schedule a callback. This should not cross threads (should be called on the same context thread). + template <typename CB, typename CBArg> + static void scheduleCallback(ScriptExecutionContext*, PassRefPtr<CB>, PassRefPtr<CBArg>); + +private: + DOMFileSystem(ScriptExecutionContext*, const String& name, PassOwnPtr<AsyncFileSystem>); + + AsyncFileSystem* asyncFileSystem() const { return m_asyncFileSystem.get(); } + + // A helper template to schedule a callback task. + // FIXME: move this to a more generic place. + template <typename CB, typename CBArg> + class DispatchCallbackTask : public ScriptExecutionContext::Task { + public: + DispatchCallbackTask(PassRefPtr<CB> callback, PassRefPtr<CBArg> arg) + : m_callback(callback) + , m_callbackArg(arg) + { + } + + virtual void performTask(ScriptExecutionContext*) + { + m_callback->handleEvent(m_callbackArg.get()); + } + + private: + RefPtr<CB> m_callback; + RefPtr<CBArg> m_callbackArg; + }; + + String m_name; + mutable OwnPtr<AsyncFileSystem> m_asyncFileSystem; +}; + +template <typename CB, typename CBArg> +void DOMFileSystem::scheduleCallback(ScriptExecutionContext* scriptExecutionContext, PassRefPtr<CB> callback, PassRefPtr<CBArg> arg) +{ + ASSERT(scriptExecutionContext->isContextThread()); + scriptExecutionContext->postTask(new DispatchCallbackTask<CB, CBArg>(callback, arg)); +} + +} // namespace + +#endif // ENABLE(FILE_SYSTEM) + +#endif // DOMFileSystem_h diff --git a/WebCore/fileapi/DOMFileSystem.idl b/WebCore/fileapi/DOMFileSystem.idl new file mode 100644 index 0000000..b7307e2 --- /dev/null +++ b/WebCore/fileapi/DOMFileSystem.idl @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +module storage { + interface [ + Conditional=FILE_SYSTEM + ] DOMFileSystem { + readonly attribute DOMString name; + readonly attribute DirectoryEntry root; + }; +} diff --git a/WebCore/fileapi/DirectoryEntry.cpp b/WebCore/fileapi/DirectoryEntry.cpp new file mode 100644 index 0000000..95c12e4 --- /dev/null +++ b/WebCore/fileapi/DirectoryEntry.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "DirectoryEntry.h" + +#if ENABLE(FILE_SYSTEM) + +#include "DirectoryReader.h" +#include "EntryCallback.h" +#include "ErrorCallback.h" + +namespace WebCore { + +DirectoryEntry::DirectoryEntry(DOMFileSystem* fileSystem, const String& fullPath) + : Entry(fileSystem, fullPath) +{ +} + +PassRefPtr<DirectoryReader> DirectoryEntry::createReader() +{ + return DirectoryReader::create(m_fileSystem, m_fullPath); +} + +void DirectoryEntry::getFile(const String& path, PassRefPtr<Flags> flags, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback) +{ + m_fileSystem->getFile(this, path, flags, successCallback, errorCallback); +} + +void DirectoryEntry::getDirectory(const String& path, PassRefPtr<Flags> flags, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback) +{ + m_fileSystem->getDirectory(this, path, flags, successCallback, errorCallback); +} + +} // namespace + +#endif // ENABLE(FILE_SYSTEM) diff --git a/WebCore/fileapi/DirectoryEntry.h b/WebCore/fileapi/DirectoryEntry.h new file mode 100644 index 0000000..3cd8ab5 --- /dev/null +++ b/WebCore/fileapi/DirectoryEntry.h @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef DirectoryEntry_h +#define DirectoryEntry_h + +#if ENABLE(FILE_SYSTEM) + +#include "Entry.h" +#include "Flags.h" +#include "PlatformString.h" +#include <wtf/PassRefPtr.h> +#include <wtf/RefCounted.h> + +namespace WebCore { + +class DirectoryReader; +class EntryCallback; +class ErrorCallback; + +class DirectoryEntry : public Entry { +public: + static PassRefPtr<DirectoryEntry> create(DOMFileSystem* fileSystem, const String& fullPath) + { + return adoptRef(new DirectoryEntry(fileSystem, fullPath)); + } + virtual bool isDirectory() const { return true; } + + PassRefPtr<DirectoryReader> createReader(); + void getFile(const String& path, PassRefPtr<Flags> options = 0, PassRefPtr<EntryCallback> successCallback = 0, PassRefPtr<ErrorCallback> errorCallback = 0); + void getDirectory(const String& path, PassRefPtr<Flags> options = 0, PassRefPtr<EntryCallback> successCallback = 0, PassRefPtr<ErrorCallback> errorCallback = 0); + +private: + DirectoryEntry(DOMFileSystem* fileSystem, const String& fullPath); +}; + +} // namespace + +#endif // ENABLE(FILE_SYSTEM) + +#endif // DirectoryEntry_h diff --git a/WebCore/fileapi/DirectoryEntry.idl b/WebCore/fileapi/DirectoryEntry.idl new file mode 100644 index 0000000..ac30c7f --- /dev/null +++ b/WebCore/fileapi/DirectoryEntry.idl @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +module storage { + interface [ + Conditional=FILE_SYSTEM, + GenerateNativeConverter, + GenerateToJS + ] DirectoryEntry : Entry { + DirectoryReader createReader(); + void getFile(in DOMString path, in [Optional] Flags options, in [Optional, Callback] EntryCallback successCallback, in [Optional, Callback] ErrorCallback errorCallback); + void getDirectory(in DOMString path, in [Optional] Flags options, in [Optional, Callback] EntryCallback successCallback, in [Optional, Callback] ErrorCallback errorCallback); + }; +} diff --git a/WebCore/fileapi/DirectoryReader.cpp b/WebCore/fileapi/DirectoryReader.cpp new file mode 100644 index 0000000..b0eef1c --- /dev/null +++ b/WebCore/fileapi/DirectoryReader.cpp @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "DirectoryReader.h" + +#if ENABLE(FILE_SYSTEM) + +#include "DOMFileSystem.h" +#include "EntriesCallback.h" +#include "ErrorCallback.h" + +namespace WebCore { + +DirectoryReader::DirectoryReader(PassRefPtr<DOMFileSystem> fileSystem, const String& fullPath) + : m_fileSystem(fileSystem) + , m_fullPath(fullPath) +{ +} + +void DirectoryReader::readEntries(PassRefPtr<EntriesCallback> entriesCallback, PassRefPtr<ErrorCallback> errorCallback) +{ + m_fileSystem->readDirectory(m_fullPath, entriesCallback, errorCallback); +} + +} // namespace + +#endif // ENABLE(FILE_SYSTEM) diff --git a/WebCore/fileapi/DirectoryReader.h b/WebCore/fileapi/DirectoryReader.h new file mode 100644 index 0000000..0e2ef0f --- /dev/null +++ b/WebCore/fileapi/DirectoryReader.h @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef DirectoryReader_h +#define DirectoryReader_h + +#if ENABLE(FILE_SYSTEM) + +#include "DOMFileSystem.h" +#include "PlatformString.h" +#include <wtf/PassRefPtr.h> +#include <wtf/RefCounted.h> + +namespace WebCore { + +class EntriesCallback; +class ErrorCallback; + +class DirectoryReader : public RefCounted<DirectoryReader> { +public: + static PassRefPtr<DirectoryReader> create(PassRefPtr<DOMFileSystem> fileSystem, const String& path) + { + return adoptRef(new DirectoryReader(fileSystem, path)); + } + + void readEntries(PassRefPtr<EntriesCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback = 0); + +private: + DirectoryReader(PassRefPtr<DOMFileSystem> fileSystem, const String& path); + + RefPtr<DOMFileSystem> m_fileSystem; + String m_fullPath; +}; + +} // namespace + +#endif // ENABLE(FILE_SYSTEM) + +#endif // DirectoryReader_h diff --git a/WebCore/fileapi/DirectoryReader.idl b/WebCore/fileapi/DirectoryReader.idl new file mode 100644 index 0000000..c3c7012 --- /dev/null +++ b/WebCore/fileapi/DirectoryReader.idl @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +module storage { + interface [ + Conditional=FILE_SYSTEM + ] DirectoryReader { + void readEntries(in [Callback] EntriesCallback successCallback, in [Optional, Callback] ErrorCallback errorCallback); + }; +} diff --git a/WebCore/fileapi/EntriesCallback.h b/WebCore/fileapi/EntriesCallback.h new file mode 100644 index 0000000..9f812e9 --- /dev/null +++ b/WebCore/fileapi/EntriesCallback.h @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef EntriesCallback_h +#define EntriesCallback_h + +#if ENABLE(FILE_SYSTEM) + +#include <wtf/RefCounted.h> + +namespace WebCore { + +class EntryArray; + +class EntriesCallback : public RefCounted<EntriesCallback> { +public: + virtual ~EntriesCallback() { } + virtual bool handleEvent(EntryArray*) = 0; +}; + +} // namespace + +#endif // ENABLE(FILE_SYSTEM) + +#endif // EntriesCallback_h diff --git a/WebCore/fileapi/EntriesCallback.idl b/WebCore/fileapi/EntriesCallback.idl new file mode 100644 index 0000000..73b374d --- /dev/null +++ b/WebCore/fileapi/EntriesCallback.idl @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +module storage { + interface [ + Conditional=FILE_SYSTEM, + Callback + ] EntriesCallback { + boolean handleEvent(in EntryArray entries); + }; +} diff --git a/WebCore/fileapi/Entry.cpp b/WebCore/fileapi/Entry.cpp new file mode 100644 index 0000000..fbbedf2 --- /dev/null +++ b/WebCore/fileapi/Entry.cpp @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "config.h" +#include "Entry.h" + +#if ENABLE(FILE_SYSTEM) + +#include "DOMFilePath.h" +#include "DOMFileSystem.h" +#include "EntryCallback.h" +#include "ErrorCallback.h" +#include "FileError.h" +#include "MetadataCallback.h" +#include "VoidCallback.h" + +namespace WebCore { + +Entry::Entry(DOMFileSystem* fileSystem, const String& fullPath) + : m_fileSystem(fileSystem) + , m_fullPath(fullPath) +{ + size_t index = fullPath.reverseFind("/"); + if (index != notFound) + m_name = fullPath.substring(index); + else + m_name = fullPath; +} + +void Entry::getMetadata(PassRefPtr<MetadataCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback) +{ + m_fileSystem->getMetadata(this, successCallback, errorCallback); +} + +void Entry::moveTo(PassRefPtr<Entry> parent, const String& name, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback) const +{ + m_fileSystem->move(this, parent, name, successCallback, errorCallback); +} + +void Entry::copyTo(PassRefPtr<Entry> parent, const String& name, PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback) const +{ + m_fileSystem->copy(this, parent, name, successCallback, errorCallback); +} + +void Entry::remove(PassRefPtr<VoidCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback) const +{ + m_fileSystem->remove(this, successCallback, errorCallback); +} + +void Entry::getParent(PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback) const +{ + m_fileSystem->getParent(this, successCallback, errorCallback); +} + +String Entry::toURI(const String&) +{ + // FIXME: to be implemented. + ASSERT_NOT_REACHED(); + return String(); +} + +} // namespace WebCore + +#endif // ENABLE(FILE_SYSTEM) diff --git a/WebCore/fileapi/Entry.h b/WebCore/fileapi/Entry.h new file mode 100644 index 0000000..9af532f --- /dev/null +++ b/WebCore/fileapi/Entry.h @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef Entry_h +#define Entry_h + +#if ENABLE(FILE_SYSTEM) + +#include "DOMFileSystem.h" +#include <wtf/PassRefPtr.h> +#include <wtf/RefCounted.h> + +namespace WebCore { + +class EntryCallback; +class ErrorCallback; +class MetadataCallback; +class VoidCallback; + +class Entry : public RefCounted<Entry> { +public: + virtual ~Entry() { } + + virtual bool isFile() const { return false; } + virtual bool isDirectory() const { return false; } + + const String& fullPath() const { return m_fullPath; } + const String& name() const { return m_name; } + + DOMFileSystem* filesystem() const { return m_fileSystem; } + + virtual void getMetadata(PassRefPtr<MetadataCallback> successCallback = 0, PassRefPtr<ErrorCallback> errorCallback = 0); + virtual void moveTo(PassRefPtr<Entry> parent, const String& name = String(), PassRefPtr<EntryCallback> successCallback = 0, PassRefPtr<ErrorCallback> errorCallback = 0) const; + virtual void copyTo(PassRefPtr<Entry> parent, const String& name = String(), PassRefPtr<EntryCallback> successCallback = 0, PassRefPtr<ErrorCallback> errorCallback = 0) const; + virtual void remove(PassRefPtr<VoidCallback> successCallback = 0, PassRefPtr<ErrorCallback> errorCallback = 0) const; + virtual void getParent(PassRefPtr<EntryCallback> successCallback = 0, PassRefPtr<ErrorCallback> errorCallback = 0) const; + + virtual String toURI(const String& mimeType = String()); + +protected: + Entry(DOMFileSystem* fileSystem, const String& fullPath); + + DOMFileSystem* m_fileSystem; + String m_fullPath; + String m_name; +}; + +} // namespace WebCore + +#endif // ENABLE(FILE_SYSTEM) + +#endif // Entry_h diff --git a/WebCore/fileapi/Entry.idl b/WebCore/fileapi/Entry.idl new file mode 100644 index 0000000..7d4ffee --- /dev/null +++ b/WebCore/fileapi/Entry.idl @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +module storage { + interface [ + Conditional=FILE_SYSTEM + ] Entry { + readonly attribute boolean isFile; + readonly attribute boolean isDirectory; + readonly attribute DOMString name; + readonly attribute DOMString fullPath; + readonly attribute DOMFileSystem filesystem; + + void moveTo(in Entry parent, in [Optional] DOMString name, in [Optional, Callback] EntryCallback successCallback, in [Optional, Callback] ErrorCallback errorCallback); + void copyTo(in Entry parent, in [Optional] DOMString name, in [Optional, Callback] EntryCallback successCallback, in [Optional, Callback] ErrorCallback errorCallback); + void remove(in [Optional, Callback] VoidCallback successCallback, in [Optional, Callback] ErrorCallback errorCallback); + void getParent(in [Optional, Callback] EntryCallback successCallback, in [Optional, Callback] ErrorCallback errorCallback); + DOMString toURI(in [Optional] DOMString mimeType); + }; +} diff --git a/WebCore/fileapi/EntryArray.cpp b/WebCore/fileapi/EntryArray.cpp new file mode 100644 index 0000000..6c4f74f --- /dev/null +++ b/WebCore/fileapi/EntryArray.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "EntryArray.h" + +#if ENABLE(FILE_SYSTEM) + +namespace WebCore { + +EntryArray::EntryArray() +{ +} + +Entry* EntryArray::item(unsigned index) const +{ + if (index >= m_entries.size()) + return 0; + return m_entries[index].get(); +} + +} // namespace + +#endif // ENABLE(FILE_SYSTEM) diff --git a/WebCore/fileapi/EntryArray.h b/WebCore/fileapi/EntryArray.h new file mode 100644 index 0000000..e5957ab --- /dev/null +++ b/WebCore/fileapi/EntryArray.h @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef EntryArray_h +#define EntryArray_h + +#if ENABLE(FILE_SYSTEM) + +#include "Entry.h" +#include <wtf/PassRefPtr.h> +#include <wtf/RefCounted.h> + +namespace WebCore { + +class EntryArray : public RefCounted<EntryArray> { +public: + static PassRefPtr<EntryArray> create() + { + return adoptRef(new EntryArray()); + } + + unsigned length() const { return m_entries.size(); } + Entry* item(unsigned index) const; + void set(unsigned index, PassRefPtr<Entry> entry); + + bool isEmpty() const { return m_entries.isEmpty(); } + void clear() { m_entries.clear(); } + void append(PassRefPtr<Entry> entry) { m_entries.append(entry); } + +private: + EntryArray(); + + Vector<RefPtr<Entry> > m_entries; +}; + +} // namespace + +#endif // ENABLE(FILE_SYSTEM) + +#endif // EntryArray_h diff --git a/WebCore/fileapi/EntryArray.idl b/WebCore/fileapi/EntryArray.idl new file mode 100644 index 0000000..e987ece --- /dev/null +++ b/WebCore/fileapi/EntryArray.idl @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +module storage { + interface [ + Conditional=FILE_SYSTEM, + HasIndexGetter, + ] EntryArray { + readonly attribute unsigned long length; + Entry item(in [IsIndex] unsigned long index); + }; +} diff --git a/WebCore/fileapi/EntryCallback.h b/WebCore/fileapi/EntryCallback.h new file mode 100644 index 0000000..9580eda --- /dev/null +++ b/WebCore/fileapi/EntryCallback.h @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef EntryCallback_h +#define EntryCallback_h + +#if ENABLE(FILE_SYSTEM) + +#include <wtf/RefCounted.h> + +namespace WebCore { + +class Entry; + +class EntryCallback : public RefCounted<EntryCallback> { +public: + virtual ~EntryCallback() { } + virtual bool handleEvent(Entry*) = 0; +}; + +} // namespace + +#endif // ENABLE(FILE_SYSTEM) + +#endif // EntryCallback_h diff --git a/WebCore/fileapi/EntryCallback.idl b/WebCore/fileapi/EntryCallback.idl new file mode 100644 index 0000000..bea3fd1 --- /dev/null +++ b/WebCore/fileapi/EntryCallback.idl @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +module storage { + interface [ + Conditional=FILE_SYSTEM, + Callback + ] EntryCallback { + boolean handleEvent(in Entry entry); + }; +} diff --git a/WebCore/fileapi/ErrorCallback.h b/WebCore/fileapi/ErrorCallback.h new file mode 100644 index 0000000..91143e8 --- /dev/null +++ b/WebCore/fileapi/ErrorCallback.h @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef ErrorCallback_h +#define ErrorCallback_h + +#if ENABLE(FILE_SYSTEM) + +#include <wtf/RefCounted.h> + +namespace WebCore { + +class FileError; +class ScriptExecutionContext; + +class ErrorCallback : public RefCounted<ErrorCallback> { +public: + virtual ~ErrorCallback() { } + virtual bool handleEvent(FileError*) = 0; +}; + +} // namespace + +#endif // ENABLE(FILE_SYSTEM) + +#endif // ErrorCallback_h diff --git a/WebCore/fileapi/ErrorCallback.idl b/WebCore/fileapi/ErrorCallback.idl new file mode 100644 index 0000000..fc7fa85 --- /dev/null +++ b/WebCore/fileapi/ErrorCallback.idl @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +module storage { + interface [ + Conditional=FILE_SYSTEM, + Callback + ] ErrorCallback { + boolean handleEvent(in FileError error); + }; +} diff --git a/WebCore/fileapi/File.cpp b/WebCore/fileapi/File.cpp new file mode 100644 index 0000000..150a05b --- /dev/null +++ b/WebCore/fileapi/File.cpp @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2008 Apple Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "File.h" + +#include "BlobRegistry.h" +#include "FileSystem.h" +#include "MIMETypeRegistry.h" + +namespace WebCore { + +static PassOwnPtr<BlobData> createBlobDataForFile(const String& path) +{ + String type; + int index = path.reverseFind('.'); + if (index != -1) + type = MIMETypeRegistry::getMIMETypeForExtension(path.substring(index + 1)); + + OwnPtr<BlobData> blobData = BlobData::create(); + blobData->setContentType(type); + blobData->appendFile(path); + return blobData.release(); +} + +File::File(ScriptExecutionContext* scriptExecutionContext, const String& path) + : Blob(scriptExecutionContext, createBlobDataForFile(path), -1) + , m_path(path) + , m_name(pathGetFileName(path)) +{ +} + +File::File(ScriptExecutionContext* scriptExecutionContext, const String& path, const KURL& url, const String& type) + : Blob(scriptExecutionContext, url, type, -1) + , m_path(path) +{ + m_name = pathGetFileName(path); +} + +#if ENABLE(DIRECTORY_UPLOAD) +File::File(ScriptExecutionContext* scriptExecutionContext, const String& relativePath, const String& path) + : Blob(scriptExecutionContext, createBlobDataForFile(path), -1) + , m_path(path) + , m_relativePath(relativePath) +{ + m_name = pathGetFileName(path); +} +#endif + +unsigned long long File::size() const +{ + // FIXME: JavaScript cannot represent sizes as large as unsigned long long, we need to + // come up with an exception to throw if file size is not representable. + long long size; + if (!getFileSize(m_path, size)) + return 0; + return static_cast<unsigned long long>(size); +} + +void File::captureSnapshot(long long& snapshotSize, double& snapshotModificationTime) const +{ + // Obtains a snapshot of the file by capturing its current size and modification time. This is used when we slice a file for the first time. + // If we fail to retrieve the size or modification time, probably due to that the file has been deleted, 0 size is returned. + // FIXME: Combine getFileSize and getFileModificationTime into one file system call. + time_t modificationTime; + if (!getFileSize(m_path, snapshotSize) || !getFileModificationTime(m_path, modificationTime)) { + snapshotSize = 0; + snapshotModificationTime = 0; + } else + snapshotModificationTime = modificationTime; +} + +} // namespace WebCore diff --git a/WebCore/fileapi/File.h b/WebCore/fileapi/File.h new file mode 100644 index 0000000..d6446d3 --- /dev/null +++ b/WebCore/fileapi/File.h @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2008 Apple Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef File_h +#define File_h + +#include "Blob.h" +#include "PlatformString.h" +#include <wtf/PassRefPtr.h> +#include <wtf/RefCounted.h> + +namespace WebCore { + +class KURL; +class ScriptExecutionContext; + +class File : public Blob { +public: + static PassRefPtr<File> create(ScriptExecutionContext* scriptExecutionContext, const String& path) + { + return adoptRef(new File(scriptExecutionContext, path)); + } + + // For deserialization. + static PassRefPtr<File> create(ScriptExecutionContext* scriptExecutionContext, const String& path, const KURL& srcURL, const String& type) + { + return adoptRef(new File(scriptExecutionContext, path, srcURL, type)); + } + +#if ENABLE(DIRECTORY_UPLOAD) + static PassRefPtr<File> create(ScriptExecutionContext* scriptExecutionContext, const String& relativePath, const String& path) + { + return adoptRef(new File(scriptExecutionContext, relativePath, path)); + } +#endif + + virtual unsigned long long size() const; + virtual bool isFile() const { return true; } + + const String& path() const { return m_path; } + const String& name() const { return m_name; } +#if ENABLE(DIRECTORY_UPLOAD) + // Returns the relative path of this file in the context of a directory selection. + const String& webkitRelativePath() const { return m_relativePath; } +#endif + + // Note that this involves synchronous file operation. Think twice before calling this function. + void captureSnapshot(long long& snapshotSize, double& snapshotModificationTime) const; + + // FIXME: obsolete attributes. To be removed. + const String& fileName() const { return name(); } + unsigned long long fileSize() const { return size(); } + +private: + File(ScriptExecutionContext*, const String& path); + + // For deserialization. + File(ScriptExecutionContext*, const String& path, const KURL& srcURL, const String& type); + +#if ENABLE(DIRECTORY_UPLOAD) + File(ScriptExecutionContext*, const String& relativePath, const String& path); +#endif + + String m_path; + String m_name; +#if ENABLE(DIRECTORY_UPLOAD) + String m_relativePath; +#endif +}; + +} // namespace WebCore + +#endif // FileList_h diff --git a/WebCore/fileapi/File.idl b/WebCore/fileapi/File.idl new file mode 100644 index 0000000..1937901 --- /dev/null +++ b/WebCore/fileapi/File.idl @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2008 Apple Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +module html { + + interface [ + GenerateNativeConverter, + GenerateToJS, + NoStaticTables + ] File : Blob { + readonly attribute DOMString name; +#if defined(ENABLE_DIRECTORY_UPLOAD) && ENABLE_DIRECTORY_UPLOAD + readonly attribute DOMString webkitRelativePath; +#endif + + // FIXME: obsolete attributes. To be removed. + readonly attribute DOMString fileName; + readonly attribute unsigned long long fileSize; + }; + +} diff --git a/WebCore/fileapi/FileEntry.cpp b/WebCore/fileapi/FileEntry.cpp new file mode 100644 index 0000000..e2a583c --- /dev/null +++ b/WebCore/fileapi/FileEntry.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "FileEntry.h" + +#if ENABLE(FILE_SYSTEM) + +namespace WebCore { + +FileEntry::FileEntry(DOMFileSystem* fileSystem, const String& fullPath) + : Entry(fileSystem, fullPath) +{ +} + +} // namespace + +#endif // ENABLE(FILE_SYSTEM) diff --git a/WebCore/fileapi/FileEntry.h b/WebCore/fileapi/FileEntry.h new file mode 100644 index 0000000..abcc6bb --- /dev/null +++ b/WebCore/fileapi/FileEntry.h @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef FileEntry_h +#define FileEntry_h + +#if ENABLE(FILE_SYSTEM) + +#include "Entry.h" + +namespace WebCore { + +class DOMFileSystem; + +class FileEntry : public Entry { +public: + static PassRefPtr<FileEntry> create(DOMFileSystem* fileSystem, const String& fullPath) + { + return adoptRef(new FileEntry(fileSystem, fullPath)); + } + virtual bool isFile() const { return true; } + +private: + FileEntry(DOMFileSystem* fileSystem, const String& fullPath); +}; + +} // namespace + +#endif // ENABLE(FILE_SYSTEM) + +#endif // FileEntry_h diff --git a/WebCore/fileapi/FileEntry.idl b/WebCore/fileapi/FileEntry.idl new file mode 100644 index 0000000..af3b807 --- /dev/null +++ b/WebCore/fileapi/FileEntry.idl @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +module storage { + interface [ + Conditional=FILE_SYSTEM, + GenerateNativeConverter, + GenerateToJS + ] FileEntry : Entry { + }; +} diff --git a/WebCore/fileapi/FileError.h b/WebCore/fileapi/FileError.h new file mode 100644 index 0000000..1c74c07 --- /dev/null +++ b/WebCore/fileapi/FileError.h @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef FileError_h +#define FileError_h + +#if ENABLE(BLOB) || ENABLE(FILE_WRITER) + +#include "ExceptionCode.h" +#include <wtf/PassRefPtr.h> +#include <wtf/RefCounted.h> + +namespace WebCore { + +class FileError : public RefCounted<FileError> { +public: + static PassRefPtr<FileError> create(ExceptionCode code) { return adoptRef(new FileError(code)); } + + ExceptionCode code() const { return m_code; } + +private: + FileError(ExceptionCode code) + : m_code(code) + { } + + ExceptionCode m_code; +}; + +} // namespace WebCore + +#endif // ENABLE(BLOB) || ENABLE(FILE_WRITER) + +#endif // FileError_h diff --git a/WebCore/fileapi/FileError.idl b/WebCore/fileapi/FileError.idl new file mode 100644 index 0000000..a5e2bac --- /dev/null +++ b/WebCore/fileapi/FileError.idl @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +module html { + interface [ + Conditional=BLOB|FILE_WRITER, + DontCheckEnums, + NoStaticTables + ] FileError { +#if !defined(LANGUAGE_OBJECTIVE_C) + const unsigned short NO_MODIFICATION_ALLOWED_ERR = 7; + const unsigned short NOT_FOUND_ERR = 8; + const unsigned short INVALID_STATE_ERR = 11; + const unsigned short SYNTAX_ERR = 12; + const unsigned short INVALID_MODIFICATION_ERR = 13; +#endif + const unsigned short SECURITY_ERR = 18; + const unsigned short ABORT_ERR = 20; + const unsigned short QUOTA_EXCEEDED_ERR = 22; + const unsigned short NOT_READABLE_ERR = 24; + const unsigned short ENCODING_ERR = 26; + readonly attribute unsigned short code; + }; +} diff --git a/WebCore/fileapi/FileException.h b/WebCore/fileapi/FileException.h new file mode 100644 index 0000000..4419eb7 --- /dev/null +++ b/WebCore/fileapi/FileException.h @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef FileException_h +#define FileException_h + +#if ENABLE(BLOB) || ENABLE(FILE_WRITER) + +#include "ExceptionBase.h" + +namespace WebCore { + +class FileException : public ExceptionBase { +public: + static PassRefPtr<FileException> create(const ExceptionCodeDescription& description) + { + return adoptRef(new FileException(description)); + } + + static const int FileExceptionOffset = 100; + static const int FileExceptionMax = 199; + + enum EventExceptionCode { + UNSPECIFIED_EVENT_TYPE_ERR = FileExceptionOffset + }; + +private: + FileException(const ExceptionCodeDescription& description) + : ExceptionBase(description) + { + } +}; + +} // namespace WebCore + +#endif // ENABLE(BLOB) || ENABLE(FILE_WRITER) + +#endif // FileException_h + diff --git a/WebCore/fileapi/FileException.idl b/WebCore/fileapi/FileException.idl new file mode 100644 index 0000000..cd53a7e --- /dev/null +++ b/WebCore/fileapi/FileException.idl @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +module html { + interface [ + Conditional=BLOB|FILE_WRITER, + DontCheckEnums, + NoStaticTables + ] FileException { + + readonly attribute unsigned short code; + readonly attribute DOMString name; + readonly attribute DOMString message; + +#if defined(LANGUAGE_JAVASCRIPT) && LANGUAGE_JAVASCRIPT + // Override in a Mozilla compatible format + [DontEnum] DOMString toString(); +#endif + + // FileExceptionCode + const unsigned short NO_MODIFICATION_ALLOWED_ERR = 7; + const unsigned short NOT_FOUND_ERR = 8; + const unsigned short INVALID_STATE_ERR = 11; + const unsigned short SYNTAX_ERR = 12; + const unsigned short INVALID_MODIFICATION_ERR = 13; + const unsigned short SECURITY_ERR = 18; + const unsigned short ABORT_ERR = 20; + const unsigned short QUOTA_EXCEEDED_ERR = 22; + const unsigned short NOT_READABLE_ERR = 24; + const unsigned short ENCODING_ERR = 26; + }; +} diff --git a/WebCore/fileapi/FileList.cpp b/WebCore/fileapi/FileList.cpp new file mode 100644 index 0000000..ba81087 --- /dev/null +++ b/WebCore/fileapi/FileList.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2008 Apple Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "FileList.h" + +#include "File.h" + +namespace WebCore { + +FileList::FileList() +{ +} + +File* FileList::item(unsigned index) const +{ + if (index >= m_files.size()) + return 0; + return m_files[index].get(); +} + +} // namespace WebCore diff --git a/WebCore/fileapi/FileList.h b/WebCore/fileapi/FileList.h new file mode 100644 index 0000000..e078191 --- /dev/null +++ b/WebCore/fileapi/FileList.h @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2008 Apple Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef FileList_h +#define FileList_h + +#include "File.h" +#include <wtf/PassRefPtr.h> +#include <wtf/RefCounted.h> +#include <wtf/RefPtr.h> +#include <wtf/Vector.h> + +namespace WebCore { + + class FileList : public RefCounted<FileList> { + public: + static PassRefPtr<FileList> create() + { + return adoptRef(new FileList); + } + + unsigned length() const { return m_files.size(); } + File* item(unsigned index) const; + + bool isEmpty() const { return m_files.isEmpty(); } + void clear() { m_files.clear(); } + void append(PassRefPtr<File> file) { m_files.append(file); } + + private: + FileList(); + + Vector<RefPtr<File> > m_files; + }; + +} // namespace WebCore + +#endif // FileList_h diff --git a/WebCore/fileapi/FileList.idl b/WebCore/fileapi/FileList.idl new file mode 100644 index 0000000..0d0b046 --- /dev/null +++ b/WebCore/fileapi/FileList.idl @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2008 Apple Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +module html { + + interface [ + HasIndexGetter, + NoStaticTables + ] FileList { + readonly attribute unsigned long length; + File item(in [IsIndex] unsigned long index); + }; + +} diff --git a/WebCore/fileapi/FileReader.cpp b/WebCore/fileapi/FileReader.cpp new file mode 100644 index 0000000..747b6ff --- /dev/null +++ b/WebCore/fileapi/FileReader.cpp @@ -0,0 +1,347 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" + +#if ENABLE(BLOB) + +#include "FileReader.h" + +#include "Base64.h" +#include "Blob.h" +#include "CrossThreadTask.h" +#include "File.h" +#include "Logging.h" +#include "ProgressEvent.h" +#include "ResourceError.h" +#include "ResourceRequest.h" +#include "ScriptExecutionContext.h" +#include "TextResourceDecoder.h" +#include "ThreadableLoader.h" +#include <wtf/CurrentTime.h> + +namespace WebCore { + +const double progressNotificationIntervalMS = 50; + +FileReader::FileReader(ScriptExecutionContext* context) + : ActiveDOMObject(context, this) + , m_state(None) + , m_readType(ReadFileAsBinaryString) + , m_result("") + , m_isRawDataConverted(false) + , m_bytesLoaded(0) + , m_totalBytes(0) + , m_lastProgressNotificationTimeMS(0) +{ +} + +FileReader::~FileReader() +{ + terminate(); +} + +bool FileReader::hasPendingActivity() const +{ + return (m_state != None && m_state != Completed) || ActiveDOMObject::hasPendingActivity(); +} + +bool FileReader::canSuspend() const +{ + // FIXME: It is not currently possible to suspend a FileReader, so pages with FileReader can not go into page cache. + return false; +} + +void FileReader::stop() +{ + terminate(); +} + +void FileReader::readAsBinaryString(Blob* blob) +{ + if (!blob) + return; + + LOG(FileAPI, "FileReader: reading as binary: %s %s\n", blob->url().string().utf8().data(), blob->isFile() ? static_cast<File*>(blob)->path().utf8().data() : ""); + + readInternal(blob, ReadFileAsBinaryString); +} + +void FileReader::readAsText(Blob* blob, const String& encoding) +{ + if (!blob) + return; + + LOG(FileAPI, "FileReader: reading as text: %s %s\n", blob->url().string().utf8().data(), blob->isFile() ? static_cast<File*>(blob)->path().utf8().data() : ""); + + if (!encoding.isEmpty()) + m_encoding = TextEncoding(encoding); + readInternal(blob, ReadFileAsText); +} + +void FileReader::readAsDataURL(Blob* blob) +{ + if (!blob) + return; + + LOG(FileAPI, "FileReader: reading as data URL: %s %s\n", blob->url().string().utf8().data(), blob->isFile() ? static_cast<File*>(blob)->path().utf8().data() : ""); + + m_fileType = blob->type(); + readInternal(blob, ReadFileAsDataURL); +} + +static void delayedStart(ScriptExecutionContext*, FileReader* reader) +{ + reader->start(); +} + +void FileReader::readInternal(Blob* blob, ReadType type) +{ + // readAs*** methods() can be called multiple times. Only the last call before the actual reading happens is processed. + if (m_state != None && m_state != Starting) + return; + + if (m_state == None) + scriptExecutionContext()->postTask(createCallbackTask(&delayedStart, this)); + + m_blob = blob; + m_readType = type; + m_state = Starting; +} + +void FileReader::abort() +{ + LOG(FileAPI, "FileReader: aborting\n"); + + terminate(); + + m_result = ""; + m_error = FileError::create(ABORT_ERR); + + fireEvent(eventNames().errorEvent); + fireEvent(eventNames().abortEvent); + fireEvent(eventNames().loadendEvent); +} + +void FileReader::terminate() +{ + if (m_loader) { + m_loader->cancel(); + m_loader = 0; + } + m_state = Completed; +} + +void FileReader::start() +{ + m_state = Opening; + + // The blob is read by routing through the request handling layer given the blob url. + ResourceRequest request(m_blob->url()); + request.setHTTPMethod("GET"); + + ThreadableLoaderOptions options; + options.sendLoadCallbacks = true; + options.sniffContent = false; + options.forcePreflight = false; + options.allowCredentials = true; + options.crossOriginRequestPolicy = DenyCrossOriginRequests; + + m_loader = ThreadableLoader::create(scriptExecutionContext(), this, request, options); +} + +void FileReader::didReceiveResponse(const ResourceResponse& response) +{ + if (response.httpStatusCode() != 200) { + failed(response.httpStatusCode()); + return; + } + + m_state = Reading; + fireEvent(eventNames().loadstartEvent); + + m_totalBytes = response.expectedContentLength(); +} + +void FileReader::didReceiveData(const char* data, int lengthReceived) +{ + ASSERT(data && lengthReceived); + + // Bail out if we have aborted the reading. + if (m_state == Completed) + return; + + switch (m_readType) { + case ReadFileAsBinaryString: + m_result += String(data, static_cast<unsigned>(lengthReceived)); + break; + case ReadFileAsText: + case ReadFileAsDataURL: + m_rawData.append(data, static_cast<unsigned>(lengthReceived)); + m_isRawDataConverted = false; + break; + default: + ASSERT_NOT_REACHED(); + } + + m_bytesLoaded += lengthReceived; + + // Fire the progress event at least every 50ms. + double now = WTF::currentTimeMS(); + if (!m_lastProgressNotificationTimeMS) + m_lastProgressNotificationTimeMS = now; + else if (now - m_lastProgressNotificationTimeMS > progressNotificationIntervalMS) { + fireEvent(eventNames().progressEvent); + m_lastProgressNotificationTimeMS = now; + } +} + +void FileReader::didFinishLoading(unsigned long) +{ + m_state = Completed; + + fireEvent(eventNames().loadEvent); + fireEvent(eventNames().loadendEvent); + + m_loader = 0; +} + +void FileReader::didFail(const ResourceError&) +{ + // Treat as internal error. + failed(500); +} + +void FileReader::failed(int httpStatusCode) +{ + m_state = Completed; + + m_error = FileError::create(httpStatusCodeToExceptionCode(httpStatusCode)); + fireEvent(eventNames().errorEvent); + fireEvent(eventNames().loadendEvent); + + m_loader = 0; +} + +ExceptionCode FileReader::httpStatusCodeToExceptionCode(int httpStatusCode) +{ + switch (httpStatusCode) { + case 403: + return SECURITY_ERR; + case 404: + return NOT_FOUND_ERR; + default: + return NOT_READABLE_ERR; + } +} + +void FileReader::fireEvent(const AtomicString& type) +{ + // FIXME: the current ProgressEvent uses "unsigned long" for total and loaded attributes. Need to talk with the spec writer to resolve the issue. + dispatchEvent(ProgressEvent::create(type, true, static_cast<unsigned>(m_bytesLoaded), static_cast<unsigned>(m_totalBytes))); +} + +FileReader::ReadyState FileReader::readyState() const +{ + switch (m_state) { + case None: + case Starting: + return EMPTY; + case Opening: + case Reading: + return LOADING; + case Completed: + return DONE; + } + ASSERT_NOT_REACHED(); + return EMPTY; +} + +const ScriptString& FileReader::result() +{ + // If reading as binary string, we can return the result immediately. + if (m_readType == ReadFileAsBinaryString) + return m_result; + + // If we already convert the raw data received so far, we can return the result now. + if (m_isRawDataConverted) + return m_result; + m_isRawDataConverted = true; + + if (m_readType == ReadFileAsText) + convertToText(); + // For data URL, we only do the coversion until we receive all the raw data. + else if (m_readType == ReadFileAsDataURL && m_state == Completed) + convertToDataURL(m_rawData, m_fileType, m_result); + + return m_result; +} + +void FileReader::convertToText() +{ + if (!m_rawData.size()) { + m_result = ""; + return; + } + + // Decode the data. + // The File API spec says that we should use the supplied encoding if it is valid. However, we choose to ignore this + // requirement in order to be consistent with how WebKit decodes the web content: always has the BOM override the + // provided encoding. + // FIXME: consider supporting incremental decoding to improve the perf. + if (!m_decoder) + m_decoder = TextResourceDecoder::create("text/plain", m_encoding.isValid() ? m_encoding : UTF8Encoding()); + m_result = m_decoder->decode(&m_rawData.at(0), m_rawData.size()); + + if (m_state == Completed && !m_error) + m_result += m_decoder->flush(); +} + +void FileReader::convertToDataURL(const Vector<char>& rawData, const String& fileType, ScriptString& result) +{ + result = "data:"; + + if (!rawData.size()) + return; + + result += fileType; + if (!fileType.isEmpty()) + result += ";"; + result += "base64,"; + + Vector<char> out; + base64Encode(rawData, out); + out.append('\0'); + result += out.data(); +} + +} // namespace WebCore + +#endif // ENABLE(BLOB) diff --git a/WebCore/fileapi/FileReader.h b/WebCore/fileapi/FileReader.h new file mode 100644 index 0000000..48ce229 --- /dev/null +++ b/WebCore/fileapi/FileReader.h @@ -0,0 +1,176 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef FileReader_h +#define FileReader_h + +#if ENABLE(BLOB) + +#include "ActiveDOMObject.h" +#include "EventTarget.h" +#include "FileError.h" +#include "PlatformString.h" +#include "ScriptString.h" +#include "TextEncoding.h" +#include "ThreadableLoaderClient.h" +#include <wtf/PassRefPtr.h> +#include <wtf/RefCounted.h> +#include <wtf/RefPtr.h> +#include <wtf/Vector.h> + +namespace WebCore { + +class Blob; +class ScriptExecutionContext; +class TextResourceDecoder; +class ThreadableLoader; + +class FileReader : public RefCounted<FileReader>, public ActiveDOMObject, public EventTarget, public ThreadableLoaderClient { +public: + static PassRefPtr<FileReader> create(ScriptExecutionContext* context) + { + return adoptRef(new FileReader(context)); + } + + virtual ~FileReader(); + + enum ReadyState { + EMPTY = 0, + LOADING = 1, + DONE = 2 + }; + + void readAsBinaryString(Blob*); + void readAsText(Blob*, const String& encoding = ""); + void readAsDataURL(Blob*); + void abort(); + + void start(); + + ReadyState readyState() const; + PassRefPtr<FileError> error() { return m_error; } + const ScriptString& result(); + + // Helper methods, also used by FileReaderSync. + static ExceptionCode httpStatusCodeToExceptionCode(int httpStatusCode); + static void convertToDataURL(const Vector<char>& rawData, const String& fileType, ScriptString& result); + + // ActiveDOMObject + virtual bool canSuspend() const; + virtual void stop(); + virtual bool hasPendingActivity() const; + + // EventTarget + virtual FileReader* toFileReader() { return this; } + virtual ScriptExecutionContext* scriptExecutionContext() const { return ActiveDOMObject::scriptExecutionContext(); } + + // ThreadableLoaderClient + virtual void didReceiveResponse(const ResourceResponse&); + virtual void didReceiveData(const char*, int); + virtual void didFinishLoading(unsigned long identifier); + virtual void didFail(const ResourceError&); + + using RefCounted<FileReader>::ref; + using RefCounted<FileReader>::deref; + + DEFINE_ATTRIBUTE_EVENT_LISTENER(loadstart); + DEFINE_ATTRIBUTE_EVENT_LISTENER(progress); + DEFINE_ATTRIBUTE_EVENT_LISTENER(load); + DEFINE_ATTRIBUTE_EVENT_LISTENER(abort); + DEFINE_ATTRIBUTE_EVENT_LISTENER(error); + DEFINE_ATTRIBUTE_EVENT_LISTENER(loadend); + +private: + enum ReadType { + ReadFileAsBinaryString, + ReadFileAsText, + ReadFileAsDataURL + }; + enum InternalState { + None, + Starting, + Opening, + Reading, + Completed + }; + + FileReader(ScriptExecutionContext*); + + // EventTarget + virtual void refEventTarget() { ref(); } + virtual void derefEventTarget() { deref(); } + virtual EventTargetData* eventTargetData() { return &m_eventTargetData; } + virtual EventTargetData* ensureEventTargetData() { return &m_eventTargetData; } + + void terminate(); + void readInternal(Blob*, ReadType); + void failed(int httpStatusCode); + void fireErrorEvent(int httpStatusCode); + void fireEvent(const AtomicString& type); + void convertToText(); + void convertToDataURL(); + + InternalState m_state; + EventTargetData m_eventTargetData; + + RefPtr<Blob> m_blob; + ReadType m_readType; + TextEncoding m_encoding; + + // Like XMLHttpRequest.m_responseText, we keep this as a ScriptString, not a WTF::String. + // That's because these strings can easily get huge (they are filled from the file) and + // because JS can easily observe many intermediate states, so it's very useful to be + // able to share the buffer with JavaScript versions of the whole or partial string. + // In contrast, this string doesn't interact much with the rest of the engine so it's not that + // big a cost that it isn't a String. + ScriptString m_result; + + // The raw data. We have to keep track of all the raw data for it to be converted to text or data URL data. + Vector<char> m_rawData; + bool m_isRawDataConverted; + + // The decoder used to decode the text data. + RefPtr<TextResourceDecoder> m_decoder; + + // Needed to create data URL. + String m_fileType; + + RefPtr<ThreadableLoader> m_loader; + RefPtr<FileError> m_error; + long long m_bytesLoaded; + long long m_totalBytes; + double m_lastProgressNotificationTimeMS; +}; + +} // namespace WebCore + +#endif // ENABLE(BLOB) + +#endif // FileReader_h diff --git a/WebCore/fileapi/FileReader.idl b/WebCore/fileapi/FileReader.idl new file mode 100644 index 0000000..b36e9d3 --- /dev/null +++ b/WebCore/fileapi/FileReader.idl @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +module html { + interface [ + Conditional=BLOB, + CanBeConstructed, + CallWith=ScriptExecutionContext, + EventTarget, + NoStaticTables + ] FileReader { + // ready states + const unsigned short EMPTY = 0; + const unsigned short LOADING = 1; + const unsigned short DONE = 2; + readonly attribute unsigned short readyState; + + // async read methods + void readAsBinaryString(in Blob fileBlob); + void readAsText(in Blob fileBlob, in [Optional] DOMString encoding); + void readAsDataURL(in File file); + + void abort(); + + // file data + readonly attribute [ConvertScriptString] DOMString result; + + readonly attribute FileError error; + + attribute EventListener onloadstart; + attribute EventListener onprogress; + attribute EventListener onload; + attribute EventListener onabort; + attribute EventListener onerror; + attribute EventListener onloadend; + }; +} diff --git a/WebCore/fileapi/FileReaderSync.cpp b/WebCore/fileapi/FileReaderSync.cpp new file mode 100644 index 0000000..a907044 --- /dev/null +++ b/WebCore/fileapi/FileReaderSync.cpp @@ -0,0 +1,193 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" + +#if ENABLE(BLOB) + +#include "FileReaderSync.h" + +#include "Base64.h" +#include "Blob.h" +#include "FileReader.h" +#include "ResourceRequest.h" +#include "ScriptExecutionContext.h" +#include "TextEncoding.h" +#include "TextResourceDecoder.h" +#include "ThreadableLoader.h" + +namespace WebCore { + +class FileReaderSyncLoader : public ThreadableLoaderClient { +public: + // If the output result is provided, use it. Otherwise, save it as the raw data. + FileReaderSyncLoader(ScriptString* result); + + // Returns the http status code. + void start(ScriptExecutionContext*, const ResourceRequest&, ExceptionCode&); + + // ThreadableLoaderClient + virtual void didReceiveResponse(const ResourceResponse&); + virtual void didReceiveData(const char*, int); + virtual void didFinishLoading(unsigned long identifier); + virtual void didFail(const ResourceError&); + + const Vector<char>& rawData() const { return m_rawData; } + +private: + // The output result. The caller provides this in order to load the binary data directly. + ScriptString* m_result; + + // The raw data. The caller does not provide the above output result and we need to save it here. + Vector<char> m_rawData; + + int m_httpStatusCode; +}; + +FileReaderSyncLoader::FileReaderSyncLoader(ScriptString* result) + : m_result(result) + , m_httpStatusCode(0) +{ +} + +void FileReaderSyncLoader::start(ScriptExecutionContext* scriptExecutionContext, const ResourceRequest& request, ExceptionCode& ec) +{ + ThreadableLoaderOptions options; + options.sendLoadCallbacks = true; + options.sniffContent = false; + options.forcePreflight = false; + options.allowCredentials = true; + options.crossOriginRequestPolicy = DenyCrossOriginRequests; + + ThreadableLoader::loadResourceSynchronously(scriptExecutionContext, request, *this, options); + + ec = (m_httpStatusCode == 200) ? 0 : FileReader::httpStatusCodeToExceptionCode(m_httpStatusCode); +} + +void FileReaderSyncLoader::didReceiveResponse(const ResourceResponse& response) +{ + m_httpStatusCode = response.httpStatusCode(); +} + +void FileReaderSyncLoader::didReceiveData(const char* data, int lengthReceived) +{ + if (m_result) + *m_result += String(data, static_cast<unsigned>(lengthReceived)); + else + m_rawData.append(data, static_cast<unsigned>(lengthReceived)); +} + +void FileReaderSyncLoader::didFinishLoading(unsigned long) +{ +} + +void FileReaderSyncLoader::didFail(const ResourceError&) +{ + // Treat as internal error. + m_httpStatusCode = 500; +} + +FileReaderSync::FileReaderSync() + : m_result("") +{ +} + +const ScriptString& FileReaderSync::readAsBinaryString(ScriptExecutionContext* scriptExecutionContext, Blob* blob, ExceptionCode& ec) +{ + if (!blob) + return m_result; + + read(scriptExecutionContext, blob, ReadAsBinaryString, ec); + return m_result; +} + +const ScriptString& FileReaderSync::readAsText(ScriptExecutionContext* scriptExecutionContext, Blob* blob, const String& encoding, ExceptionCode& ec) +{ + if (!blob) + return m_result; + + m_encoding = encoding; + read(scriptExecutionContext, blob, ReadAsText, ec); + return m_result; +} + +const ScriptString& FileReaderSync::readAsDataURL(ScriptExecutionContext* scriptExecutionContext, Blob* blob, ExceptionCode& ec) +{ + if (!blob) + return m_result; + + read(scriptExecutionContext, blob, ReadAsDataURL, ec); + return m_result; +} + +void FileReaderSync::read(ScriptExecutionContext* scriptExecutionContext, Blob* blob, ReadType readType, ExceptionCode& ec) +{ + // The blob is read by routing through the request handling layer given the blob url. + ResourceRequest request(blob->url()); + request.setHTTPMethod("GET"); + + FileReaderSyncLoader loader((readType == ReadAsBinaryString) ? &m_result : 0); + loader.start(scriptExecutionContext, request, ec); + if (ec) + return; + + switch (readType) { + case ReadAsBinaryString: + // Nothing to do since we need no conversion. + return; + case ReadAsText: + convertToText(loader.rawData().data(), loader.rawData().size(), m_result); + return; + case ReadAsDataURL: + FileReader::convertToDataURL(loader.rawData(), blob->type(), m_result); + return; + } + + ASSERT_NOT_REACHED(); +} + +void FileReaderSync::convertToText(const char* data, int size, ScriptString& result) +{ + if (!size) + return; + + // Decode the data. + // The File API spec says that we should use the supplied encoding if it is valid. However, we choose to ignore this + // requirement in order to be consistent with how WebKit decodes the web content: always have the BOM override the + // provided encoding. + // FIXME: consider supporting incremental decoding to improve the perf. + RefPtr<TextResourceDecoder> decoder = TextResourceDecoder::create("text/plain", m_encoding.isEmpty() ? UTF8Encoding() : TextEncoding(m_encoding)); + result = decoder->decode(data, size); + result += decoder->flush(); +} + +} // namespace WebCore + +#endif // ENABLE(BLOB) diff --git a/WebCore/fileapi/FileReaderSync.h b/WebCore/fileapi/FileReaderSync.h new file mode 100644 index 0000000..a517cab --- /dev/null +++ b/WebCore/fileapi/FileReaderSync.h @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef FileReaderSync_h +#define FileReaderSync_h + +#if ENABLE(BLOB) + +#include "ExceptionCode.h" +#include "ScriptString.h" +#include <wtf/PassRefPtr.h> +#include <wtf/RefCounted.h> + +namespace WebCore { + +class Blob; +class ScriptExecutionContext; + +class FileReaderSync : public RefCounted<FileReaderSync> { +public: + static PassRefPtr<FileReaderSync> create() + { + return adoptRef(new FileReaderSync()); + } + + virtual ~FileReaderSync() { } + + const ScriptString& readAsBinaryString(ScriptExecutionContext*, Blob*, ExceptionCode&); + const ScriptString& readAsText(ScriptExecutionContext* scriptExecutionContext, Blob* blob, ExceptionCode& ec) + { + return readAsText(scriptExecutionContext, blob, "", ec); + } + const ScriptString& readAsText(ScriptExecutionContext*, Blob*, const String& encoding, ExceptionCode&); + const ScriptString& readAsDataURL(ScriptExecutionContext*, Blob*, ExceptionCode&); + +private: + enum ReadType { + ReadAsBinaryString, + ReadAsText, + ReadAsDataURL + }; + + FileReaderSync(); + + void read(ScriptExecutionContext*, Blob*, ReadType, ExceptionCode&); + void convertToText(const char* data, int size, ScriptString& result); + + // Like XMLHttpRequest.m_responseText, we keep this as a ScriptString, not a WTF::String. + // That's because these strings can easily get huge (they are filled from the file) and + // because JS can easily observe many intermediate states, so it's very useful to be + // able to share the buffer with JavaScript versions of the whole or partial string. + // In contrast, this string doesn't interact much with the rest of the engine so it's not that + // big a cost that it isn't a String. + ScriptString m_result; + + String m_encoding; +}; + +} // namespace WebCore + +#endif // ENABLE(BLOB) + +#endif // FileReaderSync_h diff --git a/WebCore/fileapi/FileReaderSync.idl b/WebCore/fileapi/FileReaderSync.idl new file mode 100644 index 0000000..72818b1 --- /dev/null +++ b/WebCore/fileapi/FileReaderSync.idl @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +module html { + interface [ + Conditional=BLOB, + CanBeConstructed, + NoStaticTables + ] FileReaderSync { + [CallWith=ScriptExecutionContext, ConvertScriptString] DOMString readAsBinaryString(in Blob blob) + raises(FileException); + [CallWith=ScriptExecutionContext, ConvertScriptString] DOMString readAsText(in Blob blob, in [Optional] DOMString encoding) + raises(FileException); + [CallWith=ScriptExecutionContext, ConvertScriptString] DOMString readAsDataURL(in Blob blob) + raises(FileException); + }; +} diff --git a/WebCore/fileapi/FileStreamProxy.cpp b/WebCore/fileapi/FileStreamProxy.cpp new file mode 100644 index 0000000..30813d3 --- /dev/null +++ b/WebCore/fileapi/FileStreamProxy.cpp @@ -0,0 +1,219 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" + +#if ENABLE(BLOB) || ENABLE(FILE_WRITER) + +#include "FileStreamProxy.h" + +#include "Blob.h" +#include "CrossThreadTask.h" +#include "FileStream.h" +#include "FileThread.h" +#include "FileThreadTask.h" +#include "PlatformString.h" +#include "ScriptExecutionContext.h" + +namespace WebCore { + +inline FileStreamProxy::FileStreamProxy(ScriptExecutionContext* context, FileStreamClient* client) + : AsyncFileStream(client) + , m_context(context) + , m_stream(FileStream::create()) +{ +} + +PassRefPtr<FileStreamProxy> FileStreamProxy::create(ScriptExecutionContext* context, FileStreamClient* client) +{ + RefPtr<FileStreamProxy> proxy = adoptRef(new FileStreamProxy(context, client)); + + // Hold an ref so that the instance will not get deleted while there are tasks on the file thread. + // This is balanced by the deref in derefProxyOnContext below. + proxy->ref(); + + proxy->fileThread()->postTask(createFileThreadTask(proxy.get(), &FileStreamProxy::startOnFileThread)); + + return proxy.release(); +} + +FileStreamProxy::~FileStreamProxy() +{ +} + +FileThread* FileStreamProxy::fileThread() +{ + ASSERT(m_context->isContextThread()); + ASSERT(m_context->fileThread()); + return m_context->fileThread(); +} + +static void didStart(ScriptExecutionContext*, FileStreamProxy* proxy) +{ + if (proxy->client()) + proxy->client()->didStart(); +} + +void FileStreamProxy::startOnFileThread() +{ + m_stream->start(); + m_context->postTask(createCallbackTask(&didStart, this)); +} + +void FileStreamProxy::stop() +{ + // Clear the client so that we won't be calling callbacks on the client. + setClient(0); + + fileThread()->unscheduleTasks(m_stream.get()); + fileThread()->postTask(createFileThreadTask(this, &FileStreamProxy::stopOnFileThread)); +} + +static void derefProxyOnContext(ScriptExecutionContext*, FileStreamProxy* proxy) +{ + ASSERT(proxy->hasOneRef()); + proxy->deref(); +} + +void FileStreamProxy::stopOnFileThread() +{ + m_stream->stop(); + m_context->postTask(createCallbackTask(&derefProxyOnContext, this)); +} + +static void didGetSize(ScriptExecutionContext*, FileStreamProxy* proxy, long long size) +{ + if (proxy->client()) + proxy->client()->didGetSize(size); +} + +void FileStreamProxy::getSize(const String& path, double expectedModificationTime) +{ + fileThread()->postTask(createFileThreadTask(this, &FileStreamProxy::getSizeOnFileThread, path, expectedModificationTime)); +} + +void FileStreamProxy::getSizeOnFileThread(const String& path, double expectedModificationTime) +{ + long long size = m_stream->getSize(path, expectedModificationTime); + m_context->postTask(createCallbackTask(&didGetSize, this, size)); +} + +static void didOpen(ScriptExecutionContext*, FileStreamProxy* proxy, bool success) +{ + if (proxy->client()) + proxy->client()->didOpen(success); +} + +void FileStreamProxy::openForRead(const String& path, long long offset, long long length) +{ + fileThread()->postTask(createFileThreadTask(this, &FileStreamProxy::openForReadOnFileThread, path, offset, length)); +} + +void FileStreamProxy::openForReadOnFileThread(const String& path, long long offset, long long length) +{ + bool success = m_stream->openForRead(path, offset, length); + m_context->postTask(createCallbackTask(&didOpen, this, success)); +} + +void FileStreamProxy::openForWrite(const String& path) +{ + fileThread()->postTask(createFileThreadTask(this, &FileStreamProxy::openForWriteOnFileThread, path)); +} + +void FileStreamProxy::openForWriteOnFileThread(const String& path) +{ + bool success = m_stream->openForWrite(path); + m_context->postTask(createCallbackTask(&didOpen, this, success)); +} + +void FileStreamProxy::close() +{ + fileThread()->postTask(createFileThreadTask(this, &FileStreamProxy::closeOnFileThread)); +} + +void FileStreamProxy::closeOnFileThread() +{ + m_stream->close(); +} + +static void didRead(ScriptExecutionContext*, FileStreamProxy* proxy, int bytesRead) +{ + if (proxy->client()) + proxy->client()->didRead(bytesRead); +} + +void FileStreamProxy::read(char* buffer, int length) +{ + fileThread()->postTask(createFileThreadTask(this, &FileStreamProxy::readOnFileThread, buffer, length)); +} + +void FileStreamProxy::readOnFileThread(char* buffer, int length) +{ + int bytesRead = m_stream->read(buffer, length); + m_context->postTask(createCallbackTask(&didRead, this, bytesRead)); +} + +static void didWrite(ScriptExecutionContext*, FileStreamProxy* proxy, int bytesWritten) +{ + if (proxy->client()) + proxy->client()->didWrite(bytesWritten); +} + +void FileStreamProxy::write(const KURL& blobURL, long long position, int length) +{ + fileThread()->postTask(createFileThreadTask(this, &FileStreamProxy::writeOnFileThread, blobURL, position, length)); +} + +void FileStreamProxy::writeOnFileThread(const KURL& blobURL, long long position, int length) +{ + int bytesWritten = m_stream->write(blobURL, position, length); + m_context->postTask(createCallbackTask(&didWrite, this, bytesWritten)); +} + +static void didTruncate(ScriptExecutionContext*, FileStreamProxy* proxy, bool success) +{ + if (proxy->client()) + proxy->client()->didTruncate(success); +} + +void FileStreamProxy::truncate(long long position) +{ + fileThread()->postTask(createFileThreadTask(this, &FileStreamProxy::truncateOnFileThread, position)); +} + +void FileStreamProxy::truncateOnFileThread(long long position) +{ + bool success = m_stream->truncate(position); + m_context->postTask(createCallbackTask(&didTruncate, this, success)); +} + +} // namespace WebCore + +#endif // ENABLE(BLOB) || ENABLE(FILE_WRITER) diff --git a/WebCore/fileapi/FileStreamProxy.h b/WebCore/fileapi/FileStreamProxy.h new file mode 100644 index 0000000..35a3af8 --- /dev/null +++ b/WebCore/fileapi/FileStreamProxy.h @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * Copyright (C) 2010 Apple Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef FileStreamProxy_h +#define FileStreamProxy_h + +#if ENABLE(BLOB) || ENABLE(FILE_WRITER) + +#include "AsyncFileStream.h" +#include <wtf/Forward.h> +#include <wtf/PassRefPtr.h> +#include <wtf/RefCounted.h> +#include <wtf/RefPtr.h> + +namespace WebCore { + +class FileStream; +class FileThread; +class KURL; +class ScriptExecutionContext; + +// A proxy module that asynchronously calls corresponding FileStream methods on the file thread. Note: you must call stop() first and then release the reference to destruct the FileStreamProxy instance. +class FileStreamProxy : public AsyncFileStream { +public: + static PassRefPtr<FileStreamProxy> create(ScriptExecutionContext*, FileStreamClient*); + virtual ~FileStreamProxy(); + + virtual void getSize(const String& path, double expectedModificationTime); + virtual void openForRead(const String& path, long long offset, long long length); + virtual void openForWrite(const String& path); + virtual void close(); + virtual void read(char* buffer, int length); + virtual void write(const KURL& blobURL, long long position, int length); + virtual void truncate(long long position); + + // Stops the proxy and scedules it to be destructed. All the pending tasks will be aborted and the file stream will be closed. + // Note: the caller should deref the instance immediately after calling stop(). + virtual void stop(); + +private: + FileStreamProxy(ScriptExecutionContext*, FileStreamClient*); + + FileThread* fileThread(); + + // Called on File thread. + void startOnFileThread(); + void stopOnFileThread(); + void getSizeOnFileThread(const String& path, double expectedModificationTime); + void openForReadOnFileThread(const String& path, long long offset, long long length); + void openForWriteOnFileThread(const String& path); + void closeOnFileThread(); + void readOnFileThread(char* buffer, int length); + void writeOnFileThread(const KURL& blobURL, long long position, int length); + void truncateOnFileThread(long long position); + + RefPtr<ScriptExecutionContext> m_context; + RefPtr<FileStream> m_stream; +}; + +} // namespace WebCore + +#endif // ENABLE(BLOB) || ENABLE(FILE_WRITER) + +#endif // FileStreamProxy_h diff --git a/WebCore/fileapi/FileSystemCallback.h b/WebCore/fileapi/FileSystemCallback.h new file mode 100644 index 0000000..63f8416 --- /dev/null +++ b/WebCore/fileapi/FileSystemCallback.h @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef FileSystemCallback_h +#define FileSystemCallback_h + +#if ENABLE(FILE_SYSTEM) + +#include <wtf/RefCounted.h> + +namespace WebCore { + +class DOMFileSystem; + +class FileSystemCallback : public RefCounted<FileSystemCallback> { +public: + virtual ~FileSystemCallback() { } + virtual bool handleEvent(DOMFileSystem*) = 0; +}; + +} // namespace + +#endif // ENABLE(FILE_SYSTEM) + +#endif // FileSystemCallback_h diff --git a/WebCore/fileapi/FileSystemCallback.idl b/WebCore/fileapi/FileSystemCallback.idl new file mode 100644 index 0000000..cf686ff --- /dev/null +++ b/WebCore/fileapi/FileSystemCallback.idl @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +module storage { + interface [ + Conditional=FILE_SYSTEM, + Callback + ] FileSystemCallback { + boolean handleEvent(in DOMFileSystem fileSystem); + }; +} diff --git a/WebCore/fileapi/FileSystemCallbacks.cpp b/WebCore/fileapi/FileSystemCallbacks.cpp new file mode 100644 index 0000000..741df78 --- /dev/null +++ b/WebCore/fileapi/FileSystemCallbacks.cpp @@ -0,0 +1,209 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "FileSystemCallbacks.h" + +#if ENABLE(FILE_SYSTEM) + +#include "AsyncFileSystem.h" +#include "DOMFilePath.h" +#include "DOMFileSystem.h" +#include "DirectoryEntry.h" +#include "EntriesCallback.h" +#include "EntryArray.h" +#include "EntryCallback.h" +#include "ErrorCallback.h" +#include "ExceptionCode.h" +#include "FileEntry.h" +#include "FileError.h" +#include "FileSystemCallback.h" +#include "Metadata.h" +#include "MetadataCallback.h" +#include "ScriptExecutionContext.h" +#include "VoidCallback.h" + +namespace WebCore { + +FileSystemCallbacksBase::FileSystemCallbacksBase(PassRefPtr<ErrorCallback> errorCallback) + : m_errorCallback(errorCallback) +{ +} + +FileSystemCallbacksBase::~FileSystemCallbacksBase() +{ +} + +void FileSystemCallbacksBase::didSucceed() +{ + // Each subclass must implement an appropriate one. + ASSERT_NOT_REACHED(); +} + +void FileSystemCallbacksBase::didOpenFileSystem(const String&, PassOwnPtr<AsyncFileSystem>) +{ + // Each subclass must implement an appropriate one. + ASSERT_NOT_REACHED(); +} + +void FileSystemCallbacksBase::didReadMetadata(double) +{ + // Each subclass must implement an appropriate one. + ASSERT_NOT_REACHED(); +} + +void FileSystemCallbacksBase::didReadDirectoryEntries(bool) +{ + // Each subclass must implement an appropriate one. + ASSERT_NOT_REACHED(); +} + +void FileSystemCallbacksBase::didReadDirectoryEntry(const String&, bool) +{ + // Each subclass must implement an appropriate one. + ASSERT_NOT_REACHED(); +} + +void FileSystemCallbacksBase::didFail(int code) +{ + if (m_errorCallback) { + m_errorCallback->handleEvent(FileError::create(code).get()); + m_errorCallback.clear(); + } +} + +// EntryCallbacks ------------------------------------------------------------- + +EntryCallbacks::EntryCallbacks(PassRefPtr<EntryCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback, DOMFileSystem* fileSystem, const String& expectedPath, bool isDirectory) + : FileSystemCallbacksBase(errorCallback) + , m_successCallback(successCallback) + , m_fileSystem(fileSystem) + , m_expectedPath(expectedPath) + , m_isDirectory(isDirectory) +{ +} + +void EntryCallbacks::didSucceed() +{ + if (m_successCallback) { + if (m_isDirectory) + m_successCallback->handleEvent(DirectoryEntry::create(m_fileSystem, m_expectedPath).get()); + else + m_successCallback->handleEvent(FileEntry::create(m_fileSystem, m_expectedPath).get()); + } + m_successCallback.clear(); +} + +// EntriesCallbacks ----------------------------------------------------------- + +EntriesCallbacks::EntriesCallbacks(PassRefPtr<EntriesCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback, DOMFileSystem* fileSystem, const String& basePath) + : FileSystemCallbacksBase(errorCallback) + , m_successCallback(successCallback) + , m_fileSystem(fileSystem) + , m_basePath(basePath) +{ +} + +void EntriesCallbacks::didReadDirectoryEntry(const String& name, bool isDirectory) +{ + if (!m_entries) + m_entries = EntryArray::create(); + if (isDirectory) + m_entries->append(DirectoryEntry::create(m_fileSystem, DOMFilePath::append(m_basePath, name))); + else + m_entries->append(FileEntry::create(m_fileSystem, DOMFilePath::append(m_basePath, name))); +} + +void EntriesCallbacks::didReadDirectoryEntries(bool hasMore) +{ + ASSERT(m_entries); + if (m_successCallback) { + m_successCallback->handleEvent(m_entries.get()); + m_entries->clear(); + if (!hasMore) { + // If there're no more entries, call back once more with an empty array. + m_successCallback->handleEvent(m_entries.get()); + m_successCallback.clear(); + } + } +} + +// FileSystemCallbacks -------------------------------------------------------- + +FileSystemCallbacks::FileSystemCallbacks(PassRefPtr<FileSystemCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback, ScriptExecutionContext* context) + : FileSystemCallbacksBase(errorCallback) + , m_successCallback(successCallback) + , m_scriptExecutionContext(context) +{ +} + +void FileSystemCallbacks::didOpenFileSystem(const String& name, PassOwnPtr<AsyncFileSystem> asyncFileSystem) +{ + if (m_successCallback) { + ASSERT(asyncFileSystem); + m_successCallback->handleEvent(DOMFileSystem::create(m_scriptExecutionContext.get(), name, asyncFileSystem.leakPtr()).get()); + m_scriptExecutionContext.clear(); + } + m_successCallback.clear(); +} + +// MetadataCallbacks ---------------------------------------------------------- + +MetadataCallbacks::MetadataCallbacks(PassRefPtr<MetadataCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback) + : FileSystemCallbacksBase(errorCallback) + , m_successCallback(successCallback) +{ +} + +void MetadataCallbacks::didReadMetadata(double modificationTime) +{ + if (m_successCallback) + m_successCallback->handleEvent(Metadata::create(modificationTime).get()); + m_successCallback.clear(); +} + +// VoidCallbacks -------------------------------------------------------------- + +VoidCallbacks::VoidCallbacks(PassRefPtr<VoidCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback) + : FileSystemCallbacksBase(errorCallback) + , m_successCallback(successCallback) +{ +} + +void VoidCallbacks::didSucceed() +{ + if (m_successCallback) + m_successCallback->handleEvent(); + m_successCallback.clear(); +} + +} // namespace + +#endif // ENABLE(FILE_SYSTEM) diff --git a/WebCore/fileapi/FileSystemCallbacks.h b/WebCore/fileapi/FileSystemCallbacks.h new file mode 100644 index 0000000..31a2a0c --- /dev/null +++ b/WebCore/fileapi/FileSystemCallbacks.h @@ -0,0 +1,137 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef FileSystemCallbacks_h +#define FileSystemCallbacks_h + +#if ENABLE(FILE_SYSTEM) + +#include "AsyncFileSystemCallbacks.h" +#include "PlatformString.h" +#include <wtf/PassRefPtr.h> +#include <wtf/Vector.h> + +namespace WebCore { + +class DOMFileSystem; +class ErrorCallback; +class EntriesCallback; +class EntryArray; +class EntryCallback; +class FileSystemCallback; +class MetadataCallback; +class ScriptExecutionContext; +class VoidCallback; + +class FileSystemCallbacksBase : public AsyncFileSystemCallbacks { +public: + virtual ~FileSystemCallbacksBase(); + + // For EntryCallbacks and VoidCallbacks. + virtual void didSucceed(); + + // For FileSystemCallbacks. + virtual void didOpenFileSystem(const String& name, PassOwnPtr<AsyncFileSystem>); + + // For MetadataCallbacks. + virtual void didReadMetadata(double modificationTime); + + // For EntriesCallbacks. didReadDirectoryEntry is called each time the API reads an entry, and didReadDirectoryDone is called when a chunk of entries have been read (i.e. good time to call back to the application). If hasMore is true there can be more chunks. + virtual void didReadDirectoryEntry(const String& name, bool isDirectory); + virtual void didReadDirectoryEntries(bool hasMore); + + // For ErrorCallback. + virtual void didFail(int code); + +protected: + FileSystemCallbacksBase(PassRefPtr<ErrorCallback> errorCallback); + RefPtr<ErrorCallback> m_errorCallback; +}; + +// Subclasses ---------------------------------------------------------------- + +class EntryCallbacks : public FileSystemCallbacksBase { +public: + EntryCallbacks(PassRefPtr<EntryCallback>, PassRefPtr<ErrorCallback>, DOMFileSystem*, const String& expectedPath, bool isDirectory); + virtual void didSucceed(); + +private: + RefPtr<EntryCallback> m_successCallback; + DOMFileSystem* m_fileSystem; + String m_expectedPath; + bool m_isDirectory; +}; + +class EntriesCallbacks : public FileSystemCallbacksBase { +public: + EntriesCallbacks(PassRefPtr<EntriesCallback>, PassRefPtr<ErrorCallback>, DOMFileSystem*, const String& basePath); + virtual void didReadDirectoryEntry(const String& name, bool isDirectory); + virtual void didReadDirectoryEntries(bool hasMore); + +private: + RefPtr<EntriesCallback> m_successCallback; + DOMFileSystem* m_fileSystem; + String m_basePath; + RefPtr<EntryArray> m_entries; +}; + +class FileSystemCallbacks : public FileSystemCallbacksBase { +public: + FileSystemCallbacks(PassRefPtr<FileSystemCallback>, PassRefPtr<ErrorCallback>, ScriptExecutionContext*); + virtual void didOpenFileSystem(const String& name, PassOwnPtr<AsyncFileSystem>); + +private: + RefPtr<FileSystemCallback> m_successCallback; + RefPtr<ScriptExecutionContext> m_scriptExecutionContext; +}; + +class MetadataCallbacks : public FileSystemCallbacksBase { +public: + MetadataCallbacks(PassRefPtr<MetadataCallback>, PassRefPtr<ErrorCallback>); + virtual void didReadMetadata(double modificationTime); + +private: + RefPtr<MetadataCallback> m_successCallback; +}; + +class VoidCallbacks : public FileSystemCallbacksBase { +public: + VoidCallbacks(PassRefPtr<VoidCallback>, PassRefPtr<ErrorCallback>); + virtual void didSucceed(); + +private: + RefPtr<VoidCallback> m_successCallback; +}; + +} // namespace + +#endif // ENABLE(FILE_SYSTEM) + +#endif // FileSystemCallbacks_h diff --git a/WebCore/fileapi/FileThread.cpp b/WebCore/fileapi/FileThread.cpp new file mode 100644 index 0000000..4e48cfb --- /dev/null +++ b/WebCore/fileapi/FileThread.cpp @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" + +#if ENABLE(BLOB) || ENABLE(FILE_WRITER) + +#include "FileThread.h" + +#include "AutodrainedPool.h" +#include "Logging.h" + +namespace WebCore { + +FileThread::FileThread() + : m_threadID(0) +{ + m_selfRef = this; +} + +FileThread::~FileThread() +{ + ASSERT(m_queue.killed()); +} + +bool FileThread::start() +{ + MutexLocker lock(m_threadCreationMutex); + if (m_threadID) + return true; + m_threadID = createThread(FileThread::fileThreadStart, this, "WebCore: File"); + return m_threadID; +} + +void FileThread::stop() +{ + m_queue.kill(); +} + +void FileThread::postTask(PassOwnPtr<Task> task) +{ + m_queue.append(task); +} + +class SameInstancePredicate { +public: + SameInstancePredicate(const void* instance) : m_instance(instance) { } + bool operator()(FileThread::Task* task) const { return task->instance() == m_instance; } +private: + const void* m_instance; +}; + +void FileThread::unscheduleTasks(const void* instance) +{ + SameInstancePredicate predicate(instance); + m_queue.removeIf(predicate); +} + +void* FileThread::fileThreadStart(void* arg) +{ + FileThread* fileThread = static_cast<FileThread*>(arg); + return fileThread->runLoop(); +} + +void* FileThread::runLoop() +{ + { + // Wait for FileThread::start() to complete to have m_threadID + // established before starting the main loop. + MutexLocker lock(m_threadCreationMutex); + LOG(FileAPI, "Started FileThread %p", this); + } + + AutodrainedPool pool; + while (OwnPtr<Task> task = m_queue.waitForMessage()) { + task->performTask(); + pool.cycle(); + } + + LOG(FileAPI, "About to detach thread %i and clear the ref to FileThread %p, which currently has %i ref(s)", m_threadID, this, refCount()); + + detachThread(m_threadID); + + // Clear the self refptr, possibly resulting in deletion + m_selfRef = 0; + + return 0; +} + +} // namespace WebCore + +#endif // ENABLE(BLOB) || ENABLE(FILE_WRITER) diff --git a/WebCore/fileapi/FileThread.h b/WebCore/fileapi/FileThread.h new file mode 100644 index 0000000..16acacc --- /dev/null +++ b/WebCore/fileapi/FileThread.h @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef FileThread_h +#define FileThread_h + +#if ENABLE(BLOB) || ENABLE(FILE_WRITER) + +#include <wtf/MessageQueue.h> +#include <wtf/PassOwnPtr.h> +#include <wtf/PassRefPtr.h> +#include <wtf/Threading.h> + +namespace WebCore { + +class FileStream; + +class FileThread : public ThreadSafeShared<FileThread> { +public: + static PassRefPtr<FileThread> create() + { + return adoptRef(new FileThread()); + } + + ~FileThread(); + + bool start(); + void stop(); + + class Task : public Noncopyable { + public: + virtual ~Task() { } + virtual void performTask() = 0; + void* instance() const { return m_instance; } + protected: + Task(void* instance) : m_instance(instance) { } + void* m_instance; + }; + + void postTask(PassOwnPtr<Task> task); + + void unscheduleTasks(const void* instance); + +private: + FileThread(); + + static void* fileThreadStart(void*); + void* runLoop(); + + ThreadIdentifier m_threadID; + RefPtr<FileThread> m_selfRef; + MessageQueue<Task> m_queue; + + Mutex m_threadCreationMutex; +}; + +} // namespace WebCore + +#endif // ENABLE(BLOB) || ENABLE(FILE_WRITER) + +#endif // FileThread_h diff --git a/WebCore/fileapi/FileThreadTask.h b/WebCore/fileapi/FileThreadTask.h new file mode 100644 index 0000000..8a8ffcb --- /dev/null +++ b/WebCore/fileapi/FileThreadTask.h @@ -0,0 +1,227 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef FileThreadTask_h +#define FileThreadTask_h + +#include "CrossThreadCopier.h" +#include "CrossThreadTask.h" +#include "FileThread.h" +#include <wtf/PassOwnPtr.h> +#include <wtf/PassRefPtr.h> + +namespace WebCore { + +template<typename T> +class FileThreadTask0 : public FileThread::Task { +public: + typedef void (T::*Method)(); + typedef FileThreadTask0<T> FileThreadTaskImpl; + + static PassOwnPtr<FileThreadTaskImpl> create(T* instance, Method method) + { + return adoptPtr(new FileThreadTaskImpl(instance, method)); + } + +private: + FileThreadTask0(T* instance, Method method) + : FileThread::Task(instance) + , m_method(method) + { + } + + virtual void performTask() + { + (*static_cast<T*>(instance()).*m_method)(); + } + +private: + Method m_method; +}; + +template<typename T, typename P1, typename MP1> +class FileThreadTask1 : public FileThread::Task { +public: + typedef void (T::*Method)(MP1); + typedef FileThreadTask1<T, P1, MP1> FileThreadTaskImpl; + typedef typename CrossThreadTaskTraits<P1>::ParamType Param1; + + static PassOwnPtr<FileThreadTaskImpl> create(T* instance, Method method, Param1 parameter1) + { + return adoptPtr(new FileThreadTaskImpl(instance, method, parameter1)); + } + +private: + FileThreadTask1(T* instance, Method method, Param1 parameter1) + : FileThread::Task(instance) + , m_method(method) + , m_parameter1(parameter1) + { + } + + virtual void performTask() + { + (*static_cast<T*>(instance()).*m_method)(m_parameter1); + } + +private: + Method m_method; + P1 m_parameter1; +}; + +template<typename T, typename P1, typename MP1, typename P2, typename MP2> +class FileThreadTask2 : public FileThread::Task { +public: + typedef void (T::*Method)(MP1, MP2); + typedef FileThreadTask2<T, P1, MP1, P2, MP2> FileThreadTaskImpl; + typedef typename CrossThreadTaskTraits<P1>::ParamType Param1; + typedef typename CrossThreadTaskTraits<P2>::ParamType Param2; + + static PassOwnPtr<FileThreadTaskImpl> create(T* instance, Method method, Param1 parameter1, Param2 parameter2) + { + return adoptPtr(new FileThreadTaskImpl(instance, method, parameter1, parameter2)); + } + +private: + FileThreadTask2(T* instance, Method method, Param1 parameter1, Param2 parameter2) + : FileThread::Task(instance) + , m_method(method) + , m_parameter1(parameter1) + , m_parameter2(parameter2) + { + } + + virtual void performTask() + { + (*static_cast<T*>(instance()).*m_method)(m_parameter1, m_parameter2); + } + +private: + Method m_method; + P1 m_parameter1; + P2 m_parameter2; +}; + +template<typename T, typename P1, typename MP1, typename P2, typename MP2, typename P3, typename MP3> +class FileThreadTask3 : public FileThread::Task { +public: + typedef void (T::*Method)(MP1, MP2, MP3); + typedef FileThreadTask3<T, P1, MP1, P2, MP2, P3, MP3> FileThreadTaskImpl; + typedef typename CrossThreadTaskTraits<P1>::ParamType Param1; + typedef typename CrossThreadTaskTraits<P2>::ParamType Param2; + typedef typename CrossThreadTaskTraits<P3>::ParamType Param3; + + static PassOwnPtr<FileThreadTaskImpl> create(T* instance, Method method, Param1 parameter1, Param2 parameter2, Param3 parameter3) + { + return adoptPtr(new FileThreadTaskImpl(instance, method, parameter1, parameter2, parameter3)); + } + +private: + FileThreadTask3(T* instance, Method method, Param1 parameter1, Param2 parameter2, Param3 parameter3) + : FileThread::Task(instance) + , m_method(method) + , m_parameter1(parameter1) + , m_parameter2(parameter2) + , m_parameter3(parameter3) + { + } + + virtual void performTask() + { + (*static_cast<T*>(instance()).*m_method)(m_parameter1, m_parameter2, m_parameter3); + } + +private: + Method m_method; + P1 m_parameter1; + P2 m_parameter2; + P3 m_parameter3; +}; + +template<typename T> +PassOwnPtr<FileThread::Task> createFileThreadTask( + T* const callee, + void (T::*method)()); + +template<typename T> +PassOwnPtr<FileThread::Task> createFileThreadTask( + T* const callee, + void (T::*method)()) +{ + return FileThreadTask0<T>::create( + callee, + method); +} + +template<typename T, typename P1, typename MP1> +PassOwnPtr<FileThread::Task> createFileThreadTask( + T* const callee, + void (T::*method)(MP1), + const P1& parameter1) +{ + return FileThreadTask1<T, typename CrossThreadCopier<P1>::Type, MP1>::create( + callee, + method, + CrossThreadCopier<P1>::copy(parameter1)); +} + +template<typename T, typename P1, typename MP1, typename P2, typename MP2> +PassOwnPtr<FileThread::Task> createFileThreadTask( + T* const callee, + void (T::*method)(MP1, MP2), + const P1& parameter1, + const P2& parameter2) +{ + return FileThreadTask2<T, typename CrossThreadCopier<P1>::Type, MP1, typename CrossThreadCopier<P2>::Type, MP2>::create( + callee, + method, + CrossThreadCopier<P1>::copy(parameter1), + CrossThreadCopier<P2>::copy(parameter2)); +} + +template<typename T, typename P1, typename MP1, typename P2, typename MP2, typename P3, typename MP3> +PassOwnPtr<FileThread::Task> createFileThreadTask( + T* const callee, + void (T::*method)(MP1, MP2, MP3), + const P1& parameter1, + const P2& parameter2, + const P3& parameter3) +{ + return FileThreadTask3<T, typename CrossThreadCopier<P1>::Type, MP1, typename CrossThreadCopier<P2>::Type, MP2, typename CrossThreadCopier<P3>::Type, MP3>::create( + callee, + method, + CrossThreadCopier<P1>::copy(parameter1), + CrossThreadCopier<P2>::copy(parameter2), + CrossThreadCopier<P3>::copy(parameter3)); +} + +} // namespace WebCore + +#endif // FileThreadTask_h diff --git a/WebCore/fileapi/FileWriter.cpp b/WebCore/fileapi/FileWriter.cpp new file mode 100644 index 0000000..7ead354 --- /dev/null +++ b/WebCore/fileapi/FileWriter.cpp @@ -0,0 +1,199 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" + +#if ENABLE(FILE_WRITER) + +#include "FileWriter.h" + +#include "AsyncFileWriter.h" +#include "Blob.h" +#include "ExceptionCode.h" +#include "FileError.h" +#include "ProgressEvent.h" + +namespace WebCore { + +FileWriter::FileWriter(ScriptExecutionContext* context) + : ActiveDOMObject(context, this) + , m_readyState(INIT) + , m_position(0) + , m_bytesWritten(0) + , m_bytesToWrite(0) +{ +} + +void FileWriter::initialize(PassOwnPtr<AsyncFileWriter> writer, long long length) +{ + ASSERT(!m_writer); + ASSERT(length >= 0); + m_writer = writer; + m_length = length; +} + +FileWriter::~FileWriter() +{ + if (m_readyState == WRITING) + stop(); +} + +bool FileWriter::hasPendingActivity() const +{ + return m_readyState == WRITING || ActiveDOMObject::hasPendingActivity(); +} + +bool FileWriter::canSuspend() const +{ + // FIXME: It is not currently possible to suspend a FileWriter, so pages with FileWriter can not go into page cache. + return false; +} + +void FileWriter::stop() +{ + if (m_writer && m_readyState == WRITING) + m_writer->abort(); + m_readyState = DONE; +} + +void FileWriter::write(Blob* data, ExceptionCode& ec) +{ + ASSERT(m_writer); + if (m_readyState == WRITING) { + ec = INVALID_STATE_ERR; + m_error = FileError::create(ec); + return; + } + + m_readyState = WRITING; + m_bytesWritten = 0; + m_bytesToWrite = data->size(); + fireEvent(eventNames().writestartEvent); + m_writer->write(m_position, data); +} + +void FileWriter::seek(long long position, ExceptionCode& ec) +{ + ASSERT(m_writer); + if (m_readyState == WRITING) { + ec = INVALID_STATE_ERR; + m_error = FileError::create(ec); + return; + } + + m_bytesWritten = 0; + m_bytesToWrite = 0; + if (position > m_length) + position = m_length; + else if (position < 0) + position = m_length + position; + if (position < 0) + position = 0; + m_position = position; +} + +void FileWriter::truncate(long long position, ExceptionCode& ec) +{ + ASSERT(m_writer); + if (m_readyState == WRITING || position >= m_length) { + ec = INVALID_STATE_ERR; + m_error = FileError::create(ec); + return; + } + m_readyState = WRITING; + m_bytesWritten = 0; + m_bytesToWrite = 0; + fireEvent(eventNames().writestartEvent); + m_writer->truncate(position); +} + +void FileWriter::abort(ExceptionCode& ec) +{ + ASSERT(m_writer); + if (m_readyState != WRITING) { + ec = INVALID_STATE_ERR; + m_error = FileError::create(ec); + return; + } + m_error = FileError::create(ABORT_ERR); + m_readyState = DONE; + fireEvent(eventNames().errorEvent); + fireEvent(eventNames().abortEvent); + fireEvent(eventNames().writeendEvent); + m_writer->abort(); +} + +void FileWriter::didWrite(long long bytes, bool complete) +{ + ASSERT(bytes > 0); + ASSERT(bytes + m_bytesWritten > 0); + ASSERT(bytes + m_bytesWritten <= m_bytesToWrite); + m_bytesWritten += bytes; + ASSERT((m_bytesWritten == m_bytesToWrite) == complete); + m_position += bytes; + if (m_position > m_length) + m_length = m_position; + if (complete) + m_readyState = DONE; + fireEvent(eventNames().writeEvent); + if (complete) + fireEvent(eventNames().writeendEvent); +} + +void FileWriter::didTruncate(long long length) +{ + ASSERT(length > 0); + ASSERT(length >= 0); + ASSERT(length < m_length); + m_length = length; + if (m_position > m_length) + m_position = m_length; + m_readyState = DONE; + fireEvent(eventNames().writeEvent); + fireEvent(eventNames().writeendEvent); +} + +void FileWriter::didFail(ExceptionCode ec) +{ + m_error = FileError::create(ec); + m_readyState = DONE; + fireEvent(eventNames().errorEvent); + fireEvent(eventNames().writeendEvent); +} + +void FileWriter::fireEvent(const AtomicString& type) +{ + // FIXME: the current ProgressEvent uses "unsigned long" for total and loaded attributes. Need to talk with the spec writer to resolve the issue. + dispatchEvent(ProgressEvent::create(type, true, static_cast<unsigned>(m_bytesWritten), static_cast<unsigned>(m_bytesToWrite))); +} + +} // namespace WebCore + +#endif // ENABLE(FILE_WRITER) diff --git a/WebCore/fileapi/FileWriter.h b/WebCore/fileapi/FileWriter.h new file mode 100644 index 0000000..b0af7ea --- /dev/null +++ b/WebCore/fileapi/FileWriter.h @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef FileWriter_h +#define FileWriter_h + +#if ENABLE(FILE_WRITER) + +#include "ActiveDOMObject.h" +#include "EventTarget.h" +#include "FileWriterClient.h" +#include <wtf/OwnPtr.h> +#include <wtf/PassOwnPtr.h> +#include <wtf/PassRefPtr.h> +#include <wtf/RefCounted.h> +#include <wtf/RefPtr.h> + +namespace WebCore { + +class AsyncFileWriter; +class Blob; +class FileError; +class ScriptExecutionContext; + +class FileWriter : public RefCounted<FileWriter>, public ActiveDOMObject, public EventTarget, public FileWriterClient { +public: + static PassRefPtr<FileWriter> create(ScriptExecutionContext* context) + { + return adoptRef(new FileWriter(context)); + } + + void initialize(PassOwnPtr<AsyncFileWriter> writer, long long length); + + enum ReadyState { + INIT = 0, + WRITING = 1, + DONE = 2 + }; + + void write(Blob* data, ExceptionCode& ec); + void seek(long long position, ExceptionCode& ec); + void truncate(long long length, ExceptionCode& ec); + void abort(ExceptionCode& ec); + + ReadyState readyState() const { return m_readyState; } + FileError* error() const { return m_error.get(); } + long long position() const { return m_position; } + long long length() const { return m_length; } + + // FileWriterClient + void didWrite(long long bytes, bool complete); + void didTruncate(long long length); + void didFail(ExceptionCode ec); + + // ActiveDOMObject + virtual bool canSuspend() const; + virtual bool hasPendingActivity() const; + virtual void stop(); + + // EventTarget + virtual FileWriter* toFileWriter() { return this; } + virtual ScriptExecutionContext* scriptExecutionContext() const { return ActiveDOMObject::scriptExecutionContext(); } + + using RefCounted<FileWriter>::ref; + using RefCounted<FileWriter>::deref; + + DEFINE_ATTRIBUTE_EVENT_LISTENER(writestart); + DEFINE_ATTRIBUTE_EVENT_LISTENER(progress); + DEFINE_ATTRIBUTE_EVENT_LISTENER(write); + DEFINE_ATTRIBUTE_EVENT_LISTENER(abort); + DEFINE_ATTRIBUTE_EVENT_LISTENER(error); + DEFINE_ATTRIBUTE_EVENT_LISTENER(writeend); + +private: + FileWriter(ScriptExecutionContext*); + + virtual ~FileWriter(); + + friend class RefCounted<FileWriter>; + + // EventTarget + virtual void refEventTarget() { ref(); } + virtual void derefEventTarget() { deref(); } + virtual EventTargetData* eventTargetData() { return &m_eventTargetData; } + virtual EventTargetData* ensureEventTargetData() { return &m_eventTargetData; } + + void fireEvent(const AtomicString& type); + + RefPtr<FileError> m_error; + EventTargetData m_eventTargetData; + OwnPtr<AsyncFileWriter> m_writer; + ReadyState m_readyState; + long long m_position; + long long m_length; + long long m_bytesWritten; + long long m_bytesToWrite; +}; + +} // namespace WebCore + +#endif // ENABLE(FILE_WRITER) + +#endif // FileWriter_h diff --git a/WebCore/fileapi/FileWriter.idl b/WebCore/fileapi/FileWriter.idl new file mode 100644 index 0000000..bb95ee1 --- /dev/null +++ b/WebCore/fileapi/FileWriter.idl @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +module html { + interface [ + Conditional=FILE_WRITER, + CallWith=ScriptExecutionContext, + EventTarget, + NoStaticTables + ] FileWriter { + // ready states + const unsigned short INIT = 0; + const unsigned short WRITING = 1; + const unsigned short DONE = 2; + readonly attribute unsigned short readyState; + + // async write/modify methods + void write(in Blob data) raises (FileException); + void seek(in long long position) raises (FileException); + void truncate(in long long size) raises (FileException); + + void abort() raises (FileException); + + readonly attribute FileError error; + readonly attribute long long position; + readonly attribute long long length; + + attribute EventListener onwritestart; + attribute EventListener onprogress; + attribute EventListener onwrite; + attribute EventListener onabort; + attribute EventListener onerror; + attribute EventListener onwriteend; + }; +} diff --git a/WebCore/fileapi/FileWriterClient.h b/WebCore/fileapi/FileWriterClient.h new file mode 100644 index 0000000..091b1c1 --- /dev/null +++ b/WebCore/fileapi/FileWriterClient.h @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef FileWriterClient_h +#define FileWriterClient_h + +#if ENABLE(FILE_WRITER) + +#include "ExceptionCode.h" + +namespace WebCore { + +class FileWriterClient { +public: + virtual ~FileWriterClient() {} + + virtual void didWrite(long long bytes, bool complete) = 0; + virtual void didTruncate(long long position) = 0; + virtual void didFail(ExceptionCode) = 0; +}; + +} // namespace + +#endif // ENABLE(FILE_WRITER) + +#endif // FileWriterClient_h diff --git a/WebCore/fileapi/Flags.h b/WebCore/fileapi/Flags.h new file mode 100644 index 0000000..30884fc --- /dev/null +++ b/WebCore/fileapi/Flags.h @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef Flags_h +#define Flags_h + +#if ENABLE(FILE_SYSTEM) + +#include <wtf/PassRefPtr.h> +#include <wtf/RefCounted.h> + +namespace WebCore { + +class Flags : public RefCounted<Flags> { +public: + static PassRefPtr<Flags> create(bool create = false, bool exclusive = false) + { + return adoptRef(new Flags(create, exclusive)); + } + + bool isCreate() const { return m_create; } + void setCreate(bool create) { m_create = create; } + bool isExclusive() const { return m_exclusive; } + void setExclusive(bool exclusive) { m_exclusive = exclusive; } + +private: + Flags(bool create, bool exclusive) + : m_create(create) + , m_exclusive(exclusive) + { + } + bool m_create; + bool m_exclusive; +}; + +} // namespace + +#endif // ENABLE(FILE_SYSTEM) + +#endif // Flags_h diff --git a/WebCore/fileapi/Flags.idl b/WebCore/fileapi/Flags.idl new file mode 100644 index 0000000..cfe73d2 --- /dev/null +++ b/WebCore/fileapi/Flags.idl @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +module storage { + interface [ + Conditional=FILE_SYSTEM + ] Flags { + attribute boolean CREATE; + attribute boolean EXCLUSIVE; + }; +} diff --git a/WebCore/fileapi/LocalFileSystem.cpp b/WebCore/fileapi/LocalFileSystem.cpp new file mode 100644 index 0000000..59ec2a1 --- /dev/null +++ b/WebCore/fileapi/LocalFileSystem.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "LocalFileSystem.h" + +#if PLATFORM(CHROMIUM) +#error "Chromium should not compile this file and instead define its own version of these factories." +#endif + +#if ENABLE(FILE_SYSTEM) + +#include "CrossThreadTask.h" +#include "DOMFileSystem.h" +#include "ErrorCallback.h" +#include "ExceptionCode.h" +#include "FileError.h" +#include "FileSystemCallback.h" +#include "FileSystemCallbacks.h" +#include "ScriptExecutionContext.h" +#include "SecurityOrigin.h" +#include <wtf/PassRefPtr.h> + +namespace WebCore { + +PassRefPtr<LocalFileSystem> LocalFileSystem::create(const String& basePath) +{ + return adoptRef(new LocalFileSystem(basePath)); +} + +static void openFileSystem(ScriptExecutionContext*, const String& basePath, const String& identifier, AsyncFileSystem::Type type, PassOwnPtr<FileSystemCallbacks> callbacks) +{ + AsyncFileSystem::openFileSystem(basePath, identifier, type, callbacks); +} + +void LocalFileSystem::requestFileSystem(ScriptExecutionContext* context, AsyncFileSystem::Type type, long long, PassRefPtr<FileSystemCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback) +{ + if (type != AsyncFileSystem::Temporary && type != AsyncFileSystem::Persistent) { + DOMFileSystem::scheduleCallback(context, errorCallback, FileError::create(INVALID_MODIFICATION_ERR)); + return; + } + + // AsyncFileSystem::openFileSystem calls callbacks synchronously, so the method needs to be called asynchronously. + context->postTask(createCallbackTask(&openFileSystem, m_basePath, context->securityOrigin()->databaseIdentifier(), type, new FileSystemCallbacks(successCallback, errorCallback, context))); +} + +} // namespace + +#endif // ENABLE(FILE_SYSTEM) diff --git a/WebCore/fileapi/LocalFileSystem.h b/WebCore/fileapi/LocalFileSystem.h new file mode 100644 index 0000000..48cfa8f --- /dev/null +++ b/WebCore/fileapi/LocalFileSystem.h @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef LocalFileSystem_h +#define LocalFileSystem_h + +#if ENABLE(FILE_SYSTEM) + +#include "AsyncFileSystem.h" +#include "PlatformString.h" +#include <wtf/PassRefPtr.h> +#include <wtf/RefCounted.h> + +namespace WebCore { + +class ErrorCallback; +class FileSystemCallback; +class ScriptExecutionContext; + +class LocalFileSystem : public RefCounted<LocalFileSystem> { +public: + static PassRefPtr<LocalFileSystem> create(const String& basePath); + virtual ~LocalFileSystem() { } + + void requestFileSystem(ScriptExecutionContext*, AsyncFileSystem::Type, long long size, PassRefPtr<FileSystemCallback>, PassRefPtr<ErrorCallback>); + +protected: + LocalFileSystem(const String& basePath) + : m_basePath(basePath) + { + } + + String m_basePath; +}; + +} // namespace + +#endif // ENABLE(FILE_SYSTEM) + +#endif // LocalFileSystem_h diff --git a/WebCore/fileapi/Metadata.h b/WebCore/fileapi/Metadata.h new file mode 100644 index 0000000..1fec943 --- /dev/null +++ b/WebCore/fileapi/Metadata.h @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef Metadata_h +#define Metadata_h + +#if ENABLE(FILE_SYSTEM) + +#include <wtf/RefCounted.h> + +namespace WebCore { + +class Metadata : public RefCounted<Metadata> { +public: + static PassRefPtr<Metadata> create(double modificationTime) + { + return adoptRef(new Metadata(modificationTime)); + } + + // Needs to return epoch time in milliseconds for Date. + double modificationTime() const { return m_modificationTime * 1000.0; } + +private: + Metadata(double modificationTime) + : m_modificationTime(modificationTime) + { + } + + double m_modificationTime; +}; + +} // namespace + +#endif // ENABLE(FILE_SYSTEM) + +#endif // Metadata_h diff --git a/WebCore/fileapi/Metadata.idl b/WebCore/fileapi/Metadata.idl new file mode 100644 index 0000000..a5a90bc --- /dev/null +++ b/WebCore/fileapi/Metadata.idl @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +module storage { + interface [ + Conditional=FILE_SYSTEM + ] Metadata { + readonly attribute Date modificationTime; + }; +} diff --git a/WebCore/fileapi/MetadataCallback.h b/WebCore/fileapi/MetadataCallback.h new file mode 100644 index 0000000..3d57400 --- /dev/null +++ b/WebCore/fileapi/MetadataCallback.h @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef MetadataCallback_h +#define MetadataCallback_h + +#if ENABLE(FILE_SYSTEM) + +#include <wtf/RefCounted.h> + +namespace WebCore { + +class Metadata; +class ScriptExecutionContext; + +class MetadataCallback : public RefCounted<MetadataCallback> { +public: + virtual ~MetadataCallback() { } + virtual bool handleEvent(Metadata*) = 0; +}; + +} // namespace + +#endif // ENABLE(FILE_SYSTEM) + +#endif // MetadataCallback_h diff --git a/WebCore/fileapi/MetadataCallback.idl b/WebCore/fileapi/MetadataCallback.idl new file mode 100644 index 0000000..44ca180 --- /dev/null +++ b/WebCore/fileapi/MetadataCallback.idl @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +module storage { + interface [ + Conditional=FILE_SYSTEM, + Callback + ] MetadataCallback { + boolean handleEvent(in Metadata metadata); + }; +} diff --git a/WebCore/fileapi/ThreadableBlobRegistry.cpp b/WebCore/fileapi/ThreadableBlobRegistry.cpp new file mode 100644 index 0000000..034557b --- /dev/null +++ b/WebCore/fileapi/ThreadableBlobRegistry.cpp @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" + +#include "ThreadableBlobRegistry.h" + +#include "BlobData.h" +#include "BlobRegistry.h" +#include "ScriptExecutionContext.h" +#include <wtf/MainThread.h> + +namespace WebCore { + +struct BlobRegistryContext { + BlobRegistryContext(const KURL& url, PassOwnPtr<BlobData> blobData) + : url(url.copy()) + , blobData(blobData) + { + } + + BlobRegistryContext(const KURL& url, const KURL& srcURL) + : url(url.copy()) + , srcURL(srcURL.copy()) + { + } + + BlobRegistryContext(const KURL& url) + : url(url.copy()) + { + } + + KURL url; + KURL srcURL; + OwnPtr<BlobData> blobData; +}; + +#if ENABLE(BLOB) + +static void registerBlobURLTask(void* context) +{ + OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context)); + blobRegistry().registerBlobURL(blobRegistryContext->url, blobRegistryContext->blobData.release()); +} + +void ThreadableBlobRegistry::registerBlobURL(ScriptExecutionContext*, const KURL& url, PassOwnPtr<BlobData> blobData) +{ + if (isMainThread()) + blobRegistry().registerBlobURL(url, blobData); + else { + OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url, blobData)); + callOnMainThread(®isterBlobURLTask, context.leakPtr()); + } +} + +static void registerBlobURLFromTask(void* context) +{ + OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context)); + blobRegistry().registerBlobURL(blobRegistryContext->url, blobRegistryContext->srcURL); +} + +void ThreadableBlobRegistry::registerBlobURL(ScriptExecutionContext*, const KURL& url, const KURL& srcURL) +{ + if (isMainThread()) + blobRegistry().registerBlobURL(url, srcURL); + else { + OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url, srcURL)); + callOnMainThread(®isterBlobURLFromTask, context.leakPtr()); + } +} + +static void unregisterBlobURLTask(void* context) +{ + OwnPtr<BlobRegistryContext> blobRegistryContext = adoptPtr(static_cast<BlobRegistryContext*>(context)); + blobRegistry().unregisterBlobURL(blobRegistryContext->url); +} + +void ThreadableBlobRegistry::unregisterBlobURL(ScriptExecutionContext*, const KURL& url) +{ + if (isMainThread()) + blobRegistry().unregisterBlobURL(url); + else { + OwnPtr<BlobRegistryContext> context = adoptPtr(new BlobRegistryContext(url)); + callOnMainThread(&unregisterBlobURLTask, context.leakPtr()); + } +} + +#else + +void ThreadableBlobRegistry::registerBlobURL(ScriptExecutionContext*, const KURL&, PassOwnPtr<BlobData>) +{ +} + +void ThreadableBlobRegistry::registerBlobURL(ScriptExecutionContext*, const KURL&, const KURL&) +{ +} + +void ThreadableBlobRegistry::unregisterBlobURL(ScriptExecutionContext*, const KURL&) +{ +} +#endif // ENABL(BLOB) + +} // namespace WebCore diff --git a/WebCore/fileapi/ThreadableBlobRegistry.h b/WebCore/fileapi/ThreadableBlobRegistry.h new file mode 100644 index 0000000..7dce6bb --- /dev/null +++ b/WebCore/fileapi/ThreadableBlobRegistry.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2010 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef ThreadableBlobRegistry_h +#define ThreadableBlobRegistry_h + +#include <wtf/PassOwnPtr.h> + +namespace WebCore { + +class BlobData; +class KURL; +class ScriptExecutionContext; + +class ThreadableBlobRegistry { +public: + static void registerBlobURL(ScriptExecutionContext*, const KURL&, PassOwnPtr<BlobData>); + static void registerBlobURL(ScriptExecutionContext*, const KURL&, const KURL& srcURL); + static void unregisterBlobURL(ScriptExecutionContext*, const KURL&); +}; + +} // namespace WebCore + +#endif // ThreadableBlobRegistry_h |