Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ssl/ssl_tab_helper.h" | 5 #include "chrome/browser/ssl/ssl_tab_helper.h" |
| 6 | 6 |
| 7 #include <string> | |
| 8 #include <vector> | |
| 9 | |
| 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/infobar_service.h" | |
| 19 #include "chrome/browser/profiles/profile.h" | |
| 20 #include "chrome/browser/ssl/ssl_add_cert_handler.h" | |
| 21 #include "chrome/browser/ssl/ssl_client_certificate_selector.h" | |
| 22 #include "chrome/browser/ui/browser_finder.h" | 7 #include "chrome/browser/ui/browser_finder.h" |
| 23 #include "chrome/common/chrome_switches.h" | |
| 24 #include "components/infobars/core/confirm_infobar_delegate.h" | |
| 25 #include "components/infobars/core/infobar.h" | |
| 26 #include "components/infobars/core/infobar_manager.h" | |
| 27 #include "content/public/browser/web_contents.h" | 8 #include "content/public/browser/web_contents.h" |
| 28 #include "grit/generated_resources.h" | |
| 29 #include "grit/theme_resources.h" | |
| 30 #include "net/base/net_errors.h" | |
| 31 #include "net/cert/x509_certificate.h" | |
| 32 #include "ui/base/l10n/l10n_util.h" | |
| 33 | |
| 34 | |
| 35 // SSLCertResultInfoBarDelegate ----------------------------------------------- | |
| 36 | |
| 37 namespace { | |
| 38 | |
| 39 class SSLCertResultInfoBarDelegate : public ConfirmInfoBarDelegate { | |
| 40 public: | |
| 41 // Creates an SSL cert result infobar and delegate. If |previous_infobar| is | |
| 42 // NULL, adds the infobar to |infobar_service|; otherwise, replaces | |
| 43 // |previous_infobar|. Returns the new infobar if it was successfully added. | |
| 44 // |cert| is valid iff cert addition was successful. | |
| 45 static infobars::InfoBar* Create(InfoBarService* infobar_service, | |
| 46 infobars::InfoBar* previous_infobar, | |
| 47 const base::string16& message, | |
| 48 net::X509Certificate* cert); | |
| 49 | |
| 50 private: | |
| 51 SSLCertResultInfoBarDelegate(const base::string16& message, | |
| 52 net::X509Certificate* cert); | |
| 53 virtual ~SSLCertResultInfoBarDelegate(); | |
| 54 | |
| 55 // ConfirmInfoBarDelegate: | |
| 56 virtual int GetIconID() const OVERRIDE; | |
| 57 virtual Type GetInfoBarType() const OVERRIDE; | |
| 58 virtual base::string16 GetMessageText() const OVERRIDE; | |
| 59 virtual int GetButtons() const OVERRIDE; | |
| 60 virtual base::string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; | |
| 61 virtual bool Accept() OVERRIDE; | |
| 62 | |
| 63 base::string16 message_; | |
| 64 scoped_refptr<net::X509Certificate> cert_; // The cert we added, if any. | |
| 65 | |
| 66 DISALLOW_COPY_AND_ASSIGN(SSLCertResultInfoBarDelegate); | |
| 67 }; | |
| 68 | |
| 69 // static | |
| 70 infobars::InfoBar* SSLCertResultInfoBarDelegate::Create( | |
| 71 InfoBarService* infobar_service, | |
| 72 infobars::InfoBar* previous_infobar, | |
| 73 const base::string16& message, | |
| 74 net::X509Certificate* cert) { | |
| 75 scoped_ptr<infobars::InfoBar> infobar( | |
| 76 ConfirmInfoBarDelegate::CreateInfoBar(scoped_ptr<ConfirmInfoBarDelegate>( | |
| 77 new SSLCertResultInfoBarDelegate(message, cert)))); | |
| 78 return previous_infobar ? | |
| 79 infobar_service->ReplaceInfoBar(previous_infobar, infobar.Pass()) : | |
| 80 infobar_service->AddInfoBar(infobar.Pass()); | |
| 81 } | |
| 82 | |
| 83 SSLCertResultInfoBarDelegate::SSLCertResultInfoBarDelegate( | |
| 84 const base::string16& message, | |
| 85 net::X509Certificate* cert) | |
| 86 : ConfirmInfoBarDelegate(), | |
| 87 message_(message), | |
| 88 cert_(cert) { | |
| 89 } | |
| 90 | |
| 91 SSLCertResultInfoBarDelegate::~SSLCertResultInfoBarDelegate() { | |
| 92 } | |
| 93 | |
| 94 int SSLCertResultInfoBarDelegate::GetIconID() const { | |
| 95 // TODO(davidben): use a more appropriate icon. | |
| 96 return IDR_INFOBAR_SAVE_PASSWORD; | |
| 97 } | |
| 98 | |
| 99 infobars::InfoBarDelegate::Type SSLCertResultInfoBarDelegate::GetInfoBarType() | |
| 100 const { | |
| 101 return cert_.get() ? PAGE_ACTION_TYPE : WARNING_TYPE; | |
| 102 } | |
| 103 | |
| 104 base::string16 SSLCertResultInfoBarDelegate::GetMessageText() const { | |
| 105 return message_; | |
| 106 } | |
| 107 | |
| 108 int SSLCertResultInfoBarDelegate::GetButtons() const { | |
| 109 return cert_.get() ? BUTTON_OK : BUTTON_NONE; | |
| 110 } | |
| 111 | |
| 112 base::string16 SSLCertResultInfoBarDelegate::GetButtonLabel( | |
| 113 InfoBarButton button) const { | |
| 114 DCHECK_EQ(BUTTON_OK, button); | |
| 115 return l10n_util::GetStringUTF16(IDS_ADD_CERT_SUCCESS_INFOBAR_BUTTON); | |
| 116 } | |
| 117 | |
| 118 bool SSLCertResultInfoBarDelegate::Accept() { | |
| 119 content::WebContents* web_contents = | |
| 120 InfoBarService::WebContentsFromInfoBar(infobar()); | |
| 121 ShowCertificateViewer(web_contents, | |
| 122 web_contents->GetTopLevelNativeWindow(), | |
| 123 cert_.get()); | |
| 124 return false; // Hiding the infobar just as the dialog opens looks weird. | |
| 125 } | |
| 126 | |
| 127 } // namespace | |
| 128 | |
| 129 | |
| 130 // SSLTabHelper::SSLAddCertData ------------------------------------------------ | |
| 131 | |
| 132 class SSLTabHelper::SSLAddCertData : public infobars::InfoBarManager::Observer { | |
| 133 public: | |
| 134 explicit SSLAddCertData(InfoBarService* infobar_service); | |
| 135 virtual ~SSLAddCertData(); | |
| 136 | |
| 137 // Displays an infobar, replacing |infobar_| if it exists. | |
| 138 void ShowInfoBar(const base::string16& message, net::X509Certificate* cert); | |
| 139 | |
| 140 private: | |
| 141 // infobars::InfoBarManager::Observer: | |
| 142 virtual void OnInfoBarRemoved(infobars::InfoBar* infobar, | |
| 143 bool animate) OVERRIDE; | |
| 144 virtual void OnInfoBarReplaced(infobars::InfoBar* old_infobar, | |
| 145 infobars::InfoBar* new_infobar) OVERRIDE; | |
| 146 | |
| 147 InfoBarService* infobar_service_; | |
| 148 infobars::InfoBar* infobar_; | |
| 149 | |
| 150 DISALLOW_COPY_AND_ASSIGN(SSLAddCertData); | |
| 151 }; | |
| 152 | |
| 153 SSLTabHelper::SSLAddCertData::SSLAddCertData(InfoBarService* infobar_service) | |
| 154 : infobar_service_(infobar_service), | |
| 155 infobar_(NULL) { | |
| 156 infobar_service_->AddObserver(this); | |
| 157 } | |
| 158 | |
| 159 SSLTabHelper::SSLAddCertData::~SSLAddCertData() { | |
| 160 infobar_service_->RemoveObserver(this); | |
| 161 } | |
| 162 | |
| 163 void SSLTabHelper::SSLAddCertData::ShowInfoBar(const base::string16& message, | |
| 164 net::X509Certificate* cert) { | |
| 165 infobar_ = SSLCertResultInfoBarDelegate::Create(infobar_service_, infobar_, | |
| 166 message, cert); | |
| 167 } | |
| 168 | |
| 169 void SSLTabHelper::SSLAddCertData::OnInfoBarRemoved(infobars::InfoBar* infobar, | |
| 170 bool animate) { | |
| 171 if (infobar_ == infobar) | |
| 172 infobar_ = NULL; | |
| 173 } | |
| 174 | |
| 175 void SSLTabHelper::SSLAddCertData::OnInfoBarReplaced( | |
| 176 infobars::InfoBar* old_infobar, | |
| 177 infobars::InfoBar* new_infobar) { | |
| 178 if (infobar_ == old_infobar) | |
| 179 infobar_ = NULL; | |
| 180 } | |
| 181 | |
| 182 | |
| 183 // SSLTabHelper ---------------------------------------------------------------- | |
| 184 | 9 |
| 185 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SSLTabHelper); | 10 DEFINE_WEB_CONTENTS_USER_DATA_KEY(SSLTabHelper); |
| 186 | 11 |
| 187 SSLTabHelper::SSLTabHelper(content::WebContents* contents) | 12 SSLTabHelper::SSLTabHelper(content::WebContents* contents) |
| 188 : WebContentsObserver(contents), | 13 : WebContentsObserver(contents) { |
| 189 web_contents_(contents) { | |
| 190 } | 14 } |
| 191 | 15 |
| 192 SSLTabHelper::~SSLTabHelper() { | 16 SSLTabHelper::~SSLTabHelper() { |
| 193 } | 17 } |
| 194 | 18 |
| 195 void SSLTabHelper::DidChangeVisibleSSLState() { | 19 void SSLTabHelper::DidChangeVisibleSSLState() { |
| 196 #if !defined(OS_ANDROID) | 20 #if !defined(OS_ANDROID) |
| 197 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); | 21 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); |
| 198 if (browser) | 22 if (browser) |
| 199 browser->VisibleSSLStateChanged(web_contents_); | 23 browser->VisibleSSLStateChanged(web_contents()); |
|
davidben
2014/05/27 21:07:12
Given that SSLTabHelper's only purpose in life at
| |
| 200 #endif // !defined(OS_ANDROID) | 24 #endif // !defined(OS_ANDROID) |
| 201 } | 25 } |
| 202 | |
| 203 void SSLTabHelper::ShowClientCertificateRequestDialog( | |
| 204 const net::HttpNetworkSession* network_session, | |
| 205 net::SSLCertRequestInfo* cert_request_info, | |
| 206 const base::Callback<void(net::X509Certificate*)>& callback) { | |
| 207 chrome::ShowSSLClientCertificateSelector(web_contents_, network_session, | |
| 208 cert_request_info, callback); | |
| 209 } | |
| 210 | |
| 211 void SSLTabHelper::OnVerifyClientCertificateError( | |
| 212 scoped_refptr<SSLAddCertHandler> handler, int error_code) { | |
| 213 // Display an infobar with the error message. | |
| 214 // TODO(davidben): Display a more user-friendly error string. | |
| 215 GetAddCertData(handler.get())->ShowInfoBar( | |
| 216 l10n_util::GetStringFUTF16(IDS_ADD_CERT_ERR_INVALID_CERT, | |
| 217 base::IntToString16(-error_code), | |
| 218 base::ASCIIToUTF16( | |
| 219 net::ErrorToString(error_code))), | |
| 220 NULL); | |
| 221 } | |
| 222 | |
| 223 void SSLTabHelper::AskToAddClientCertificate( | |
| 224 scoped_refptr<SSLAddCertHandler> handler) { | |
| 225 NOTREACHED(); // Not implemented yet. | |
| 226 } | |
| 227 | |
| 228 void SSLTabHelper::OnAddClientCertificateSuccess( | |
| 229 scoped_refptr<SSLAddCertHandler> handler) { | |
| 230 net::X509Certificate* cert = handler->cert(); | |
| 231 // TODO(evanm): GetDisplayName should return UTF-16. | |
| 232 GetAddCertData(handler.get())->ShowInfoBar( | |
| 233 l10n_util::GetStringFUTF16(IDS_ADD_CERT_SUCCESS_INFOBAR_LABEL, | |
| 234 base::UTF8ToUTF16( | |
| 235 cert->issuer().GetDisplayName())), | |
| 236 cert); | |
| 237 } | |
| 238 | |
| 239 void SSLTabHelper::OnAddClientCertificateError( | |
| 240 scoped_refptr<SSLAddCertHandler> handler, | |
| 241 int error_code) { | |
| 242 // TODO(davidben): Display a more user-friendly error string. | |
| 243 GetAddCertData(handler.get())->ShowInfoBar( | |
| 244 l10n_util::GetStringFUTF16(IDS_ADD_CERT_ERR_FAILED, | |
| 245 base::IntToString16(-error_code), | |
| 246 base::ASCIIToUTF16( | |
| 247 net::ErrorToString(error_code))), | |
| 248 NULL); | |
| 249 } | |
| 250 | |
| 251 void SSLTabHelper::OnAddClientCertificateFinished( | |
| 252 scoped_refptr<SSLAddCertHandler> handler) { | |
| 253 // Clean up. | |
| 254 request_id_to_add_cert_data_.erase(handler->network_request_id()); | |
| 255 } | |
| 256 | |
| 257 SSLTabHelper::SSLAddCertData* | |
| 258 SSLTabHelper::GetAddCertData(SSLAddCertHandler* handler) { | |
| 259 // Find/create the slot. | |
| 260 linked_ptr<SSLAddCertData>& ptr_ref = | |
| 261 request_id_to_add_cert_data_[handler->network_request_id()]; | |
| 262 // Fill it if necessary. | |
| 263 if (!ptr_ref.get()) { | |
| 264 ptr_ref.reset( | |
| 265 new SSLAddCertData(InfoBarService::FromWebContents(web_contents_))); | |
| 266 } | |
| 267 return ptr_ref.get(); | |
| 268 } | |
| OLD | NEW |