Home | History | Annotate | Download | only in fileapi
      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 #ifndef FileReaderLoader_h
     32 #define FileReaderLoader_h
     33 
     34 #include "core/fileapi/FileError.h"
     35 #include "core/loader/ThreadableLoaderClient.h"
     36 #include "weborigin/KURL.h"
     37 #include "wtf/Forward.h"
     38 #include "wtf/text/TextEncoding.h"
     39 #include "wtf/text/WTFString.h"
     40 
     41 namespace WebCore {
     42 
     43 class Blob;
     44 class FileReaderLoaderClient;
     45 class ScriptExecutionContext;
     46 class Stream;
     47 class TextResourceDecoder;
     48 class ThreadableLoader;
     49 
     50 class FileReaderLoader : public ThreadableLoaderClient {
     51 public:
     52     enum ReadType {
     53         ReadAsArrayBuffer,
     54         ReadAsBinaryString,
     55         ReadAsBlob,
     56         ReadAsText,
     57         ReadAsDataURL
     58     };
     59 
     60     // If client is given, do the loading asynchronously. Otherwise, load synchronously.
     61     FileReaderLoader(ReadType, FileReaderLoaderClient*);
     62     ~FileReaderLoader();
     63 
     64     void start(ScriptExecutionContext*, const Blob&);
     65     void start(ScriptExecutionContext*, const Stream&);
     66     void cancel();
     67 
     68     // ThreadableLoaderClient
     69     virtual void didReceiveResponse(unsigned long, const ResourceResponse&);
     70     virtual void didReceiveData(const char*, int);
     71     virtual void didFinishLoading(unsigned long, double);
     72     virtual void didFail(const ResourceError&);
     73 
     74     String stringResult();
     75     PassRefPtr<ArrayBuffer> arrayBufferResult() const;
     76     unsigned bytesLoaded() const { return m_bytesLoaded; }
     77     unsigned totalBytes() const { return m_totalBytes; }
     78     FileError::ErrorCode errorCode() const { return m_errorCode; }
     79 
     80     void setEncoding(const String&);
     81     void setDataType(const String& dataType) { m_dataType = dataType; }
     82 
     83 private:
     84     // We have start() methods for Blob and Stream instead of exposing this
     85     // method so that users don't misuse this by calling with non Blob/Stream
     86     // URL.
     87     void startForURL(ScriptExecutionContext*, const KURL&);
     88     void terminate();
     89     void cleanup();
     90     void failed(FileError::ErrorCode);
     91     void convertToText();
     92     void convertToDataURL();
     93 
     94     bool isCompleted() const;
     95 
     96     static FileError::ErrorCode httpStatusCodeToErrorCode(int);
     97 
     98     ReadType m_readType;
     99     FileReaderLoaderClient* m_client;
    100     WTF::TextEncoding m_encoding;
    101     String m_dataType;
    102 
    103     KURL m_urlForReading;
    104     RefPtr<ThreadableLoader> m_loader;
    105 
    106     RefPtr<ArrayBuffer> m_rawData;
    107     bool m_isRawDataConverted;
    108 
    109     String m_stringResult;
    110     RefPtr<Blob> m_blobResult;
    111 
    112     // The decoder used to decode the text data.
    113     RefPtr<TextResourceDecoder> m_decoder;
    114 
    115     bool m_variableLength;
    116     unsigned m_bytesLoaded;
    117     unsigned m_totalBytes;
    118 
    119     bool m_hasRange;
    120     unsigned m_rangeStart;
    121     unsigned m_rangeEnd;
    122 
    123     FileError::ErrorCode m_errorCode;
    124 };
    125 
    126 } // namespace WebCore
    127 
    128 #endif // FileReaderLoader_h
    129