OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/loader/navigation_resource_handler.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "content/browser/devtools/devtools_netlog_observer.h" |
| 9 #include "content/browser/loader/navigation_url_loader_core.h" |
| 10 #include "content/browser/loader/resource_request_info_impl.h" |
| 11 #include "content/browser/resource_context_impl.h" |
| 12 #include "content/browser/streams/stream.h" |
| 13 #include "content/browser/streams/stream_context.h" |
| 14 #include "content/public/browser/resource_controller.h" |
| 15 #include "content/public/browser/stream_handle.h" |
| 16 #include "content/public/common/resource_response.h" |
| 17 #include "net/base/net_errors.h" |
| 18 #include "net/url_request/url_request.h" |
| 19 |
| 20 namespace content { |
| 21 |
| 22 NavigationResourceHandler::NavigationResourceHandler( |
| 23 net::URLRequest* request, |
| 24 NavigationURLLoaderCore* core) |
| 25 : ResourceHandler(request), |
| 26 core_(core) { |
| 27 core_->set_resource_handler(this); |
| 28 } |
| 29 |
| 30 NavigationResourceHandler::~NavigationResourceHandler() { |
| 31 if (core_.get()) { |
| 32 core_->RequestFailedOnIOThread(net::ERR_ABORTED); |
| 33 core_->set_resource_handler(NULL); |
| 34 core_ = NULL; |
| 35 } |
| 36 } |
| 37 |
| 38 void NavigationResourceHandler::Cancel() { |
| 39 controller()->Cancel(); |
| 40 |
| 41 if (core_.get()) { |
| 42 // At this point, the core is detached from the UI-thread loader, so there |
| 43 // is no use in signaling back in OnResponseCompleted. |
| 44 core_->set_resource_handler(NULL); |
| 45 core_ = NULL; |
| 46 } |
| 47 } |
| 48 |
| 49 void NavigationResourceHandler::FollowRedirect() { |
| 50 controller()->Resume(); |
| 51 } |
| 52 |
| 53 void NavigationResourceHandler::SetController(ResourceController* controller) { |
| 54 writer_.set_controller(controller); |
| 55 ResourceHandler::SetController(controller); |
| 56 } |
| 57 |
| 58 bool NavigationResourceHandler::OnUploadProgress(uint64 position, |
| 59 uint64 size) { |
| 60 return true; |
| 61 } |
| 62 |
| 63 bool NavigationResourceHandler::OnRequestRedirected( |
| 64 const net::RedirectInfo& redirect_info, |
| 65 ResourceResponse* response, |
| 66 bool* defer) { |
| 67 // TODO(davidben): Perform a CSP check here, and anything else that would have |
| 68 // been done renderer-side. |
| 69 DevToolsNetLogObserver::PopulateResponseInfo(request(), response); |
| 70 core_->RequestRedirectedOnIOThread(redirect_info, response); |
| 71 *defer = true; |
| 72 return true; |
| 73 } |
| 74 |
| 75 bool NavigationResourceHandler::OnResponseStarted(ResourceResponse* response, |
| 76 bool* defer) { |
| 77 DCHECK(core_.get()); |
| 78 |
| 79 ResourceRequestInfoImpl* info = GetRequestInfo(); |
| 80 |
| 81 // If the BufferedResourceHandler intercepted this request and converted it |
| 82 // into a download, it will still call OnResponseStarted and immediately |
| 83 // cancel. Ignore the call; OnReadCompleted will happen shortly. |
| 84 // |
| 85 // TODO(davidben): Move the dispatch out of BufferedResourceHandler. Perhaps |
| 86 // all the way to the UI thread. Downloads, user certificates, etc., should be |
| 87 // dispatched at the navigation layer. |
| 88 if (info->IsDownload() || info->is_stream()) |
| 89 return true; |
| 90 |
| 91 StreamContext* stream_context = |
| 92 GetStreamContextForResourceContext(info->GetContext()); |
| 93 writer_.InitializeStream(stream_context->registry(), |
| 94 request()->url().GetOrigin()); |
| 95 |
| 96 // Detach from the loader; at this point, the request is now owned by the |
| 97 // StreamHandle. |
| 98 DevToolsNetLogObserver::PopulateResponseInfo(request(), response); |
| 99 core_->ResponseStartedOnIOThread(response, writer_.stream()->CreateHandle()); |
| 100 core_->set_resource_handler(NULL); |
| 101 core_ = NULL; |
| 102 return true; |
| 103 } |
| 104 |
| 105 bool NavigationResourceHandler::OnWillStart(const GURL& url, bool* defer) { |
| 106 return true; |
| 107 } |
| 108 |
| 109 bool NavigationResourceHandler::OnBeforeNetworkStart(const GURL& url, |
| 110 bool* defer) { |
| 111 return true; |
| 112 } |
| 113 |
| 114 bool NavigationResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| 115 int* buf_size, |
| 116 int min_size) { |
| 117 writer_.OnWillRead(buf, buf_size, min_size); |
| 118 return true; |
| 119 } |
| 120 |
| 121 bool NavigationResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { |
| 122 writer_.OnReadCompleted(bytes_read, defer); |
| 123 return true; |
| 124 } |
| 125 |
| 126 void NavigationResourceHandler::OnResponseCompleted( |
| 127 const net::URLRequestStatus& status, |
| 128 const std::string& security_info, |
| 129 bool* defer) { |
| 130 // If the request has already committed, close the stream and leave it as-is. |
| 131 // |
| 132 // TODO(davidben): Is the final status of a navigation request meaningful? |
| 133 // Perhaps we should eventually extend the streams interface to push the |
| 134 // status down. (If there was an error partway through loading the top-level.) |
| 135 if (writer_.stream()) { |
| 136 writer_.Finalize(); |
| 137 return; |
| 138 } |
| 139 |
| 140 if (core_.get()) { |
| 141 DCHECK_NE(net::OK, status.error()); |
| 142 core_->RequestFailedOnIOThread(status.error()); |
| 143 core_->set_resource_handler(NULL); |
| 144 core_ = NULL; |
| 145 } |
| 146 } |
| 147 |
| 148 void NavigationResourceHandler::OnDataDownloaded(int bytes_downloaded) { |
| 149 NOTREACHED(); |
| 150 } |
| 151 |
| 152 } // namespace content |
OLD | NEW |