blob: 40b2b564506286c92013d699305ea6652399ac86 [file] [log] [blame]
[email protected]35b9ae792012-02-28 00:03:171// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]1d89a82f2009-05-14 05:46:242// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]74b962a2011-06-03 21:22:545#include "content/browser/ssl/ssl_error_handler.h"
[email protected]1d89a82f2009-05-14 05:46:246
[email protected]8a27abf2011-09-30 21:59:587#include "base/bind.h"
[email protected]d4a8ca482013-10-30 21:06:408#include "content/browser/frame_host/navigation_controller_impl.h"
[email protected]1a4e9752013-12-31 20:10:589#include "content/browser/frame_host/render_frame_host_impl.h"
[email protected]74b962a2011-06-03 21:22:5410#include "content/browser/ssl/ssl_cert_error_handler.h"
[email protected]93ddb3c2012-04-11 21:44:2911#include "content/browser/web_contents/web_contents_impl.h"
[email protected]c38831a12011-10-28 12:44:4912#include "content/public/browser/browser_thread.h"
[email protected]ea114722012-03-12 01:11:2513#include "content/public/browser/resource_request_info.h"
[email protected]1d89a82f2009-05-14 05:46:2414#include "net/base/net_errors.h"
15#include "net/url_request/url_request.h"
16
[email protected]043cc112012-03-13 02:24:3417using net::SSLInfo;
[email protected]631bb742011-11-02 11:29:3918
[email protected]89f23a32012-10-24 22:31:2419namespace content {
20
[email protected]5385c442012-05-31 11:01:3221SSLErrorHandler::SSLErrorHandler(const base::WeakPtr<Delegate>& delegate,
[email protected]6c1e05212014-07-31 00:59:4022 ResourceType resource_type,
clamy0d32d6d2015-11-24 11:16:2623 const GURL& url)
[email protected]0d3dc8e22009-11-03 02:27:0124 : manager_(NULL),
[email protected]043cc112012-03-13 02:24:3425 delegate_(delegate),
[email protected]043cc112012-03-13 02:24:3426 request_url_(url),
[email protected]1d89a82f2009-05-14 05:46:2427 resource_type_(resource_type),
[email protected]1d89a82f2009-05-14 05:46:2428 request_has_been_notified_(false) {
[email protected]9a7e1502010-10-08 04:03:5029 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]c679b2a82013-06-03 21:25:0130 DCHECK(delegate.get());
[email protected]1d89a82f2009-05-14 05:46:2431
32 // This makes sure we don't disappear on the IO thread until we've given an
[email protected]6981d9632010-11-30 21:34:0233 // answer to the net::URLRequest.
[email protected]1d89a82f2009-05-14 05:46:2434 //
[email protected]8a58f9a2010-05-18 18:38:0935 // Release in CompleteCancelRequest, CompleteContinueRequest, or
36 // CompleteTakeNoAction.
[email protected]1d89a82f2009-05-14 05:46:2437 AddRef();
38}
39
[email protected]02d08e02010-10-08 17:50:4640SSLErrorHandler::~SSLErrorHandler() {}
41
42void SSLErrorHandler::OnDispatchFailed() {
43 TakeNoAction();
44}
45
46void SSLErrorHandler::OnDispatched() {
47 TakeNoAction();
48}
49
[email protected]e4be2dd2010-12-14 00:44:3950SSLCertErrorHandler* SSLErrorHandler::AsSSLCertErrorHandler() {
51 return NULL;
52}
53
clamy0d32d6d2015-11-24 11:16:2654void SSLErrorHandler::Dispatch(
55 const base::Callback<WebContents*(void)>& web_contents_getter) {
mostynb042582e2015-03-16 22:13:4056 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]1d89a82f2009-05-14 05:46:2457
clamy0d32d6d2015-11-24 11:16:2658 WebContents* web_contents = web_contents_getter.Run();
[email protected]8ec26472011-06-06 16:52:4559
[email protected]52f89a42012-01-24 20:54:5060 if (!web_contents) {
[email protected]1d89a82f2009-05-14 05:46:2461 // We arrived on the UI thread, but the tab we're looking for is no longer
62 // here.
63 OnDispatchFailed();
64 return;
65 }
66
67 // Hand ourselves off to the SSLManager.
[email protected]330614de2012-02-13 17:07:1868 manager_ =
69 static_cast<NavigationControllerImpl*>(&web_contents->GetController())->
70 ssl_manager();
[email protected]1d89a82f2009-05-14 05:46:2471 OnDispatched();
72}
73
[email protected]1d89a82f2009-05-14 05:46:2474void SSLErrorHandler::CancelRequest() {
mostynb042582e2015-03-16 22:13:4075 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]1d89a82f2009-05-14 05:46:2476
77 // We need to complete this task on the IO thread.
[email protected]9a7e1502010-10-08 04:03:5078 BrowserThread::PostTask(
79 BrowserThread::IO, FROM_HERE,
[email protected]8a27abf2011-09-30 21:59:5880 base::Bind(
81 &SSLErrorHandler::CompleteCancelRequest, this, net::ERR_ABORTED));
[email protected]1d89a82f2009-05-14 05:46:2482}
83
84void SSLErrorHandler::DenyRequest() {
mostynb042582e2015-03-16 22:13:4085 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]1d89a82f2009-05-14 05:46:2486
87 // We need to complete this task on the IO thread.
[email protected]9a7e1502010-10-08 04:03:5088 BrowserThread::PostTask(
89 BrowserThread::IO, FROM_HERE,
[email protected]8a27abf2011-09-30 21:59:5890 base::Bind(
91 &SSLErrorHandler::CompleteCancelRequest, this,
[email protected]0d3dc8e22009-11-03 02:27:0192 net::ERR_INSECURE_RESPONSE));
[email protected]1d89a82f2009-05-14 05:46:2493}
94
95void SSLErrorHandler::ContinueRequest() {
mostynb042582e2015-03-16 22:13:4096 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]1d89a82f2009-05-14 05:46:2497
98 // We need to complete this task on the IO thread.
[email protected]9a7e1502010-10-08 04:03:5099 BrowserThread::PostTask(
100 BrowserThread::IO, FROM_HERE,
[email protected]8a27abf2011-09-30 21:59:58101 base::Bind(&SSLErrorHandler::CompleteContinueRequest, this));
[email protected]1d89a82f2009-05-14 05:46:24102}
103
[email protected]1d89a82f2009-05-14 05:46:24104void SSLErrorHandler::TakeNoAction() {
mostynb042582e2015-03-16 22:13:40105 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]1d89a82f2009-05-14 05:46:24106
107 // We need to complete this task on the IO thread.
[email protected]9a7e1502010-10-08 04:03:50108 BrowserThread::PostTask(
109 BrowserThread::IO, FROM_HERE,
[email protected]8a27abf2011-09-30 21:59:58110 base::Bind(&SSLErrorHandler::CompleteTakeNoAction, this));
[email protected]1d89a82f2009-05-14 05:46:24111}
112
clamy0d32d6d2015-11-24 11:16:26113SSLManager* SSLErrorHandler::GetManager() const {
114 DCHECK_CURRENTLY_ON(BrowserThread::UI);
115 return manager_;
116}
117
[email protected]1d89a82f2009-05-14 05:46:24118void SSLErrorHandler::CompleteCancelRequest(int error) {
mostynb042582e2015-03-16 22:13:40119 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]1d89a82f2009-05-14 05:46:24120
[email protected]6981d9632010-11-30 21:34:02121 // It is important that we notify the net::URLRequest only once. If we try
122 // to notify the request twice, it may no longer exist and |this| might have
[email protected]1d89a82f2009-05-14 05:46:24123 // already have been deleted.
124 DCHECK(!request_has_been_notified_);
[email protected]67039c732009-05-14 07:50:35125 if (request_has_been_notified_)
126 return;
[email protected]1d89a82f2009-05-14 05:46:24127
[email protected]043cc112012-03-13 02:24:34128 SSLCertErrorHandler* cert_error = AsSSLCertErrorHandler();
129 const SSLInfo* ssl_info = NULL;
130 if (cert_error)
131 ssl_info = &cert_error->ssl_info();
[email protected]c679b2a82013-06-03 21:25:01132 if (delegate_.get())
davidben21163ec2014-10-01 23:05:23133 delegate_->CancelSSLRequest(error, ssl_info);
[email protected]67039c732009-05-14 07:50:35134 request_has_been_notified_ = true;
135
136 // We're done with this object on the IO thread.
137 Release();
[email protected]1d89a82f2009-05-14 05:46:24138}
139
140void SSLErrorHandler::CompleteContinueRequest() {
mostynb042582e2015-03-16 22:13:40141 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]1d89a82f2009-05-14 05:46:24142
[email protected]6981d9632010-11-30 21:34:02143 // It is important that we notify the net::URLRequest only once. If we try to
[email protected]1d89a82f2009-05-14 05:46:24144 // notify the request twice, it may no longer exist and |this| might have
145 // already have been deleted.
146 DCHECK(!request_has_been_notified_);
[email protected]67039c732009-05-14 07:50:35147 if (request_has_been_notified_)
148 return;
[email protected]1d89a82f2009-05-14 05:46:24149
[email protected]c679b2a82013-06-03 21:25:01150 if (delegate_.get())
davidben21163ec2014-10-01 23:05:23151 delegate_->ContinueSSLRequest();
[email protected]67039c732009-05-14 07:50:35152 request_has_been_notified_ = true;
153
154 // We're done with this object on the IO thread.
155 Release();
[email protected]1d89a82f2009-05-14 05:46:24156}
157
[email protected]1d89a82f2009-05-14 05:46:24158void SSLErrorHandler::CompleteTakeNoAction() {
mostynb042582e2015-03-16 22:13:40159 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]1d89a82f2009-05-14 05:46:24160
[email protected]6981d9632010-11-30 21:34:02161 // It is important that we notify the net::URLRequest only once. If we try to
[email protected]1d89a82f2009-05-14 05:46:24162 // notify the request twice, it may no longer exist and |this| might have
163 // already have been deleted.
164 DCHECK(!request_has_been_notified_);
[email protected]67039c732009-05-14 07:50:35165 if (request_has_been_notified_)
166 return;
[email protected]1d89a82f2009-05-14 05:46:24167
[email protected]67039c732009-05-14 07:50:35168 request_has_been_notified_ = true;
[email protected]1d89a82f2009-05-14 05:46:24169
[email protected]67039c732009-05-14 07:50:35170 // We're done with this object on the IO thread.
171 Release();
[email protected]1d89a82f2009-05-14 05:46:24172}
[email protected]89f23a32012-10-24 22:31:24173
174} // namespace content