| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 const CString utf8BlobURL(Blob* blob) | 48 const CString utf8BlobURL(Blob* blob) |
| 49 { | 49 { |
| 50 return blob->url().string().utf8(); | 50 return blob->url().string().utf8(); |
| 51 } | 51 } |
| 52 | 52 |
| 53 const CString utf8FilePath(Blob* blob) | 53 const CString utf8FilePath(Blob* blob) |
| 54 { | 54 { |
| 55 return blob->isFile() ? toFile(blob)->path().utf8() : ""; | 55 return blob->isFile() ? toFile(blob)->path().utf8() : ""; |
| 56 } | 56 } |
| 57 | 57 |
| 58 const CString utf8BlobURL(Stream* stream) |
| 59 { |
| 60 return stream->url().string().utf8(); |
| 61 } |
| 62 |
| 58 } // namespace | 63 } // namespace |
| 59 | 64 |
| 60 static const double progressNotificationIntervalMS = 50; | 65 static const double progressNotificationIntervalMS = 50; |
| 61 | 66 |
| 62 PassRefPtr<FileReader> FileReader::create(ScriptExecutionContext* context) | 67 PassRefPtr<FileReader> FileReader::create(ScriptExecutionContext* context) |
| 63 { | 68 { |
| 64 RefPtr<FileReader> fileReader(adoptRef(new FileReader(context))); | 69 RefPtr<FileReader> fileReader(adoptRef(new FileReader(context))); |
| 65 fileReader->suspendIfNeeded(); | 70 fileReader->suspendIfNeeded(); |
| 66 return fileReader.release(); | 71 return fileReader.release(); |
| 67 } | 72 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 95 void FileReader::stop() | 100 void FileReader::stop() |
| 96 { | 101 { |
| 97 terminate(); | 102 terminate(); |
| 98 } | 103 } |
| 99 | 104 |
| 100 void FileReader::readAsArrayBuffer(Blob* blob, ExceptionCode& ec) | 105 void FileReader::readAsArrayBuffer(Blob* blob, ExceptionCode& ec) |
| 101 { | 106 { |
| 102 if (!blob) | 107 if (!blob) |
| 103 return; | 108 return; |
| 104 | 109 |
| 105 LOG(FileAPI, "FileReader: reading as array buffer: %s %s\n", utf8BlobURL(blo
b).data(), utf8FilePath(blob).data()); | 110 LOG(FileAPI, "FileReader: reading Blob as array buffer: %s %s\n", utf8BlobUR
L(blob).data(), utf8FilePath(blob).data()); |
| 106 | 111 |
| 107 readInternal(blob, FileReaderLoader::ReadAsArrayBuffer, ec); | 112 readInternal(blob, 0, FileReaderLoader::ReadAsArrayBuffer, ec); |
| 108 } | 113 } |
| 109 | 114 |
| 110 void FileReader::readAsBinaryString(Blob* blob, ExceptionCode& ec) | 115 void FileReader::readAsBinaryString(Blob* blob, ExceptionCode& ec) |
| 111 { | 116 { |
| 112 if (!blob) | 117 if (!blob) |
| 113 return; | 118 return; |
| 114 | 119 |
| 115 LOG(FileAPI, "FileReader: reading as binary: %s %s\n", utf8BlobURL(blob).dat
a(), utf8FilePath(blob).data()); | 120 LOG(FileAPI, "FileReader: reading Blob as binary: %s %s\n", utf8BlobURL(blob
).data(), utf8FilePath(blob).data()); |
| 116 | 121 |
| 117 readInternal(blob, FileReaderLoader::ReadAsBinaryString, ec); | 122 readInternal(blob, 0, FileReaderLoader::ReadAsBinaryString, ec); |
| 118 } | 123 } |
| 119 | 124 |
| 120 void FileReader::readAsText(Blob* blob, const String& encoding, ExceptionCode& e
c) | 125 void FileReader::readAsText(Blob* blob, const String& encoding, ExceptionCode& e
c) |
| 121 { | 126 { |
| 122 if (!blob) | 127 if (!blob) |
| 123 return; | 128 return; |
| 124 | 129 |
| 125 LOG(FileAPI, "FileReader: reading as text: %s %s\n", utf8BlobURL(blob).data(
), utf8FilePath(blob).data()); | 130 LOG(FileAPI, "FileReader: reading Blob as text: %s %s\n", utf8BlobURL(blob).
data(), utf8FilePath(blob).data()); |
| 126 | 131 |
| 127 m_encoding = encoding; | 132 m_encoding = encoding; |
| 128 readInternal(blob, FileReaderLoader::ReadAsText, ec); | 133 readInternal(blob, 0, FileReaderLoader::ReadAsText, ec); |
| 129 } | 134 } |
| 130 | 135 |
| 131 void FileReader::readAsText(Blob* blob, ExceptionCode& ec) | 136 void FileReader::readAsText(Blob* blob, ExceptionCode& ec) |
| 132 { | 137 { |
| 133 readAsText(blob, String(), ec); | 138 readAsText(blob, String(), ec); |
| 134 } | 139 } |
| 135 | 140 |
| 136 void FileReader::readAsDataURL(Blob* blob, ExceptionCode& ec) | 141 void FileReader::readAsDataURL(Blob* blob, ExceptionCode& ec) |
| 137 { | 142 { |
| 138 if (!blob) | 143 if (!blob) |
| 139 return; | 144 return; |
| 140 | 145 |
| 141 LOG(FileAPI, "FileReader: reading as data URL: %s %s\n", utf8BlobURL(blob).d
ata(), utf8FilePath(blob).data()); | 146 LOG(FileAPI, "FileReader: reading Blob as data URL: %s %s\n", utf8BlobURL(bl
ob).data(), utf8FilePath(blob).data()); |
| 142 | 147 |
| 143 readInternal(blob, FileReaderLoader::ReadAsDataURL, ec); | 148 readInternal(blob, 0, FileReaderLoader::ReadAsDataURL, ec); |
| 144 } | 149 } |
| 145 | 150 |
| 146 void FileReader::readInternal(Blob* blob, FileReaderLoader::ReadType type, Excep
tionCode& ec) | 151 void FileReader::readAsArrayBuffer(Stream* stream, ExceptionCode& ec) |
| 152 { |
| 153 if (!stream) |
| 154 return; |
| 155 |
| 156 LOG(FileAPI, "FileReader: reading Stream as array buffer: %s\n", utf8BlobURL
(stream).data()); |
| 157 |
| 158 readInternal(0, stream, FileReaderLoader::ReadAsArrayBuffer, ec); |
| 159 } |
| 160 |
| 161 void FileReader::readInternal(Blob* blob, Stream* stream, FileReaderLoader::Read
Type type, ExceptionCode& ec) |
| 147 { | 162 { |
| 148 // If multiple concurrent read methods are called on the same FileReader, In
validStateError should be thrown when the state is LOADING. | 163 // If multiple concurrent read methods are called on the same FileReader, In
validStateError should be thrown when the state is LOADING. |
| 149 if (m_state == LOADING) { | 164 if (m_state == LOADING) { |
| 150 ec = InvalidStateError; | 165 ec = InvalidStateError; |
| 151 return; | 166 return; |
| 152 } | 167 } |
| 153 | 168 |
| 154 setPendingActivity(this); | 169 setPendingActivity(this); |
| 155 | 170 |
| 156 m_blob = blob; | 171 m_blob = blob; |
| 172 m_stream = stream; |
| 157 m_readType = type; | 173 m_readType = type; |
| 158 m_state = LOADING; | 174 m_state = LOADING; |
| 159 m_error = 0; | 175 m_error = 0; |
| 160 | 176 |
| 161 m_loader = adoptPtr(new FileReaderLoader(m_readType, this)); | 177 m_loader = adoptPtr(new FileReaderLoader(m_readType, this)); |
| 162 m_loader->setEncoding(m_encoding); | 178 m_loader->setEncoding(m_encoding); |
| 163 m_loader->setDataType(m_blob->type()); | 179 if (m_blob) { |
| 164 m_loader->start(scriptExecutionContext(), *m_blob); | 180 m_loader->setDataType(m_blob->type()); |
| 181 m_loader->start(scriptExecutionContext(), m_blob.get()); |
| 182 } else if (m_stream) { |
| 183 if (m_stream->isNeutered()) { |
| 184 ec = InvalidStateError; |
| 185 return; |
| 186 } |
| 187 stream->neuter(); |
| 188 m_loader->setDataType(m_stream->type()); |
| 189 m_loader->start(scriptExecutionContext(), m_stream.get()); |
| 190 } |
| 165 } | 191 } |
| 166 | 192 |
| 167 static void delayedAbort(ScriptExecutionContext*, FileReader* reader) | 193 static void delayedAbort(ScriptExecutionContext*, FileReader* reader) |
| 168 { | 194 { |
| 169 reader->doAbort(); | 195 reader->doAbort(); |
| 170 } | 196 } |
| 171 | 197 |
| 172 void FileReader::abort() | 198 void FileReader::abort() |
| 173 { | 199 { |
| 174 LOG(FileAPI, "FileReader: aborting\n"); | 200 LOG(FileAPI, "FileReader: aborting\n"); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 | 236 |
| 211 void FileReader::didStartLoading() | 237 void FileReader::didStartLoading() |
| 212 { | 238 { |
| 213 fireEvent(eventNames().loadstartEvent); | 239 fireEvent(eventNames().loadstartEvent); |
| 214 } | 240 } |
| 215 | 241 |
| 216 void FileReader::didReceiveData() | 242 void FileReader::didReceiveData() |
| 217 { | 243 { |
| 218 // Fire the progress event at least every 50ms. | 244 // Fire the progress event at least every 50ms. |
| 219 double now = currentTimeMS(); | 245 double now = currentTimeMS(); |
| 246 |
| 247 LOG(FileAPI, "FileReader::didReceiveData %f", now); |
| 248 |
| 220 if (!m_lastProgressNotificationTimeMS) | 249 if (!m_lastProgressNotificationTimeMS) |
| 221 m_lastProgressNotificationTimeMS = now; | 250 m_lastProgressNotificationTimeMS = now; |
| 222 else if (now - m_lastProgressNotificationTimeMS > progressNotificationInterv
alMS) { | 251 else if (now - m_lastProgressNotificationTimeMS > progressNotificationInterv
alMS) { |
| 223 fireEvent(eventNames().progressEvent); | 252 fireEvent(eventNames().progressEvent); |
| 224 m_lastProgressNotificationTimeMS = now; | 253 m_lastProgressNotificationTimeMS = now; |
| 225 } | 254 } |
| 226 } | 255 } |
| 227 | 256 |
| 228 void FileReader::didFinishLoading() | 257 void FileReader::didFinishLoading() |
| 229 { | 258 { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 } | 300 } |
| 272 | 301 |
| 273 String FileReader::stringResult() | 302 String FileReader::stringResult() |
| 274 { | 303 { |
| 275 if (!m_loader || m_error) | 304 if (!m_loader || m_error) |
| 276 return String(); | 305 return String(); |
| 277 return m_loader->stringResult(); | 306 return m_loader->stringResult(); |
| 278 } | 307 } |
| 279 | 308 |
| 280 } // namespace WebCore | 309 } // namespace WebCore |
| OLD | NEW |