Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / ssl / ssl_error_handler.cc
blob45b6dfd830ce6a3ab66d3c582812ddd418e0f226
1 // Copyright (c) 2012 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/ssl/ssl_error_handler.h"
7 #include "base/bind.h"
8 #include "content/browser/frame_host/navigation_controller_impl.h"
9 #include "content/browser/frame_host/render_frame_host_impl.h"
10 #include "content/browser/ssl/ssl_cert_error_handler.h"
11 #include "content/browser/web_contents/web_contents_impl.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/resource_request_info.h"
14 #include "net/base/net_errors.h"
15 #include "net/url_request/url_request.h"
17 using net::SSLInfo;
19 namespace content {
21 SSLErrorHandler::SSLErrorHandler(const base::WeakPtr<Delegate>& delegate,
22 ResourceType resource_type,
23 const GURL& url,
24 int render_process_id,
25 int render_frame_id)
26 : manager_(NULL),
27 delegate_(delegate),
28 render_process_id_(render_process_id),
29 render_frame_id_(render_frame_id),
30 request_url_(url),
31 resource_type_(resource_type),
32 request_has_been_notified_(false) {
33 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
34 DCHECK(delegate.get());
36 // This makes sure we don't disappear on the IO thread until we've given an
37 // answer to the net::URLRequest.
39 // Release in CompleteCancelRequest, CompleteContinueRequest, or
40 // CompleteTakeNoAction.
41 AddRef();
44 SSLErrorHandler::~SSLErrorHandler() {}
46 void SSLErrorHandler::OnDispatchFailed() {
47 TakeNoAction();
50 void SSLErrorHandler::OnDispatched() {
51 TakeNoAction();
54 SSLCertErrorHandler* SSLErrorHandler::AsSSLCertErrorHandler() {
55 return NULL;
58 void SSLErrorHandler::Dispatch() {
59 DCHECK_CURRENTLY_ON(BrowserThread::UI);
61 WebContents* web_contents = NULL;
62 RenderFrameHost* render_frame_host =
63 RenderFrameHost::FromID(render_process_id_, render_frame_id_);
64 web_contents = WebContents::FromRenderFrameHost(render_frame_host);
66 if (!web_contents) {
67 // We arrived on the UI thread, but the tab we're looking for is no longer
68 // here.
69 OnDispatchFailed();
70 return;
73 // Hand ourselves off to the SSLManager.
74 manager_ =
75 static_cast<NavigationControllerImpl*>(&web_contents->GetController())->
76 ssl_manager();
77 OnDispatched();
80 void SSLErrorHandler::CancelRequest() {
81 DCHECK_CURRENTLY_ON(BrowserThread::UI);
83 // We need to complete this task on the IO thread.
84 BrowserThread::PostTask(
85 BrowserThread::IO, FROM_HERE,
86 base::Bind(
87 &SSLErrorHandler::CompleteCancelRequest, this, net::ERR_ABORTED));
90 void SSLErrorHandler::DenyRequest() {
91 DCHECK_CURRENTLY_ON(BrowserThread::UI);
93 // We need to complete this task on the IO thread.
94 BrowserThread::PostTask(
95 BrowserThread::IO, FROM_HERE,
96 base::Bind(
97 &SSLErrorHandler::CompleteCancelRequest, this,
98 net::ERR_INSECURE_RESPONSE));
101 void SSLErrorHandler::ContinueRequest() {
102 DCHECK_CURRENTLY_ON(BrowserThread::UI);
104 // We need to complete this task on the IO thread.
105 BrowserThread::PostTask(
106 BrowserThread::IO, FROM_HERE,
107 base::Bind(&SSLErrorHandler::CompleteContinueRequest, this));
110 void SSLErrorHandler::TakeNoAction() {
111 DCHECK_CURRENTLY_ON(BrowserThread::UI);
113 // We need to complete this task on the IO thread.
114 BrowserThread::PostTask(
115 BrowserThread::IO, FROM_HERE,
116 base::Bind(&SSLErrorHandler::CompleteTakeNoAction, this));
119 void SSLErrorHandler::CompleteCancelRequest(int error) {
120 DCHECK_CURRENTLY_ON(BrowserThread::IO);
122 // It is important that we notify the net::URLRequest only once. If we try
123 // to notify the request twice, it may no longer exist and |this| might have
124 // already have been deleted.
125 DCHECK(!request_has_been_notified_);
126 if (request_has_been_notified_)
127 return;
129 SSLCertErrorHandler* cert_error = AsSSLCertErrorHandler();
130 const SSLInfo* ssl_info = NULL;
131 if (cert_error)
132 ssl_info = &cert_error->ssl_info();
133 if (delegate_.get())
134 delegate_->CancelSSLRequest(error, ssl_info);
135 request_has_been_notified_ = true;
137 // We're done with this object on the IO thread.
138 Release();
141 void SSLErrorHandler::CompleteContinueRequest() {
142 DCHECK_CURRENTLY_ON(BrowserThread::IO);
144 // It is important that we notify the net::URLRequest only once. If we try to
145 // notify the request twice, it may no longer exist and |this| might have
146 // already have been deleted.
147 DCHECK(!request_has_been_notified_);
148 if (request_has_been_notified_)
149 return;
151 if (delegate_.get())
152 delegate_->ContinueSSLRequest();
153 request_has_been_notified_ = true;
155 // We're done with this object on the IO thread.
156 Release();
159 void SSLErrorHandler::CompleteTakeNoAction() {
160 DCHECK_CURRENTLY_ON(BrowserThread::IO);
162 // It is important that we notify the net::URLRequest only once. If we try to
163 // notify the request twice, it may no longer exist and |this| might have
164 // already have been deleted.
165 DCHECK(!request_has_been_notified_);
166 if (request_has_been_notified_)
167 return;
169 request_has_been_notified_ = true;
171 // We're done with this object on the IO thread.
172 Release();
175 } // namespace content