Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / ssl / ssl_tab_helper.cc
bloba1d5a0b88cce0c14cdbd1c68a157eea3f68f3389
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 "chrome/browser/ssl/ssl_tab_helper.h"
7 #include <string>
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "base/command_line.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/values.h"
15 #include "chrome/browser/certificate_viewer.h"
16 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/content_settings/host_content_settings_map.h"
18 #include "chrome/browser/infobars/confirm_infobar_delegate.h"
19 #include "chrome/browser/infobars/infobar_service.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/ssl/ssl_add_cert_handler.h"
22 #include "chrome/browser/ssl/ssl_client_certificate_selector.h"
23 #include "chrome/browser/ui/browser_finder.h"
24 #include "chrome/common/chrome_switches.h"
25 #include "components/infobars/core/infobar.h"
26 #include "content/public/browser/notification_details.h"
27 #include "content/public/browser/notification_observer.h"
28 #include "content/public/browser/notification_registrar.h"
29 #include "content/public/browser/notification_source.h"
30 #include "content/public/browser/web_contents.h"
31 #include "grit/generated_resources.h"
32 #include "grit/theme_resources.h"
33 #include "net/base/net_errors.h"
34 #include "net/cert/x509_certificate.h"
35 #include "ui/base/l10n/l10n_util.h"
38 // SSLCertResultInfoBarDelegate -----------------------------------------------
40 namespace {
42 class SSLCertResultInfoBarDelegate : public ConfirmInfoBarDelegate {
43 public:
44 // Creates an SSL cert result infobar and delegate. If |previous_infobar| is
45 // NULL, adds the infobar to |infobar_service|; otherwise, replaces
46 // |previous_infobar|. Returns the new infobar if it was successfully added.
47 // |cert| is valid iff cert addition was successful.
48 static infobars::InfoBar* Create(InfoBarService* infobar_service,
49 infobars::InfoBar* previous_infobar,
50 const base::string16& message,
51 net::X509Certificate* cert);
53 private:
54 SSLCertResultInfoBarDelegate(const base::string16& message,
55 net::X509Certificate* cert);
56 virtual ~SSLCertResultInfoBarDelegate();
58 // ConfirmInfoBarDelegate:
59 virtual int GetIconID() const OVERRIDE;
60 virtual Type GetInfoBarType() const OVERRIDE;
61 virtual base::string16 GetMessageText() const OVERRIDE;
62 virtual int GetButtons() const OVERRIDE;
63 virtual base::string16 GetButtonLabel(InfoBarButton button) const OVERRIDE;
64 virtual bool Accept() OVERRIDE;
66 base::string16 message_;
67 scoped_refptr<net::X509Certificate> cert_; // The cert we added, if any.
69 DISALLOW_COPY_AND_ASSIGN(SSLCertResultInfoBarDelegate);
72 // static
73 infobars::InfoBar* SSLCertResultInfoBarDelegate::Create(
74 InfoBarService* infobar_service,
75 infobars::InfoBar* previous_infobar,
76 const base::string16& message,
77 net::X509Certificate* cert) {
78 scoped_ptr<infobars::InfoBar> infobar(
79 ConfirmInfoBarDelegate::CreateInfoBar(scoped_ptr<ConfirmInfoBarDelegate>(
80 new SSLCertResultInfoBarDelegate(message, cert))));
81 return previous_infobar ?
82 infobar_service->ReplaceInfoBar(previous_infobar, infobar.Pass()) :
83 infobar_service->AddInfoBar(infobar.Pass());
86 SSLCertResultInfoBarDelegate::SSLCertResultInfoBarDelegate(
87 const base::string16& message,
88 net::X509Certificate* cert)
89 : ConfirmInfoBarDelegate(),
90 message_(message),
91 cert_(cert) {
94 SSLCertResultInfoBarDelegate::~SSLCertResultInfoBarDelegate() {
97 int SSLCertResultInfoBarDelegate::GetIconID() const {
98 // TODO(davidben): use a more appropriate icon.
99 return IDR_INFOBAR_SAVE_PASSWORD;
102 infobars::InfoBarDelegate::Type SSLCertResultInfoBarDelegate::GetInfoBarType()
103 const {
104 return cert_.get() ? PAGE_ACTION_TYPE : WARNING_TYPE;
107 base::string16 SSLCertResultInfoBarDelegate::GetMessageText() const {
108 return message_;
111 int SSLCertResultInfoBarDelegate::GetButtons() const {
112 return cert_.get() ? BUTTON_OK : BUTTON_NONE;
115 base::string16 SSLCertResultInfoBarDelegate::GetButtonLabel(
116 InfoBarButton button) const {
117 DCHECK_EQ(BUTTON_OK, button);
118 return l10n_util::GetStringUTF16(IDS_ADD_CERT_SUCCESS_INFOBAR_BUTTON);
121 bool SSLCertResultInfoBarDelegate::Accept() {
122 content::WebContents* web_contents =
123 InfoBarService::WebContentsFromInfoBar(infobar());
124 ShowCertificateViewer(web_contents,
125 web_contents->GetTopLevelNativeWindow(),
126 cert_.get());
127 return false; // Hiding the infobar just as the dialog opens looks weird.
130 } // namespace
133 // SSLTabHelper::SSLAddCertData ------------------------------------------------
135 class SSLTabHelper::SSLAddCertData
136 : public content::NotificationObserver {
137 public:
138 explicit SSLAddCertData(InfoBarService* infobar_service);
139 virtual ~SSLAddCertData();
141 // Displays an infobar, replacing |infobar_| if it exists.
142 void ShowInfoBar(const base::string16& message, net::X509Certificate* cert);
144 private:
145 // content::NotificationObserver:
146 virtual void Observe(int type,
147 const content::NotificationSource& source,
148 const content::NotificationDetails& details) OVERRIDE;
150 InfoBarService* infobar_service_;
151 infobars::InfoBar* infobar_;
152 content::NotificationRegistrar registrar_;
154 DISALLOW_COPY_AND_ASSIGN(SSLAddCertData);
157 SSLTabHelper::SSLAddCertData::SSLAddCertData(InfoBarService* infobar_service)
158 : infobar_service_(infobar_service),
159 infobar_(NULL) {
160 content::Source<InfoBarService> source(infobar_service_);
161 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
162 source);
163 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED,
164 source);
167 SSLTabHelper::SSLAddCertData::~SSLAddCertData() {
170 void SSLTabHelper::SSLAddCertData::ShowInfoBar(const base::string16& message,
171 net::X509Certificate* cert) {
172 infobar_ = SSLCertResultInfoBarDelegate::Create(infobar_service_, infobar_,
173 message, cert);
176 void SSLTabHelper::SSLAddCertData::Observe(
177 int type,
178 const content::NotificationSource& source,
179 const content::NotificationDetails& details) {
180 DCHECK(type == chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED ||
181 type == chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED);
182 if (infobar_ ==
183 ((type == chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED) ?
184 content::Details<infobars::InfoBar::RemovedDetails>(
185 details)->first :
186 content::Details<infobars::InfoBar::ReplacedDetails>(
187 details)->first)) {
188 infobar_ = NULL;
193 // SSLTabHelper ----------------------------------------------------------------
195 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SSLTabHelper);
197 SSLTabHelper::SSLTabHelper(content::WebContents* contents)
198 : WebContentsObserver(contents),
199 web_contents_(contents) {
202 SSLTabHelper::~SSLTabHelper() {
205 void SSLTabHelper::DidChangeVisibleSSLState() {
206 #if !defined(OS_ANDROID)
207 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
208 if (browser)
209 browser->VisibleSSLStateChanged(web_contents_);
210 #endif // !defined(OS_ANDROID)
213 void SSLTabHelper::ShowClientCertificateRequestDialog(
214 const net::HttpNetworkSession* network_session,
215 net::SSLCertRequestInfo* cert_request_info,
216 const base::Callback<void(net::X509Certificate*)>& callback) {
217 chrome::ShowSSLClientCertificateSelector(web_contents_, network_session,
218 cert_request_info, callback);
221 void SSLTabHelper::OnVerifyClientCertificateError(
222 scoped_refptr<SSLAddCertHandler> handler, int error_code) {
223 // Display an infobar with the error message.
224 // TODO(davidben): Display a more user-friendly error string.
225 GetAddCertData(handler.get())->ShowInfoBar(
226 l10n_util::GetStringFUTF16(IDS_ADD_CERT_ERR_INVALID_CERT,
227 base::IntToString16(-error_code),
228 base::ASCIIToUTF16(
229 net::ErrorToString(error_code))),
230 NULL);
233 void SSLTabHelper::AskToAddClientCertificate(
234 scoped_refptr<SSLAddCertHandler> handler) {
235 NOTREACHED(); // Not implemented yet.
238 void SSLTabHelper::OnAddClientCertificateSuccess(
239 scoped_refptr<SSLAddCertHandler> handler) {
240 net::X509Certificate* cert = handler->cert();
241 // TODO(evanm): GetDisplayName should return UTF-16.
242 GetAddCertData(handler.get())->ShowInfoBar(
243 l10n_util::GetStringFUTF16(IDS_ADD_CERT_SUCCESS_INFOBAR_LABEL,
244 base::UTF8ToUTF16(
245 cert->issuer().GetDisplayName())),
246 cert);
249 void SSLTabHelper::OnAddClientCertificateError(
250 scoped_refptr<SSLAddCertHandler> handler,
251 int error_code) {
252 // TODO(davidben): Display a more user-friendly error string.
253 GetAddCertData(handler.get())->ShowInfoBar(
254 l10n_util::GetStringFUTF16(IDS_ADD_CERT_ERR_FAILED,
255 base::IntToString16(-error_code),
256 base::ASCIIToUTF16(
257 net::ErrorToString(error_code))),
258 NULL);
261 void SSLTabHelper::OnAddClientCertificateFinished(
262 scoped_refptr<SSLAddCertHandler> handler) {
263 // Clean up.
264 request_id_to_add_cert_data_.erase(handler->network_request_id());
267 SSLTabHelper::SSLAddCertData*
268 SSLTabHelper::GetAddCertData(SSLAddCertHandler* handler) {
269 // Find/create the slot.
270 linked_ptr<SSLAddCertData>& ptr_ref =
271 request_id_to_add_cert_data_[handler->network_request_id()];
272 // Fill it if necessary.
273 if (!ptr_ref.get()) {
274 ptr_ref.reset(
275 new SSLAddCertData(InfoBarService::FromWebContents(web_contents_)));
277 return ptr_ref.get();