Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / loader / navigation_resource_handler.cc
blob393b295776e4a935a117c2abece15d16d7f37fae
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.
5 #include "content/browser/loader/navigation_resource_handler.h"
7 #include "base/logging.h"
8 #include "content/browser/devtools/devtools_netlog_observer.h"
9 #include "content/browser/loader/navigation_url_loader_impl_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"
20 namespace content {
22 NavigationResourceHandler::NavigationResourceHandler(
23 net::URLRequest* request,
24 NavigationURLLoaderImplCore* core)
25 : ResourceHandler(request),
26 core_(core) {
27 core_->set_resource_handler(this);
30 NavigationResourceHandler::~NavigationResourceHandler() {
31 if (core_) {
32 core_->NotifyRequestFailed(net::ERR_ABORTED);
33 DetachFromCore();
37 void NavigationResourceHandler::Cancel() {
38 controller()->Cancel();
39 core_ = nullptr;
42 void NavigationResourceHandler::FollowRedirect() {
43 controller()->Resume();
46 void NavigationResourceHandler::SetController(ResourceController* controller) {
47 writer_.set_controller(controller);
48 ResourceHandler::SetController(controller);
51 bool NavigationResourceHandler::OnUploadProgress(uint64 position, uint64 size) {
52 return true;
55 bool NavigationResourceHandler::OnRequestRedirected(
56 const net::RedirectInfo& redirect_info,
57 ResourceResponse* response,
58 bool* defer) {
59 DCHECK(core_);
61 // TODO(davidben): Perform a CSP check here, and anything else that would have
62 // been done renderer-side.
63 DevToolsNetLogObserver::PopulateResponseInfo(request(), response);
64 core_->NotifyRequestRedirected(redirect_info, response);
65 *defer = true;
66 return true;
69 bool NavigationResourceHandler::OnResponseStarted(ResourceResponse* response,
70 bool* defer) {
71 DCHECK(core_);
73 ResourceRequestInfoImpl* info = GetRequestInfo();
75 // If the BufferedResourceHandler intercepted this request and converted it
76 // into a download, it will still call OnResponseStarted and immediately
77 // cancel. Ignore the call; OnReadCompleted will happen shortly.
79 // TODO(davidben): Move the dispatch out of BufferedResourceHandler. Perhaps
80 // all the way to the UI thread. Downloads, user certificates, etc., should be
81 // dispatched at the navigation layer.
82 if (info->IsDownload() || info->is_stream())
83 return true;
85 StreamContext* stream_context =
86 GetStreamContextForResourceContext(info->GetContext());
87 writer_.InitializeStream(stream_context->registry(),
88 request()->url().GetOrigin());
90 // Detach from the loader; at this point, the request is now owned by the
91 // StreamHandle.
92 DevToolsNetLogObserver::PopulateResponseInfo(request(), response);
93 core_->NotifyResponseStarted(response, writer_.stream()->CreateHandle());
94 DetachFromCore();
95 return true;
98 bool NavigationResourceHandler::OnWillStart(const GURL& url, bool* defer) {
99 return true;
102 bool NavigationResourceHandler::OnBeforeNetworkStart(const GURL& url,
103 bool* defer) {
104 return true;
107 bool NavigationResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
108 int* buf_size,
109 int min_size) {
110 writer_.OnWillRead(buf, buf_size, min_size);
111 return true;
114 bool NavigationResourceHandler::OnReadCompleted(int bytes_read, bool* defer) {
115 writer_.OnReadCompleted(bytes_read, defer);
116 return true;
119 void NavigationResourceHandler::OnResponseCompleted(
120 const net::URLRequestStatus& status,
121 const std::string& security_info,
122 bool* defer) {
123 // If the request has already committed, close the stream and leave it as-is.
125 // TODO(davidben): The net error code should be passed through StreamWriter
126 // down to the stream's consumer. See https://crbug.com/426162.
127 if (writer_.stream()) {
128 writer_.Finalize();
129 return;
132 if (core_) {
133 DCHECK_NE(net::OK, status.error());
134 core_->NotifyRequestFailed(status.error());
135 DetachFromCore();
139 void NavigationResourceHandler::OnDataDownloaded(int bytes_downloaded) {
140 NOTREACHED();
143 void NavigationResourceHandler::DetachFromCore() {
144 DCHECK(core_);
145 core_->set_resource_handler(nullptr);
146 core_ = nullptr;
149 } // namespace content