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

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: . Created 10 years, 1 month 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/22 21:27:29 do we know if frame should ever be NULL? what's s
annacc 2010/11/24 21:34:44 |frame_| is detached by setting to NULL in Abort()
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(
scherkus (not reviewing) 2010/11/22 21:27:29 watch indentation here
annacc 2010/11/24 21:34:44 Done.
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,
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, const WebURLResponse& response) {
scherkus (not reviewing) 2010/11/22 21:27:29 2nd argument should be dropped to next line
annacc 2010/11/24 21:34:44 Done.
266 300
267 // The loader may have been stopped and |start_callback| is destroyed. 301 // The loader may have been stopped and |start_callback| is destroyed.
268 // In this case we shouldn't do anything. 302 // In this case we shouldn't do anything.
269 if (!start_callback_.get()) 303 if (!start_callback_.get())
270 return; 304 return;
271 305
272 // We make a strong assumption that when we reach here we have either 306 // 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 307 // received a response from HTTP/HTTPS protocol or the request was
274 // successful (in particular range request). So we only verify the partial 308 // successful (in particular range request). So we only verify the partial
275 // response for HTTP and HTTPS protocol. 309 // response for HTTP and HTTPS protocol.
276 if (IsHttpProtocol(url_)) { 310 if (IsHttpProtocol(url_)) {
277 int error = net::OK; 311 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 312
286 if (range_requested_ && partial_response_) { 313 if (response.httpStatusCode() == kHttpPartialContent)
287 // If we have verified the partial response and it is correct, we will 314 partial_response_ = true;
288 // return net::OK. 315
289 if (!VerifyPartialResponse(info)) 316 if (range_requested_ && partial_response_) {
317 // If we have verified the partial response and it is correct, we will
318 // return net::OK.
319 if (!VerifyPartialResponse(response))
290 error = net::ERR_INVALID_RESPONSE; 320 error = net::ERR_INVALID_RESPONSE;
scherkus (not reviewing) 2010/11/22 21:27:29 fix indentation here
annacc 2010/11/24 21:34:44 Done.
291 } else if (info.headers->response_code() != kHttpOK) { 321 } else if (response.httpStatusCode() != kHttpOK) {
292 // We didn't request a range but server didn't reply with "200 OK". 322 // We didn't request a range but server didn't reply with "200 OK".
293 error = net::ERR_FAILED; 323 error = net::ERR_FAILED;
294 }
295 } 324 }
296 325
297 if (error != net::OK) { 326 if (error != net::OK) {
298 DoneStart(error); 327 DoneStart(error);
299 Stop(); 328 Stop();
300 return; 329 return;
301 } 330 }
302 } else { 331 } else {
303 // For any protocol other than HTTP and HTTPS, assume range request is 332 // For any protocol other than HTTP and HTTPS, assume range request is
304 // always fulfilled. 333 // always fulfilled.
305 partial_response_ = range_requested_; 334 partial_response_ = range_requested_;
306 } 335 }
307 336
308 // |info.content_length| can be -1, in that case |content_length_| is 337 // Expected content length can be -1, in that case |content_length_| is
309 // not specified and this is a streaming response. 338 // not specified and this is a streaming response.
310 content_length_ = info.content_length; 339 content_length_ = response.expectedContentLength();
311 340
312 // If we have not requested a range, then the size of the instance is equal 341 // If we have not requested a range, then the size of the instance is equal
313 // to the content length. 342 // to the content length.
314 if (!partial_response_) 343 if (!partial_response_)
315 instance_size_ = content_length_; 344 instance_size_ = content_length_;
316 345
317 // Calls with a successful response. 346 // Calls with a successful response.
318 DoneStart(net::OK); 347 DoneStart(net::OK);
319 } 348 }
320 349
321 void BufferedResourceLoader::OnReceivedData(const char* data, int len) { 350 void BufferedResourceLoader::didReceiveData(
322 DCHECK(bridge_.get()); 351 WebURLLoader* loader, const char* data, int data_length) {
scherkus (not reviewing) 2010/11/22 21:27:29 2nd and 3rd parameters should be dropped to next l
annacc 2010/11/24 21:34:44 Done.
352 DCHECK(!completed_);
353 DCHECK_GT(data_length, 0);
323 354
324 // If this loader has been stopped, |buffer_| would be destroyed. 355 // If this loader has been stopped, |buffer_| would be destroyed.
325 // In this case we shouldn't do anything. 356 // In this case we shouldn't do anything.
326 if (!buffer_.get()) 357 if (!buffer_.get())
327 return; 358 return;
328 359
329 // Writes more data to |buffer_|. 360 // Writes more data to |buffer_|.
330 buffer_->Append(reinterpret_cast<const uint8*>(data), len); 361 buffer_->Append(reinterpret_cast<const uint8*>(data), data_length);
331 362
332 // If there is an active read request, try to fulfill the request. 363 // If there is an active read request, try to fulfill the request.
333 if (HasPendingRead() && CanFulfillRead()) { 364 if (HasPendingRead() && CanFulfillRead()) {
334 ReadInternal(); 365 ReadInternal();
335 } else if (!defer_allowed_) { 366 } else if (!defer_allowed_) {
336 // If we're not allowed to defer, slide the buffer window forward instead 367 // If we're not allowed to defer, slide the buffer window forward instead
337 // of deferring. 368 // of deferring.
338 if (buffer_->forward_bytes() > buffer_->forward_capacity()) { 369 if (buffer_->forward_bytes() > buffer_->forward_capacity()) {
339 size_t excess = buffer_->forward_bytes() - buffer_->forward_capacity(); 370 size_t excess = buffer_->forward_bytes() - buffer_->forward_capacity();
340 bool success = buffer_->Seek(excess); 371 bool success = buffer_->Seek(excess);
341 DCHECK(success); 372 DCHECK(success);
342 offset_ += first_offset_ + excess; 373 offset_ += first_offset_ + excess;
343 } 374 }
344 } 375 }
345 376
346 // At last see if the buffer is full and we need to defer the downloading. 377 // At last see if the buffer is full and we need to defer the downloading.
347 EnableDeferIfNeeded(); 378 EnableDeferIfNeeded();
348 379
349 // Notify that we have received some data. 380 // Notify that we have received some data.
350 NotifyNetworkEvent(); 381 NotifyNetworkEvent();
351 } 382 }
352 383
353 void BufferedResourceLoader::OnCompletedRequest( 384 void BufferedResourceLoader::didDownloadData(
354 const URLRequestStatus& status, 385 WebKit::WebURLLoader* loader, int dataLength) {
scherkus (not reviewing) 2010/11/22 21:27:29 ditto here
annacc 2010/11/24 21:34:44 Done.
355 const std::string& security_info, 386 NOTIMPLEMENTED();
356 const base::Time& completion_time) { 387 }
357 DCHECK(bridge_.get());
358 388
359 // Saves the information that the request has completed. 389 void BufferedResourceLoader::didReceiveCachedMetadata(
390 WebURLLoader* loader, const char* data, int data_length) {
scherkus (not reviewing) 2010/11/22 21:27:29 ditto
annacc 2010/11/24 21:34:44 Done.
391 NOTIMPLEMENTED();
392 }
393
394 void BufferedResourceLoader::didFinishLoading(
395 WebURLLoader* loader, double finishTime) {
scherkus (not reviewing) 2010/11/22 21:27:29 ditto
annacc 2010/11/24 21:34:44 Done.
396 DCHECK(!completed_);
360 completed_ = true; 397 completed_ = true;
361 398
362 // If there is a start callback, calls it. 399 // If there is a start callback, calls it.
363 if (start_callback_.get()) { 400 if (start_callback_.get()) {
364 DoneStart(status.os_error()); 401 DoneStart(net::OK);
365 } 402 }
366 403
367 // If there is a pending read but the request has ended, returns with what 404 // If there is a pending read but the request has ended, returns with what
368 // we have. 405 // we have.
369 if (HasPendingRead()) { 406 if (HasPendingRead()) {
370 // Make sure we have a valid buffer before we satisfy a read request. 407 // Make sure we have a valid buffer before we satisfy a read request.
371 DCHECK(buffer_.get()); 408 DCHECK(buffer_.get());
372 409
373 if (status.is_success()) { 410 // Try to fulfill with what is in the buffer.
374 // Try to fulfill with what is in the buffer. 411 if (CanFulfillRead())
375 if (CanFulfillRead()) 412 ReadInternal();
376 ReadInternal(); 413 else
377 else 414 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 } 415 }
384 416
385 // There must not be any outstanding read request. 417 // There must not be any outstanding read request.
386 DCHECK(!HasPendingRead()); 418 DCHECK(!HasPendingRead());
387 419
388 // Notify that network response is completed. 420 // Notify that network response is completed.
389 NotifyNetworkEvent(); 421 NotifyNetworkEvent();
390 422
391 // We incremented the reference count when the loader was started. We balance 423 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(); 424 Release();
396 } 425 }
397 426
427 void BufferedResourceLoader::didFail(
428 WebURLLoader* loader,
429 const WebURLError& error) {
430 DCHECK(!completed_);
431 completed_ = true;
432
433 // If there is a start callback, calls it.
434 if (start_callback_.get()) {
435 DoneStart(error.reason);
436 }
437
438 // If there is a pending read but the request failed, return with the
439 // reason for the error.
440 if (HasPendingRead()) {
441 DoneRead(error.reason);
442 }
443
444 // Notify that network response is completed.
445 NotifyNetworkEvent();
446
447 url_loader_.reset();
448 Release();
449 }
450
398 ///////////////////////////////////////////////////////////////////////////// 451 /////////////////////////////////////////////////////////////////////////////
399 // BufferedResourceLoader, private 452 // BufferedResourceLoader, private
400 void BufferedResourceLoader::EnableDeferIfNeeded() { 453 void BufferedResourceLoader::EnableDeferIfNeeded() {
401 if (!defer_allowed_) 454 if (!defer_allowed_)
402 return; 455 return;
403 456
404 if (!deferred_ && 457 if (!deferred_ &&
405 buffer_->forward_bytes() >= buffer_->forward_capacity()) { 458 buffer_->forward_bytes() >= buffer_->forward_capacity()) {
406 deferred_ = true; 459 deferred_ = true;
407 460
408 if (bridge_.get()) 461 if (url_loader_.get())
409 bridge_->SetDefersLoading(true); 462 url_loader_->setDefersLoading(true);
410 463
411 NotifyNetworkEvent(); 464 NotifyNetworkEvent();
412 } 465 }
413 } 466 }
414 467
415 void BufferedResourceLoader::DisableDeferIfNeeded() { 468 void BufferedResourceLoader::DisableDeferIfNeeded() {
416 if (deferred_ && 469 if (deferred_ &&
417 (!defer_allowed_ || 470 (!defer_allowed_ ||
418 buffer_->forward_bytes() < buffer_->forward_capacity() / 2)) { 471 buffer_->forward_bytes() < buffer_->forward_capacity() / 2)) {
419 deferred_ = false; 472 deferred_ = false;
420 473
421 if (bridge_.get()) 474 if (url_loader_.get())
422 bridge_->SetDefersLoading(false); 475 url_loader_->setDefersLoading(false);
423 476
424 NotifyNetworkEvent(); 477 NotifyNetworkEvent();
425 } 478 }
426 } 479 }
427 480
428 bool BufferedResourceLoader::CanFulfillRead() { 481 bool BufferedResourceLoader::CanFulfillRead() {
429 // If we are reading too far in the backward direction. 482 // If we are reading too far in the backward direction.
430 if (first_offset_ < 0 && 483 if (first_offset_ < 0 &&
431 first_offset_ + static_cast<int>(buffer_->backward_bytes()) < 0) 484 first_offset_ + static_cast<int>(buffer_->backward_bytes()) < 0)
432 return false; 485 return false;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 526
474 // Then do the read. 527 // Then do the read.
475 int read = static_cast<int>(buffer_->Read(read_buffer_, read_size_)); 528 int read = static_cast<int>(buffer_->Read(read_buffer_, read_size_));
476 offset_ += first_offset_ + read; 529 offset_ += first_offset_ + read;
477 530
478 // And report with what we have read. 531 // And report with what we have read.
479 DoneRead(read); 532 DoneRead(read);
480 } 533 }
481 534
482 bool BufferedResourceLoader::VerifyPartialResponse( 535 bool BufferedResourceLoader::VerifyPartialResponse(
483 const ResourceResponseInfo& info) { 536 const WebURLResponse& response) {
484 int64 first_byte_position, last_byte_position, instance_size; 537 int first_byte_position, last_byte_position, instance_size;
485 if (!info.headers->GetContentRange(&first_byte_position, 538
486 &last_byte_position, 539 if (!MultipartResponseDelegate::ReadContentRanges(response,
487 &instance_size)) { 540 &first_byte_position,
541 &last_byte_position,
542 &instance_size)) {
488 return false; 543 return false;
489 } 544 }
490 545
491 if (instance_size != kPositionNotSpecified) 546 if (instance_size != kPositionNotSpecified) {
492 instance_size_ = instance_size; 547 instance_size_ = instance_size;
548 }
493 549
494 if (first_byte_position_ != -1 && 550 if (first_byte_position_ != kPositionNotSpecified &&
495 first_byte_position_ != first_byte_position) { 551 first_byte_position_ != first_byte_position) {
496 return false; 552 return false;
497 } 553 }
498 554
499 // TODO(hclam): I should also check |last_byte_position|, but since 555 // 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. 556 // we will never make such a request that it is ok to leave it unimplemented.
501 return true; 557 return true;
502 } 558 }
503 559
560 std::string BufferedResourceLoader::GenerateHeaders(
561 int64 first_byte_position, int64 last_byte_position) {
scherkus (not reviewing) 2010/11/22 21:27:29 ditto
annacc 2010/11/24 21:34:44 Done.
562 // Construct the value for the range header.
563 std::string header;
564 if (first_byte_position > kPositionNotSpecified &&
565 last_byte_position > kPositionNotSpecified) {
566 if (first_byte_position <= last_byte_position) {
567 header = base::StringPrintf("bytes=%" PRId64 "-%" PRId64,
568 first_byte_position,
569 last_byte_position);
570 }
571 } else if (first_byte_position > kPositionNotSpecified) {
572 header = base::StringPrintf("bytes=%" PRId64 "-",
573 first_byte_position);
574 } else if (last_byte_position > kPositionNotSpecified) {
575 NOTIMPLEMENTED() << "Suffix range not implemented";
576 }
577 return header;
578 }
579
504 void BufferedResourceLoader::DoneRead(int error) { 580 void BufferedResourceLoader::DoneRead(int error) {
505 read_callback_->RunWithParams(Tuple1<int>(error)); 581 read_callback_->RunWithParams(Tuple1<int>(error));
506 read_callback_.reset(); 582 read_callback_.reset();
507 read_position_ = 0; 583 read_position_ = 0;
508 read_size_ = 0; 584 read_size_ = 0;
509 read_buffer_ = NULL; 585 read_buffer_ = NULL;
510 first_offset_ = 0; 586 first_offset_ = 0;
511 last_offset_ = 0; 587 last_offset_ = 0;
512 } 588 }
513 589
514 void BufferedResourceLoader::DoneStart(int error) { 590 void BufferedResourceLoader::DoneStart(int error) {
515 start_callback_->RunWithParams(Tuple1<int>(error)); 591 start_callback_->RunWithParams(Tuple1<int>(error));
516 start_callback_.reset(); 592 start_callback_.reset();
517 } 593 }
518 594
519 void BufferedResourceLoader::NotifyNetworkEvent() { 595 void BufferedResourceLoader::NotifyNetworkEvent() {
520 if (event_callback_.get()) 596 if (event_callback_.get())
521 event_callback_->Run(); 597 event_callback_->Run();
522 } 598 }
523 599
524 ///////////////////////////////////////////////////////////////////////////// 600 /////////////////////////////////////////////////////////////////////////////
525 // BufferedDataSource 601 // BufferedDataSource
526 BufferedDataSource::BufferedDataSource( 602 BufferedDataSource::BufferedDataSource(
527 MessageLoop* render_loop, 603 MessageLoop* render_loop,
528 webkit_glue::MediaResourceLoaderBridgeFactory* bridge_factory) 604 WebFrame* frame)
529 : total_bytes_(kPositionNotSpecified), 605 : total_bytes_(kPositionNotSpecified),
530 loaded_(false), 606 loaded_(false),
531 streaming_(false), 607 streaming_(false),
608 frame_(frame),
532 single_origin_(true), 609 single_origin_(true),
533 bridge_factory_(bridge_factory),
534 loader_(NULL), 610 loader_(NULL),
535 network_activity_(false), 611 network_activity_(false),
536 initialize_callback_(NULL), 612 initialize_callback_(NULL),
537 read_callback_(NULL), 613 read_callback_(NULL),
538 read_position_(0), 614 read_position_(0),
539 read_size_(0), 615 read_size_(0),
540 read_buffer_(NULL), 616 read_buffer_(NULL),
541 read_attempts_(0), 617 read_attempts_(0),
542 intermediate_read_buffer_(new uint8[kInitialReadBufferSize]), 618 intermediate_read_buffer_(new uint8[kInitialReadBufferSize]),
543 intermediate_read_buffer_size_(kInitialReadBufferSize), 619 intermediate_read_buffer_size_(kInitialReadBufferSize),
544 render_loop_(render_loop), 620 render_loop_(render_loop),
545 stop_signal_received_(false), 621 stop_signal_received_(false),
546 stopped_on_render_loop_(false), 622 stopped_on_render_loop_(false),
547 media_is_paused_(true), 623 media_is_paused_(true),
548 using_range_request_(true) { 624 using_range_request_(true) {
549 } 625 }
550 626
551 BufferedDataSource::~BufferedDataSource() { 627 BufferedDataSource::~BufferedDataSource() {
552 } 628 }
553 629
554 // A factory method to create BufferedResourceLoader using the read parameters. 630 // A factory method to create BufferedResourceLoader using the read parameters.
555 // This method can be overrided to inject mock BufferedResourceLoader object 631 // This method can be overrided to inject mock BufferedResourceLoader object
556 // for testing purpose. 632 // for testing purpose.
557 BufferedResourceLoader* BufferedDataSource::CreateResourceLoader( 633 BufferedResourceLoader* BufferedDataSource::CreateResourceLoader(
558 int64 first_byte_position, int64 last_byte_position) { 634 int64 first_byte_position, int64 last_byte_position) {
559 DCHECK(MessageLoop::current() == render_loop_); 635 DCHECK(MessageLoop::current() == render_loop_);
560 636
561 return new BufferedResourceLoader(bridge_factory_.get(), url_, 637 return new BufferedResourceLoader(url_,
562 first_byte_position, 638 first_byte_position,
563 last_byte_position); 639 last_byte_position);
564 } 640 }
565 641
566 // This method simply returns kTimeoutMilliseconds. The purpose of this 642 // This method simply returns kTimeoutMilliseconds. The purpose of this
567 // method is to be overidded so as to provide a different timeout value 643 // method is to be overidded so as to provide a different timeout value
568 // for testing purpose. 644 // for testing purpose.
569 base::TimeDelta BufferedDataSource::GetTimeoutMilliseconds() { 645 base::TimeDelta BufferedDataSource::GetTimeoutMilliseconds() {
570 return base::TimeDelta::FromMilliseconds(kTimeoutMilliseconds); 646 return base::TimeDelta::FromMilliseconds(kTimeoutMilliseconds);
571 } 647 }
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
651 DCHECK(MessageLoop::current() == render_loop_); 727 DCHECK(MessageLoop::current() == render_loop_);
652 return single_origin_; 728 return single_origin_;
653 } 729 }
654 730
655 void BufferedDataSource::Abort() { 731 void BufferedDataSource::Abort() {
656 DCHECK(MessageLoop::current() == render_loop_); 732 DCHECK(MessageLoop::current() == render_loop_);
657 733
658 // If we are told to abort, immediately return from any pending read 734 // If we are told to abort, immediately return from any pending read
659 // with an error. 735 // with an error.
660 if (read_callback_.get()) { 736 if (read_callback_.get()) {
661 {
662 AutoLock auto_lock(lock_); 737 AutoLock auto_lock(lock_);
663 DoneRead_Locked(net::ERR_FAILED); 738 DoneRead_Locked(net::ERR_FAILED);
664 }
665 CleanupTask();
666 } 739 }
740
741 CleanupTask();
742 frame_ = NULL;
667 } 743 }
668 744
669 ///////////////////////////////////////////////////////////////////////////// 745 /////////////////////////////////////////////////////////////////////////////
670 // BufferedDataSource, render thread tasks 746 // BufferedDataSource, render thread tasks
671 void BufferedDataSource::InitializeTask() { 747 void BufferedDataSource::InitializeTask() {
672 DCHECK(MessageLoop::current() == render_loop_); 748 DCHECK(MessageLoop::current() == render_loop_);
673 DCHECK(!loader_.get()); 749 DCHECK(!loader_.get());
674 DCHECK(!stopped_on_render_loop_); 750 DCHECK(!stopped_on_render_loop_);
675 751
676 // Kick starts the watch dog task that will handle connection timeout. 752 // 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 753 // We run the watch dog 2 times faster the actual timeout so as to catch
678 // the timeout more accurately. 754 // the timeout more accurately.
679 watch_dog_timer_.Start( 755 watch_dog_timer_.Start(
680 GetTimeoutMilliseconds() / 2, 756 GetTimeoutMilliseconds() / 2,
681 this, 757 this,
682 &BufferedDataSource::WatchDogTask); 758 &BufferedDataSource::WatchDogTask);
683 759
684 if (IsHttpProtocol(url_)) { 760 if (IsHttpProtocol(url_)) {
685 // Fetch only first 1024 bytes as this usually covers the header portion 761 // 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. 762 // 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 763 // This also serve as a probe to determine server capability to serve
688 // range request. 764 // range request.
689 // TODO(hclam): Do some experiments for the best approach. 765 // TODO(hclam): Do some experiments for the best approach.
690 loader_ = CreateResourceLoader(0, 1024); 766 loader_ = CreateResourceLoader(0, 1024);
691 loader_->Start( 767 loader_->Start(
692 NewCallback(this, &BufferedDataSource::HttpInitialStartCallback), 768 NewCallback(this, &BufferedDataSource::HttpInitialStartCallback),
693 NewCallback(this, &BufferedDataSource::NetworkEventCallback)); 769 NewCallback(this, &BufferedDataSource::NetworkEventCallback),
770 frame_);
694 } else { 771 } else {
695 // For all other protocols, assume they support range request. We fetch 772 // For all other protocols, assume they support range request. We fetch
696 // the full range of the resource to obtain the instance size because 773 // the full range of the resource to obtain the instance size because
697 // we won't be served HTTP headers. 774 // we won't be served HTTP headers.
698 loader_ = CreateResourceLoader(-1, -1); 775 loader_ = CreateResourceLoader(-1, -1);
699 loader_->Start( 776 loader_->Start(
700 NewCallback(this, &BufferedDataSource::NonHttpInitialStartCallback), 777 NewCallback(this, &BufferedDataSource::NonHttpInitialStartCallback),
701 NewCallback(this, &BufferedDataSource::NetworkEventCallback)); 778 NewCallback(this, &BufferedDataSource::NetworkEventCallback),
779 frame_);
702 } 780 }
703 } 781 }
704 782
705 void BufferedDataSource::ReadTask( 783 void BufferedDataSource::ReadTask(
706 int64 position, int read_size, uint8* buffer, 784 int64 position, int read_size, uint8* buffer,
707 media::DataSource::ReadCallback* read_callback) { 785 media::DataSource::ReadCallback* read_callback) {
708 DCHECK(MessageLoop::current() == render_loop_); 786 DCHECK(MessageLoop::current() == render_loop_);
709 787
710 // If CleanupTask() was executed we should return immediately. We check this 788 // 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 789 // 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; 846 return;
769 847
770 // If there's no outstanding read then return early. 848 // If there's no outstanding read then return early.
771 if (!read_callback_.get()) 849 if (!read_callback_.get())
772 return; 850 return;
773 851
774 loader_ = CreateResourceLoader(read_position_, -1); 852 loader_ = CreateResourceLoader(read_position_, -1);
775 loader_->SetAllowDefer(!media_is_paused_); 853 loader_->SetAllowDefer(!media_is_paused_);
776 loader_->Start( 854 loader_->Start(
777 NewCallback(this, &BufferedDataSource::PartialReadStartCallback), 855 NewCallback(this, &BufferedDataSource::PartialReadStartCallback),
778 NewCallback(this, &BufferedDataSource::NetworkEventCallback)); 856 NewCallback(this, &BufferedDataSource::NetworkEventCallback),
857 frame_);
779 } 858 }
780 859
781 void BufferedDataSource::WatchDogTask() { 860 void BufferedDataSource::WatchDogTask() {
782 DCHECK(MessageLoop::current() == render_loop_); 861 DCHECK(MessageLoop::current() == render_loop_);
783 DCHECK(!stopped_on_render_loop_); 862 DCHECK(!stopped_on_render_loop_);
784 863
785 // We only care if there is an active read request. 864 // We only care if there is an active read request.
786 if (!read_callback_.get()) 865 if (!read_callback_.get())
787 return; 866 return;
788 867
(...skipping 10 matching lines...) Expand all
799 ++read_attempts_; 878 ++read_attempts_;
800 read_submitted_time_ = base::Time::Now(); 879 read_submitted_time_ = base::Time::Now();
801 880
802 // Stops the current loader and creates a new resource loader and 881 // Stops the current loader and creates a new resource loader and
803 // retry the request. 882 // retry the request.
804 loader_->Stop(); 883 loader_->Stop();
805 loader_ = CreateResourceLoader(read_position_, -1); 884 loader_ = CreateResourceLoader(read_position_, -1);
806 loader_->SetAllowDefer(!media_is_paused_); 885 loader_->SetAllowDefer(!media_is_paused_);
807 loader_->Start( 886 loader_->Start(
808 NewCallback(this, &BufferedDataSource::PartialReadStartCallback), 887 NewCallback(this, &BufferedDataSource::PartialReadStartCallback),
809 NewCallback(this, &BufferedDataSource::NetworkEventCallback)); 888 NewCallback(this, &BufferedDataSource::NetworkEventCallback),
889 frame_);
810 } 890 }
811 891
812 void BufferedDataSource::SetPlaybackRateTask(float playback_rate) { 892 void BufferedDataSource::SetPlaybackRateTask(float playback_rate) {
813 DCHECK(MessageLoop::current() == render_loop_); 893 DCHECK(MessageLoop::current() == render_loop_);
814 DCHECK(loader_.get()); 894 DCHECK(loader_.get());
815 895
816 bool previously_paused = media_is_paused_; 896 bool previously_paused = media_is_paused_;
817 media_is_paused_ = (playback_rate == 0.0); 897 media_is_paused_ = (playback_rate == 0.0);
818 898
819 // Disallow deferring data when we are pausing, allow deferring data 899 // Disallow deferring data when we are pausing, allow deferring data
820 // when we resume playing. 900 // when we resume playing.
821 if (previously_paused && !media_is_paused_) { 901 if (previously_paused && !media_is_paused_) {
822 loader_->SetAllowDefer(true); 902 loader_->SetAllowDefer(true);
823 } else if (!previously_paused && media_is_paused_) { 903 } else if (!previously_paused && media_is_paused_) {
824 loader_->SetAllowDefer(false); 904 loader_->SetAllowDefer(false);
825 } 905 }
826 } 906 }
827 907
828 // This method is the place where actual read happens, |loader_| must be valid 908 // This method is the place where actual read happens, |loader_| must be valid
829 // prior to make this method call. 909 // prior to make this method call.
830 void BufferedDataSource::ReadInternal() { 910 void BufferedDataSource::ReadInternal() {
831 DCHECK(MessageLoop::current() == render_loop_); 911 DCHECK(MessageLoop::current() == render_loop_);
832 DCHECK(loader_.get()); 912 DCHECK(loader_);
833 913
834 // First we prepare the intermediate read buffer for BufferedResourceLoader 914 // First we prepare the intermediate read buffer for BufferedResourceLoader
835 // to write to. 915 // to write to.
836 if (read_size_ > intermediate_read_buffer_size_) { 916 if (read_size_ > intermediate_read_buffer_size_) {
837 intermediate_read_buffer_.reset(new uint8[read_size_]); 917 intermediate_read_buffer_.reset(new uint8[read_size_]);
838 } 918 }
839 919
840 // Perform the actual read with BufferedResourceLoader. 920 // Perform the actual read with BufferedResourceLoader.
841 loader_->Read(read_position_, read_size_, intermediate_read_buffer_.get(), 921 loader_->Read(read_position_, read_size_, intermediate_read_buffer_.get(),
842 NewCallback(this, &BufferedDataSource::ReadCallback)); 922 NewCallback(this, &BufferedDataSource::ReadCallback));
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
897 loader_->Stop(); 977 loader_->Stop();
898 } 978 }
899 979
900 if (error == net::ERR_INVALID_RESPONSE && using_range_request_) { 980 if (error == net::ERR_INVALID_RESPONSE && using_range_request_) {
901 // Assuming that the Range header was causing the problem. Retry without 981 // Assuming that the Range header was causing the problem. Retry without
902 // the Range header. 982 // the Range header.
903 using_range_request_ = false; 983 using_range_request_ = false;
904 loader_ = CreateResourceLoader(-1, -1); 984 loader_ = CreateResourceLoader(-1, -1);
905 loader_->Start( 985 loader_->Start(
906 NewCallback(this, &BufferedDataSource::HttpInitialStartCallback), 986 NewCallback(this, &BufferedDataSource::HttpInitialStartCallback),
907 NewCallback(this, &BufferedDataSource::NetworkEventCallback)); 987 NewCallback(this, &BufferedDataSource::NetworkEventCallback),
988 frame_);
908 return; 989 return;
909 } 990 }
910 991
911 // We need to prevent calling to filter host and running the callback if 992 // 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 993 // 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 994 // method to prevent bad things from happening. The reason behind this is
914 // that we cannot guarantee tasks on render thread have completely stopped 995 // 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 996 // 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 997 // 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 998 // 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; 1158 return;
1078 1159
1079 if (network_activity != network_activity_) { 1160 if (network_activity != network_activity_) {
1080 network_activity_ = network_activity; 1161 network_activity_ = network_activity;
1081 host()->SetNetworkActivity(network_activity); 1162 host()->SetNetworkActivity(network_activity);
1082 } 1163 }
1083 host()->SetBufferedBytes(buffered_last_byte_position + 1); 1164 host()->SetBufferedBytes(buffered_last_byte_position + 1);
1084 } 1165 }
1085 1166
1086 } // namespace webkit_glue 1167 } // namespace webkit_glue
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698