Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(420)

Side by Side Diff: webkit/glue/media/buffered_data_source.cc

Issue 3863002: Refactoring BufferedDataSource to work with WebURLLoader instead of a MediaResourceLoaderBridge. (Closed) Base URL: http://git.chromium.org/git/chromium.git
Patch Set: addressing andrew's comments Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 "base/callback.h" 5 #include "base/callback.h"
6 #include "base/compiler_specific.h" 6 #include "base/compiler_specific.h"
7 #include "base/format_macros.h"
7 #include "base/message_loop.h" 8 #include "base/message_loop.h"
8 #include "base/process_util.h" 9 #include "base/process_util.h"
9 #include "base/stl_util-inl.h" 10 #include "base/stl_util-inl.h"
11 #include "base/string_number_conversions.h"
10 #include "base/string_util.h" 12 #include "base/string_util.h"
11 #include "media/base/filter_host.h" 13 #include "media/base/filter_host.h"
12 #include "media/base/media_format.h" 14 #include "media/base/media_format.h"
13 #include "net/base/load_flags.h" 15 #include "net/base/load_flags.h"
14 #include "net/base/net_errors.h" 16 #include "net/base/net_errors.h"
15 #include "net/http/http_response_headers.h" 17 #include "third_party/WebKit/WebKit/chromium/public/WebKit.h"
18 #include "third_party/WebKit/WebKit/chromium/public/WebKitClient.h"
19 #include "third_party/WebKit/WebKit/chromium/public/WebString.h"
20 #include "third_party/WebKit/WebKit/chromium/public/WebURLError.h"
16 #include "webkit/glue/media/buffered_data_source.h" 21 #include "webkit/glue/media/buffered_data_source.h"
22 #include "webkit/glue/multipart_response_delegate.h"
17 #include "webkit/glue/webkit_glue.h" 23 #include "webkit/glue/webkit_glue.h"
18 #include "webkit/glue/webmediaplayer_impl.h" 24 #include "webkit/glue/webmediaplayer_impl.h"
19 25
26 using WebKit::WebFrame;
27 using WebKit::WebString;
28 using WebKit::WebURLError;
29 using WebKit::WebURLLoader;
30 using WebKit::WebURLRequest;
31 using WebKit::WebURLResponse;
32 using webkit_glue::MultipartResponseDelegate;
33
20 namespace { 34 namespace {
21 35
22 const char kHttpScheme[] = "http"; 36 const char kHttpScheme[] = "http";
23 const char kHttpsScheme[] = "https"; 37 const char kHttpsScheme[] = "https";
24 const char kDataScheme[] = "data"; 38 const char kDataScheme[] = "data";
25 const int64 kPositionNotSpecified = -1; 39 const int64 kPositionNotSpecified = -1;
26 const int kHttpOK = 200; 40 const int kHttpOK = 200;
27 const int kHttpPartialContent = 206; 41 const int kHttpPartialContent = 206;
28 42
29 // Define the number of bytes in a megabyte. 43 // Define the number of bytes in a megabyte.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 return url.SchemeIs(kHttpScheme) || url.SchemeIs(kHttpsScheme); 77 return url.SchemeIs(kHttpScheme) || url.SchemeIs(kHttpsScheme);
64 } 78 }
65 79
66 bool IsDataProtocol(const GURL& url) { 80 bool IsDataProtocol(const GURL& url) {
67 return url.SchemeIs(kDataScheme); 81 return url.SchemeIs(kDataScheme);
68 } 82 }
69 83
70 } // namespace 84 } // namespace
71 85
72 namespace webkit_glue { 86 namespace webkit_glue {
87
73 ///////////////////////////////////////////////////////////////////////////// 88 /////////////////////////////////////////////////////////////////////////////
74 // BufferedResourceLoader 89 // BufferedResourceLoader
75 BufferedResourceLoader::BufferedResourceLoader( 90 BufferedResourceLoader::BufferedResourceLoader(
76 webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory,
77 const GURL& url, 91 const GURL& url,
78 int64 first_byte_position, 92 int64 first_byte_position,
79 int64 last_byte_position) 93 int64 last_byte_position)
80 : buffer_(new media::SeekableBuffer(kBackwardCapcity, kForwardCapacity)), 94 : buffer_(new media::SeekableBuffer(kBackwardCapcity, kForwardCapacity)),
81 deferred_(false), 95 deferred_(false),
82 defer_allowed_(true), 96 defer_allowed_(true),
83 completed_(false), 97 completed_(false),
84 range_requested_(false), 98 range_requested_(false),
85 partial_response_(false), 99 partial_response_(false),
86 bridge_factory_(bridge_factory),
87 url_(url), 100 url_(url),
88 first_byte_position_(first_byte_position), 101 first_byte_position_(first_byte_position),
89 last_byte_position_(last_byte_position), 102 last_byte_position_(last_byte_position),
90 start_callback_(NULL), 103 start_callback_(NULL),
91 bridge_(NULL),
92 offset_(0), 104 offset_(0),
93 content_length_(kPositionNotSpecified), 105 content_length_(kPositionNotSpecified),
94 instance_size_(kPositionNotSpecified), 106 instance_size_(kPositionNotSpecified),
95 read_callback_(NULL), 107 read_callback_(NULL),
96 read_position_(0), 108 read_position_(0),
97 read_size_(0), 109 read_size_(0),
98 read_buffer_(NULL), 110 read_buffer_(NULL),
99 first_offset_(0), 111 first_offset_(0),
100 last_offset_(0) { 112 last_offset_(0),
113 keep_test_loader_(false) {
101 } 114 }
102 115
103 BufferedResourceLoader::~BufferedResourceLoader() { 116 BufferedResourceLoader::~BufferedResourceLoader() {
117 if (!completed_ && url_loader_.get())
118 url_loader_->cancel();
104 } 119 }
105 120
106 void BufferedResourceLoader::Start(net::CompletionCallback* start_callback, 121 void BufferedResourceLoader::Start(net::CompletionCallback* start_callback,
107 NetworkEventCallback* event_callback) { 122 NetworkEventCallback* event_callback,
123 WebFrame* frame) {
108 // Make sure we have not started. 124 // Make sure we have not started.
109 DCHECK(!bridge_.get());
110 DCHECK(!start_callback_.get()); 125 DCHECK(!start_callback_.get());
111 DCHECK(!event_callback_.get()); 126 DCHECK(!event_callback_.get());
112 DCHECK(start_callback); 127 DCHECK(start_callback);
113 DCHECK(event_callback); 128 DCHECK(event_callback);
114 129
130 // TODO(annacc): Maybe throw up an error here if frame is null?
131 if (frame == NULL)
scherkus (not reviewing) 2010/11/30 20:05:39 I'd make this a CHECK instead tracing through the
annacc 2010/11/30 21:51:45 Done.
132 return;
133
115 start_callback_.reset(start_callback); 134 start_callback_.reset(start_callback);
116 event_callback_.reset(event_callback); 135 event_callback_.reset(event_callback);
117 136
118 if (first_byte_position_ != kPositionNotSpecified) { 137 if (first_byte_position_ != kPositionNotSpecified) {
119 range_requested_ = true; 138 range_requested_ = true;
120 // TODO(hclam): server may not support range request so |offset_| may not 139 // TODO(hclam): server may not support range request so |offset_| may not
121 // equal to |first_byte_position_|. 140 // equal to |first_byte_position_|.
122 offset_ = first_byte_position_; 141 offset_ = first_byte_position_;
123 } 142 }
124 143
125 // Creates the bridge on render thread since we can only access
126 // ResourceDispatcher on this thread.
127 bridge_.reset(
128 bridge_factory_->CreateBridge(
129 url_,
130 net::LOAD_NORMAL,
131 first_byte_position_,
132 last_byte_position_));
133
134 // Increment the reference count right before we start the request. This 144 // Increment the reference count right before we start the request. This
135 // reference will be release when this request has ended. 145 // reference will be release when this request has ended.
136 AddRef(); 146 AddRef();
137 147
138 // And start the resource loading. 148 // Prepare the request.
139 bridge_->Start(this); 149 WebURLRequest request(url_);
150 request.setHTTPHeaderField(WebString::fromUTF8("Range"),
151 WebString::fromUTF8(GenerateHeaders(
152 first_byte_position_,
153 last_byte_position_)));
154 frame->setReferrerForRequest(request, WebKit::WebURL());
155 frame->dispatchWillSendRequest(request);
156
157 // This flag is for unittests as we don't want to reset |url_loader|
158 if (!keep_test_loader_)
159 url_loader_.reset(WebKit::webKitClient()->createURLLoader());
160
161 // Start the resource loading.
162 url_loader_->loadAsynchronously(request, this);
140 } 163 }
141 164
142 void BufferedResourceLoader::Stop() { 165 void BufferedResourceLoader::Stop() {
143 // Reset callbacks. 166 // Reset callbacks.
144 start_callback_.reset(); 167 start_callback_.reset();
145 event_callback_.reset(); 168 event_callback_.reset();
146 read_callback_.reset(); 169 read_callback_.reset();
147 170
148 // Use the internal buffer to signal that we have been stopped. 171 // Use the internal buffer to signal that we have been stopped.
149 // TODO(hclam): Not so pretty to do this. 172 // TODO(hclam): Not so pretty to do this.
150 if (!buffer_.get()) 173 if (!buffer_.get())
151 return; 174 return;
152 175
153 // Destroy internal buffer. 176 // Destroy internal buffer.
154 buffer_.reset(); 177 buffer_.reset();
155 178
156 if (bridge_.get()) { 179 if (url_loader_.get()) {
157 // Cancel the request. This method call will cancel the request
158 // asynchronously. We may still get data or messages until we receive
159 // a response completed message.
160 if (deferred_) 180 if (deferred_)
161 bridge_->SetDefersLoading(false); 181 url_loader_->setDefersLoading(false);
162 deferred_ = false; 182 deferred_ = false;
163 bridge_->Cancel(); 183
184 if (!completed_) {
185 url_loader_->cancel();
186 completed_ = true;
187 }
164 } 188 }
165 } 189 }
166 190
167 void BufferedResourceLoader::Read(int64 position, 191 void BufferedResourceLoader::Read(int64 position,
168 int read_size, 192 int read_size,
169 uint8* buffer, 193 uint8* buffer,
170 net::CompletionCallback* read_callback) { 194 net::CompletionCallback* read_callback) {
171 DCHECK(!read_callback_.get()); 195 DCHECK(!read_callback_.get());
172 DCHECK(buffer_.get()); 196 DCHECK(buffer_.get());
173 DCHECK(read_callback); 197 DCHECK(read_callback);
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 if (buffer_.get()) 248 if (buffer_.get())
225 return offset_ + static_cast<int>(buffer_->forward_bytes()) - 1; 249 return offset_ + static_cast<int>(buffer_->forward_bytes()) - 1;
226 return kPositionNotSpecified; 250 return kPositionNotSpecified;
227 } 251 }
228 252
229 void BufferedResourceLoader::SetAllowDefer(bool is_allowed) { 253 void BufferedResourceLoader::SetAllowDefer(bool is_allowed) {
230 defer_allowed_ = is_allowed; 254 defer_allowed_ = is_allowed;
231 DisableDeferIfNeeded(); 255 DisableDeferIfNeeded();
232 } 256 }
233 257
258 void BufferedResourceLoader::SetURLLoaderForTest(WebURLLoader* mock_loader) {
259 url_loader_.reset(mock_loader);
260 keep_test_loader_ = true;
261 }
262
234 ///////////////////////////////////////////////////////////////////////////// 263 /////////////////////////////////////////////////////////////////////////////
235 // BufferedResourceLoader, 264 // BufferedResourceLoader,
scherkus (not reviewing) 2010/11/30 20:05:39 nit: this comment can fit on a single line
annacc 2010/11/30 21:51:45 Done.
236 // webkit_glue::ResourceLoaderBridge::Peer implementations 265 // WebKit::WebURLLoaderClient implementations.
237 bool BufferedResourceLoader::OnReceivedRedirect( 266 void BufferedResourceLoader::willSendRequest(
238 const GURL& new_url, 267 WebURLLoader* loader,
239 const webkit_glue::ResourceResponseInfo& info, 268 WebURLRequest& newRequest,
240 bool* has_new_first_party_for_cookies, 269 const WebURLResponse& redirectResponse) {
241 GURL* new_first_party_for_cookies) {
242 DCHECK(bridge_.get());
243
244 // Save the new URL.
245 url_ = new_url;
246 // TODO(wtc): should we return a new first party for cookies URL?
247 *has_new_first_party_for_cookies = false;
248 270
249 // The load may have been stopped and |start_callback| is destroyed. 271 // The load may have been stopped and |start_callback| is destroyed.
250 // In this case we shouldn't do anything. 272 // In this case we shouldn't do anything.
251 if (!start_callback_.get()) 273 if (!start_callback_.get()) {
252 return true; 274 // Set the url in the request to an invalid value (empty url).
275 newRequest.setURL(WebKit::WebURL());
276 return;
277 }
253 278
254 if (!IsProtocolSupportedForMedia(new_url)) { 279 if (!IsProtocolSupportedForMedia(newRequest.url())) {
280 // Set the url in the request to an invalid value (empty url).
281 newRequest.setURL(WebKit::WebURL());
255 DoneStart(net::ERR_ADDRESS_INVALID); 282 DoneStart(net::ERR_ADDRESS_INVALID);
256 Stop(); 283 Stop();
257 return false; 284 return;
258 } 285 }
259 return true; 286
287 url_loader_.reset(loader);
288 url_ = newRequest.url();
260 } 289 }
261 290
262 void BufferedResourceLoader::OnReceivedResponse( 291 void BufferedResourceLoader::didSendData(
263 const webkit_glue::ResourceResponseInfo& info, 292 WebURLLoader* loader,
264 bool content_filtered) { 293 unsigned long long bytes_sent,
265 DCHECK(bridge_.get()); 294 unsigned long long total_bytes_to_be_sent) {
295 NOTIMPLEMENTED();
296 }
297
298 void BufferedResourceLoader::didReceiveResponse(
299 WebURLLoader* loader,
300 const WebURLResponse& response) {
266 301
267 // The loader may have been stopped and |start_callback| is destroyed. 302 // The loader may have been stopped and |start_callback| is destroyed.
268 // In this case we shouldn't do anything. 303 // In this case we shouldn't do anything.
269 if (!start_callback_.get()) 304 if (!start_callback_.get())
270 return; 305 return;
271 306
272 // We make a strong assumption that when we reach here we have either 307 // We make a strong assumption that when we reach here we have either
273 // received a response from HTTP/HTTPS protocol or the request was 308 // received a response from HTTP/HTTPS protocol or the request was
274 // successful (in particular range request). So we only verify the partial 309 // successful (in particular range request). So we only verify the partial
275 // response for HTTP and HTTPS protocol. 310 // response for HTTP and HTTPS protocol.
276 if (IsHttpProtocol(url_)) { 311 if (IsHttpProtocol(url_)) {
277 int error = net::OK; 312 int error = net::OK;
278 if (!info.headers) {
279 // We expect to receive headers because this is a HTTP or HTTPS protocol,
280 // if not report failure.
281 error = net::ERR_INVALID_RESPONSE;
282 } else {
283 if (info.headers->response_code() == kHttpPartialContent)
284 partial_response_ = true;
285 313
286 if (range_requested_ && partial_response_) { 314 if (response.httpStatusCode() == kHttpPartialContent)
287 // If we have verified the partial response and it is correct, we will 315 partial_response_ = true;
288 // return net::OK. 316
289 if (!VerifyPartialResponse(info)) 317 if (range_requested_ && partial_response_) {
290 error = net::ERR_INVALID_RESPONSE; 318 // If we have verified the partial response and it is correct, we will
291 } else if (info.headers->response_code() != kHttpOK) { 319 // return net::OK.
292 // We didn't request a range but server didn't reply with "200 OK". 320 if (!VerifyPartialResponse(response))
293 error = net::ERR_FAILED; 321 error = net::ERR_INVALID_RESPONSE;
294 } 322 } else if (response.httpStatusCode() != kHttpOK) {
323 // We didn't request a range but server didn't reply with "200 OK".
324 error = net::ERR_FAILED;
295 } 325 }
296 326
297 if (error != net::OK) { 327 if (error != net::OK) {
298 DoneStart(error); 328 DoneStart(error);
299 Stop(); 329 Stop();
300 return; 330 return;
301 } 331 }
302 } else { 332 } else {
303 // For any protocol other than HTTP and HTTPS, assume range request is 333 // For any protocol other than HTTP and HTTPS, assume range request is
304 // always fulfilled. 334 // always fulfilled.
305 partial_response_ = range_requested_; 335 partial_response_ = range_requested_;
306 } 336 }
307 337
308 // |info.content_length| can be -1, in that case |content_length_| is 338 // Expected content length can be -1, in that case |content_length_| is
309 // not specified and this is a streaming response. 339 // not specified and this is a streaming response.
310 content_length_ = info.content_length; 340 content_length_ = response.expectedContentLength();
311 341
312 // If we have not requested a range, then the size of the instance is equal 342 // If we have not requested a range, then the size of the instance is equal
313 // to the content length. 343 // to the content length.
314 if (!partial_response_) 344 if (!partial_response_)
315 instance_size_ = content_length_; 345 instance_size_ = content_length_;
316 346
317 // Calls with a successful response. 347 // Calls with a successful response.
318 DoneStart(net::OK); 348 DoneStart(net::OK);
319 } 349 }
320 350
321 void BufferedResourceLoader::OnReceivedData(const char* data, int len) { 351 void BufferedResourceLoader::didReceiveData(
322 DCHECK(bridge_.get()); 352 WebURLLoader* loader,
353 const char* data,
354 int data_length) {
355 DCHECK(!completed_);
356 DCHECK_GT(data_length, 0);
323 357
324 // If this loader has been stopped, |buffer_| would be destroyed. 358 // If this loader has been stopped, |buffer_| would be destroyed.
325 // In this case we shouldn't do anything. 359 // In this case we shouldn't do anything.
326 if (!buffer_.get()) 360 if (!buffer_.get())
327 return; 361 return;
328 362
329 // Writes more data to |buffer_|. 363 // Writes more data to |buffer_|.
330 buffer_->Append(reinterpret_cast<const uint8*>(data), len); 364 buffer_->Append(reinterpret_cast<const uint8*>(data), data_length);
331 365
332 // If there is an active read request, try to fulfill the request. 366 // If there is an active read request, try to fulfill the request.
333 if (HasPendingRead() && CanFulfillRead()) { 367 if (HasPendingRead() && CanFulfillRead()) {
334 ReadInternal(); 368 ReadInternal();
335 } else if (!defer_allowed_) { 369 } else if (!defer_allowed_) {
336 // If we're not allowed to defer, slide the buffer window forward instead 370 // If we're not allowed to defer, slide the buffer window forward instead
337 // of deferring. 371 // of deferring.
338 if (buffer_->forward_bytes() > buffer_->forward_capacity()) { 372 if (buffer_->forward_bytes() > buffer_->forward_capacity()) {
339 size_t excess = buffer_->forward_bytes() - buffer_->forward_capacity(); 373 size_t excess = buffer_->forward_bytes() - buffer_->forward_capacity();
340 bool success = buffer_->Seek(excess); 374 bool success = buffer_->Seek(excess);
341 DCHECK(success); 375 DCHECK(success);
342 offset_ += first_offset_ + excess; 376 offset_ += first_offset_ + excess;
343 } 377 }
344 } 378 }
345 379
346 // At last see if the buffer is full and we need to defer the downloading. 380 // At last see if the buffer is full and we need to defer the downloading.
347 EnableDeferIfNeeded(); 381 EnableDeferIfNeeded();
348 382
349 // Notify that we have received some data. 383 // Notify that we have received some data.
350 NotifyNetworkEvent(); 384 NotifyNetworkEvent();
351 } 385 }
352 386
353 void BufferedResourceLoader::OnCompletedRequest( 387 void BufferedResourceLoader::didDownloadData(
354 const URLRequestStatus& status, 388 WebKit::WebURLLoader* loader,
355 const std::string& security_info, 389 int dataLength) {
356 const base::Time& completion_time) { 390 NOTIMPLEMENTED();
357 DCHECK(bridge_.get()); 391 }
358 392
359 // Saves the information that the request has completed. 393 void BufferedResourceLoader::didReceiveCachedMetadata(
394 WebURLLoader* loader,
395 const char* data,
396 int data_length) {
397 NOTIMPLEMENTED();
398 }
399
400 void BufferedResourceLoader::didFinishLoading(
401 WebURLLoader* loader,
402 double finishTime) {
403 DCHECK(!completed_);
360 completed_ = true; 404 completed_ = true;
361 405
362 // If there is a start callback, calls it. 406 // If there is a start callback, calls it.
363 if (start_callback_.get()) { 407 if (start_callback_.get()) {
364 DoneStart(status.os_error()); 408 DoneStart(net::OK);
365 } 409 }
366 410
367 // If there is a pending read but the request has ended, returns with what 411 // If there is a pending read but the request has ended, returns with what
368 // we have. 412 // we have.
369 if (HasPendingRead()) { 413 if (HasPendingRead()) {
370 // Make sure we have a valid buffer before we satisfy a read request. 414 // Make sure we have a valid buffer before we satisfy a read request.
371 DCHECK(buffer_.get()); 415 DCHECK(buffer_.get());
372 416
373 if (status.is_success()) { 417 // Try to fulfill with what is in the buffer.
374 // Try to fulfill with what is in the buffer. 418 if (CanFulfillRead())
375 if (CanFulfillRead()) 419 ReadInternal();
376 ReadInternal(); 420 else
377 else 421 DoneRead(net::ERR_CACHE_MISS);
378 DoneRead(net::ERR_CACHE_MISS);
379 } else {
380 // If the request has failed, then fail the read.
381 DoneRead(net::ERR_FAILED);
382 }
383 } 422 }
384 423
385 // There must not be any outstanding read request. 424 // There must not be any outstanding read request.
386 DCHECK(!HasPendingRead()); 425 DCHECK(!HasPendingRead());
387 426
388 // Notify that network response is completed. 427 // Notify that network response is completed.
389 NotifyNetworkEvent(); 428 NotifyNetworkEvent();
390 429
391 // We incremented the reference count when the loader was started. We balance 430 url_loader_.reset();
392 // that reference here so that we get destroyed. This is also the only safe
393 // place to destroy the ResourceLoaderBridge.
394 bridge_.reset();
395 Release(); 431 Release();
396 } 432 }
397 433
434 void BufferedResourceLoader::didFail(
435 WebURLLoader* loader,
436 const WebURLError& error) {
437 DCHECK(!completed_);
438 completed_ = true;
439
440 // If there is a start callback, calls it.
441 if (start_callback_.get()) {
442 DoneStart(error.reason);
443 }
444
445 // If there is a pending read but the request failed, return with the
446 // reason for the error.
447 if (HasPendingRead()) {
448 DoneRead(error.reason);
449 }
450
451 // Notify that network response is completed.
452 NotifyNetworkEvent();
453
454 url_loader_.reset();
455 Release();
456 }
457
398 ///////////////////////////////////////////////////////////////////////////// 458 /////////////////////////////////////////////////////////////////////////////
399 // BufferedResourceLoader, private 459 // BufferedResourceLoader, private
400 void BufferedResourceLoader::EnableDeferIfNeeded() { 460 void BufferedResourceLoader::EnableDeferIfNeeded() {
401 if (!defer_allowed_) 461 if (!defer_allowed_)
402 return; 462 return;
403 463
404 if (!deferred_ && 464 if (!deferred_ &&
405 buffer_->forward_bytes() >= buffer_->forward_capacity()) { 465 buffer_->forward_bytes() >= buffer_->forward_capacity()) {
406 deferred_ = true; 466 deferred_ = true;
407 467
408 if (bridge_.get()) 468 if (url_loader_.get())
409 bridge_->SetDefersLoading(true); 469 url_loader_->setDefersLoading(true);
410 470
411 NotifyNetworkEvent(); 471 NotifyNetworkEvent();
412 } 472 }
413 } 473 }
414 474
415 void BufferedResourceLoader::DisableDeferIfNeeded() { 475 void BufferedResourceLoader::DisableDeferIfNeeded() {
416 if (deferred_ && 476 if (deferred_ &&
417 (!defer_allowed_ || 477 (!defer_allowed_ ||
418 buffer_->forward_bytes() < buffer_->forward_capacity() / 2)) { 478 buffer_->forward_bytes() < buffer_->forward_capacity() / 2)) {
419 deferred_ = false; 479 deferred_ = false;
420 480
421 if (bridge_.get()) 481 if (url_loader_.get())
422 bridge_->SetDefersLoading(false); 482 url_loader_->setDefersLoading(false);
423 483
424 NotifyNetworkEvent(); 484 NotifyNetworkEvent();
425 } 485 }
426 } 486 }
427 487
428 bool BufferedResourceLoader::CanFulfillRead() { 488 bool BufferedResourceLoader::CanFulfillRead() {
429 // If we are reading too far in the backward direction. 489 // If we are reading too far in the backward direction.
430 if (first_offset_ < 0 && 490 if (first_offset_ < 0 &&
431 first_offset_ + static_cast<int>(buffer_->backward_bytes()) < 0) 491 first_offset_ + static_cast<int>(buffer_->backward_bytes()) < 0)
432 return false; 492 return false;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 533
474 // Then do the read. 534 // Then do the read.
475 int read = static_cast<int>(buffer_->Read(read_buffer_, read_size_)); 535 int read = static_cast<int>(buffer_->Read(read_buffer_, read_size_));
476 offset_ += first_offset_ + read; 536 offset_ += first_offset_ + read;
477 537
478 // And report with what we have read. 538 // And report with what we have read.
479 DoneRead(read); 539 DoneRead(read);
480 } 540 }
481 541
482 bool BufferedResourceLoader::VerifyPartialResponse( 542 bool BufferedResourceLoader::VerifyPartialResponse(
483 const ResourceResponseInfo& info) { 543 const WebURLResponse& response) {
484 int64 first_byte_position, last_byte_position, instance_size; 544 int first_byte_position, last_byte_position, instance_size;
485 if (!info.headers->GetContentRange(&first_byte_position, 545
486 &last_byte_position, 546 if (!MultipartResponseDelegate::ReadContentRanges(response,
487 &instance_size)) { 547 &first_byte_position,
548 &last_byte_position,
549 &instance_size)) {
488 return false; 550 return false;
489 } 551 }
490 552
491 if (instance_size != kPositionNotSpecified) 553 if (instance_size != kPositionNotSpecified) {
492 instance_size_ = instance_size; 554 instance_size_ = instance_size;
555 }
493 556
494 if (first_byte_position_ != -1 && 557 if (first_byte_position_ != kPositionNotSpecified &&
495 first_byte_position_ != first_byte_position) { 558 first_byte_position_ != first_byte_position) {
496 return false; 559 return false;
497 } 560 }
498 561
499 // TODO(hclam): I should also check |last_byte_position|, but since 562 // TODO(hclam): I should also check |last_byte_position|, but since
500 // we will never make such a request that it is ok to leave it unimplemented. 563 // we will never make such a request that it is ok to leave it unimplemented.
501 return true; 564 return true;
502 } 565 }
503 566
567 std::string BufferedResourceLoader::GenerateHeaders(
568 int64 first_byte_position,
569 int64 last_byte_position) {
570 // Construct the value for the range header.
571 std::string header;
572 if (first_byte_position > kPositionNotSpecified &&
573 last_byte_position > kPositionNotSpecified) {
574 if (first_byte_position <= last_byte_position) {
575 header = base::StringPrintf("bytes=%" PRId64 "-%" PRId64,
576 first_byte_position,
577 last_byte_position);
578 }
579 } else if (first_byte_position > kPositionNotSpecified) {
580 header = base::StringPrintf("bytes=%" PRId64 "-",
581 first_byte_position);
582 } else if (last_byte_position > kPositionNotSpecified) {
583 NOTIMPLEMENTED() << "Suffix range not implemented";
584 }
585 return header;
586 }
587
504 void BufferedResourceLoader::DoneRead(int error) { 588 void BufferedResourceLoader::DoneRead(int error) {
505 read_callback_->RunWithParams(Tuple1<int>(error)); 589 read_callback_->RunWithParams(Tuple1<int>(error));
506 read_callback_.reset(); 590 read_callback_.reset();
507 read_position_ = 0; 591 read_position_ = 0;
508 read_size_ = 0; 592 read_size_ = 0;
509 read_buffer_ = NULL; 593 read_buffer_ = NULL;
510 first_offset_ = 0; 594 first_offset_ = 0;
511 last_offset_ = 0; 595 last_offset_ = 0;
512 } 596 }
513 597
514 void BufferedResourceLoader::DoneStart(int error) { 598 void BufferedResourceLoader::DoneStart(int error) {
515 start_callback_->RunWithParams(Tuple1<int>(error)); 599 start_callback_->RunWithParams(Tuple1<int>(error));
516 start_callback_.reset(); 600 start_callback_.reset();
517 } 601 }
518 602
519 void BufferedResourceLoader::NotifyNetworkEvent() { 603 void BufferedResourceLoader::NotifyNetworkEvent() {
520 if (event_callback_.get()) 604 if (event_callback_.get())
521 event_callback_->Run(); 605 event_callback_->Run();
522 } 606 }
523 607
524 ///////////////////////////////////////////////////////////////////////////// 608 /////////////////////////////////////////////////////////////////////////////
525 // BufferedDataSource 609 // BufferedDataSource
526 BufferedDataSource::BufferedDataSource( 610 BufferedDataSource::BufferedDataSource(
527 MessageLoop* render_loop, 611 MessageLoop* render_loop,
528 webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory) 612 WebFrame* frame)
529 : total_bytes_(kPositionNotSpecified), 613 : total_bytes_(kPositionNotSpecified),
530 loaded_(false), 614 loaded_(false),
531 streaming_(false), 615 streaming_(false),
616 frame_(frame),
532 single_origin_(true), 617 single_origin_(true),
533 bridge_factory_(bridge_factory),
534 loader_(NULL), 618 loader_(NULL),
535 network_activity_(false), 619 network_activity_(false),
536 initialize_callback_(NULL), 620 initialize_callback_(NULL),
537 read_callback_(NULL), 621 read_callback_(NULL),
538 read_position_(0), 622 read_position_(0),
539 read_size_(0), 623 read_size_(0),
540 read_buffer_(NULL), 624 read_buffer_(NULL),
541 read_attempts_(0), 625 read_attempts_(0),
542 intermediate_read_buffer_(new uint8[kInitialReadBufferSize]), 626 intermediate_read_buffer_(new uint8[kInitialReadBufferSize]),
543 intermediate_read_buffer_size_(kInitialReadBufferSize), 627 intermediate_read_buffer_size_(kInitialReadBufferSize),
544 render_loop_(render_loop), 628 render_loop_(render_loop),
545 stop_signal_received_(false), 629 stop_signal_received_(false),
546 stopped_on_render_loop_(false), 630 stopped_on_render_loop_(false),
547 media_is_paused_(true), 631 media_is_paused_(true),
548 using_range_request_(true) { 632 using_range_request_(true) {
549 } 633 }
550 634
551 BufferedDataSource::~BufferedDataSource() { 635 BufferedDataSource::~BufferedDataSource() {
552 } 636 }
553 637
554 // A factory method to create BufferedResourceLoader using the read parameters. 638 // A factory method to create BufferedResourceLoader using the read parameters.
555 // This method can be overrided to inject mock BufferedResourceLoader object 639 // This method can be overrided to inject mock BufferedResourceLoader object
556 // for testing purpose. 640 // for testing purpose.
557 BufferedResourceLoader* BufferedDataSource::CreateResourceLoader( 641 BufferedResourceLoader* BufferedDataSource::CreateResourceLoader(
558 int64 first_byte_position, int64 last_byte_position) { 642 int64 first_byte_position, int64 last_byte_position) {
559 DCHECK(MessageLoop::current() == render_loop_); 643 DCHECK(MessageLoop::current() == render_loop_);
560 644
561 return new BufferedResourceLoader(bridge_factory_.get(), url_, 645 return new BufferedResourceLoader(url_,
562 first_byte_position, 646 first_byte_position,
563 last_byte_position); 647 last_byte_position);
564 } 648 }
565 649
566 // This method simply returns kTimeoutMilliseconds. The purpose of this 650 // This method simply returns kTimeoutMilliseconds. The purpose of this
567 // method is to be overidded so as to provide a different timeout value 651 // method is to be overidded so as to provide a different timeout value
568 // for testing purpose. 652 // for testing purpose.
569 base::TimeDelta BufferedDataSource::GetTimeoutMilliseconds() { 653 base::TimeDelta BufferedDataSource::GetTimeoutMilliseconds() {
570 return base::TimeDelta::FromMilliseconds(kTimeoutMilliseconds); 654 return base::TimeDelta::FromMilliseconds(kTimeoutMilliseconds);
571 } 655 }
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
651 DCHECK(MessageLoop::current() == render_loop_); 735 DCHECK(MessageLoop::current() == render_loop_);
652 return single_origin_; 736 return single_origin_;
653 } 737 }
654 738
655 void BufferedDataSource::Abort() { 739 void BufferedDataSource::Abort() {
656 DCHECK(MessageLoop::current() == render_loop_); 740 DCHECK(MessageLoop::current() == render_loop_);
657 741
658 // If we are told to abort, immediately return from any pending read 742 // If we are told to abort, immediately return from any pending read
659 // with an error. 743 // with an error.
660 if (read_callback_.get()) { 744 if (read_callback_.get()) {
661 {
662 AutoLock auto_lock(lock_); 745 AutoLock auto_lock(lock_);
663 DoneRead_Locked(net::ERR_FAILED); 746 DoneRead_Locked(net::ERR_FAILED);
664 }
665 CleanupTask();
666 } 747 }
748
749 CleanupTask();
750 frame_ = NULL;
667 } 751 }
668 752
669 ///////////////////////////////////////////////////////////////////////////// 753 /////////////////////////////////////////////////////////////////////////////
670 // BufferedDataSource, render thread tasks 754 // BufferedDataSource, render thread tasks
671 void BufferedDataSource::InitializeTask() { 755 void BufferedDataSource::InitializeTask() {
672 DCHECK(MessageLoop::current() == render_loop_); 756 DCHECK(MessageLoop::current() == render_loop_);
673 DCHECK(!loader_.get()); 757 DCHECK(!loader_.get());
674 DCHECK(!stopped_on_render_loop_); 758 DCHECK(!stopped_on_render_loop_);
675 759
676 // Kick starts the watch dog task that will handle connection timeout. 760 // Kick starts the watch dog task that will handle connection timeout.
677 // We run the watch dog 2 times faster the actual timeout so as to catch 761 // We run the watch dog 2 times faster the actual timeout so as to catch
678 // the timeout more accurately. 762 // the timeout more accurately.
679 watch_dog_timer_.Start( 763 watch_dog_timer_.Start(
680 GetTimeoutMilliseconds() / 2, 764 GetTimeoutMilliseconds() / 2,
681 this, 765 this,
682 &BufferedDataSource::WatchDogTask); 766 &BufferedDataSource::WatchDogTask);
683 767
684 if (IsHttpProtocol(url_)) { 768 if (IsHttpProtocol(url_)) {
685 // Fetch only first 1024 bytes as this usually covers the header portion 769 // Fetch only first 1024 bytes as this usually covers the header portion
686 // of a media file that gives enough information about the codecs, etc. 770 // of a media file that gives enough information about the codecs, etc.
687 // This also serve as a probe to determine server capability to serve 771 // This also serve as a probe to determine server capability to serve
688 // range request. 772 // range request.
689 // TODO(hclam): Do some experiments for the best approach. 773 // TODO(hclam): Do some experiments for the best approach.
690 loader_ = CreateResourceLoader(0, 1024); 774 loader_ = CreateResourceLoader(0, 1024);
691 loader_->Start( 775 loader_->Start(
692 NewCallback(this, &BufferedDataSource::HttpInitialStartCallback), 776 NewCallback(this, &BufferedDataSource::HttpInitialStartCallback),
693 NewCallback(this, &BufferedDataSource::NetworkEventCallback)); 777 NewCallback(this, &BufferedDataSource::NetworkEventCallback),
778 frame_);
694 } else { 779 } else {
695 // For all other protocols, assume they support range request. We fetch 780 // For all other protocols, assume they support range request. We fetch
696 // the full range of the resource to obtain the instance size because 781 // the full range of the resource to obtain the instance size because
697 // we won't be served HTTP headers. 782 // we won't be served HTTP headers.
698 loader_ = CreateResourceLoader(-1, -1); 783 loader_ = CreateResourceLoader(-1, -1);
699 loader_->Start( 784 loader_->Start(
700 NewCallback(this, &BufferedDataSource::NonHttpInitialStartCallback), 785 NewCallback(this, &BufferedDataSource::NonHttpInitialStartCallback),
701 NewCallback(this, &BufferedDataSource::NetworkEventCallback)); 786 NewCallback(this, &BufferedDataSource::NetworkEventCallback),
787 frame_);
702 } 788 }
703 } 789 }
704 790
705 void BufferedDataSource::ReadTask( 791 void BufferedDataSource::ReadTask(
706 int64 position, int read_size, uint8* buffer, 792 int64 position, int read_size, uint8* buffer,
707 media::DataSource::ReadCallback* read_callback) { 793 media::DataSource::ReadCallback* read_callback) {
708 DCHECK(MessageLoop::current() == render_loop_); 794 DCHECK(MessageLoop::current() == render_loop_);
709 795
710 // If CleanupTask() was executed we should return immediately. We check this 796 // If CleanupTask() was executed we should return immediately. We check this
711 // variable to prevent doing any actual work after clean up was done. We do 797 // variable to prevent doing any actual work after clean up was done. We do
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
768 return; 854 return;
769 855
770 // If there's no outstanding read then return early. 856 // If there's no outstanding read then return early.
771 if (!read_callback_.get()) 857 if (!read_callback_.get())
772 return; 858 return;
773 859
774 loader_ = CreateResourceLoader(read_position_, -1); 860 loader_ = CreateResourceLoader(read_position_, -1);
775 loader_->SetAllowDefer(!media_is_paused_); 861 loader_->SetAllowDefer(!media_is_paused_);
776 loader_->Start( 862 loader_->Start(
777 NewCallback(this, &BufferedDataSource::PartialReadStartCallback), 863 NewCallback(this, &BufferedDataSource::PartialReadStartCallback),
778 NewCallback(this, &BufferedDataSource::NetworkEventCallback)); 864 NewCallback(this, &BufferedDataSource::NetworkEventCallback),
865 frame_);
779 } 866 }
780 867
781 void BufferedDataSource::WatchDogTask() { 868 void BufferedDataSource::WatchDogTask() {
782 DCHECK(MessageLoop::current() == render_loop_); 869 DCHECK(MessageLoop::current() == render_loop_);
783 DCHECK(!stopped_on_render_loop_); 870 DCHECK(!stopped_on_render_loop_);
784 871
785 // We only care if there is an active read request. 872 // We only care if there is an active read request.
786 if (!read_callback_.get()) 873 if (!read_callback_.get())
787 return; 874 return;
788 875
(...skipping 10 matching lines...) Expand all
799 ++read_attempts_; 886 ++read_attempts_;
800 read_submitted_time_ = base::Time::Now(); 887 read_submitted_time_ = base::Time::Now();
801 888
802 // Stops the current loader and creates a new resource loader and 889 // Stops the current loader and creates a new resource loader and
803 // retry the request. 890 // retry the request.
804 loader_->Stop(); 891 loader_->Stop();
805 loader_ = CreateResourceLoader(read_position_, -1); 892 loader_ = CreateResourceLoader(read_position_, -1);
806 loader_->SetAllowDefer(!media_is_paused_); 893 loader_->SetAllowDefer(!media_is_paused_);
807 loader_->Start( 894 loader_->Start(
808 NewCallback(this, &BufferedDataSource::PartialReadStartCallback), 895 NewCallback(this, &BufferedDataSource::PartialReadStartCallback),
809 NewCallback(this, &BufferedDataSource::NetworkEventCallback)); 896 NewCallback(this, &BufferedDataSource::NetworkEventCallback),
897 frame_);
810 } 898 }
811 899
812 void BufferedDataSource::SetPlaybackRateTask(float playback_rate) { 900 void BufferedDataSource::SetPlaybackRateTask(float playback_rate) {
813 DCHECK(MessageLoop::current() == render_loop_); 901 DCHECK(MessageLoop::current() == render_loop_);
814 DCHECK(loader_.get()); 902 DCHECK(loader_.get());
815 903
816 bool previously_paused = media_is_paused_; 904 bool previously_paused = media_is_paused_;
817 media_is_paused_ = (playback_rate == 0.0); 905 media_is_paused_ = (playback_rate == 0.0);
818 906
819 // Disallow deferring data when we are pausing, allow deferring data 907 // Disallow deferring data when we are pausing, allow deferring data
820 // when we resume playing. 908 // when we resume playing.
821 if (previously_paused && !media_is_paused_) { 909 if (previously_paused && !media_is_paused_) {
822 loader_->SetAllowDefer(true); 910 loader_->SetAllowDefer(true);
823 } else if (!previously_paused && media_is_paused_) { 911 } else if (!previously_paused && media_is_paused_) {
824 loader_->SetAllowDefer(false); 912 loader_->SetAllowDefer(false);
825 } 913 }
826 } 914 }
827 915
828 // This method is the place where actual read happens, |loader_| must be valid 916 // This method is the place where actual read happens, |loader_| must be valid
829 // prior to make this method call. 917 // prior to make this method call.
830 void BufferedDataSource::ReadInternal() { 918 void BufferedDataSource::ReadInternal() {
831 DCHECK(MessageLoop::current() == render_loop_); 919 DCHECK(MessageLoop::current() == render_loop_);
832 DCHECK(loader_.get()); 920 DCHECK(loader_);
833 921
834 // First we prepare the intermediate read buffer for BufferedResourceLoader 922 // First we prepare the intermediate read buffer for BufferedResourceLoader
835 // to write to. 923 // to write to.
836 if (read_size_ > intermediate_read_buffer_size_) { 924 if (read_size_ > intermediate_read_buffer_size_) {
837 intermediate_read_buffer_.reset(new uint8[read_size_]); 925 intermediate_read_buffer_.reset(new uint8[read_size_]);
838 } 926 }
839 927
840 // Perform the actual read with BufferedResourceLoader. 928 // Perform the actual read with BufferedResourceLoader.
841 loader_->Read(read_position_, read_size_, intermediate_read_buffer_.get(), 929 loader_->Read(read_position_, read_size_, intermediate_read_buffer_.get(),
842 NewCallback(this, &BufferedDataSource::ReadCallback)); 930 NewCallback(this, &BufferedDataSource::ReadCallback));
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
897 loader_->Stop(); 985 loader_->Stop();
898 } 986 }
899 987
900 if (error == net::ERR_INVALID_RESPONSE && using_range_request_) { 988 if (error == net::ERR_INVALID_RESPONSE && using_range_request_) {
901 // Assuming that the Range header was causing the problem. Retry without 989 // Assuming that the Range header was causing the problem. Retry without
902 // the Range header. 990 // the Range header.
903 using_range_request_ = false; 991 using_range_request_ = false;
904 loader_ = CreateResourceLoader(-1, -1); 992 loader_ = CreateResourceLoader(-1, -1);
905 loader_->Start( 993 loader_->Start(
906 NewCallback(this, &BufferedDataSource::HttpInitialStartCallback), 994 NewCallback(this, &BufferedDataSource::HttpInitialStartCallback),
907 NewCallback(this, &BufferedDataSource::NetworkEventCallback)); 995 NewCallback(this, &BufferedDataSource::NetworkEventCallback),
996 frame_);
908 return; 997 return;
909 } 998 }
910 999
911 // We need to prevent calling to filter host and running the callback if 1000 // We need to prevent calling to filter host and running the callback if
912 // we have received the stop signal. We need to lock down the whole callback 1001 // we have received the stop signal. We need to lock down the whole callback
913 // method to prevent bad things from happening. The reason behind this is 1002 // method to prevent bad things from happening. The reason behind this is
914 // that we cannot guarantee tasks on render thread have completely stopped 1003 // that we cannot guarantee tasks on render thread have completely stopped
915 // when we receive the Stop() method call. The only way to solve this is to 1004 // when we receive the Stop() method call. The only way to solve this is to
916 // let tasks on render thread to run but make sure they don't call outside 1005 // let tasks on render thread to run but make sure they don't call outside
917 // this object when Stop() method is ever called. Locking this method is safe 1006 // this object when Stop() method is ever called. Locking this method is safe
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
1077 return; 1166 return;
1078 1167
1079 if (network_activity != network_activity_) { 1168 if (network_activity != network_activity_) {
1080 network_activity_ = network_activity; 1169 network_activity_ = network_activity;
1081 host()->SetNetworkActivity(network_activity); 1170 host()->SetNetworkActivity(network_activity);
1082 } 1171 }
1083 host()->SetBufferedBytes(buffered_last_byte_position + 1); 1172 host()->SetBufferedBytes(buffered_last_byte_position + 1);
1084 } 1173 }
1085 1174
1086 } // namespace webkit_glue 1175 } // namespace webkit_glue
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698