OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/tab_contents/tab_contents_ssl_helper.h" | 5 #include "chrome/browser/tab_contents/tab_contents_ssl_helper.h" |
6 | 6 |
7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 #include "chrome/browser/certificate_viewer.h" | 11 #include "chrome/browser/certificate_viewer.h" |
(...skipping 10 matching lines...) Expand all Loading... |
22 #include "ui/base/resource/resource_bundle.h" | 22 #include "ui/base/resource/resource_bundle.h" |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 SkBitmap* GetCertIcon() { | 26 SkBitmap* GetCertIcon() { |
27 // TODO(davidben): use a more appropriate icon. | 27 // TODO(davidben): use a more appropriate icon. |
28 return ResourceBundle::GetSharedInstance().GetBitmapNamed( | 28 return ResourceBundle::GetSharedInstance().GetBitmapNamed( |
29 IDR_INFOBAR_SAVE_PASSWORD); | 29 IDR_INFOBAR_SAVE_PASSWORD); |
30 } | 30 } |
31 | 31 |
| 32 |
| 33 // SSLCertAddedInfoBarDelegate ------------------------------------------------ |
| 34 |
32 class SSLCertAddedInfoBarDelegate : public ConfirmInfoBarDelegate { | 35 class SSLCertAddedInfoBarDelegate : public ConfirmInfoBarDelegate { |
33 public: | 36 public: |
34 SSLCertAddedInfoBarDelegate(TabContents* tab_contents, | 37 SSLCertAddedInfoBarDelegate(TabContents* tab_contents, |
35 net::X509Certificate* cert) | 38 net::X509Certificate* cert); |
36 : ConfirmInfoBarDelegate(tab_contents), | |
37 tab_contents_(tab_contents), | |
38 cert_(cert) { | |
39 } | |
40 | |
41 virtual ~SSLCertAddedInfoBarDelegate() { | |
42 } | |
43 | |
44 // Overridden from ConfirmInfoBarDelegate: | |
45 virtual string16 GetMessageText() const { | |
46 // TODO(evanm): GetDisplayName should return UTF-16. | |
47 return l10n_util::GetStringFUTF16( | |
48 IDS_ADD_CERT_SUCCESS_INFOBAR_LABEL, | |
49 UTF8ToUTF16(cert_->issuer().GetDisplayName())); | |
50 } | |
51 | |
52 virtual SkBitmap* GetIcon() const { | |
53 return GetCertIcon(); | |
54 } | |
55 | |
56 virtual int GetButtons() const { | |
57 return BUTTON_OK; | |
58 } | |
59 | |
60 virtual string16 GetButtonLabel(InfoBarButton button) const { | |
61 switch (button) { | |
62 case BUTTON_OK: | |
63 return l10n_util::GetStringUTF16(IDS_ADD_CERT_SUCCESS_INFOBAR_BUTTON); | |
64 default: | |
65 return string16(); | |
66 } | |
67 } | |
68 | |
69 virtual Type GetInfoBarType() { | |
70 return PAGE_ACTION_TYPE; | |
71 } | |
72 | |
73 virtual bool Accept() { | |
74 ShowCertificateViewer(tab_contents_->GetMessageBoxRootWindow(), cert_); | |
75 return false; // Hiding the infobar just as the dialog opens looks weird. | |
76 } | |
77 | |
78 virtual void InfoBarClosed() { | |
79 // ConfirmInfoBarDelegate doesn't delete itself. | |
80 delete this; | |
81 } | |
82 | 39 |
83 private: | 40 private: |
| 41 virtual ~SSLCertAddedInfoBarDelegate(); |
| 42 |
| 43 // ConfirmInfoBarDelegate: |
| 44 virtual void InfoBarClosed(); |
| 45 virtual SkBitmap* GetIcon() const; |
| 46 virtual Type GetInfoBarType() const; |
| 47 virtual string16 GetMessageText() const; |
| 48 virtual int GetButtons() const; |
| 49 virtual string16 GetButtonLabel(InfoBarButton button) const; |
| 50 virtual bool Accept(); |
| 51 |
84 // The TabContents we are attached to | 52 // The TabContents we are attached to |
85 TabContents* tab_contents_; | 53 TabContents* tab_contents_; |
86 // The cert we added. | 54 // The cert we added. |
87 scoped_refptr<net::X509Certificate> cert_; | 55 scoped_refptr<net::X509Certificate> cert_; |
88 }; | 56 }; |
89 | 57 |
| 58 SSLCertAddedInfoBarDelegate::SSLCertAddedInfoBarDelegate( |
| 59 TabContents* tab_contents, |
| 60 net::X509Certificate* cert) |
| 61 : ConfirmInfoBarDelegate(tab_contents), |
| 62 tab_contents_(tab_contents), |
| 63 cert_(cert) { |
| 64 } |
| 65 |
| 66 SSLCertAddedInfoBarDelegate::~SSLCertAddedInfoBarDelegate() { |
| 67 } |
| 68 |
| 69 void SSLCertAddedInfoBarDelegate::InfoBarClosed() { |
| 70 // ConfirmInfoBarDelegate doesn't delete itself. |
| 71 delete this; |
| 72 } |
| 73 |
| 74 SkBitmap* SSLCertAddedInfoBarDelegate::GetIcon() const { |
| 75 return GetCertIcon(); |
| 76 } |
| 77 |
| 78 InfoBarDelegate::Type SSLCertAddedInfoBarDelegate::GetInfoBarType() const { |
| 79 return PAGE_ACTION_TYPE; |
| 80 } |
| 81 |
| 82 string16 SSLCertAddedInfoBarDelegate::GetMessageText() const { |
| 83 // TODO(evanm): GetDisplayName should return UTF-16. |
| 84 return l10n_util::GetStringFUTF16(IDS_ADD_CERT_SUCCESS_INFOBAR_LABEL, |
| 85 UTF8ToUTF16(cert_->issuer().GetDisplayName())); |
| 86 } |
| 87 |
| 88 int SSLCertAddedInfoBarDelegate::GetButtons() const { |
| 89 return BUTTON_OK; |
| 90 } |
| 91 |
| 92 string16 SSLCertAddedInfoBarDelegate::GetButtonLabel( |
| 93 InfoBarButton button) const { |
| 94 DCHECK_EQ(BUTTON_OK, button); |
| 95 return l10n_util::GetStringUTF16(IDS_ADD_CERT_SUCCESS_INFOBAR_BUTTON); |
| 96 } |
| 97 |
| 98 bool SSLCertAddedInfoBarDelegate::Accept() { |
| 99 ShowCertificateViewer(tab_contents_->GetMessageBoxRootWindow(), cert_); |
| 100 return false; // Hiding the infobar just as the dialog opens looks weird. |
| 101 } |
| 102 |
90 } // namespace | 103 } // namespace |
91 | 104 |
| 105 |
| 106 // TabContentsSSLHelper::SSLAddCertData --------------------------------------- |
| 107 |
92 class TabContentsSSLHelper::SSLAddCertData : public NotificationObserver { | 108 class TabContentsSSLHelper::SSLAddCertData : public NotificationObserver { |
93 public: | 109 public: |
94 SSLAddCertData(TabContents* tab, SSLAddCertHandler* handler) | 110 SSLAddCertData(TabContents* tab, SSLAddCertHandler* handler); |
95 : tab_(tab), | 111 virtual ~SSLAddCertData(); |
96 handler_(handler), | |
97 infobar_delegate_(NULL) { | |
98 // Listen for disappearing InfoBars. | |
99 Source<TabContents> tc_source(tab_); | |
100 registrar_.Add(this, NotificationType::TAB_CONTENTS_INFOBAR_REMOVED, | |
101 tc_source); | |
102 registrar_.Add(this, NotificationType::TAB_CONTENTS_INFOBAR_REPLACED, | |
103 tc_source); | |
104 } | |
105 ~SSLAddCertData() {} | |
106 | 112 |
107 // Displays |delegate| as an infobar in |tab_|, replacing our current one if | 113 // Displays |delegate| as an infobar in |tab_|, replacing our current one if |
108 // still active. | 114 // still active. |
109 void ShowInfoBar(InfoBarDelegate* delegate) { | 115 void ShowInfoBar(InfoBarDelegate* delegate); |
110 if (infobar_delegate_) { | |
111 tab_->ReplaceInfoBar(infobar_delegate_, delegate); | |
112 } else { | |
113 tab_->AddInfoBar(delegate); | |
114 } | |
115 infobar_delegate_ = delegate; | |
116 } | |
117 | 116 |
118 void ShowErrorInfoBar(const string16& message) { | 117 // Same as above, for the common case of wanting to show a simple alert |
119 ShowInfoBar( | 118 // message. |
120 new SimpleAlertInfoBarDelegate(tab_, message, GetCertIcon(), true)); | 119 void ShowErrorInfoBar(const string16& message); |
121 } | |
122 | 120 |
123 // NotificationObserver implementation. | 121 private: |
| 122 // NotificationObserver: |
124 virtual void Observe(NotificationType type, | 123 virtual void Observe(NotificationType type, |
125 const NotificationSource& source, | 124 const NotificationSource& source, |
126 const NotificationDetails& details) { | 125 const NotificationDetails& details); |
127 switch (type.value) { | |
128 case NotificationType::TAB_CONTENTS_INFOBAR_REMOVED: | |
129 InfoBarClosed(Details<InfoBarDelegate>(details).ptr()); | |
130 break; | |
131 case NotificationType::TAB_CONTENTS_INFOBAR_REPLACED: | |
132 typedef std::pair<InfoBarDelegate*, InfoBarDelegate*> | |
133 InfoBarDelegatePair; | |
134 InfoBarClosed(Details<InfoBarDelegatePair>(details).ptr()->first); | |
135 break; | |
136 default: | |
137 NOTREACHED(); | |
138 break; | |
139 } | |
140 } | |
141 | 126 |
142 private: | 127 TabContents* tab_contents_; |
143 void InfoBarClosed(InfoBarDelegate* delegate) { | 128 scoped_refptr<SSLAddCertHandler> handler_; // The handler we call back to. |
144 if (infobar_delegate_ == delegate) | |
145 infobar_delegate_ = NULL; | |
146 } | |
147 | |
148 // The TabContents we are attached to. | |
149 TabContents* tab_; | |
150 // The handler we call back to. | |
151 scoped_refptr<SSLAddCertHandler> handler_; | |
152 // The current InfoBarDelegate we're displaying. | |
153 InfoBarDelegate* infobar_delegate_; | 129 InfoBarDelegate* infobar_delegate_; |
154 | |
155 NotificationRegistrar registrar_; | 130 NotificationRegistrar registrar_; |
156 | 131 |
157 DISALLOW_COPY_AND_ASSIGN(SSLAddCertData); | 132 DISALLOW_COPY_AND_ASSIGN(SSLAddCertData); |
158 }; | 133 }; |
159 | 134 |
| 135 TabContentsSSLHelper::SSLAddCertData::SSLAddCertData(TabContents* tab_contents, |
| 136 SSLAddCertHandler* handler) |
| 137 : tab_contents_(tab_contents), |
| 138 handler_(handler), |
| 139 infobar_delegate_(NULL) { |
| 140 Source<TabContents> source(tab_contents_); |
| 141 registrar_.Add(this, NotificationType::TAB_CONTENTS_INFOBAR_REMOVED, source); |
| 142 registrar_.Add(this, NotificationType::TAB_CONTENTS_INFOBAR_REPLACED, source); |
| 143 } |
| 144 |
| 145 TabContentsSSLHelper::SSLAddCertData::~SSLAddCertData() { |
| 146 } |
| 147 |
| 148 void TabContentsSSLHelper::SSLAddCertData::ShowInfoBar( |
| 149 InfoBarDelegate* delegate) { |
| 150 if (infobar_delegate_) |
| 151 tab_contents_->ReplaceInfoBar(infobar_delegate_, delegate); |
| 152 else |
| 153 tab_contents_->AddInfoBar(delegate); |
| 154 infobar_delegate_ = delegate; |
| 155 } |
| 156 |
| 157 void TabContentsSSLHelper::SSLAddCertData::ShowErrorInfoBar( |
| 158 const string16& message) { |
| 159 ShowInfoBar(new SimpleAlertInfoBarDelegate(tab_contents_, GetCertIcon(), |
| 160 message, true)); |
| 161 } |
| 162 |
| 163 void TabContentsSSLHelper::SSLAddCertData::Observe( |
| 164 NotificationType type, |
| 165 const NotificationSource& source, |
| 166 const NotificationDetails& details) { |
| 167 typedef std::pair<InfoBarDelegate*, InfoBarDelegate*> InfoBarDelegatePair; |
| 168 if (infobar_delegate_ == |
| 169 ((type.value == NotificationType::TAB_CONTENTS_INFOBAR_REMOVED) ? |
| 170 Details<InfoBarDelegate>(details).ptr() : |
| 171 Details<InfoBarDelegatePair>(details).ptr()->first)) |
| 172 infobar_delegate_ = NULL; |
| 173 } |
| 174 |
| 175 |
| 176 // TabContentsSSLHelper ------------------------------------------------------- |
| 177 |
160 TabContentsSSLHelper::TabContentsSSLHelper(TabContents* tab_contents) | 178 TabContentsSSLHelper::TabContentsSSLHelper(TabContents* tab_contents) |
161 : tab_contents_(tab_contents) { | 179 : tab_contents_(tab_contents) { |
162 } | 180 } |
163 | 181 |
164 TabContentsSSLHelper::~TabContentsSSLHelper() { | 182 TabContentsSSLHelper::~TabContentsSSLHelper() { |
165 } | 183 } |
166 | 184 |
167 void TabContentsSSLHelper::ShowClientCertificateRequestDialog( | 185 void TabContentsSSLHelper::ShowClientCertificateRequestDialog( |
168 scoped_refptr<SSLClientAuthHandler> handler) { | 186 scoped_refptr<SSLClientAuthHandler> handler) { |
169 browser::ShowSSLClientCertificateSelector( | 187 browser::ShowSSLClientCertificateSelector( |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 TabContentsSSLHelper::SSLAddCertData* TabContentsSSLHelper::GetAddCertData( | 232 TabContentsSSLHelper::SSLAddCertData* TabContentsSSLHelper::GetAddCertData( |
215 SSLAddCertHandler* handler) { | 233 SSLAddCertHandler* handler) { |
216 // Find/create the slot. | 234 // Find/create the slot. |
217 linked_ptr<SSLAddCertData>& ptr_ref = | 235 linked_ptr<SSLAddCertData>& ptr_ref = |
218 request_id_to_add_cert_data_[handler->network_request_id()]; | 236 request_id_to_add_cert_data_[handler->network_request_id()]; |
219 // Fill it if necessary. | 237 // Fill it if necessary. |
220 if (!ptr_ref.get()) | 238 if (!ptr_ref.get()) |
221 ptr_ref.reset(new SSLAddCertData(tab_contents_, handler)); | 239 ptr_ref.reset(new SSLAddCertData(tab_contents_, handler)); |
222 return ptr_ref.get(); | 240 return ptr_ref.get(); |
223 } | 241 } |
OLD | NEW |