location_bar_view.cc revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
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.
4
5#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
6
7#include <algorithm>
8#include <map>
9
10#include "base/command_line.h"
11#include "base/i18n/rtl.h"
12#include "base/prefs/pref_service.h"
13#include "base/stl_util.h"
14#include "base/strings/utf_string_conversions.h"
15#include "chrome/app/chrome_command_ids.h"
16#include "chrome/browser/chrome_notification_types.h"
17#include "chrome/browser/command_updater.h"
18#include "chrome/browser/defaults.h"
19#include "chrome/browser/extensions/api/omnibox/omnibox_api.h"
20#include "chrome/browser/extensions/extension_action.h"
21#include "chrome/browser/extensions/location_bar_controller.h"
22#include "chrome/browser/extensions/tab_helper.h"
23#include "chrome/browser/favicon/favicon_tab_helper.h"
24#include "chrome/browser/profiles/profile.h"
25#include "chrome/browser/search/instant_service.h"
26#include "chrome/browser/search/instant_service_factory.h"
27#include "chrome/browser/search/search.h"
28#include "chrome/browser/search_engines/template_url_service_factory.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/omnibox/omnibox_popup_model.h"
36#include "chrome/browser/ui/omnibox/omnibox_popup_view.h"
37#include "chrome/browser/ui/passwords/manage_passwords_icon.h"
38#include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h"
39#include "chrome/browser/ui/tabs/tab_strip_model.h"
40#include "chrome/browser/ui/view_ids.h"
41#include "chrome/browser/ui/views/browser_dialogs.h"
42#include "chrome/browser/ui/views/location_bar/add_to_app_launcher_view.h"
43#include "chrome/browser/ui/views/location_bar/content_setting_image_view.h"
44#include "chrome/browser/ui/views/location_bar/ev_bubble_view.h"
45#include "chrome/browser/ui/views/location_bar/generated_credit_card_view.h"
46#include "chrome/browser/ui/views/location_bar/keyword_hint_view.h"
47#include "chrome/browser/ui/views/location_bar/location_bar_layout.h"
48#include "chrome/browser/ui/views/location_bar/location_icon_view.h"
49#include "chrome/browser/ui/views/location_bar/open_pdf_in_reader_view.h"
50#include "chrome/browser/ui/views/location_bar/origin_chip_view.h"
51#include "chrome/browser/ui/views/location_bar/page_action_image_view.h"
52#include "chrome/browser/ui/views/location_bar/page_action_with_badge_view.h"
53#include "chrome/browser/ui/views/location_bar/search_button.h"
54#include "chrome/browser/ui/views/location_bar/selected_keyword_view.h"
55#include "chrome/browser/ui/views/location_bar/star_view.h"
56#include "chrome/browser/ui/views/location_bar/translate_icon_view.h"
57#include "chrome/browser/ui/views/location_bar/zoom_bubble_view.h"
58#include "chrome/browser/ui/views/location_bar/zoom_view.h"
59#include "chrome/browser/ui/views/passwords/manage_passwords_bubble_view.h"
60#include "chrome/browser/ui/views/passwords/manage_passwords_icon_view.h"
61#include "chrome/browser/ui/views/translate/translate_bubble_view.h"
62#include "chrome/browser/ui/zoom/zoom_controller.h"
63#include "chrome/common/chrome_switches.h"
64#include "chrome/common/pref_names.h"
65#include "components/search_engines/template_url.h"
66#include "components/search_engines/template_url_service.h"
67#include "components/translate/core/browser/language_state.h"
68#include "content/public/browser/notification_service.h"
69#include "content/public/browser/render_widget_host_view.h"
70#include "content/public/browser/web_contents.h"
71#include "extensions/browser/extension_registry.h"
72#include "extensions/common/feature_switch.h"
73#include "extensions/common/permissions/permissions_data.h"
74#include "grit/component_scaled_resources.h"
75#include "grit/generated_resources.h"
76#include "grit/theme_resources.h"
77#include "ui/accessibility/ax_view_state.h"
78#include "ui/base/dragdrop/drag_drop_types.h"
79#include "ui/base/l10n/l10n_util.h"
80#include "ui/base/resource/resource_bundle.h"
81#include "ui/base/theme_provider.h"
82#include "ui/events/event.h"
83#include "ui/gfx/animation/slide_animation.h"
84#include "ui/gfx/canvas.h"
85#include "ui/gfx/color_utils.h"
86#include "ui/gfx/image/image.h"
87#include "ui/gfx/image/image_skia_operations.h"
88#include "ui/gfx/scoped_canvas.h"
89#include "ui/gfx/skia_util.h"
90#include "ui/gfx/text_utils.h"
91#include "ui/native_theme/native_theme.h"
92#include "ui/views/background.h"
93#include "ui/views/border.h"
94#include "ui/views/button_drag_utils.h"
95#include "ui/views/controls/button/image_button.h"
96#include "ui/views/controls/label.h"
97#include "ui/views/widget/widget.h"
98
99#if !defined(OS_CHROMEOS)
100#include "chrome/browser/ui/views/first_run_bubble.h"
101#endif
102
103using content::WebContents;
104using views::View;
105
106namespace {
107
108const gfx::Tween::Type kShowTweenType = gfx::Tween::LINEAR_OUT_SLOW_IN;
109const gfx::Tween::Type kHideTweenType = gfx::Tween::FAST_OUT_LINEAR_IN;
110
111// The search button images are made to look as if they overlay the normal edge
112// images, but to align things, the search button needs to be inset horizontally
113// by 1 px.
114const int kSearchButtonInset = 1;
115
116// Given a containing |height| and a |base_font_list|, shrinks the font size
117// until the font list will fit within |height| while having its cap height
118// vertically centered.  Returns the correctly-sized font list.
119//
120// The expected layout:
121//   +--------+-----------------------------------------------+------------+
122//   |        | y offset                                      | space      |
123//   |        +--------+-------------------+------------------+ above      |
124//   |        |        |                   | internal leading | cap height |
125//   | box    | font   | ascent (baseline) +------------------+------------+
126//   | height | height |                   | cap height                    |
127//   |        |        |-------------------+------------------+------------+
128//   |        |        | descent (height - baseline)          | space      |
129//   |        +--------+--------------------------------------+ below      |
130//   |        | space at bottom                               | cap height |
131//   +--------+-----------------------------------------------+------------+
132// Goal:
133//     center of box height == center of cap height
134//     (i.e. space above cap height == space below cap height)
135// Restrictions:
136//     y offset >= 0
137//     space at bottom >= 0
138//     (i.e. Entire font must be visible inside the box.)
139gfx::FontList GetLargestFontListWithHeightBound(
140    const gfx::FontList& base_font_list,
141    int height) {
142  gfx::FontList font_list = base_font_list;
143  for (int font_size = font_list.GetFontSize(); font_size > 1; --font_size) {
144    const int internal_leading =
145        font_list.GetBaseline() - font_list.GetCapHeight();
146    // Some platforms don't support getting the cap height, and simply return
147    // the entire font ascent from GetCapHeight().  Centering the ascent makes
148    // the font look too low, so if GetCapHeight() returns the ascent, center
149    // the entire font height instead.
150    const int space =
151        height - ((internal_leading != 0) ?
152                  font_list.GetCapHeight() : font_list.GetHeight());
153    const int y_offset = space / 2 - internal_leading;
154    const int space_at_bottom = height - (y_offset + font_list.GetHeight());
155    if ((y_offset >= 0) && (space_at_bottom >= 0))
156      break;
157    font_list = font_list.DeriveWithSizeDelta(-1);
158  }
159  return font_list;
160}
161
162int GetEditLeadingInternalSpace() {
163  // The textfield has 1 px of whitespace before the text in the RTL case only.
164  return base::i18n::IsRTL() ? 1 : 0;
165}
166
167// Functor for moving BookmarkManagerPrivate page actions to the right via
168// stable_partition.
169class IsPageActionViewRightAligned {
170 public:
171  explicit IsPageActionViewRightAligned(
172      extensions::ExtensionRegistry* extension_registry)
173      : extension_registry_(extension_registry) {}
174
175  bool operator()(PageActionWithBadgeView* page_action_view) {
176    return extension_registry_->enabled_extensions().GetByID(
177        page_action_view->image_view()->extension_action()->extension_id())->
178        permissions_data()->
179        HasAPIPermission(extensions::APIPermission::kBookmarkManagerPrivate);
180  }
181
182 private:
183  extensions::ExtensionRegistry* extension_registry_;
184
185  // NOTE: Can't DISALLOW_COPY_AND_ASSIGN as we pass this object by value to
186  // std::stable_partition().
187};
188
189}  // namespace
190
191
192// LocationBarView -----------------------------------------------------------
193
194// static
195const int LocationBarView::kNormalEdgeThickness = 2;
196const int LocationBarView::kPopupEdgeThickness = 1;
197const int LocationBarView::kItemPadding = 3;
198const int LocationBarView::kIconInternalPadding = 2;
199const int LocationBarView::kBubblePadding = 1;
200const char LocationBarView::kViewClassName[] = "LocationBarView";
201
202LocationBarView::LocationBarView(Browser* browser,
203                                 Profile* profile,
204                                 CommandUpdater* command_updater,
205                                 Delegate* delegate,
206                                 bool is_popup_mode)
207    : LocationBar(profile),
208      OmniboxEditController(command_updater),
209      browser_(browser),
210      omnibox_view_(NULL),
211      delegate_(delegate),
212      origin_chip_view_(NULL),
213      location_icon_view_(NULL),
214      ev_bubble_view_(NULL),
215      ime_inline_autocomplete_view_(NULL),
216      selected_keyword_view_(NULL),
217      suggested_text_view_(NULL),
218      keyword_hint_view_(NULL),
219      mic_search_view_(NULL),
220      zoom_view_(NULL),
221      generated_credit_card_view_(NULL),
222      open_pdf_in_reader_view_(NULL),
223      manage_passwords_icon_view_(NULL),
224      translate_icon_view_(NULL),
225      add_to_app_launcher_view_(NULL),
226      star_view_(NULL),
227      search_button_(NULL),
228      is_popup_mode_(is_popup_mode),
229      show_focus_rect_(false),
230      template_url_service_(NULL),
231      dropdown_animation_offset_(0),
232      starting_omnibox_offset_(0),
233      current_omnibox_offset_(0),
234      starting_omnibox_leading_inset_(0),
235      current_omnibox_leading_inset_(0),
236      current_omnibox_width_(0),
237      ending_omnibox_width_(0) {
238  edit_bookmarks_enabled_.Init(
239      prefs::kEditBookmarksEnabled, profile->GetPrefs(),
240      base::Bind(&LocationBarView::Update, base::Unretained(this),
241                 static_cast<content::WebContents*>(NULL)));
242
243  if (browser_)
244    browser_->search_model()->AddObserver(this);
245}
246
247LocationBarView::~LocationBarView() {
248  if (template_url_service_)
249    template_url_service_->RemoveObserver(this);
250  if (browser_)
251    browser_->search_model()->RemoveObserver(this);
252}
253
254////////////////////////////////////////////////////////////////////////////////
255// LocationBarView, public:
256
257void LocationBarView::Init() {
258  // We need to be in a Widget, otherwise GetNativeTheme() may change and we're
259  // not prepared for that.
260  DCHECK(GetWidget());
261
262  const int kOmniboxPopupBorderImages[] =
263      IMAGE_GRID(IDR_OMNIBOX_POPUP_BORDER_AND_SHADOW);
264  const int kOmniboxBorderImages[] = IMAGE_GRID(IDR_TEXTFIELD);
265  border_painter_.reset(views::Painter::CreateImageGridPainter(
266      is_popup_mode_ ? kOmniboxPopupBorderImages : kOmniboxBorderImages));
267
268  location_icon_view_ = new LocationIconView(this);
269  location_icon_view_->set_drag_controller(this);
270  AddChildView(location_icon_view_);
271
272  // Determine the main font.
273  gfx::FontList font_list = ResourceBundle::GetSharedInstance().GetFontList(
274      ResourceBundle::BaseFont);
275  const int current_font_size = font_list.GetFontSize();
276  const int desired_font_size = browser_defaults::kOmniboxFontPixelSize;
277  if (current_font_size != desired_font_size) {
278    font_list =
279        font_list.DeriveWithSizeDelta(desired_font_size - current_font_size);
280  }
281  // Shrink large fonts to make them fit.
282  // TODO(pkasting): Stretch the location bar instead in this case.
283  const int location_height = GetInternalHeight(true);
284  font_list = GetLargestFontListWithHeightBound(font_list, location_height);
285
286  // Determine the font for use inside the bubbles.  The bubble background
287  // images have 1 px thick edges, which we don't want to overlap.
288  const int kBubbleInteriorVerticalPadding = 1;
289  const int bubble_vertical_padding =
290      (kBubblePadding + kBubbleInteriorVerticalPadding) * 2;
291  const gfx::FontList bubble_font_list(
292      GetLargestFontListWithHeightBound(
293          font_list, location_height - bubble_vertical_padding));
294
295  const SkColor background_color =
296      GetColor(ToolbarModel::NONE, LocationBarView::BACKGROUND);
297  ev_bubble_view_ = new EVBubbleView(
298      bubble_font_list, GetColor(ToolbarModel::EV_SECURE, SECURITY_TEXT),
299      background_color, this);
300  ev_bubble_view_->set_drag_controller(this);
301  AddChildView(ev_bubble_view_);
302
303  // Initialize the Omnibox view.
304  omnibox_view_ = new OmniboxViewViews(
305      this, profile(), command_updater(),
306      is_popup_mode_ ||
307          (browser_->is_app() && CommandLine::ForCurrentProcess()->
308              HasSwitch(switches::kEnableStreamlinedHostedApps)),
309      this, font_list);
310  omnibox_view_->Init();
311  omnibox_view_->SetFocusable(true);
312  AddChildView(omnibox_view_);
313
314  // Initialize the inline autocomplete view which is visible only when IME is
315  // turned on.  Use the same font with the omnibox and highlighted background.
316  ime_inline_autocomplete_view_ = new views::Label(base::string16(), font_list);
317  ime_inline_autocomplete_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
318  ime_inline_autocomplete_view_->SetAutoColorReadabilityEnabled(false);
319  ime_inline_autocomplete_view_->set_background(
320      views::Background::CreateSolidBackground(GetNativeTheme()->GetSystemColor(
321          ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused)));
322  ime_inline_autocomplete_view_->SetEnabledColor(
323      GetNativeTheme()->GetSystemColor(
324          ui::NativeTheme::kColorId_TextfieldSelectionColor));
325  ime_inline_autocomplete_view_->SetVisible(false);
326  AddChildView(ime_inline_autocomplete_view_);
327
328  origin_chip_view_ = new OriginChipView(this, profile(), font_list);
329  origin_chip_view_->SetFocusable(false);
330  origin_chip_view_->set_drag_controller(this);
331  AddChildView(origin_chip_view_);
332
333  const SkColor text_color = GetColor(ToolbarModel::NONE, TEXT);
334  selected_keyword_view_ = new SelectedKeywordView(
335      bubble_font_list, text_color, background_color, profile());
336  AddChildView(selected_keyword_view_);
337
338  suggested_text_view_ = new views::Label(base::string16(), font_list);
339  suggested_text_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
340  suggested_text_view_->SetAutoColorReadabilityEnabled(false);
341  suggested_text_view_->SetEnabledColor(GetColor(
342      ToolbarModel::NONE, LocationBarView::DEEMPHASIZED_TEXT));
343  suggested_text_view_->SetVisible(false);
344  AddChildView(suggested_text_view_);
345
346  keyword_hint_view_ = new KeywordHintView(
347      profile(), font_list,
348      GetColor(ToolbarModel::NONE, LocationBarView::DEEMPHASIZED_TEXT),
349      background_color);
350  AddChildView(keyword_hint_view_);
351
352  mic_search_view_ = new views::ImageButton(this);
353  mic_search_view_->set_id(VIEW_ID_MIC_SEARCH_BUTTON);
354  mic_search_view_->SetAccessibilityFocusable(true);
355  mic_search_view_->SetTooltipText(
356      l10n_util::GetStringUTF16(IDS_TOOLTIP_MIC_SEARCH));
357  mic_search_view_->SetImage(
358      views::Button::STATE_NORMAL,
359      ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
360          IDR_OMNIBOX_MIC_SEARCH));
361  mic_search_view_->SetImageAlignment(views::ImageButton::ALIGN_CENTER,
362                                      views::ImageButton::ALIGN_MIDDLE);
363  mic_search_view_->SetVisible(false);
364  AddChildView(mic_search_view_);
365
366  for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) {
367    ContentSettingImageView* content_blocked_view =
368        new ContentSettingImageView(static_cast<ContentSettingsType>(i), this,
369                                    bubble_font_list, text_color,
370                                    background_color);
371    content_setting_views_.push_back(content_blocked_view);
372    content_blocked_view->SetVisible(false);
373    AddChildView(content_blocked_view);
374  }
375
376  generated_credit_card_view_ = new GeneratedCreditCardView(delegate_);
377  AddChildView(generated_credit_card_view_);
378
379  zoom_view_ = new ZoomView(delegate_);
380  zoom_view_->set_id(VIEW_ID_ZOOM_BUTTON);
381  AddChildView(zoom_view_);
382
383  open_pdf_in_reader_view_ = new OpenPDFInReaderView();
384  AddChildView(open_pdf_in_reader_view_);
385
386  manage_passwords_icon_view_ = new ManagePasswordsIconView(command_updater());
387  AddChildView(manage_passwords_icon_view_);
388
389  translate_icon_view_ = new TranslateIconView(command_updater());
390  translate_icon_view_->SetVisible(false);
391  AddChildView(translate_icon_view_);
392
393  add_to_app_launcher_view_ = new AddToAppLauncherView(
394      this, bubble_font_list, text_color, background_color);
395  add_to_app_launcher_view_->SetVisible(false);
396  AddChildView(add_to_app_launcher_view_);
397
398  star_view_ = new StarView(command_updater());
399  star_view_->SetVisible(false);
400  AddChildView(star_view_);
401
402  search_button_ = new SearchButton(this);
403  search_button_->SetVisible(false);
404  AddChildView(search_button_);
405
406  show_url_animation_.reset(new gfx::SlideAnimation(this));
407  show_url_animation_->SetTweenType(kShowTweenType);
408  show_url_animation_->SetSlideDuration(200);
409
410  hide_url_animation_.reset(new gfx::SlideAnimation(this));
411  hide_url_animation_->SetTweenType(kHideTweenType);
412  hide_url_animation_->SetSlideDuration(175);
413
414  content::Source<Profile> profile_source = content::Source<Profile>(profile());
415  registrar_.Add(this,
416                 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED,
417                 profile_source);
418  registrar_.Add(this,
419                 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
420                 profile_source);
421
422  // Initialize the location entry. We do this to avoid a black flash which is
423  // visible when the location entry has just been initialized.
424  Update(NULL);
425}
426
427bool LocationBarView::IsInitialized() const {
428  return omnibox_view_ != NULL;
429}
430
431SkColor LocationBarView::GetColor(ToolbarModel::SecurityLevel security_level,
432                                  ColorKind kind) const {
433  const ui::NativeTheme* native_theme = GetNativeTheme();
434  switch (kind) {
435    case BACKGROUND:
436      return native_theme->GetSystemColor(
437          ui::NativeTheme::kColorId_TextfieldDefaultBackground);
438
439    case TEXT:
440      return native_theme->GetSystemColor(
441          ui::NativeTheme::kColorId_TextfieldDefaultColor);
442
443    case SELECTED_TEXT:
444      return native_theme->GetSystemColor(
445          ui::NativeTheme::kColorId_TextfieldSelectionColor);
446
447    case DEEMPHASIZED_TEXT:
448      return color_utils::AlphaBlend(
449          GetColor(security_level, TEXT),
450          GetColor(security_level, BACKGROUND),
451          128);
452
453    case SECURITY_TEXT: {
454      SkColor color;
455      switch (security_level) {
456        case ToolbarModel::EV_SECURE:
457        case ToolbarModel::SECURE:
458          color = SkColorSetRGB(7, 149, 0);
459          break;
460
461        case ToolbarModel::SECURITY_WARNING:
462        case ToolbarModel::SECURITY_POLICY_WARNING:
463          return GetColor(security_level, DEEMPHASIZED_TEXT);
464          break;
465
466        case ToolbarModel::SECURITY_ERROR:
467          color = SkColorSetRGB(162, 0, 0);
468          break;
469
470        default:
471          NOTREACHED();
472          return GetColor(security_level, TEXT);
473      }
474      return color_utils::GetReadableColor(
475          color, GetColor(security_level, BACKGROUND));
476    }
477
478    default:
479      NOTREACHED();
480      return GetColor(security_level, TEXT);
481  }
482}
483
484void LocationBarView::ZoomChangedForActiveTab(bool can_show_bubble) {
485  DCHECK(zoom_view_);
486  if (RefreshZoomView()) {
487    Layout();
488    SchedulePaint();
489  }
490
491  WebContents* web_contents = GetWebContents();
492  if (can_show_bubble && zoom_view_->visible() && web_contents)
493    ZoomBubbleView::ShowBubble(web_contents, true);
494}
495
496void LocationBarView::SetPreviewEnabledPageAction(ExtensionAction* page_action,
497                                                  bool preview_enabled) {
498  if (is_popup_mode_)
499    return;
500
501  DCHECK(page_action);
502  WebContents* web_contents = GetWebContents();
503
504  RefreshPageActionViews();
505  PageActionWithBadgeView* page_action_view =
506      static_cast<PageActionWithBadgeView*>(GetPageActionView(page_action));
507  DCHECK(page_action_view);
508  if (!page_action_view)
509    return;
510
511  page_action_view->image_view()->set_preview_enabled(preview_enabled);
512  page_action_view->UpdateVisibility(web_contents);
513  Layout();
514  SchedulePaint();
515}
516
517PageActionWithBadgeView* LocationBarView::GetPageActionView(
518    ExtensionAction* page_action) {
519  DCHECK(page_action);
520  for (PageActionViews::const_iterator i(page_action_views_.begin());
521       i != page_action_views_.end(); ++i) {
522    if ((*i)->image_view()->extension_action() == page_action)
523      return *i;
524  }
525  return NULL;
526}
527
528void LocationBarView::SetStarToggled(bool on) {
529  if (star_view_)
530    star_view_->SetToggled(on);
531}
532
533void LocationBarView::SetTranslateIconToggled(bool on) {
534  translate_icon_view_->SetToggled(on);
535}
536
537gfx::Point LocationBarView::GetOmniboxViewOrigin() const {
538  gfx::Point origin(omnibox_view_->bounds().origin());
539  origin.set_x(GetMirroredXInView(origin.x() - current_omnibox_offset_));
540  views::View::ConvertPointToScreen(this, &origin);
541  return origin;
542}
543
544void LocationBarView::SetImeInlineAutocompletion(const base::string16& text) {
545  ime_inline_autocomplete_view_->SetText(text);
546  ime_inline_autocomplete_view_->SetVisible(!text.empty());
547}
548
549void LocationBarView::SetGrayTextAutocompletion(const base::string16& text) {
550  if (suggested_text_view_->text() != text) {
551    suggested_text_view_->SetText(text);
552    suggested_text_view_->SetVisible(!text.empty());
553    Layout();
554    SchedulePaint();
555  }
556}
557
558base::string16 LocationBarView::GetGrayTextAutocompletion() const {
559  return HasValidSuggestText() ?
560      suggested_text_view_->text() : base::string16();
561}
562
563void LocationBarView::SetShowFocusRect(bool show) {
564  show_focus_rect_ = show;
565  SchedulePaint();
566}
567
568void LocationBarView::SelectAll() {
569  omnibox_view_->SelectAll(true);
570}
571
572gfx::Point LocationBarView::GetLocationBarAnchorPoint() const {
573  // The +1 in the next line creates a 1-px gap between icon and arrow tip.
574  gfx::Point icon_bottom(0, location_icon_view_->GetImageBounds().bottom() -
575      LocationBarView::kIconInternalPadding + 1);
576  gfx::Point icon_center(location_icon_view_->GetImageBounds().CenterPoint());
577  gfx::Point point(icon_center.x(), icon_bottom.y());
578  ConvertPointToTarget(location_icon_view_, this, &point);
579  return point;
580}
581
582views::View* LocationBarView::generated_credit_card_view() {
583  return generated_credit_card_view_;
584}
585
586int LocationBarView::GetInternalHeight(bool use_preferred_size) {
587  int total_height =
588      use_preferred_size ? GetPreferredSize().height() : height();
589  return std::max(total_height - (vertical_edge_thickness() * 2), 0);
590}
591
592void LocationBarView::GetOmniboxPopupPositioningInfo(
593    gfx::Point* top_left_screen_coord,
594    int* popup_width,
595    int* left_margin,
596    int* right_margin) {
597  // Because the popup might appear atop the attached bookmark bar, there won't
598  // necessarily be a client edge separating it from the rest of the toolbar.
599  // Therefore we position the popup high enough so it can draw its own client
600  // edge at the top, in the same place the toolbar would normally draw the
601  // client edge.
602  *top_left_screen_coord = gfx::Point(
603      0,
604      parent()->height() - views::NonClientFrameView::kClientEdgeThickness);
605  views::View::ConvertPointToScreen(parent(), top_left_screen_coord);
606  *popup_width = parent()->width();
607
608  gfx::Rect location_bar_bounds(bounds());
609  location_bar_bounds.Inset(kNormalEdgeThickness, 0);
610  *left_margin = location_bar_bounds.x();
611  *right_margin = *popup_width - location_bar_bounds.right();
612}
613
614////////////////////////////////////////////////////////////////////////////////
615// LocationBarView, public LocationBar implementation:
616
617void LocationBarView::FocusLocation(bool select_all) {
618  omnibox_view_->SetFocus();
619  if (select_all)
620    omnibox_view_->SelectAll(true);
621}
622
623void LocationBarView::Revert() {
624  omnibox_view_->RevertAll();
625}
626
627OmniboxView* LocationBarView::GetOmniboxView() {
628  return omnibox_view_;
629}
630
631////////////////////////////////////////////////////////////////////////////////
632// LocationBarView, public views::View implementation:
633
634bool LocationBarView::HasFocus() const {
635  return omnibox_view_->model()->has_focus();
636}
637
638void LocationBarView::GetAccessibleState(ui::AXViewState* state) {
639  state->role = ui::AX_ROLE_GROUP;
640}
641
642gfx::Size LocationBarView::GetPreferredSize() const {
643  // Compute minimum height.
644  gfx::Size min_size(border_painter_->GetMinimumSize());
645  if (!IsInitialized())
646    return min_size;
647  gfx::Size search_button_min_size(search_button_->GetMinimumSize());
648  min_size.SetToMax(search_button_min_size);
649
650  // Compute width of omnibox-leading content.
651  const int horizontal_edge_thickness = GetHorizontalEdgeThickness();
652  int leading_width = horizontal_edge_thickness;
653  // TODO(pkasting): Make the origin chip min width sane, and make the chip
654  // handle being shrunken down more gracefully; then uncomment this.
655  /*if (GetToolbarModel()->ShouldShowOriginChip())
656    leading_width += origin_chip_view_->GetMinimumSize().width();*/
657  if (ShouldShowKeywordBubble()) {
658    // The selected keyword view can collapse completely.
659  } else if (ShouldShowEVBubble()) {
660    leading_width += kBubblePadding +
661        ev_bubble_view_->GetMinimumSizeForLabelText(
662            GetToolbarModel()->GetEVCertName()).width();
663  } else if (!origin_chip_view_->visible()) {
664    leading_width +=
665        kItemPadding + location_icon_view_->GetMinimumSize().width();
666  }
667  leading_width += kItemPadding - GetEditLeadingInternalSpace();
668
669  // Compute width of omnibox-trailing content.
670  int trailing_width = search_button_->visible() ?
671      (search_button_->GetMinimumSize().width() + kSearchButtonInset) :
672      horizontal_edge_thickness;
673  trailing_width += IncrementalMinimumWidth(star_view_) +
674      IncrementalMinimumWidth(translate_icon_view_) +
675      IncrementalMinimumWidth(open_pdf_in_reader_view_) +
676      IncrementalMinimumWidth(manage_passwords_icon_view_) +
677      IncrementalMinimumWidth(zoom_view_) +
678      IncrementalMinimumWidth(generated_credit_card_view_) +
679      IncrementalMinimumWidth(mic_search_view_) +
680      IncrementalMinimumWidth(add_to_app_launcher_view_) + kItemPadding;
681  for (PageActionViews::const_iterator i(page_action_views_.begin());
682       i != page_action_views_.end(); ++i)
683    trailing_width += IncrementalMinimumWidth((*i));
684  for (ContentSettingViews::const_iterator i(content_setting_views_.begin());
685       i != content_setting_views_.end(); ++i)
686    trailing_width += IncrementalMinimumWidth((*i));
687
688  min_size.set_width(
689      leading_width + omnibox_view_->GetMinimumSize().width() + trailing_width);
690  return min_size;
691}
692
693void LocationBarView::Layout() {
694  if (!IsInitialized())
695    return;
696
697  origin_chip_view_->SetVisible(GetToolbarModel()->ShouldShowOriginChip());
698  selected_keyword_view_->SetVisible(false);
699  location_icon_view_->SetVisible(false);
700  ev_bubble_view_->SetVisible(false);
701  keyword_hint_view_->SetVisible(false);
702
703  LocationBarLayout leading_decorations(
704      LocationBarLayout::LEFT_EDGE,
705      kItemPadding - GetEditLeadingInternalSpace());
706  LocationBarLayout trailing_decorations(LocationBarLayout::RIGHT_EDGE,
707                                         kItemPadding);
708
709  const int origin_chip_preferred_width =
710      origin_chip_view_->GetPreferredSize().width();
711  const int origin_chip_width =
712      origin_chip_view_->visible() ? origin_chip_preferred_width : 0;
713  // Always give the origin chip view its desired size and lay it out, even when
714  // it's not visible, so we can calculate the correct animation values below
715  // when switching to tabs that have the origin chip hidden.
716  origin_chip_view_->SetBounds(0, 0, origin_chip_preferred_width, height());
717  origin_chip_view_->Layout();
718
719  const int bubble_location_y = vertical_edge_thickness() + kBubblePadding;
720  const base::string16 keyword(omnibox_view_->model()->keyword());
721  // In some cases (e.g. fullscreen mode) we may have 0 height.  We still want
722  // to position our child views in this case, because other things may be
723  // positioned relative to them (e.g. the "bookmark added" bubble if the user
724  // hits ctrl-d).
725  const int location_height = GetInternalHeight(false);
726  const int bubble_height = std::max(location_height - (kBubblePadding * 2), 0);
727  if (ShouldShowKeywordBubble()) {
728    leading_decorations.AddDecoration(bubble_location_y, bubble_height, true, 0,
729                                      kBubblePadding, kItemPadding,
730                                      selected_keyword_view_);
731    if (selected_keyword_view_->keyword() != keyword) {
732      selected_keyword_view_->SetKeyword(keyword);
733      const TemplateURL* template_url =
734          TemplateURLServiceFactory::GetForProfile(profile())->
735          GetTemplateURLForKeyword(keyword);
736      if (template_url &&
737          (template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION)) {
738        gfx::Image image = extensions::OmniboxAPI::Get(profile())->
739            GetOmniboxIcon(template_url->GetExtensionId());
740        selected_keyword_view_->SetImage(image.AsImageSkia());
741        selected_keyword_view_->set_is_extension_icon(true);
742      } else {
743        selected_keyword_view_->SetImage(
744            *(GetThemeProvider()->GetImageSkiaNamed(IDR_OMNIBOX_SEARCH)));
745        selected_keyword_view_->set_is_extension_icon(false);
746      }
747    }
748  } else if (ShouldShowEVBubble()) {
749    ev_bubble_view_->SetLabel(GetToolbarModel()->GetEVCertName());
750    // The largest fraction of the omnibox that can be taken by the EV bubble.
751    const double kMaxBubbleFraction = 0.5;
752    leading_decorations.AddDecoration(bubble_location_y, bubble_height, false,
753                                      kMaxBubbleFraction, kBubblePadding,
754                                      kItemPadding, ev_bubble_view_);
755  } else if (!origin_chip_view_->visible()) {
756    leading_decorations.AddDecoration(
757        vertical_edge_thickness(), location_height,
758        location_icon_view_);
759  }
760
761  if (star_view_->visible()) {
762    trailing_decorations.AddDecoration(
763        vertical_edge_thickness(), location_height, star_view_);
764  }
765  if (add_to_app_launcher_view_->visible()) {
766    trailing_decorations.AddDecoration(
767        vertical_edge_thickness(), location_height, add_to_app_launcher_view_);
768  }
769  if (translate_icon_view_->visible()) {
770    trailing_decorations.AddDecoration(
771        vertical_edge_thickness(), location_height, translate_icon_view_);
772  }
773  if (open_pdf_in_reader_view_->visible()) {
774    trailing_decorations.AddDecoration(
775        vertical_edge_thickness(), location_height, open_pdf_in_reader_view_);
776  }
777  if (manage_passwords_icon_view_->visible()) {
778    trailing_decorations.AddDecoration(vertical_edge_thickness(),
779                                       location_height,
780                                       manage_passwords_icon_view_);
781  }
782  for (PageActionViews::const_iterator i(page_action_views_.begin());
783       i != page_action_views_.end(); ++i) {
784    if ((*i)->visible()) {
785      trailing_decorations.AddDecoration(
786          vertical_edge_thickness(), location_height, (*i));
787    }
788  }
789  if (zoom_view_->visible()) {
790    trailing_decorations.AddDecoration(vertical_edge_thickness(),
791                                       location_height, zoom_view_);
792  }
793  for (ContentSettingViews::const_reverse_iterator i(
794           content_setting_views_.rbegin()); i != content_setting_views_.rend();
795       ++i) {
796    if ((*i)->visible()) {
797      trailing_decorations.AddDecoration(
798          bubble_location_y, bubble_height, false, 0, kItemPadding,
799          kItemPadding, (*i));
800    }
801  }
802  if (generated_credit_card_view_->visible()) {
803    trailing_decorations.AddDecoration(vertical_edge_thickness(),
804                                       location_height,
805                                       generated_credit_card_view_);
806  }
807  if (mic_search_view_->visible()) {
808    trailing_decorations.AddDecoration(vertical_edge_thickness(),
809                                       location_height, mic_search_view_);
810  }
811  // Because IMEs may eat the tab key, we don't show "press tab to search" while
812  // IME composition is in progress.
813  if (!keyword.empty() && omnibox_view_->model()->is_keyword_hint() &&
814      !omnibox_view_->IsImeComposing()) {
815    trailing_decorations.AddDecoration(vertical_edge_thickness(),
816                                       location_height, true, 0, kItemPadding,
817                                       kItemPadding, keyword_hint_view_);
818    if (keyword_hint_view_->keyword() != keyword)
819      keyword_hint_view_->SetKeyword(keyword);
820  }
821
822  // Perform layout.
823  const int horizontal_edge_thickness = GetHorizontalEdgeThickness();
824  int full_width = width() - horizontal_edge_thickness - origin_chip_width;
825
826  const gfx::Size search_button_size(search_button_->GetPreferredSize());
827  const int search_button_reserved_width =
828      search_button_size.width() + kSearchButtonInset;
829  full_width -= search_button_->visible() ?
830      search_button_reserved_width : horizontal_edge_thickness;
831  int entry_width = full_width;
832  leading_decorations.LayoutPass1(&entry_width);
833  trailing_decorations.LayoutPass1(&entry_width);
834  leading_decorations.LayoutPass2(&entry_width);
835  trailing_decorations.LayoutPass2(&entry_width);
836
837  int location_needed_width = omnibox_view_->GetTextWidth();
838  int available_width = entry_width - location_needed_width;
839  // The bounds must be wide enough for all the decorations to fit.
840  gfx::Rect location_bounds(
841      origin_chip_width + horizontal_edge_thickness, vertical_edge_thickness(),
842      std::max(full_width, full_width - entry_width), location_height);
843  leading_decorations.LayoutPass3(&location_bounds, &available_width);
844  trailing_decorations.LayoutPass3(&location_bounds, &available_width);
845
846  // Calculate the animation parameters (see comments on these members in the
847  // header).  We have to do this in Layout, after |origin_chip_view_| is laid
848  // out, because that may affect the host label offset in the origin chip.
849  const base::string16& chip_text(origin_chip_view_->host_label_text());
850  // If the chip is clicked, the omnibox text will become the toolbar model's
851  // formatted URL.  We can't ask the omnibox for its current text, because
852  // while the chip is visible the current text is empty.
853  size_t prefix_end = 0;
854  const base::string16& omnibox_text(
855      GetToolbarModel()->GetFormattedURL(&prefix_end));
856  // Do a case-insensitive search to better match cases like
857  // "Settings" <-> "chrome://settings".  Skip any pre-hostname text.
858  size_t chip_text_offset = std::search(
859      omnibox_text.begin() + prefix_end, omnibox_text.end(),
860      chip_text.begin(), chip_text.end(),
861      base::CaseInsensitiveCompare<base::char16>()) - omnibox_text.begin();
862  // If we couldn't find the chip text, try checking whether the omnibox text
863  // starts with it, as is true for e.g. file: URLs.
864  if ((chip_text_offset >= omnibox_text.length()) &&
865      StartsWith(omnibox_text, chip_text, true))
866    chip_text_offset = 0;
867  const gfx::FontList& font_list = omnibox_view_->GetFontList();
868  const int chip_text_width = gfx::GetStringWidth(chip_text, font_list);
869  const int old_starting_offset = starting_omnibox_offset_;
870  const int old_starting_leading_inset = starting_omnibox_leading_inset_;
871  const int old_ending_width = ending_omnibox_width_;
872  starting_omnibox_offset_ = current_omnibox_offset_ = 0;
873  starting_omnibox_leading_inset_ = current_omnibox_leading_inset_ = 0;
874  ending_omnibox_width_ = gfx::GetStringWidth(omnibox_text, font_list);
875  if (chip_text_offset < omnibox_text.length()) {
876    if (base::i18n::IsRTL())
877      chip_text_offset += chip_text.length();
878    base::string16 extra_omnibox_text(base::i18n::IsRTL() ?
879        omnibox_text.substr(chip_text_offset) :
880        omnibox_text.substr(0, chip_text_offset));
881    starting_omnibox_leading_inset_ =
882        gfx::GetStringWidth(extra_omnibox_text, font_list);
883    starting_omnibox_offset_ = origin_chip_view_->HostLabelOffset() -
884        starting_omnibox_leading_inset_;
885    current_omnibox_width_ = chip_text_width;
886  } else {
887    // If the chip text wasn't found in the omnibox text, then instead of
888    // starting the show animation clipped to the "hostname", we'll start with
889    // the entire omnibox text visible, clipped to the remaining chip width, and
890    // only animate any necessary expansion of that width, without moving the
891    // omnibox bounds.
892    current_omnibox_width_ = origin_chip_view_->WidthFromStartOfLabels();
893  }
894
895  // End the animations immediately if the parameters have changed.
896  if ((starting_omnibox_offset_ != old_starting_offset) ||
897      (starting_omnibox_leading_inset_ != old_starting_leading_inset) ||
898      (ending_omnibox_width_ != old_ending_width))
899    EndOriginChipAnimations(true);
900
901  // Also end the animations immediately if there's nothing to animate (but do
902  // allow the chip to fade back in).
903  const ui::NativeTheme* native_theme = GetNativeTheme();
904  const SkColor ending_selection_text_color = native_theme->GetSystemColor(
905      ui::NativeTheme::kColorId_TextfieldSelectionColor);
906  const SkColor ending_selection_background_color =
907      native_theme->GetSystemColor(
908          ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused);
909  if ((starting_omnibox_offset_ == 0) &&
910      (starting_omnibox_leading_inset_ == 0) &&
911      (ending_omnibox_width_ == chip_text_width) &&
912      (hide_url_animation_->is_animating() ||
913       ((ending_selection_text_color ==
914            origin_chip_view_->pressed_text_color()) &&
915        (ending_selection_background_color ==
916            origin_chip_view_->pressed_background_color()))))
917    EndOriginChipAnimations(false);
918
919  if (show_url_animation_->is_animating()) {
920    omnibox_view_->SetSelectionTextColor(gfx::Tween::ColorValueBetween(
921        show_url_animation_->GetCurrentValue(),
922        origin_chip_view_->pressed_text_color(),
923        ending_selection_text_color));
924    omnibox_view_->SetSelectionBackgroundColor(gfx::Tween::ColorValueBetween(
925        show_url_animation_->GetCurrentValue(),
926        origin_chip_view_->pressed_background_color(),
927        ending_selection_background_color));
928    current_omnibox_offset_ =
929        show_url_animation_->CurrentValueBetween(starting_omnibox_offset_, 0);
930    current_omnibox_leading_inset_ = show_url_animation_->CurrentValueBetween(
931        starting_omnibox_leading_inset_, 0);
932    current_omnibox_width_ = show_url_animation_->CurrentValueBetween(
933        chip_text_width, ending_omnibox_width_);
934  } else if (hide_url_animation_->is_animating()) {
935    current_omnibox_offset_ =
936        hide_url_animation_->CurrentValueBetween(0, starting_omnibox_offset_);
937    current_omnibox_leading_inset_ = hide_url_animation_->CurrentValueBetween(
938        0, starting_omnibox_leading_inset_);
939    current_omnibox_width_ = hide_url_animation_->CurrentValueBetween(
940        ending_omnibox_width_, chip_text_width);
941  }
942  // Contract |available_width| as necessary, but never expand it.  This way,
943  // we'll never draw suggested text at first and then have it disappear
944  // midway through the animation.
945  if (current_omnibox_offset_ > 0)
946    available_width -= current_omnibox_offset_;
947  location_bounds.Inset(current_omnibox_offset_, 0, 0, 0);
948
949  // Layout out the suggested text view right aligned to the location
950  // entry. Only show the suggested text if we can fit the text from one
951  // character before the end of the selection to the end of the text and the
952  // suggested text. If we can't it means either the suggested text is too big,
953  // or the user has scrolled.
954
955  // TODO(sky): We could potentially adjust this to take into account suggested
956  // text to force using minimum size if necessary, but currently the chance of
957  // showing keyword hints and suggested text is minimal and we're not confident
958  // this is the right approach for suggested text.
959
960  int omnibox_view_margin = 0;
961  if (suggested_text_view_->visible()) {
962    // We do not display the suggested text when it contains a mix of RTL and
963    // LTR characters since this could mean the suggestion should be displayed
964    // in the middle of the string.
965    base::i18n::TextDirection text_direction =
966        base::i18n::GetStringDirection(omnibox_view_->GetText());
967    if (text_direction !=
968        base::i18n::GetStringDirection(suggested_text_view_->text()))
969      text_direction = base::i18n::UNKNOWN_DIRECTION;
970
971    // TODO(sky): need to layout when the user changes caret position.
972    gfx::Size suggested_text_size(suggested_text_view_->GetPreferredSize());
973    if (suggested_text_size.width() > available_width ||
974        text_direction == base::i18n::UNKNOWN_DIRECTION) {
975      // Hide the suggested text if the user has scrolled or we can't fit all
976      // the suggested text, or we have a mix of RTL and LTR characters.
977      suggested_text_view_->SetBounds(0, 0, 0, 0);
978    } else {
979      location_needed_width =
980          std::min(location_needed_width,
981                   location_bounds.width() - suggested_text_size.width());
982      gfx::Rect suggested_text_bounds(location_bounds.x(), location_bounds.y(),
983                                      suggested_text_size.width(),
984                                      location_bounds.height());
985      // TODO(sky): figure out why this needs the -1.
986      suggested_text_bounds.Offset(location_needed_width - 1, 0);
987
988      // We reverse the order of the location entry and suggested text if:
989      // - Chrome is RTL but the text is fully LTR, or
990      // - Chrome is LTR but the text is fully RTL.
991      // This ensures the suggested text is correctly displayed to the right
992      // (or left) of the user text.
993      if (text_direction == (base::i18n::IsRTL() ?
994          base::i18n::LEFT_TO_RIGHT : base::i18n::RIGHT_TO_LEFT)) {
995        // TODO(sky): Figure out why we need the +1.
996        suggested_text_bounds.set_x(location_bounds.x() + 1);
997        // Use a margin to prevent omnibox text from overlapping suggest text.
998        omnibox_view_margin = suggested_text_bounds.width();
999      }
1000      suggested_text_view_->SetBoundsRect(suggested_text_bounds);
1001    }
1002  }
1003
1004  const gfx::Insets insets = omnibox_view_->GetInsets();
1005  omnibox_view_->SetBorder(views::Border::CreateEmptyBorder(
1006      insets.top(), insets.left(), insets.bottom(), omnibox_view_margin));
1007
1008  // Layout |ime_inline_autocomplete_view_| next to the user input.
1009  if (ime_inline_autocomplete_view_->visible()) {
1010    int width =
1011        gfx::GetStringWidth(ime_inline_autocomplete_view_->text(),
1012                            ime_inline_autocomplete_view_->font_list()) +
1013        ime_inline_autocomplete_view_->GetInsets().width();
1014    // All the target languages (IMEs) are LTR, and we do not need to support
1015    // RTL so far.  In other words, no testable RTL environment so far.
1016    int x = location_needed_width;
1017    if (width > entry_width)
1018      x = 0;
1019    else if (location_needed_width + width > entry_width)
1020      x = entry_width - width;
1021    location_bounds.set_width(x);
1022    ime_inline_autocomplete_view_->SetBounds(
1023        location_bounds.right(), location_bounds.y(),
1024        std::min(width, entry_width), location_bounds.height());
1025  }
1026
1027  omnibox_view_->SetBoundsRect(location_bounds);
1028
1029  search_button_->SetBoundsRect(gfx::Rect(
1030      gfx::Point(width() - search_button_reserved_width, 0),
1031      search_button_size));
1032}
1033
1034////////////////////////////////////////////////////////////////////////////////
1035// LocationBarView, public OmniboxEditController implementation:
1036
1037void LocationBarView::Update(const WebContents* contents) {
1038  mic_search_view_->SetVisible(
1039      !GetToolbarModel()->input_in_progress() && browser_ &&
1040      browser_->search_model()->voice_search_supported());
1041  RefreshContentSettingViews();
1042  generated_credit_card_view_->Update();
1043  ZoomBubbleView::CloseBubble();
1044  TranslateBubbleView::CloseBubble();
1045  RefreshZoomView();
1046  RefreshPageActionViews();
1047  RefreshTranslateIcon();
1048  RefreshManagePasswordsIconView();
1049  content::WebContents* web_contents_for_sub_views =
1050      GetToolbarModel()->input_in_progress() ? NULL : GetWebContents();
1051  open_pdf_in_reader_view_->Update(web_contents_for_sub_views);
1052  add_to_app_launcher_view_->Update(web_contents_for_sub_views);
1053
1054  if (star_view_) {
1055    star_view_->SetVisible(
1056        browser_defaults::bookmarks_enabled && !is_popup_mode_ &&
1057        !GetToolbarModel()->input_in_progress() &&
1058        edit_bookmarks_enabled_.GetValue() &&
1059        !IsBookmarkStarHiddenByExtension());
1060  }
1061
1062  if (contents)
1063    omnibox_view_->OnTabChanged(contents);
1064  else
1065    omnibox_view_->Update();
1066
1067  OnChanged();  // NOTE: Calls Layout().
1068}
1069
1070void LocationBarView::ShowURL() {
1071  // Start the animation before calling ShowURL(), since the latter eventually
1072  // calls back to Layout(), and if the animation is not marked as "running",
1073  // we'll draw the omnibox in its final position briefly until the first
1074  // animation callback reaches us.
1075  if (chrome::ShouldDisplayOriginChip()) {
1076    // If we're currently hiding, reverse the hide by swapping to the show
1077    // animation, offset so that the text is in the same position.
1078    if (hide_url_animation_->is_animating()) {
1079      const double show_value = GetValueForAnimation(false);
1080      hide_url_animation_->Reset();
1081      show_url_animation_->Show();
1082      // This must be done after calling Show() and is not equivalent to
1083      // calling Reset(n) before Show(); Reset() would have caused the entire
1084      // animation curve (and time) to run between this value and the final
1085      // value, whereas Show() + SetCurrentValue() skips the animation forward
1086      // to the supplied value.
1087      show_url_animation_->SetCurrentValue(show_value);
1088    } else {
1089      show_url_animation_->Show();
1090    }
1091  }
1092  omnibox_view_->ShowURL();
1093}
1094
1095void LocationBarView::EndOriginChipAnimations(bool cancel_fade) {
1096  show_url_animation_->End();
1097  hide_url_animation_->End();
1098  if (cancel_fade)
1099    origin_chip_view_->CancelFade();
1100}
1101
1102ToolbarModel* LocationBarView::GetToolbarModel() {
1103  return delegate_->GetToolbarModel();
1104}
1105
1106WebContents* LocationBarView::GetWebContents() {
1107  return delegate_->GetWebContents();
1108}
1109
1110////////////////////////////////////////////////////////////////////////////////
1111// LocationBarView, private:
1112
1113// static
1114int LocationBarView::IncrementalMinimumWidth(views::View* view) {
1115  return view->visible() ? (kItemPadding + view->GetMinimumSize().width()) : 0;
1116}
1117
1118int LocationBarView::GetHorizontalEdgeThickness() const {
1119  // In maximized popup mode, there isn't any edge.
1120  return (is_popup_mode_ && browser_ && browser_->window() &&
1121      browser_->window()->IsMaximized()) ? 0 : vertical_edge_thickness();
1122}
1123
1124bool LocationBarView::RefreshContentSettingViews() {
1125  bool visibility_changed = false;
1126  for (ContentSettingViews::const_iterator i(content_setting_views_.begin());
1127       i != content_setting_views_.end(); ++i) {
1128    const bool was_visible = (*i)->visible();
1129    (*i)->Update(GetToolbarModel()->input_in_progress() ?
1130        NULL : GetWebContents());
1131    if (was_visible != (*i)->visible())
1132      visibility_changed = true;
1133  }
1134  return visibility_changed;
1135}
1136
1137void LocationBarView::DeletePageActionViews() {
1138  for (PageActionViews::const_iterator i(page_action_views_.begin());
1139       i != page_action_views_.end(); ++i)
1140    RemoveChildView(*i);
1141  STLDeleteElements(&page_action_views_);
1142}
1143
1144bool LocationBarView::RefreshPageActionViews() {
1145  if (is_popup_mode_)
1146    return false;
1147
1148  bool changed = false;
1149  PageActions new_page_actions;
1150
1151  WebContents* web_contents = GetWebContents();
1152  if (web_contents) {
1153    extensions::TabHelper* extensions_tab_helper =
1154        extensions::TabHelper::FromWebContents(web_contents);
1155    extensions::LocationBarController* controller =
1156        extensions_tab_helper->location_bar_controller();
1157    new_page_actions = controller->GetCurrentActions();
1158  }
1159
1160  // On startup we sometimes haven't loaded any extensions. This makes sure
1161  // we catch up when the extensions (and any page actions) load.
1162  if (page_actions_ != new_page_actions) {
1163    changed = true;
1164
1165    page_actions_.swap(new_page_actions);
1166    DeletePageActionViews();  // Delete the old views (if any).
1167
1168    // Create the page action views.
1169    for (PageActions::const_iterator i = page_actions_.begin();
1170         i != page_actions_.end(); ++i) {
1171      PageActionWithBadgeView* page_action_view = new PageActionWithBadgeView(
1172          delegate_->CreatePageActionImageView(this, *i));
1173      page_action_view->SetVisible(false);
1174      page_action_views_.push_back(page_action_view);
1175    }
1176
1177    // Move rightmost extensions to the start.
1178    std::stable_partition(
1179        page_action_views_.begin(),
1180        page_action_views_.end(),
1181        IsPageActionViewRightAligned(
1182            extensions::ExtensionRegistry::Get(profile())));
1183
1184    View* right_anchor = open_pdf_in_reader_view_;
1185    if (!right_anchor)
1186      right_anchor = star_view_;
1187    DCHECK(right_anchor);
1188
1189    // |page_action_views_| are ordered right-to-left.  Add them as children in
1190    // reverse order so the logical order and visual order match for
1191    // accessibility purposes.
1192    for (PageActionViews::reverse_iterator i = page_action_views_.rbegin();
1193         i != page_action_views_.rend(); ++i)
1194      AddChildViewAt(*i, GetIndexOf(right_anchor));
1195  }
1196
1197  if (!page_action_views_.empty() && web_contents) {
1198    for (PageActionViews::const_iterator i(page_action_views_.begin());
1199         i != page_action_views_.end(); ++i) {
1200      bool old_visibility = (*i)->visible();
1201      (*i)->UpdateVisibility(
1202          GetToolbarModel()->input_in_progress() ? NULL : web_contents);
1203      changed |= old_visibility != (*i)->visible();
1204    }
1205  }
1206  return changed;
1207}
1208
1209bool LocationBarView::RefreshZoomView() {
1210  DCHECK(zoom_view_);
1211  WebContents* web_contents = GetWebContents();
1212  if (!web_contents)
1213    return false;
1214  const bool was_visible = zoom_view_->visible();
1215  zoom_view_->Update(ZoomController::FromWebContents(web_contents));
1216  return was_visible != zoom_view_->visible();
1217}
1218
1219void LocationBarView::RefreshTranslateIcon() {
1220  if (!TranslateService::IsTranslateBubbleEnabled())
1221    return;
1222
1223  WebContents* web_contents = GetWebContents();
1224  if (!web_contents)
1225    return;
1226  translate::LanguageState& language_state =
1227      ChromeTranslateClient::FromWebContents(web_contents)->GetLanguageState();
1228  bool enabled = language_state.translate_enabled();
1229  command_updater()->UpdateCommandEnabled(IDC_TRANSLATE_PAGE, enabled);
1230  translate_icon_view_->SetVisible(enabled);
1231  translate_icon_view_->SetToggled(language_state.IsPageTranslated());
1232}
1233
1234bool LocationBarView::RefreshManagePasswordsIconView() {
1235  DCHECK(manage_passwords_icon_view_);
1236  WebContents* web_contents = GetWebContents();
1237  if (!web_contents)
1238    return false;
1239  const bool was_visible = manage_passwords_icon_view_->visible();
1240  ManagePasswordsUIController::FromWebContents(
1241      web_contents)->UpdateIconAndBubbleState(manage_passwords_icon_view_);
1242  return was_visible != manage_passwords_icon_view_->visible();
1243}
1244
1245void LocationBarView::ShowFirstRunBubbleInternal() {
1246  // First run bubble doesn't make sense for Chrome OS.
1247#if !defined(OS_CHROMEOS)
1248  WebContents* web_contents = delegate_->GetWebContents();
1249  if (!web_contents)
1250    return;
1251  Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
1252  if (browser)
1253    FirstRunBubble::ShowBubble(browser, location_icon_view_);
1254#endif
1255}
1256
1257bool LocationBarView::HasValidSuggestText() const {
1258  return suggested_text_view_->visible() &&
1259      !suggested_text_view_->size().IsEmpty();
1260}
1261
1262bool LocationBarView::ShouldShowKeywordBubble() const {
1263  return !omnibox_view_->model()->keyword().empty() &&
1264      !omnibox_view_->model()->is_keyword_hint();
1265}
1266
1267bool LocationBarView::ShouldShowEVBubble() const {
1268  return !chrome::ShouldDisplayOriginChip() &&
1269      (GetToolbarModel()->GetSecurityLevel(false) == ToolbarModel::EV_SECURE);
1270}
1271
1272double LocationBarView::GetValueForAnimation(bool hide) const {
1273  int calculated_offset;
1274  const gfx::Tween::Type tween_type = hide ? kHideTweenType : kShowTweenType;
1275  int start_offset = starting_omnibox_offset_, end_offset = 0;
1276  if (hide)
1277    std::swap(start_offset, end_offset);
1278  const int desired_offset = abs(current_omnibox_offset_);
1279  // Binary-search the value space (0 <= value <= 1) to find the appropriate
1280  // position.  We only bother to iterate to within 1/64 of the desired value,
1281  // because the longer of the two animations will only run for twelve frames
1282  // anyway (200 ms * 60 Hz), so at this point we'll have a maximum error of
1283  // less than a fifth of an animation frame, which the user isn't going to
1284  // notice.
1285  //
1286  // We have to use this method because Tween::CalculateValue() is not
1287  // necessarily easily invertible.  Luckily, this only runs when the user
1288  // reverses the animation (rare), and the limit on how many iterations we'll
1289  // do ensures the cost is unnoticeable.
1290  double value = 0.5;
1291  double step = value / 2;
1292  do {
1293    calculated_offset = abs(gfx::Tween::IntValueBetween(
1294        gfx::Tween::CalculateValue(tween_type, value), start_offset,
1295        end_offset));
1296    if (calculated_offset < desired_offset)
1297      value += step;
1298    else if (calculated_offset > desired_offset)
1299      value -= step;
1300    step /= 2;
1301  } while ((calculated_offset != desired_offset) && (step >= (1.0 / 64)));
1302  return value;
1303}
1304
1305void LocationBarView::ResetShowAnimationAndColors() {
1306  show_url_animation_->Reset();
1307  omnibox_view_->UseDefaultSelectionTextColor();
1308  omnibox_view_->UseDefaultSelectionBackgroundColor();
1309}
1310
1311////////////////////////////////////////////////////////////////////////////////
1312// LocationBarView, private LocationBar implementation:
1313
1314void LocationBarView::ShowFirstRunBubble() {
1315  // Wait until search engines have loaded to show the first run bubble.
1316  TemplateURLService* url_service =
1317      TemplateURLServiceFactory::GetForProfile(profile());
1318  if (!url_service->loaded()) {
1319    template_url_service_ = url_service;
1320    template_url_service_->AddObserver(this);
1321    template_url_service_->Load();
1322    return;
1323  }
1324  ShowFirstRunBubbleInternal();
1325}
1326
1327GURL LocationBarView::GetDestinationURL() const {
1328  return destination_url();
1329}
1330
1331WindowOpenDisposition LocationBarView::GetWindowOpenDisposition() const {
1332  return disposition();
1333}
1334
1335content::PageTransition LocationBarView::GetPageTransition() const {
1336  return transition();
1337}
1338
1339void LocationBarView::AcceptInput() {
1340  omnibox_view_->model()->AcceptInput(CURRENT_TAB, false);
1341}
1342
1343void LocationBarView::FocusSearch() {
1344  omnibox_view_->SetFocus();
1345  omnibox_view_->SetForcedQuery();
1346}
1347
1348void LocationBarView::UpdateContentSettingsIcons() {
1349  if (RefreshContentSettingViews()) {
1350    Layout();
1351    SchedulePaint();
1352  }
1353}
1354
1355void LocationBarView::UpdateManagePasswordsIconAndBubble() {
1356  if (RefreshManagePasswordsIconView()) {
1357    Layout();
1358    SchedulePaint();
1359  }
1360}
1361
1362void LocationBarView::UpdatePageActions() {
1363  if (RefreshPageActionViews()) {  // Changed.
1364    Layout();
1365    SchedulePaint();
1366  }
1367}
1368
1369void LocationBarView::InvalidatePageActions() {
1370  DeletePageActionViews();
1371}
1372
1373void LocationBarView::UpdateOpenPDFInReaderPrompt() {
1374  open_pdf_in_reader_view_->Update(
1375      GetToolbarModel()->input_in_progress() ? NULL : GetWebContents());
1376  Layout();
1377  SchedulePaint();
1378}
1379
1380void LocationBarView::UpdateGeneratedCreditCardView() {
1381  generated_credit_card_view_->Update();
1382  Layout();
1383  SchedulePaint();
1384}
1385
1386void LocationBarView::SaveStateToContents(WebContents* contents) {
1387  // If we're about to switch tabs, complete any current animations, so that if
1388  // the user is in the midst of hiding the URL, when he returns to this tab,
1389  // the URL will be hidden rather than shown.
1390  // NOTE: This must be called before SaveStateToTab().
1391  EndOriginChipAnimations(true);
1392  omnibox_view_->SaveStateToTab(contents);
1393}
1394
1395const OmniboxView* LocationBarView::GetOmniboxView() const {
1396  return omnibox_view_;
1397}
1398
1399LocationBarTesting* LocationBarView::GetLocationBarForTesting() {
1400  return this;
1401}
1402
1403////////////////////////////////////////////////////////////////////////////////
1404// LocationBarView, private LocationBarTesting implementation:
1405
1406int LocationBarView::PageActionCount() {
1407  return page_action_views_.size();
1408}
1409
1410int LocationBarView::PageActionVisibleCount() {
1411  int result = 0;
1412  for (size_t i = 0; i < page_action_views_.size(); i++) {
1413    if (page_action_views_[i]->visible())
1414      ++result;
1415  }
1416  return result;
1417}
1418
1419ExtensionAction* LocationBarView::GetPageAction(size_t index) {
1420  if (index < page_action_views_.size())
1421    return page_action_views_[index]->image_view()->extension_action();
1422
1423  NOTREACHED();
1424  return NULL;
1425}
1426
1427ExtensionAction* LocationBarView::GetVisiblePageAction(size_t index) {
1428  size_t current = 0;
1429  for (size_t i = 0; i < page_action_views_.size(); ++i) {
1430    if (page_action_views_[i]->visible()) {
1431      if (current == index)
1432        return page_action_views_[i]->image_view()->extension_action();
1433
1434      ++current;
1435    }
1436  }
1437
1438  NOTREACHED();
1439  return NULL;
1440}
1441
1442void LocationBarView::TestPageActionPressed(size_t index) {
1443  size_t current = 0;
1444  for (size_t i = 0; i < page_action_views_.size(); ++i) {
1445    if (page_action_views_[i]->visible()) {
1446      if (current == index) {
1447        page_action_views_[i]->image_view()->view_controller()->
1448            ExecuteAction(ExtensionPopup::SHOW, true);
1449        return;
1450      }
1451      ++current;
1452    }
1453  }
1454
1455  NOTREACHED();
1456}
1457
1458bool LocationBarView::GetBookmarkStarVisibility() {
1459  DCHECK(star_view_);
1460  return star_view_->visible();
1461}
1462
1463////////////////////////////////////////////////////////////////////////////////
1464// LocationBarView, private views::View implementation:
1465
1466const char* LocationBarView::GetClassName() const {
1467  return kViewClassName;
1468}
1469
1470void LocationBarView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
1471  InstantServiceFactory::GetForProfile(profile())->OnOmniboxStartMarginChanged(
1472      bounds().x());
1473
1474  OmniboxPopupView* popup = omnibox_view_->model()->popup_model()->view();
1475  if (popup->IsOpen())
1476    popup->UpdatePopupAppearance();
1477}
1478
1479void LocationBarView::OnFocus() {
1480  // Explicitly focus the omnibox so a focus ring will be displayed around it on
1481  // Windows.
1482  omnibox_view_->SetFocus();
1483}
1484
1485void LocationBarView::OnPaint(gfx::Canvas* canvas) {
1486  View::OnPaint(canvas);
1487
1488  // Fill the location bar background color behind the border.  Parts of the
1489  // border images are meant to rest atop the toolbar background and parts atop
1490  // the omnibox background, so we can't just blindly fill our entire bounds.
1491  gfx::Rect bounds(GetContentsBounds());
1492  bounds.Inset(GetHorizontalEdgeThickness(), vertical_edge_thickness());
1493  SkColor color(GetColor(ToolbarModel::NONE, BACKGROUND));
1494  if (is_popup_mode_) {
1495    canvas->FillRect(bounds, color);
1496  } else {
1497    SkPaint paint;
1498    paint.setStyle(SkPaint::kFill_Style);
1499    paint.setColor(color);
1500    const int kBorderCornerRadius = 2;
1501    canvas->DrawRoundRect(bounds, kBorderCornerRadius, paint);
1502  }
1503
1504  // The border itself will be drawn in PaintChildren() since it includes an
1505  // inner shadow which should be drawn over the contents.
1506}
1507
1508void LocationBarView::PaintChildren(gfx::Canvas* canvas,
1509                                    const views::CullSet& cull_set) {
1510  // Paint all the children except for the omnibox itself, which may need to be
1511  // clipped if it's animating in, and the origin chip and the search button,
1512  // which will be painted after the border.
1513  for (int i = 0, count = child_count(); i < count; ++i) {
1514    views::View* child = child_at(i);
1515    if (!child->layer() && (child != omnibox_view_) &&
1516        (child != origin_chip_view_) && (child != search_button_))
1517      child->Paint(canvas, cull_set);
1518  }
1519
1520  {
1521    gfx::ScopedCanvas scoped_canvas(canvas);
1522    if (show_url_animation_->is_animating() ||
1523        hide_url_animation_->is_animating()) {
1524      gfx::Rect clip_rect(omnibox_view_->bounds());
1525      clip_rect.Inset(current_omnibox_leading_inset_, 0, 0, 0);
1526      clip_rect.set_width(current_omnibox_width_);
1527      clip_rect.set_x(GetMirroredXForRect(clip_rect));
1528      canvas->ClipRect(clip_rect);
1529    }
1530    omnibox_view_->Paint(canvas, cull_set);
1531  }
1532
1533  // For non-InstantExtendedAPI cases, if necessary, show focus rect. As we need
1534  // the focus rect to appear on top of children we paint here rather than
1535  // OnPaint().
1536  // Note: |Canvas::DrawFocusRect| paints a dashed rect with gray color.
1537  if (show_focus_rect_ && HasFocus())
1538    canvas->DrawFocusRect(omnibox_view_->bounds());
1539
1540  // Maximized popup windows don't draw the horizontal edges.  We implement this
1541  // by simply expanding the paint area outside the view by the edge thickness.
1542  gfx::Rect border_rect(GetContentsBounds());
1543  if (is_popup_mode_ && (GetHorizontalEdgeThickness() == 0))
1544    border_rect.Inset(-kPopupEdgeThickness, 0);
1545  views::Painter::PaintPainterAt(canvas, border_painter_.get(), border_rect);
1546
1547  // The origin chip and the search button must be painted after the border so
1548  // that the border shadow is not drawn over them.
1549  origin_chip_view_->Paint(canvas, cull_set);
1550  search_button_->Paint(canvas, cull_set);
1551}
1552
1553////////////////////////////////////////////////////////////////////////////////
1554// LocationBarView, private views::ButtonListener implementation:
1555
1556void LocationBarView::ButtonPressed(views::Button* sender,
1557                                    const ui::Event& event) {
1558  if (sender == mic_search_view_) {
1559    command_updater()->ExecuteCommand(IDC_TOGGLE_SPEECH_INPUT);
1560    return;
1561  }
1562
1563  DCHECK_EQ(search_button_, sender);
1564  // TODO(pkasting): When macourteau adds UMA stats for this, wire them up here.
1565  omnibox_view_->model()->AcceptInput(
1566      ui::DispositionFromEventFlags(event.flags()), false);
1567}
1568
1569////////////////////////////////////////////////////////////////////////////////
1570// LocationBarView, private views::DragController implementation:
1571
1572void LocationBarView::WriteDragDataForView(views::View* sender,
1573                                           const gfx::Point& press_pt,
1574                                           OSExchangeData* data) {
1575  DCHECK_NE(GetDragOperationsForView(sender, press_pt),
1576            ui::DragDropTypes::DRAG_NONE);
1577
1578  WebContents* web_contents = GetWebContents();
1579  FaviconTabHelper* favicon_tab_helper =
1580      FaviconTabHelper::FromWebContents(web_contents);
1581  gfx::ImageSkia favicon = favicon_tab_helper->GetFavicon().AsImageSkia();
1582  button_drag_utils::SetURLAndDragImage(web_contents->GetURL(),
1583                                        web_contents->GetTitle(),
1584                                        favicon,
1585                                        NULL,
1586                                        data,
1587                                        sender->GetWidget());
1588}
1589
1590int LocationBarView::GetDragOperationsForView(views::View* sender,
1591                                              const gfx::Point& p) {
1592  DCHECK((sender == location_icon_view_) || (sender == ev_bubble_view_) ||
1593         (sender == origin_chip_view_));
1594  WebContents* web_contents = delegate_->GetWebContents();
1595  return (web_contents && web_contents->GetURL().is_valid() &&
1596          (!GetOmniboxView()->IsEditingOrEmpty() ||
1597           sender == origin_chip_view_)) ?
1598      (ui::DragDropTypes::DRAG_COPY | ui::DragDropTypes::DRAG_LINK) :
1599      ui::DragDropTypes::DRAG_NONE;
1600}
1601
1602bool LocationBarView::CanStartDragForView(View* sender,
1603                                          const gfx::Point& press_pt,
1604                                          const gfx::Point& p) {
1605  return true;
1606}
1607
1608////////////////////////////////////////////////////////////////////////////////
1609// LocationBarView, private OmniboxEditController implementation:
1610
1611void LocationBarView::OnChanged() {
1612  int icon_id = omnibox_view_->GetIcon();
1613  location_icon_view_->SetImage(GetThemeProvider()->GetImageSkiaNamed(icon_id));
1614  location_icon_view_->ShowTooltip(!GetOmniboxView()->IsEditingOrEmpty());
1615
1616  ToolbarModel* toolbar_model = GetToolbarModel();
1617  chrome::DisplaySearchButtonConditions conditions =
1618      chrome::GetDisplaySearchButtonConditions();
1619  bool meets_conditions =
1620      (conditions == chrome::DISPLAY_SEARCH_BUTTON_ALWAYS) ||
1621      ((conditions != chrome::DISPLAY_SEARCH_BUTTON_NEVER) &&
1622       (toolbar_model->WouldPerformSearchTermReplacement(true) ||
1623        ((conditions == chrome::DISPLAY_SEARCH_BUTTON_FOR_STR_OR_IIP) &&
1624         toolbar_model->input_in_progress())));
1625  search_button_->SetVisible(!is_popup_mode_ && meets_conditions);
1626  search_button_->UpdateIcon(icon_id == IDR_OMNIBOX_SEARCH);
1627
1628  origin_chip_view_->OnChanged();
1629
1630  Layout();
1631  SchedulePaint();
1632}
1633
1634void LocationBarView::OnSetFocus() {
1635  GetFocusManager()->SetFocusedView(this);
1636}
1637
1638InstantController* LocationBarView::GetInstant() {
1639  return delegate_->GetInstant();
1640}
1641
1642const ToolbarModel* LocationBarView::GetToolbarModel() const {
1643  return delegate_->GetToolbarModel();
1644}
1645
1646void LocationBarView::HideURL() {
1647  DCHECK(chrome::ShouldDisplayOriginChip());
1648
1649  // If we're currently showing, reverse the hide by swapping to the hide
1650  // animation, offset so that the text is in the same position.
1651  if (show_url_animation_->is_animating()) {
1652    const double hide_value = GetValueForAnimation(true);
1653    ResetShowAnimationAndColors();
1654    hide_url_animation_->Show();
1655    // This must be done after calling Show() and is not equivalent to Reset(n);
1656    // see comments in ShowURL().
1657    hide_url_animation_->SetCurrentValue(hide_value);
1658  } else {
1659    hide_url_animation_->Show();
1660  }
1661}
1662
1663////////////////////////////////////////////////////////////////////////////////
1664// LocationBarView, private DropdownBarHostDelegate implementation:
1665
1666void LocationBarView::SetFocusAndSelection(bool select_all) {
1667  FocusLocation(select_all);
1668}
1669
1670void LocationBarView::SetAnimationOffset(int offset) {
1671  dropdown_animation_offset_ = offset;
1672}
1673
1674////////////////////////////////////////////////////////////////////////////////
1675// LocationBarView, private gfx::AnimationDelegate implementation:
1676
1677void LocationBarView::AnimationProgressed(const gfx::Animation* animation) {
1678  DCHECK((animation == show_url_animation_.get()) ||
1679      (animation == hide_url_animation_.get()));
1680  Layout();
1681  SchedulePaint();
1682}
1683
1684void LocationBarView::AnimationEnded(const gfx::Animation* animation) {
1685  if (animation == show_url_animation_.get()) {
1686    ResetShowAnimationAndColors();
1687    Layout();
1688    SchedulePaint();
1689  } else {
1690    DCHECK(animation == hide_url_animation_.get());
1691    hide_url_animation_->Reset();
1692    origin_chip_view_->FadeIn();
1693    omnibox_view_->HideURL();  // Calls OnChanged(), triggering layout.
1694  }
1695}
1696
1697////////////////////////////////////////////////////////////////////////////////
1698// LocationBarView, private TemplateURLServiceObserver implementation:
1699
1700void LocationBarView::OnTemplateURLServiceChanged() {
1701  template_url_service_->RemoveObserver(this);
1702  template_url_service_ = NULL;
1703  // If the browser is no longer active, let's not show the info bubble, as this
1704  // would make the browser the active window again.
1705  if (omnibox_view_ && omnibox_view_->GetWidget()->IsActive())
1706    ShowFirstRunBubble();
1707}
1708
1709////////////////////////////////////////////////////////////////////////////////
1710// LocationBarView, private content::NotificationObserver implementation:
1711
1712void LocationBarView::Observe(int type,
1713                              const content::NotificationSource& source,
1714                              const content::NotificationDetails& details) {
1715  switch (type) {
1716    case extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED:
1717    case extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED:
1718      Update(NULL);
1719      break;
1720
1721    default:
1722      NOTREACHED() << "Unexpected notification.";
1723  }
1724}
1725
1726////////////////////////////////////////////////////////////////////////////////
1727// LocationBarView, private SearchModelObserver implementation:
1728
1729void LocationBarView::ModelChanged(const SearchModel::State& old_state,
1730                                   const SearchModel::State& new_state) {
1731  const bool visible = !GetToolbarModel()->input_in_progress() &&
1732      new_state.voice_search_supported;
1733  if (mic_search_view_->visible() != visible) {
1734    mic_search_view_->SetVisible(visible);
1735    Layout();
1736  }
1737}
1738