[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / loader / navigation_resource_handler.cc
blob94e046dbf0c74bf8044e6cd93484c3cab38563fa
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);
28 writer_.set_immediate_mode(true);
31 NavigationResourceHandler::~NavigationResourceHandler() {
32 if (core_) {
33 core_->NotifyRequestFailed(false, net::ERR_ABORTED);
34 DetachFromCore();
38 void NavigationResourceHandler::Cancel() {
39 controller()->Cancel();
40 core_ = nullptr;
43 void NavigationResourceHandler::FollowRedirect() {
44 controller()->Resume();
47 void NavigationResourceHandler::SetController(ResourceController* controller) {
48 writer_.set_controller(controller);
49 ResourceHandler::SetController(controller);
52 bool NavigationResourceHandler::OnRequestRedirected(
53 const net::RedirectInfo& redirect_info,
54 ResourceResponse* response,
55 bool* defer) {
56 DCHECK(core_);
58 // TODO(davidben): Perform a CSP check here, and anything else that would have
59 // been done renderer-side.
60 DevToolsNetLogObserver::PopulateResponseInfo(request(), response);
61 core_->NotifyRequestRedirected(redirect_info, response);
62 *defer = true;
63 return true;
66 bool NavigationResourceHandler::OnResponseStarted(ResourceResponse* response,
67 bool* defer) {
68 DCHECK(core_);
70 ResourceRequestInfoImpl* info = GetRequestInfo();
72 // If the MimeTypeResourceHandler intercepted this request and converted it
73 // into a download, it will still call OnResponseStarted and immediately
74 // cancel. Ignore the call; OnReadCompleted will happen shortly.
76 // TODO(davidben): Move the dispatch out of MimeTypeResourceHandler. Perhaps
77 // all the way to the UI thread. Downloads, user certificates, etc., should be
78 // dispatched at the navigation layer.
79 if (info->IsDownload() || info->is_stream())
80 return true;
82 StreamContext* stream_context =
83 GetStreamContextForResourceContext(info->GetContext());
84 writer_.InitializeStream(stream_context->registry(),
85 request()->url().GetOrigin());
87 // Detach from the loader; at this point, the request is now owned by the
88 // StreamHandle.
89 DevToolsNetLogObserver::PopulateResponseInfo(request(), response);
90 core_->NotifyResponseStarted(response, writer_.stream()->CreateHandle());
91 DetachFromCore();
92 return true;
95 bool NavigationResourceHandler::OnWillStart(const GURL& url, bool* defer) {
96 return true;
99 bool NavigationResourceHandler::OnBeforeNetworkStart(const GURL& url,
100 bool* defer) {
101 return true;
104 bool NavigationResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
105 int* buf_size,
106 int min_size) {
107 writer_.OnWillRead(buf, buf_size, min_size);
108 return true;
111 bool NavigationResourceHandler::OnReadCompleted(int bytes_read, bool* defer) {
112 writer_.OnReadCompleted(bytes_read, defer);
113 return true;
116 void NavigationResourceHandler::OnResponseCompleted(
117 const net::URLRequestStatus& status,
118 const std::string& security_info,
119 bool* defer) {
120 // If the request has already committed, close the stream and leave it as-is.
122 // TODO(davidben): The net error code should be passed through StreamWriter
123 // down to the stream's consumer. See https://crbug.com/426162.
124 if (writer_.stream()) {
125 writer_.Finalize();
126 return;
129 if (core_) {
130 DCHECK_NE(net::OK, status.error());
131 core_->NotifyRequestFailed(request()->response_info().was_cached,
132 status.error());
133 DetachFromCore();
137 void NavigationResourceHandler::OnDataDownloaded(int bytes_downloaded) {
138 NOTREACHED();
141 void NavigationResourceHandler::DetachFromCore() {
142 DCHECK(core_);
143 core_->set_resource_handler(nullptr);
144 core_ = nullptr;
147 } // namespace content