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