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 "platform/weborigin/KURL.h" 37 #include "wtf/ArrayBuffer.h" 38 #include "wtf/ArrayBufferBuilder.h" 39 #include "wtf/Forward.h" 40 #include "wtf/OwnPtr.h" 41 #include "wtf/text/TextEncoding.h" 42 #include "wtf/text/WTFString.h" 43 44 namespace blink { 45 46 class BlobDataHandle; 47 class FileReaderLoaderClient; 48 class ExecutionContext; 49 class Stream; 50 class TextResourceDecoder; 51 class ThreadableLoader; 52 53 class FileReaderLoader FINAL : public ThreadableLoaderClient { 54 public: 55 enum ReadType { 56 ReadAsArrayBuffer, 57 ReadAsBinaryString, 58 ReadAsBlob, 59 ReadAsText, 60 ReadAsDataURL, 61 ReadByClient 62 }; 63 64 // If client is given, do the loading asynchronously. Otherwise, load synchronously. 65 FileReaderLoader(ReadType, FileReaderLoaderClient*); 66 virtual ~FileReaderLoader(); 67 68 void start(ExecutionContext*, PassRefPtr<BlobDataHandle>); 69 void start(ExecutionContext*, const Stream&, unsigned readSize); 70 void cancel(); 71 72 // ThreadableLoaderClient 73 virtual void didReceiveResponse(unsigned long, const ResourceResponse&) OVERRIDE; 74 virtual void didReceiveData(const char*, int) OVERRIDE; 75 virtual void didFinishLoading(unsigned long, double) OVERRIDE; 76 virtual void didFail(const ResourceError&) OVERRIDE; 77 78 String stringResult(); 79 PassRefPtr<ArrayBuffer> arrayBufferResult() const; 80 81 // Returns the total bytes received. Bytes ignored by m_rawData won't be 82 // counted. 83 // 84 // This value doesn't grow more than numeric_limits<unsigned> when 85 // m_readType is not set to ReadByClient. bytesLoaded()86 long long bytesLoaded() const { return m_bytesLoaded; } 87 88 // Before didReceiveResponse() is called: Returns -1. 89 // After didReceiveResponse() is called: 90 // - If the size of the resource is known (from 91 // m_response.expectedContentLength() or once didFinishLoading() is 92 // called), returns it. 93 // - Otherwise, returns -1. totalBytes()94 long long totalBytes() const { return m_totalBytes; } 95 errorCode()96 FileError::ErrorCode errorCode() const { return m_errorCode; } 97 98 void setEncoding(const String&); setDataType(const String & dataType)99 void setDataType(const String& dataType) { m_dataType = dataType; } 100 101 private: 102 void startInternal(ExecutionContext&, const Stream*, PassRefPtr<BlobDataHandle>); 103 void terminate(); 104 void cleanup(); 105 106 void failed(FileError::ErrorCode); 107 void convertToText(); 108 void convertToDataURL(); 109 110 static FileError::ErrorCode httpStatusCodeToErrorCode(int); 111 112 ReadType m_readType; 113 FileReaderLoaderClient* m_client; 114 WTF::TextEncoding m_encoding; 115 String m_dataType; 116 117 KURL m_urlForReading; 118 bool m_urlForReadingIsStream; 119 RefPtr<ThreadableLoader> m_loader; 120 121 OwnPtr<ArrayBufferBuilder> m_rawData; 122 bool m_isRawDataConverted; 123 124 String m_stringResult; 125 126 // The decoder used to decode the text data. 127 OwnPtr<TextResourceDecoder> m_decoder; 128 129 bool m_finishedLoading; 130 long long m_bytesLoaded; 131 // If the total size of the resource is unknown, m_totalBytes is set to -1 132 // until completion of loading, and the buffer for receiving data is set to 133 // dynamically grow. Otherwise, m_totalBytes is set to the total size and 134 // the buffer for receiving data of m_totalBytes is allocated and never grow 135 // even when extra data is appeneded. 136 long long m_totalBytes; 137 138 bool m_hasRange; 139 unsigned m_rangeStart; 140 unsigned m_rangeEnd; 141 142 FileError::ErrorCode m_errorCode; 143 }; 144 145 } // namespace blink 146 147 #endif // FileReaderLoader_h 148