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"
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"
21 SSLErrorHandler::SSLErrorHandler(const base::WeakPtr
<Delegate
>& delegate
,
22 const GlobalRequestID
& id
,
23 ResourceType::Type resource_type
,
25 int render_process_id
,
30 render_process_id_(render_process_id
),
31 render_frame_id_(render_frame_id
),
33 resource_type_(resource_type
),
34 request_has_been_notified_(false) {
35 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI
));
36 DCHECK(delegate
.get());
38 // This makes sure we don't disappear on the IO thread until we've given an
39 // answer to the net::URLRequest.
41 // Release in CompleteCancelRequest, CompleteContinueRequest, or
42 // CompleteTakeNoAction.
46 SSLErrorHandler::~SSLErrorHandler() {}
48 void SSLErrorHandler::OnDispatchFailed() {
52 void SSLErrorHandler::OnDispatched() {
56 SSLCertErrorHandler
* SSLErrorHandler::AsSSLCertErrorHandler() {
60 void SSLErrorHandler::Dispatch() {
61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
63 WebContents
* web_contents
= NULL
;
64 RenderFrameHost
* render_frame_host
=
65 RenderFrameHost::FromID(render_process_id_
, render_frame_id_
);
66 if (render_frame_host
)
67 web_contents
= WebContents::FromRenderFrameHost(render_frame_host
);
70 // We arrived on the UI thread, but the tab we're looking for is no longer
76 // Hand ourselves off to the SSLManager.
78 static_cast<NavigationControllerImpl
*>(&web_contents
->GetController())->
83 void SSLErrorHandler::CancelRequest() {
84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
86 // We need to complete this task on the IO thread.
87 BrowserThread::PostTask(
88 BrowserThread::IO
, FROM_HERE
,
90 &SSLErrorHandler::CompleteCancelRequest
, this, net::ERR_ABORTED
));
93 void SSLErrorHandler::DenyRequest() {
94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
96 // We need to complete this task on the IO thread.
97 BrowserThread::PostTask(
98 BrowserThread::IO
, FROM_HERE
,
100 &SSLErrorHandler::CompleteCancelRequest
, this,
101 net::ERR_INSECURE_RESPONSE
));
104 void SSLErrorHandler::ContinueRequest() {
105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
107 // We need to complete this task on the IO thread.
108 BrowserThread::PostTask(
109 BrowserThread::IO
, FROM_HERE
,
110 base::Bind(&SSLErrorHandler::CompleteContinueRequest
, this));
113 void SSLErrorHandler::TakeNoAction() {
114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
116 // We need to complete this task on the IO thread.
117 BrowserThread::PostTask(
118 BrowserThread::IO
, FROM_HERE
,
119 base::Bind(&SSLErrorHandler::CompleteTakeNoAction
, this));
122 void SSLErrorHandler::CompleteCancelRequest(int error
) {
123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
125 // It is important that we notify the net::URLRequest only once. If we try
126 // to notify the request twice, it may no longer exist and |this| might have
127 // already have been deleted.
128 DCHECK(!request_has_been_notified_
);
129 if (request_has_been_notified_
)
132 SSLCertErrorHandler
* cert_error
= AsSSLCertErrorHandler();
133 const SSLInfo
* ssl_info
= NULL
;
135 ssl_info
= &cert_error
->ssl_info();
137 delegate_
->CancelSSLRequest(request_id_
, error
, ssl_info
);
138 request_has_been_notified_
= true;
140 // We're done with this object on the IO thread.
144 void SSLErrorHandler::CompleteContinueRequest() {
145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
147 // It is important that we notify the net::URLRequest only once. If we try to
148 // notify the request twice, it may no longer exist and |this| might have
149 // already have been deleted.
150 DCHECK(!request_has_been_notified_
);
151 if (request_has_been_notified_
)
155 delegate_
->ContinueSSLRequest(request_id_
);
156 request_has_been_notified_
= true;
158 // We're done with this object on the IO thread.
162 void SSLErrorHandler::CompleteTakeNoAction() {
163 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
165 // It is important that we notify the net::URLRequest only once. If we try to
166 // notify the request twice, it may no longer exist and |this| might have
167 // already have been deleted.
168 DCHECK(!request_has_been_notified_
);
169 if (request_has_been_notified_
)
172 request_has_been_notified_
= true;
174 // We're done with this object on the IO thread.
178 } // namespace content