| 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 FileWriter* FileWriter::create(ExecutionContext* context) | 46 FileWriter* FileWriter::create(ExecutionContext* context) |
| 47 { | 47 { |
| 48 FileWriter* fileWriter = new FileWriter(context); | 48 FileWriter* fileWriter = new FileWriter(context); |
| 49 fileWriter->suspendIfNeeded(); | 49 fileWriter->suspendIfNeeded(); |
| 50 return fileWriter; | 50 return fileWriter; |
| 51 } | 51 } |
| 52 | 52 |
| 53 FileWriter::FileWriter(ExecutionContext* context) | 53 FileWriter::FileWriter(ExecutionContext* context) |
| 54 : ActiveScriptWrappable(this) | 54 : ActiveScriptWrappable(this) |
| 55 , ActiveDOMObject(context) | 55 , ActiveDOMObject(context) |
| 56 , m_readyState(INIT) | 56 , m_readyState(kInit) |
| 57 , m_operationInProgress(OperationNone) | 57 , m_operationInProgress(OperationNone) |
| 58 , m_queuedOperation(OperationNone) | 58 , m_queuedOperation(OperationNone) |
| 59 , m_bytesWritten(0) | 59 , m_bytesWritten(0) |
| 60 , m_bytesToWrite(0) | 60 , m_bytesToWrite(0) |
| 61 , m_truncateLength(-1) | 61 , m_truncateLength(-1) |
| 62 , m_numAborts(0) | 62 , m_numAborts(0) |
| 63 , m_recursionDepth(0) | 63 , m_recursionDepth(0) |
| 64 , m_lastProgressNotificationTimeMS(0) | 64 , m_lastProgressNotificationTimeMS(0) |
| 65 { | 65 { |
| 66 } | 66 } |
| 67 | 67 |
| 68 FileWriter::~FileWriter() | 68 FileWriter::~FileWriter() |
| 69 { | 69 { |
| 70 ASSERT(!m_recursionDepth); | 70 ASSERT(!m_recursionDepth); |
| 71 if (m_readyState == WRITING) | 71 if (m_readyState == kWriting) |
| 72 stop(); | 72 stop(); |
| 73 } | 73 } |
| 74 | 74 |
| 75 const AtomicString& FileWriter::interfaceName() const | 75 const AtomicString& FileWriter::interfaceName() const |
| 76 { | 76 { |
| 77 return EventTargetNames::FileWriter; | 77 return EventTargetNames::FileWriter; |
| 78 } | 78 } |
| 79 | 79 |
| 80 void FileWriter::stop() | 80 void FileWriter::stop() |
| 81 { | 81 { |
| 82 // Make sure we've actually got something to stop, and haven't already calle
d abort(). | 82 // Make sure we've actually got something to stop, and haven't already calle
d abort(). |
| 83 if (!writer() || m_readyState != WRITING) | 83 if (!writer() || m_readyState != kWriting) |
| 84 return; | 84 return; |
| 85 doOperation(OperationAbort); | 85 doOperation(OperationAbort); |
| 86 m_readyState = DONE; | 86 m_readyState = kDone; |
| 87 } | 87 } |
| 88 | 88 |
| 89 bool FileWriter::hasPendingActivity() const | 89 bool FileWriter::hasPendingActivity() const |
| 90 { | 90 { |
| 91 return m_operationInProgress != OperationNone || m_queuedOperation != Operat
ionNone || m_readyState == WRITING; | 91 return m_operationInProgress != OperationNone || m_queuedOperation != Operat
ionNone || m_readyState == kWriting; |
| 92 } | 92 } |
| 93 | 93 |
| 94 void FileWriter::write(Blob* data, ExceptionState& exceptionState) | 94 void FileWriter::write(Blob* data, ExceptionState& exceptionState) |
| 95 { | 95 { |
| 96 ASSERT(data); | 96 ASSERT(data); |
| 97 ASSERT(writer()); | 97 ASSERT(writer()); |
| 98 ASSERT(m_truncateLength == -1); | 98 ASSERT(m_truncateLength == -1); |
| 99 if (m_readyState == WRITING) { | 99 if (m_readyState == kWriting) { |
| 100 setError(FileError::INVALID_STATE_ERR, exceptionState); | 100 setError(FileError::kInvalidStateErr, exceptionState); |
| 101 return; | 101 return; |
| 102 } | 102 } |
| 103 if (m_recursionDepth > kMaxRecursionDepth) { | 103 if (m_recursionDepth > kMaxRecursionDepth) { |
| 104 setError(FileError::SECURITY_ERR, exceptionState); | 104 setError(FileError::kSecurityErr, exceptionState); |
| 105 return; | 105 return; |
| 106 } | 106 } |
| 107 | 107 |
| 108 m_blobBeingWritten = data; | 108 m_blobBeingWritten = data; |
| 109 m_readyState = WRITING; | 109 m_readyState = kWriting; |
| 110 m_bytesWritten = 0; | 110 m_bytesWritten = 0; |
| 111 m_bytesToWrite = data->size(); | 111 m_bytesToWrite = data->size(); |
| 112 ASSERT(m_queuedOperation == OperationNone); | 112 ASSERT(m_queuedOperation == OperationNone); |
| 113 if (m_operationInProgress != OperationNone) { | 113 if (m_operationInProgress != OperationNone) { |
| 114 // We must be waiting for an abort to complete, since m_readyState wasn'
t WRITING. | 114 // We must be waiting for an abort to complete, since m_readyState wasn'
t kWriting. |
| 115 ASSERT(m_operationInProgress == OperationAbort); | 115 ASSERT(m_operationInProgress == OperationAbort); |
| 116 m_queuedOperation = OperationWrite; | 116 m_queuedOperation = OperationWrite; |
| 117 } else | 117 } else |
| 118 doOperation(OperationWrite); | 118 doOperation(OperationWrite); |
| 119 | 119 |
| 120 fireEvent(EventTypeNames::writestart); | 120 fireEvent(EventTypeNames::writestart); |
| 121 } | 121 } |
| 122 | 122 |
| 123 void FileWriter::seek(long long position, ExceptionState& exceptionState) | 123 void FileWriter::seek(long long position, ExceptionState& exceptionState) |
| 124 { | 124 { |
| 125 ASSERT(writer()); | 125 ASSERT(writer()); |
| 126 if (m_readyState == WRITING) { | 126 if (m_readyState == kWriting) { |
| 127 setError(FileError::INVALID_STATE_ERR, exceptionState); | 127 setError(FileError::kInvalidStateErr, exceptionState); |
| 128 return; | 128 return; |
| 129 } | 129 } |
| 130 | 130 |
| 131 ASSERT(m_truncateLength == -1); | 131 ASSERT(m_truncateLength == -1); |
| 132 ASSERT(m_queuedOperation == OperationNone); | 132 ASSERT(m_queuedOperation == OperationNone); |
| 133 seekInternal(position); | 133 seekInternal(position); |
| 134 } | 134 } |
| 135 | 135 |
| 136 void FileWriter::truncate(long long position, ExceptionState& exceptionState) | 136 void FileWriter::truncate(long long position, ExceptionState& exceptionState) |
| 137 { | 137 { |
| 138 ASSERT(writer()); | 138 ASSERT(writer()); |
| 139 ASSERT(m_truncateLength == -1); | 139 ASSERT(m_truncateLength == -1); |
| 140 if (m_readyState == WRITING || position < 0) { | 140 if (m_readyState == kWriting || position < 0) { |
| 141 setError(FileError::INVALID_STATE_ERR, exceptionState); | 141 setError(FileError::kInvalidStateErr, exceptionState); |
| 142 return; | 142 return; |
| 143 } | 143 } |
| 144 if (m_recursionDepth > kMaxRecursionDepth) { | 144 if (m_recursionDepth > kMaxRecursionDepth) { |
| 145 setError(FileError::SECURITY_ERR, exceptionState); | 145 setError(FileError::kSecurityErr, exceptionState); |
| 146 return; | 146 return; |
| 147 } | 147 } |
| 148 | 148 |
| 149 m_readyState = WRITING; | 149 m_readyState = kWriting; |
| 150 m_bytesWritten = 0; | 150 m_bytesWritten = 0; |
| 151 m_bytesToWrite = 0; | 151 m_bytesToWrite = 0; |
| 152 m_truncateLength = position; | 152 m_truncateLength = position; |
| 153 ASSERT(m_queuedOperation == OperationNone); | 153 ASSERT(m_queuedOperation == OperationNone); |
| 154 if (m_operationInProgress != OperationNone) { | 154 if (m_operationInProgress != OperationNone) { |
| 155 // We must be waiting for an abort to complete, since m_readyState wasn'
t WRITING. | 155 // We must be waiting for an abort to complete, since m_readyState wasn'
t kWriting. |
| 156 ASSERT(m_operationInProgress == OperationAbort); | 156 ASSERT(m_operationInProgress == OperationAbort); |
| 157 m_queuedOperation = OperationTruncate; | 157 m_queuedOperation = OperationTruncate; |
| 158 } else | 158 } else |
| 159 doOperation(OperationTruncate); | 159 doOperation(OperationTruncate); |
| 160 fireEvent(EventTypeNames::writestart); | 160 fireEvent(EventTypeNames::writestart); |
| 161 } | 161 } |
| 162 | 162 |
| 163 void FileWriter::abort(ExceptionState& exceptionState) | 163 void FileWriter::abort(ExceptionState& exceptionState) |
| 164 { | 164 { |
| 165 ASSERT(writer()); | 165 ASSERT(writer()); |
| 166 if (m_readyState != WRITING) | 166 if (m_readyState != kWriting) |
| 167 return; | 167 return; |
| 168 ++m_numAborts; | 168 ++m_numAborts; |
| 169 | 169 |
| 170 doOperation(OperationAbort); | 170 doOperation(OperationAbort); |
| 171 signalCompletion(FileError::ABORT_ERR); | 171 signalCompletion(FileError::kAbortErr); |
| 172 } | 172 } |
| 173 | 173 |
| 174 void FileWriter::didWrite(long long bytes, bool complete) | 174 void FileWriter::didWrite(long long bytes, bool complete) |
| 175 { | 175 { |
| 176 if (m_operationInProgress == OperationAbort) { | 176 if (m_operationInProgress == OperationAbort) { |
| 177 completeAbort(); | 177 completeAbort(); |
| 178 return; | 178 return; |
| 179 } | 179 } |
| 180 ASSERT(m_readyState == WRITING); | 180 DCHECK_EQ(kWriting, m_readyState); |
| 181 ASSERT(m_truncateLength == -1); | 181 DCHECK_EQ(-1, m_truncateLength); |
| 182 ASSERT(m_operationInProgress == OperationWrite); | 182 DCHECK_EQ(OperationWrite, m_operationInProgress); |
| 183 ASSERT(!m_bytesToWrite || bytes + m_bytesWritten > 0); | 183 DCHECK(!m_bytesToWrite || bytes + m_bytesWritten > 0); |
| 184 ASSERT(bytes + m_bytesWritten <= m_bytesToWrite); | 184 DCHECK(bytes + m_bytesWritten <= m_bytesToWrite); |
| 185 m_bytesWritten += bytes; | 185 m_bytesWritten += bytes; |
| 186 ASSERT((m_bytesWritten == m_bytesToWrite) || !complete); | 186 DCHECK((m_bytesWritten == m_bytesToWrite) || !complete); |
| 187 setPosition(position() + bytes); | 187 setPosition(position() + bytes); |
| 188 if (position() > length()) | 188 if (position() > length()) |
| 189 setLength(position()); | 189 setLength(position()); |
| 190 if (complete) { | 190 if (complete) { |
| 191 m_blobBeingWritten.clear(); | 191 m_blobBeingWritten.clear(); |
| 192 m_operationInProgress = OperationNone; | 192 m_operationInProgress = OperationNone; |
| 193 } | 193 } |
| 194 | 194 |
| 195 int numAborts = m_numAborts; | 195 int numAborts = m_numAborts; |
| 196 // We could get an abort in the handler for this event. If we do, it's | 196 // We could get an abort in the handler for this event. If we do, it's |
| 197 // already handled the cleanup and signalCompletion call. | 197 // already handled the cleanup and signalCompletion call. |
| 198 double now = currentTimeMS(); | 198 double now = currentTimeMS(); |
| 199 if (complete || !m_lastProgressNotificationTimeMS || (now - m_lastProgressNo
tificationTimeMS > progressNotificationIntervalMS)) { | 199 if (complete || !m_lastProgressNotificationTimeMS || (now - m_lastProgressNo
tificationTimeMS > progressNotificationIntervalMS)) { |
| 200 m_lastProgressNotificationTimeMS = now; | 200 m_lastProgressNotificationTimeMS = now; |
| 201 fireEvent(EventTypeNames::progress); | 201 fireEvent(EventTypeNames::progress); |
| 202 } | 202 } |
| 203 | 203 |
| 204 if (complete) { | 204 if (complete) { |
| 205 if (numAborts == m_numAborts) | 205 if (numAborts == m_numAborts) |
| 206 signalCompletion(FileError::OK); | 206 signalCompletion(FileError::kOK); |
| 207 } | 207 } |
| 208 } | 208 } |
| 209 | 209 |
| 210 void FileWriter::didTruncate() | 210 void FileWriter::didTruncate() |
| 211 { | 211 { |
| 212 if (m_operationInProgress == OperationAbort) { | 212 if (m_operationInProgress == OperationAbort) { |
| 213 completeAbort(); | 213 completeAbort(); |
| 214 return; | 214 return; |
| 215 } | 215 } |
| 216 ASSERT(m_operationInProgress == OperationTruncate); | 216 ASSERT(m_operationInProgress == OperationTruncate); |
| 217 ASSERT(m_truncateLength >= 0); | 217 ASSERT(m_truncateLength >= 0); |
| 218 setLength(m_truncateLength); | 218 setLength(m_truncateLength); |
| 219 if (position() > length()) | 219 if (position() > length()) |
| 220 setPosition(length()); | 220 setPosition(length()); |
| 221 m_operationInProgress = OperationNone; | 221 m_operationInProgress = OperationNone; |
| 222 signalCompletion(FileError::OK); | 222 signalCompletion(FileError::kOK); |
| 223 } | 223 } |
| 224 | 224 |
| 225 void FileWriter::didFail(WebFileError code) | 225 void FileWriter::didFail(WebFileError code) |
| 226 { | 226 { |
| 227 ASSERT(m_operationInProgress != OperationNone); | 227 DCHECK_NE(OperationNone, m_operationInProgress); |
| 228 ASSERT(static_cast<FileError::ErrorCode>(code) != FileError::OK); | 228 DCHECK_NE(FileError::kOK, static_cast<FileError::ErrorCode>(code)); |
| 229 if (m_operationInProgress == OperationAbort) { | 229 if (m_operationInProgress == OperationAbort) { |
| 230 completeAbort(); | 230 completeAbort(); |
| 231 return; | 231 return; |
| 232 } | 232 } |
| 233 ASSERT(m_queuedOperation == OperationNone); | 233 DCHECK_EQ(OperationNone, m_queuedOperation); |
| 234 ASSERT(m_readyState == WRITING); | 234 DCHECK_EQ(kWriting, m_readyState); |
| 235 m_blobBeingWritten.clear(); | 235 m_blobBeingWritten.clear(); |
| 236 m_operationInProgress = OperationNone; | 236 m_operationInProgress = OperationNone; |
| 237 signalCompletion(static_cast<FileError::ErrorCode>(code)); | 237 signalCompletion(static_cast<FileError::ErrorCode>(code)); |
| 238 } | 238 } |
| 239 | 239 |
| 240 void FileWriter::completeAbort() | 240 void FileWriter::completeAbort() |
| 241 { | 241 { |
| 242 ASSERT(m_operationInProgress == OperationAbort); | 242 ASSERT(m_operationInProgress == OperationAbort); |
| 243 m_operationInProgress = OperationNone; | 243 m_operationInProgress = OperationNone; |
| 244 Operation operation = m_queuedOperation; | 244 Operation operation = m_queuedOperation; |
| 245 m_queuedOperation = OperationNone; | 245 m_queuedOperation = OperationNone; |
| 246 doOperation(operation); | 246 doOperation(operation); |
| 247 } | 247 } |
| 248 | 248 |
| 249 void FileWriter::doOperation(Operation operation) | 249 void FileWriter::doOperation(Operation operation) |
| 250 { | 250 { |
| 251 InspectorInstrumentation::asyncTaskScheduled(getExecutionContext(), "FileWri
ter", this); | 251 InspectorInstrumentation::asyncTaskScheduled(getExecutionContext(), "FileWri
ter", this); |
| 252 switch (operation) { | 252 switch (operation) { |
| 253 case OperationWrite: | 253 case OperationWrite: |
| 254 ASSERT(m_operationInProgress == OperationNone); | 254 DCHECK_EQ(OperationNone, m_operationInProgress); |
| 255 ASSERT(m_truncateLength == -1); | 255 DCHECK_EQ(-1, m_truncateLength); |
| 256 ASSERT(m_blobBeingWritten.get()); | 256 DCHECK(m_blobBeingWritten.get()); |
| 257 ASSERT(m_readyState == WRITING); | 257 DCHECK_EQ(kWriting, m_readyState); |
| 258 writer()->write(position(), m_blobBeingWritten->uuid()); | 258 writer()->write(position(), m_blobBeingWritten->uuid()); |
| 259 break; | 259 break; |
| 260 case OperationTruncate: | 260 case OperationTruncate: |
| 261 ASSERT(m_operationInProgress == OperationNone); | 261 DCHECK_EQ(OperationNone, m_operationInProgress); |
| 262 ASSERT(m_truncateLength >= 0); | 262 DCHECK_GE(m_truncateLength, 0); |
| 263 ASSERT(m_readyState == WRITING); | 263 DCHECK_EQ(kWriting, m_readyState); |
| 264 writer()->truncate(m_truncateLength); | 264 writer()->truncate(m_truncateLength); |
| 265 break; | 265 break; |
| 266 case OperationNone: | 266 case OperationNone: |
| 267 ASSERT(m_operationInProgress == OperationNone); | 267 DCHECK_EQ(OperationNone, m_operationInProgress); |
| 268 ASSERT(m_truncateLength == -1); | 268 DCHECK_EQ(-1, m_truncateLength); |
| 269 ASSERT(!m_blobBeingWritten.get()); | 269 DCHECK(!m_blobBeingWritten.get()); |
| 270 ASSERT(m_readyState == DONE); | 270 DCHECK_EQ(kDone, m_readyState); |
| 271 break; | 271 break; |
| 272 case OperationAbort: | 272 case OperationAbort: |
| 273 if (m_operationInProgress == OperationWrite || m_operationInProgress ==
OperationTruncate) | 273 if (m_operationInProgress == OperationWrite || m_operationInProgress ==
OperationTruncate) |
| 274 writer()->cancel(); | 274 writer()->cancel(); |
| 275 else if (m_operationInProgress != OperationAbort) | 275 else if (m_operationInProgress != OperationAbort) |
| 276 operation = OperationNone; | 276 operation = OperationNone; |
| 277 m_queuedOperation = OperationNone; | 277 m_queuedOperation = OperationNone; |
| 278 m_blobBeingWritten.clear(); | 278 m_blobBeingWritten.clear(); |
| 279 m_truncateLength = -1; | 279 m_truncateLength = -1; |
| 280 break; | 280 break; |
| 281 } | 281 } |
| 282 ASSERT(m_queuedOperation == OperationNone); | 282 ASSERT(m_queuedOperation == OperationNone); |
| 283 m_operationInProgress = operation; | 283 m_operationInProgress = operation; |
| 284 } | 284 } |
| 285 | 285 |
| 286 void FileWriter::signalCompletion(FileError::ErrorCode code) | 286 void FileWriter::signalCompletion(FileError::ErrorCode code) |
| 287 { | 287 { |
| 288 m_readyState = DONE; | 288 m_readyState = kDone; |
| 289 m_truncateLength = -1; | 289 m_truncateLength = -1; |
| 290 if (FileError::OK != code) { | 290 if (FileError::kOK != code) { |
| 291 m_error = FileError::createDOMException(code); | 291 m_error = FileError::createDOMException(code); |
| 292 if (FileError::ABORT_ERR == code) | 292 if (FileError::kAbortErr == code) |
| 293 fireEvent(EventTypeNames::abort); | 293 fireEvent(EventTypeNames::abort); |
| 294 else | 294 else |
| 295 fireEvent(EventTypeNames::error); | 295 fireEvent(EventTypeNames::error); |
| 296 } else | 296 } else |
| 297 fireEvent(EventTypeNames::write); | 297 fireEvent(EventTypeNames::write); |
| 298 fireEvent(EventTypeNames::writeend); | 298 fireEvent(EventTypeNames::writeend); |
| 299 | 299 |
| 300 InspectorInstrumentation::asyncTaskCanceled(getExecutionContext(), this); | 300 InspectorInstrumentation::asyncTaskCanceled(getExecutionContext(), this); |
| 301 } | 301 } |
| 302 | 302 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 319 DEFINE_TRACE(FileWriter) | 319 DEFINE_TRACE(FileWriter) |
| 320 { | 320 { |
| 321 visitor->trace(m_error); | 321 visitor->trace(m_error); |
| 322 visitor->trace(m_blobBeingWritten); | 322 visitor->trace(m_blobBeingWritten); |
| 323 EventTargetWithInlineData::trace(visitor); | 323 EventTargetWithInlineData::trace(visitor); |
| 324 FileWriterBase::trace(visitor); | 324 FileWriterBase::trace(visitor); |
| 325 ActiveDOMObject::trace(visitor); | 325 ActiveDOMObject::trace(visitor); |
| 326 } | 326 } |
| 327 | 327 |
| 328 } // namespace blink | 328 } // namespace blink |
| OLD | NEW |