Home | History | Annotate | Download | only in filesystem
      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 
     33 #include "modules/filesystem/FileWriterSync.h"
     34 
     35 #include "bindings/v8/ExceptionState.h"
     36 #include "core/dom/ExceptionCode.h"
     37 #include "core/fileapi/Blob.h"
     38 #include "core/fileapi/FileError.h"
     39 #include "modules/filesystem/AsyncFileWriter.h"
     40 
     41 namespace WebCore {
     42 
     43 void FileWriterSync::write(Blob* data, ExceptionState& es)
     44 {
     45     ASSERT(writer());
     46     ASSERT(m_complete);
     47     if (!data) {
     48         es.throwDOMException(TypeMismatchError, FileError::typeMismatchErrorMessage);
     49         return;
     50     }
     51 
     52     prepareForWrite();
     53     writer()->write(position(), data);
     54     writer()->waitForOperationToComplete();
     55     ASSERT(m_complete);
     56     if (m_error) {
     57         FileError::throwDOMException(es, m_error);
     58         return;
     59     }
     60     setPosition(position() + data->size());
     61     if (position() > length())
     62         setLength(position());
     63 }
     64 
     65 void FileWriterSync::seek(long long position, ExceptionState& es)
     66 {
     67     ASSERT(writer());
     68     ASSERT(m_complete);
     69     seekInternal(position);
     70 }
     71 
     72 void FileWriterSync::truncate(long long offset, ExceptionState& es)
     73 {
     74     ASSERT(writer());
     75     ASSERT(m_complete);
     76     if (offset < 0) {
     77         es.throwDOMException(InvalidStateError, FileError::invalidStateErrorMessage);
     78         return;
     79     }
     80     prepareForWrite();
     81     writer()->truncate(offset);
     82     writer()->waitForOperationToComplete();
     83     ASSERT(m_complete);
     84     if (m_error) {
     85         FileError::throwDOMException(es, m_error);
     86         return;
     87     }
     88     if (offset < position())
     89         setPosition(offset);
     90     setLength(offset);
     91 }
     92 
     93 void FileWriterSync::didWrite(long long bytes, bool complete)
     94 {
     95     ASSERT(m_error == FileError::OK);
     96     ASSERT(!m_complete);
     97 #ifndef NDEBUG
     98     m_complete = complete;
     99 #else
    100     ASSERT_UNUSED(complete, complete);
    101 #endif
    102 }
    103 
    104 void FileWriterSync::didTruncate()
    105 {
    106     ASSERT(m_error == FileError::OK);
    107     ASSERT(!m_complete);
    108 #ifndef NDEBUG
    109     m_complete = true;
    110 #endif
    111 }
    112 
    113 void FileWriterSync::didFail(FileError::ErrorCode error)
    114 {
    115     ASSERT(m_error == FileError::OK);
    116     m_error = error;
    117     ASSERT(!m_complete);
    118 #ifndef NDEBUG
    119     m_complete = true;
    120 #endif
    121 }
    122 
    123 FileWriterSync::FileWriterSync()
    124     : m_error(FileError::OK)
    125 #ifndef NDEBUG
    126     , m_complete(true)
    127 #endif
    128 {
    129     ScriptWrappable::init(this);
    130 }
    131 
    132 void FileWriterSync::prepareForWrite()
    133 {
    134     ASSERT(m_complete);
    135     m_error = FileError::OK;
    136 #ifndef NDEBUG
    137     m_complete = false;
    138 #endif
    139 }
    140 
    141 FileWriterSync::~FileWriterSync()
    142 {
    143 }
    144 
    145 
    146 } // namespace WebCore
    147