WebCore/platform/BlobItem.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2010 Google Inc. All rights reserved.
       
     3  *
       
     4  * Redistribution and use in source and binary forms, with or without
       
     5  * modification, are permitted provided that the following conditions are
       
     6  * met:
       
     7  *
       
     8  *     * Redistributions of source code must retain the above copyright
       
     9  * notice, this list of conditions and the following disclaimer.
       
    10  *     * Redistributions in binary form must reproduce the above
       
    11  * copyright notice, this list of conditions and the following disclaimer
       
    12  * in the documentation and/or other materials provided with the
       
    13  * distribution.
       
    14  *     * Neither the name of Google Inc. nor the names of its
       
    15  * contributors may be used to endorse or promote products derived from
       
    16  * this software without specific prior written permission.
       
    17  *
       
    18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
    19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
    20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
    21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       
    22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       
    24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       
    26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    29  */
       
    30 
       
    31 #include "config.h"
       
    32 #include "BlobItem.h"
       
    33 
       
    34 #include "FileSystem.h"
       
    35 #include "UUID.h"
       
    36 #include <wtf/Assertions.h>
       
    37 
       
    38 namespace WebCore {
       
    39 
       
    40 #if ENABLE(BLOB_SLICE)
       
    41 static const double invalidModificationTime = 0;
       
    42 
       
    43 static double getFileSnapshotModificationTime(const String& path)
       
    44 {
       
    45     // FIXME: synchronized file call
       
    46     time_t modificationTime;
       
    47     if (getFileModificationTime(path, modificationTime))
       
    48         return static_cast<double>(modificationTime);
       
    49     return invalidModificationTime;
       
    50 }
       
    51 #endif // ENABLE(BLOB_SLICE)
       
    52 
       
    53 // DataBlobItem ----------------------------------------------------------------
       
    54 
       
    55 #if ENABLE(BLOB_SLICE)
       
    56 PassRefPtr<BlobItem> DataBlobItem::slice(long long start, long long length)
       
    57 {
       
    58     ASSERT(start >= 0 && length >= 0);
       
    59     ASSERT(static_cast<unsigned long long>(start) < size());
       
    60     if (!start && size() <= static_cast<unsigned long long>(length))
       
    61         return this;
       
    62     if (static_cast<unsigned long long>(start + length) > size())
       
    63         length = size() - start;
       
    64     return DataRangeBlobItem::create(this, start, length);
       
    65 }
       
    66 #endif // ENABLE(BLOB_SLICE)
       
    67 
       
    68 // FileBlobItem ----------------------------------------------------------------
       
    69 
       
    70 PassRefPtr<BlobItem> FileBlobItem::create(const String& path)
       
    71 {
       
    72     return adoptRef(static_cast<BlobItem*>(new FileBlobItem(path)));
       
    73 }
       
    74 
       
    75 FileBlobItem::FileBlobItem(const String& path)
       
    76     : m_path(path)
       
    77     , m_fileName(pathGetFileName(m_path))
       
    78 {
       
    79 }
       
    80 
       
    81 #if ENABLE(DIRECTORY_UPLOAD)
       
    82 PassRefPtr<BlobItem> FileBlobItem::create(const String& path, const String& relativePath)
       
    83 {
       
    84     return adoptRef(static_cast<BlobItem*>(new FileBlobItem(path, relativePath)));
       
    85 }
       
    86 
       
    87 FileBlobItem::FileBlobItem(const String& path, const String& relativePath)
       
    88     : m_path(path)
       
    89     , m_fileName(pathGetFileName(m_path))
       
    90     , m_relativePath(relativePath)
       
    91 {
       
    92 }
       
    93 #endif
       
    94 
       
    95 unsigned long long FileBlobItem::size() const
       
    96 {
       
    97     // FIXME: synchronized file call
       
    98     long long size;
       
    99     if (!getFileSize(m_path, size))
       
   100         return 0;
       
   101     return static_cast<unsigned long long>(size);
       
   102 }
       
   103 
       
   104 #if ENABLE(BLOB_SLICE)
       
   105 PassRefPtr<BlobItem> FileBlobItem::slice(long long start, long long length)
       
   106 {
       
   107     ASSERT(start >= 0 && length >= 0);
       
   108     long long fileSize = size();
       
   109     ASSERT(start < fileSize);
       
   110     if (!start && fileSize <= length)
       
   111         return this;
       
   112     if (start + length > fileSize)
       
   113         length = fileSize - start;
       
   114     const FileRangeBlobItem* fileRangeItem = toFileRangeBlobItem();
       
   115     double modificationTime = fileRangeItem ? fileRangeItem->snapshotModificationTime() : getFileSnapshotModificationTime(path());
       
   116     return FileRangeBlobItem::create(path(), start, length, modificationTime);
       
   117 }
       
   118 #endif // ENABLE(BLOB_SLICE)
       
   119 
       
   120 // StringBlobItem --------------------------------------------------------------
       
   121 
       
   122 PassRefPtr<BlobItem> StringBlobItem::create(const CString& text)
       
   123 {
       
   124     return adoptRef(static_cast<BlobItem*>(new StringBlobItem(text)));
       
   125 }
       
   126 
       
   127 StringBlobItem::StringBlobItem(const CString& text)
       
   128     : m_data(text)
       
   129 {
       
   130 }
       
   131 
       
   132 // ByteArrayBlobItem ----------------------------------------------------------
       
   133 
       
   134 PassRefPtr<BlobItem> ByteArrayBlobItem::create(const char* data, size_t size)
       
   135 {
       
   136     return adoptRef(static_cast<BlobItem*>(new ByteArrayBlobItem(data, size)));
       
   137 }
       
   138 
       
   139 ByteArrayBlobItem::ByteArrayBlobItem(const char* data, size_t size)
       
   140 {
       
   141     m_bytesArray.append(data, size);
       
   142 }
       
   143 
       
   144 #if ENABLE(BLOB_SLICE)
       
   145 
       
   146 // DataRangeBlobItem -----------------------------------------------------------
       
   147 
       
   148 PassRefPtr<BlobItem> DataRangeBlobItem::create(PassRefPtr<DataBlobItem> item, long long start, long long length)
       
   149 {
       
   150     return adoptRef(static_cast<BlobItem*>(new DataRangeBlobItem(item, start, length)));
       
   151 }
       
   152 
       
   153 DataRangeBlobItem::DataRangeBlobItem(PassRefPtr<DataBlobItem> item, long long start, long long length)
       
   154     : m_length(length)
       
   155 {
       
   156     const DataRangeBlobItem* rangeItem = item->toDataRangeBlobItem();
       
   157     if (rangeItem) {
       
   158         m_item = rangeItem->m_item;
       
   159         m_start = start + rangeItem->m_start;
       
   160         ASSERT(!m_item->toDataRangeBlobItem());
       
   161         ASSERT(static_cast<unsigned long long>(m_start + m_length) <= m_item->size());
       
   162     } else {
       
   163         m_item = item;
       
   164         m_start = start;
       
   165     }
       
   166 }
       
   167 
       
   168 const char* DataRangeBlobItem::data() const
       
   169 {
       
   170     return m_item->data() + m_start;
       
   171 }
       
   172 
       
   173 // FileRangeBlobItem -----------------------------------------------------------
       
   174 
       
   175 PassRefPtr<BlobItem> FileRangeBlobItem::create(const String& path, long long start, long long length, double snapshotModificationTime)
       
   176 {
       
   177     return adoptRef(static_cast<BlobItem*>(new FileRangeBlobItem(path, start, length, snapshotModificationTime)));
       
   178 }
       
   179 
       
   180 FileRangeBlobItem::FileRangeBlobItem(const String& path, long long start, long long length, double modificationTime)
       
   181     : FileBlobItem(path)
       
   182     , m_start(start)
       
   183     , m_length(length)
       
   184     , m_snapshotModificationTime(modificationTime)
       
   185 {
       
   186     m_uniqueName = "Blob" + createCanonicalUUIDString();
       
   187     m_uniqueName.replace("-", ""); // For safty, remove '-' from the filename snce some servers may not like it.
       
   188 }
       
   189 
       
   190 #endif // ENABLE(BLOB_SLICE)
       
   191 
       
   192 } // namespace WebCore