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"
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_service.h"
21 #include "chrome/browser/extensions/location_bar_controller.h"
22 #include "chrome/browser/extensions/script_bubble_controller.h"
23 #include "chrome/browser/extensions/tab_helper.h"
24 #include "chrome/browser/favicon/favicon_tab_helper.h"
25 #include "chrome/browser/search/search.h"
26 #include "chrome/browser/search_engines/template_url.h"
27 #include "chrome/browser/search_engines/template_url_service.h"
28 #include "chrome/browser/search_engines/template_url_service_factory.h"
29 #include "chrome/browser/translate/translate_manager.h"
30 #include "chrome/browser/translate/translate_tab_helper.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/location_bar_util.h"
36 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h"
37 #include "chrome/browser/ui/omnibox/omnibox_popup_view.h"
38 #include "chrome/browser/ui/passwords/manage_passwords_bubble_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/bookmarks/bookmark_prompt_view.h"
42 #include "chrome/browser/ui/views/browser_dialogs.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/page_action_image_view.h"
51 #include "chrome/browser/ui/views/location_bar/page_action_with_badge_view.h"
52 #include "chrome/browser/ui/views/location_bar/script_bubble_icon_view.h"
53 #include "chrome/browser/ui/views/location_bar/selected_keyword_view.h"
54 #include "chrome/browser/ui/views/location_bar/star_view.h"
55 #include "chrome/browser/ui/views/location_bar/translate_icon_view.h"
56 #include "chrome/browser/ui/views/location_bar/zoom_bubble_view.h"
57 #include "chrome/browser/ui/views/location_bar/zoom_view.h"
58 #include "chrome/browser/ui/views/passwords/manage_passwords_bubble_view.h"
59 #include "chrome/browser/ui/views/passwords/manage_passwords_icon_view.h"
60 #include "chrome/browser/ui/views/toolbar/site_chip_view.h"
61 #include "chrome/browser/ui/zoom/zoom_controller.h"
62 #include "chrome/common/chrome_switches.h"
63 #include "chrome/common/pref_names.h"
64 #include "content/public/browser/notification_service.h"
65 #include "content/public/browser/render_widget_host_view.h"
66 #include "content/public/browser/web_contents.h"
67 #include "extensions/common/feature_switch.h"
68 #include "extensions/common/permissions/permissions_data.h"
69 #include "grit/generated_resources.h"
70 #include "grit/theme_resources.h"
71 #include "ui/base/accessibility/accessible_view_state.h"
72 #include "ui/base/dragdrop/drag_drop_types.h"
73 #include "ui/base/l10n/l10n_util.h"
74 #include "ui/base/layout.h"
75 #include "ui/base/resource/resource_bundle.h"
76 #include "ui/base/theme_provider.h"
77 #include "ui/events/event.h"
78 #include "ui/gfx/canvas.h"
79 #include "ui/gfx/color_utils.h"
80 #include "ui/gfx/image/image.h"
81 #include "ui/gfx/image/image_skia_operations.h"
82 #include "ui/gfx/skia_util.h"
83 #include "ui/gfx/text_utils.h"
84 #include "ui/native_theme/native_theme.h"
85 #include "ui/views/background.h"
86 #include "ui/views/border.h"
87 #include "ui/views/button_drag_utils.h"
88 #include "ui/views/controls/button/image_button.h"
89 #include "ui/views/controls/button/label_button.h"
90 #include "ui/views/controls/button/label_button_border.h"
91 #include "ui/views/controls/label.h"
92 #include "ui/views/widget/widget.h"
94 #if !defined(OS_CHROMEOS)
95 #include "chrome/browser/ui/views/first_run_bubble.h"
98 using content::WebContents
;
103 #if !defined(OS_CHROMEOS)
104 Browser
* GetBrowserFromDelegate(LocationBarView::Delegate
* delegate
) {
105 WebContents
* contents
= delegate
->GetWebContents();
106 return contents
? chrome::FindBrowserWithWebContents(contents
) : NULL
;
110 // Given a containing |height| and a |base_font_list|, shrinks the font size
111 // until the font list will fit within |height| while having its cap height
112 // vertically centered. Returns the correctly-sized font list.
114 // The expected layout:
115 // +--------+-----------------------------------------------+------------+
116 // | | y offset | space |
117 // | +--------+-------------------+------------------+ above |
118 // | | | | internal leading | cap height |
119 // | box | font | ascent (baseline) +------------------+------------+
120 // | height | height | | cap height |
121 // | | |-------------------+------------------+------------+
122 // | | | descent (height - baseline) | space |
123 // | +--------+--------------------------------------+ below |
124 // | | space at bottom | cap height |
125 // +--------+-----------------------------------------------+------------+
127 // center of box height == center of cap height
128 // (i.e. space above cap height == space below cap height)
131 // space at bottom >= 0
132 // (i.e. Entire font must be visible inside the box.)
133 gfx::FontList
GetLargestFontListWithHeightBound(
134 const gfx::FontList
& base_font_list
,
136 gfx::FontList font_list
= base_font_list
;
137 for (int font_size
= font_list
.GetFontSize(); font_size
> 1; --font_size
) {
138 const int internal_leading
=
139 font_list
.GetBaseline() - font_list
.GetCapHeight();
140 // Some platforms don't support getting the cap height, and simply return
141 // the entire font ascent from GetCapHeight(). Centering the ascent makes
142 // the font look too low, so if GetCapHeight() returns the ascent, center
143 // the entire font height instead.
145 height
- ((internal_leading
!= 0) ?
146 font_list
.GetCapHeight() : font_list
.GetHeight());
147 const int y_offset
= space
/ 2 - internal_leading
;
148 const int space_at_bottom
= height
- (y_offset
+ font_list
.GetHeight());
149 if ((y_offset
>= 0) && (space_at_bottom
>= 0))
151 font_list
= font_list
.DeriveFontListWithSizeDelta(-1);
156 // Functor for moving BookmarkManagerPrivate page actions to the right via
158 class IsPageActionViewRightAligned
{
160 explicit IsPageActionViewRightAligned(ExtensionService
* extension_service
)
161 : extension_service_(extension_service
) {}
163 bool operator()(PageActionWithBadgeView
* page_action_view
) {
164 return extensions::PermissionsData::HasAPIPermission(
165 extension_service_
->GetExtensionById(
166 page_action_view
->image_view()->page_action()->extension_id(),
168 extensions::APIPermission::kBookmarkManagerPrivate
);
172 ExtensionService
* extension_service_
;
174 // NOTE: Can't DISALLOW_COPY_AND_ASSIGN as we pass this object by value to
175 // std::stable_partition().
181 // LocationBarView -----------------------------------------------------------
184 const int LocationBarView::kNormalEdgeThickness
= 2;
185 const int LocationBarView::kPopupEdgeThickness
= 1;
186 const int LocationBarView::kIconInternalPadding
= 2;
187 const int LocationBarView::kBubblePadding
= 1;
188 const char LocationBarView::kViewClassName
[] = "LocationBarView";
190 LocationBarView::LocationBarView(Browser
* browser
,
192 CommandUpdater
* command_updater
,
195 : LocationBar(profile
),
196 OmniboxEditController(command_updater
),
200 location_icon_view_(NULL
),
201 ev_bubble_view_(NULL
),
202 ime_inline_autocomplete_view_(NULL
),
203 selected_keyword_view_(NULL
),
204 suggested_text_view_(NULL
),
205 keyword_hint_view_(NULL
),
206 mic_search_view_(NULL
),
208 generated_credit_card_view_(NULL
),
209 open_pdf_in_reader_view_(NULL
),
210 manage_passwords_icon_view_(NULL
),
211 script_bubble_icon_view_(NULL
),
212 site_chip_view_(NULL
),
213 translate_icon_view_(NULL
),
215 search_button_(NULL
),
216 is_popup_mode_(is_popup_mode
),
217 show_focus_rect_(false),
218 template_url_service_(NULL
),
219 animation_offset_(0),
220 weak_ptr_factory_(this) {
221 edit_bookmarks_enabled_
.Init(
222 prefs::kEditBookmarksEnabled
, profile
->GetPrefs(),
223 base::Bind(&LocationBarView::Update
, base::Unretained(this),
224 static_cast<content::WebContents
*>(NULL
)));
227 browser_
->search_model()->AddObserver(this);
230 LocationBarView::~LocationBarView() {
231 if (template_url_service_
)
232 template_url_service_
->RemoveObserver(this);
234 browser_
->search_model()->RemoveObserver(this);
238 void LocationBarView::InitTouchableLocationBarChildView(views::View
* view
) {
239 int horizontal_padding
= GetBuiltInHorizontalPaddingForChildViews();
240 if (horizontal_padding
!= 0) {
241 view
->set_border(views::Border::CreateEmptyBorder(
242 3, horizontal_padding
, 3, horizontal_padding
));
246 void LocationBarView::Init() {
247 // We need to be in a Widget, otherwise GetNativeTheme() may change and we're
248 // not prepared for that.
251 const int kOmniboxPopupBorderImages
[] =
252 IMAGE_GRID(IDR_OMNIBOX_POPUP_BORDER_AND_SHADOW
);
253 const int kOmniboxBorderImages
[] =
254 IMAGE_GRID(IDR_OMNIBOX_BORDER_AND_SHADOW
);
255 border_painter_
.reset(views::Painter::CreateImageGridPainter(
256 is_popup_mode_
? kOmniboxPopupBorderImages
: kOmniboxBorderImages
));
258 location_icon_view_
= new LocationIconView(this);
259 location_icon_view_
->set_drag_controller(this);
260 AddChildView(location_icon_view_
);
262 // Determine the main font.
263 gfx::FontList font_list
= ResourceBundle::GetSharedInstance().GetFontList(
264 ResourceBundle::BaseFont
);
265 const int current_font_size
= font_list
.GetFontSize();
266 const int desired_font_size
= browser_defaults::kOmniboxFontPixelSize
;
267 if (current_font_size
< desired_font_size
)
268 font_list
= font_list
.DeriveFontListWithSize(desired_font_size
);
269 // Shrink large fonts to make them fit.
270 // TODO(pkasting): Stretch the location bar instead in this case.
271 const int location_height
= GetInternalHeight(true);
272 font_list
= GetLargestFontListWithHeightBound(font_list
, location_height
);
274 // Determine the font for use inside the bubbles. The bubble background
275 // images have 1 px thick edges, which we don't want to overlap.
276 const int kBubbleInteriorVerticalPadding
= 1;
277 const int bubble_vertical_padding
=
278 (kBubblePadding
+ kBubbleInteriorVerticalPadding
) * 2;
279 const gfx::FontList
bubble_font_list(
280 GetLargestFontListWithHeightBound(
281 font_list
, location_height
- bubble_vertical_padding
));
283 const SkColor background_color
=
284 GetColor(ToolbarModel::NONE
, LocationBarView::BACKGROUND
);
285 ev_bubble_view_
= new EVBubbleView(
286 bubble_font_list
, GetColor(ToolbarModel::EV_SECURE
, SECURITY_TEXT
),
287 background_color
, this);
288 ev_bubble_view_
->set_drag_controller(this);
289 AddChildView(ev_bubble_view_
);
291 // Initialize the Omnibox view.
292 omnibox_view_
= new OmniboxViewViews(
293 this, profile(), command_updater(),
295 (browser_
->is_app() && CommandLine::ForCurrentProcess()->
296 HasSwitch(switches::kEnableStreamlinedHostedApps
)),
298 omnibox_view_
->Init();
299 omnibox_view_
->SetFocusable(true);
300 AddChildView(omnibox_view_
);
302 // Initialize the inline autocomplete view which is visible only when IME is
303 // turned on. Use the same font with the omnibox and highlighted background.
304 ime_inline_autocomplete_view_
= new views::Label(base::string16(), font_list
);
305 ime_inline_autocomplete_view_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
306 ime_inline_autocomplete_view_
->SetAutoColorReadabilityEnabled(false);
307 ime_inline_autocomplete_view_
->set_background(
308 views::Background::CreateSolidBackground(GetNativeTheme()->GetSystemColor(
309 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused
)));
310 ime_inline_autocomplete_view_
->SetEnabledColor(
311 GetNativeTheme()->GetSystemColor(
312 ui::NativeTheme::kColorId_TextfieldSelectionColor
));
313 ime_inline_autocomplete_view_
->SetVisible(false);
314 AddChildView(ime_inline_autocomplete_view_
);
316 const SkColor text_color
= GetColor(ToolbarModel::NONE
, TEXT
);
317 selected_keyword_view_
= new SelectedKeywordView(
318 bubble_font_list
, text_color
, background_color
, profile());
319 AddChildView(selected_keyword_view_
);
321 suggested_text_view_
= new views::Label(base::string16(), font_list
);
322 suggested_text_view_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
323 suggested_text_view_
->SetAutoColorReadabilityEnabled(false);
324 suggested_text_view_
->SetEnabledColor(GetColor(
325 ToolbarModel::NONE
, LocationBarView::DEEMPHASIZED_TEXT
));
326 suggested_text_view_
->SetVisible(false);
327 AddChildView(suggested_text_view_
);
329 keyword_hint_view_
= new KeywordHintView(
330 profile(), font_list
,
331 GetColor(ToolbarModel::NONE
, LocationBarView::DEEMPHASIZED_TEXT
),
333 AddChildView(keyword_hint_view_
);
335 mic_search_view_
= new views::ImageButton(this);
336 mic_search_view_
->set_id(VIEW_ID_MIC_SEARCH_BUTTON
);
337 mic_search_view_
->SetAccessibilityFocusable(true);
338 mic_search_view_
->SetTooltipText(
339 l10n_util::GetStringUTF16(IDS_TOOLTIP_MIC_SEARCH
));
340 mic_search_view_
->SetImage(
341 views::Button::STATE_NORMAL
,
342 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
343 IDR_OMNIBOX_MIC_SEARCH
));
344 mic_search_view_
->SetImageAlignment(views::ImageButton::ALIGN_CENTER
,
345 views::ImageButton::ALIGN_MIDDLE
);
346 mic_search_view_
->SetVisible(false);
347 InitTouchableLocationBarChildView(mic_search_view_
);
348 AddChildView(mic_search_view_
);
350 for (int i
= 0; i
< CONTENT_SETTINGS_NUM_TYPES
; ++i
) {
351 ContentSettingImageView
* content_blocked_view
=
352 new ContentSettingImageView(static_cast<ContentSettingsType
>(i
), this,
353 bubble_font_list
, text_color
,
355 content_setting_views_
.push_back(content_blocked_view
);
356 content_blocked_view
->SetVisible(false);
357 AddChildView(content_blocked_view
);
360 generated_credit_card_view_
= new GeneratedCreditCardView(delegate_
);
361 AddChildView(generated_credit_card_view_
);
363 zoom_view_
= new ZoomView(delegate_
);
364 zoom_view_
->set_id(VIEW_ID_ZOOM_BUTTON
);
365 AddChildView(zoom_view_
);
367 open_pdf_in_reader_view_
= new OpenPDFInReaderView(this);
368 AddChildView(open_pdf_in_reader_view_
);
370 manage_passwords_icon_view_
= new ManagePasswordsIconView(delegate_
);
371 manage_passwords_icon_view_
->set_id(VIEW_ID_MANAGE_PASSWORDS_ICON_BUTTON
);
372 AddChildView(manage_passwords_icon_view_
);
374 script_bubble_icon_view_
= new ScriptBubbleIconView(delegate());
375 script_bubble_icon_view_
->SetVisible(false);
376 AddChildView(script_bubble_icon_view_
);
378 translate_icon_view_
= new TranslateIconView(command_updater());
379 translate_icon_view_
->SetVisible(false);
380 AddChildView(translate_icon_view_
);
382 star_view_
= new StarView(command_updater());
383 star_view_
->SetVisible(false);
384 AddChildView(star_view_
);
386 search_button_
= new views::LabelButton(this, base::string16());
387 search_button_
->set_triggerable_event_flags(
388 ui::EF_LEFT_MOUSE_BUTTON
| ui::EF_MIDDLE_MOUSE_BUTTON
);
389 search_button_
->SetStyle(views::Button::STYLE_BUTTON
);
390 search_button_
->SetFocusable(false);
391 search_button_
->set_min_size(gfx::Size());
392 views::LabelButtonBorder
* search_button_border
=
393 new views::LabelButtonBorder(search_button_
->style());
394 search_button_border
->set_insets(gfx::Insets());
395 const int kSearchButtonNormalImages
[] = IMAGE_GRID(IDR_OMNIBOX_SEARCH_BUTTON
);
396 search_button_border
->SetPainter(
397 false, views::Button::STATE_NORMAL
,
398 views::Painter::CreateImageGridPainter(kSearchButtonNormalImages
));
399 const int kSearchButtonHoveredImages
[] =
400 IMAGE_GRID(IDR_OMNIBOX_SEARCH_BUTTON_HOVER
);
401 search_button_border
->SetPainter(
402 false, views::Button::STATE_HOVERED
,
403 views::Painter::CreateImageGridPainter(kSearchButtonHoveredImages
));
404 const int kSearchButtonPressedImages
[] =
405 IMAGE_GRID(IDR_OMNIBOX_SEARCH_BUTTON_PRESSED
);
406 search_button_border
->SetPainter(
407 false, views::Button::STATE_PRESSED
,
408 views::Painter::CreateImageGridPainter(kSearchButtonPressedImages
));
409 search_button_border
->SetPainter(false, views::Button::STATE_DISABLED
, NULL
);
410 search_button_border
->SetPainter(true, views::Button::STATE_NORMAL
, NULL
);
411 search_button_border
->SetPainter(true, views::Button::STATE_HOVERED
, NULL
);
412 search_button_border
->SetPainter(true, views::Button::STATE_PRESSED
, NULL
);
413 search_button_border
->SetPainter(true, views::Button::STATE_DISABLED
, NULL
);
414 search_button_
->set_border(search_button_border
);
415 const int kSearchButtonWidth
= 56;
416 search_button_
->set_min_size(gfx::Size(kSearchButtonWidth
, 0));
417 search_button_
->SetVisible(false);
418 AddChildView(search_button_
);
420 content::Source
<Profile
> profile_source
= content::Source
<Profile
>(profile());
422 chrome::NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED
,
424 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED
, profile_source
);
425 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED
, profile_source
);
427 // Initialize the location entry. We do this to avoid a black flash which is
428 // visible when the location entry has just been initialized.
432 bool LocationBarView::IsInitialized() const {
433 return omnibox_view_
!= NULL
;
436 SkColor
LocationBarView::GetColor(ToolbarModel::SecurityLevel security_level
,
437 ColorKind kind
) const {
438 const ui::NativeTheme
* native_theme
= GetNativeTheme();
441 return native_theme
->GetSystemColor(
442 ui::NativeTheme::kColorId_TextfieldDefaultBackground
);
445 return native_theme
->GetSystemColor(
446 ui::NativeTheme::kColorId_TextfieldDefaultColor
);
449 return native_theme
->GetSystemColor(
450 ui::NativeTheme::kColorId_TextfieldSelectionColor
);
452 case DEEMPHASIZED_TEXT
:
453 return color_utils::AlphaBlend(
454 GetColor(security_level
, TEXT
),
455 GetColor(security_level
, BACKGROUND
),
458 case SECURITY_TEXT
: {
460 switch (security_level
) {
461 case ToolbarModel::EV_SECURE
:
462 case ToolbarModel::SECURE
:
463 color
= SkColorSetRGB(7, 149, 0);
466 case ToolbarModel::SECURITY_WARNING
:
467 case ToolbarModel::SECURITY_POLICY_WARNING
:
468 return GetColor(security_level
, DEEMPHASIZED_TEXT
);
471 case ToolbarModel::SECURITY_ERROR
:
472 color
= SkColorSetRGB(162, 0, 0);
477 return GetColor(security_level
, TEXT
);
479 return color_utils::GetReadableColor(
480 color
, GetColor(security_level
, BACKGROUND
));
485 return GetColor(security_level
, TEXT
);
489 void LocationBarView::GetOmniboxPopupPositioningInfo(
490 gfx::Point
* top_left_screen_coord
,
494 // Because the popup might appear atop the attached bookmark bar, there won't
495 // necessarily be a client edge separating it from the rest of the toolbar.
496 // Therefore we position the popup high enough so it can draw its own client
497 // edge at the top, in the same place the toolbar would normally draw the
499 *top_left_screen_coord
= gfx::Point(
501 parent()->height() - views::NonClientFrameView::kClientEdgeThickness
);
502 views::View::ConvertPointToScreen(parent(), top_left_screen_coord
);
503 *popup_width
= parent()->width();
505 gfx::Rect
location_bar_bounds(bounds());
506 location_bar_bounds
.Inset(kNormalEdgeThickness
, 0);
507 *left_margin
= location_bar_bounds
.x();
508 *right_margin
= *popup_width
- location_bar_bounds
.right();
512 int LocationBarView::GetItemPadding() {
513 const int kTouchItemPadding
= 8;
514 if (ui::GetDisplayLayout() == ui::LAYOUT_TOUCH
)
515 return kTouchItemPadding
;
517 const int kDesktopScriptBadgeItemPadding
= 9;
518 const int kDesktopItemPadding
= 3;
519 return extensions::FeatureSwitch::script_badges()->IsEnabled() ?
520 kDesktopScriptBadgeItemPadding
: kDesktopItemPadding
;
523 // DropdownBarHostDelegate
524 void LocationBarView::SetFocusAndSelection(bool select_all
) {
525 FocusLocation(select_all
);
528 void LocationBarView::SetAnimationOffset(int offset
) {
529 animation_offset_
= offset
;
532 void LocationBarView::UpdateContentSettingsIcons() {
533 if (RefreshContentSettingViews()) {
539 void LocationBarView::UpdateManagePasswordsIconAndBubble() {
540 if (RefreshManagePasswordsIconView()) {
544 ShowManagePasswordsBubbleIfNeeded();
547 void LocationBarView::UpdatePageActions() {
548 size_t count_before
= page_action_views_
.size();
549 bool changed
= RefreshPageActionViews();
550 changed
|= RefreshScriptBubble();
551 if (page_action_views_
.size() != count_before
) {
552 content::NotificationService::current()->Notify(
553 chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_COUNT_CHANGED
,
554 content::Source
<LocationBar
>(this),
555 content::NotificationService::NoDetails());
564 void LocationBarView::InvalidatePageActions() {
565 size_t count_before
= page_action_views_
.size();
566 DeletePageActionViews();
567 if (page_action_views_
.size() != count_before
) {
568 content::NotificationService::current()->Notify(
569 chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_COUNT_CHANGED
,
570 content::Source
<LocationBar
>(this),
571 content::NotificationService::NoDetails());
575 void LocationBarView::UpdateOpenPDFInReaderPrompt() {
576 open_pdf_in_reader_view_
->Update(
577 GetToolbarModel()->input_in_progress() ? NULL
: GetWebContents());
582 void LocationBarView::UpdateGeneratedCreditCardView() {
583 generated_credit_card_view_
->Update();
588 void LocationBarView::OnFocus() {
589 // Focus the view widget first which implements accessibility for
590 // Chrome OS. It is noop on Win. This should be removed once
591 // Chrome OS migrates to aura, which uses Views' textfield that receives
592 // focus. See crbug.com/106428.
593 NotifyAccessibilityEvent(ui::AccessibilityTypes::EVENT_FOCUS
, false);
595 // Then focus the native location view which implements accessibility for
597 omnibox_view_
->SetFocus();
600 void LocationBarView::SetPreviewEnabledPageAction(ExtensionAction
* page_action
,
601 bool preview_enabled
) {
606 WebContents
* contents
= delegate_
->GetWebContents();
608 RefreshPageActionViews();
609 PageActionWithBadgeView
* page_action_view
=
610 static_cast<PageActionWithBadgeView
*>(GetPageActionView(page_action
));
611 DCHECK(page_action_view
);
612 if (!page_action_view
)
615 page_action_view
->image_view()->set_preview_enabled(preview_enabled
);
616 page_action_view
->UpdateVisibility(contents
, GetToolbarModel()->GetURL());
621 views::View
* LocationBarView::GetPageActionView(ExtensionAction
*page_action
) {
623 for (PageActionViews::const_iterator
i(page_action_views_
.begin());
624 i
!= page_action_views_
.end(); ++i
) {
625 if ((*i
)->image_view()->page_action() == page_action
)
631 void LocationBarView::SetStarToggled(bool on
) {
633 star_view_
->SetToggled(on
);
636 void LocationBarView::SetTranslateIconToggled(bool on
) {
637 translate_icon_view_
->SetToggled(on
);
640 void LocationBarView::ShowBookmarkPrompt() {
641 if (star_view_
&& star_view_
->visible())
642 BookmarkPromptView::ShowPrompt(star_view_
, profile()->GetPrefs());
645 void LocationBarView::ZoomChangedForActiveTab(bool can_show_bubble
) {
647 if (RefreshZoomView()) {
652 if (can_show_bubble
&& zoom_view_
->visible() && delegate_
->GetWebContents())
653 ZoomBubbleView::ShowBubble(delegate_
->GetWebContents(), true);
656 gfx::Point
LocationBarView::GetOmniboxViewOrigin() const {
657 gfx::Point
origin(omnibox_view_
->bounds().origin());
658 // If the UI layout is RTL, the coordinate system is not transformed and
659 // therefore we need to adjust the X coordinate so that bubble appears on the
660 // right hand side of the location bar.
661 if (base::i18n::IsRTL())
662 origin
.set_x(width() - origin
.x());
663 views::View::ConvertPointToScreen(this, &origin
);
667 void LocationBarView::SetImeInlineAutocompletion(const base::string16
& text
) {
668 ime_inline_autocomplete_view_
->SetText(text
);
669 ime_inline_autocomplete_view_
->SetVisible(!text
.empty());
672 void LocationBarView::SetGrayTextAutocompletion(const base::string16
& text
) {
673 if (suggested_text_view_
->text() != text
) {
674 suggested_text_view_
->SetText(text
);
675 suggested_text_view_
->SetVisible(!text
.empty());
681 base::string16
LocationBarView::GetGrayTextAutocompletion() const {
682 return HasValidSuggestText() ? suggested_text_view_
->text()
686 gfx::Size
LocationBarView::GetPreferredSize() {
687 gfx::Size
background_min_size(border_painter_
->GetMinimumSize());
688 if (!IsInitialized())
689 return background_min_size
;
690 gfx::Size
search_button_min_size(search_button_
->GetMinimumSize());
691 gfx::Size
min_size(background_min_size
);
692 min_size
.SetToMax(search_button_min_size
);
694 background_min_size
.width() + search_button_min_size
.width());
698 void LocationBarView::Layout() {
699 if (!IsInitialized())
702 selected_keyword_view_
->SetVisible(false);
703 location_icon_view_
->SetVisible(false);
704 ev_bubble_view_
->SetVisible(false);
705 keyword_hint_view_
->SetVisible(false);
707 const int item_padding
= GetItemPadding();
708 // The textfield has 1 px of whitespace before the text in the RTL case only.
709 const int kEditLeadingInternalSpace
= base::i18n::IsRTL() ? 1 : 0;
710 LocationBarLayout
leading_decorations(
711 LocationBarLayout::LEFT_EDGE
, item_padding
- kEditLeadingInternalSpace
);
712 LocationBarLayout
trailing_decorations(LocationBarLayout::RIGHT_EDGE
,
715 const base::string16
keyword(omnibox_view_
->model()->keyword());
716 const bool is_keyword_hint(omnibox_view_
->model()->is_keyword_hint());
717 const int bubble_location_y
= vertical_edge_thickness() + kBubblePadding
;
718 // In some cases (e.g. fullscreen mode) we may have 0 height. We still want
719 // to position our child views in this case, because other things may be
720 // positioned relative to them (e.g. the "bookmark added" bubble if the user
722 const int location_height
= GetInternalHeight(false);
723 const int bubble_height
= std::max(location_height
- (kBubblePadding
* 2), 0);
724 if (!keyword
.empty() && !is_keyword_hint
) {
725 leading_decorations
.AddDecoration(bubble_location_y
, bubble_height
, true, 0,
726 kBubblePadding
, item_padding
, 0,
727 selected_keyword_view_
);
728 if (selected_keyword_view_
->keyword() != keyword
) {
729 selected_keyword_view_
->SetKeyword(keyword
);
730 const TemplateURL
* template_url
=
731 TemplateURLServiceFactory::GetForProfile(profile())->
732 GetTemplateURLForKeyword(keyword
);
734 (template_url
->GetType() == TemplateURL::OMNIBOX_API_EXTENSION
)) {
735 gfx::Image image
= extensions::OmniboxAPI::Get(profile())->
736 GetOmniboxIcon(template_url
->GetExtensionId());
737 selected_keyword_view_
->SetImage(image
.AsImageSkia());
738 selected_keyword_view_
->set_is_extension_icon(true);
740 selected_keyword_view_
->SetImage(
741 *(GetThemeProvider()->GetImageSkiaNamed(IDR_OMNIBOX_SEARCH
)));
742 selected_keyword_view_
->set_is_extension_icon(false);
745 } else if (!site_chip_view_
&&
746 (GetToolbarModel()->GetSecurityLevel(false) == ToolbarModel::EV_SECURE
)) {
747 ev_bubble_view_
->SetLabel(GetToolbarModel()->GetEVCertName());
748 // The largest fraction of the omnibox that can be taken by the EV bubble.
749 const double kMaxBubbleFraction
= 0.5;
750 leading_decorations
.AddDecoration(bubble_location_y
, bubble_height
, false,
751 kMaxBubbleFraction
, kBubblePadding
,
752 item_padding
, 0, ev_bubble_view_
);
754 leading_decorations
.AddDecoration(
755 vertical_edge_thickness(), location_height
,
756 GetBuiltInHorizontalPaddingForChildViews(),
757 location_icon_view_
);
760 if (star_view_
->visible()) {
761 trailing_decorations
.AddDecoration(
762 vertical_edge_thickness(), location_height
,
763 GetBuiltInHorizontalPaddingForChildViews(), star_view_
);
765 if (translate_icon_view_
->visible()) {
766 trailing_decorations
.AddDecoration(
767 vertical_edge_thickness(), location_height
,
768 GetBuiltInHorizontalPaddingForChildViews(),
769 translate_icon_view_
);
771 if (script_bubble_icon_view_
->visible()) {
772 trailing_decorations
.AddDecoration(
773 vertical_edge_thickness(), location_height
,
774 GetBuiltInHorizontalPaddingForChildViews(),
775 script_bubble_icon_view_
);
777 if (open_pdf_in_reader_view_
->visible()) {
778 trailing_decorations
.AddDecoration(
779 vertical_edge_thickness(), location_height
,
780 GetBuiltInHorizontalPaddingForChildViews(),
781 open_pdf_in_reader_view_
);
783 if (manage_passwords_icon_view_
->visible()) {
784 trailing_decorations
.AddDecoration(vertical_edge_thickness(),
786 manage_passwords_icon_view_
);
788 for (PageActionViews::const_iterator
i(page_action_views_
.begin());
789 i
!= page_action_views_
.end(); ++i
) {
790 if ((*i
)->visible()) {
791 trailing_decorations
.AddDecoration(
792 vertical_edge_thickness(), location_height
,
793 GetBuiltInHorizontalPaddingForChildViews(), (*i
));
796 if (zoom_view_
->visible()) {
797 trailing_decorations
.AddDecoration(vertical_edge_thickness(),
798 location_height
, 0, zoom_view_
);
800 for (ContentSettingViews::const_reverse_iterator
i(
801 content_setting_views_
.rbegin()); i
!= content_setting_views_
.rend();
803 if ((*i
)->visible()) {
804 trailing_decorations
.AddDecoration(
805 bubble_location_y
, bubble_height
, false, 0, item_padding
,
806 item_padding
, GetBuiltInHorizontalPaddingForChildViews(), (*i
));
809 if (generated_credit_card_view_
->visible()) {
810 trailing_decorations
.AddDecoration(vertical_edge_thickness(),
812 generated_credit_card_view_
);
814 if (mic_search_view_
->visible()) {
815 trailing_decorations
.AddDecoration(vertical_edge_thickness(),
816 location_height
, 0, mic_search_view_
);
818 // Because IMEs may eat the tab key, we don't show "press tab to search" while
819 // IME composition is in progress.
820 if (!keyword
.empty() && is_keyword_hint
&& !omnibox_view_
->IsImeComposing()) {
821 trailing_decorations
.AddDecoration(vertical_edge_thickness(),
822 location_height
, true, 0, item_padding
,
823 item_padding
, 0, keyword_hint_view_
);
824 if (keyword_hint_view_
->keyword() != keyword
)
825 keyword_hint_view_
->SetKeyword(keyword
);
829 const int horizontal_edge_thickness
= GetHorizontalEdgeThickness();
830 int full_width
= width() - horizontal_edge_thickness
;
831 // The search button images are made to look as if they overlay the normal
832 // edge images, but to align things, the search button needs to be inset
833 // horizontally by 1 px.
834 const int kSearchButtonInset
= 1;
835 const gfx::Size
search_button_size(search_button_
->GetPreferredSize());
836 const int search_button_reserved_width
=
837 search_button_size
.width() + kSearchButtonInset
;
838 full_width
-= search_button_
->visible() ?
839 search_button_reserved_width
: horizontal_edge_thickness
;
840 int entry_width
= full_width
;
841 leading_decorations
.LayoutPass1(&entry_width
);
842 trailing_decorations
.LayoutPass1(&entry_width
);
843 leading_decorations
.LayoutPass2(&entry_width
);
844 trailing_decorations
.LayoutPass2(&entry_width
);
846 int location_needed_width
= omnibox_view_
->GetTextWidth();
847 int available_width
= entry_width
- location_needed_width
;
848 // The bounds must be wide enough for all the decorations to fit.
849 gfx::Rect
location_bounds(
850 horizontal_edge_thickness
, vertical_edge_thickness(),
851 std::max(full_width
, full_width
- entry_width
), location_height
);
852 leading_decorations
.LayoutPass3(&location_bounds
, &available_width
);
853 trailing_decorations
.LayoutPass3(&location_bounds
, &available_width
);
855 // Layout out the suggested text view right aligned to the location
856 // entry. Only show the suggested text if we can fit the text from one
857 // character before the end of the selection to the end of the text and the
858 // suggested text. If we can't it means either the suggested text is too big,
859 // or the user has scrolled.
861 // TODO(sky): We could potentially adjust this to take into account suggested
862 // text to force using minimum size if necessary, but currently the chance of
863 // showing keyword hints and suggested text is minimal and we're not confident
864 // this is the right approach for suggested text.
866 int omnibox_view_margin
= 0;
867 if (suggested_text_view_
->visible()) {
868 // We do not display the suggested text when it contains a mix of RTL and
869 // LTR characters since this could mean the suggestion should be displayed
870 // in the middle of the string.
871 base::i18n::TextDirection text_direction
=
872 base::i18n::GetStringDirection(omnibox_view_
->GetText());
873 if (text_direction
!=
874 base::i18n::GetStringDirection(suggested_text_view_
->text()))
875 text_direction
= base::i18n::UNKNOWN_DIRECTION
;
877 // TODO(sky): need to layout when the user changes caret position.
878 gfx::Size
suggested_text_size(suggested_text_view_
->GetPreferredSize());
879 if (suggested_text_size
.width() > available_width
||
880 text_direction
== base::i18n::UNKNOWN_DIRECTION
) {
881 // Hide the suggested text if the user has scrolled or we can't fit all
882 // the suggested text, or we have a mix of RTL and LTR characters.
883 suggested_text_view_
->SetBounds(0, 0, 0, 0);
885 location_needed_width
=
886 std::min(location_needed_width
,
887 location_bounds
.width() - suggested_text_size
.width());
888 gfx::Rect
suggested_text_bounds(location_bounds
.x(), location_bounds
.y(),
889 suggested_text_size
.width(),
890 location_bounds
.height());
891 // TODO(sky): figure out why this needs the -1.
892 suggested_text_bounds
.Offset(location_needed_width
- 1, 0);
894 // We reverse the order of the location entry and suggested text if:
895 // - Chrome is RTL but the text is fully LTR, or
896 // - Chrome is LTR but the text is fully RTL.
897 // This ensures the suggested text is correctly displayed to the right
898 // (or left) of the user text.
899 if (text_direction
== (base::i18n::IsRTL() ?
900 base::i18n::LEFT_TO_RIGHT
: base::i18n::RIGHT_TO_LEFT
)) {
901 // TODO(sky): Figure out why we need the +1.
902 suggested_text_bounds
.set_x(location_bounds
.x() + 1);
903 // Use a margin to prevent omnibox text from overlapping suggest text.
904 omnibox_view_margin
= suggested_text_bounds
.width();
906 suggested_text_view_
->SetBoundsRect(suggested_text_bounds
);
910 const gfx::Insets insets
= omnibox_view_
->GetInsets();
911 omnibox_view_
->set_border(views::Border::CreateEmptyBorder(
912 insets
.top(), insets
.left(), insets
.bottom(), omnibox_view_margin
));
914 // Layout |ime_inline_autocomplete_view_| next to the user input.
915 if (ime_inline_autocomplete_view_
->visible()) {
917 gfx::GetStringWidth(ime_inline_autocomplete_view_
->text(),
918 ime_inline_autocomplete_view_
->font_list()) +
919 ime_inline_autocomplete_view_
->GetInsets().width();
920 // All the target languages (IMEs) are LTR, and we do not need to support
921 // RTL so far. In other words, no testable RTL environment so far.
922 int x
= location_needed_width
;
923 if (width
> entry_width
)
925 else if (location_needed_width
+ width
> entry_width
)
926 x
= entry_width
- width
;
927 location_bounds
.set_width(x
);
928 ime_inline_autocomplete_view_
->SetBounds(
929 location_bounds
.right(), location_bounds
.y(),
930 std::min(width
, entry_width
), location_bounds
.height());
933 omnibox_view_
->SetBoundsRect(location_bounds
);
935 search_button_
->SetBoundsRect(gfx::Rect(
936 gfx::Point(width() - search_button_reserved_width
, 0),
937 search_button_size
));
940 void LocationBarView::PaintChildren(gfx::Canvas
* canvas
) {
941 View::PaintChildren(canvas
);
943 // For non-InstantExtendedAPI cases, if necessary, show focus rect. As we need
944 // the focus rect to appear on top of children we paint here rather than
946 // Note: |Canvas::DrawFocusRect| paints a dashed rect with gray color.
947 if (show_focus_rect_
&& HasFocus())
948 canvas
->DrawFocusRect(omnibox_view_
->bounds());
950 // Maximized popup windows don't draw the horizontal edges. We implement this
951 // by simply expanding the paint area outside the view by the edge thickness.
952 gfx::Rect
border_rect(GetContentsBounds());
953 if (is_popup_mode_
&& (GetHorizontalEdgeThickness() == 0))
954 border_rect
.Inset(-kPopupEdgeThickness
, 0);
955 views::Painter::PaintPainterAt(canvas
, border_painter_
.get(), border_rect
);
958 void LocationBarView::OnPaint(gfx::Canvas
* canvas
) {
959 View::OnPaint(canvas
);
961 // Fill the location bar background color behind the border. Parts of the
962 // border images are meant to rest atop the toolbar background and parts atop
963 // the omnibox background, so we can't just blindly fill our entire bounds.
964 gfx::Rect
bounds(GetContentsBounds());
965 bounds
.Inset(GetHorizontalEdgeThickness(), vertical_edge_thickness());
966 SkColor
color(GetColor(ToolbarModel::NONE
, BACKGROUND
));
967 if (is_popup_mode_
) {
968 canvas
->FillRect(bounds
, color
);
971 paint
.setStyle(SkPaint::kFill_Style
);
972 paint
.setColor(color
);
973 const int kBorderCornerRadius
= 2;
974 canvas
->DrawRoundRect(bounds
, kBorderCornerRadius
, paint
);
977 // The border itself will be drawn in PaintChildren() since it includes an
978 // inner shadow which should be drawn over the contents.
981 PaintPageActionBackgrounds(canvas
);
984 void LocationBarView::SetShowFocusRect(bool show
) {
985 show_focus_rect_
= show
;
989 void LocationBarView::SelectAll() {
990 omnibox_view_
->SelectAll(true);
993 views::ImageView
* LocationBarView::GetLocationIconView() {
994 return site_chip_view_
?
995 site_chip_view_
->location_icon_view() : location_icon_view_
;
998 const views::ImageView
* LocationBarView::GetLocationIconView() const {
999 return site_chip_view_
?
1000 site_chip_view_
->location_icon_view() : location_icon_view_
;
1003 views::View
* LocationBarView::GetLocationBarAnchor() {
1004 return GetLocationIconView();
1007 gfx::Point
LocationBarView::GetLocationBarAnchorPoint() const {
1008 // The +1 in the next line creates a 1-px gap between icon and arrow tip.
1009 gfx::Point
icon_bottom(0, GetLocationIconView()->GetImageBounds().bottom() -
1010 LocationBarView::kIconInternalPadding
+ 1);
1011 gfx::Point
icon_center(GetLocationIconView()->GetImageBounds().CenterPoint());
1012 gfx::Point
point(icon_center
.x(), icon_bottom
.y());
1013 ConvertPointToTarget(GetLocationIconView(), this, &point
);
1017 views::View
* LocationBarView::generated_credit_card_view() {
1018 return generated_credit_card_view_
;
1021 void LocationBarView::Update(const WebContents
* contents
) {
1022 mic_search_view_
->SetVisible(
1023 !GetToolbarModel()->input_in_progress() && browser_
&&
1024 browser_
->search_model()->voice_search_supported());
1025 RefreshContentSettingViews();
1026 generated_credit_card_view_
->Update();
1027 ZoomBubbleView::CloseBubble();
1029 RefreshPageActionViews();
1030 RefreshScriptBubble();
1031 RefreshTranslateIcon();
1032 RefreshManagePasswordsIconView();
1033 open_pdf_in_reader_view_
->Update(
1034 GetToolbarModel()->input_in_progress() ? NULL
: GetWebContents());
1037 star_view_
->SetVisible(
1038 browser_defaults::bookmarks_enabled
&& !is_popup_mode_
&&
1039 !GetToolbarModel()->input_in_progress() &&
1040 edit_bookmarks_enabled_
.GetValue() &&
1041 !IsBookmarkStarHiddenByExtension());
1045 omnibox_view_
->OnTabChanged(contents
);
1047 omnibox_view_
->Update();
1049 OnChanged(); // NOTE: Calls Layout().
1052 void LocationBarView::OnChanged() {
1053 int icon_id
= omnibox_view_
->GetIcon();
1054 location_icon_view_
->SetImage(GetThemeProvider()->GetImageSkiaNamed(icon_id
));
1055 location_icon_view_
->ShowTooltip(!GetOmniboxView()->IsEditingOrEmpty());
1057 ToolbarModel
* toolbar_model
= GetToolbarModel();
1058 chrome::DisplaySearchButtonConditions conditions
=
1059 chrome::GetDisplaySearchButtonConditions();
1060 bool meets_conditions
=
1061 (conditions
== chrome::DISPLAY_SEARCH_BUTTON_ALWAYS
) ||
1062 ((conditions
!= chrome::DISPLAY_SEARCH_BUTTON_NEVER
) &&
1063 (toolbar_model
->WouldPerformSearchTermReplacement(true) ||
1064 ((conditions
== chrome::DISPLAY_SEARCH_BUTTON_FOR_STR_OR_IIP
) &&
1065 toolbar_model
->input_in_progress())));
1066 search_button_
->SetVisible(!is_popup_mode_
&& meets_conditions
);
1067 search_button_
->SetImage(
1068 views::Button::STATE_NORMAL
,
1069 *GetThemeProvider()->GetImageSkiaNamed((icon_id
== IDR_OMNIBOX_SEARCH
) ?
1070 IDR_OMNIBOX_SEARCH_BUTTON_LOUPE
: IDR_OMNIBOX_SEARCH_BUTTON_ARROW
));
1072 if (site_chip_view_
)
1073 site_chip_view_
->OnChanged();
1079 void LocationBarView::OnSetFocus() {
1080 GetFocusManager()->SetFocusedView(this);
1083 InstantController
* LocationBarView::GetInstant() {
1084 return delegate_
->GetInstant();
1087 WebContents
* LocationBarView::GetWebContents() {
1088 return delegate_
->GetWebContents();
1091 ToolbarModel
* LocationBarView::GetToolbarModel() {
1092 return delegate_
->GetToolbarModel();
1095 const ToolbarModel
* LocationBarView::GetToolbarModel() const {
1096 return delegate_
->GetToolbarModel();
1099 const char* LocationBarView::GetClassName() const {
1100 return kViewClassName
;
1103 bool LocationBarView::HasFocus() const {
1104 return omnibox_view_
->model()->has_focus();
1107 void LocationBarView::GetAccessibleState(ui::AccessibleViewState
* state
) {
1108 if (!IsInitialized())
1111 state
->role
= ui::AccessibilityTypes::ROLE_LOCATION_BAR
;
1112 state
->name
= l10n_util::GetStringUTF16(IDS_ACCNAME_LOCATION
);
1113 state
->value
= omnibox_view_
->GetText();
1115 base::string16::size_type entry_start
;
1116 base::string16::size_type entry_end
;
1117 omnibox_view_
->GetSelectionBounds(&entry_start
, &entry_end
);
1118 state
->selection_start
= entry_start
;
1119 state
->selection_end
= entry_end
;
1121 if (is_popup_mode_
) {
1122 state
->state
|= ui::AccessibilityTypes::STATE_READONLY
;
1124 state
->set_value_callback
=
1125 base::Bind(&LocationBarView::AccessibilitySetValue
,
1126 weak_ptr_factory_
.GetWeakPtr());
1130 void LocationBarView::OnBoundsChanged(const gfx::Rect
& previous_bounds
) {
1131 if (browser_
&& browser_
->instant_controller() && parent())
1132 browser_
->instant_controller()->SetOmniboxBounds(bounds());
1133 OmniboxPopupView
* popup
= omnibox_view_
->model()->popup_model()->view();
1134 if (popup
->IsOpen())
1135 popup
->UpdatePopupAppearance();
1138 void LocationBarView::ButtonPressed(views::Button
* sender
,
1139 const ui::Event
& event
) {
1140 if (sender
== mic_search_view_
) {
1141 command_updater()->ExecuteCommand(IDC_TOGGLE_SPEECH_INPUT
);
1145 DCHECK_EQ(search_button_
, sender
);
1146 // TODO(pkasting): When macourteau adds UMA stats for this, wire them up here.
1147 omnibox_view_
->model()->AcceptInput(
1148 ui::DispositionFromEventFlags(event
.flags()), false);
1151 void LocationBarView::WriteDragDataForView(views::View
* sender
,
1152 const gfx::Point
& press_pt
,
1153 OSExchangeData
* data
) {
1154 DCHECK_NE(GetDragOperationsForView(sender
, press_pt
),
1155 ui::DragDropTypes::DRAG_NONE
);
1157 WebContents
* web_contents
= GetWebContents();
1158 FaviconTabHelper
* favicon_tab_helper
=
1159 FaviconTabHelper::FromWebContents(web_contents
);
1160 gfx::ImageSkia favicon
= favicon_tab_helper
->GetFavicon().AsImageSkia();
1161 button_drag_utils::SetURLAndDragImage(web_contents
->GetURL(),
1162 web_contents
->GetTitle(),
1165 sender
->GetWidget());
1168 int LocationBarView::GetDragOperationsForView(views::View
* sender
,
1169 const gfx::Point
& p
) {
1170 DCHECK((sender
== location_icon_view_
) || (sender
== ev_bubble_view_
));
1171 WebContents
* web_contents
= delegate_
->GetWebContents();
1172 return (web_contents
&& web_contents
->GetURL().is_valid() &&
1173 !GetOmniboxView()->IsEditingOrEmpty()) ?
1174 (ui::DragDropTypes::DRAG_COPY
| ui::DragDropTypes::DRAG_LINK
) :
1175 ui::DragDropTypes::DRAG_NONE
;
1178 bool LocationBarView::CanStartDragForView(View
* sender
,
1179 const gfx::Point
& press_pt
,
1180 const gfx::Point
& p
) {
1184 ////////////////////////////////////////////////////////////////////////////////
1185 // LocationBarView, LocationBar implementation:
1187 void LocationBarView::ShowFirstRunBubble() {
1188 // Wait until search engines have loaded to show the first run bubble.
1189 TemplateURLService
* url_service
=
1190 TemplateURLServiceFactory::GetForProfile(profile());
1191 if (!url_service
->loaded()) {
1192 template_url_service_
= url_service
;
1193 template_url_service_
->AddObserver(this);
1194 template_url_service_
->Load();
1197 ShowFirstRunBubbleInternal();
1200 GURL
LocationBarView::GetDestinationURL() const {
1201 return destination_url();
1204 WindowOpenDisposition
LocationBarView::GetWindowOpenDisposition() const {
1205 return disposition();
1208 content::PageTransition
LocationBarView::GetPageTransition() const {
1209 return transition();
1212 void LocationBarView::AcceptInput() {
1213 omnibox_view_
->model()->AcceptInput(CURRENT_TAB
, false);
1216 void LocationBarView::FocusLocation(bool select_all
) {
1217 omnibox_view_
->SetFocus();
1219 omnibox_view_
->SelectAll(true);
1222 void LocationBarView::FocusSearch() {
1223 omnibox_view_
->SetFocus();
1224 omnibox_view_
->SetForcedQuery();
1227 void LocationBarView::SaveStateToContents(WebContents
* contents
) {
1228 omnibox_view_
->SaveStateToTab(contents
);
1231 void LocationBarView::Revert() {
1232 omnibox_view_
->RevertAll();
1235 const OmniboxView
* LocationBarView::GetOmniboxView() const {
1236 return omnibox_view_
;
1239 OmniboxView
* LocationBarView::GetOmniboxView() {
1240 return omnibox_view_
;
1243 LocationBarTesting
* LocationBarView::GetLocationBarForTesting() {
1247 int LocationBarView::PageActionCount() {
1248 return page_action_views_
.size();
1251 int LocationBarView::PageActionVisibleCount() {
1253 for (size_t i
= 0; i
< page_action_views_
.size(); i
++) {
1254 if (page_action_views_
[i
]->visible())
1260 ExtensionAction
* LocationBarView::GetPageAction(size_t index
) {
1261 if (index
< page_action_views_
.size())
1262 return page_action_views_
[index
]->image_view()->page_action();
1268 ExtensionAction
* LocationBarView::GetVisiblePageAction(size_t index
) {
1270 for (size_t i
= 0; i
< page_action_views_
.size(); ++i
) {
1271 if (page_action_views_
[i
]->visible()) {
1272 if (current
== index
)
1273 return page_action_views_
[i
]->image_view()->page_action();
1283 void LocationBarView::TestPageActionPressed(size_t index
) {
1285 for (size_t i
= 0; i
< page_action_views_
.size(); ++i
) {
1286 if (page_action_views_
[i
]->visible()) {
1287 if (current
== index
) {
1288 page_action_views_
[i
]->image_view()->ExecuteAction(
1289 ExtensionPopup::SHOW
);
1299 bool LocationBarView::GetBookmarkStarVisibility() {
1301 return star_view_
->visible();
1304 void LocationBarView::OnTemplateURLServiceChanged() {
1305 template_url_service_
->RemoveObserver(this);
1306 template_url_service_
= NULL
;
1307 // If the browser is no longer active, let's not show the info bubble, as this
1308 // would make the browser the active window again.
1309 if (omnibox_view_
&& omnibox_view_
->GetWidget()->IsActive())
1310 ShowFirstRunBubble();
1313 void LocationBarView::Observe(int type
,
1314 const content::NotificationSource
& source
,
1315 const content::NotificationDetails
& details
) {
1317 case chrome::NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED
: {
1318 // Only update if the updated action box was for the active tab contents.
1319 WebContents
* target_tab
= content::Details
<WebContents
>(details
).ptr();
1320 if (target_tab
== GetWebContents())
1321 UpdatePageActions();
1325 case chrome::NOTIFICATION_EXTENSION_LOADED
:
1326 case chrome::NOTIFICATION_EXTENSION_UNLOADED
:
1331 NOTREACHED() << "Unexpected notification.";
1335 void LocationBarView::ModelChanged(const SearchModel::State
& old_state
,
1336 const SearchModel::State
& new_state
) {
1337 const bool visible
= !GetToolbarModel()->input_in_progress() &&
1338 new_state
.voice_search_supported
;
1339 if (mic_search_view_
->visible() != visible
) {
1340 mic_search_view_
->SetVisible(visible
);
1345 int LocationBarView::GetInternalHeight(bool use_preferred_size
) {
1347 use_preferred_size
? GetPreferredSize().height() : height();
1348 return std::max(total_height
- (vertical_edge_thickness() * 2), 0);
1351 ////////////////////////////////////////////////////////////////////////////////
1352 // LocationBarView, private:
1355 int LocationBarView::GetBuiltInHorizontalPaddingForChildViews() {
1356 return (ui::GetDisplayLayout() == ui::LAYOUT_TOUCH
) ?
1357 GetItemPadding() / 2 : 0;
1360 int LocationBarView::GetHorizontalEdgeThickness() const {
1361 // In maximized popup mode, there isn't any edge.
1362 return (is_popup_mode_
&& browser_
&& browser_
->window() &&
1363 browser_
->window()->IsMaximized()) ? 0 : vertical_edge_thickness();
1366 bool LocationBarView::RefreshContentSettingViews() {
1367 bool visibility_changed
= false;
1368 for (ContentSettingViews::const_iterator
i(content_setting_views_
.begin());
1369 i
!= content_setting_views_
.end(); ++i
) {
1370 const bool was_visible
= (*i
)->visible();
1371 (*i
)->Update(GetToolbarModel()->input_in_progress() ?
1372 NULL
: GetWebContents());
1373 if (was_visible
!= (*i
)->visible())
1374 visibility_changed
= true;
1376 return visibility_changed
;
1379 void LocationBarView::DeletePageActionViews() {
1380 for (PageActionViews::const_iterator
i(page_action_views_
.begin());
1381 i
!= page_action_views_
.end(); ++i
)
1382 RemoveChildView(*i
);
1383 STLDeleteElements(&page_action_views_
);
1386 bool LocationBarView::RefreshPageActionViews() {
1390 bool changed
= false;
1392 // Remember the previous visibility of the page actions so that we can
1393 // notify when this changes.
1394 std::map
<ExtensionAction
*, bool> old_visibility
;
1395 for (PageActionViews::const_iterator
i(page_action_views_
.begin());
1396 i
!= page_action_views_
.end(); ++i
) {
1397 old_visibility
[(*i
)->image_view()->page_action()] = (*i
)->visible();
1400 PageActions new_page_actions
;
1402 WebContents
* contents
= delegate_
->GetWebContents();
1404 extensions::TabHelper
* extensions_tab_helper
=
1405 extensions::TabHelper::FromWebContents(contents
);
1406 extensions::LocationBarController
* controller
=
1407 extensions_tab_helper
->location_bar_controller();
1408 new_page_actions
= controller
->GetCurrentActions();
1411 // On startup we sometimes haven't loaded any extensions. This makes sure
1412 // we catch up when the extensions (and any page actions) load.
1413 if (page_actions_
!= new_page_actions
) {
1416 page_actions_
.swap(new_page_actions
);
1417 DeletePageActionViews(); // Delete the old views (if any).
1419 // Create the page action views.
1420 for (PageActions::const_iterator i
= page_actions_
.begin();
1421 i
!= page_actions_
.end(); ++i
) {
1422 PageActionWithBadgeView
* page_action_view
= new PageActionWithBadgeView(
1423 delegate_
->CreatePageActionImageView(this, *i
));
1424 page_action_view
->SetVisible(false);
1425 page_action_views_
.push_back(page_action_view
);
1428 // Move rightmost extensions to the start.
1429 std::stable_partition(
1430 page_action_views_
.begin(),
1431 page_action_views_
.end(),
1432 IsPageActionViewRightAligned(
1433 extensions::ExtensionSystem::Get(profile())->extension_service()));
1435 View
* right_anchor
= open_pdf_in_reader_view_
;
1437 right_anchor
= star_view_
;
1439 right_anchor
= script_bubble_icon_view_
;
1440 DCHECK(right_anchor
);
1442 // |page_action_views_| are ordered right-to-left. Add them as children in
1443 // reverse order so the logical order and visual order match for
1444 // accessibility purposes.
1445 for (PageActionViews::reverse_iterator i
= page_action_views_
.rbegin();
1446 i
!= page_action_views_
.rend(); ++i
)
1447 AddChildViewAt(*i
, GetIndexOf(right_anchor
));
1450 if (!page_action_views_
.empty() && contents
) {
1451 Browser
* browser
= chrome::FindBrowserWithWebContents(contents
);
1452 GURL url
= browser
->tab_strip_model()->GetActiveWebContents()->GetURL();
1454 for (PageActionViews::const_iterator
i(page_action_views_
.begin());
1455 i
!= page_action_views_
.end(); ++i
) {
1456 (*i
)->UpdateVisibility(
1457 GetToolbarModel()->input_in_progress() ? NULL
: contents
, url
);
1459 // Check if the visibility of the action changed and notify if it did.
1460 ExtensionAction
* action
= (*i
)->image_view()->page_action();
1461 if (old_visibility
.find(action
) == old_visibility
.end() ||
1462 old_visibility
[action
] != (*i
)->visible()) {
1464 content::NotificationService::current()->Notify(
1465 chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED
,
1466 content::Source
<ExtensionAction
>(action
),
1467 content::Details
<WebContents
>(contents
));
1474 size_t LocationBarView::ScriptBubbleScriptsRunning() {
1475 WebContents
* contents
= delegate_
->GetWebContents();
1478 extensions::TabHelper
* extensions_tab_helper
=
1479 extensions::TabHelper::FromWebContents(contents
);
1480 if (!extensions_tab_helper
)
1482 extensions::ScriptBubbleController
* script_bubble_controller
=
1483 extensions_tab_helper
->script_bubble_controller();
1484 if (!script_bubble_controller
)
1486 size_t script_count
=
1487 script_bubble_controller
->extensions_running_scripts().size();
1488 return script_count
;
1491 bool LocationBarView::RefreshScriptBubble() {
1492 if (!script_bubble_icon_view_
)
1494 size_t script_count
= ScriptBubbleScriptsRunning();
1495 const bool was_visible
= script_bubble_icon_view_
->visible();
1496 script_bubble_icon_view_
->SetVisible(script_count
> 0);
1497 if (script_count
> 0)
1498 script_bubble_icon_view_
->SetScriptCount(script_count
);
1499 return was_visible
!= script_bubble_icon_view_
->visible();
1502 bool LocationBarView::RefreshZoomView() {
1504 WebContents
* web_contents
= GetWebContents();
1507 const bool was_visible
= zoom_view_
->visible();
1508 zoom_view_
->Update(ZoomController::FromWebContents(web_contents
));
1509 return was_visible
!= zoom_view_
->visible();
1512 bool LocationBarView::RefreshManagePasswordsIconView() {
1513 DCHECK(manage_passwords_icon_view_
);
1514 WebContents
* web_contents
= GetWebContents();
1517 const bool was_visible
= manage_passwords_icon_view_
->visible();
1518 manage_passwords_icon_view_
->Update(
1519 ManagePasswordsBubbleUIController::FromWebContents(web_contents
));
1520 return was_visible
!= manage_passwords_icon_view_
->visible();
1523 void LocationBarView::RefreshTranslateIcon() {
1524 if (!TranslateManager::IsTranslateBubbleEnabled())
1527 WebContents
* web_contents
= GetWebContents();
1530 LanguageState
& language_state
= TranslateTabHelper::FromWebContents(
1531 web_contents
)->language_state();
1532 bool enabled
= language_state
.translate_enabled();
1533 command_updater()->UpdateCommandEnabled(IDC_TRANSLATE_PAGE
, enabled
);
1534 translate_icon_view_
->SetVisible(enabled
);
1535 translate_icon_view_
->SetToggled(language_state
.IsPageTranslated());
1538 void LocationBarView::ShowManagePasswordsBubbleIfNeeded() {
1539 DCHECK(manage_passwords_icon_view_
);
1540 WebContents
* web_contents
= GetWebContents();
1543 manage_passwords_icon_view_
->ShowBubbleIfNeeded(
1544 ManagePasswordsBubbleUIController::FromWebContents(web_contents
));
1547 bool LocationBarView::HasValidSuggestText() const {
1548 return suggested_text_view_
->visible() &&
1549 !suggested_text_view_
->size().IsEmpty();
1552 void LocationBarView::ShowFirstRunBubbleInternal() {
1553 #if !defined(OS_CHROMEOS)
1554 // First run bubble doesn't make sense for Chrome OS.
1555 Browser
* browser
= GetBrowserFromDelegate(delegate_
);
1557 return; // Possible when browser is shutting down.
1559 FirstRunBubble::ShowBubble(browser
, GetLocationBarAnchor());
1563 void LocationBarView::PaintPageActionBackgrounds(gfx::Canvas
* canvas
) {
1564 WebContents
* web_contents
= GetWebContents();
1565 // web_contents may be NULL while the browser is shutting down.
1569 const int32 tab_id
= SessionID::IdForTab(web_contents
);
1570 const ToolbarModel::SecurityLevel security_level
=
1571 GetToolbarModel()->GetSecurityLevel(false);
1572 const SkColor text_color
= GetColor(security_level
, TEXT
);
1573 const SkColor background_color
= GetColor(security_level
, BACKGROUND
);
1575 for (PageActionViews::const_iterator
1576 page_action_view
= page_action_views_
.begin();
1577 page_action_view
!= page_action_views_
.end();
1578 ++page_action_view
) {
1579 gfx::Rect bounds
= (*page_action_view
)->bounds();
1580 int horizontal_padding
=
1581 GetItemPadding() - GetBuiltInHorizontalPaddingForChildViews();
1582 // Make the bounding rectangle include the whole vertical range of the
1583 // location bar, and the mid-point pixels between adjacent page actions.
1585 // For odd horizontal_paddings, "horizontal_padding + 1" includes the
1586 // mid-point between two page actions in the bounding rectangle. For even
1587 // paddings, the +1 is dropped, which is right since there is no pixel at
1589 bounds
.Inset(-(horizontal_padding
+ 1) / 2, 0);
1590 location_bar_util::PaintExtensionActionBackground(
1591 *(*page_action_view
)->image_view()->page_action(),
1592 tab_id
, canvas
, bounds
, text_color
, background_color
);
1596 void LocationBarView::AccessibilitySetValue(const base::string16
& new_value
) {
1597 omnibox_view_
->SetUserText(new_value
, new_value
, true);