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

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

Powered by Google App Engine
This is Rietveld 408576698