[email protected] | 35b9ae79 | 2012-02-28 00:03:17 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 74b962a | 2011-06-03 21:22:54 | [diff] [blame] | 5 | #include "content/browser/ssl/ssl_error_handler.h" |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 6 | |
[email protected] | 8a27abf | 2011-09-30 21:59:58 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | d4a8ca48 | 2013-10-30 21:06:40 | [diff] [blame] | 8 | #include "content/browser/frame_host/navigation_controller_impl.h" |
[email protected] | 1a4e975 | 2013-12-31 20:10:58 | [diff] [blame] | 9 | #include "content/browser/frame_host/render_frame_host_impl.h" |
[email protected] | 74b962a | 2011-06-03 21:22:54 | [diff] [blame] | 10 | #include "content/browser/ssl/ssl_cert_error_handler.h" |
[email protected] | 93ddb3c | 2012-04-11 21:44:29 | [diff] [blame] | 11 | #include "content/browser/web_contents/web_contents_impl.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 12 | #include "content/public/browser/browser_thread.h" |
[email protected] | ea11472 | 2012-03-12 01:11:25 | [diff] [blame] | 13 | #include "content/public/browser/resource_request_info.h" |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 14 | #include "net/base/net_errors.h" |
| 15 | #include "net/url_request/url_request.h" |
| 16 | |
[email protected] | 043cc11 | 2012-03-13 02:24:34 | [diff] [blame] | 17 | using net::SSLInfo; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 18 | |
[email protected] | 89f23a3 | 2012-10-24 22:31:24 | [diff] [blame] | 19 | namespace content { |
| 20 | |
[email protected] | 5385c44 | 2012-05-31 11:01:32 | [diff] [blame] | 21 | SSLErrorHandler::SSLErrorHandler(const base::WeakPtr<Delegate>& delegate, |
[email protected] | 6c1e0521 | 2014-07-31 00:59:40 | [diff] [blame] | 22 | ResourceType resource_type, |
clamy | 0d32d6d | 2015-11-24 11:16:26 | [diff] [blame] | 23 | const GURL& url) |
[email protected] | 0d3dc8e2 | 2009-11-03 02:27:01 | [diff] [blame] | 24 | : manager_(NULL), |
[email protected] | 043cc11 | 2012-03-13 02:24:34 | [diff] [blame] | 25 | delegate_(delegate), |
[email protected] | 043cc11 | 2012-03-13 02:24:34 | [diff] [blame] | 26 | request_url_(url), |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 27 | resource_type_(resource_type), |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 28 | request_has_been_notified_(false) { |
[email protected] | 9a7e150 | 2010-10-08 04:03:50 | [diff] [blame] | 29 | DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | c679b2a8 | 2013-06-03 21:25:01 | [diff] [blame] | 30 | DCHECK(delegate.get()); |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 31 | |
| 32 | // This makes sure we don't disappear on the IO thread until we've given an |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 33 | // answer to the net::URLRequest. |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 34 | // |
[email protected] | 8a58f9a | 2010-05-18 18:38:09 | [diff] [blame] | 35 | // Release in CompleteCancelRequest, CompleteContinueRequest, or |
| 36 | // CompleteTakeNoAction. |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 37 | AddRef(); |
| 38 | } |
| 39 | |
[email protected] | 02d08e0 | 2010-10-08 17:50:46 | [diff] [blame] | 40 | SSLErrorHandler::~SSLErrorHandler() {} |
| 41 | |
| 42 | void SSLErrorHandler::OnDispatchFailed() { |
| 43 | TakeNoAction(); |
| 44 | } |
| 45 | |
| 46 | void SSLErrorHandler::OnDispatched() { |
| 47 | TakeNoAction(); |
| 48 | } |
| 49 | |
[email protected] | e4be2dd | 2010-12-14 00:44:39 | [diff] [blame] | 50 | SSLCertErrorHandler* SSLErrorHandler::AsSSLCertErrorHandler() { |
| 51 | return NULL; |
| 52 | } |
| 53 | |
clamy | 0d32d6d | 2015-11-24 11:16:26 | [diff] [blame] | 54 | void SSLErrorHandler::Dispatch( |
| 55 | const base::Callback<WebContents*(void)>& web_contents_getter) { |
mostynb | 042582e | 2015-03-16 22:13:40 | [diff] [blame] | 56 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 57 | |
clamy | 0d32d6d | 2015-11-24 11:16:26 | [diff] [blame] | 58 | WebContents* web_contents = web_contents_getter.Run(); |
[email protected] | 8ec2647 | 2011-06-06 16:52:45 | [diff] [blame] | 59 | |
[email protected] | 52f89a4 | 2012-01-24 20:54:50 | [diff] [blame] | 60 | if (!web_contents) { |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 61 | // 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] | 330614de | 2012-02-13 17:07:18 | [diff] [blame] | 68 | manager_ = |
| 69 | static_cast<NavigationControllerImpl*>(&web_contents->GetController())-> |
| 70 | ssl_manager(); |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 71 | OnDispatched(); |
| 72 | } |
| 73 | |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 74 | void SSLErrorHandler::CancelRequest() { |
mostynb | 042582e | 2015-03-16 22:13:40 | [diff] [blame] | 75 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 76 | |
| 77 | // We need to complete this task on the IO thread. |
[email protected] | 9a7e150 | 2010-10-08 04:03:50 | [diff] [blame] | 78 | BrowserThread::PostTask( |
| 79 | BrowserThread::IO, FROM_HERE, |
[email protected] | 8a27abf | 2011-09-30 21:59:58 | [diff] [blame] | 80 | base::Bind( |
| 81 | &SSLErrorHandler::CompleteCancelRequest, this, net::ERR_ABORTED)); |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void SSLErrorHandler::DenyRequest() { |
mostynb | 042582e | 2015-03-16 22:13:40 | [diff] [blame] | 85 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 86 | |
| 87 | // We need to complete this task on the IO thread. |
[email protected] | 9a7e150 | 2010-10-08 04:03:50 | [diff] [blame] | 88 | BrowserThread::PostTask( |
| 89 | BrowserThread::IO, FROM_HERE, |
[email protected] | 8a27abf | 2011-09-30 21:59:58 | [diff] [blame] | 90 | base::Bind( |
| 91 | &SSLErrorHandler::CompleteCancelRequest, this, |
[email protected] | 0d3dc8e2 | 2009-11-03 02:27:01 | [diff] [blame] | 92 | net::ERR_INSECURE_RESPONSE)); |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void SSLErrorHandler::ContinueRequest() { |
mostynb | 042582e | 2015-03-16 22:13:40 | [diff] [blame] | 96 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 97 | |
| 98 | // We need to complete this task on the IO thread. |
[email protected] | 9a7e150 | 2010-10-08 04:03:50 | [diff] [blame] | 99 | BrowserThread::PostTask( |
| 100 | BrowserThread::IO, FROM_HERE, |
[email protected] | 8a27abf | 2011-09-30 21:59:58 | [diff] [blame] | 101 | base::Bind(&SSLErrorHandler::CompleteContinueRequest, this)); |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 102 | } |
| 103 | |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 104 | void SSLErrorHandler::TakeNoAction() { |
mostynb | 042582e | 2015-03-16 22:13:40 | [diff] [blame] | 105 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 106 | |
| 107 | // We need to complete this task on the IO thread. |
[email protected] | 9a7e150 | 2010-10-08 04:03:50 | [diff] [blame] | 108 | BrowserThread::PostTask( |
| 109 | BrowserThread::IO, FROM_HERE, |
[email protected] | 8a27abf | 2011-09-30 21:59:58 | [diff] [blame] | 110 | base::Bind(&SSLErrorHandler::CompleteTakeNoAction, this)); |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 111 | } |
| 112 | |
clamy | 0d32d6d | 2015-11-24 11:16:26 | [diff] [blame] | 113 | SSLManager* SSLErrorHandler::GetManager() const { |
| 114 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 115 | return manager_; |
| 116 | } |
| 117 | |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 118 | void SSLErrorHandler::CompleteCancelRequest(int error) { |
mostynb | 042582e | 2015-03-16 22:13:40 | [diff] [blame] | 119 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 120 | |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 121 | // 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] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 123 | // already have been deleted. |
| 124 | DCHECK(!request_has_been_notified_); |
[email protected] | 67039c73 | 2009-05-14 07:50:35 | [diff] [blame] | 125 | if (request_has_been_notified_) |
| 126 | return; |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 127 | |
[email protected] | 043cc11 | 2012-03-13 02:24:34 | [diff] [blame] | 128 | SSLCertErrorHandler* cert_error = AsSSLCertErrorHandler(); |
| 129 | const SSLInfo* ssl_info = NULL; |
| 130 | if (cert_error) |
| 131 | ssl_info = &cert_error->ssl_info(); |
[email protected] | c679b2a8 | 2013-06-03 21:25:01 | [diff] [blame] | 132 | if (delegate_.get()) |
davidben | 21163ec | 2014-10-01 23:05:23 | [diff] [blame] | 133 | delegate_->CancelSSLRequest(error, ssl_info); |
[email protected] | 67039c73 | 2009-05-14 07:50:35 | [diff] [blame] | 134 | request_has_been_notified_ = true; |
| 135 | |
| 136 | // We're done with this object on the IO thread. |
| 137 | Release(); |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void SSLErrorHandler::CompleteContinueRequest() { |
mostynb | 042582e | 2015-03-16 22:13:40 | [diff] [blame] | 141 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 142 | |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 143 | // It is important that we notify the net::URLRequest only once. If we try to |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 144 | // 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] | 67039c73 | 2009-05-14 07:50:35 | [diff] [blame] | 147 | if (request_has_been_notified_) |
| 148 | return; |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 149 | |
[email protected] | c679b2a8 | 2013-06-03 21:25:01 | [diff] [blame] | 150 | if (delegate_.get()) |
davidben | 21163ec | 2014-10-01 23:05:23 | [diff] [blame] | 151 | delegate_->ContinueSSLRequest(); |
[email protected] | 67039c73 | 2009-05-14 07:50:35 | [diff] [blame] | 152 | request_has_been_notified_ = true; |
| 153 | |
| 154 | // We're done with this object on the IO thread. |
| 155 | Release(); |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 156 | } |
| 157 | |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 158 | void SSLErrorHandler::CompleteTakeNoAction() { |
mostynb | 042582e | 2015-03-16 22:13:40 | [diff] [blame] | 159 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 160 | |
[email protected] | 6981d963 | 2010-11-30 21:34:02 | [diff] [blame] | 161 | // It is important that we notify the net::URLRequest only once. If we try to |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 162 | // 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] | 67039c73 | 2009-05-14 07:50:35 | [diff] [blame] | 165 | if (request_has_been_notified_) |
| 166 | return; |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 167 | |
[email protected] | 67039c73 | 2009-05-14 07:50:35 | [diff] [blame] | 168 | request_has_been_notified_ = true; |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 169 | |
[email protected] | 67039c73 | 2009-05-14 07:50:35 | [diff] [blame] | 170 | // We're done with this object on the IO thread. |
| 171 | Release(); |
[email protected] | 1d89a82f | 2009-05-14 05:46:24 | [diff] [blame] | 172 | } |
[email protected] | 89f23a3 | 2012-10-24 22:31:24 | [diff] [blame] | 173 | |
| 174 | } // namespace content |