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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 operation_in_progress_(kOperationNone), | 53 operation_in_progress_(kOperationNone), |
54 queued_operation_(kOperationNone), | 54 queued_operation_(kOperationNone), |
55 bytes_written_(0), | 55 bytes_written_(0), |
56 bytes_to_write_(0), | 56 bytes_to_write_(0), |
57 truncate_length_(-1), | 57 truncate_length_(-1), |
58 num_aborts_(0), | 58 num_aborts_(0), |
59 recursion_depth_(0), | 59 recursion_depth_(0), |
60 last_progress_notification_time_ms_(0) {} | 60 last_progress_notification_time_ms_(0) {} |
61 | 61 |
62 FileWriter::~FileWriter() { | 62 FileWriter::~FileWriter() { |
63 ASSERT(!recursion_depth_); | 63 DCHECK(!recursion_depth_); |
64 DCHECK(!Writer()); | 64 DCHECK(!Writer()); |
65 } | 65 } |
66 | 66 |
67 const AtomicString& FileWriter::InterfaceName() const { | 67 const AtomicString& FileWriter::InterfaceName() const { |
68 return EventTargetNames::FileWriter; | 68 return EventTargetNames::FileWriter; |
69 } | 69 } |
70 | 70 |
71 void FileWriter::ContextDestroyed(ExecutionContext*) { | 71 void FileWriter::ContextDestroyed(ExecutionContext*) { |
72 Dispose(); | 72 Dispose(); |
73 } | 73 } |
74 | 74 |
75 bool FileWriter::HasPendingActivity() const { | 75 bool FileWriter::HasPendingActivity() const { |
76 return operation_in_progress_ != kOperationNone || | 76 return operation_in_progress_ != kOperationNone || |
77 queued_operation_ != kOperationNone || ready_state_ == kWriting; | 77 queued_operation_ != kOperationNone || ready_state_ == kWriting; |
78 } | 78 } |
79 | 79 |
80 void FileWriter::write(Blob* data, ExceptionState& exception_state) { | 80 void FileWriter::write(Blob* data, ExceptionState& exception_state) { |
81 if (!GetExecutionContext()) | 81 if (!GetExecutionContext()) |
82 return; | 82 return; |
83 ASSERT(data); | 83 DCHECK(data); |
84 ASSERT(Writer()); | 84 DCHECK(Writer()); |
85 ASSERT(truncate_length_ == -1); | 85 DCHECK_EQ(truncate_length_, -1); |
86 if (ready_state_ == kWriting) { | 86 if (ready_state_ == kWriting) { |
87 SetError(FileError::kInvalidStateErr, exception_state); | 87 SetError(FileError::kInvalidStateErr, exception_state); |
88 return; | 88 return; |
89 } | 89 } |
90 if (recursion_depth_ > kMaxRecursionDepth) { | 90 if (recursion_depth_ > kMaxRecursionDepth) { |
91 SetError(FileError::kSecurityErr, exception_state); | 91 SetError(FileError::kSecurityErr, exception_state); |
92 return; | 92 return; |
93 } | 93 } |
94 | 94 |
95 blob_being_written_ = data; | 95 blob_being_written_ = data; |
96 ready_state_ = kWriting; | 96 ready_state_ = kWriting; |
97 bytes_written_ = 0; | 97 bytes_written_ = 0; |
98 bytes_to_write_ = data->size(); | 98 bytes_to_write_ = data->size(); |
99 ASSERT(queued_operation_ == kOperationNone); | 99 DCHECK_EQ(queued_operation_, kOperationNone); |
100 if (operation_in_progress_ != kOperationNone) { | 100 if (operation_in_progress_ != kOperationNone) { |
101 // We must be waiting for an abort to complete, since m_readyState wasn't | 101 // We must be waiting for an abort to complete, since m_readyState wasn't |
102 // kWriting. | 102 // kWriting. |
103 ASSERT(operation_in_progress_ == kOperationAbort); | 103 DCHECK_EQ(operation_in_progress_, kOperationAbort); |
104 queued_operation_ = kOperationWrite; | 104 queued_operation_ = kOperationWrite; |
105 } else | 105 } else |
106 DoOperation(kOperationWrite); | 106 DoOperation(kOperationWrite); |
107 | 107 |
108 FireEvent(EventTypeNames::writestart); | 108 FireEvent(EventTypeNames::writestart); |
109 } | 109 } |
110 | 110 |
111 void FileWriter::seek(long long position, ExceptionState& exception_state) { | 111 void FileWriter::seek(long long position, ExceptionState& exception_state) { |
112 if (!GetExecutionContext()) | 112 if (!GetExecutionContext()) |
113 return; | 113 return; |
114 ASSERT(Writer()); | 114 DCHECK(Writer()); |
115 if (ready_state_ == kWriting) { | 115 if (ready_state_ == kWriting) { |
116 SetError(FileError::kInvalidStateErr, exception_state); | 116 SetError(FileError::kInvalidStateErr, exception_state); |
117 return; | 117 return; |
118 } | 118 } |
119 | 119 |
120 ASSERT(truncate_length_ == -1); | 120 DCHECK_EQ(truncate_length_, -1); |
121 ASSERT(queued_operation_ == kOperationNone); | 121 DCHECK_EQ(queued_operation_, kOperationNone); |
122 SeekInternal(position); | 122 SeekInternal(position); |
123 } | 123 } |
124 | 124 |
125 void FileWriter::truncate(long long position, ExceptionState& exception_state) { | 125 void FileWriter::truncate(long long position, ExceptionState& exception_state) { |
126 if (!GetExecutionContext()) | 126 if (!GetExecutionContext()) |
127 return; | 127 return; |
128 ASSERT(Writer()); | 128 DCHECK(Writer()); |
129 ASSERT(truncate_length_ == -1); | 129 DCHECK_EQ(truncate_length_, -1); |
130 if (ready_state_ == kWriting || position < 0) { | 130 if (ready_state_ == kWriting || position < 0) { |
131 SetError(FileError::kInvalidStateErr, exception_state); | 131 SetError(FileError::kInvalidStateErr, exception_state); |
132 return; | 132 return; |
133 } | 133 } |
134 if (recursion_depth_ > kMaxRecursionDepth) { | 134 if (recursion_depth_ > kMaxRecursionDepth) { |
135 SetError(FileError::kSecurityErr, exception_state); | 135 SetError(FileError::kSecurityErr, exception_state); |
136 return; | 136 return; |
137 } | 137 } |
138 | 138 |
139 ready_state_ = kWriting; | 139 ready_state_ = kWriting; |
140 bytes_written_ = 0; | 140 bytes_written_ = 0; |
141 bytes_to_write_ = 0; | 141 bytes_to_write_ = 0; |
142 truncate_length_ = position; | 142 truncate_length_ = position; |
143 ASSERT(queued_operation_ == kOperationNone); | 143 DCHECK_EQ(queued_operation_, kOperationNone); |
144 if (operation_in_progress_ != kOperationNone) { | 144 if (operation_in_progress_ != kOperationNone) { |
145 // We must be waiting for an abort to complete, since m_readyState wasn't | 145 // We must be waiting for an abort to complete, since m_readyState wasn't |
146 // kWriting. | 146 // kWriting. |
147 ASSERT(operation_in_progress_ == kOperationAbort); | 147 DCHECK_EQ(operation_in_progress_, kOperationAbort); |
148 queued_operation_ = kOperationTruncate; | 148 queued_operation_ = kOperationTruncate; |
149 } else | 149 } else |
150 DoOperation(kOperationTruncate); | 150 DoOperation(kOperationTruncate); |
151 FireEvent(EventTypeNames::writestart); | 151 FireEvent(EventTypeNames::writestart); |
152 } | 152 } |
153 | 153 |
154 void FileWriter::abort(ExceptionState& exception_state) { | 154 void FileWriter::abort(ExceptionState& exception_state) { |
155 if (!GetExecutionContext()) | 155 if (!GetExecutionContext()) |
156 return; | 156 return; |
157 ASSERT(Writer()); | 157 DCHECK(Writer()); |
158 if (ready_state_ != kWriting) | 158 if (ready_state_ != kWriting) |
159 return; | 159 return; |
160 ++num_aborts_; | 160 ++num_aborts_; |
161 | 161 |
162 DoOperation(kOperationAbort); | 162 DoOperation(kOperationAbort); |
163 SignalCompletion(FileError::kAbortErr); | 163 SignalCompletion(FileError::kAbortErr); |
164 } | 164 } |
165 | 165 |
166 void FileWriter::DidWrite(long long bytes, bool complete) { | 166 void FileWriter::DidWrite(long long bytes, bool complete) { |
167 if (operation_in_progress_ == kOperationAbort) { | 167 if (operation_in_progress_ == kOperationAbort) { |
(...skipping 30 matching lines...) Expand all Loading... |
198 if (num_aborts == num_aborts_) | 198 if (num_aborts == num_aborts_) |
199 SignalCompletion(FileError::kOK); | 199 SignalCompletion(FileError::kOK); |
200 } | 200 } |
201 } | 201 } |
202 | 202 |
203 void FileWriter::DidTruncate() { | 203 void FileWriter::DidTruncate() { |
204 if (operation_in_progress_ == kOperationAbort) { | 204 if (operation_in_progress_ == kOperationAbort) { |
205 CompleteAbort(); | 205 CompleteAbort(); |
206 return; | 206 return; |
207 } | 207 } |
208 ASSERT(operation_in_progress_ == kOperationTruncate); | 208 DCHECK_EQ(operation_in_progress_, kOperationTruncate); |
209 ASSERT(truncate_length_ >= 0); | 209 DCHECK_GE(truncate_length_, 0); |
210 SetLength(truncate_length_); | 210 SetLength(truncate_length_); |
211 if (position() > length()) | 211 if (position() > length()) |
212 SetPosition(length()); | 212 SetPosition(length()); |
213 operation_in_progress_ = kOperationNone; | 213 operation_in_progress_ = kOperationNone; |
214 SignalCompletion(FileError::kOK); | 214 SignalCompletion(FileError::kOK); |
215 } | 215 } |
216 | 216 |
217 void FileWriter::DidFail(WebFileError code) { | 217 void FileWriter::DidFail(WebFileError code) { |
218 DCHECK_NE(kOperationNone, operation_in_progress_); | 218 DCHECK_NE(kOperationNone, operation_in_progress_); |
219 DCHECK_NE(FileError::kOK, static_cast<FileError::ErrorCode>(code)); | 219 DCHECK_NE(FileError::kOK, static_cast<FileError::ErrorCode>(code)); |
220 if (operation_in_progress_ == kOperationAbort) { | 220 if (operation_in_progress_ == kOperationAbort) { |
221 CompleteAbort(); | 221 CompleteAbort(); |
222 return; | 222 return; |
223 } | 223 } |
224 DCHECK_EQ(kOperationNone, queued_operation_); | 224 DCHECK_EQ(kOperationNone, queued_operation_); |
225 DCHECK_EQ(kWriting, ready_state_); | 225 DCHECK_EQ(kWriting, ready_state_); |
226 blob_being_written_.Clear(); | 226 blob_being_written_.Clear(); |
227 operation_in_progress_ = kOperationNone; | 227 operation_in_progress_ = kOperationNone; |
228 SignalCompletion(static_cast<FileError::ErrorCode>(code)); | 228 SignalCompletion(static_cast<FileError::ErrorCode>(code)); |
229 } | 229 } |
230 | 230 |
231 void FileWriter::CompleteAbort() { | 231 void FileWriter::CompleteAbort() { |
232 ASSERT(operation_in_progress_ == kOperationAbort); | 232 DCHECK_EQ(operation_in_progress_, kOperationAbort); |
233 operation_in_progress_ = kOperationNone; | 233 operation_in_progress_ = kOperationNone; |
234 Operation operation = queued_operation_; | 234 Operation operation = queued_operation_; |
235 queued_operation_ = kOperationNone; | 235 queued_operation_ = kOperationNone; |
236 DoOperation(operation); | 236 DoOperation(operation); |
237 } | 237 } |
238 | 238 |
239 void FileWriter::DoOperation(Operation operation) { | 239 void FileWriter::DoOperation(Operation operation) { |
240 probe::AsyncTaskScheduled(GetExecutionContext(), "FileWriter", this); | 240 probe::AsyncTaskScheduled(GetExecutionContext(), "FileWriter", this); |
241 switch (operation) { | 241 switch (operation) { |
242 case kOperationWrite: | 242 case kOperationWrite: |
(...skipping 19 matching lines...) Expand all Loading... |
262 if (operation_in_progress_ == kOperationWrite || | 262 if (operation_in_progress_ == kOperationWrite || |
263 operation_in_progress_ == kOperationTruncate) | 263 operation_in_progress_ == kOperationTruncate) |
264 Writer()->Cancel(); | 264 Writer()->Cancel(); |
265 else if (operation_in_progress_ != kOperationAbort) | 265 else if (operation_in_progress_ != kOperationAbort) |
266 operation = kOperationNone; | 266 operation = kOperationNone; |
267 queued_operation_ = kOperationNone; | 267 queued_operation_ = kOperationNone; |
268 blob_being_written_.Clear(); | 268 blob_being_written_.Clear(); |
269 truncate_length_ = -1; | 269 truncate_length_ = -1; |
270 break; | 270 break; |
271 } | 271 } |
272 ASSERT(queued_operation_ == kOperationNone); | 272 DCHECK_EQ(queued_operation_, kOperationNone); |
273 operation_in_progress_ = operation; | 273 operation_in_progress_ = operation; |
274 } | 274 } |
275 | 275 |
276 void FileWriter::SignalCompletion(FileError::ErrorCode code) { | 276 void FileWriter::SignalCompletion(FileError::ErrorCode code) { |
277 ready_state_ = kDone; | 277 ready_state_ = kDone; |
278 truncate_length_ = -1; | 278 truncate_length_ = -1; |
279 if (FileError::kOK != code) { | 279 if (FileError::kOK != code) { |
280 error_ = FileError::CreateDOMException(code); | 280 error_ = FileError::CreateDOMException(code); |
281 if (FileError::kAbortErr == code) | 281 if (FileError::kAbortErr == code) |
282 FireEvent(EventTypeNames::abort); | 282 FireEvent(EventTypeNames::abort); |
283 else | 283 else |
284 FireEvent(EventTypeNames::error); | 284 FireEvent(EventTypeNames::error); |
285 } else | 285 } else |
286 FireEvent(EventTypeNames::write); | 286 FireEvent(EventTypeNames::write); |
287 FireEvent(EventTypeNames::writeend); | 287 FireEvent(EventTypeNames::writeend); |
288 | 288 |
289 probe::AsyncTaskCanceled(GetExecutionContext(), this); | 289 probe::AsyncTaskCanceled(GetExecutionContext(), this); |
290 } | 290 } |
291 | 291 |
292 void FileWriter::FireEvent(const AtomicString& type) { | 292 void FileWriter::FireEvent(const AtomicString& type) { |
293 probe::AsyncTask async_task(GetExecutionContext(), this); | 293 probe::AsyncTask async_task(GetExecutionContext(), this); |
294 ++recursion_depth_; | 294 ++recursion_depth_; |
295 DispatchEvent( | 295 DispatchEvent( |
296 ProgressEvent::Create(type, true, bytes_written_, bytes_to_write_)); | 296 ProgressEvent::Create(type, true, bytes_written_, bytes_to_write_)); |
297 --recursion_depth_; | 297 --recursion_depth_; |
298 ASSERT(recursion_depth_ >= 0); | 298 DCHECK_GE(recursion_depth_, 0); |
299 } | 299 } |
300 | 300 |
301 void FileWriter::SetError(FileError::ErrorCode error_code, | 301 void FileWriter::SetError(FileError::ErrorCode error_code, |
302 ExceptionState& exception_state) { | 302 ExceptionState& exception_state) { |
303 ASSERT(error_code); | 303 DCHECK(error_code); |
304 FileError::ThrowDOMException(exception_state, error_code); | 304 FileError::ThrowDOMException(exception_state, error_code); |
305 error_ = FileError::CreateDOMException(error_code); | 305 error_ = FileError::CreateDOMException(error_code); |
306 } | 306 } |
307 | 307 |
308 void FileWriter::Dispose() { | 308 void FileWriter::Dispose() { |
309 // Make sure we've actually got something to stop, and haven't already called | 309 // Make sure we've actually got something to stop, and haven't already called |
310 // abort(). | 310 // abort(). |
311 if (Writer() && ready_state_ == kWriting) { | 311 if (Writer() && ready_state_ == kWriting) { |
312 DoOperation(kOperationAbort); | 312 DoOperation(kOperationAbort); |
313 ready_state_ = kDone; | 313 ready_state_ = kDone; |
314 } | 314 } |
315 ResetWriter(); | 315 ResetWriter(); |
316 } | 316 } |
317 | 317 |
318 DEFINE_TRACE(FileWriter) { | 318 DEFINE_TRACE(FileWriter) { |
319 visitor->Trace(error_); | 319 visitor->Trace(error_); |
320 visitor->Trace(blob_being_written_); | 320 visitor->Trace(blob_being_written_); |
321 EventTargetWithInlineData::Trace(visitor); | 321 EventTargetWithInlineData::Trace(visitor); |
322 FileWriterBase::Trace(visitor); | 322 FileWriterBase::Trace(visitor); |
323 ContextLifecycleObserver::Trace(visitor); | 323 ContextLifecycleObserver::Trace(visitor); |
324 } | 324 } |
325 | 325 |
326 } // namespace blink | 326 } // namespace blink |
OLD | NEW |