OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/glue/media/buffered_resource_loader.h" | 5 #include "webkit/glue/media/buffered_resource_loader.h" |
6 | 6 |
7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" | 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" |
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKitClient.h" | 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKitClient.h" |
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" | 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLError.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLError.h" |
14 #include "webkit/glue/media/buffered_data_source.h" | |
15 #include "webkit/glue/media/defer_strategy.h" | |
14 #include "webkit/glue/multipart_response_delegate.h" | 16 #include "webkit/glue/multipart_response_delegate.h" |
15 #include "webkit/glue/webkit_glue.h" | 17 #include "webkit/glue/webkit_glue.h" |
16 | 18 |
17 using WebKit::WebFrame; | 19 using WebKit::WebFrame; |
18 using WebKit::WebString; | 20 using WebKit::WebString; |
19 using WebKit::WebURLError; | 21 using WebKit::WebURLError; |
20 using WebKit::WebURLLoader; | 22 using WebKit::WebURLLoader; |
21 using WebKit::WebURLRequest; | 23 using WebKit::WebURLRequest; |
22 using WebKit::WebURLResponse; | 24 using WebKit::WebURLResponse; |
23 using webkit_glue::MultipartResponseDelegate; | 25 using webkit_glue::MultipartResponseDelegate; |
(...skipping 19 matching lines...) Expand all Loading... | |
43 // number to be 2MB. | 45 // number to be 2MB. |
44 // TODO(hclam): determine a better value for this. | 46 // TODO(hclam): determine a better value for this. |
45 static const int kForwardWaitThreshold = 2 * kMegabyte; | 47 static const int kForwardWaitThreshold = 2 * kMegabyte; |
46 | 48 |
47 BufferedResourceLoader::BufferedResourceLoader( | 49 BufferedResourceLoader::BufferedResourceLoader( |
48 const GURL& url, | 50 const GURL& url, |
49 int64 first_byte_position, | 51 int64 first_byte_position, |
50 int64 last_byte_position) | 52 int64 last_byte_position) |
51 : buffer_(new media::SeekableBuffer(kBackwardCapcity, kForwardCapacity)), | 53 : buffer_(new media::SeekableBuffer(kBackwardCapcity, kForwardCapacity)), |
52 deferred_(false), | 54 deferred_(false), |
53 defer_allowed_(true), | 55 defer_strategy_(ReadThenDeferStrategy::GetInstance()), |
54 completed_(false), | 56 completed_(false), |
55 range_requested_(false), | 57 range_requested_(false), |
56 partial_response_(false), | 58 partial_response_(false), |
57 url_(url), | 59 url_(url), |
58 first_byte_position_(first_byte_position), | 60 first_byte_position_(first_byte_position), |
59 last_byte_position_(last_byte_position), | 61 last_byte_position_(last_byte_position), |
60 single_origin_(true), | 62 single_origin_(true), |
61 start_callback_(NULL), | 63 start_callback_(NULL), |
62 offset_(0), | 64 offset_(0), |
63 content_length_(kPositionNotSpecified), | 65 content_length_(kPositionNotSpecified), |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
177 first_offset_ = static_cast<int>(read_position_ - offset_); | 179 first_offset_ = static_cast<int>(read_position_ - offset_); |
178 last_offset_ = first_offset_ + read_size_; | 180 last_offset_ = first_offset_ + read_size_; |
179 | 181 |
180 // If we can serve the request now, do the actual read. | 182 // If we can serve the request now, do the actual read. |
181 if (CanFulfillRead()) { | 183 if (CanFulfillRead()) { |
182 ReadInternal(); | 184 ReadInternal(); |
183 DisableDeferIfNeeded(); | 185 DisableDeferIfNeeded(); |
184 return; | 186 return; |
185 } | 187 } |
186 | 188 |
189 DisableDeferIfNeeded(); | |
acolwell GONE FROM CHROMIUM
2011/03/25 04:35:28
Why is this call needed now?
vrk (LEFT CHROMIUM)
2011/03/25 21:33:32
Ah, a little hard to explain :)
This call is nee
| |
190 | |
187 // If we expected the read request to be fulfilled later, returns | 191 // If we expected the read request to be fulfilled later, returns |
188 // immediately and let more data to flow in. | 192 // immediately and let more data to flow in. |
189 if (WillFulfillRead()) | 193 if (WillFulfillRead()) |
190 return; | 194 return; |
191 | 195 |
192 // Make a callback to report failure. | 196 // Make a callback to report failure. |
193 DoneRead(net::ERR_CACHE_MISS); | 197 DoneRead(net::ERR_CACHE_MISS); |
194 } | 198 } |
195 | 199 |
196 int64 BufferedResourceLoader::GetBufferedPosition() { | 200 int64 BufferedResourceLoader::GetBufferedPosition() { |
197 if (buffer_.get()) | 201 if (buffer_.get()) |
198 return offset_ + static_cast<int>(buffer_->forward_bytes()) - 1; | 202 return offset_ + static_cast<int>(buffer_->forward_bytes()) - 1; |
199 return kPositionNotSpecified; | 203 return kPositionNotSpecified; |
200 } | 204 } |
201 | 205 |
202 void BufferedResourceLoader::SetAllowDefer(bool is_allowed) { | 206 void BufferedResourceLoader::UpdateDeferStrategy( |
203 defer_allowed_ = is_allowed; | 207 BufferedDataSource* data_source) { |
204 DisableDeferIfNeeded(); | 208 DeferStrategy* strategy = data_source->ChooseDeferStrategy(); |
acolwell GONE FROM CHROMIUM
2011/03/25 04:35:28
Why don't you do the ChooseDeferStrategy() in the
vrk (LEFT CHROMIUM)
2011/03/25 21:33:32
Done.
| |
209 if (strategy != defer_strategy_) { | |
210 defer_strategy_ = strategy; | |
211 EnableDeferIfNeeded(); | |
212 DisableDeferIfNeeded(); | |
acolwell GONE FROM CHROMIUM
2011/03/25 04:35:28
The EnableDeferIfNeeded() followed by DisableDefer
vrk (LEFT CHROMIUM)
2011/03/25 21:33:32
Redid this to something more intuitive.
| |
213 } | |
205 } | 214 } |
206 | 215 |
207 int64 BufferedResourceLoader::content_length() { | 216 int64 BufferedResourceLoader::content_length() { |
208 return content_length_; | 217 return content_length_; |
209 } | 218 } |
210 | 219 |
211 int64 BufferedResourceLoader::instance_size() { | 220 int64 BufferedResourceLoader::instance_size() { |
212 return instance_size_; | 221 return instance_size_; |
213 } | 222 } |
214 | 223 |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
329 // In this case we shouldn't do anything. | 338 // In this case we shouldn't do anything. |
330 if (!buffer_.get()) | 339 if (!buffer_.get()) |
331 return; | 340 return; |
332 | 341 |
333 // Writes more data to |buffer_|. | 342 // Writes more data to |buffer_|. |
334 buffer_->Append(reinterpret_cast<const uint8*>(data), data_length); | 343 buffer_->Append(reinterpret_cast<const uint8*>(data), data_length); |
335 | 344 |
336 // If there is an active read request, try to fulfill the request. | 345 // If there is an active read request, try to fulfill the request. |
337 if (HasPendingRead() && CanFulfillRead()) { | 346 if (HasPendingRead() && CanFulfillRead()) { |
338 ReadInternal(); | 347 ReadInternal(); |
339 } else if (!defer_allowed_) { | 348 } else if (!defer_strategy_->AllowsDeferring()) { |
340 // If we're not allowed to defer, slide the buffer window forward instead | 349 // If we're not allowed to defer, slide the buffer window forward instead |
341 // of deferring. | 350 // of deferring. |
342 if (buffer_->forward_bytes() > buffer_->forward_capacity()) { | 351 if (buffer_->forward_bytes() > buffer_->forward_capacity()) { |
343 size_t excess = buffer_->forward_bytes() - buffer_->forward_capacity(); | 352 size_t excess = buffer_->forward_bytes() - buffer_->forward_capacity(); |
344 bool success = buffer_->Seek(excess); | 353 bool success = buffer_->Seek(excess); |
345 DCHECK(success); | 354 DCHECK(success); |
346 offset_ += first_offset_ + excess; | 355 offset_ += first_offset_ + excess; |
347 } | 356 } |
348 } | 357 } |
349 | 358 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
425 Release(); | 434 Release(); |
426 } | 435 } |
427 | 436 |
428 bool BufferedResourceLoader::HasSingleOrigin() const { | 437 bool BufferedResourceLoader::HasSingleOrigin() const { |
429 return single_origin_; | 438 return single_origin_; |
430 } | 439 } |
431 | 440 |
432 ///////////////////////////////////////////////////////////////////////////// | 441 ///////////////////////////////////////////////////////////////////////////// |
433 // Helper methods. | 442 // Helper methods. |
434 void BufferedResourceLoader::EnableDeferIfNeeded() { | 443 void BufferedResourceLoader::EnableDeferIfNeeded() { |
435 if (!defer_allowed_) | 444 bool event_occurred = defer_strategy_->EnableDeferIfNeeded(this); |
436 return; | 445 if (event_occurred) |
437 | |
438 if (!deferred_ && | |
439 buffer_->forward_bytes() >= buffer_->forward_capacity()) { | |
440 deferred_ = true; | |
441 | |
442 if (url_loader_.get()) | |
443 url_loader_->setDefersLoading(true); | |
444 | |
445 NotifyNetworkEvent(); | 446 NotifyNetworkEvent(); |
446 } | |
447 } | 447 } |
448 | 448 |
449 void BufferedResourceLoader::DisableDeferIfNeeded() { | 449 void BufferedResourceLoader::DisableDeferIfNeeded() { |
450 if (deferred_ && | 450 bool event_occurred = defer_strategy_->DisableDeferIfNeeded(this); |
451 (!defer_allowed_ || | 451 if (event_occurred) |
452 buffer_->forward_bytes() < buffer_->forward_capacity() / 2)) { | |
453 deferred_ = false; | |
454 | |
455 if (url_loader_.get()) | |
456 url_loader_->setDefersLoading(false); | |
457 | |
458 NotifyNetworkEvent(); | 452 NotifyNetworkEvent(); |
459 } | |
460 } | 453 } |
461 | 454 |
462 bool BufferedResourceLoader::CanFulfillRead() { | 455 bool BufferedResourceLoader::CanFulfillRead() { |
463 // If we are reading too far in the backward direction. | 456 // If we are reading too far in the backward direction. |
464 if (first_offset_ < 0 && | 457 if (first_offset_ < 0 && |
465 first_offset_ + static_cast<int>(buffer_->backward_bytes()) < 0) | 458 first_offset_ + static_cast<int>(buffer_->backward_bytes()) < 0) |
466 return false; | 459 return false; |
467 | 460 |
468 // If the start offset is too far ahead. | 461 // If the start offset is too far ahead. |
469 if (first_offset_ >= static_cast<int>(buffer_->forward_bytes())) | 462 if (first_offset_ >= static_cast<int>(buffer_->forward_bytes())) |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
573 start_callback_->RunWithParams(Tuple1<int>(error)); | 566 start_callback_->RunWithParams(Tuple1<int>(error)); |
574 start_callback_.reset(); | 567 start_callback_.reset(); |
575 } | 568 } |
576 | 569 |
577 void BufferedResourceLoader::NotifyNetworkEvent() { | 570 void BufferedResourceLoader::NotifyNetworkEvent() { |
578 if (event_callback_.get()) | 571 if (event_callback_.get()) |
579 event_callback_->Run(); | 572 event_callback_->Run(); |
580 } | 573 } |
581 | 574 |
582 } // namespace webkit_glue | 575 } // namespace webkit_glue |
OLD | NEW |