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

Side by Side Diff: content/child/web_url_loader_impl.cc

Issue 226273005: Remove webkit's ResourceLoaderBridge interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: more changes for John Created 6 years, 8 months 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 // An implementation of WebURLLoader in terms of ResourceLoaderBridge.
6
7 #include "content/child/web_url_loader_impl.h" 5 #include "content/child/web_url_loader_impl.h"
8 6
9 #include "base/bind.h" 7 #include "base/bind.h"
10 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
11 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
13 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
14 #include "base/time/time.h" 12 #include "base/time/time.h"
15 #include "content/child/child_thread.h" 13 #include "content/child/child_thread.h"
16 #include "content/child/ftp_directory_listing_response_delegate.h" 14 #include "content/child/ftp_directory_listing_response_delegate.h"
17 #include "content/child/request_extra_data.h" 15 #include "content/child/request_extra_data.h"
18 #include "content/child/request_info.h" 16 #include "content/child/request_info.h"
17 #include "content/child/resource_dispatcher.h"
19 #include "content/child/sync_load_response.h" 18 #include "content/child/sync_load_response.h"
19 #include "content/common/resource_messages.h"
20 #include "content/common/resource_request_body.h" 20 #include "content/common/resource_request_body.h"
21 #include "content/public/child/request_peer.h" 21 #include "content/public/child/request_peer.h"
22 #include "net/base/data_url.h" 22 #include "net/base/data_url.h"
23 #include "net/base/filename_util.h" 23 #include "net/base/filename_util.h"
24 #include "net/base/load_flags.h" 24 #include "net/base/load_flags.h"
25 #include "net/base/mime_util.h" 25 #include "net/base/mime_util.h"
26 #include "net/base/net_errors.h" 26 #include "net/base/net_errors.h"
27 #include "net/http/http_response_headers.h" 27 #include "net/http/http_response_headers.h"
28 #include "net/http/http_util.h" 28 #include "net/http/http_util.h"
29 #include "net/url_request/url_request.h" 29 #include "net/url_request/url_request.h"
30 #include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h" 30 #include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h"
31 #include "third_party/WebKit/public/platform/WebHTTPLoadInfo.h" 31 #include "third_party/WebKit/public/platform/WebHTTPLoadInfo.h"
32 #include "third_party/WebKit/public/platform/WebURL.h" 32 #include "third_party/WebKit/public/platform/WebURL.h"
33 #include "third_party/WebKit/public/platform/WebURLError.h" 33 #include "third_party/WebKit/public/platform/WebURLError.h"
34 #include "third_party/WebKit/public/platform/WebURLLoadTiming.h" 34 #include "third_party/WebKit/public/platform/WebURLLoadTiming.h"
35 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h" 35 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
36 #include "third_party/WebKit/public/platform/WebURLRequest.h" 36 #include "third_party/WebKit/public/platform/WebURLRequest.h"
37 #include "third_party/WebKit/public/platform/WebURLResponse.h" 37 #include "third_party/WebKit/public/platform/WebURLResponse.h"
38 #include "third_party/WebKit/public/web/WebSecurityPolicy.h" 38 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
39 #include "webkit/child/multipart_response_delegate.h" 39 #include "webkit/child/multipart_response_delegate.h"
40 #include "webkit/child/resource_loader_bridge.h"
41 #include "webkit/child/weburlresponse_extradata_impl.h" 40 #include "webkit/child/weburlresponse_extradata_impl.h"
42 41
43 using base::Time; 42 using base::Time;
44 using base::TimeTicks; 43 using base::TimeTicks;
45 using blink::WebData; 44 using blink::WebData;
46 using blink::WebHTTPBody; 45 using blink::WebHTTPBody;
47 using blink::WebHTTPHeaderVisitor; 46 using blink::WebHTTPHeaderVisitor;
48 using blink::WebHTTPLoadInfo; 47 using blink::WebHTTPLoadInfo;
49 using blink::WebReferrerPolicy; 48 using blink::WebReferrerPolicy;
50 using blink::WebSecurityPolicy; 49 using blink::WebSecurityPolicy;
51 using blink::WebString; 50 using blink::WebString;
52 using blink::WebURL; 51 using blink::WebURL;
53 using blink::WebURLError; 52 using blink::WebURLError;
54 using blink::WebURLLoadTiming; 53 using blink::WebURLLoadTiming;
55 using blink::WebURLLoader; 54 using blink::WebURLLoader;
56 using blink::WebURLLoaderClient; 55 using blink::WebURLLoaderClient;
57 using blink::WebURLRequest; 56 using blink::WebURLRequest;
58 using blink::WebURLResponse; 57 using blink::WebURLResponse;
59 using webkit_glue::MultipartResponseDelegate; 58 using webkit_glue::MultipartResponseDelegate;
60 using webkit_glue::ResourceDevToolsInfo; 59 using webkit_glue::ResourceDevToolsInfo;
61 using webkit_glue::ResourceLoaderBridge;
62 using webkit_glue::ResourceResponseInfo; 60 using webkit_glue::ResourceResponseInfo;
63 using webkit_glue::WebURLResponseExtraDataImpl; 61 using webkit_glue::WebURLResponseExtraDataImpl;
64 62
65 namespace content { 63 namespace content {
66 64
67 // Utilities ------------------------------------------------------------------ 65 // Utilities ------------------------------------------------------------------
68 66
69 namespace { 67 namespace {
70 68
71 const char kThrottledErrorDescription[] = 69 const char kThrottledErrorDescription[] =
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 virtual void OnCompletedRequest( 250 virtual void OnCompletedRequest(
253 int error_code, 251 int error_code,
254 bool was_ignored_by_handler, 252 bool was_ignored_by_handler,
255 bool stale_copy_in_cache, 253 bool stale_copy_in_cache,
256 const std::string& security_info, 254 const std::string& security_info,
257 const base::TimeTicks& completion_time, 255 const base::TimeTicks& completion_time,
258 int64 total_transfer_size) OVERRIDE; 256 int64 total_transfer_size) OVERRIDE;
259 257
260 private: 258 private:
261 friend class base::RefCounted<Context>; 259 friend class base::RefCounted<Context>;
262 virtual ~Context() {} 260 virtual ~Context();
263 261
264 // We can optimize the handling of data URLs in most cases. 262 // We can optimize the handling of data URLs in most cases.
265 bool CanHandleDataURL(const GURL& url) const; 263 bool CanHandleDataURL(const GURL& url) const;
266 void HandleDataURL(); 264 void HandleDataURL();
267 265
268 WebURLLoaderImpl* loader_; 266 WebURLLoaderImpl* loader_;
269 WebURLRequest request_; 267 WebURLRequest request_;
270 WebURLLoaderClient* client_; 268 WebURLLoaderClient* client_;
271 WebReferrerPolicy referrer_policy_; 269 WebReferrerPolicy referrer_policy_;
272 scoped_ptr<ResourceLoaderBridge> bridge_;
273 scoped_ptr<FtpDirectoryListingResponseDelegate> ftp_listing_delegate_; 270 scoped_ptr<FtpDirectoryListingResponseDelegate> ftp_listing_delegate_;
274 scoped_ptr<MultipartResponseDelegate> multipart_delegate_; 271 scoped_ptr<MultipartResponseDelegate> multipart_delegate_;
275 scoped_ptr<ResourceLoaderBridge> completed_bridge_; 272 int request_id_;
276 }; 273 };
277 274
278 WebURLLoaderImpl::Context::Context(WebURLLoaderImpl* loader) 275 WebURLLoaderImpl::Context::Context(WebURLLoaderImpl* loader)
279 : loader_(loader), 276 : loader_(loader),
280 client_(NULL), 277 client_(NULL),
281 referrer_policy_(blink::WebReferrerPolicyDefault) { 278 referrer_policy_(blink::WebReferrerPolicyDefault),
279 request_id_(-1) {
282 } 280 }
283 281
284 void WebURLLoaderImpl::Context::Cancel() { 282 void WebURLLoaderImpl::Context::Cancel() {
285 // The bridge will still send OnCompletedRequest, which will Release() us, so 283 ChildThread::current()->resource_dispatcher()->Cancel(request_id_);
tfarina 2014/04/30 01:33:11 John, this is crashing: [ RUN ] RenderViewIm
jam 2014/05/01 16:28:16 you probably need to check for if (ChildThread::c
tfarina 2014/05/01 19:57:16 Fixed, it does not crash now, but the test failed
286 // we don't do that here.
287 if (bridge_)
288 bridge_->Cancel();
289 284
290 // Ensure that we do not notify the multipart delegate anymore as it has 285 // Ensure that we do not notify the multipart delegate anymore as it has
291 // its own pointer to the client. 286 // its own pointer to the client.
292 if (multipart_delegate_) 287 if (multipart_delegate_)
293 multipart_delegate_->Cancel(); 288 multipart_delegate_->Cancel();
294 289
295 // Do not make any further calls to the client. 290 // Do not make any further calls to the client.
296 client_ = NULL; 291 client_ = NULL;
297 loader_ = NULL; 292 loader_ = NULL;
298 } 293 }
299 294
300 void WebURLLoaderImpl::Context::SetDefersLoading(bool value) { 295 void WebURLLoaderImpl::Context::SetDefersLoading(bool value) {
301 if (bridge_) 296 ChildThread::current()->resource_dispatcher()->SetDefersLoading(request_id_,
302 bridge_->SetDefersLoading(value); 297 value);
303 } 298 }
304 299
305 void WebURLLoaderImpl::Context::DidChangePriority( 300 void WebURLLoaderImpl::Context::DidChangePriority(
306 WebURLRequest::Priority new_priority, int intra_priority_value) { 301 WebURLRequest::Priority new_priority,
307 if (bridge_) 302 int intra_priority_value) {
308 bridge_->DidChangePriority( 303 ChildThread::current()->resource_dispatcher()->DidChangePriority(
309 ConvertWebKitPriorityToNetPriority(new_priority), intra_priority_value); 304 request_id_,
305 ConvertWebKitPriorityToNetPriority(new_priority),
306 intra_priority_value);
310 } 307 }
311 308
312 void WebURLLoaderImpl::Context::Start(const WebURLRequest& request, 309 void WebURLLoaderImpl::Context::Start(const WebURLRequest& request,
313 SyncLoadResponse* sync_load_response) { 310 SyncLoadResponse* sync_load_response) {
314 DCHECK(!bridge_.get());
315
316 request_ = request; // Save the request. 311 request_ = request; // Save the request.
317 312
318 GURL url = request.url(); 313 GURL url = request.url();
319 if (url.SchemeIs("data") && CanHandleDataURL(url)) { 314 if (url.SchemeIs("data") && CanHandleDataURL(url)) {
320 if (sync_load_response) { 315 if (sync_load_response) {
321 // This is a sync load. Do the work now. 316 // This is a sync load. Do the work now.
322 sync_load_response->url = url; 317 sync_load_response->url = url;
323 std::string data; 318 std::string data;
324 GetInfoFromDataURL(sync_load_response->url, sync_load_response, 319 GetInfoFromDataURL(sync_load_response->url, sync_load_response,
325 &sync_load_response->data, 320 &sync_load_response->data,
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 ResourceType::FromTargetType(request.targetType()); 388 ResourceType::FromTargetType(request.targetType());
394 request_info.priority = 389 request_info.priority =
395 ConvertWebKitPriorityToNetPriority(request.priority()); 390 ConvertWebKitPriorityToNetPriority(request.priority());
396 request_info.appcache_host_id = request.appCacheHostID(); 391 request_info.appcache_host_id = request.appCacheHostID();
397 request_info.routing_id = request.requestorID(); 392 request_info.routing_id = request.requestorID();
398 request_info.download_to_file = request.downloadToFile(); 393 request_info.download_to_file = request.downloadToFile();
399 request_info.has_user_gesture = request.hasUserGesture(); 394 request_info.has_user_gesture = request.hasUserGesture();
400 request_info.extra_data = request.extraData(); 395 request_info.extra_data = request.extraData();
401 referrer_policy_ = request.referrerPolicy(); 396 referrer_policy_ = request.referrerPolicy();
402 request_info.referrer_policy = request.referrerPolicy(); 397 request_info.referrer_policy = request.referrerPolicy();
403 bridge_.reset(ChildThread::current()->CreateBridge(request_info)); 398 ResourceDispatcher* dispatcher =
399 ChildThread::current()->resource_dispatcher();
400
401 scoped_refptr<ResourceRequestBody> request_body = new ResourceRequestBody;
404 402
405 if (!request.httpBody().isNull()) { 403 if (!request.httpBody().isNull()) {
406 // GET and HEAD requests shouldn't have http bodies. 404 // GET and HEAD requests shouldn't have http bodies.
407 DCHECK(method != "GET" && method != "HEAD"); 405 DCHECK(method != "GET" && method != "HEAD");
408 const WebHTTPBody& httpBody = request.httpBody(); 406 const WebHTTPBody& httpBody = request.httpBody();
409 size_t i = 0; 407 size_t i = 0;
410 WebHTTPBody::Element element; 408 WebHTTPBody::Element element;
411 scoped_refptr<ResourceRequestBody> request_body = new ResourceRequestBody;
412 while (httpBody.elementAt(i++, element)) { 409 while (httpBody.elementAt(i++, element)) {
413 switch (element.type) { 410 switch (element.type) {
414 case WebHTTPBody::Element::TypeData: 411 case WebHTTPBody::Element::TypeData:
415 if (!element.data.isEmpty()) { 412 if (!element.data.isEmpty()) {
416 // WebKit sometimes gives up empty data to append. These aren't 413 // WebKit sometimes gives up empty data to append. These aren't
417 // necessary so we just optimize those out here. 414 // necessary so we just optimize those out here.
418 request_body->AppendBytes( 415 request_body->AppendBytes(
419 element.data.data(), static_cast<int>(element.data.size())); 416 element.data.data(), static_cast<int>(element.data.size()));
420 } 417 }
421 break; 418 break;
(...skipping 21 matching lines...) Expand all
443 break; 440 break;
444 } 441 }
445 case WebHTTPBody::Element::TypeBlob: 442 case WebHTTPBody::Element::TypeBlob:
446 request_body->AppendBlob(element.blobUUID.utf8()); 443 request_body->AppendBlob(element.blobUUID.utf8());
447 break; 444 break;
448 default: 445 default:
449 NOTREACHED(); 446 NOTREACHED();
450 } 447 }
451 } 448 }
452 request_body->set_identifier(request.httpBody().identifier()); 449 request_body->set_identifier(request.httpBody().identifier());
453 bridge_->SetRequestBody(request_body.get()); 450
454 } 451 }
455 452
456 if (sync_load_response) { 453 if (sync_load_response) {
457 bridge_->SyncLoad(sync_load_response); 454 dispatcher->StartSync(request_info, request_body.get(), sync_load_response);
458 return; 455 return;
459 } 456 }
460 457
461 if (bridge_->Start(this)) { 458 AddRef(); // Balanced in OnCompletedRequest.
462 AddRef(); // Balanced in OnCompletedRequest 459 request_id_ = dispatcher->StartAsync(request_info, request_body.get(), this);
463 } else {
464 bridge_.reset();
465 }
466 } 460 }
467 461
468 void WebURLLoaderImpl::Context::OnUploadProgress(uint64 position, uint64 size) { 462 void WebURLLoaderImpl::Context::OnUploadProgress(uint64 position, uint64 size) {
469 if (client_) 463 if (client_)
470 client_->didSendData(loader_, position, size); 464 client_->didSendData(loader_, position, size);
471 } 465 }
472 466
473 bool WebURLLoaderImpl::Context::OnReceivedRedirect( 467 bool WebURLLoaderImpl::Context::OnReceivedRedirect(
474 const GURL& new_url, 468 const GURL& new_url,
475 const ResourceResponseInfo& info, 469 const ResourceResponseInfo& info,
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
592 ftp_listing_delegate_->OnReceivedData(data, data_length); 586 ftp_listing_delegate_->OnReceivedData(data, data_length);
593 } else if (multipart_delegate_) { 587 } else if (multipart_delegate_) {
594 // The multipart delegate will make the appropriate calls to 588 // The multipart delegate will make the appropriate calls to
595 // client_->didReceiveData and client_->didReceiveResponse. 589 // client_->didReceiveData and client_->didReceiveResponse.
596 multipart_delegate_->OnReceivedData(data, data_length, encoded_data_length); 590 multipart_delegate_->OnReceivedData(data, data_length, encoded_data_length);
597 } else { 591 } else {
598 client_->didReceiveData(loader_, data, data_length, encoded_data_length); 592 client_->didReceiveData(loader_, data, data_length, encoded_data_length);
599 } 593 }
600 } 594 }
601 595
602 void WebURLLoaderImpl::Context::OnReceivedCachedMetadata( 596 void WebURLLoaderImpl::Context::OnReceivedCachedMetadata(const char* data,
603 const char* data, int len) { 597 int len) {
604 if (client_) 598 if (client_)
605 client_->didReceiveCachedMetadata(loader_, data, len); 599 client_->didReceiveCachedMetadata(loader_, data, len);
606 } 600 }
607 601
608 void WebURLLoaderImpl::Context::OnCompletedRequest( 602 void WebURLLoaderImpl::Context::OnCompletedRequest(
609 int error_code, 603 int error_code,
610 bool was_ignored_by_handler, 604 bool was_ignored_by_handler,
611 bool stale_copy_in_cache, 605 bool stale_copy_in_cache,
612 const std::string& security_info, 606 const std::string& security_info,
613 const base::TimeTicks& completion_time, 607 const base::TimeTicks& completion_time,
614 int64 total_transfer_size) { 608 int64 total_transfer_size) {
615 if (ftp_listing_delegate_) { 609 if (ftp_listing_delegate_) {
616 ftp_listing_delegate_->OnCompletedRequest(); 610 ftp_listing_delegate_->OnCompletedRequest();
617 ftp_listing_delegate_.reset(NULL); 611 ftp_listing_delegate_.reset(NULL);
618 } else if (multipart_delegate_) { 612 } else if (multipart_delegate_) {
619 multipart_delegate_->OnCompletedRequest(); 613 multipart_delegate_->OnCompletedRequest();
620 multipart_delegate_.reset(NULL); 614 multipart_delegate_.reset(NULL);
621 } 615 }
622 616
623 // Prevent any further IPC to the browser now that we're complete, but
624 // don't delete it to keep any downloaded temp files alive.
625 DCHECK(!completed_bridge_.get());
626 completed_bridge_.swap(bridge_);
627
628 if (client_) { 617 if (client_) {
629 if (error_code != net::OK) { 618 if (error_code != net::OK) {
630 client_->didFail(loader_, CreateError(request_.url(), 619 client_->didFail(loader_, CreateError(request_.url(),
631 stale_copy_in_cache, 620 stale_copy_in_cache,
632 error_code)); 621 error_code));
633 } else { 622 } else {
634 client_->didFinishLoading( 623 client_->didFinishLoading(
635 loader_, (completion_time - TimeTicks()).InSecondsF(), 624 loader_, (completion_time - TimeTicks()).InSecondsF(),
636 total_transfer_size); 625 total_transfer_size);
637 } 626 }
638 } 627 }
639 628
640 // We are done with the bridge now, and so we need to release the reference 629 // We are done with the bridge now, and so we need to release the reference
641 // to ourselves that we took on behalf of the bridge. This may cause our 630 // to ourselves that we took on behalf of the bridge. This may cause our
642 // destruction. 631 // destruction.
643 Release(); 632 Release();
644 } 633 }
645 634
635 WebURLLoaderImpl::Context::~Context() {
636 if (request_id_ >= 0) {
637 ChildThread::current()->resource_dispatcher()->RemovePendingRequest(
638 request_id_);
639 }
640 if (request_.downloadToFile()) {
641 ChildThread::current()->resource_dispatcher()->message_sender()->Send(
642 new ResourceHostMsg_ReleaseDownloadedFile(request_id_));
jam 2014/04/28 15:26:13 nit: we should keep the sending of resource relate
tfarina 2014/04/30 01:33:11 John, ResourceDispatcher does not have |request_|
jam 2014/05/01 16:28:16 sure
tfarina 2014/05/01 19:57:16 Done.
643 }
644 }
645
646 bool WebURLLoaderImpl::Context::CanHandleDataURL(const GURL& url) const { 646 bool WebURLLoaderImpl::Context::CanHandleDataURL(const GURL& url) const {
647 DCHECK(url.SchemeIs("data")); 647 DCHECK(url.SchemeIs("data"));
648 648
649 // Optimize for the case where we can handle a data URL locally. We must 649 // Optimize for the case where we can handle a data URL locally. We must
650 // skip this for data URLs targetted at frames since those could trigger a 650 // skip this for data URLs targetted at frames since those could trigger a
651 // download. 651 // download.
652 // 652 //
653 // NOTE: We special case MIME types we can render both for performance 653 // NOTE: We special case MIME types we can render both for performance
654 // reasons as well as to support unit tests, which do not have an underlying 654 // reasons as well as to support unit tests.
655 // ResourceLoaderBridge implementation.
656 655
657 #if defined(OS_ANDROID) 656 #if defined(OS_ANDROID)
658 // For compatibility reasons on Android we need to expose top-level data:// 657 // For compatibility reasons on Android we need to expose top-level data://
659 // to the browser. 658 // to the browser.
660 if (request_.targetType() == WebURLRequest::TargetIsMainFrame) 659 if (request_.targetType() == WebURLRequest::TargetIsMainFrame)
661 return false; 660 return false;
662 #endif 661 #endif
663 662
664 if (request_.targetType() != WebURLRequest::TargetIsMainFrame && 663 if (request_.targetType() != WebURLRequest::TargetIsMainFrame &&
665 request_.targetType() != WebURLRequest::TargetIsSubframe) 664 request_.targetType() != WebURLRequest::TargetIsSubframe)
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
867 void WebURLLoaderImpl::setDefersLoading(bool value) { 866 void WebURLLoaderImpl::setDefersLoading(bool value) {
868 context_->SetDefersLoading(value); 867 context_->SetDefersLoading(value);
869 } 868 }
870 869
871 void WebURLLoaderImpl::didChangePriority(WebURLRequest::Priority new_priority, 870 void WebURLLoaderImpl::didChangePriority(WebURLRequest::Priority new_priority,
872 int intra_priority_value) { 871 int intra_priority_value) {
873 context_->DidChangePriority(new_priority, intra_priority_value); 872 context_->DidChangePriority(new_priority, intra_priority_value);
874 } 873 }
875 874
876 } // namespace content 875 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698