cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / chrome / browser / ui / views / location_bar / location_bar_view.cc
blobf97c39b674d533ac957b2565541db8aa3a8536ec
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.
5 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
7 #include <algorithm>
8 #include <map>
10 #include "base/i18n/rtl.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/stl_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/app/chrome_command_ids.h"
15 #include "chrome/browser/command_updater.h"
16 #include "chrome/browser/defaults.h"
17 #include "chrome/browser/extensions/api/omnibox/omnibox_api.h"
18 #include "chrome/browser/extensions/extension_action.h"
19 #include "chrome/browser/extensions/extension_action_manager.h"
20 #include "chrome/browser/extensions/extension_util.h"
21 #include "chrome/browser/extensions/location_bar_controller.h"
22 #include "chrome/browser/extensions/tab_helper.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/search/instant_service.h"
25 #include "chrome/browser/search/instant_service_factory.h"
26 #include "chrome/browser/search/search.h"
27 #include "chrome/browser/search_engines/template_url_service_factory.h"
28 #include "chrome/browser/themes/theme_properties.h"
29 #include "chrome/browser/translate/chrome_translate_client.h"
30 #include "chrome/browser/translate/translate_service.h"
31 #include "chrome/browser/ui/browser.h"
32 #include "chrome/browser/ui/browser_finder.h"
33 #include "chrome/browser/ui/browser_instant_controller.h"
34 #include "chrome/browser/ui/browser_window.h"
35 #include "chrome/browser/ui/content_settings/content_setting_bubble_model.h"
36 #include "chrome/browser/ui/passwords/manage_passwords_icon.h"
37 #include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h"
38 #include "chrome/browser/ui/tabs/tab_strip_model.h"
39 #include "chrome/browser/ui/view_ids.h"
40 #include "chrome/browser/ui/views/browser_dialogs.h"
41 #include "chrome/browser/ui/views/location_bar/content_setting_image_view.h"
42 #include "chrome/browser/ui/views/location_bar/ev_bubble_view.h"
43 #include "chrome/browser/ui/views/location_bar/keyword_hint_view.h"
44 #include "chrome/browser/ui/views/location_bar/location_bar_layout.h"
45 #include "chrome/browser/ui/views/location_bar/location_icon_view.h"
46 #include "chrome/browser/ui/views/location_bar/open_pdf_in_reader_view.h"
47 #include "chrome/browser/ui/views/location_bar/page_action_image_view.h"
48 #include "chrome/browser/ui/views/location_bar/page_action_with_badge_view.h"
49 #include "chrome/browser/ui/views/location_bar/selected_keyword_view.h"
50 #include "chrome/browser/ui/views/location_bar/star_view.h"
51 #include "chrome/browser/ui/views/location_bar/translate_icon_view.h"
52 #include "chrome/browser/ui/views/location_bar/zoom_bubble_view.h"
53 #include "chrome/browser/ui/views/location_bar/zoom_view.h"
54 #include "chrome/browser/ui/views/passwords/manage_passwords_bubble_view.h"
55 #include "chrome/browser/ui/views/passwords/manage_passwords_icon_view.h"
56 #include "chrome/browser/ui/views/translate/translate_bubble_view.h"
57 #include "chrome/common/pref_names.h"
58 #include "chrome/grit/generated_resources.h"
59 #include "components/favicon/content/content_favicon_driver.h"
60 #include "components/omnibox/browser/omnibox_popup_model.h"
61 #include "components/omnibox/browser/omnibox_popup_view.h"
62 #include "components/search_engines/template_url.h"
63 #include "components/search_engines/template_url_service.h"
64 #include "components/translate/core/browser/language_state.h"
65 #include "components/ui/zoom/zoom_controller.h"
66 #include "components/ui/zoom/zoom_event_manager.h"
67 #include "content/public/browser/render_widget_host_view.h"
68 #include "content/public/browser/web_contents.h"
69 #include "extensions/browser/extension_registry.h"
70 #include "extensions/common/feature_switch.h"
71 #include "grit/components_scaled_resources.h"
72 #include "grit/theme_resources.h"
73 #include "ui/accessibility/ax_view_state.h"
74 #include "ui/base/dragdrop/drag_drop_types.h"
75 #include "ui/base/l10n/l10n_util.h"
76 #include "ui/base/resource/material_design/material_design_controller.h"
77 #include "ui/base/resource/resource_bundle.h"
78 #include "ui/base/theme_provider.h"
79 #include "ui/compositor/paint_recorder.h"
80 #include "ui/events/event.h"
81 #include "ui/gfx/animation/slide_animation.h"
82 #include "ui/gfx/canvas.h"
83 #include "ui/gfx/color_utils.h"
84 #include "ui/gfx/image/image.h"
85 #include "ui/gfx/image/image_skia_operations.h"
86 #include "ui/gfx/scoped_canvas.h"
87 #include "ui/gfx/skia_util.h"
88 #include "ui/gfx/text_utils.h"
89 #include "ui/native_theme/native_theme.h"
90 #include "ui/views/background.h"
91 #include "ui/views/border.h"
92 #include "ui/views/button_drag_utils.h"
93 #include "ui/views/controls/button/image_button.h"
94 #include "ui/views/controls/label.h"
95 #include "ui/views/widget/widget.h"
97 #if !defined(OS_CHROMEOS)
98 #include "chrome/browser/ui/views/first_run_bubble.h"
99 #endif
101 using content::WebContents;
102 using views::View;
104 namespace {
106 int GetEditLeadingInternalSpace() {
107 // The textfield has 1 px of whitespace before the text in the RTL case only.
108 return base::i18n::IsRTL() ? 1 : 0;
111 } // namespace
114 // LocationBarView -----------------------------------------------------------
116 // static
118 // TODO(jonross): Replace with lookup once Material Design asset has landed.
119 // (https://crbug.com/495654)
120 // Thickness of the edges of the omnibox background images, for normal and
121 // popup windows.
122 const int kNormalEdgeThickness = 2;
123 const int kPopupEdgeThickness = 1;
125 const char LocationBarView::kViewClassName[] = "LocationBarView";
127 LocationBarView::LocationBarView(Browser* browser,
128 Profile* profile,
129 CommandUpdater* command_updater,
130 Delegate* delegate,
131 bool is_popup_mode)
132 : LocationBar(profile),
133 ChromeOmniboxEditController(command_updater),
134 browser_(browser),
135 omnibox_view_(NULL),
136 delegate_(delegate),
137 location_icon_view_(NULL),
138 ev_bubble_view_(NULL),
139 ime_inline_autocomplete_view_(NULL),
140 selected_keyword_view_(NULL),
141 suggested_text_view_(NULL),
142 keyword_hint_view_(NULL),
143 mic_search_view_(NULL),
144 zoom_view_(NULL),
145 open_pdf_in_reader_view_(NULL),
146 manage_passwords_icon_view_(NULL),
147 translate_icon_view_(NULL),
148 star_view_(NULL),
149 size_animation_(this),
150 is_popup_mode_(is_popup_mode),
151 show_focus_rect_(false),
152 template_url_service_(NULL),
153 dropdown_animation_offset_(0),
154 web_contents_null_at_last_refresh_(true) {
155 edit_bookmarks_enabled_.Init(
156 bookmarks::prefs::kEditBookmarksEnabled, profile->GetPrefs(),
157 base::Bind(&LocationBarView::UpdateWithoutTabRestore,
158 base::Unretained(this)));
160 if (browser_)
161 browser_->search_model()->AddObserver(this);
163 ui_zoom::ZoomEventManager::GetForBrowserContext(profile)
164 ->AddZoomEventManagerObserver(this);
167 LocationBarView::~LocationBarView() {
168 if (template_url_service_)
169 template_url_service_->RemoveObserver(this);
170 if (browser_)
171 browser_->search_model()->RemoveObserver(this);
173 ui_zoom::ZoomEventManager::GetForBrowserContext(profile())
174 ->RemoveZoomEventManagerObserver(this);
177 ////////////////////////////////////////////////////////////////////////////////
178 // LocationBarView, public:
180 void LocationBarView::Init() {
181 // We need to be in a Widget, otherwise GetNativeTheme() may change and we're
182 // not prepared for that.
183 DCHECK(GetWidget());
185 if (is_popup_mode_) {
186 const int kOmniboxPopupBorderImages[] =
187 IMAGE_GRID(IDR_OMNIBOX_POPUP_BORDER_AND_SHADOW);
188 border_painter_.reset(
189 views::Painter::CreateImageGridPainter(kOmniboxPopupBorderImages));
190 } else {
191 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
192 const gfx::Insets omnibox_border_insets(14, 9, 14, 9);
193 border_painter_.reset(views::Painter::CreateImagePainter(
194 *rb.GetImageSkiaNamed(IDR_OMNIBOX_BORDER), omnibox_border_insets));
197 location_icon_view_ = new LocationIconView(this);
198 location_icon_view_->set_drag_controller(this);
199 AddChildView(location_icon_view_);
201 // Determine the main font.
202 gfx::FontList font_list = ResourceBundle::GetSharedInstance().GetFontList(
203 ResourceBundle::BaseFont);
204 const int current_font_size = font_list.GetFontSize();
205 const int desired_font_size = browser_defaults::kOmniboxFontPixelSize;
206 if (current_font_size != desired_font_size) {
207 font_list =
208 font_list.DeriveWithSizeDelta(desired_font_size - current_font_size);
210 // Shrink large fonts to make them fit.
211 // TODO(pkasting): Stretch the location bar instead in this case.
212 const int location_height = GetInternalHeight(true);
213 font_list = font_list.DeriveWithHeightUpperBound(location_height);
215 // Determine the font for use inside the bubbles. The bubble background
216 // images have 1 px thick edges, which we don't want to overlap.
217 const int kBubbleInteriorVerticalPadding = 1;
218 const int bubble_padding = GetThemeProvider()->GetDisplayProperty(
219 ThemeProperties::PROPERTY_LOCATION_BAR_BUBBLE_VERTICAL_PADDING);
220 const int bubble_vertical_padding =
221 (bubble_padding + kBubbleInteriorVerticalPadding) * 2;
222 const gfx::FontList bubble_font_list(font_list.DeriveWithHeightUpperBound(
223 location_height - bubble_vertical_padding));
225 const SkColor background_color =
226 GetColor(SecurityStateModel::NONE, LocationBarView::BACKGROUND);
227 ev_bubble_view_ = new EVBubbleView(
228 bubble_font_list, GetColor(SecurityStateModel::EV_SECURE, SECURITY_TEXT),
229 background_color, this);
230 ev_bubble_view_->set_drag_controller(this);
231 AddChildView(ev_bubble_view_);
233 // Initialize the Omnibox view.
234 omnibox_view_ = new OmniboxViewViews(
235 this, profile(), command_updater(), is_popup_mode_, this, font_list);
236 omnibox_view_->Init();
237 omnibox_view_->SetFocusable(true);
238 AddChildView(omnibox_view_);
240 // Initialize the inline autocomplete view which is visible only when IME is
241 // turned on. Use the same font with the omnibox and highlighted background.
242 ime_inline_autocomplete_view_ = new views::Label(base::string16(), font_list);
243 ime_inline_autocomplete_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
244 ime_inline_autocomplete_view_->SetAutoColorReadabilityEnabled(false);
245 ime_inline_autocomplete_view_->set_background(
246 views::Background::CreateSolidBackground(GetNativeTheme()->GetSystemColor(
247 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused)));
248 ime_inline_autocomplete_view_->SetEnabledColor(
249 GetNativeTheme()->GetSystemColor(
250 ui::NativeTheme::kColorId_TextfieldSelectionColor));
251 ime_inline_autocomplete_view_->SetVisible(false);
252 AddChildView(ime_inline_autocomplete_view_);
254 const SkColor text_color = GetColor(SecurityStateModel::NONE, TEXT);
255 selected_keyword_view_ = new SelectedKeywordView(
256 bubble_font_list, text_color, background_color, profile());
257 AddChildView(selected_keyword_view_);
259 suggested_text_view_ = new views::Label(base::string16(), font_list);
260 suggested_text_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
261 suggested_text_view_->SetAutoColorReadabilityEnabled(false);
262 suggested_text_view_->SetEnabledColor(
263 GetColor(SecurityStateModel::NONE, LocationBarView::DEEMPHASIZED_TEXT));
264 suggested_text_view_->SetVisible(false);
265 AddChildView(suggested_text_view_);
267 keyword_hint_view_ = new KeywordHintView(
268 profile(), font_list,
269 GetColor(SecurityStateModel::NONE, LocationBarView::DEEMPHASIZED_TEXT),
270 background_color);
271 AddChildView(keyword_hint_view_);
273 mic_search_view_ = new views::ImageButton(this);
274 mic_search_view_->set_id(VIEW_ID_MIC_SEARCH_BUTTON);
275 mic_search_view_->SetAccessibilityFocusable(true);
276 mic_search_view_->SetTooltipText(
277 l10n_util::GetStringUTF16(IDS_TOOLTIP_MIC_SEARCH));
278 mic_search_view_->SetImage(
279 views::Button::STATE_NORMAL,
280 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
281 IDR_OMNIBOX_MIC_SEARCH));
282 mic_search_view_->SetImageAlignment(views::ImageButton::ALIGN_CENTER,
283 views::ImageButton::ALIGN_MIDDLE);
284 mic_search_view_->SetVisible(false);
285 AddChildView(mic_search_view_);
287 for (ContentSettingsType type :
288 ContentSettingBubbleModel::GetSupportedBubbleTypes()) {
289 ContentSettingImageView* content_blocked_view = new ContentSettingImageView(
290 type, this, bubble_font_list, text_color, background_color);
291 content_setting_views_.push_back(content_blocked_view);
292 content_blocked_view->SetVisible(false);
293 AddChildView(content_blocked_view);
296 zoom_view_ = new ZoomView(delegate_);
297 AddChildView(zoom_view_);
299 open_pdf_in_reader_view_ = new OpenPDFInReaderView();
300 AddChildView(open_pdf_in_reader_view_);
302 manage_passwords_icon_view_ = new ManagePasswordsIconView(command_updater());
303 AddChildView(manage_passwords_icon_view_);
305 translate_icon_view_ = new TranslateIconView(command_updater());
306 translate_icon_view_->SetVisible(false);
307 AddChildView(translate_icon_view_);
309 star_view_ = new StarView(command_updater(), browser_);
310 star_view_->SetVisible(false);
311 AddChildView(star_view_);
313 // Initialize the location entry. We do this to avoid a black flash which is
314 // visible when the location entry has just been initialized.
315 Update(NULL);
317 size_animation_.Reset(1);
320 bool LocationBarView::IsInitialized() const {
321 return omnibox_view_ != NULL;
324 SkColor LocationBarView::GetColor(
325 SecurityStateModel::SecurityLevel security_level,
326 ColorKind kind) const {
327 const ui::NativeTheme* native_theme = GetNativeTheme();
328 switch (kind) {
329 case BACKGROUND:
330 return native_theme->GetSystemColor(
331 ui::NativeTheme::kColorId_TextfieldDefaultBackground);
333 case TEXT:
334 return native_theme->GetSystemColor(
335 ui::NativeTheme::kColorId_TextfieldDefaultColor);
337 case SELECTED_TEXT:
338 return native_theme->GetSystemColor(
339 ui::NativeTheme::kColorId_TextfieldSelectionColor);
341 case DEEMPHASIZED_TEXT:
342 return color_utils::AlphaBlend(
343 GetColor(security_level, TEXT),
344 GetColor(security_level, BACKGROUND),
345 128);
347 case SECURITY_TEXT: {
348 SkColor color;
349 switch (security_level) {
350 case SecurityStateModel::EV_SECURE:
351 case SecurityStateModel::SECURE:
352 color = SkColorSetRGB(7, 149, 0);
353 break;
355 case SecurityStateModel::SECURITY_POLICY_WARNING:
356 return GetColor(security_level, DEEMPHASIZED_TEXT);
357 break;
359 case SecurityStateModel::SECURITY_ERROR:
360 color = SkColorSetRGB(162, 0, 0);
361 break;
363 case SecurityStateModel::SECURITY_WARNING:
364 return GetColor(security_level, TEXT);
365 break;
367 default:
368 NOTREACHED();
369 return GetColor(security_level, TEXT);
371 return color_utils::GetReadableColor(
372 color, GetColor(security_level, BACKGROUND));
375 default:
376 NOTREACHED();
377 return GetColor(security_level, TEXT);
381 void LocationBarView::ZoomChangedForActiveTab(bool can_show_bubble) {
382 DCHECK(zoom_view_);
383 if (RefreshZoomView()) {
384 Layout();
385 SchedulePaint();
388 WebContents* web_contents = GetWebContents();
389 if (can_show_bubble && zoom_view_->visible() && web_contents)
390 ZoomBubbleView::ShowBubble(web_contents, true);
393 void LocationBarView::SetPreviewEnabledPageAction(ExtensionAction* page_action,
394 bool preview_enabled) {
395 if (is_popup_mode_)
396 return;
398 DCHECK(page_action);
399 WebContents* web_contents = GetWebContents();
401 RefreshPageActionViews();
402 PageActionWithBadgeView* page_action_view =
403 static_cast<PageActionWithBadgeView*>(GetPageActionView(page_action));
404 DCHECK(page_action_view);
405 if (!page_action_view)
406 return;
408 page_action_view->image_view()->set_preview_enabled(preview_enabled);
409 page_action_view->UpdateVisibility(web_contents);
410 Layout();
411 SchedulePaint();
414 PageActionWithBadgeView* LocationBarView::GetPageActionView(
415 ExtensionAction* page_action) {
416 DCHECK(page_action);
417 for (PageActionViews::const_iterator i(page_action_views_.begin());
418 i != page_action_views_.end(); ++i) {
419 if ((*i)->image_view()->extension_action() == page_action)
420 return *i;
422 return NULL;
425 void LocationBarView::SetStarToggled(bool on) {
426 if (star_view_)
427 star_view_->SetToggled(on);
430 void LocationBarView::SetTranslateIconToggled(bool on) {
431 translate_icon_view_->SetToggled(on);
434 gfx::Point LocationBarView::GetOmniboxViewOrigin() const {
435 gfx::Point origin(omnibox_view_->bounds().origin());
436 origin.set_x(GetMirroredXInView(origin.x()));
437 views::View::ConvertPointToScreen(this, &origin);
438 return origin;
441 void LocationBarView::SetImeInlineAutocompletion(const base::string16& text) {
442 ime_inline_autocomplete_view_->SetText(text);
443 ime_inline_autocomplete_view_->SetVisible(!text.empty());
446 void LocationBarView::SetGrayTextAutocompletion(const base::string16& text) {
447 if (suggested_text_view_->text() != text) {
448 suggested_text_view_->SetText(text);
449 suggested_text_view_->SetVisible(!text.empty());
450 Layout();
451 SchedulePaint();
455 base::string16 LocationBarView::GetGrayTextAutocompletion() const {
456 return HasValidSuggestText() ?
457 suggested_text_view_->text() : base::string16();
460 void LocationBarView::SetShowFocusRect(bool show) {
461 show_focus_rect_ = show;
462 SchedulePaint();
465 void LocationBarView::SelectAll() {
466 omnibox_view_->SelectAll(true);
469 gfx::Point LocationBarView::GetLocationBarAnchorPoint() const {
470 // The +1 in the next line creates a 1-px gap between icon and arrow tip.
471 const int icon_internal_padding = GetThemeProvider()->GetDisplayProperty(
472 ThemeProperties::PROPERTY_ICON_LABEL_VIEW_TRAILING_PADDING);
473 gfx::Point icon_bottom(0, location_icon_view_->GetImageBounds().bottom() -
474 icon_internal_padding + 1);
475 gfx::Point icon_center(location_icon_view_->GetImageBounds().CenterPoint());
476 gfx::Point point(icon_center.x(), icon_bottom.y());
477 ConvertPointToTarget(location_icon_view_, this, &point);
478 return point;
481 int LocationBarView::GetInternalHeight(bool use_preferred_size) {
482 int total_height =
483 use_preferred_size ? GetPreferredSize().height() : height();
484 return std::max(total_height - (VerticalPadding() * 2), 0);
487 void LocationBarView::GetOmniboxPopupPositioningInfo(
488 gfx::Point* top_left_screen_coord,
489 int* popup_width,
490 int* left_margin,
491 int* right_margin,
492 int top_edge_overlap) {
493 *top_left_screen_coord = gfx::Point(0, parent()->height() - top_edge_overlap);
494 views::View::ConvertPointToScreen(parent(), top_left_screen_coord);
496 *popup_width = parent()->width();
497 gfx::Rect location_bar_bounds(bounds());
498 location_bar_bounds.Inset(kNormalEdgeThickness, 0);
499 *left_margin = location_bar_bounds.x();
500 *right_margin = *popup_width - location_bar_bounds.right();
503 ////////////////////////////////////////////////////////////////////////////////
504 // LocationBarView, public LocationBar implementation:
506 void LocationBarView::FocusLocation(bool select_all) {
507 omnibox_view_->SetFocus();
508 if (select_all)
509 omnibox_view_->SelectAll(true);
512 void LocationBarView::Revert() {
513 omnibox_view_->RevertAll();
516 OmniboxView* LocationBarView::GetOmniboxView() {
517 return omnibox_view_;
520 ////////////////////////////////////////////////////////////////////////////////
521 // LocationBarView, public views::View implementation:
523 bool LocationBarView::HasFocus() const {
524 return omnibox_view_->model()->has_focus();
527 void LocationBarView::GetAccessibleState(ui::AXViewState* state) {
528 state->role = ui::AX_ROLE_GROUP;
531 gfx::Size LocationBarView::GetPreferredSize() const {
532 // Compute minimum height.
533 gfx::Size min_size(border_painter_->GetMinimumSize());
534 // For non-material the size of the asset determines the size of the
535 // LocationBarView. For Material Design the height is encoded as a constant in
536 // ThemeProperties.
537 ui::ThemeProvider* theme_provider = GetThemeProvider();
538 if (ui::MaterialDesignController::IsModeMaterial()) {
539 min_size.set_height(theme_provider->GetDisplayProperty(
540 ThemeProperties::PROPERTY_LOCATION_BAR_HEIGHT));
543 if (!IsInitialized())
544 return min_size;
546 min_size.set_height(min_size.height() * size_animation_.GetCurrentValue());
548 const int horizontal_item_padding = theme_provider->GetDisplayProperty(
549 ThemeProperties::PROPERTY_LOCATION_BAR_HORIZONTAL_PADDING);
550 const int bubble_horizontal_padding = theme_provider->GetDisplayProperty(
551 ThemeProperties::PROPERTY_LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING);
553 // Compute width of omnibox-leading content.
554 const int horizontal_edge_thickness = GetHorizontalEdgeThickness();
555 int leading_width = horizontal_edge_thickness;
556 if (ShouldShowKeywordBubble()) {
557 // The selected keyword view can collapse completely.
558 } else if (ShouldShowEVBubble()) {
559 leading_width += bubble_horizontal_padding +
560 ev_bubble_view_->GetMinimumSizeForLabelText(
561 GetToolbarModel()->GetEVCertName())
562 .width();
563 } else {
564 leading_width +=
565 horizontal_item_padding + location_icon_view_->GetMinimumSize().width();
568 // Compute width of omnibox-trailing content.
569 int trailing_width = horizontal_edge_thickness;
570 trailing_width += IncrementalMinimumWidth(star_view_) +
571 IncrementalMinimumWidth(translate_icon_view_) +
572 IncrementalMinimumWidth(open_pdf_in_reader_view_) +
573 IncrementalMinimumWidth(manage_passwords_icon_view_) +
574 IncrementalMinimumWidth(zoom_view_) +
575 IncrementalMinimumWidth(mic_search_view_);
576 for (PageActionViews::const_iterator i(page_action_views_.begin());
577 i != page_action_views_.end(); ++i)
578 trailing_width += IncrementalMinimumWidth((*i));
579 for (ContentSettingViews::const_iterator i(content_setting_views_.begin());
580 i != content_setting_views_.end(); ++i)
581 trailing_width += IncrementalMinimumWidth((*i));
583 min_size.set_width(leading_width + omnibox_view_->GetMinimumSize().width() +
584 2 * horizontal_item_padding -
585 omnibox_view_->GetInsets().width() + trailing_width);
586 return min_size;
589 void LocationBarView::Layout() {
590 if (!IsInitialized())
591 return;
593 selected_keyword_view_->SetVisible(false);
594 location_icon_view_->SetVisible(false);
595 ev_bubble_view_->SetVisible(false);
596 keyword_hint_view_->SetVisible(false);
598 ui::ThemeProvider* theme_provider = GetThemeProvider();
599 const int horizontal_item_padding = theme_provider->GetDisplayProperty(
600 ThemeProperties::PROPERTY_LOCATION_BAR_HORIZONTAL_PADDING);
601 int trailing_edge_item_padding = 0;
602 if (!ui::MaterialDesignController::IsModeMaterial()) {
603 trailing_edge_item_padding = horizontal_item_padding -
604 GetHorizontalEdgeThickness() -
605 omnibox_view_->GetInsets().right();
608 LocationBarLayout leading_decorations(
609 LocationBarLayout::LEFT_EDGE, horizontal_item_padding,
610 horizontal_item_padding - omnibox_view_->GetInsets().left() -
611 GetEditLeadingInternalSpace());
612 LocationBarLayout trailing_decorations(LocationBarLayout::RIGHT_EDGE,
613 horizontal_item_padding,
614 trailing_edge_item_padding);
616 const base::string16 keyword(omnibox_view_->model()->keyword());
617 // In some cases (e.g. fullscreen mode) we may have 0 height. We still want
618 // to position our child views in this case, because other things may be
619 // positioned relative to them (e.g. the "bookmark added" bubble if the user
620 // hits ctrl-d).
621 const int bubble_vertical_padding =
622 GetVerticalEdgeThickness() +
623 theme_provider->GetDisplayProperty(
624 ThemeProperties::PROPERTY_LOCATION_BAR_BUBBLE_VERTICAL_PADDING);
625 const int bubble_height =
626 std::max(height() - (bubble_vertical_padding * 2), 0);
627 const int bubble_horizontal_padding = theme_provider->GetDisplayProperty(
628 ThemeProperties::PROPERTY_LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING);
629 const int location_height = GetInternalHeight(false);
630 const int vertical_padding = VerticalPadding();
631 if (ShouldShowKeywordBubble()) {
632 leading_decorations.AddDecoration(bubble_vertical_padding, bubble_height,
633 true, 0, bubble_horizontal_padding,
634 horizontal_item_padding,
635 selected_keyword_view_);
636 if (selected_keyword_view_->keyword() != keyword) {
637 selected_keyword_view_->SetKeyword(keyword);
638 const TemplateURL* template_url =
639 TemplateURLServiceFactory::GetForProfile(profile())->
640 GetTemplateURLForKeyword(keyword);
641 if (template_url &&
642 (template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION)) {
643 gfx::Image image = extensions::OmniboxAPI::Get(profile())->
644 GetOmniboxIcon(template_url->GetExtensionId());
645 selected_keyword_view_->SetImage(image.AsImageSkia());
646 selected_keyword_view_->set_is_extension_icon(true);
647 } else {
648 selected_keyword_view_->SetImage(
649 *(GetThemeProvider()->GetImageSkiaNamed(IDR_OMNIBOX_SEARCH)));
650 selected_keyword_view_->set_is_extension_icon(false);
653 } else if (ShouldShowEVBubble()) {
654 ev_bubble_view_->SetLabel(GetToolbarModel()->GetEVCertName());
655 // The largest fraction of the omnibox that can be taken by the EV bubble.
656 const double kMaxBubbleFraction = 0.5;
657 leading_decorations.AddDecoration(
658 bubble_vertical_padding, bubble_height, false, kMaxBubbleFraction,
659 bubble_horizontal_padding, horizontal_item_padding, ev_bubble_view_);
660 } else {
661 leading_decorations.AddDecoration(vertical_padding, location_height,
662 location_icon_view_);
665 if (star_view_->visible()) {
666 trailing_decorations.AddDecoration(vertical_padding, location_height,
667 star_view_);
669 if (translate_icon_view_->visible()) {
670 trailing_decorations.AddDecoration(vertical_padding, location_height,
671 translate_icon_view_);
673 if (open_pdf_in_reader_view_->visible()) {
674 trailing_decorations.AddDecoration(vertical_padding, location_height,
675 open_pdf_in_reader_view_);
677 if (manage_passwords_icon_view_->visible()) {
678 trailing_decorations.AddDecoration(vertical_padding, location_height,
679 manage_passwords_icon_view_);
681 for (PageActionViews::const_iterator i(page_action_views_.begin());
682 i != page_action_views_.end(); ++i) {
683 if ((*i)->visible()) {
684 trailing_decorations.AddDecoration(vertical_padding, location_height,
685 (*i));
688 if (zoom_view_->visible()) {
689 trailing_decorations.AddDecoration(vertical_padding, location_height,
690 zoom_view_);
692 for (ContentSettingViews::const_reverse_iterator i(
693 content_setting_views_.rbegin()); i != content_setting_views_.rend();
694 ++i) {
695 if ((*i)->visible()) {
696 trailing_decorations.AddDecoration(bubble_vertical_padding, bubble_height,
697 false, 0, horizontal_item_padding,
698 horizontal_item_padding, (*i));
701 if (mic_search_view_->visible()) {
702 trailing_decorations.AddDecoration(vertical_padding, location_height,
703 mic_search_view_);
705 // Because IMEs may eat the tab key, we don't show "press tab to search" while
706 // IME composition is in progress.
707 if (!keyword.empty() && omnibox_view_->model()->is_keyword_hint() &&
708 !omnibox_view_->IsImeComposing()) {
709 trailing_decorations.AddDecoration(
710 vertical_padding, location_height, true, 0, horizontal_item_padding,
711 horizontal_item_padding, keyword_hint_view_);
712 if (keyword_hint_view_->keyword() != keyword)
713 keyword_hint_view_->SetKeyword(keyword);
716 // Perform layout.
717 const int horizontal_edge_thickness = GetHorizontalEdgeThickness();
718 int full_width = width() - (2 * horizontal_edge_thickness);
720 int entry_width = full_width;
721 leading_decorations.LayoutPass1(&entry_width);
722 trailing_decorations.LayoutPass1(&entry_width);
723 leading_decorations.LayoutPass2(&entry_width);
724 trailing_decorations.LayoutPass2(&entry_width);
726 int location_needed_width = omnibox_view_->GetTextWidth();
727 int available_width = entry_width - location_needed_width;
728 // The bounds must be wide enough for all the decorations to fit.
729 gfx::Rect location_bounds(horizontal_edge_thickness, vertical_padding,
730 std::max(full_width, full_width - entry_width),
731 location_height);
732 leading_decorations.LayoutPass3(&location_bounds, &available_width);
733 trailing_decorations.LayoutPass3(&location_bounds, &available_width);
735 // Layout out the suggested text view right aligned to the location
736 // entry. Only show the suggested text if we can fit the text from one
737 // character before the end of the selection to the end of the text and the
738 // suggested text. If we can't it means either the suggested text is too big,
739 // or the user has scrolled.
741 // TODO(sky): We could potentially adjust this to take into account suggested
742 // text to force using minimum size if necessary, but currently the chance of
743 // showing keyword hints and suggested text is minimal and we're not confident
744 // this is the right approach for suggested text.
746 int omnibox_view_margin = 0;
747 if (suggested_text_view_->visible()) {
748 // We do not display the suggested text when it contains a mix of RTL and
749 // LTR characters since this could mean the suggestion should be displayed
750 // in the middle of the string.
751 base::i18n::TextDirection text_direction =
752 base::i18n::GetStringDirection(omnibox_view_->GetText());
753 if (text_direction !=
754 base::i18n::GetStringDirection(suggested_text_view_->text()))
755 text_direction = base::i18n::UNKNOWN_DIRECTION;
757 // TODO(sky): need to layout when the user changes caret position.
758 gfx::Size suggested_text_size(suggested_text_view_->GetPreferredSize());
759 if (suggested_text_size.width() > available_width ||
760 text_direction == base::i18n::UNKNOWN_DIRECTION) {
761 // Hide the suggested text if the user has scrolled or we can't fit all
762 // the suggested text, or we have a mix of RTL and LTR characters.
763 suggested_text_view_->SetBounds(0, 0, 0, 0);
764 } else {
765 location_needed_width =
766 std::min(location_needed_width,
767 location_bounds.width() - suggested_text_size.width());
768 gfx::Rect suggested_text_bounds(location_bounds.x(), location_bounds.y(),
769 suggested_text_size.width(),
770 location_bounds.height());
771 // TODO(sky): figure out why this needs the -1.
772 suggested_text_bounds.Offset(location_needed_width - 1, 0);
774 // We reverse the order of the location entry and suggested text if:
775 // - Chrome is RTL but the text is fully LTR, or
776 // - Chrome is LTR but the text is fully RTL.
777 // This ensures the suggested text is correctly displayed to the right
778 // (or left) of the user text.
779 if (text_direction == (base::i18n::IsRTL() ?
780 base::i18n::LEFT_TO_RIGHT : base::i18n::RIGHT_TO_LEFT)) {
781 // TODO(sky): Figure out why we need the +1.
782 suggested_text_bounds.set_x(location_bounds.x() + 1);
783 // Use a margin to prevent omnibox text from overlapping suggest text.
784 omnibox_view_margin = suggested_text_bounds.width();
786 suggested_text_view_->SetBoundsRect(suggested_text_bounds);
790 omnibox_view_->SetBorder(
791 views::Border::CreateEmptyBorder(0, 0, 0, omnibox_view_margin));
793 // Layout |ime_inline_autocomplete_view_| next to the user input.
794 if (ime_inline_autocomplete_view_->visible()) {
795 int width =
796 gfx::GetStringWidth(ime_inline_autocomplete_view_->text(),
797 ime_inline_autocomplete_view_->font_list()) +
798 ime_inline_autocomplete_view_->GetInsets().width();
799 // All the target languages (IMEs) are LTR, and we do not need to support
800 // RTL so far. In other words, no testable RTL environment so far.
801 int x = location_needed_width;
802 if (width > entry_width)
803 x = 0;
804 else if (location_needed_width + width > entry_width)
805 x = entry_width - width;
806 location_bounds.set_width(x);
807 ime_inline_autocomplete_view_->SetBounds(
808 location_bounds.right(), location_bounds.y(),
809 std::min(width, entry_width), location_bounds.height());
811 omnibox_view_->SetBoundsRect(location_bounds);
814 void LocationBarView::Update(const WebContents* contents) {
815 mic_search_view_->SetVisible(
816 !GetToolbarModel()->input_in_progress() && browser_ &&
817 browser_->search_model()->voice_search_supported());
818 RefreshContentSettingViews();
819 RefreshZoomView();
820 RefreshPageActionViews();
821 RefreshTranslateIcon();
822 RefreshManagePasswordsIconView();
823 content::WebContents* web_contents_for_sub_views =
824 GetToolbarModel()->input_in_progress() ? NULL : GetWebContents();
825 open_pdf_in_reader_view_->Update(web_contents_for_sub_views);
827 if (star_view_)
828 UpdateBookmarkStarVisibility();
830 if (contents)
831 omnibox_view_->OnTabChanged(contents);
832 else
833 omnibox_view_->Update();
835 OnChanged(); // NOTE: Calls Layout().
838 void LocationBarView::ResetTabState(WebContents* contents) {
839 omnibox_view_->ResetTabState(contents);
842 ////////////////////////////////////////////////////////////////////////////////
843 // LocationBarView, public OmniboxEditController implementation:
845 void LocationBarView::UpdateWithoutTabRestore() {
846 Update(nullptr);
849 void LocationBarView::ShowURL() {
850 omnibox_view_->ShowURL();
853 ToolbarModel* LocationBarView::GetToolbarModel() {
854 return delegate_->GetToolbarModel();
857 WebContents* LocationBarView::GetWebContents() {
858 return delegate_->GetWebContents();
861 ////////////////////////////////////////////////////////////////////////////////
862 // LocationBarView, private:
864 int LocationBarView::IncrementalMinimumWidth(views::View* view) const {
865 const int horizontal_item_padding = GetThemeProvider()->GetDisplayProperty(
866 ThemeProperties::PROPERTY_LOCATION_BAR_HORIZONTAL_PADDING);
867 return view->visible()
868 ? (horizontal_item_padding + view->GetMinimumSize().width())
869 : 0;
872 int LocationBarView::GetHorizontalEdgeThickness() const {
873 // In maximized popup mode, there isn't any edge.
874 return (is_popup_mode_ && browser_ && browser_->window() &&
875 browser_->window()->IsMaximized())
877 : GetVerticalEdgeThickness();
880 int LocationBarView::GetVerticalEdgeThickness() const {
881 return is_popup_mode_ ? kPopupEdgeThickness : kNormalEdgeThickness;
884 int LocationBarView::VerticalPadding() const {
885 return is_popup_mode_
886 ? kPopupEdgeThickness
887 : GetThemeProvider()->GetDisplayProperty(
888 ThemeProperties::PROPERTY_LOCATION_BAR_VERTICAL_PADDING);
891 bool LocationBarView::RefreshContentSettingViews() {
892 bool visibility_changed = false;
893 for (ContentSettingViews::const_iterator i(content_setting_views_.begin());
894 i != content_setting_views_.end(); ++i) {
895 const bool was_visible = (*i)->visible();
896 (*i)->Update(GetToolbarModel()->input_in_progress() ?
897 NULL : GetWebContents());
898 if (was_visible != (*i)->visible())
899 visibility_changed = true;
901 return visibility_changed;
904 void LocationBarView::DeletePageActionViews() {
905 for (PageActionViews::const_iterator i(page_action_views_.begin());
906 i != page_action_views_.end(); ++i)
907 RemoveChildView(*i);
908 STLDeleteElements(&page_action_views_);
911 bool LocationBarView::RefreshPageActionViews() {
912 if (is_popup_mode_)
913 return false;
915 bool changed = false;
916 PageActions new_page_actions;
918 WebContents* web_contents = GetWebContents();
919 if (web_contents) {
920 extensions::TabHelper* extensions_tab_helper =
921 extensions::TabHelper::FromWebContents(web_contents);
922 extensions::LocationBarController* controller =
923 extensions_tab_helper->location_bar_controller();
924 new_page_actions = controller->GetCurrentActions();
926 web_contents_null_at_last_refresh_ = web_contents == NULL;
928 // On startup we sometimes haven't loaded any extensions. This makes sure
929 // we catch up when the extensions (and any page actions) load.
930 if (PageActionsDiffer(new_page_actions)) {
931 changed = true;
933 DeletePageActionViews();
935 // Create the page action views.
936 for (PageActions::const_iterator i = new_page_actions.begin();
937 i != new_page_actions.end(); ++i) {
938 PageActionWithBadgeView* page_action_view = new PageActionWithBadgeView(
939 delegate_->CreatePageActionImageView(this, *i));
940 page_action_view->SetVisible(false);
941 page_action_views_.push_back(page_action_view);
944 View* right_anchor = open_pdf_in_reader_view_;
945 if (!right_anchor)
946 right_anchor = star_view_;
947 DCHECK(right_anchor);
949 // |page_action_views_| are ordered right-to-left. Add them as children in
950 // reverse order so the logical order and visual order match for
951 // accessibility purposes.
952 for (PageActionViews::reverse_iterator i = page_action_views_.rbegin();
953 i != page_action_views_.rend(); ++i)
954 AddChildViewAt(*i, GetIndexOf(right_anchor));
957 for (PageActionViews::const_iterator i(page_action_views_.begin());
958 i != page_action_views_.end(); ++i) {
959 bool old_visibility = (*i)->visible();
960 (*i)->UpdateVisibility(
961 GetToolbarModel()->input_in_progress() ? NULL : web_contents);
962 changed |= old_visibility != (*i)->visible();
964 return changed;
967 bool LocationBarView::PageActionsDiffer(
968 const PageActions& page_actions) const {
969 if (page_action_views_.size() != page_actions.size())
970 return true;
972 for (size_t index = 0; index < page_actions.size(); ++index) {
973 PageActionWithBadgeView* view = page_action_views_[index];
974 if (view->image_view()->extension_action() != page_actions[index])
975 return true;
978 return false;
981 bool LocationBarView::RefreshZoomView() {
982 DCHECK(zoom_view_);
983 WebContents* web_contents = GetWebContents();
984 if (!web_contents)
985 return false;
986 const bool was_visible = zoom_view_->visible();
987 zoom_view_->Update(ui_zoom::ZoomController::FromWebContents(web_contents));
988 if (!zoom_view_->visible())
989 ZoomBubbleView::CloseBubble();
990 return was_visible != zoom_view_->visible();
993 void LocationBarView::OnDefaultZoomLevelChanged() {
994 RefreshZoomView();
997 void LocationBarView::RefreshTranslateIcon() {
998 if (!TranslateService::IsTranslateBubbleEnabled())
999 return;
1001 WebContents* web_contents = GetWebContents();
1002 if (!web_contents)
1003 return;
1004 translate::LanguageState& language_state =
1005 ChromeTranslateClient::FromWebContents(web_contents)->GetLanguageState();
1006 bool enabled = language_state.translate_enabled();
1007 command_updater()->UpdateCommandEnabled(IDC_TRANSLATE_PAGE, enabled);
1008 translate_icon_view_->SetVisible(enabled);
1009 translate_icon_view_->SetToggled(language_state.IsPageTranslated());
1010 if (!enabled)
1011 TranslateBubbleView::CloseBubble();
1014 bool LocationBarView::RefreshManagePasswordsIconView() {
1015 DCHECK(manage_passwords_icon_view_);
1016 WebContents* web_contents = GetWebContents();
1017 if (!web_contents)
1018 return false;
1019 const bool was_visible = manage_passwords_icon_view_->visible();
1020 ManagePasswordsUIController::FromWebContents(
1021 web_contents)->UpdateIconAndBubbleState(manage_passwords_icon_view_);
1022 return was_visible != manage_passwords_icon_view_->visible();
1025 void LocationBarView::ShowFirstRunBubbleInternal() {
1026 // First run bubble doesn't make sense for Chrome OS.
1027 #if !defined(OS_CHROMEOS)
1028 WebContents* web_contents = delegate_->GetWebContents();
1029 if (!web_contents)
1030 return;
1031 Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
1032 if (browser)
1033 FirstRunBubble::ShowBubble(browser, location_icon_view_);
1034 #endif
1037 bool LocationBarView::HasValidSuggestText() const {
1038 return suggested_text_view_->visible() &&
1039 !suggested_text_view_->size().IsEmpty();
1042 bool LocationBarView::ShouldShowKeywordBubble() const {
1043 return !omnibox_view_->model()->keyword().empty() &&
1044 !omnibox_view_->model()->is_keyword_hint();
1047 bool LocationBarView::ShouldShowEVBubble() const {
1048 const ChromeToolbarModel* chrome_toolbar_model =
1049 static_cast<const ChromeToolbarModel*>(GetToolbarModel());
1050 return (chrome_toolbar_model->GetSecurityLevel(false) ==
1051 SecurityStateModel::EV_SECURE);
1054 ////////////////////////////////////////////////////////////////////////////////
1055 // LocationBarView, private LocationBar implementation:
1057 void LocationBarView::ShowFirstRunBubble() {
1058 // Wait until search engines have loaded to show the first run bubble.
1059 TemplateURLService* url_service =
1060 TemplateURLServiceFactory::GetForProfile(profile());
1061 if (!url_service->loaded()) {
1062 template_url_service_ = url_service;
1063 template_url_service_->AddObserver(this);
1064 template_url_service_->Load();
1065 return;
1067 ShowFirstRunBubbleInternal();
1070 GURL LocationBarView::GetDestinationURL() const {
1071 return destination_url();
1074 WindowOpenDisposition LocationBarView::GetWindowOpenDisposition() const {
1075 return disposition();
1078 ui::PageTransition LocationBarView::GetPageTransition() const {
1079 return transition();
1082 void LocationBarView::AcceptInput() {
1083 omnibox_view_->model()->AcceptInput(CURRENT_TAB, false);
1086 void LocationBarView::FocusSearch() {
1087 omnibox_view_->SetFocus();
1088 omnibox_view_->SetForcedQuery();
1091 void LocationBarView::UpdateContentSettingsIcons() {
1092 if (RefreshContentSettingViews()) {
1093 Layout();
1094 SchedulePaint();
1098 void LocationBarView::UpdateManagePasswordsIconAndBubble() {
1099 if (RefreshManagePasswordsIconView()) {
1100 Layout();
1101 SchedulePaint();
1105 void LocationBarView::UpdatePageActions() {
1106 if (RefreshPageActionViews()) { // Changed.
1107 Layout();
1108 SchedulePaint();
1112 void LocationBarView::UpdateBookmarkStarVisibility() {
1113 if (star_view_) {
1114 star_view_->SetVisible(
1115 browser_defaults::bookmarks_enabled && !is_popup_mode_ &&
1116 !GetToolbarModel()->input_in_progress() &&
1117 edit_bookmarks_enabled_.GetValue() &&
1118 !IsBookmarkStarHiddenByExtension());
1122 void LocationBarView::UpdateLocationBarVisibility(bool visible, bool animate) {
1123 if (!animate) {
1124 size_animation_.Reset(visible ? 1 : 0);
1125 return;
1128 if (visible)
1129 size_animation_.Show();
1130 else
1131 size_animation_.Hide();
1134 bool LocationBarView::ShowPageActionPopup(
1135 const extensions::Extension* extension,
1136 bool grant_tab_permissions) {
1137 ExtensionAction* extension_action =
1138 extensions::ExtensionActionManager::Get(profile())->GetPageAction(
1139 *extension);
1140 CHECK(extension_action);
1141 PageActionWithBadgeView* page_action_view =
1142 GetPageActionView(extension_action);
1143 if (!page_action_view) {
1144 CHECK(!web_contents_null_at_last_refresh_);
1145 CHECK(!is_popup_mode_);
1146 CHECK(!extensions::FeatureSwitch::extension_action_redesign()->IsEnabled());
1147 CHECK(false);
1149 PageActionImageView* page_action_image_view = page_action_view->image_view();
1150 CHECK(page_action_image_view);
1151 ExtensionActionViewController* extension_action_view_controller =
1152 page_action_image_view->view_controller();
1153 CHECK(extension_action_view_controller);
1154 return extension_action_view_controller->ExecuteAction(grant_tab_permissions);
1157 void LocationBarView::UpdateOpenPDFInReaderPrompt() {
1158 open_pdf_in_reader_view_->Update(
1159 GetToolbarModel()->input_in_progress() ? NULL : GetWebContents());
1160 Layout();
1161 SchedulePaint();
1164 void LocationBarView::SaveStateToContents(WebContents* contents) {
1165 omnibox_view_->SaveStateToTab(contents);
1168 const OmniboxView* LocationBarView::GetOmniboxView() const {
1169 return omnibox_view_;
1172 LocationBarTesting* LocationBarView::GetLocationBarForTesting() {
1173 return this;
1176 ////////////////////////////////////////////////////////////////////////////////
1177 // LocationBarView, private LocationBarTesting implementation:
1179 int LocationBarView::PageActionCount() {
1180 return page_action_views_.size();
1183 int LocationBarView::PageActionVisibleCount() {
1184 int result = 0;
1185 for (size_t i = 0; i < page_action_views_.size(); i++) {
1186 if (page_action_views_[i]->visible())
1187 ++result;
1189 return result;
1192 ExtensionAction* LocationBarView::GetPageAction(size_t index) {
1193 if (index < page_action_views_.size())
1194 return page_action_views_[index]->image_view()->extension_action();
1196 NOTREACHED();
1197 return NULL;
1200 ExtensionAction* LocationBarView::GetVisiblePageAction(size_t index) {
1201 size_t current = 0;
1202 for (size_t i = 0; i < page_action_views_.size(); ++i) {
1203 if (page_action_views_[i]->visible()) {
1204 if (current == index)
1205 return page_action_views_[i]->image_view()->extension_action();
1207 ++current;
1211 NOTREACHED();
1212 return NULL;
1215 void LocationBarView::TestPageActionPressed(size_t index) {
1216 size_t current = 0;
1217 for (size_t i = 0; i < page_action_views_.size(); ++i) {
1218 if (page_action_views_[i]->visible()) {
1219 if (current == index) {
1220 page_action_views_[i]->image_view()->view_controller()->
1221 ExecuteAction(true);
1222 return;
1224 ++current;
1228 NOTREACHED();
1231 bool LocationBarView::GetBookmarkStarVisibility() {
1232 DCHECK(star_view_);
1233 return star_view_->visible();
1236 ////////////////////////////////////////////////////////////////////////////////
1237 // LocationBarView, private views::View implementation:
1239 const char* LocationBarView::GetClassName() const {
1240 return kViewClassName;
1243 void LocationBarView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
1244 InstantServiceFactory::GetForProfile(profile())->OnOmniboxStartMarginChanged(
1245 bounds().x());
1247 OmniboxPopupView* popup = omnibox_view_->model()->popup_model()->view();
1248 if (popup->IsOpen())
1249 popup->UpdatePopupAppearance();
1252 void LocationBarView::OnFocus() {
1253 // Explicitly focus the omnibox so a focus ring will be displayed around it on
1254 // Windows.
1255 omnibox_view_->SetFocus();
1258 void LocationBarView::OnPaint(gfx::Canvas* canvas) {
1259 View::OnPaint(canvas);
1261 // Fill the location bar background color behind the border. Parts of the
1262 // border images are meant to rest atop the toolbar background and parts atop
1263 // the omnibox background, so we can't just blindly fill our entire bounds.
1264 gfx::Rect bounds(GetContentsBounds());
1265 bounds.Inset(GetHorizontalEdgeThickness(), GetVerticalEdgeThickness());
1266 SkColor color(GetColor(SecurityStateModel::NONE, BACKGROUND));
1267 if (is_popup_mode_) {
1268 canvas->FillRect(bounds, color);
1269 } else {
1270 SkPaint paint;
1271 paint.setStyle(SkPaint::kFill_Style);
1272 paint.setColor(color);
1273 const int kBorderCornerRadius = 2;
1274 canvas->DrawRoundRect(bounds, kBorderCornerRadius, paint);
1277 // The border itself will be drawn in PaintChildren() since it includes an
1278 // inner shadow which should be drawn over the contents.
1281 void LocationBarView::PaintChildren(const ui::PaintContext& context) {
1282 View::PaintChildren(context);
1284 ui::PaintRecorder recorder(context, size());
1286 // For non-InstantExtendedAPI cases, if necessary, show focus rect. As we need
1287 // the focus rect to appear on top of children we paint here rather than
1288 // OnPaint().
1289 // Note: |Canvas::DrawFocusRect| paints a dashed rect with gray color.
1290 if (show_focus_rect_ && HasFocus())
1291 recorder.canvas()->DrawFocusRect(omnibox_view_->bounds());
1293 // Maximized popup windows don't draw the horizontal edges. We implement this
1294 // by simply expanding the paint area outside the view by the edge thickness.
1295 gfx::Rect border_rect(GetContentsBounds());
1296 if (is_popup_mode_ && (GetHorizontalEdgeThickness() == 0))
1297 border_rect.Inset(-kPopupEdgeThickness, 0);
1298 views::Painter::PaintPainterAt(recorder.canvas(), border_painter_.get(),
1299 border_rect);
1302 ////////////////////////////////////////////////////////////////////////////////
1303 // LocationBarView, private views::ButtonListener implementation:
1305 void LocationBarView::ButtonPressed(views::Button* sender,
1306 const ui::Event& event) {
1307 DCHECK_EQ(mic_search_view_, sender);
1308 command_updater()->ExecuteCommand(IDC_TOGGLE_SPEECH_INPUT);
1311 ////////////////////////////////////////////////////////////////////////////////
1312 // LocationBarView, private views::DragController implementation:
1314 void LocationBarView::WriteDragDataForView(views::View* sender,
1315 const gfx::Point& press_pt,
1316 OSExchangeData* data) {
1317 DCHECK_NE(GetDragOperationsForView(sender, press_pt),
1318 ui::DragDropTypes::DRAG_NONE);
1320 WebContents* web_contents = GetWebContents();
1321 favicon::FaviconDriver* favicon_driver =
1322 favicon::ContentFaviconDriver::FromWebContents(web_contents);
1323 gfx::ImageSkia favicon = favicon_driver->GetFavicon().AsImageSkia();
1324 button_drag_utils::SetURLAndDragImage(web_contents->GetURL(),
1325 web_contents->GetTitle(),
1326 favicon,
1327 NULL,
1328 data,
1329 sender->GetWidget());
1332 int LocationBarView::GetDragOperationsForView(views::View* sender,
1333 const gfx::Point& p) {
1334 DCHECK((sender == location_icon_view_) || (sender == ev_bubble_view_));
1335 WebContents* web_contents = delegate_->GetWebContents();
1336 return (web_contents && web_contents->GetURL().is_valid() &&
1337 (!GetOmniboxView()->IsEditingOrEmpty())) ?
1338 (ui::DragDropTypes::DRAG_COPY | ui::DragDropTypes::DRAG_LINK) :
1339 ui::DragDropTypes::DRAG_NONE;
1342 bool LocationBarView::CanStartDragForView(View* sender,
1343 const gfx::Point& press_pt,
1344 const gfx::Point& p) {
1345 return true;
1348 ////////////////////////////////////////////////////////////////////////////////
1349 // LocationBarView, private gfx::AnimationDelegate implementation:
1350 void LocationBarView::AnimationProgressed(const gfx::Animation* animation) {
1351 browser_->window()->ToolbarSizeChanged(true);
1354 void LocationBarView::AnimationEnded(const gfx::Animation* animation) {
1355 browser_->window()->ToolbarSizeChanged(false);
1358 ////////////////////////////////////////////////////////////////////////////////
1359 // LocationBarView, private OmniboxEditController implementation:
1361 void LocationBarView::OnChanged() {
1362 int icon_id = omnibox_view_->GetIcon();
1363 location_icon_view_->SetImage(GetThemeProvider()->GetImageSkiaNamed(icon_id));
1364 location_icon_view_->ShowTooltip(!GetOmniboxView()->IsEditingOrEmpty());
1366 Layout();
1367 SchedulePaint();
1370 void LocationBarView::OnSetFocus() {
1371 GetFocusManager()->SetFocusedView(this);
1374 const ToolbarModel* LocationBarView::GetToolbarModel() const {
1375 return delegate_->GetToolbarModel();
1378 ////////////////////////////////////////////////////////////////////////////////
1379 // LocationBarView, private DropdownBarHostDelegate implementation:
1381 void LocationBarView::SetFocusAndSelection(bool select_all) {
1382 FocusLocation(select_all);
1385 void LocationBarView::SetAnimationOffset(int offset) {
1386 dropdown_animation_offset_ = offset;
1389 ////////////////////////////////////////////////////////////////////////////////
1390 // LocationBarView, private TemplateURLServiceObserver implementation:
1392 void LocationBarView::OnTemplateURLServiceChanged() {
1393 template_url_service_->RemoveObserver(this);
1394 template_url_service_ = NULL;
1395 // If the browser is no longer active, let's not show the info bubble, as this
1396 // would make the browser the active window again.
1397 if (omnibox_view_ && omnibox_view_->GetWidget()->IsActive())
1398 ShowFirstRunBubble();
1401 ////////////////////////////////////////////////////////////////////////////////
1402 // LocationBarView, private SearchModelObserver implementation:
1404 void LocationBarView::ModelChanged(const SearchModel::State& old_state,
1405 const SearchModel::State& new_state) {
1406 const bool visible = !GetToolbarModel()->input_in_progress() &&
1407 new_state.voice_search_supported;
1408 if (mic_search_view_->visible() != visible) {
1409 mic_search_view_->SetVisible(visible);
1410 Layout();