OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/views/page_info_bubble_view.h" | 5 #include "chrome/browser/views/page_info_bubble_view.h" |
6 | 6 |
7 #include "app/l10n_util.h" | |
8 #include "app/resource_bundle.h" | 7 #include "app/resource_bundle.h" |
9 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/certificate_viewer.h" | |
11 #include "chrome/browser/views/frame/browser_view.h" | 9 #include "chrome/browser/views/frame/browser_view.h" |
12 #include "chrome/browser/views/info_bubble.h" | 10 #include "chrome/browser/views/info_bubble.h" |
13 #include "chrome/browser/views/toolbar_view.h" | 11 #include "chrome/browser/views/toolbar_view.h" |
14 #include "grit/generated_resources.h" | |
15 #include "grit/locale_settings.h" | 12 #include "grit/locale_settings.h" |
16 #include "grit/theme_resources.h" | 13 #include "grit/theme_resources.h" |
17 #include "views/controls/image_view.h" | 14 #include "views/controls/image_view.h" |
18 #include "views/controls/label.h" | 15 #include "views/controls/label.h" |
19 #include "views/controls/link.h" | |
20 #include "views/controls/separator.h" | 16 #include "views/controls/separator.h" |
21 #include "views/grid_layout.h" | 17 #include "views/grid_layout.h" |
22 #include "views/widget/widget_win.h" | 18 #include "views/widget/widget_win.h" |
23 #include "views/window/window.h" | 19 #include "views/window/window.h" |
24 | 20 |
25 namespace { | |
26 | |
27 // Layout constants. | 21 // Layout constants. |
28 const int kHGapToBorder = 11; | 22 const int kHGapToBorder = 11; |
29 const int kVGapToImage = 10; | 23 const int kVGapToImage = 10; |
30 const int kVGapToHeadline = 7; | 24 const int kVGapToHeadline = 7; |
31 const int kHGapImageToDescription = 6; | 25 const int kHGapImageToDescription = 6; |
32 const int kTextPaddingRight = 10; | 26 const int kTextPaddingRight = 10; |
33 const int kPaddingBelowSeparator = 4; | 27 const int kPaddingBelowSeparator = 4; |
34 const int kPaddingAboveSeparator = 13; | 28 const int kPaddingAboveSeparator = 13; |
35 const int kIconOffset = 28; | 29 const int kIconOffset = 28; |
36 | 30 |
37 // A section contains an image that shows a status (good or bad), a title, an | 31 // A section contains an image that shows a status (good or bad), a title, an |
38 // optional head-line (in bold) and a description. | 32 // optional head-line (in bold) and a description. |
39 class Section : public views::View, | 33 class Section : public views::View { |
40 public views::LinkController { | |
41 public: | 34 public: |
42 Section(PageInfoBubbleView* owner, | 35 Section(bool state, |
43 const PageInfoModel::SectionInfo& section_info); | 36 const string16& headline, |
| 37 const string16& description); |
44 virtual ~Section(); | 38 virtual ~Section(); |
45 | 39 |
46 // views::View methods: | |
47 virtual int GetHeightForWidth(int w); | 40 virtual int GetHeightForWidth(int w); |
48 virtual void Layout(); | 41 virtual void Layout(); |
49 | 42 |
50 // views::LinkController methods: | |
51 virtual void LinkActivated(views::Link* source, int event_flags); | |
52 | |
53 private: | 43 private: |
54 // Calculate the layout if |compute_bounds_only|, otherwise does Layout also. | 44 // Calculate the layout if |compute_bounds_only|, otherwise does Layout also. |
55 gfx::Size LayoutItems(bool compute_bounds_only, int width); | 45 gfx::Size LayoutItems(bool compute_bounds_only, int width); |
56 | 46 |
57 // The view that owns this Section object. | 47 // Whether to show the good/bad icon. |
58 PageInfoBubbleView* owner_; | 48 bool state_; |
59 | 49 |
60 // The information this view represents. | 50 // The first line of the description, show in bold. |
61 PageInfoModel::SectionInfo info_; | 51 string16 headline_; |
| 52 |
| 53 // The description, displayed below the head line. |
| 54 string16 description_; |
62 | 55 |
63 static SkBitmap* good_state_icon_; | 56 static SkBitmap* good_state_icon_; |
64 static SkBitmap* bad_state_icon_; | 57 static SkBitmap* bad_state_icon_; |
65 | 58 |
66 views::ImageView* status_image_; | 59 views::ImageView* status_image_; |
67 views::Label* headline_label_; | 60 views::Label* headline_label_; |
68 views::Label* description_label_; | 61 views::Label* description_label_; |
69 views::Link* link_; | |
70 | 62 |
71 DISALLOW_COPY_AND_ASSIGN(Section); | 63 DISALLOW_COPY_AND_ASSIGN(Section); |
72 }; | 64 }; |
73 | 65 |
74 // static | 66 // static |
75 SkBitmap* Section::good_state_icon_ = NULL; | 67 SkBitmap* Section::good_state_icon_ = NULL; |
76 SkBitmap* Section::bad_state_icon_ = NULL; | 68 SkBitmap* Section::bad_state_icon_ = NULL; |
77 | 69 |
78 } // namespace | |
79 | |
80 //////////////////////////////////////////////////////////////////////////////// | 70 //////////////////////////////////////////////////////////////////////////////// |
81 // PageInfoBubbleView | 71 // PageInfoBubbleView |
82 | 72 |
83 PageInfoBubbleView::PageInfoBubbleView(gfx::NativeWindow parent_window, | 73 PageInfoBubbleView::PageInfoBubbleView(Profile* profile, |
84 Profile* profile, | |
85 const GURL& url, | 74 const GURL& url, |
86 const NavigationEntry::SSLStatus& ssl, | 75 const NavigationEntry::SSLStatus& ssl, |
87 bool show_history) | 76 bool show_history) |
88 : ALLOW_THIS_IN_INITIALIZER_LIST(model_(profile, url, ssl, | 77 : ALLOW_THIS_IN_INITIALIZER_LIST(model_(profile, url, ssl, |
89 show_history, this)), | 78 show_history, this)), |
90 parent_window_(parent_window), | |
91 cert_id_(ssl.cert_id()), | 79 cert_id_(ssl.cert_id()), |
92 info_bubble_(NULL) { | 80 info_bubble_(NULL) { |
93 LayoutSections(); | 81 LayoutSections(); |
94 } | 82 } |
95 | 83 |
96 PageInfoBubbleView::~PageInfoBubbleView() { | 84 PageInfoBubbleView::~PageInfoBubbleView() { |
97 } | 85 } |
98 | 86 |
99 void PageInfoBubbleView::ShowCertDialog() { | |
100 ShowCertificateViewerByID(parent_window_, cert_id_); | |
101 } | |
102 | |
103 void PageInfoBubbleView::LayoutSections() { | 87 void PageInfoBubbleView::LayoutSections() { |
104 // Remove all the existing sections. | 88 // Remove all the existing sections. |
105 RemoveAllChildViews(true); | 89 RemoveAllChildViews(true); |
106 | 90 |
107 views::GridLayout* layout = new views::GridLayout(this); | 91 views::GridLayout* layout = new views::GridLayout(this); |
108 SetLayoutManager(layout); | 92 SetLayoutManager(layout); |
109 views::ColumnSet* columns = layout->AddColumnSet(0); | 93 views::ColumnSet* columns = layout->AddColumnSet(0); |
110 columns->AddColumn(views::GridLayout::FILL, // Horizontal resize. | 94 columns->AddColumn(views::GridLayout::FILL, // Horizontal resize. |
111 views::GridLayout::FILL, // Vertical resize. | 95 views::GridLayout::FILL, // Vertical resize. |
112 1, // Resize weight. | 96 1, // Resize weight. |
113 views::GridLayout::USE_PREF, // Size type. | 97 views::GridLayout::USE_PREF, // Size type. |
114 0, // Ignored for USE_PREF. | 98 0, // Ignored for USE_PREF. |
115 0); // Minimum size. | 99 0); // Minimum size. |
116 | 100 |
117 int count = model_.GetSectionCount(); | 101 int count = model_.GetSectionCount(); |
118 for (int i = 0; i < count; ++i) { | 102 for (int i = 0; i < count; ++i) { |
119 PageInfoModel::SectionInfo info = model_.GetSectionInfo(i); | 103 PageInfoModel::SectionInfo info = model_.GetSectionInfo(i); |
120 layout->StartRow(0, 0); | 104 layout->StartRow(0, 0); |
121 // TODO(finnur): Remove title from the info struct, since it is | 105 // TODO(finnur): Remove title from the info struct, since it is |
122 // not used anymore. | 106 // not used anymore. |
123 layout->AddView(new Section(this, info)); | 107 layout->AddView(new Section(info.state, info.head_line, info.description)); |
124 | 108 |
125 // Add separator after all sections except the last. | 109 // Add separator after all sections except the last. |
126 if (i < count - 1) { | 110 if (i < count - 1) { |
127 layout->AddPaddingRow(0, kPaddingAboveSeparator); | 111 layout->AddPaddingRow(0, kPaddingAboveSeparator); |
128 layout->StartRow(0, 0); | 112 layout->StartRow(0, 0); |
129 layout->AddView(new views::Separator()); | 113 layout->AddView(new views::Separator()); |
130 layout->AddPaddingRow(0, kPaddingBelowSeparator); | 114 layout->AddPaddingRow(0, kPaddingBelowSeparator); |
131 } | 115 } |
132 } | 116 } |
133 } | 117 } |
134 | 118 |
135 gfx::Size PageInfoBubbleView::GetPreferredSize() { | 119 gfx::Size PageInfoBubbleView::GetPreferredSize() { |
136 gfx::Size size(views::Window::GetLocalizedContentsSize( | 120 gfx::Size size(views::Window::GetLocalizedContentsSize( |
137 IDS_PAGEINFOBUBBLE_WIDTH_CHARS, IDS_PAGEINFOBUBBLE_HEIGHT_LINES)); | 121 IDS_PAGEINFOBUBBLE_WIDTH_CHARS, IDS_PAGEINFOBUBBLE_HEIGHT_LINES)); |
138 size.set_height(0); | 122 size.set_height(0); |
139 | 123 |
140 int count = model_.GetSectionCount(); | 124 int count = model_.GetSectionCount(); |
141 for (int i = 0; i < count; ++i) { | 125 for (int i = 0; i < count; ++i) { |
142 PageInfoModel::SectionInfo info = model_.GetSectionInfo(i); | 126 PageInfoModel::SectionInfo info = model_.GetSectionInfo(i); |
143 Section section(this, info); | 127 Section section(info.state, info.head_line, info.description); |
144 size.Enlarge(0, section.GetHeightForWidth(size.width())); | 128 size.Enlarge(0, section.GetHeightForWidth(size.width())); |
145 } | 129 } |
146 | 130 |
147 // Account for the separators and padding. | 131 // Account for the separators and padding. |
148 views::Separator separator; | 132 views::Separator separator; |
149 gfx::Size separator_size = separator.GetPreferredSize(); | 133 gfx::Size separator_size = separator.GetPreferredSize(); |
150 size.Enlarge(0, (count - 1) * (separator_size.height() + | 134 size.Enlarge(0, (count - 1) * (separator_size.height() + |
151 kPaddingAboveSeparator + | 135 kPaddingAboveSeparator + |
152 kPaddingBelowSeparator)); | 136 kPaddingBelowSeparator)); |
153 return size; | 137 return size; |
154 } | 138 } |
155 | 139 |
156 void PageInfoBubbleView::ModelChanged() { | 140 void PageInfoBubbleView::ModelChanged() { |
157 LayoutSections(); | 141 LayoutSections(); |
158 info_bubble_->SizeToContents(); | 142 info_bubble_->SizeToContents(); |
159 } | 143 } |
160 | 144 |
161 //////////////////////////////////////////////////////////////////////////////// | 145 //////////////////////////////////////////////////////////////////////////////// |
162 // Section | 146 // Section |
163 | 147 |
164 Section::Section(PageInfoBubbleView* owner, | 148 Section::Section(bool state, |
165 const PageInfoModel::SectionInfo& section_info) | 149 const string16& headline, |
166 : owner_(owner), | 150 const string16& description) |
167 info_(section_info) { | 151 : state_(state), |
| 152 headline_(headline), |
| 153 description_(description) { |
168 if (!good_state_icon_) { | 154 if (!good_state_icon_) { |
169 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 155 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
170 good_state_icon_ = rb.GetBitmapNamed(IDR_PAGEINFO_GOOD); | 156 good_state_icon_ = rb.GetBitmapNamed(IDR_PAGEINFO_GOOD); |
171 bad_state_icon_ = rb.GetBitmapNamed(IDR_PAGEINFO_BAD); | 157 bad_state_icon_ = rb.GetBitmapNamed(IDR_PAGEINFO_BAD); |
172 } | 158 } |
173 | 159 |
174 if (info_.type == PageInfoModel::SECTION_INFO_IDENTITY || | 160 status_image_ = new views::ImageView(); |
175 info_.type == PageInfoModel::SECTION_INFO_CONNECTION) { | 161 status_image_->SetImage(state ? good_state_icon_ : bad_state_icon_); |
176 status_image_ = new views::ImageView(); | 162 AddChildView(status_image_); |
177 status_image_->SetImage(info_.state ? good_state_icon_ : bad_state_icon_); | |
178 AddChildView(status_image_); | |
179 } | |
180 | 163 |
181 headline_label_ = new views::Label(UTF16ToWideHack(info_.headline)); | 164 headline_label_ = new views::Label(UTF16ToWideHack(headline)); |
182 headline_label_->SetFont( | 165 headline_label_->SetFont( |
183 headline_label_->font().DeriveFont(0, gfx::Font::BOLD)); | 166 headline_label_->font().DeriveFont(0, gfx::Font::BOLD)); |
184 headline_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | 167 headline_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
185 AddChildView(headline_label_); | 168 AddChildView(headline_label_); |
186 | 169 |
187 description_label_ = new views::Label(UTF16ToWideHack(info_.description)); | 170 description_label_ = new views::Label(UTF16ToWideHack(description)); |
188 description_label_->SetMultiLine(true); | 171 description_label_->SetMultiLine(true); |
189 description_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | 172 description_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
190 // Allow linebreaking in the middle of words if necessary, so that extremely | 173 // Allow linebreaking in the middle of words if necessary, so that extremely |
191 // long hostnames (longer than one line) will still be completely shown. | 174 // long hostnames (longer than one line) will still be completely shown. |
192 description_label_->SetAllowCharacterBreak(true); | 175 description_label_->SetAllowCharacterBreak(true); |
193 AddChildView(description_label_); | 176 AddChildView(description_label_); |
194 | |
195 if (info_.type == PageInfoModel::SECTION_INFO_IDENTITY) { | |
196 link_ = new views::Link( | |
197 l10n_util::GetStringUTF16(IDS_PAGEINFO_CERT_INFO_BUTTON)); | |
198 link_->SetController(this); | |
199 AddChildView(link_); | |
200 } | |
201 } | 177 } |
202 | 178 |
203 Section::~Section() { | 179 Section::~Section() { |
204 } | 180 } |
205 | 181 |
206 int Section::GetHeightForWidth(int width) { | 182 int Section::GetHeightForWidth(int width) { |
207 return LayoutItems(true, width).height(); | 183 return LayoutItems(true, width).height(); |
208 } | 184 } |
209 | 185 |
210 void Section::Layout() { | 186 void Section::Layout() { |
211 LayoutItems(false, width()); | 187 LayoutItems(false, width()); |
212 } | 188 } |
213 | 189 |
214 void Section::LinkActivated(views::Link* source, int event_flags) { | |
215 owner_->ShowCertDialog(); | |
216 } | |
217 | |
218 gfx::Size Section::LayoutItems(bool compute_bounds_only, int width) { | 190 gfx::Size Section::LayoutItems(bool compute_bounds_only, int width) { |
219 int x = kHGapToBorder; | 191 int x = kHGapToBorder; |
220 int y = kVGapToImage; | 192 int y = kVGapToImage; |
221 | 193 |
222 // Layout the image, head-line and description. | 194 // Layout the image, head-line and description. |
223 gfx::Size size; | 195 gfx::Size size = status_image_->GetPreferredSize(); |
224 if (info_.type == PageInfoModel::SECTION_INFO_IDENTITY || | 196 if (!compute_bounds_only) |
225 info_.type == PageInfoModel::SECTION_INFO_CONNECTION) { | 197 status_image_->SetBounds(x, y, size.width(), size.height()); |
226 size = status_image_->GetPreferredSize(); | |
227 if (!compute_bounds_only) | |
228 status_image_->SetBounds(x, y, size.width(), size.height()); | |
229 } | |
230 int image_height = y + size.height(); | 198 int image_height = y + size.height(); |
231 x += size.width() + kHGapImageToDescription; | 199 x += size.width() + kHGapImageToDescription; |
232 int w = width - x - kTextPaddingRight; | 200 int w = width - x - kTextPaddingRight; |
233 y = kVGapToHeadline; | 201 y = kVGapToHeadline; |
234 if (!headline_label_->GetText().empty()) { | 202 if (!headline_label_->GetText().empty()) { |
235 size = headline_label_->GetPreferredSize(); | 203 size = headline_label_->GetPreferredSize(); |
236 if (!compute_bounds_only) | 204 if (!compute_bounds_only) |
237 headline_label_->SetBounds(x, y, w > 0 ? w : 0, size.height()); | 205 headline_label_->SetBounds(x, y, w > 0 ? w : 0, size.height()); |
238 y += size.height(); | 206 y += size.height(); |
239 } else { | 207 } else { |
240 if (!compute_bounds_only) | 208 if (!compute_bounds_only) |
241 headline_label_->SetBounds(x, y, 0, 0); | 209 headline_label_->SetBounds(x, y, 0, 0); |
242 } | 210 } |
243 if (w > 0) { | 211 if (w > 0) { |
244 int height = description_label_->GetHeightForWidth(w); | 212 int height = description_label_->GetHeightForWidth(w); |
245 if (!compute_bounds_only) | 213 if (!compute_bounds_only) |
246 description_label_->SetBounds(x, y, w, height); | 214 description_label_->SetBounds(x, y, w, height); |
247 y += height; | 215 y += height; |
248 } else { | 216 } else { |
249 if (!compute_bounds_only) | 217 if (!compute_bounds_only) |
250 description_label_->SetBounds(x, y, 0, 0); | 218 description_label_->SetBounds(x, y, 0, 0); |
251 } | 219 } |
252 if (info_.type == PageInfoModel::SECTION_INFO_IDENTITY) { | |
253 size = link_->GetPreferredSize(); | |
254 link_->SetBounds(x, y, size.width(), size.height()); | |
255 y += size.height(); | |
256 } | |
257 | 220 |
258 // Make sure the image is not truncated if the text doesn't contain much. | 221 // Make sure the image is not truncated if the text doesn't contain much. |
259 y = std::max(y, image_height); | 222 y = std::max(y, image_height); |
260 return gfx::Size(width, y); | 223 return gfx::Size(width, y); |
261 } | 224 } |
262 | 225 |
263 namespace browser { | 226 namespace browser { |
264 | 227 |
265 void ShowPageInfoBubble(gfx::NativeWindow parent, | 228 void ShowPageInfoBubble(gfx::NativeWindow parent, |
266 Profile* profile, | 229 Profile* profile, |
267 const GURL& url, | 230 const GURL& url, |
268 const NavigationEntry::SSLStatus& ssl, | 231 const NavigationEntry::SSLStatus& ssl, |
269 bool show_history) { | 232 bool show_history) { |
270 // Find where to point the bubble at. | 233 // Find where to point the bubble at. |
271 BrowserView* browser_view = | 234 BrowserView* browser_view = |
272 BrowserView::GetBrowserViewForNativeWindow(parent); | 235 BrowserView::GetBrowserViewForNativeWindow(parent); |
273 gfx::Rect bounds = browser_view->toolbar()->location_bar()->bounds(); | 236 gfx::Rect bounds = browser_view->toolbar()->location_bar()->bounds(); |
274 gfx::Point point; | 237 gfx::Point point; |
275 views::View::ConvertPointToScreen(browser_view->toolbar()->location_bar(), | 238 views::View::ConvertPointToScreen(browser_view->toolbar()->location_bar(), |
276 &point); | 239 &point); |
277 bounds.set_origin(point); | 240 bounds.set_origin(point); |
278 bounds.set_width(kIconOffset); | 241 bounds.set_width(kIconOffset); |
279 | 242 |
280 // Show the bubble. | 243 // Show the bubble. |
281 PageInfoBubbleView* page_info_bubble = | 244 PageInfoBubbleView* page_info_bubble = |
282 new PageInfoBubbleView(parent, profile, url, ssl, show_history); | 245 new PageInfoBubbleView(profile, url, ssl, show_history); |
283 InfoBubble* info_bubble = | 246 InfoBubble* info_bubble = |
284 InfoBubble::Show(browser_view->GetWidget(), bounds, | 247 InfoBubble::Show(browser_view->GetWidget(), bounds, |
285 BubbleBorder::TOP_LEFT, | 248 BubbleBorder::TOP_LEFT, |
286 page_info_bubble, page_info_bubble); | 249 page_info_bubble, page_info_bubble); |
287 page_info_bubble->set_info_bubble(info_bubble); | 250 page_info_bubble->set_info_bubble(info_bubble); |
288 } | 251 } |
289 | 252 |
290 } | 253 } |
OLD | NEW |