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