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

Side by Side Diff: media/blink/resource_multibuffer_data_provider.cc

Issue 2399463007: AssociatedURLLoader shouldn't derive from WebURLLoader (Closed)
Patch Set: Rebase Created 4 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "media/blink/resource_multibuffer_data_provider.h" 5 #include "media/blink/resource_multibuffer_data_provider.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/bits.h" 11 #include "base/bits.h"
12 #include "base/callback_helpers.h" 12 #include "base/callback_helpers.h"
13 #include "base/location.h" 13 #include "base/location.h"
14 #include "base/metrics/histogram.h" 14 #include "base/metrics/histogram.h"
15 #include "base/single_thread_task_runner.h" 15 #include "base/single_thread_task_runner.h"
16 #include "base/strings/string_number_conversions.h" 16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_util.h" 17 #include "base/strings/string_util.h"
18 #include "base/threading/thread_task_runner_handle.h" 18 #include "base/threading/thread_task_runner_handle.h"
19 #include "media/blink/active_loader.h" 19 #include "media/blink/active_loader.h"
20 #include "media/blink/cache_util.h" 20 #include "media/blink/cache_util.h"
21 #include "media/blink/media_blink_export.h" 21 #include "media/blink/media_blink_export.h"
22 #include "media/blink/url_index.h" 22 #include "media/blink/url_index.h"
23 #include "net/http/http_byte_range.h" 23 #include "net/http/http_byte_range.h"
24 #include "net/http/http_request_headers.h" 24 #include "net/http/http_request_headers.h"
25 #include "third_party/WebKit/public/platform/WebURLError.h" 25 #include "third_party/WebKit/public/platform/WebURLError.h"
26 #include "third_party/WebKit/public/platform/WebURLResponse.h" 26 #include "third_party/WebKit/public/platform/WebURLResponse.h"
27 #include "third_party/WebKit/public/web/WebAssociatedURLLoader.h"
27 28
29 using blink::WebAssociatedURLLoader;
30 using blink::WebAssociatedURLLoaderOptions;
28 using blink::WebFrame; 31 using blink::WebFrame;
29 using blink::WebString; 32 using blink::WebString;
30 using blink::WebURLError; 33 using blink::WebURLError;
31 using blink::WebURLLoader;
32 using blink::WebURLLoaderOptions;
33 using blink::WebURLRequest; 34 using blink::WebURLRequest;
34 using blink::WebURLResponse; 35 using blink::WebURLResponse;
35 36
36 namespace media { 37 namespace media {
37 38
38 // The number of milliseconds to wait before retrying a failed load. 39 // The number of milliseconds to wait before retrying a failed load.
39 const int kLoaderFailedRetryDelayMs = 250; 40 const int kLoaderFailedRetryDelayMs = 250;
40 41
41 // Each retry, add this many MS to the delay. 42 // Each retry, add this many MS to the delay.
42 // total delay is: 43 // total delay is:
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 WebString::fromUTF8(url_data_->etag())); 90 WebString::fromUTF8(url_data_->etag()));
90 } 91 }
91 92
92 url_data_->frame()->setReferrerForRequest(request, blink::WebURL()); 93 url_data_->frame()->setReferrerForRequest(request, blink::WebURL());
93 94
94 // Disable compression, compression for audio/video doesn't make sense... 95 // Disable compression, compression for audio/video doesn't make sense...
95 request.setHTTPHeaderField( 96 request.setHTTPHeaderField(
96 WebString::fromUTF8(net::HttpRequestHeaders::kAcceptEncoding), 97 WebString::fromUTF8(net::HttpRequestHeaders::kAcceptEncoding),
97 WebString::fromUTF8("identity;q=1, *;q=0")); 98 WebString::fromUTF8("identity;q=1, *;q=0"));
98 99
99 // Check for our test WebURLLoader. 100 // Check for our test WebAssociatedURLLoader.
100 std::unique_ptr<WebURLLoader> loader; 101 std::unique_ptr<WebAssociatedURLLoader> loader;
101 if (test_loader_) { 102 if (test_loader_) {
102 loader = std::move(test_loader_); 103 loader = std::move(test_loader_);
103 } else { 104 } else {
104 WebURLLoaderOptions options; 105 WebAssociatedURLLoaderOptions options;
105 if (url_data_->cors_mode() == UrlData::CORS_UNSPECIFIED) { 106 if (url_data_->cors_mode() == UrlData::CORS_UNSPECIFIED) {
106 options.allowCredentials = true; 107 options.allowCredentials = true;
107 options.crossOriginRequestPolicy = 108 options.crossOriginRequestPolicy =
108 WebURLLoaderOptions::CrossOriginRequestPolicyAllow; 109 WebAssociatedURLLoaderOptions::CrossOriginRequestPolicyAllow;
109 } else { 110 } else {
110 options.exposeAllResponseHeaders = true; 111 options.exposeAllResponseHeaders = true;
111 // The author header set is empty, no preflight should go ahead. 112 // The author header set is empty, no preflight should go ahead.
112 options.preflightPolicy = WebURLLoaderOptions::PreventPreflight; 113 options.preflightPolicy = WebAssociatedURLLoaderOptions::PreventPreflight;
113 options.crossOriginRequestPolicy = 114 options.crossOriginRequestPolicy = WebAssociatedURLLoaderOptions::
114 WebURLLoaderOptions::CrossOriginRequestPolicyUseAccessControl; 115 CrossOriginRequestPolicyUseAccessControl;
115 if (url_data_->cors_mode() == UrlData::CORS_USE_CREDENTIALS) 116 if (url_data_->cors_mode() == UrlData::CORS_USE_CREDENTIALS)
116 options.allowCredentials = true; 117 options.allowCredentials = true;
117 } 118 }
118 loader.reset(url_data_->frame()->createAssociatedURLLoader(options)); 119 loader.reset(url_data_->frame()->createAssociatedURLLoader(options));
119 } 120 }
120 121
121 // Start the resource loading. 122 // Start the resource loading.
122 loader->loadAsynchronously(request, this); 123 loader->loadAsynchronously(request, this);
123 active_loader_.reset(new ActiveLoader(std::move(loader))); 124 active_loader_.reset(new ActiveLoader(std::move(loader)));
124 } 125 }
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 return ret; 160 return ret;
160 } 161 }
161 162
162 void ResourceMultiBufferDataProvider::SetDeferred(bool deferred) { 163 void ResourceMultiBufferDataProvider::SetDeferred(bool deferred) {
163 if (!active_loader_ || active_loader_->deferred() == deferred) 164 if (!active_loader_ || active_loader_->deferred() == deferred)
164 return; 165 return;
165 active_loader_->SetDeferred(deferred); 166 active_loader_->SetDeferred(deferred);
166 } 167 }
167 168
168 ///////////////////////////////////////////////////////////////////////////// 169 /////////////////////////////////////////////////////////////////////////////
169 // WebURLLoaderClient implementation. 170 // WebAssociatedURLLoaderClient implementation.
170 171
171 bool ResourceMultiBufferDataProvider::willFollowRedirect( 172 bool ResourceMultiBufferDataProvider::willFollowRedirect(
172 WebURLLoader* loader, 173 const WebURLRequest& newRequest,
173 WebURLRequest& newRequest,
174 const WebURLResponse& redirectResponse) { 174 const WebURLResponse& redirectResponse) {
175 redirects_to_ = newRequest.url(); 175 redirects_to_ = newRequest.url();
176 url_data_->set_valid_until(base::Time::Now() + 176 url_data_->set_valid_until(base::Time::Now() +
177 GetCacheValidUntil(redirectResponse)); 177 GetCacheValidUntil(redirectResponse));
178 178
179 // This test is vital for security! 179 // This test is vital for security!
180 if (cors_mode_ == UrlData::CORS_UNSPECIFIED) { 180 if (cors_mode_ == UrlData::CORS_UNSPECIFIED) {
181 // We allow the redirect if the origin is the same. 181 // We allow the redirect if the origin is the same.
182 if (origin_ != redirects_to_.GetOrigin()) { 182 if (origin_ != redirects_to_.GetOrigin()) {
183 // We also allow the redirect if we don't have any data in the 183 // We also allow the redirect if we don't have any data in the
184 // cache, as that means that no dangerous data mixing can occur. 184 // cache, as that means that no dangerous data mixing can occur.
185 if (url_data_->multibuffer()->map().empty() && fifo_.empty()) 185 if (url_data_->multibuffer()->map().empty() && fifo_.empty())
186 return true; 186 return true;
187 187
188 active_loader_ = nullptr; 188 active_loader_ = nullptr;
189 url_data_->Fail(); 189 url_data_->Fail();
190 return false; // "this" may be deleted now. 190 return false; // "this" may be deleted now.
191 } 191 }
192 } 192 }
193 return true; 193 return true;
194 } 194 }
195 195
196 void ResourceMultiBufferDataProvider::didSendData( 196 void ResourceMultiBufferDataProvider::didSendData(
197 WebURLLoader* loader,
198 unsigned long long bytes_sent, 197 unsigned long long bytes_sent,
199 unsigned long long total_bytes_to_be_sent) { 198 unsigned long long total_bytes_to_be_sent) {
200 NOTIMPLEMENTED(); 199 NOTIMPLEMENTED();
201 } 200 }
202 201
203 void ResourceMultiBufferDataProvider::didReceiveResponse( 202 void ResourceMultiBufferDataProvider::didReceiveResponse(
204 WebURLLoader* loader,
205 const WebURLResponse& response) { 203 const WebURLResponse& response) {
206 #if ENABLE_DLOG 204 #if ENABLE_DLOG
207 string version; 205 string version;
208 switch (response.httpVersion()) { 206 switch (response.httpVersion()) {
209 case WebURLResponse::HTTPVersion_0_9: 207 case WebURLResponse::HTTPVersion_0_9:
210 version = "0.9"; 208 version = "0.9";
211 break; 209 break;
212 case WebURLResponse::HTTPVersion_1_0: 210 case WebURLResponse::HTTPVersion_1_0:
213 version = "1.0"; 211 version = "1.0";
214 break; 212 break;
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 url_data_->Fail(); 349 url_data_->Fail();
352 return; // "this" may be deleted now. 350 return; // "this" may be deleted now.
353 } 351 }
354 352
355 if (end_of_file) { 353 if (end_of_file) {
356 fifo_.push_back(DataBuffer::CreateEOSBuffer()); 354 fifo_.push_back(DataBuffer::CreateEOSBuffer());
357 url_data_->multibuffer()->OnDataProviderEvent(this); 355 url_data_->multibuffer()->OnDataProviderEvent(this);
358 } 356 }
359 } 357 }
360 358
361 void ResourceMultiBufferDataProvider::didReceiveData(WebURLLoader* loader, 359 void ResourceMultiBufferDataProvider::didReceiveData(const char* data,
362 const char* data, 360 int data_length) {
363 int data_length,
364 int encoded_data_length,
365 int encoded_body_length) {
366 DVLOG(1) << "didReceiveData: " << data_length << " bytes"; 361 DVLOG(1) << "didReceiveData: " << data_length << " bytes";
367 DCHECK(!Available()); 362 DCHECK(!Available());
368 DCHECK(active_loader_); 363 DCHECK(active_loader_);
369 DCHECK_GT(data_length, 0); 364 DCHECK_GT(data_length, 0);
370 365
371 // When we receive data, we allow more retries. 366 // When we receive data, we allow more retries.
372 retries_ = 0; 367 retries_ = 0;
373 368
374 while (data_length) { 369 while (data_length) {
375 if (fifo_.empty() || fifo_.back()->data_size() == block_size()) { 370 if (fifo_.empty() || fifo_.back()->data_size() == block_size()) {
376 fifo_.push_back(new DataBuffer(block_size())); 371 fifo_.push_back(new DataBuffer(block_size()));
377 fifo_.back()->set_data_size(0); 372 fifo_.back()->set_data_size(0);
378 } 373 }
379 int last_block_size = fifo_.back()->data_size(); 374 int last_block_size = fifo_.back()->data_size();
380 int to_append = std::min<int>(data_length, block_size() - last_block_size); 375 int to_append = std::min<int>(data_length, block_size() - last_block_size);
381 DCHECK_GT(to_append, 0); 376 DCHECK_GT(to_append, 0);
382 memcpy(fifo_.back()->writable_data() + last_block_size, data, to_append); 377 memcpy(fifo_.back()->writable_data() + last_block_size, data, to_append);
383 data += to_append; 378 data += to_append;
384 fifo_.back()->set_data_size(last_block_size + to_append); 379 fifo_.back()->set_data_size(last_block_size + to_append);
385 data_length -= to_append; 380 data_length -= to_append;
386 } 381 }
387 382
388 url_data_->multibuffer()->OnDataProviderEvent(this); 383 url_data_->multibuffer()->OnDataProviderEvent(this);
389 384
390 // Beware, this object might be deleted here. 385 // Beware, this object might be deleted here.
391 } 386 }
392 387
393 void ResourceMultiBufferDataProvider::didDownloadData(WebURLLoader* loader, 388 void ResourceMultiBufferDataProvider::didDownloadData(int dataLength) {
394 int dataLength,
395 int encoded_data_length) {
396 NOTIMPLEMENTED(); 389 NOTIMPLEMENTED();
397 } 390 }
398 391
399 void ResourceMultiBufferDataProvider::didReceiveCachedMetadata( 392 void ResourceMultiBufferDataProvider::didReceiveCachedMetadata(
400 WebURLLoader* loader,
401 const char* data, 393 const char* data,
402 int data_length) { 394 int data_length) {
403 NOTIMPLEMENTED(); 395 NOTIMPLEMENTED();
404 } 396 }
405 397
406 void ResourceMultiBufferDataProvider::didFinishLoading( 398 void ResourceMultiBufferDataProvider::didFinishLoading(double finishTime) {
407 WebURLLoader* loader,
408 double finishTime,
409 int64_t total_encoded_data_length) {
410 DVLOG(1) << "didFinishLoading"; 399 DVLOG(1) << "didFinishLoading";
411 DCHECK(active_loader_.get()); 400 DCHECK(active_loader_.get());
412 DCHECK(!Available()); 401 DCHECK(!Available());
413 402
414 // We're done with the loader. 403 // We're done with the loader.
415 active_loader_.reset(); 404 active_loader_.reset();
416 405
417 // If we didn't know the |instance_size_| we do now. 406 // If we didn't know the |instance_size_| we do now.
418 int64_t size = byte_pos(); 407 int64_t size = byte_pos();
419 408
(...skipping 18 matching lines...) Expand all
438 427
439 url_data_->set_length(size); 428 url_data_->set_length(size);
440 fifo_.push_back(DataBuffer::CreateEOSBuffer()); 429 fifo_.push_back(DataBuffer::CreateEOSBuffer());
441 430
442 DCHECK(Available()); 431 DCHECK(Available());
443 url_data_->multibuffer()->OnDataProviderEvent(this); 432 url_data_->multibuffer()->OnDataProviderEvent(this);
444 433
445 // Beware, this object might be deleted here. 434 // Beware, this object might be deleted here.
446 } 435 }
447 436
448 void ResourceMultiBufferDataProvider::didFail(WebURLLoader* loader, 437 void ResourceMultiBufferDataProvider::didFail(const WebURLError& error) {
449 const WebURLError& error) {
450 DVLOG(1) << "didFail: reason=" << error.reason 438 DVLOG(1) << "didFail: reason=" << error.reason
451 << ", isCancellation=" << error.isCancellation 439 << ", isCancellation=" << error.isCancellation
452 << ", domain=" << error.domain.utf8().data() 440 << ", domain=" << error.domain.utf8().data()
453 << ", localizedDescription=" 441 << ", localizedDescription="
454 << error.localizedDescription.utf8().data(); 442 << error.localizedDescription.utf8().data();
455 DCHECK(active_loader_.get()); 443 DCHECK(active_loader_.get());
456 444
457 if (retries_ < kMaxRetries && pos_ != 0) { 445 if (retries_ < kMaxRetries && pos_ != 0) {
458 retries_++; 446 retries_++;
459 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 447 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
547 } 535 }
548 536
549 if (byte_pos() != first_byte_position) { 537 if (byte_pos() != first_byte_position) {
550 return false; 538 return false;
551 } 539 }
552 540
553 return true; 541 return true;
554 } 542 }
555 543
556 } // namespace media 544 } // namespace media
OLDNEW
« no previous file with comments | « media/blink/resource_multibuffer_data_provider.h ('k') | media/blink/resource_multibuffer_data_provider_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698