Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / location_bar / location_bar_view.cc
blobb0ef71c4d045b0090f81800a40932018fa3d68c5
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/translate/chrome_translate_client.h"
29 #include "chrome/browser/translate/translate_service.h"
30 #include "chrome/browser/ui/browser.h"
31 #include "chrome/browser/ui/browser_finder.h"
32 #include "chrome/browser/ui/browser_instant_controller.h"
33 #include "chrome/browser/ui/browser_window.h"
34 #include "chrome/browser/ui/content_settings/content_setting_bubble_model.h"
35 #include "chrome/browser/ui/passwords/manage_passwords_icon.h"
36 #include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h"
37 #include "chrome/browser/ui/tabs/tab_strip_model.h"
38 #include "chrome/browser/ui/view_ids.h"
39 #include "chrome/browser/ui/views/browser_dialogs.h"
40 #include "chrome/browser/ui/views/layout_constants.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 = GetLayoutConstant(OMNIBOX_FONT_PIXEL_SIZE);
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_vertical_padding =
219 (GetLayoutConstant(LOCATION_BAR_BUBBLE_VERTICAL_PADDING) +
220 kBubbleInteriorVerticalPadding) * 2;
221 const gfx::FontList bubble_font_list(font_list.DeriveWithHeightUpperBound(
222 location_height - bubble_vertical_padding));
224 const SkColor background_color =
225 GetColor(SecurityStateModel::NONE, LocationBarView::BACKGROUND);
226 ev_bubble_view_ = new EVBubbleView(
227 bubble_font_list, GetColor(SecurityStateModel::EV_SECURE, SECURITY_TEXT),
228 background_color, this);
229 ev_bubble_view_->set_drag_controller(this);
230 AddChildView(ev_bubble_view_);
232 // Initialize the Omnibox view.
233 omnibox_view_ = new OmniboxViewViews(
234 this, profile(), command_updater(), is_popup_mode_, this, font_list);
235 omnibox_view_->Init();
236 omnibox_view_->SetFocusable(true);
237 AddChildView(omnibox_view_);
239 // Initialize the inline autocomplete view which is visible only when IME is
240 // turned on. Use the same font with the omnibox and highlighted background.
241 ime_inline_autocomplete_view_ = new views::Label(base::string16(), font_list);
242 ime_inline_autocomplete_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
243 ime_inline_autocomplete_view_->SetAutoColorReadabilityEnabled(false);
244 ime_inline_autocomplete_view_->set_background(
245 views::Background::CreateSolidBackground(GetNativeTheme()->GetSystemColor(
246 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused)));
247 ime_inline_autocomplete_view_->SetEnabledColor(
248 GetNativeTheme()->GetSystemColor(
249 ui::NativeTheme::kColorId_TextfieldSelectionColor));
250 ime_inline_autocomplete_view_->SetVisible(false);
251 AddChildView(ime_inline_autocomplete_view_);
253 const SkColor text_color = GetColor(SecurityStateModel::NONE, TEXT);
254 selected_keyword_view_ = new SelectedKeywordView(
255 bubble_font_list, text_color, background_color, profile());
256 AddChildView(selected_keyword_view_);
258 suggested_text_view_ = new views::Label(base::string16(), font_list);
259 suggested_text_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
260 suggested_text_view_->SetAutoColorReadabilityEnabled(false);
261 suggested_text_view_->SetEnabledColor(
262 GetColor(SecurityStateModel::NONE, LocationBarView::DEEMPHASIZED_TEXT));
263 suggested_text_view_->SetVisible(false);
264 AddChildView(suggested_text_view_);
266 keyword_hint_view_ = new KeywordHintView(
267 profile(), font_list,
268 GetColor(SecurityStateModel::NONE, LocationBarView::DEEMPHASIZED_TEXT),
269 background_color);
270 AddChildView(keyword_hint_view_);
272 mic_search_view_ = new views::ImageButton(this);
273 mic_search_view_->set_id(VIEW_ID_MIC_SEARCH_BUTTON);
274 mic_search_view_->SetAccessibilityFocusable(true);
275 mic_search_view_->SetTooltipText(
276 l10n_util::GetStringUTF16(IDS_TOOLTIP_MIC_SEARCH));
277 mic_search_view_->SetImage(
278 views::Button::STATE_NORMAL,
279 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
280 IDR_OMNIBOX_MIC_SEARCH));
281 mic_search_view_->SetImageAlignment(views::ImageButton::ALIGN_CENTER,
282 views::ImageButton::ALIGN_MIDDLE);
283 mic_search_view_->SetVisible(false);
284 AddChildView(mic_search_view_);
286 for (ContentSettingsType type :
287 ContentSettingBubbleModel::GetSupportedBubbleTypes()) {
288 ContentSettingImageView* content_blocked_view = new ContentSettingImageView(
289 type, this, bubble_font_list, text_color, background_color);
290 content_setting_views_.push_back(content_blocked_view);
291 content_blocked_view->SetVisible(false);
292 AddChildView(content_blocked_view);
295 zoom_view_ = new ZoomView(delegate_);
296 AddChildView(zoom_view_);
298 open_pdf_in_reader_view_ = new OpenPDFInReaderView();
299 AddChildView(open_pdf_in_reader_view_);
301 manage_passwords_icon_view_ = new ManagePasswordsIconView(command_updater());
302 AddChildView(manage_passwords_icon_view_);
304 translate_icon_view_ = new TranslateIconView(command_updater());
305 translate_icon_view_->SetVisible(false);
306 AddChildView(translate_icon_view_);
308 star_view_ = new StarView(command_updater(), browser_);
309 star_view_->SetVisible(false);
310 AddChildView(star_view_);
312 // Initialize the location entry. We do this to avoid a black flash which is
313 // visible when the location entry has just been initialized.
314 Update(NULL);
316 size_animation_.Reset(1);
319 bool LocationBarView::IsInitialized() const {
320 return omnibox_view_ != NULL;
323 SkColor LocationBarView::GetColor(
324 SecurityStateModel::SecurityLevel security_level,
325 ColorKind kind) const {
326 const ui::NativeTheme* native_theme = GetNativeTheme();
327 switch (kind) {
328 case BACKGROUND:
329 return native_theme->GetSystemColor(
330 ui::NativeTheme::kColorId_TextfieldDefaultBackground);
332 case TEXT:
333 return native_theme->GetSystemColor(
334 ui::NativeTheme::kColorId_TextfieldDefaultColor);
336 case SELECTED_TEXT:
337 return native_theme->GetSystemColor(
338 ui::NativeTheme::kColorId_TextfieldSelectionColor);
340 case DEEMPHASIZED_TEXT:
341 return color_utils::AlphaBlend(
342 GetColor(security_level, TEXT),
343 GetColor(security_level, BACKGROUND),
344 128);
346 case SECURITY_TEXT: {
347 SkColor color;
348 switch (security_level) {
349 case SecurityStateModel::EV_SECURE:
350 case SecurityStateModel::SECURE:
351 color = SkColorSetRGB(7, 149, 0);
352 break;
354 case SecurityStateModel::SECURITY_POLICY_WARNING:
355 return GetColor(security_level, DEEMPHASIZED_TEXT);
356 break;
358 case SecurityStateModel::SECURITY_ERROR:
359 color = SkColorSetRGB(162, 0, 0);
360 break;
362 case SecurityStateModel::SECURITY_WARNING:
363 return GetColor(security_level, TEXT);
364 break;
366 default:
367 NOTREACHED();
368 return GetColor(security_level, TEXT);
370 return color_utils::GetReadableColor(
371 color, GetColor(security_level, BACKGROUND));
374 default:
375 NOTREACHED();
376 return GetColor(security_level, TEXT);
380 void LocationBarView::ZoomChangedForActiveTab(bool can_show_bubble) {
381 DCHECK(zoom_view_);
382 if (RefreshZoomView()) {
383 Layout();
384 SchedulePaint();
387 WebContents* web_contents = GetWebContents();
388 if (can_show_bubble && zoom_view_->visible() && web_contents)
389 ZoomBubbleView::ShowBubble(web_contents, true);
392 void LocationBarView::SetPreviewEnabledPageAction(ExtensionAction* page_action,
393 bool preview_enabled) {
394 if (is_popup_mode_)
395 return;
397 DCHECK(page_action);
398 WebContents* web_contents = GetWebContents();
400 RefreshPageActionViews();
401 PageActionWithBadgeView* page_action_view =
402 static_cast<PageActionWithBadgeView*>(GetPageActionView(page_action));
403 DCHECK(page_action_view);
404 if (!page_action_view)
405 return;
407 page_action_view->image_view()->set_preview_enabled(preview_enabled);
408 page_action_view->UpdateVisibility(web_contents);
409 Layout();
410 SchedulePaint();
413 PageActionWithBadgeView* LocationBarView::GetPageActionView(
414 ExtensionAction* page_action) {
415 DCHECK(page_action);
416 for (PageActionViews::const_iterator i(page_action_views_.begin());
417 i != page_action_views_.end(); ++i) {
418 if ((*i)->image_view()->extension_action() == page_action)
419 return *i;
421 return NULL;
424 void LocationBarView::SetStarToggled(bool on) {
425 if (star_view_)
426 star_view_->SetToggled(on);
429 void LocationBarView::SetTranslateIconToggled(bool on) {
430 translate_icon_view_->SetToggled(on);
433 gfx::Point LocationBarView::GetOmniboxViewOrigin() const {
434 gfx::Point origin(omnibox_view_->bounds().origin());
435 origin.set_x(GetMirroredXInView(origin.x()));
436 views::View::ConvertPointToScreen(this, &origin);
437 return origin;
440 void LocationBarView::SetImeInlineAutocompletion(const base::string16& text) {
441 ime_inline_autocomplete_view_->SetText(text);
442 ime_inline_autocomplete_view_->SetVisible(!text.empty());
445 void LocationBarView::SetGrayTextAutocompletion(const base::string16& text) {
446 if (suggested_text_view_->text() != text) {
447 suggested_text_view_->SetText(text);
448 suggested_text_view_->SetVisible(!text.empty());
449 Layout();
450 SchedulePaint();
454 base::string16 LocationBarView::GetGrayTextAutocompletion() const {
455 return HasValidSuggestText() ?
456 suggested_text_view_->text() : base::string16();
459 void LocationBarView::SetShowFocusRect(bool show) {
460 show_focus_rect_ = show;
461 SchedulePaint();
464 void LocationBarView::SelectAll() {
465 omnibox_view_->SelectAll(true);
468 gfx::Point LocationBarView::GetLocationBarAnchorPoint() const {
469 // The +1 in the next line creates a 1-px gap between icon and arrow tip.
470 const int icon_padding = GetLayoutConstant(ICON_LABEL_VIEW_TRAILING_PADDING);
471 gfx::Point icon_bottom(
472 0, location_icon_view_->GetImageBounds().bottom() - icon_padding + 1);
473 gfx::Point icon_center(location_icon_view_->GetImageBounds().CenterPoint());
474 gfx::Point point(icon_center.x(), icon_bottom.y());
475 ConvertPointToTarget(location_icon_view_, this, &point);
476 return point;
479 int LocationBarView::GetInternalHeight(bool use_preferred_size) {
480 int total_height =
481 use_preferred_size ? GetPreferredSize().height() : height();
482 return std::max(total_height - (VerticalPadding() * 2), 0);
485 void LocationBarView::GetOmniboxPopupPositioningInfo(
486 gfx::Point* top_left_screen_coord,
487 int* popup_width,
488 int* left_margin,
489 int* right_margin,
490 int top_edge_overlap) {
491 *top_left_screen_coord = gfx::Point(0, parent()->height() - top_edge_overlap);
492 views::View::ConvertPointToScreen(parent(), top_left_screen_coord);
494 *popup_width = parent()->width();
495 gfx::Rect location_bar_bounds(bounds());
496 location_bar_bounds.Inset(kNormalEdgeThickness, 0);
497 *left_margin = location_bar_bounds.x();
498 *right_margin = *popup_width - location_bar_bounds.right();
501 ////////////////////////////////////////////////////////////////////////////////
502 // LocationBarView, public LocationBar implementation:
504 void LocationBarView::FocusLocation(bool select_all) {
505 omnibox_view_->SetFocus();
506 if (select_all)
507 omnibox_view_->SelectAll(true);
510 void LocationBarView::Revert() {
511 omnibox_view_->RevertAll();
514 OmniboxView* LocationBarView::GetOmniboxView() {
515 return omnibox_view_;
518 ////////////////////////////////////////////////////////////////////////////////
519 // LocationBarView, public views::View implementation:
521 bool LocationBarView::HasFocus() const {
522 return omnibox_view_->model()->has_focus();
525 void LocationBarView::GetAccessibleState(ui::AXViewState* state) {
526 state->role = ui::AX_ROLE_GROUP;
529 gfx::Size LocationBarView::GetPreferredSize() const {
530 // Compute minimum height.
531 gfx::Size min_size(border_painter_->GetMinimumSize());
532 // For non-material the size of the asset determines the size of the
533 // LocationBarView.
534 if (ui::MaterialDesignController::IsModeMaterial())
535 min_size.set_height(GetLayoutConstant(LOCATION_BAR_HEIGHT));
537 if (!IsInitialized())
538 return min_size;
540 min_size.set_height(min_size.height() * size_animation_.GetCurrentValue());
542 const int padding = GetLayoutConstant(LOCATION_BAR_HORIZONTAL_PADDING);
544 // Compute width of omnibox-leading content.
545 const int horizontal_edge_thickness = GetHorizontalEdgeThickness();
546 int leading_width = horizontal_edge_thickness;
547 if (ShouldShowKeywordBubble()) {
548 // The selected keyword view can collapse completely.
549 } else if (ShouldShowEVBubble()) {
550 leading_width += GetLayoutConstant(LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING) +
551 ev_bubble_view_->GetMinimumSizeForLabelText(
552 GetToolbarModel()->GetEVCertName()).width();
553 } else {
554 leading_width += padding + location_icon_view_->GetMinimumSize().width();
557 // Compute width of omnibox-trailing content.
558 int trailing_width = horizontal_edge_thickness;
559 trailing_width += IncrementalMinimumWidth(star_view_) +
560 IncrementalMinimumWidth(translate_icon_view_) +
561 IncrementalMinimumWidth(open_pdf_in_reader_view_) +
562 IncrementalMinimumWidth(manage_passwords_icon_view_) +
563 IncrementalMinimumWidth(zoom_view_) +
564 IncrementalMinimumWidth(mic_search_view_);
565 for (PageActionViews::const_iterator i(page_action_views_.begin());
566 i != page_action_views_.end(); ++i)
567 trailing_width += IncrementalMinimumWidth((*i));
568 for (ContentSettingViews::const_iterator i(content_setting_views_.begin());
569 i != content_setting_views_.end(); ++i)
570 trailing_width += IncrementalMinimumWidth((*i));
572 min_size.set_width(leading_width + omnibox_view_->GetMinimumSize().width() +
573 2 * padding - omnibox_view_->GetInsets().width() +
574 trailing_width);
575 return min_size;
578 void LocationBarView::Layout() {
579 if (!IsInitialized())
580 return;
582 selected_keyword_view_->SetVisible(false);
583 location_icon_view_->SetVisible(false);
584 ev_bubble_view_->SetVisible(false);
585 keyword_hint_view_->SetVisible(false);
587 const int item_padding = GetLayoutConstant(LOCATION_BAR_HORIZONTAL_PADDING);
588 int trailing_edge_item_padding = 0;
589 if (!ui::MaterialDesignController::IsModeMaterial()) {
590 trailing_edge_item_padding = item_padding - GetHorizontalEdgeThickness() -
591 omnibox_view_->GetInsets().right();
594 LocationBarLayout leading_decorations(
595 LocationBarLayout::LEFT_EDGE, item_padding,
596 item_padding - omnibox_view_->GetInsets().left() -
597 GetEditLeadingInternalSpace());
598 LocationBarLayout trailing_decorations(
599 LocationBarLayout::RIGHT_EDGE, item_padding, trailing_edge_item_padding);
601 const base::string16 keyword(omnibox_view_->model()->keyword());
602 // In some cases (e.g. fullscreen mode) we may have 0 height. We still want
603 // to position our child views in this case, because other things may be
604 // positioned relative to them (e.g. the "bookmark added" bubble if the user
605 // hits ctrl-d).
606 const int bubble_vertical_padding =
607 GetVerticalEdgeThickness() +
608 GetLayoutConstant(LOCATION_BAR_BUBBLE_VERTICAL_PADDING);
609 const int bubble_height =
610 std::max(height() - (bubble_vertical_padding * 2), 0);
611 const int bubble_horizontal_padding =
612 GetLayoutConstant(LOCATION_BAR_BUBBLE_HORIZONTAL_PADDING);
613 const int location_height = GetInternalHeight(false);
614 const int vertical_padding = VerticalPadding();
615 if (ShouldShowKeywordBubble()) {
616 leading_decorations.AddDecoration(bubble_vertical_padding, bubble_height,
617 true, 0, bubble_horizontal_padding,
618 item_padding, selected_keyword_view_);
619 if (selected_keyword_view_->keyword() != keyword) {
620 selected_keyword_view_->SetKeyword(keyword);
621 const TemplateURL* template_url =
622 TemplateURLServiceFactory::GetForProfile(profile())->
623 GetTemplateURLForKeyword(keyword);
624 if (template_url &&
625 (template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION)) {
626 gfx::Image image = extensions::OmniboxAPI::Get(profile())->
627 GetOmniboxIcon(template_url->GetExtensionId());
628 selected_keyword_view_->SetImage(image.AsImageSkia());
629 selected_keyword_view_->set_is_extension_icon(true);
630 } else {
631 selected_keyword_view_->SetImage(
632 *(GetThemeProvider()->GetImageSkiaNamed(IDR_OMNIBOX_SEARCH)));
633 selected_keyword_view_->set_is_extension_icon(false);
636 } else if (ShouldShowEVBubble()) {
637 ev_bubble_view_->SetLabel(GetToolbarModel()->GetEVCertName());
638 // The largest fraction of the omnibox that can be taken by the EV bubble.
639 const double kMaxBubbleFraction = 0.5;
640 leading_decorations.AddDecoration(
641 bubble_vertical_padding, bubble_height, false, kMaxBubbleFraction,
642 bubble_horizontal_padding, item_padding, ev_bubble_view_);
643 } else {
644 leading_decorations.AddDecoration(vertical_padding, location_height,
645 location_icon_view_);
648 if (star_view_->visible()) {
649 trailing_decorations.AddDecoration(vertical_padding, location_height,
650 star_view_);
652 if (translate_icon_view_->visible()) {
653 trailing_decorations.AddDecoration(vertical_padding, location_height,
654 translate_icon_view_);
656 if (open_pdf_in_reader_view_->visible()) {
657 trailing_decorations.AddDecoration(vertical_padding, location_height,
658 open_pdf_in_reader_view_);
660 if (manage_passwords_icon_view_->visible()) {
661 trailing_decorations.AddDecoration(vertical_padding, location_height,
662 manage_passwords_icon_view_);
664 for (PageActionViews::const_iterator i(page_action_views_.begin());
665 i != page_action_views_.end(); ++i) {
666 if ((*i)->visible()) {
667 trailing_decorations.AddDecoration(vertical_padding, location_height,
668 (*i));
671 if (zoom_view_->visible()) {
672 trailing_decorations.AddDecoration(vertical_padding, location_height,
673 zoom_view_);
675 for (ContentSettingViews::const_reverse_iterator i(
676 content_setting_views_.rbegin()); i != content_setting_views_.rend();
677 ++i) {
678 if ((*i)->visible()) {
679 trailing_decorations.AddDecoration(bubble_vertical_padding, bubble_height,
680 false, 0, item_padding, item_padding,
681 *i);
684 if (mic_search_view_->visible()) {
685 trailing_decorations.AddDecoration(vertical_padding, location_height,
686 mic_search_view_);
688 // Because IMEs may eat the tab key, we don't show "press tab to search" while
689 // IME composition is in progress.
690 if (!keyword.empty() && omnibox_view_->model()->is_keyword_hint() &&
691 !omnibox_view_->IsImeComposing()) {
692 trailing_decorations.AddDecoration(vertical_padding, location_height, true,
693 0, item_padding, item_padding,
694 keyword_hint_view_);
695 if (keyword_hint_view_->keyword() != keyword)
696 keyword_hint_view_->SetKeyword(keyword);
699 // Perform layout.
700 const int horizontal_edge_thickness = GetHorizontalEdgeThickness();
701 int full_width = width() - (2 * horizontal_edge_thickness);
703 int entry_width = full_width;
704 leading_decorations.LayoutPass1(&entry_width);
705 trailing_decorations.LayoutPass1(&entry_width);
706 leading_decorations.LayoutPass2(&entry_width);
707 trailing_decorations.LayoutPass2(&entry_width);
709 int location_needed_width = omnibox_view_->GetTextWidth();
710 int available_width = entry_width - location_needed_width;
711 // The bounds must be wide enough for all the decorations to fit.
712 gfx::Rect location_bounds(horizontal_edge_thickness, vertical_padding,
713 std::max(full_width, full_width - entry_width),
714 location_height);
715 leading_decorations.LayoutPass3(&location_bounds, &available_width);
716 trailing_decorations.LayoutPass3(&location_bounds, &available_width);
718 // Layout out the suggested text view right aligned to the location
719 // entry. Only show the suggested text if we can fit the text from one
720 // character before the end of the selection to the end of the text and the
721 // suggested text. If we can't it means either the suggested text is too big,
722 // or the user has scrolled.
724 // TODO(sky): We could potentially adjust this to take into account suggested
725 // text to force using minimum size if necessary, but currently the chance of
726 // showing keyword hints and suggested text is minimal and we're not confident
727 // this is the right approach for suggested text.
729 int omnibox_view_margin = 0;
730 if (suggested_text_view_->visible()) {
731 // We do not display the suggested text when it contains a mix of RTL and
732 // LTR characters since this could mean the suggestion should be displayed
733 // in the middle of the string.
734 base::i18n::TextDirection text_direction =
735 base::i18n::GetStringDirection(omnibox_view_->GetText());
736 if (text_direction !=
737 base::i18n::GetStringDirection(suggested_text_view_->text()))
738 text_direction = base::i18n::UNKNOWN_DIRECTION;
740 // TODO(sky): need to layout when the user changes caret position.
741 gfx::Size suggested_text_size(suggested_text_view_->GetPreferredSize());
742 if (suggested_text_size.width() > available_width ||
743 text_direction == base::i18n::UNKNOWN_DIRECTION) {
744 // Hide the suggested text if the user has scrolled or we can't fit all
745 // the suggested text, or we have a mix of RTL and LTR characters.
746 suggested_text_view_->SetBounds(0, 0, 0, 0);
747 } else {
748 location_needed_width =
749 std::min(location_needed_width,
750 location_bounds.width() - suggested_text_size.width());
751 gfx::Rect suggested_text_bounds(location_bounds.x(), location_bounds.y(),
752 suggested_text_size.width(),
753 location_bounds.height());
754 // TODO(sky): figure out why this needs the -1.
755 suggested_text_bounds.Offset(location_needed_width - 1, 0);
757 // We reverse the order of the location entry and suggested text if:
758 // - Chrome is RTL but the text is fully LTR, or
759 // - Chrome is LTR but the text is fully RTL.
760 // This ensures the suggested text is correctly displayed to the right
761 // (or left) of the user text.
762 if (text_direction == (base::i18n::IsRTL() ?
763 base::i18n::LEFT_TO_RIGHT : base::i18n::RIGHT_TO_LEFT)) {
764 // TODO(sky): Figure out why we need the +1.
765 suggested_text_bounds.set_x(location_bounds.x() + 1);
766 // Use a margin to prevent omnibox text from overlapping suggest text.
767 omnibox_view_margin = suggested_text_bounds.width();
769 suggested_text_view_->SetBoundsRect(suggested_text_bounds);
773 omnibox_view_->SetBorder(
774 views::Border::CreateEmptyBorder(0, 0, 0, omnibox_view_margin));
776 // Layout |ime_inline_autocomplete_view_| next to the user input.
777 if (ime_inline_autocomplete_view_->visible()) {
778 int width =
779 gfx::GetStringWidth(ime_inline_autocomplete_view_->text(),
780 ime_inline_autocomplete_view_->font_list()) +
781 ime_inline_autocomplete_view_->GetInsets().width();
782 // All the target languages (IMEs) are LTR, and we do not need to support
783 // RTL so far. In other words, no testable RTL environment so far.
784 int x = location_needed_width;
785 if (width > entry_width)
786 x = 0;
787 else if (location_needed_width + width > entry_width)
788 x = entry_width - width;
789 location_bounds.set_width(x);
790 ime_inline_autocomplete_view_->SetBounds(
791 location_bounds.right(), location_bounds.y(),
792 std::min(width, entry_width), location_bounds.height());
794 omnibox_view_->SetBoundsRect(location_bounds);
797 void LocationBarView::Update(const WebContents* contents) {
798 mic_search_view_->SetVisible(
799 !GetToolbarModel()->input_in_progress() && browser_ &&
800 browser_->search_model()->voice_search_supported());
801 RefreshContentSettingViews();
802 RefreshZoomView();
803 RefreshPageActionViews();
804 RefreshTranslateIcon();
805 RefreshManagePasswordsIconView();
806 content::WebContents* web_contents_for_sub_views =
807 GetToolbarModel()->input_in_progress() ? NULL : GetWebContents();
808 open_pdf_in_reader_view_->Update(web_contents_for_sub_views);
810 if (star_view_)
811 UpdateBookmarkStarVisibility();
813 if (contents)
814 omnibox_view_->OnTabChanged(contents);
815 else
816 omnibox_view_->Update();
818 OnChanged(); // NOTE: Calls Layout().
821 void LocationBarView::ResetTabState(WebContents* contents) {
822 omnibox_view_->ResetTabState(contents);
825 ////////////////////////////////////////////////////////////////////////////////
826 // LocationBarView, public OmniboxEditController implementation:
828 void LocationBarView::UpdateWithoutTabRestore() {
829 Update(nullptr);
832 void LocationBarView::ShowURL() {
833 omnibox_view_->ShowURL();
836 ToolbarModel* LocationBarView::GetToolbarModel() {
837 return delegate_->GetToolbarModel();
840 WebContents* LocationBarView::GetWebContents() {
841 return delegate_->GetWebContents();
844 ////////////////////////////////////////////////////////////////////////////////
845 // LocationBarView, private:
847 int LocationBarView::IncrementalMinimumWidth(views::View* view) const {
848 return view->visible() ?
849 (GetLayoutConstant(LOCATION_BAR_HORIZONTAL_PADDING) +
850 view->GetMinimumSize().width()) : 0;
853 int LocationBarView::GetHorizontalEdgeThickness() const {
854 // In maximized popup mode, there isn't any edge.
855 return (is_popup_mode_ && browser_ && browser_->window() &&
856 browser_->window()->IsMaximized()) ?
857 0 : GetVerticalEdgeThickness();
860 int LocationBarView::GetVerticalEdgeThickness() const {
861 return is_popup_mode_ ? kPopupEdgeThickness : kNormalEdgeThickness;
864 int LocationBarView::VerticalPadding() const {
865 return is_popup_mode_ ?
866 kPopupEdgeThickness : GetLayoutConstant(LOCATION_BAR_VERTICAL_PADDING);
869 bool LocationBarView::RefreshContentSettingViews() {
870 bool visibility_changed = false;
871 for (ContentSettingViews::const_iterator i(content_setting_views_.begin());
872 i != content_setting_views_.end(); ++i) {
873 const bool was_visible = (*i)->visible();
874 (*i)->Update(GetToolbarModel()->input_in_progress() ?
875 NULL : GetWebContents());
876 if (was_visible != (*i)->visible())
877 visibility_changed = true;
879 return visibility_changed;
882 void LocationBarView::DeletePageActionViews() {
883 for (PageActionViews::const_iterator i(page_action_views_.begin());
884 i != page_action_views_.end(); ++i)
885 RemoveChildView(*i);
886 STLDeleteElements(&page_action_views_);
889 bool LocationBarView::RefreshPageActionViews() {
890 if (is_popup_mode_)
891 return false;
893 bool changed = false;
894 PageActions new_page_actions;
896 WebContents* web_contents = GetWebContents();
897 if (web_contents) {
898 extensions::TabHelper* extensions_tab_helper =
899 extensions::TabHelper::FromWebContents(web_contents);
900 extensions::LocationBarController* controller =
901 extensions_tab_helper->location_bar_controller();
902 new_page_actions = controller->GetCurrentActions();
904 web_contents_null_at_last_refresh_ = web_contents == NULL;
906 // On startup we sometimes haven't loaded any extensions. This makes sure
907 // we catch up when the extensions (and any page actions) load.
908 if (PageActionsDiffer(new_page_actions)) {
909 changed = true;
911 DeletePageActionViews();
913 // Create the page action views.
914 for (PageActions::const_iterator i = new_page_actions.begin();
915 i != new_page_actions.end(); ++i) {
916 PageActionWithBadgeView* page_action_view = new PageActionWithBadgeView(
917 delegate_->CreatePageActionImageView(this, *i));
918 page_action_view->SetVisible(false);
919 page_action_views_.push_back(page_action_view);
922 View* right_anchor = open_pdf_in_reader_view_;
923 if (!right_anchor)
924 right_anchor = star_view_;
925 DCHECK(right_anchor);
927 // |page_action_views_| are ordered right-to-left. Add them as children in
928 // reverse order so the logical order and visual order match for
929 // accessibility purposes.
930 for (PageActionViews::reverse_iterator i = page_action_views_.rbegin();
931 i != page_action_views_.rend(); ++i)
932 AddChildViewAt(*i, GetIndexOf(right_anchor));
935 for (PageActionViews::const_iterator i(page_action_views_.begin());
936 i != page_action_views_.end(); ++i) {
937 bool old_visibility = (*i)->visible();
938 (*i)->UpdateVisibility(
939 GetToolbarModel()->input_in_progress() ? NULL : web_contents);
940 changed |= old_visibility != (*i)->visible();
942 return changed;
945 bool LocationBarView::PageActionsDiffer(
946 const PageActions& page_actions) const {
947 if (page_action_views_.size() != page_actions.size())
948 return true;
950 for (size_t index = 0; index < page_actions.size(); ++index) {
951 PageActionWithBadgeView* view = page_action_views_[index];
952 if (view->image_view()->extension_action() != page_actions[index])
953 return true;
956 return false;
959 bool LocationBarView::RefreshZoomView() {
960 DCHECK(zoom_view_);
961 WebContents* web_contents = GetWebContents();
962 if (!web_contents)
963 return false;
964 const bool was_visible = zoom_view_->visible();
965 zoom_view_->Update(ui_zoom::ZoomController::FromWebContents(web_contents));
966 if (!zoom_view_->visible())
967 ZoomBubbleView::CloseBubble();
968 return was_visible != zoom_view_->visible();
971 void LocationBarView::OnDefaultZoomLevelChanged() {
972 RefreshZoomView();
975 void LocationBarView::RefreshTranslateIcon() {
976 if (!TranslateService::IsTranslateBubbleEnabled())
977 return;
979 WebContents* web_contents = GetWebContents();
980 if (!web_contents)
981 return;
982 translate::LanguageState& language_state =
983 ChromeTranslateClient::FromWebContents(web_contents)->GetLanguageState();
984 bool enabled = language_state.translate_enabled();
985 command_updater()->UpdateCommandEnabled(IDC_TRANSLATE_PAGE, enabled);
986 translate_icon_view_->SetVisible(enabled);
987 translate_icon_view_->SetToggled(language_state.IsPageTranslated());
988 if (!enabled)
989 TranslateBubbleView::CloseBubble();
992 bool LocationBarView::RefreshManagePasswordsIconView() {
993 DCHECK(manage_passwords_icon_view_);
994 WebContents* web_contents = GetWebContents();
995 if (!web_contents)
996 return false;
997 const bool was_visible = manage_passwords_icon_view_->visible();
998 ManagePasswordsUIController::FromWebContents(
999 web_contents)->UpdateIconAndBubbleState(manage_passwords_icon_view_);
1000 return was_visible != manage_passwords_icon_view_->visible();
1003 void LocationBarView::ShowFirstRunBubbleInternal() {
1004 // First run bubble doesn't make sense for Chrome OS.
1005 #if !defined(OS_CHROMEOS)
1006 WebContents* web_contents = delegate_->GetWebContents();
1007 if (!web_contents)
1008 return;
1009 Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
1010 if (browser)
1011 FirstRunBubble::ShowBubble(browser, location_icon_view_);
1012 #endif
1015 bool LocationBarView::HasValidSuggestText() const {
1016 return suggested_text_view_->visible() &&
1017 !suggested_text_view_->size().IsEmpty();
1020 bool LocationBarView::ShouldShowKeywordBubble() const {
1021 return !omnibox_view_->model()->keyword().empty() &&
1022 !omnibox_view_->model()->is_keyword_hint();
1025 bool LocationBarView::ShouldShowEVBubble() const {
1026 const ChromeToolbarModel* chrome_toolbar_model =
1027 static_cast<const ChromeToolbarModel*>(GetToolbarModel());
1028 return (chrome_toolbar_model->GetSecurityLevel(false) ==
1029 SecurityStateModel::EV_SECURE);
1032 ////////////////////////////////////////////////////////////////////////////////
1033 // LocationBarView, private LocationBar implementation:
1035 void LocationBarView::ShowFirstRunBubble() {
1036 // Wait until search engines have loaded to show the first run bubble.
1037 TemplateURLService* url_service =
1038 TemplateURLServiceFactory::GetForProfile(profile());
1039 if (!url_service->loaded()) {
1040 template_url_service_ = url_service;
1041 template_url_service_->AddObserver(this);
1042 template_url_service_->Load();
1043 return;
1045 ShowFirstRunBubbleInternal();
1048 GURL LocationBarView::GetDestinationURL() const {
1049 return destination_url();
1052 WindowOpenDisposition LocationBarView::GetWindowOpenDisposition() const {
1053 return disposition();
1056 ui::PageTransition LocationBarView::GetPageTransition() const {
1057 return transition();
1060 void LocationBarView::AcceptInput() {
1061 omnibox_view_->model()->AcceptInput(CURRENT_TAB, false);
1064 void LocationBarView::FocusSearch() {
1065 omnibox_view_->SetFocus();
1066 omnibox_view_->SetForcedQuery();
1069 void LocationBarView::UpdateContentSettingsIcons() {
1070 if (RefreshContentSettingViews()) {
1071 Layout();
1072 SchedulePaint();
1076 void LocationBarView::UpdateManagePasswordsIconAndBubble() {
1077 if (RefreshManagePasswordsIconView()) {
1078 Layout();
1079 SchedulePaint();
1083 void LocationBarView::UpdatePageActions() {
1084 if (RefreshPageActionViews()) { // Changed.
1085 Layout();
1086 SchedulePaint();
1090 void LocationBarView::UpdateBookmarkStarVisibility() {
1091 if (star_view_) {
1092 star_view_->SetVisible(
1093 browser_defaults::bookmarks_enabled && !is_popup_mode_ &&
1094 !GetToolbarModel()->input_in_progress() &&
1095 edit_bookmarks_enabled_.GetValue() &&
1096 !IsBookmarkStarHiddenByExtension());
1100 void LocationBarView::UpdateLocationBarVisibility(bool visible, bool animate) {
1101 if (!animate) {
1102 size_animation_.Reset(visible ? 1 : 0);
1103 return;
1106 if (visible)
1107 size_animation_.Show();
1108 else
1109 size_animation_.Hide();
1112 bool LocationBarView::ShowPageActionPopup(
1113 const extensions::Extension* extension,
1114 bool grant_tab_permissions) {
1115 ExtensionAction* extension_action =
1116 extensions::ExtensionActionManager::Get(profile())->GetPageAction(
1117 *extension);
1118 CHECK(extension_action);
1119 PageActionWithBadgeView* page_action_view =
1120 GetPageActionView(extension_action);
1121 if (!page_action_view) {
1122 CHECK(!web_contents_null_at_last_refresh_);
1123 CHECK(!is_popup_mode_);
1124 CHECK(!extensions::FeatureSwitch::extension_action_redesign()->IsEnabled());
1125 CHECK(false);
1127 PageActionImageView* page_action_image_view = page_action_view->image_view();
1128 CHECK(page_action_image_view);
1129 ExtensionActionViewController* extension_action_view_controller =
1130 page_action_image_view->view_controller();
1131 CHECK(extension_action_view_controller);
1132 return extension_action_view_controller->ExecuteAction(grant_tab_permissions);
1135 void LocationBarView::UpdateOpenPDFInReaderPrompt() {
1136 open_pdf_in_reader_view_->Update(
1137 GetToolbarModel()->input_in_progress() ? NULL : GetWebContents());
1138 Layout();
1139 SchedulePaint();
1142 void LocationBarView::SaveStateToContents(WebContents* contents) {
1143 omnibox_view_->SaveStateToTab(contents);
1146 const OmniboxView* LocationBarView::GetOmniboxView() const {
1147 return omnibox_view_;
1150 LocationBarTesting* LocationBarView::GetLocationBarForTesting() {
1151 return this;
1154 ////////////////////////////////////////////////////////////////////////////////
1155 // LocationBarView, private LocationBarTesting implementation:
1157 int LocationBarView::PageActionCount() {
1158 return page_action_views_.size();
1161 int LocationBarView::PageActionVisibleCount() {
1162 int result = 0;
1163 for (size_t i = 0; i < page_action_views_.size(); i++) {
1164 if (page_action_views_[i]->visible())
1165 ++result;
1167 return result;
1170 ExtensionAction* LocationBarView::GetPageAction(size_t index) {
1171 if (index < page_action_views_.size())
1172 return page_action_views_[index]->image_view()->extension_action();
1174 NOTREACHED();
1175 return NULL;
1178 ExtensionAction* LocationBarView::GetVisiblePageAction(size_t index) {
1179 size_t current = 0;
1180 for (size_t i = 0; i < page_action_views_.size(); ++i) {
1181 if (page_action_views_[i]->visible()) {
1182 if (current == index)
1183 return page_action_views_[i]->image_view()->extension_action();
1185 ++current;
1189 NOTREACHED();
1190 return NULL;
1193 void LocationBarView::TestPageActionPressed(size_t index) {
1194 size_t current = 0;
1195 for (size_t i = 0; i < page_action_views_.size(); ++i) {
1196 if (page_action_views_[i]->visible()) {
1197 if (current == index) {
1198 page_action_views_[i]->image_view()->view_controller()->
1199 ExecuteAction(true);
1200 return;
1202 ++current;
1206 NOTREACHED();
1209 bool LocationBarView::GetBookmarkStarVisibility() {
1210 DCHECK(star_view_);
1211 return star_view_->visible();
1214 ////////////////////////////////////////////////////////////////////////////////
1215 // LocationBarView, private views::View implementation:
1217 const char* LocationBarView::GetClassName() const {
1218 return kViewClassName;
1221 void LocationBarView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
1222 InstantServiceFactory::GetForProfile(profile())->OnOmniboxStartMarginChanged(
1223 bounds().x());
1225 OmniboxPopupView* popup = omnibox_view_->model()->popup_model()->view();
1226 if (popup->IsOpen())
1227 popup->UpdatePopupAppearance();
1230 void LocationBarView::OnFocus() {
1231 // Explicitly focus the omnibox so a focus ring will be displayed around it on
1232 // Windows.
1233 omnibox_view_->SetFocus();
1236 void LocationBarView::OnPaint(gfx::Canvas* canvas) {
1237 View::OnPaint(canvas);
1239 // Fill the location bar background color behind the border. Parts of the
1240 // border images are meant to rest atop the toolbar background and parts atop
1241 // the omnibox background, so we can't just blindly fill our entire bounds.
1242 gfx::Rect bounds(GetContentsBounds());
1243 bounds.Inset(GetHorizontalEdgeThickness(), GetVerticalEdgeThickness());
1244 SkColor color(GetColor(SecurityStateModel::NONE, BACKGROUND));
1245 if (is_popup_mode_) {
1246 canvas->FillRect(bounds, color);
1247 } else {
1248 SkPaint paint;
1249 paint.setStyle(SkPaint::kFill_Style);
1250 paint.setColor(color);
1251 const int kBorderCornerRadius = 2;
1252 canvas->DrawRoundRect(bounds, kBorderCornerRadius, paint);
1255 // The border itself will be drawn in PaintChildren() since it includes an
1256 // inner shadow which should be drawn over the contents.
1259 void LocationBarView::PaintChildren(const ui::PaintContext& context) {
1260 View::PaintChildren(context);
1262 ui::PaintRecorder recorder(context, size());
1264 // For non-InstantExtendedAPI cases, if necessary, show focus rect. As we need
1265 // the focus rect to appear on top of children we paint here rather than
1266 // OnPaint().
1267 // Note: |Canvas::DrawFocusRect| paints a dashed rect with gray color.
1268 if (show_focus_rect_ && HasFocus())
1269 recorder.canvas()->DrawFocusRect(omnibox_view_->bounds());
1271 // Maximized popup windows don't draw the horizontal edges. We implement this
1272 // by simply expanding the paint area outside the view by the edge thickness.
1273 gfx::Rect border_rect(GetContentsBounds());
1274 if (is_popup_mode_ && (GetHorizontalEdgeThickness() == 0))
1275 border_rect.Inset(-kPopupEdgeThickness, 0);
1276 views::Painter::PaintPainterAt(recorder.canvas(), border_painter_.get(),
1277 border_rect);
1280 ////////////////////////////////////////////////////////////////////////////////
1281 // LocationBarView, private views::ButtonListener implementation:
1283 void LocationBarView::ButtonPressed(views::Button* sender,
1284 const ui::Event& event) {
1285 DCHECK_EQ(mic_search_view_, sender);
1286 command_updater()->ExecuteCommand(IDC_TOGGLE_SPEECH_INPUT);
1289 ////////////////////////////////////////////////////////////////////////////////
1290 // LocationBarView, private views::DragController implementation:
1292 void LocationBarView::WriteDragDataForView(views::View* sender,
1293 const gfx::Point& press_pt,
1294 OSExchangeData* data) {
1295 DCHECK_NE(GetDragOperationsForView(sender, press_pt),
1296 ui::DragDropTypes::DRAG_NONE);
1298 WebContents* web_contents = GetWebContents();
1299 favicon::FaviconDriver* favicon_driver =
1300 favicon::ContentFaviconDriver::FromWebContents(web_contents);
1301 gfx::ImageSkia favicon = favicon_driver->GetFavicon().AsImageSkia();
1302 button_drag_utils::SetURLAndDragImage(web_contents->GetURL(),
1303 web_contents->GetTitle(),
1304 favicon,
1305 NULL,
1306 data,
1307 sender->GetWidget());
1310 int LocationBarView::GetDragOperationsForView(views::View* sender,
1311 const gfx::Point& p) {
1312 DCHECK((sender == location_icon_view_) || (sender == ev_bubble_view_));
1313 WebContents* web_contents = delegate_->GetWebContents();
1314 return (web_contents && web_contents->GetURL().is_valid() &&
1315 (!GetOmniboxView()->IsEditingOrEmpty())) ?
1316 (ui::DragDropTypes::DRAG_COPY | ui::DragDropTypes::DRAG_LINK) :
1317 ui::DragDropTypes::DRAG_NONE;
1320 bool LocationBarView::CanStartDragForView(View* sender,
1321 const gfx::Point& press_pt,
1322 const gfx::Point& p) {
1323 return true;
1326 ////////////////////////////////////////////////////////////////////////////////
1327 // LocationBarView, private gfx::AnimationDelegate implementation:
1328 void LocationBarView::AnimationProgressed(const gfx::Animation* animation) {
1329 browser_->window()->ToolbarSizeChanged(true);
1332 void LocationBarView::AnimationEnded(const gfx::Animation* animation) {
1333 browser_->window()->ToolbarSizeChanged(false);
1336 ////////////////////////////////////////////////////////////////////////////////
1337 // LocationBarView, private OmniboxEditController implementation:
1339 void LocationBarView::OnChanged() {
1340 int icon_id = omnibox_view_->GetIcon();
1341 location_icon_view_->SetImage(GetThemeProvider()->GetImageSkiaNamed(icon_id));
1342 location_icon_view_->ShowTooltip(!GetOmniboxView()->IsEditingOrEmpty());
1344 Layout();
1345 SchedulePaint();
1348 void LocationBarView::OnSetFocus() {
1349 GetFocusManager()->SetFocusedView(this);
1352 const ToolbarModel* LocationBarView::GetToolbarModel() const {
1353 return delegate_->GetToolbarModel();
1356 ////////////////////////////////////////////////////////////////////////////////
1357 // LocationBarView, private DropdownBarHostDelegate implementation:
1359 void LocationBarView::SetFocusAndSelection(bool select_all) {
1360 FocusLocation(select_all);
1363 void LocationBarView::SetAnimationOffset(int offset) {
1364 dropdown_animation_offset_ = offset;
1367 ////////////////////////////////////////////////////////////////////////////////
1368 // LocationBarView, private TemplateURLServiceObserver implementation:
1370 void LocationBarView::OnTemplateURLServiceChanged() {
1371 template_url_service_->RemoveObserver(this);
1372 template_url_service_ = NULL;
1373 // If the browser is no longer active, let's not show the info bubble, as this
1374 // would make the browser the active window again.
1375 if (omnibox_view_ && omnibox_view_->GetWidget()->IsActive())
1376 ShowFirstRunBubble();
1379 ////////////////////////////////////////////////////////////////////////////////
1380 // LocationBarView, private SearchModelObserver implementation:
1382 void LocationBarView::ModelChanged(const SearchModel::State& old_state,
1383 const SearchModel::State& new_state) {
1384 const bool visible = !GetToolbarModel()->input_in_progress() &&
1385 new_state.voice_search_supported;
1386 if (mic_search_view_->visible() != visible) {
1387 mic_search_view_->SetVisible(visible);
1388 Layout();