| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/tab_contents/tab_contents_ssl_helper.h" | |
| 6 | |
| 7 #include <string> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/command_line.h" | |
| 12 #include "base/string_number_conversions.h" | |
| 13 #include "base/utf_string_conversions.h" | |
| 14 #include "base/values.h" | |
| 15 #include "chrome/browser/api/infobars/confirm_infobar_delegate.h" | |
| 16 #include "chrome/browser/api/infobars/simple_alert_infobar_delegate.h" | |
| 17 #include "chrome/browser/certificate_viewer.h" | |
| 18 #include "chrome/browser/content_settings/host_content_settings_map.h" | |
| 19 #include "chrome/browser/infobars/infobar.h" | |
| 20 #include "chrome/browser/infobars/infobar_tab_helper.h" | |
| 21 #include "chrome/browser/profiles/profile.h" | |
| 22 #include "chrome/browser/ssl/ssl_add_cert_handler.h" | |
| 23 #include "chrome/browser/ssl_client_certificate_selector.h" | |
| 24 #include "chrome/browser/ui/tab_contents/tab_contents.h" | |
| 25 #include "chrome/common/chrome_notification_types.h" | |
| 26 #include "chrome/common/chrome_switches.h" | |
| 27 #include "content/public/browser/notification_details.h" | |
| 28 #include "content/public/browser/notification_source.h" | |
| 29 #include "content/public/browser/web_contents.h" | |
| 30 #include "content/public/browser/web_contents_view.h" | |
| 31 #include "grit/generated_resources.h" | |
| 32 #include "grit/theme_resources.h" | |
| 33 #include "net/base/net_errors.h" | |
| 34 #include "net/base/x509_certificate.h" | |
| 35 #include "ui/base/l10n/l10n_util.h" | |
| 36 #include "ui/base/resource/resource_bundle.h" | |
| 37 | |
| 38 namespace { | |
| 39 | |
| 40 gfx::Image* GetCertIcon() { | |
| 41 // TODO(davidben): use a more appropriate icon. | |
| 42 return &ResourceBundle::GetSharedInstance().GetNativeImageNamed( | |
| 43 IDR_INFOBAR_SAVE_PASSWORD); | |
| 44 } | |
| 45 | |
| 46 // SSLCertAddedInfoBarDelegate ------------------------------------------------ | |
| 47 | |
| 48 class SSLCertAddedInfoBarDelegate : public ConfirmInfoBarDelegate { | |
| 49 public: | |
| 50 SSLCertAddedInfoBarDelegate(InfoBarTabHelper* infobar_helper, | |
| 51 net::X509Certificate* cert); | |
| 52 | |
| 53 private: | |
| 54 virtual ~SSLCertAddedInfoBarDelegate(); | |
| 55 | |
| 56 // ConfirmInfoBarDelegate: | |
| 57 virtual gfx::Image* GetIcon() const OVERRIDE; | |
| 58 virtual Type GetInfoBarType() const OVERRIDE; | |
| 59 virtual string16 GetMessageText() const OVERRIDE; | |
| 60 virtual int GetButtons() const OVERRIDE; | |
| 61 virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; | |
| 62 virtual bool Accept() OVERRIDE; | |
| 63 | |
| 64 scoped_refptr<net::X509Certificate> cert_; // The cert we added. | |
| 65 }; | |
| 66 | |
| 67 SSLCertAddedInfoBarDelegate::SSLCertAddedInfoBarDelegate( | |
| 68 InfoBarTabHelper* infobar_helper, | |
| 69 net::X509Certificate* cert) | |
| 70 : ConfirmInfoBarDelegate(infobar_helper), | |
| 71 cert_(cert) { | |
| 72 } | |
| 73 | |
| 74 SSLCertAddedInfoBarDelegate::~SSLCertAddedInfoBarDelegate() { | |
| 75 } | |
| 76 | |
| 77 gfx::Image* SSLCertAddedInfoBarDelegate::GetIcon() const { | |
| 78 return GetCertIcon(); | |
| 79 } | |
| 80 | |
| 81 InfoBarDelegate::Type SSLCertAddedInfoBarDelegate::GetInfoBarType() const { | |
| 82 return PAGE_ACTION_TYPE; | |
| 83 } | |
| 84 | |
| 85 string16 SSLCertAddedInfoBarDelegate::GetMessageText() const { | |
| 86 // TODO(evanm): GetDisplayName should return UTF-16. | |
| 87 return l10n_util::GetStringFUTF16(IDS_ADD_CERT_SUCCESS_INFOBAR_LABEL, | |
| 88 UTF8ToUTF16(cert_->issuer().GetDisplayName())); | |
| 89 } | |
| 90 | |
| 91 int SSLCertAddedInfoBarDelegate::GetButtons() const { | |
| 92 return BUTTON_OK; | |
| 93 } | |
| 94 | |
| 95 string16 SSLCertAddedInfoBarDelegate::GetButtonLabel( | |
| 96 InfoBarButton button) const { | |
| 97 DCHECK_EQ(BUTTON_OK, button); | |
| 98 return l10n_util::GetStringUTF16(IDS_ADD_CERT_SUCCESS_INFOBAR_BUTTON); | |
| 99 } | |
| 100 | |
| 101 bool SSLCertAddedInfoBarDelegate::Accept() { | |
| 102 ShowCertificateViewer( | |
| 103 owner()->GetWebContents(), | |
| 104 owner()->GetWebContents()->GetView()->GetTopLevelNativeWindow(), | |
| 105 cert_); | |
| 106 return false; // Hiding the infobar just as the dialog opens looks weird. | |
| 107 } | |
| 108 | |
| 109 } // namespace | |
| 110 | |
| 111 | |
| 112 // TabContentsSSLHelper::SSLAddCertData --------------------------------------- | |
| 113 | |
| 114 class TabContentsSSLHelper::SSLAddCertData | |
| 115 : public content::NotificationObserver { | |
| 116 public: | |
| 117 explicit SSLAddCertData(TabContents* tab_contents); | |
| 118 virtual ~SSLAddCertData(); | |
| 119 | |
| 120 // Displays |delegate| as an infobar in |tab_|, replacing our current one if | |
| 121 // still active. | |
| 122 void ShowInfoBar(InfoBarDelegate* delegate); | |
| 123 | |
| 124 // Same as above, for the common case of wanting to show a simple alert | |
| 125 // message. | |
| 126 void ShowErrorInfoBar(const string16& message); | |
| 127 | |
| 128 private: | |
| 129 // content::NotificationObserver: | |
| 130 virtual void Observe(int type, | |
| 131 const content::NotificationSource& source, | |
| 132 const content::NotificationDetails& details); | |
| 133 | |
| 134 TabContents* tab_contents_; | |
| 135 InfoBarDelegate* infobar_delegate_; | |
| 136 content::NotificationRegistrar registrar_; | |
| 137 | |
| 138 DISALLOW_COPY_AND_ASSIGN(SSLAddCertData); | |
| 139 }; | |
| 140 | |
| 141 TabContentsSSLHelper::SSLAddCertData::SSLAddCertData( | |
| 142 TabContents* tab_contents) | |
| 143 : tab_contents_(tab_contents), | |
| 144 infobar_delegate_(NULL) { | |
| 145 content::Source<InfoBarTabHelper> source(tab_contents_->infobar_tab_helper()); | |
| 146 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | |
| 147 source); | |
| 148 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED, | |
| 149 source); | |
| 150 } | |
| 151 | |
| 152 TabContentsSSLHelper::SSLAddCertData::~SSLAddCertData() { | |
| 153 } | |
| 154 | |
| 155 void TabContentsSSLHelper::SSLAddCertData::ShowInfoBar( | |
| 156 InfoBarDelegate* delegate) { | |
| 157 if (infobar_delegate_) | |
| 158 tab_contents_->infobar_tab_helper()->ReplaceInfoBar(infobar_delegate_, | |
| 159 delegate); | |
| 160 else | |
| 161 tab_contents_->infobar_tab_helper()->AddInfoBar(delegate); | |
| 162 infobar_delegate_ = delegate; | |
| 163 } | |
| 164 | |
| 165 void TabContentsSSLHelper::SSLAddCertData::ShowErrorInfoBar( | |
| 166 const string16& message) { | |
| 167 ShowInfoBar(new SimpleAlertInfoBarDelegate( | |
| 168 tab_contents_->infobar_tab_helper(), GetCertIcon(), message, true)); | |
| 169 } | |
| 170 | |
| 171 void TabContentsSSLHelper::SSLAddCertData::Observe( | |
| 172 int type, | |
| 173 const content::NotificationSource& source, | |
| 174 const content::NotificationDetails& details) { | |
| 175 DCHECK(type == chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED || | |
| 176 type == chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED); | |
| 177 if (infobar_delegate_ == | |
| 178 ((type == chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED) ? | |
| 179 content::Details<InfoBarRemovedDetails>(details)->first : | |
| 180 content::Details<InfoBarReplacedDetails>(details)->first)) | |
| 181 infobar_delegate_ = NULL; | |
| 182 } | |
| 183 | |
| 184 | |
| 185 // TabContentsSSLHelper ------------------------------------------------------- | |
| 186 | |
| 187 TabContentsSSLHelper::TabContentsSSLHelper(TabContents* tab_contents) | |
| 188 : tab_contents_(tab_contents) { | |
| 189 } | |
| 190 | |
| 191 TabContentsSSLHelper::~TabContentsSSLHelper() { | |
| 192 } | |
| 193 | |
| 194 void TabContentsSSLHelper::ShowClientCertificateRequestDialog( | |
| 195 const net::HttpNetworkSession* network_session, | |
| 196 net::SSLCertRequestInfo* cert_request_info, | |
| 197 const base::Callback<void(net::X509Certificate*)>& callback) { | |
| 198 chrome::ShowSSLClientCertificateSelector( | |
| 199 tab_contents_, network_session, cert_request_info, callback); | |
| 200 } | |
| 201 | |
| 202 void TabContentsSSLHelper::OnVerifyClientCertificateError( | |
| 203 scoped_refptr<SSLAddCertHandler> handler, int error_code) { | |
| 204 SSLAddCertData* add_cert_data = GetAddCertData(handler); | |
| 205 // Display an infobar with the error message. | |
| 206 // TODO(davidben): Display a more user-friendly error string. | |
| 207 add_cert_data->ShowErrorInfoBar( | |
| 208 l10n_util::GetStringFUTF16(IDS_ADD_CERT_ERR_INVALID_CERT, | |
| 209 base::IntToString16(-error_code), | |
| 210 ASCIIToUTF16(net::ErrorToString(error_code)))); | |
| 211 } | |
| 212 | |
| 213 void TabContentsSSLHelper::AskToAddClientCertificate( | |
| 214 scoped_refptr<SSLAddCertHandler> handler) { | |
| 215 NOTREACHED(); // Not implemented yet. | |
| 216 } | |
| 217 | |
| 218 void TabContentsSSLHelper::OnAddClientCertificateSuccess( | |
| 219 scoped_refptr<SSLAddCertHandler> handler) { | |
| 220 SSLAddCertData* add_cert_data = GetAddCertData(handler); | |
| 221 // Display an infobar to inform the user. | |
| 222 add_cert_data->ShowInfoBar(new SSLCertAddedInfoBarDelegate( | |
| 223 tab_contents_->infobar_tab_helper(), handler->cert())); | |
| 224 } | |
| 225 | |
| 226 void TabContentsSSLHelper::OnAddClientCertificateError( | |
| 227 scoped_refptr<SSLAddCertHandler> handler, int error_code) { | |
| 228 SSLAddCertData* add_cert_data = GetAddCertData(handler); | |
| 229 // Display an infobar with the error message. | |
| 230 // TODO(davidben): Display a more user-friendly error string. | |
| 231 add_cert_data->ShowErrorInfoBar( | |
| 232 l10n_util::GetStringFUTF16(IDS_ADD_CERT_ERR_FAILED, | |
| 233 base::IntToString16(-error_code), | |
| 234 ASCIIToUTF16(net::ErrorToString(error_code)))); | |
| 235 } | |
| 236 | |
| 237 void TabContentsSSLHelper::OnAddClientCertificateFinished( | |
| 238 scoped_refptr<SSLAddCertHandler> handler) { | |
| 239 // Clean up. | |
| 240 request_id_to_add_cert_data_.erase(handler->network_request_id()); | |
| 241 } | |
| 242 | |
| 243 TabContentsSSLHelper::SSLAddCertData* TabContentsSSLHelper::GetAddCertData( | |
| 244 SSLAddCertHandler* handler) { | |
| 245 // Find/create the slot. | |
| 246 linked_ptr<SSLAddCertData>& ptr_ref = | |
| 247 request_id_to_add_cert_data_[handler->network_request_id()]; | |
| 248 // Fill it if necessary. | |
| 249 if (!ptr_ref.get()) | |
| 250 ptr_ref.reset(new SSLAddCertData(tab_contents_)); | |
| 251 return ptr_ref.get(); | |
| 252 } | |
| OLD | NEW |