location_bar_view.cc revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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/location_bar_controller.h" 21#include "chrome/browser/extensions/script_bubble_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/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_tab_helper.h" 30#include "chrome/browser/ui/browser.h" 31#include "chrome/browser/ui/browser_finder.h" 32#include "chrome/browser/ui/browser_instant_controller.h" 33#include "chrome/browser/ui/browser_window.h" 34#include "chrome/browser/ui/omnibox/location_bar_util.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_bubble_ui_controller.h" 38#include "chrome/browser/ui/tabs/tab_strip_model.h" 39#include "chrome/browser/ui/view_ids.h" 40#include "chrome/browser/ui/views/bookmarks/bookmark_prompt_view.h" 41#include "chrome/browser/ui/views/browser_dialogs.h" 42#include "chrome/browser/ui/views/location_bar/content_setting_image_view.h" 43#include "chrome/browser/ui/views/location_bar/ev_bubble_view.h" 44#include "chrome/browser/ui/views/location_bar/generated_credit_card_view.h" 45#include "chrome/browser/ui/views/location_bar/keyword_hint_view.h" 46#include "chrome/browser/ui/views/location_bar/location_bar_layout.h" 47#include "chrome/browser/ui/views/location_bar/location_icon_view.h" 48#include "chrome/browser/ui/views/location_bar/open_pdf_in_reader_view.h" 49#include "chrome/browser/ui/views/location_bar/page_action_image_view.h" 50#include "chrome/browser/ui/views/location_bar/page_action_with_badge_view.h" 51#include "chrome/browser/ui/views/location_bar/script_bubble_icon_view.h" 52#include "chrome/browser/ui/views/location_bar/selected_keyword_view.h" 53#include "chrome/browser/ui/views/location_bar/star_view.h" 54#include "chrome/browser/ui/views/location_bar/translate_icon_view.h" 55#include "chrome/browser/ui/views/location_bar/zoom_bubble_view.h" 56#include "chrome/browser/ui/views/location_bar/zoom_view.h" 57#include "chrome/browser/ui/views/passwords/manage_passwords_bubble_view.h" 58#include "chrome/browser/ui/views/passwords/manage_passwords_icon_view.h" 59#include "chrome/browser/ui/zoom/zoom_controller.h" 60#include "chrome/common/chrome_switches.h" 61#include "chrome/common/pref_names.h" 62#include "content/public/browser/notification_service.h" 63#include "content/public/browser/render_widget_host_view.h" 64#include "content/public/browser/web_contents.h" 65#include "extensions/common/feature_switch.h" 66#include "grit/generated_resources.h" 67#include "grit/theme_resources.h" 68#include "ui/base/accessibility/accessible_view_state.h" 69#include "ui/base/dragdrop/drag_drop_types.h" 70#include "ui/base/l10n/l10n_util.h" 71#include "ui/base/layout.h" 72#include "ui/base/resource/resource_bundle.h" 73#include "ui/base/theme_provider.h" 74#include "ui/events/event.h" 75#include "ui/gfx/canvas.h" 76#include "ui/gfx/color_utils.h" 77#include "ui/gfx/image/image.h" 78#include "ui/gfx/image/image_skia_operations.h" 79#include "ui/gfx/skia_util.h" 80#include "ui/native_theme/native_theme.h" 81#include "ui/views/background.h" 82#include "ui/views/border.h" 83#include "ui/views/button_drag_utils.h" 84#include "ui/views/controls/button/image_button.h" 85#include "ui/views/controls/button/label_button.h" 86#include "ui/views/controls/button/label_button_border.h" 87#include "ui/views/controls/label.h" 88#include "ui/views/widget/widget.h" 89 90#if !defined(OS_CHROMEOS) 91#include "chrome/browser/ui/views/first_run_bubble.h" 92#endif 93 94using content::WebContents; 95using views::View; 96 97namespace { 98 99#if !defined(OS_CHROMEOS) 100Browser* GetBrowserFromDelegate(LocationBarView::Delegate* delegate) { 101 WebContents* contents = delegate->GetWebContents(); 102 return contents ? chrome::FindBrowserWithWebContents(contents) : NULL; 103} 104#endif 105 106// Given a containing |height| and a |base_font_list|, shrinks the font size 107// until the font list will fit within |height| while having its cap height 108// vertically centered. Returns the correctly-sized font list. 109// 110// The expected layout: 111// +--------+-----------------------------------------------+------------+ 112// | | y offset | space | 113// | +--------+-------------------+------------------+ above | 114// | | | | internal leading | cap height | 115// | box | font | ascent (baseline) +------------------+------------+ 116// | height | height | | cap height | 117// | | |-------------------+------------------+------------+ 118// | | | descent (height - baseline) | space | 119// | +--------+--------------------------------------+ below | 120// | | space at bottom | cap height | 121// +--------+-----------------------------------------------+------------+ 122// Goal: 123// center of box height == center of cap height 124// (i.e. space above cap height == space below cap height) 125// Restrictions: 126// y offset >= 0 127// space at bottom >= 0 128// (i.e. Entire font must be visible inside the box.) 129gfx::FontList GetLargestFontListWithHeightBound( 130 const gfx::FontList& base_font_list, 131 int height) { 132 gfx::FontList font_list = base_font_list; 133 for (int font_size = font_list.GetFontSize(); font_size > 1; --font_size) { 134 const int internal_leading = 135 font_list.GetBaseline() - font_list.GetCapHeight(); 136 // Some platforms don't support getting the cap height, and simply return 137 // the entire font ascent from GetCapHeight(). Centering the ascent makes 138 // the font look too low, so if GetCapHeight() returns the ascent, center 139 // the entire font height instead. 140 const int space = 141 height - ((internal_leading != 0) ? 142 font_list.GetCapHeight() : font_list.GetHeight()); 143 const int y_offset = space / 2 - internal_leading; 144 const int space_at_bottom = height - (y_offset + font_list.GetHeight()); 145 if ((y_offset >= 0) && (space_at_bottom >= 0)) 146 break; 147 font_list = font_list.DeriveFontListWithSizeDelta(-1); 148 } 149 return font_list; 150} 151 152} // namespace 153 154 155// LocationBarView ----------------------------------------------------------- 156 157// static 158const int LocationBarView::kNormalEdgeThickness = 2; 159const int LocationBarView::kPopupEdgeThickness = 1; 160const int LocationBarView::kIconInternalPadding = 2; 161const int LocationBarView::kBubblePadding = 1; 162const char LocationBarView::kViewClassName[] = "LocationBarView"; 163 164LocationBarView::LocationBarView(Browser* browser, 165 Profile* profile, 166 CommandUpdater* command_updater, 167 Delegate* delegate, 168 bool is_popup_mode) 169 : OmniboxEditController(command_updater), 170 browser_(browser), 171 omnibox_view_(NULL), 172 profile_(profile), 173 delegate_(delegate), 174 location_icon_view_(NULL), 175 ev_bubble_view_(NULL), 176 ime_inline_autocomplete_view_(NULL), 177 selected_keyword_view_(NULL), 178 suggested_text_view_(NULL), 179 keyword_hint_view_(NULL), 180 mic_search_view_(NULL), 181 zoom_view_(NULL), 182 generated_credit_card_view_(NULL), 183 open_pdf_in_reader_view_(NULL), 184 manage_passwords_icon_view_(NULL), 185 script_bubble_icon_view_(NULL), 186 translate_icon_view_(NULL), 187 star_view_(NULL), 188 search_button_(NULL), 189 is_popup_mode_(is_popup_mode), 190 show_focus_rect_(false), 191 template_url_service_(NULL), 192 animation_offset_(0), 193 weak_ptr_factory_(this) { 194 const int kOmniboxBorderImages[] = IMAGE_GRID(IDR_OMNIBOX_BORDER); 195 const int kOmniboxPopupImages[] = IMAGE_GRID(IDR_OMNIBOX_POPUP_BORDER); 196 background_border_painter_.reset( 197 views::Painter::CreateImageGridPainter( 198 is_popup_mode_ ? kOmniboxPopupImages : kOmniboxBorderImages)); 199#if defined(OS_CHROMEOS) 200 if (!is_popup_mode_) { 201 const int kOmniboxFillingImages[] = IMAGE_GRID(IDR_OMNIBOX_FILLING); 202 background_filling_painter_.reset( 203 views::Painter::CreateImageGridPainter(kOmniboxFillingImages)); 204 } 205#endif 206 207 edit_bookmarks_enabled_.Init( 208 prefs::kEditBookmarksEnabled, 209 profile_->GetPrefs(), 210 base::Bind(&LocationBarView::Update, 211 base::Unretained(this), 212 static_cast<content::WebContents*>(NULL))); 213 214 if (browser_) 215 browser_->search_model()->AddObserver(this); 216} 217 218LocationBarView::~LocationBarView() { 219 if (template_url_service_) 220 template_url_service_->RemoveObserver(this); 221 if (browser_) 222 browser_->search_model()->RemoveObserver(this); 223} 224 225// static 226void LocationBarView::InitTouchableLocationBarChildView(views::View* view) { 227 int horizontal_padding = GetBuiltInHorizontalPaddingForChildViews(); 228 if (horizontal_padding != 0) { 229 view->set_border(views::Border::CreateEmptyBorder( 230 3, horizontal_padding, 3, horizontal_padding)); 231 } 232} 233 234void LocationBarView::Init() { 235 // We need to be in a Widget, otherwise GetNativeTheme() may change and we're 236 // not prepared for that. 237 DCHECK(GetWidget()); 238 239 location_icon_view_ = new LocationIconView(this); 240 location_icon_view_->set_drag_controller(this); 241 AddChildView(location_icon_view_); 242 243 // Determine the main font. 244 gfx::FontList font_list = ResourceBundle::GetSharedInstance().GetFontList( 245 ResourceBundle::BaseFont); 246 const int current_font_size = font_list.GetFontSize(); 247 const int desired_font_size = browser_defaults::kOmniboxFontPixelSize; 248 if (current_font_size < desired_font_size) 249 font_list = font_list.DeriveFontListWithSize(desired_font_size); 250 // Shrink large fonts to make them fit. 251 // TODO(pkasting): Stretch the location bar instead in this case. 252 const int location_height = GetInternalHeight(true); 253 font_list = GetLargestFontListWithHeightBound(font_list, location_height); 254 255 // Determine the font for use inside the bubbles. The bubble background 256 // images have 1 px thick edges, which we don't want to overlap. 257 const int kBubbleInteriorVerticalPadding = 1; 258 const int bubble_vertical_padding = 259 (kBubblePadding + kBubbleInteriorVerticalPadding) * 2; 260 const gfx::FontList bubble_font_list( 261 GetLargestFontListWithHeightBound( 262 font_list, location_height - bubble_vertical_padding)); 263 264 const SkColor background_color = 265 GetColor(ToolbarModel::NONE, LocationBarView::BACKGROUND); 266 ev_bubble_view_ = new EVBubbleView( 267 bubble_font_list, GetColor(ToolbarModel::EV_SECURE, SECURITY_TEXT), 268 background_color, this); 269 ev_bubble_view_->set_drag_controller(this); 270 AddChildView(ev_bubble_view_); 271 272 // Initialize the Omnibox view. 273 omnibox_view_ = new OmniboxViewViews(this, profile_, command_updater(), 274 is_popup_mode_, this, font_list); 275 omnibox_view_->Init(); 276 omnibox_view_->set_focusable(true); 277 AddChildView(omnibox_view_); 278 279 // Initialize the inline autocomplete view which is visible only when IME is 280 // turned on. Use the same font with the omnibox and highlighted background. 281 ime_inline_autocomplete_view_ = new views::Label(base::string16(), font_list); 282 ime_inline_autocomplete_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT); 283 ime_inline_autocomplete_view_->SetAutoColorReadabilityEnabled(false); 284 ime_inline_autocomplete_view_->set_background( 285 views::Background::CreateSolidBackground(GetNativeTheme()->GetSystemColor( 286 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused))); 287 ime_inline_autocomplete_view_->SetEnabledColor( 288 GetNativeTheme()->GetSystemColor( 289 ui::NativeTheme::kColorId_TextfieldSelectionColor)); 290 ime_inline_autocomplete_view_->SetVisible(false); 291 AddChildView(ime_inline_autocomplete_view_); 292 293 const SkColor text_color = GetColor(ToolbarModel::NONE, TEXT); 294 selected_keyword_view_ = new SelectedKeywordView( 295 bubble_font_list, text_color, background_color, profile_); 296 AddChildView(selected_keyword_view_); 297 298 suggested_text_view_ = new views::Label(base::string16(), font_list); 299 suggested_text_view_->SetHorizontalAlignment(gfx::ALIGN_LEFT); 300 suggested_text_view_->SetAutoColorReadabilityEnabled(false); 301 suggested_text_view_->SetEnabledColor(GetColor( 302 ToolbarModel::NONE, LocationBarView::DEEMPHASIZED_TEXT)); 303 suggested_text_view_->SetVisible(false); 304 AddChildView(suggested_text_view_); 305 306 keyword_hint_view_ = new KeywordHintView( 307 profile_, font_list, 308 GetColor(ToolbarModel::NONE, LocationBarView::DEEMPHASIZED_TEXT), 309 background_color); 310 AddChildView(keyword_hint_view_); 311 312 mic_search_view_ = new views::ImageButton(this); 313 mic_search_view_->set_id(VIEW_ID_MIC_SEARCH_BUTTON); 314 mic_search_view_->set_accessibility_focusable(true); 315 mic_search_view_->SetTooltipText( 316 l10n_util::GetStringUTF16(IDS_TOOLTIP_MIC_SEARCH)); 317 mic_search_view_->SetImage( 318 views::Button::STATE_NORMAL, 319 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( 320 IDR_OMNIBOX_MIC_SEARCH)); 321 mic_search_view_->SetImageAlignment(views::ImageButton::ALIGN_CENTER, 322 views::ImageButton::ALIGN_MIDDLE); 323 mic_search_view_->SetVisible(false); 324 InitTouchableLocationBarChildView(mic_search_view_); 325 AddChildView(mic_search_view_); 326 327 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) { 328 ContentSettingImageView* content_blocked_view = 329 new ContentSettingImageView(static_cast<ContentSettingsType>(i), this, 330 bubble_font_list, text_color, 331 background_color); 332 content_setting_views_.push_back(content_blocked_view); 333 content_blocked_view->SetVisible(false); 334 AddChildView(content_blocked_view); 335 } 336 337 generated_credit_card_view_ = new GeneratedCreditCardView(delegate_); 338 AddChildView(generated_credit_card_view_); 339 340 zoom_view_ = new ZoomView(delegate_); 341 zoom_view_->set_id(VIEW_ID_ZOOM_BUTTON); 342 AddChildView(zoom_view_); 343 344 open_pdf_in_reader_view_ = new OpenPDFInReaderView(this); 345 AddChildView(open_pdf_in_reader_view_); 346 347 manage_passwords_icon_view_ = new ManagePasswordsIconView(delegate_); 348 manage_passwords_icon_view_->set_id(VIEW_ID_MANAGE_PASSWORDS_ICON_BUTTON); 349 AddChildView(manage_passwords_icon_view_); 350 351 script_bubble_icon_view_ = new ScriptBubbleIconView(delegate()); 352 script_bubble_icon_view_->SetVisible(false); 353 AddChildView(script_bubble_icon_view_); 354 355 translate_icon_view_ = new TranslateIconView(command_updater()); 356 translate_icon_view_->SetVisible(false); 357 AddChildView(translate_icon_view_); 358 359 star_view_ = new StarView(command_updater()); 360 star_view_->SetVisible(false); 361 AddChildView(star_view_); 362 363 search_button_ = new views::LabelButton(this, base::string16()); 364 search_button_->set_triggerable_event_flags( 365 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_MIDDLE_MOUSE_BUTTON); 366 search_button_->SetStyle(views::Button::STYLE_BUTTON); 367 search_button_->set_focusable(false); 368 search_button_->set_min_size(gfx::Size()); 369 views::LabelButtonBorder* search_button_border = 370 static_cast<views::LabelButtonBorder*>(search_button_->border()); 371 search_button_border->set_insets(gfx::Insets()); 372 const int kSearchButtonNormalImages[] = IMAGE_GRID(IDR_OMNIBOX_SEARCH_BUTTON); 373 search_button_border->SetPainter( 374 false, views::Button::STATE_NORMAL, 375 views::Painter::CreateImageGridPainter(kSearchButtonNormalImages)); 376 const int kSearchButtonHoveredImages[] = 377 IMAGE_GRID(IDR_OMNIBOX_SEARCH_BUTTON_HOVER); 378 search_button_border->SetPainter( 379 false, views::Button::STATE_HOVERED, 380 views::Painter::CreateImageGridPainter(kSearchButtonHoveredImages)); 381 const int kSearchButtonPressedImages[] = 382 IMAGE_GRID(IDR_OMNIBOX_SEARCH_BUTTON_PRESSED); 383 search_button_border->SetPainter( 384 false, views::Button::STATE_PRESSED, 385 views::Painter::CreateImageGridPainter(kSearchButtonPressedImages)); 386 search_button_border->SetPainter(false, views::Button::STATE_DISABLED, NULL); 387 search_button_border->SetPainter(true, views::Button::STATE_NORMAL, NULL); 388 search_button_border->SetPainter(true, views::Button::STATE_HOVERED, NULL); 389 search_button_border->SetPainter(true, views::Button::STATE_PRESSED, NULL); 390 search_button_border->SetPainter(true, views::Button::STATE_DISABLED, NULL); 391 const int kSearchButtonWidth = 56; 392 search_button_->set_min_size(gfx::Size(kSearchButtonWidth, 0)); 393 search_button_->SetVisible(false); 394 AddChildView(search_button_); 395 396 registrar_.Add(this, 397 chrome::NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED, 398 content::Source<Profile>(profile_)); 399 400 // Initialize the location entry. We do this to avoid a black flash which is 401 // visible when the location entry has just been initialized. 402 Update(NULL); 403} 404 405bool LocationBarView::IsInitialized() const { 406 return omnibox_view_ != NULL; 407} 408 409SkColor LocationBarView::GetColor(ToolbarModel::SecurityLevel security_level, 410 ColorKind kind) const { 411 const ui::NativeTheme* native_theme = GetNativeTheme(); 412 switch (kind) { 413 case BACKGROUND: 414#if defined(OS_CHROMEOS) 415 // Chrome OS requires a transparent omnibox background color. 416 return SkColorSetARGB(0, 255, 255, 255); 417#else 418 return native_theme->GetSystemColor( 419 ui::NativeTheme::kColorId_TextfieldDefaultBackground); 420#endif 421 422 case TEXT: 423 return native_theme->GetSystemColor( 424 ui::NativeTheme::kColorId_TextfieldDefaultColor); 425 426 case SELECTED_TEXT: 427 return native_theme->GetSystemColor( 428 ui::NativeTheme::kColorId_TextfieldSelectionColor); 429 430 case DEEMPHASIZED_TEXT: 431 return color_utils::AlphaBlend( 432 GetColor(security_level, TEXT), 433 GetColor(security_level, BACKGROUND), 434 128); 435 436 case SECURITY_TEXT: { 437 SkColor color; 438 switch (security_level) { 439 case ToolbarModel::EV_SECURE: 440 case ToolbarModel::SECURE: 441 color = SkColorSetRGB(7, 149, 0); 442 break; 443 444 case ToolbarModel::SECURITY_WARNING: 445 case ToolbarModel::SECURITY_POLICY_WARNING: 446 return GetColor(security_level, DEEMPHASIZED_TEXT); 447 break; 448 449 case ToolbarModel::SECURITY_ERROR: 450 color = SkColorSetRGB(162, 0, 0); 451 break; 452 453 default: 454 NOTREACHED(); 455 return GetColor(security_level, TEXT); 456 } 457 return color_utils::GetReadableColor( 458 color, GetColor(security_level, BACKGROUND)); 459 } 460 461 default: 462 NOTREACHED(); 463 return GetColor(security_level, TEXT); 464 } 465} 466 467void LocationBarView::GetOmniboxPopupPositioningInfo( 468 gfx::Point* top_left_screen_coord, 469 int* popup_width, 470 int* left_margin, 471 int* right_margin) { 472 // Because the popup might appear atop the attached bookmark bar, there won't 473 // necessarily be a client edge separating it from the rest of the toolbar. 474 // Therefore we position the popup high enough so it can draw its own client 475 // edge at the top, in the same place the toolbar would normally draw the 476 // client edge. 477 *top_left_screen_coord = gfx::Point( 478 0, 479 parent()->height() - views::NonClientFrameView::kClientEdgeThickness); 480 views::View::ConvertPointToScreen(parent(), top_left_screen_coord); 481 *popup_width = parent()->width(); 482 483 gfx::Rect location_bar_bounds(bounds()); 484 location_bar_bounds.Inset(kNormalEdgeThickness, 0); 485 *left_margin = location_bar_bounds.x(); 486 *right_margin = *popup_width - location_bar_bounds.right(); 487} 488 489// static 490int LocationBarView::GetItemPadding() { 491 const int kTouchItemPadding = 8; 492 if (ui::GetDisplayLayout() == ui::LAYOUT_TOUCH) 493 return kTouchItemPadding; 494 495 const int kDesktopScriptBadgeItemPadding = 9; 496 const int kDesktopItemPadding = 3; 497 return extensions::FeatureSwitch::script_badges()->IsEnabled() ? 498 kDesktopScriptBadgeItemPadding : kDesktopItemPadding; 499} 500 501// DropdownBarHostDelegate 502void LocationBarView::SetFocusAndSelection(bool select_all) { 503 FocusLocation(select_all); 504} 505 506void LocationBarView::SetAnimationOffset(int offset) { 507 animation_offset_ = offset; 508} 509 510void LocationBarView::UpdateContentSettingsIcons() { 511 if (RefreshContentSettingViews()) { 512 Layout(); 513 SchedulePaint(); 514 } 515} 516 517void LocationBarView::UpdateManagePasswordsIconAndBubble() { 518 if (RefreshManagePasswordsIconView()) { 519 Layout(); 520 SchedulePaint(); 521 } 522 ShowManagePasswordsBubbleIfNeeded(); 523} 524 525void LocationBarView::UpdatePageActions() { 526 size_t count_before = page_action_views_.size(); 527 bool changed = RefreshPageActionViews(); 528 changed |= RefreshScriptBubble(); 529 if (page_action_views_.size() != count_before) { 530 content::NotificationService::current()->Notify( 531 chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_COUNT_CHANGED, 532 content::Source<LocationBar>(this), 533 content::NotificationService::NoDetails()); 534 } 535 536 if (changed) { 537 Layout(); 538 SchedulePaint(); 539 } 540} 541 542void LocationBarView::InvalidatePageActions() { 543 size_t count_before = page_action_views_.size(); 544 DeletePageActionViews(); 545 if (page_action_views_.size() != count_before) { 546 content::NotificationService::current()->Notify( 547 chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_COUNT_CHANGED, 548 content::Source<LocationBar>(this), 549 content::NotificationService::NoDetails()); 550 } 551} 552 553void LocationBarView::UpdateOpenPDFInReaderPrompt() { 554 open_pdf_in_reader_view_->Update( 555 GetToolbarModel()->input_in_progress() ? NULL : GetWebContents()); 556 Layout(); 557 SchedulePaint(); 558} 559 560void LocationBarView::UpdateGeneratedCreditCardView() { 561 generated_credit_card_view_->Update(); 562 Layout(); 563 SchedulePaint(); 564} 565 566void LocationBarView::OnFocus() { 567 // Focus the view widget first which implements accessibility for 568 // Chrome OS. It is noop on Win. This should be removed once 569 // Chrome OS migrates to aura, which uses Views' textfield that receives 570 // focus. See crbug.com/106428. 571 NotifyAccessibilityEvent(ui::AccessibilityTypes::EVENT_FOCUS, false); 572 573 // Then focus the native location view which implements accessibility for 574 // Windows. 575 omnibox_view_->SetFocus(); 576} 577 578void LocationBarView::SetPreviewEnabledPageAction(ExtensionAction* page_action, 579 bool preview_enabled) { 580 if (is_popup_mode_) 581 return; 582 583 DCHECK(page_action); 584 WebContents* contents = delegate_->GetWebContents(); 585 586 RefreshPageActionViews(); 587 PageActionWithBadgeView* page_action_view = 588 static_cast<PageActionWithBadgeView*>(GetPageActionView(page_action)); 589 DCHECK(page_action_view); 590 if (!page_action_view) 591 return; 592 593 page_action_view->image_view()->set_preview_enabled(preview_enabled); 594 page_action_view->UpdateVisibility(contents, GetToolbarModel()->GetURL()); 595 Layout(); 596 SchedulePaint(); 597} 598 599views::View* LocationBarView::GetPageActionView(ExtensionAction *page_action) { 600 DCHECK(page_action); 601 for (PageActionViews::const_iterator i(page_action_views_.begin()); 602 i != page_action_views_.end(); ++i) { 603 if ((*i)->image_view()->page_action() == page_action) 604 return *i; 605 } 606 return NULL; 607} 608 609void LocationBarView::SetStarToggled(bool on) { 610 if (star_view_) 611 star_view_->SetToggled(on); 612} 613 614void LocationBarView::SetTranslateIconToggled(bool on) { 615 translate_icon_view_->SetToggled(on); 616} 617 618void LocationBarView::ShowBookmarkPrompt() { 619 if (star_view_ && star_view_->visible()) 620 BookmarkPromptView::ShowPrompt(star_view_, profile_->GetPrefs()); 621} 622 623void LocationBarView::ZoomChangedForActiveTab(bool can_show_bubble) { 624 DCHECK(zoom_view_); 625 if (RefreshZoomView()) { 626 Layout(); 627 SchedulePaint(); 628 } 629 630 if (can_show_bubble && zoom_view_->visible() && delegate_->GetWebContents()) 631 ZoomBubbleView::ShowBubble(delegate_->GetWebContents(), true); 632} 633 634gfx::Point LocationBarView::GetOmniboxViewOrigin() const { 635 gfx::Point origin(omnibox_view_->bounds().origin()); 636 // If the UI layout is RTL, the coordinate system is not transformed and 637 // therefore we need to adjust the X coordinate so that bubble appears on the 638 // right hand side of the location bar. 639 if (base::i18n::IsRTL()) 640 origin.set_x(width() - origin.x()); 641 views::View::ConvertPointToScreen(this, &origin); 642 return origin; 643} 644 645void LocationBarView::SetImeInlineAutocompletion(const base::string16& text) { 646 ime_inline_autocomplete_view_->SetText(text); 647 ime_inline_autocomplete_view_->SetVisible(!text.empty()); 648} 649 650void LocationBarView::SetGrayTextAutocompletion(const base::string16& text) { 651 if (suggested_text_view_->text() != text) { 652 suggested_text_view_->SetText(text); 653 suggested_text_view_->SetVisible(!text.empty()); 654 Layout(); 655 SchedulePaint(); 656 } 657} 658 659base::string16 LocationBarView::GetGrayTextAutocompletion() const { 660 return HasValidSuggestText() ? suggested_text_view_->text() 661 : base::string16(); 662} 663 664gfx::Size LocationBarView::GetPreferredSize() { 665 gfx::Size background_min_size(background_border_painter_->GetMinimumSize()); 666 if (!IsInitialized()) 667 return background_min_size; 668 gfx::Size search_button_min_size(search_button_->GetMinimumSize()); 669 gfx::Size min_size(background_min_size); 670 min_size.SetToMax(search_button_min_size); 671 min_size.set_width( 672 background_min_size.width() + search_button_min_size.width()); 673 return min_size; 674} 675 676void LocationBarView::Layout() { 677 if (!IsInitialized()) 678 return; 679 680 selected_keyword_view_->SetVisible(false); 681 location_icon_view_->SetVisible(false); 682 ev_bubble_view_->SetVisible(false); 683 keyword_hint_view_->SetVisible(false); 684 685 const int item_padding = GetItemPadding(); 686 // The textfield has 1 px of whitespace before the text in the RTL case only. 687 const int kEditLeadingInternalSpace = base::i18n::IsRTL() ? 1 : 0; 688 LocationBarLayout leading_decorations( 689 LocationBarLayout::LEFT_EDGE, item_padding - kEditLeadingInternalSpace); 690 LocationBarLayout trailing_decorations(LocationBarLayout::RIGHT_EDGE, 691 item_padding); 692 693 const base::string16 keyword(omnibox_view_->model()->keyword()); 694 const bool is_keyword_hint(omnibox_view_->model()->is_keyword_hint()); 695 const int bubble_location_y = vertical_edge_thickness() + kBubblePadding; 696 // In some cases (e.g. fullscreen mode) we may have 0 height. We still want 697 // to position our child views in this case, because other things may be 698 // positioned relative to them (e.g. the "bookmark added" bubble if the user 699 // hits ctrl-d). 700 const int location_height = GetInternalHeight(false); 701 const int bubble_height = std::max(location_height - (kBubblePadding * 2), 0); 702 if (!keyword.empty() && !is_keyword_hint) { 703 leading_decorations.AddDecoration(bubble_location_y, bubble_height, true, 0, 704 kBubblePadding, item_padding, 0, 705 selected_keyword_view_); 706 if (selected_keyword_view_->keyword() != keyword) { 707 selected_keyword_view_->SetKeyword(keyword); 708 const TemplateURL* template_url = 709 TemplateURLServiceFactory::GetForProfile(profile_)-> 710 GetTemplateURLForKeyword(keyword); 711 if (template_url && 712 (template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION)) { 713 gfx::Image image = extensions::OmniboxAPI::Get(profile_)-> 714 GetOmniboxIcon(template_url->GetExtensionId()); 715 selected_keyword_view_->SetImage(image.AsImageSkia()); 716 selected_keyword_view_->set_is_extension_icon(true); 717 } else { 718 selected_keyword_view_->SetImage( 719 *(GetThemeProvider()->GetImageSkiaNamed(IDR_OMNIBOX_SEARCH))); 720 selected_keyword_view_->set_is_extension_icon(false); 721 } 722 } 723 } else if (GetToolbarModel()->GetSecurityLevel(false) == 724 ToolbarModel::EV_SECURE) { 725 ev_bubble_view_->SetLabel(GetToolbarModel()->GetEVCertName()); 726 // The largest fraction of the omnibox that can be taken by the EV bubble. 727 const double kMaxBubbleFraction = 0.5; 728 leading_decorations.AddDecoration(bubble_location_y, bubble_height, false, 729 kMaxBubbleFraction, kBubblePadding, 730 item_padding, 0, ev_bubble_view_); 731 } else { 732 leading_decorations.AddDecoration( 733 vertical_edge_thickness(), location_height, 734 GetBuiltInHorizontalPaddingForChildViews(), 735 location_icon_view_); 736 } 737 738 if (star_view_->visible()) { 739 trailing_decorations.AddDecoration( 740 vertical_edge_thickness(), location_height, 741 GetBuiltInHorizontalPaddingForChildViews(), star_view_); 742 } 743 if (translate_icon_view_->visible()) { 744 trailing_decorations.AddDecoration( 745 vertical_edge_thickness(), location_height, 746 GetBuiltInHorizontalPaddingForChildViews(), 747 translate_icon_view_); 748 } 749 if (script_bubble_icon_view_->visible()) { 750 trailing_decorations.AddDecoration( 751 vertical_edge_thickness(), location_height, 752 GetBuiltInHorizontalPaddingForChildViews(), 753 script_bubble_icon_view_); 754 } 755 if (open_pdf_in_reader_view_->visible()) { 756 trailing_decorations.AddDecoration( 757 vertical_edge_thickness(), location_height, 758 GetBuiltInHorizontalPaddingForChildViews(), 759 open_pdf_in_reader_view_); 760 } 761 if (manage_passwords_icon_view_->visible()) { 762 trailing_decorations.AddDecoration(vertical_edge_thickness(), 763 location_height, 0, 764 manage_passwords_icon_view_); 765 } 766 for (PageActionViews::const_iterator i(page_action_views_.begin()); 767 i != page_action_views_.end(); ++i) { 768 if ((*i)->visible()) { 769 trailing_decorations.AddDecoration( 770 vertical_edge_thickness(), location_height, 771 GetBuiltInHorizontalPaddingForChildViews(), (*i)); 772 } 773 } 774 if (zoom_view_->visible()) { 775 trailing_decorations.AddDecoration(vertical_edge_thickness(), 776 location_height, 0, zoom_view_); 777 } 778 for (ContentSettingViews::const_reverse_iterator i( 779 content_setting_views_.rbegin()); i != content_setting_views_.rend(); 780 ++i) { 781 if ((*i)->visible()) { 782 trailing_decorations.AddDecoration( 783 bubble_location_y, bubble_height, false, 0, item_padding, 784 item_padding, GetBuiltInHorizontalPaddingForChildViews(), (*i)); 785 } 786 } 787 if (generated_credit_card_view_->visible()) { 788 trailing_decorations.AddDecoration(vertical_edge_thickness(), 789 location_height, 0, 790 generated_credit_card_view_); 791 } 792 if (mic_search_view_->visible()) { 793 trailing_decorations.AddDecoration(vertical_edge_thickness(), 794 location_height, 0, mic_search_view_); 795 } 796 // Because IMEs may eat the tab key, we don't show "press tab to search" while 797 // IME composition is in progress. 798 if (!keyword.empty() && is_keyword_hint && !omnibox_view_->IsImeComposing()) { 799 trailing_decorations.AddDecoration(vertical_edge_thickness(), 800 location_height, true, 0, item_padding, 801 item_padding, 0, keyword_hint_view_); 802 if (keyword_hint_view_->keyword() != keyword) 803 keyword_hint_view_->SetKeyword(keyword); 804 } 805 806 // Perform layout. 807 const int horizontal_edge_thickness = GetHorizontalEdgeThickness(); 808 int full_width = width() - horizontal_edge_thickness; 809 // The search button images are made to look as if they overlay the normal 810 // edge images, but to align things, the search button needs to be inset 811 // horizontally by 1 px. 812 const int kSearchButtonInset = 1; 813 const gfx::Size search_button_size(search_button_->GetPreferredSize()); 814 const int search_button_reserved_width = 815 search_button_size.width() + kSearchButtonInset; 816 full_width -= search_button_->visible() ? 817 search_button_reserved_width : horizontal_edge_thickness; 818 int entry_width = full_width; 819 leading_decorations.LayoutPass1(&entry_width); 820 trailing_decorations.LayoutPass1(&entry_width); 821 leading_decorations.LayoutPass2(&entry_width); 822 trailing_decorations.LayoutPass2(&entry_width); 823 824 int location_needed_width = omnibox_view_->TextWidth(); 825 int available_width = entry_width - location_needed_width; 826 // The bounds must be wide enough for all the decorations to fit. 827 gfx::Rect location_bounds( 828 horizontal_edge_thickness, vertical_edge_thickness(), 829 std::max(full_width, full_width - entry_width), location_height); 830 leading_decorations.LayoutPass3(&location_bounds, &available_width); 831 trailing_decorations.LayoutPass3(&location_bounds, &available_width); 832 833 // Layout out the suggested text view right aligned to the location 834 // entry. Only show the suggested text if we can fit the text from one 835 // character before the end of the selection to the end of the text and the 836 // suggested text. If we can't it means either the suggested text is too big, 837 // or the user has scrolled. 838 839 // TODO(sky): We could potentially adjust this to take into account suggested 840 // text to force using minimum size if necessary, but currently the chance of 841 // showing keyword hints and suggested text is minimal and we're not confident 842 // this is the right approach for suggested text. 843 844 int omnibox_view_margin = 0; 845 if (suggested_text_view_->visible()) { 846 // We do not display the suggested text when it contains a mix of RTL and 847 // LTR characters since this could mean the suggestion should be displayed 848 // in the middle of the string. 849 base::i18n::TextDirection text_direction = 850 base::i18n::GetStringDirection(omnibox_view_->GetText()); 851 if (text_direction != 852 base::i18n::GetStringDirection(suggested_text_view_->text())) 853 text_direction = base::i18n::UNKNOWN_DIRECTION; 854 855 // TODO(sky): need to layout when the user changes caret position. 856 gfx::Size suggested_text_size(suggested_text_view_->GetPreferredSize()); 857 if (suggested_text_size.width() > available_width || 858 text_direction == base::i18n::UNKNOWN_DIRECTION) { 859 // Hide the suggested text if the user has scrolled or we can't fit all 860 // the suggested text, or we have a mix of RTL and LTR characters. 861 suggested_text_view_->SetBounds(0, 0, 0, 0); 862 } else { 863 location_needed_width = 864 std::min(location_needed_width, 865 location_bounds.width() - suggested_text_size.width()); 866 gfx::Rect suggested_text_bounds(location_bounds.x(), location_bounds.y(), 867 suggested_text_size.width(), 868 location_bounds.height()); 869 // TODO(sky): figure out why this needs the -1. 870 suggested_text_bounds.Offset(location_needed_width - 1, 0); 871 872 // We reverse the order of the location entry and suggested text if: 873 // - Chrome is RTL but the text is fully LTR, or 874 // - Chrome is LTR but the text is fully RTL. 875 // This ensures the suggested text is correctly displayed to the right 876 // (or left) of the user text. 877 if (text_direction == (base::i18n::IsRTL() ? 878 base::i18n::LEFT_TO_RIGHT : base::i18n::RIGHT_TO_LEFT)) { 879 // TODO(sky): Figure out why we need the +1. 880 suggested_text_bounds.set_x(location_bounds.x() + 1); 881 // Use a margin to prevent omnibox text from overlapping suggest text. 882 omnibox_view_margin = suggested_text_bounds.width(); 883 } 884 suggested_text_view_->SetBoundsRect(suggested_text_bounds); 885 } 886 } 887 888 omnibox_view_->SetHorizontalMargins(0, omnibox_view_margin); 889 890 // Layout |ime_inline_autocomplete_view_| next to the user input. 891 if (ime_inline_autocomplete_view_->visible()) { 892 int width = 893 ime_inline_autocomplete_view_->font().GetStringWidth( 894 ime_inline_autocomplete_view_->text()) + 895 ime_inline_autocomplete_view_->GetInsets().width(); 896 // All the target languages (IMEs) are LTR, and we do not need to support 897 // RTL so far. In other words, no testable RTL environment so far. 898 int x = location_needed_width; 899 if (width > entry_width) 900 x = 0; 901 else if (location_needed_width + width > entry_width) 902 x = entry_width - width; 903 location_bounds.set_width(x); 904 ime_inline_autocomplete_view_->SetBounds( 905 location_bounds.right(), location_bounds.y(), 906 std::min(width, entry_width), location_bounds.height()); 907 } 908 909 omnibox_view_->SetBoundsRect(location_bounds); 910 911 search_button_->SetBoundsRect(gfx::Rect( 912 gfx::Point(width() - search_button_reserved_width, 0), 913 search_button_size)); 914} 915 916void LocationBarView::PaintChildren(gfx::Canvas* canvas) { 917 View::PaintChildren(canvas); 918 919 // For non-InstantExtendedAPI cases, if necessary, show focus rect. As we need 920 // the focus rect to appear on top of children we paint here rather than 921 // OnPaint(). 922 // Note: |Canvas::DrawFocusRect| paints a dashed rect with gray color. 923 if (show_focus_rect_ && HasFocus()) 924 canvas->DrawFocusRect(omnibox_view_->bounds()); 925} 926 927void LocationBarView::OnPaint(gfx::Canvas* canvas) { 928 View::OnPaint(canvas); 929 930 // Fill the location bar background color behind the border. Parts of the 931 // border images are meant to rest atop the toolbar background and parts atop 932 // the omnibox background, so we can't just blindly fill our entire bounds. 933 const int horizontal_edge_thickness = GetHorizontalEdgeThickness(); 934 if (!background_filling_painter_) { 935 gfx::Rect bounds(GetContentsBounds()); 936 bounds.Inset(horizontal_edge_thickness, vertical_edge_thickness()); 937 SkColor color(GetColor(ToolbarModel::NONE, BACKGROUND)); 938 if (is_popup_mode_) { 939 canvas->FillRect(bounds, color); 940 } else { 941 SkPaint paint; 942 paint.setStyle(SkPaint::kFill_Style); 943 paint.setColor(color); 944 const int kBorderCornerRadius = 2; 945 canvas->DrawRoundRect(bounds, kBorderCornerRadius, paint); 946 } 947 } 948 949 // Maximized popup windows don't draw the horizontal edges. We implement this 950 // by simply expanding the paint area outside the view by the edge thickness. 951 gfx::Rect background_rect(GetContentsBounds()); 952 if (is_popup_mode_ && (horizontal_edge_thickness == 0)) 953 background_rect.Inset(-kPopupEdgeThickness, 0); 954 views::Painter::PaintPainterAt(canvas, background_border_painter_.get(), 955 background_rect); 956 if (background_filling_painter_) 957 background_filling_painter_->Paint(canvas, size()); 958 959 if (!is_popup_mode_) 960 PaintPageActionBackgrounds(canvas); 961} 962 963void LocationBarView::SetShowFocusRect(bool show) { 964 show_focus_rect_ = show; 965 SchedulePaint(); 966} 967 968void LocationBarView::SelectAll() { 969 omnibox_view_->SelectAll(true); 970} 971 972views::ImageView* LocationBarView::GetLocationIconView() { 973 return location_icon_view_; 974} 975 976const views::ImageView* LocationBarView::GetLocationIconView() const { 977 return location_icon_view_; 978} 979 980views::View* LocationBarView::GetLocationBarAnchor() { 981 return GetLocationIconView(); 982} 983 984gfx::Point LocationBarView::GetLocationBarAnchorPoint() const { 985 // The +1 in the next line creates a 1-px gap between icon and arrow tip. 986 gfx::Point icon_bottom(0, GetLocationIconView()->GetImageBounds().bottom() - 987 LocationBarView::kIconInternalPadding + 1); 988 gfx::Point icon_center(GetLocationIconView()->GetImageBounds().CenterPoint()); 989 gfx::Point point(icon_center.x(), icon_bottom.y()); 990 ConvertPointToTarget(GetLocationIconView(), this, &point); 991 return point; 992} 993 994views::View* LocationBarView::generated_credit_card_view() { 995 return generated_credit_card_view_; 996} 997 998void LocationBarView::Update(const WebContents* contents) { 999 mic_search_view_->SetVisible( 1000 !GetToolbarModel()->input_in_progress() && browser_ && 1001 browser_->search_model()->voice_search_supported()); 1002 RefreshContentSettingViews(); 1003 generated_credit_card_view_->Update(); 1004 ZoomBubbleView::CloseBubble(); 1005 RefreshZoomView(); 1006 RefreshPageActionViews(); 1007 RefreshScriptBubble(); 1008 RefreshTranslateIcon(); 1009 RefreshManagePasswordsIconView(); 1010 open_pdf_in_reader_view_->Update( 1011 GetToolbarModel()->input_in_progress() ? NULL : GetWebContents()); 1012 1013 bool star_enabled = browser_defaults::bookmarks_enabled && !is_popup_mode_ && 1014 star_view_ && !GetToolbarModel()->input_in_progress() && 1015 edit_bookmarks_enabled_.GetValue(); 1016 1017 command_updater()->UpdateCommandEnabled(IDC_BOOKMARK_PAGE, star_enabled); 1018 command_updater()->UpdateCommandEnabled(IDC_BOOKMARK_PAGE_FROM_STAR, 1019 star_enabled); 1020 if (star_view_) 1021 star_view_->SetVisible(star_enabled); 1022 1023 if (contents) 1024 omnibox_view_->OnTabChanged(contents); 1025 else 1026 omnibox_view_->Update(); 1027 1028 OnChanged(); // NOTE: Calls Layout(). 1029} 1030 1031void LocationBarView::OnChanged() { 1032 int icon_id = omnibox_view_->GetIcon(); 1033 location_icon_view_->SetImage(GetThemeProvider()->GetImageSkiaNamed(icon_id)); 1034 location_icon_view_->ShowTooltip(!GetOmniboxView()->IsEditingOrEmpty()); 1035 1036 ToolbarModel* toolbar_model = GetToolbarModel(); 1037 chrome::DisplaySearchButtonConditions conditions = 1038 chrome::GetDisplaySearchButtonConditions(); 1039 bool meets_conditions = 1040 (conditions == chrome::DISPLAY_SEARCH_BUTTON_ALWAYS) || 1041 ((conditions != chrome::DISPLAY_SEARCH_BUTTON_NEVER) && 1042 (toolbar_model->WouldPerformSearchTermReplacement(true) || 1043 ((conditions == chrome::DISPLAY_SEARCH_BUTTON_FOR_STR_OR_IIP) && 1044 toolbar_model->input_in_progress()))); 1045 search_button_->SetVisible(!is_popup_mode_ && meets_conditions); 1046 search_button_->SetImage( 1047 views::Button::STATE_NORMAL, 1048 *GetThemeProvider()->GetImageSkiaNamed((icon_id == IDR_OMNIBOX_SEARCH) ? 1049 IDR_OMNIBOX_SEARCH_BUTTON_LOUPE : IDR_OMNIBOX_SEARCH_BUTTON_ARROW)); 1050 1051 Layout(); 1052 SchedulePaint(); 1053} 1054 1055void LocationBarView::OnSetFocus() { 1056 GetFocusManager()->SetFocusedView(this); 1057} 1058 1059InstantController* LocationBarView::GetInstant() { 1060 return delegate_->GetInstant(); 1061} 1062 1063WebContents* LocationBarView::GetWebContents() { 1064 return delegate_->GetWebContents(); 1065} 1066 1067ToolbarModel* LocationBarView::GetToolbarModel() { 1068 return delegate_->GetToolbarModel(); 1069} 1070 1071const ToolbarModel* LocationBarView::GetToolbarModel() const { 1072 return delegate_->GetToolbarModel(); 1073} 1074 1075const char* LocationBarView::GetClassName() const { 1076 return kViewClassName; 1077} 1078 1079bool LocationBarView::HasFocus() const { 1080 return omnibox_view_->model()->has_focus(); 1081} 1082 1083void LocationBarView::GetAccessibleState(ui::AccessibleViewState* state) { 1084 if (!IsInitialized()) 1085 return; 1086 1087 state->role = ui::AccessibilityTypes::ROLE_LOCATION_BAR; 1088 state->name = l10n_util::GetStringUTF16(IDS_ACCNAME_LOCATION); 1089 state->value = omnibox_view_->GetText(); 1090 1091 base::string16::size_type entry_start; 1092 base::string16::size_type entry_end; 1093 omnibox_view_->GetSelectionBounds(&entry_start, &entry_end); 1094 state->selection_start = entry_start; 1095 state->selection_end = entry_end; 1096 1097 if (is_popup_mode_) { 1098 state->state |= ui::AccessibilityTypes::STATE_READONLY; 1099 } else { 1100 state->set_value_callback = 1101 base::Bind(&LocationBarView::AccessibilitySetValue, 1102 weak_ptr_factory_.GetWeakPtr()); 1103 } 1104} 1105 1106void LocationBarView::OnBoundsChanged(const gfx::Rect& previous_bounds) { 1107 if (browser_ && browser_->instant_controller() && parent()) 1108 browser_->instant_controller()->SetOmniboxBounds(bounds()); 1109 OmniboxPopupView* popup = omnibox_view_->model()->popup_model()->view(); 1110 if (popup->IsOpen()) 1111 popup->UpdatePopupAppearance(); 1112} 1113 1114void LocationBarView::ButtonPressed(views::Button* sender, 1115 const ui::Event& event) { 1116 if (sender == mic_search_view_) { 1117 command_updater()->ExecuteCommand(IDC_TOGGLE_SPEECH_INPUT); 1118 return; 1119 } 1120 1121 DCHECK_EQ(search_button_, sender); 1122 // TODO(pkasting): When macourteau adds UMA stats for this, wire them up here. 1123 omnibox_view_->model()->AcceptInput( 1124 ui::DispositionFromEventFlags(event.flags()), false); 1125} 1126 1127void LocationBarView::WriteDragDataForView(views::View* sender, 1128 const gfx::Point& press_pt, 1129 OSExchangeData* data) { 1130 DCHECK_NE(GetDragOperationsForView(sender, press_pt), 1131 ui::DragDropTypes::DRAG_NONE); 1132 1133 WebContents* web_contents = GetWebContents(); 1134 FaviconTabHelper* favicon_tab_helper = 1135 FaviconTabHelper::FromWebContents(web_contents); 1136 gfx::ImageSkia favicon = favicon_tab_helper->GetFavicon().AsImageSkia(); 1137 button_drag_utils::SetURLAndDragImage(web_contents->GetURL(), 1138 web_contents->GetTitle(), 1139 favicon, 1140 data, 1141 sender->GetWidget()); 1142} 1143 1144int LocationBarView::GetDragOperationsForView(views::View* sender, 1145 const gfx::Point& p) { 1146 DCHECK((sender == location_icon_view_) || (sender == ev_bubble_view_)); 1147 WebContents* web_contents = delegate_->GetWebContents(); 1148 return (web_contents && web_contents->GetURL().is_valid() && 1149 !GetOmniboxView()->IsEditingOrEmpty()) ? 1150 (ui::DragDropTypes::DRAG_COPY | ui::DragDropTypes::DRAG_LINK) : 1151 ui::DragDropTypes::DRAG_NONE; 1152} 1153 1154bool LocationBarView::CanStartDragForView(View* sender, 1155 const gfx::Point& press_pt, 1156 const gfx::Point& p) { 1157 return true; 1158} 1159 1160//////////////////////////////////////////////////////////////////////////////// 1161// LocationBarView, LocationBar implementation: 1162 1163void LocationBarView::ShowFirstRunBubble() { 1164 // Wait until search engines have loaded to show the first run bubble. 1165 TemplateURLService* url_service = 1166 TemplateURLServiceFactory::GetForProfile(profile_); 1167 if (!url_service->loaded()) { 1168 template_url_service_ = url_service; 1169 template_url_service_->AddObserver(this); 1170 template_url_service_->Load(); 1171 return; 1172 } 1173 ShowFirstRunBubbleInternal(); 1174} 1175 1176GURL LocationBarView::GetDestinationURL() const { 1177 return destination_url(); 1178} 1179 1180WindowOpenDisposition LocationBarView::GetWindowOpenDisposition() const { 1181 return disposition(); 1182} 1183 1184content::PageTransition LocationBarView::GetPageTransition() const { 1185 return transition(); 1186} 1187 1188void LocationBarView::AcceptInput() { 1189 omnibox_view_->model()->AcceptInput(CURRENT_TAB, false); 1190} 1191 1192void LocationBarView::FocusLocation(bool select_all) { 1193 omnibox_view_->SetFocus(); 1194 if (select_all) 1195 omnibox_view_->SelectAll(true); 1196} 1197 1198void LocationBarView::FocusSearch() { 1199 omnibox_view_->SetFocus(); 1200 omnibox_view_->SetForcedQuery(); 1201} 1202 1203void LocationBarView::SaveStateToContents(WebContents* contents) { 1204 omnibox_view_->SaveStateToTab(contents); 1205} 1206 1207void LocationBarView::Revert() { 1208 omnibox_view_->RevertAll(); 1209} 1210 1211const OmniboxView* LocationBarView::GetOmniboxView() const { 1212 return omnibox_view_; 1213} 1214 1215OmniboxView* LocationBarView::GetOmniboxView() { 1216 return omnibox_view_; 1217} 1218 1219LocationBarTesting* LocationBarView::GetLocationBarForTesting() { 1220 return this; 1221} 1222 1223int LocationBarView::PageActionCount() { 1224 return page_action_views_.size(); 1225} 1226 1227int LocationBarView::PageActionVisibleCount() { 1228 int result = 0; 1229 for (size_t i = 0; i < page_action_views_.size(); i++) { 1230 if (page_action_views_[i]->visible()) 1231 ++result; 1232 } 1233 return result; 1234} 1235 1236ExtensionAction* LocationBarView::GetPageAction(size_t index) { 1237 if (index < page_action_views_.size()) 1238 return page_action_views_[index]->image_view()->page_action(); 1239 1240 NOTREACHED(); 1241 return NULL; 1242} 1243 1244ExtensionAction* LocationBarView::GetVisiblePageAction(size_t index) { 1245 size_t current = 0; 1246 for (size_t i = 0; i < page_action_views_.size(); ++i) { 1247 if (page_action_views_[i]->visible()) { 1248 if (current == index) 1249 return page_action_views_[i]->image_view()->page_action(); 1250 1251 ++current; 1252 } 1253 } 1254 1255 NOTREACHED(); 1256 return NULL; 1257} 1258 1259void LocationBarView::TestPageActionPressed(size_t index) { 1260 size_t current = 0; 1261 for (size_t i = 0; i < page_action_views_.size(); ++i) { 1262 if (page_action_views_[i]->visible()) { 1263 if (current == index) { 1264 page_action_views_[i]->image_view()->ExecuteAction( 1265 ExtensionPopup::SHOW); 1266 return; 1267 } 1268 ++current; 1269 } 1270 } 1271 1272 NOTREACHED(); 1273} 1274 1275bool LocationBarView::GetBookmarkStarVisibility() { 1276 DCHECK(star_view_); 1277 return star_view_->visible(); 1278} 1279 1280void LocationBarView::OnTemplateURLServiceChanged() { 1281 template_url_service_->RemoveObserver(this); 1282 template_url_service_ = NULL; 1283 // If the browser is no longer active, let's not show the info bubble, as this 1284 // would make the browser the active window again. 1285 if (omnibox_view_ && omnibox_view_->GetWidget()->IsActive()) 1286 ShowFirstRunBubble(); 1287} 1288 1289void LocationBarView::Observe(int type, 1290 const content::NotificationSource& source, 1291 const content::NotificationDetails& details) { 1292 switch (type) { 1293 case chrome::NOTIFICATION_EXTENSION_LOCATION_BAR_UPDATED: { 1294 // Only update if the updated action box was for the active tab contents. 1295 WebContents* target_tab = content::Details<WebContents>(details).ptr(); 1296 if (target_tab == GetWebContents()) 1297 UpdatePageActions(); 1298 break; 1299 } 1300 1301 default: 1302 NOTREACHED() << "Unexpected notification."; 1303 } 1304} 1305 1306void LocationBarView::ModelChanged(const SearchModel::State& old_state, 1307 const SearchModel::State& new_state) { 1308 const bool visible = !GetToolbarModel()->input_in_progress() && 1309 new_state.voice_search_supported; 1310 if (mic_search_view_->visible() != visible) { 1311 mic_search_view_->SetVisible(visible); 1312 Layout(); 1313 } 1314} 1315 1316int LocationBarView::GetInternalHeight(bool use_preferred_size) { 1317 int total_height = 1318 use_preferred_size ? GetPreferredSize().height() : height(); 1319 return std::max(total_height - (vertical_edge_thickness() * 2), 0); 1320} 1321 1322//////////////////////////////////////////////////////////////////////////////// 1323// LocationBarView, private: 1324 1325// static 1326int LocationBarView::GetBuiltInHorizontalPaddingForChildViews() { 1327 return (ui::GetDisplayLayout() == ui::LAYOUT_TOUCH) ? 1328 GetItemPadding() / 2 : 0; 1329} 1330 1331int LocationBarView::GetHorizontalEdgeThickness() const { 1332 // In maximized popup mode, there isn't any edge. 1333 return (is_popup_mode_ && browser_ && browser_->window() && 1334 browser_->window()->IsMaximized()) ? 0 : vertical_edge_thickness(); 1335} 1336 1337bool LocationBarView::RefreshContentSettingViews() { 1338 bool visibility_changed = false; 1339 for (ContentSettingViews::const_iterator i(content_setting_views_.begin()); 1340 i != content_setting_views_.end(); ++i) { 1341 const bool was_visible = (*i)->visible(); 1342 (*i)->Update(GetToolbarModel()->input_in_progress() ? 1343 NULL : GetWebContents()); 1344 if (was_visible != (*i)->visible()) 1345 visibility_changed = true; 1346 } 1347 return visibility_changed; 1348} 1349 1350void LocationBarView::DeletePageActionViews() { 1351 for (PageActionViews::const_iterator i(page_action_views_.begin()); 1352 i != page_action_views_.end(); ++i) 1353 RemoveChildView(*i); 1354 STLDeleteElements(&page_action_views_); 1355} 1356 1357bool LocationBarView::RefreshPageActionViews() { 1358 if (is_popup_mode_) 1359 return false; 1360 1361 bool changed = false; 1362 1363 // Remember the previous visibility of the page actions so that we can 1364 // notify when this changes. 1365 std::map<ExtensionAction*, bool> old_visibility; 1366 for (PageActionViews::const_iterator i(page_action_views_.begin()); 1367 i != page_action_views_.end(); ++i) { 1368 old_visibility[(*i)->image_view()->page_action()] = (*i)->visible(); 1369 } 1370 1371 std::vector<ExtensionAction*> new_page_actions; 1372 1373 WebContents* contents = delegate_->GetWebContents(); 1374 if (contents) { 1375 extensions::TabHelper* extensions_tab_helper = 1376 extensions::TabHelper::FromWebContents(contents); 1377 extensions::LocationBarController* controller = 1378 extensions_tab_helper->location_bar_controller(); 1379 new_page_actions = controller->GetCurrentActions(); 1380 } 1381 1382 // On startup we sometimes haven't loaded any extensions. This makes sure 1383 // we catch up when the extensions (and any page actions) load. 1384 if (page_actions_ != new_page_actions) { 1385 changed = true; 1386 1387 page_actions_.swap(new_page_actions); 1388 DeletePageActionViews(); // Delete the old views (if any). 1389 1390 page_action_views_.resize(page_actions_.size()); 1391 View* right_anchor = open_pdf_in_reader_view_; 1392 if (!right_anchor) 1393 right_anchor = star_view_; 1394 if (!right_anchor) 1395 right_anchor = script_bubble_icon_view_; 1396 DCHECK(right_anchor); 1397 1398 // Add the page actions in reverse order, so that the child views are 1399 // inserted in left-to-right order for accessibility. 1400 for (int i = page_actions_.size() - 1; i >= 0; --i) { 1401 page_action_views_[i] = new PageActionWithBadgeView( 1402 delegate_->CreatePageActionImageView(this, page_actions_[i])); 1403 page_action_views_[i]->SetVisible(false); 1404 AddChildViewAt(page_action_views_[i], GetIndexOf(right_anchor)); 1405 } 1406 } 1407 1408 if (!page_action_views_.empty() && contents) { 1409 Browser* browser = chrome::FindBrowserWithWebContents(contents); 1410 GURL url = browser->tab_strip_model()->GetActiveWebContents()->GetURL(); 1411 1412 for (PageActionViews::const_iterator i(page_action_views_.begin()); 1413 i != page_action_views_.end(); ++i) { 1414 (*i)->UpdateVisibility( 1415 GetToolbarModel()->input_in_progress() ? NULL : contents, url); 1416 1417 // Check if the visibility of the action changed and notify if it did. 1418 ExtensionAction* action = (*i)->image_view()->page_action(); 1419 if (old_visibility.find(action) == old_visibility.end() || 1420 old_visibility[action] != (*i)->visible()) { 1421 changed = true; 1422 content::NotificationService::current()->Notify( 1423 chrome::NOTIFICATION_EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED, 1424 content::Source<ExtensionAction>(action), 1425 content::Details<WebContents>(contents)); 1426 } 1427 } 1428 } 1429 return changed; 1430} 1431 1432size_t LocationBarView::ScriptBubbleScriptsRunning() { 1433 WebContents* contents = delegate_->GetWebContents(); 1434 if (!contents) 1435 return false; 1436 extensions::TabHelper* extensions_tab_helper = 1437 extensions::TabHelper::FromWebContents(contents); 1438 if (!extensions_tab_helper) 1439 return false; 1440 extensions::ScriptBubbleController* script_bubble_controller = 1441 extensions_tab_helper->script_bubble_controller(); 1442 if (!script_bubble_controller) 1443 return false; 1444 size_t script_count = 1445 script_bubble_controller->extensions_running_scripts().size(); 1446 return script_count; 1447} 1448 1449bool LocationBarView::RefreshScriptBubble() { 1450 if (!script_bubble_icon_view_) 1451 return false; 1452 size_t script_count = ScriptBubbleScriptsRunning(); 1453 const bool was_visible = script_bubble_icon_view_->visible(); 1454 script_bubble_icon_view_->SetVisible(script_count > 0); 1455 if (script_count > 0) 1456 script_bubble_icon_view_->SetScriptCount(script_count); 1457 return was_visible != script_bubble_icon_view_->visible(); 1458} 1459 1460bool LocationBarView::RefreshZoomView() { 1461 DCHECK(zoom_view_); 1462 WebContents* web_contents = GetWebContents(); 1463 if (!web_contents) 1464 return false; 1465 const bool was_visible = zoom_view_->visible(); 1466 zoom_view_->Update(ZoomController::FromWebContents(web_contents)); 1467 return was_visible != zoom_view_->visible(); 1468} 1469 1470bool LocationBarView::RefreshManagePasswordsIconView() { 1471 DCHECK(manage_passwords_icon_view_); 1472 WebContents* web_contents = GetWebContents(); 1473 if (!web_contents) 1474 return false; 1475 const bool was_visible = manage_passwords_icon_view_->visible(); 1476 manage_passwords_icon_view_->Update( 1477 ManagePasswordsBubbleUIController::FromWebContents(web_contents)); 1478 return was_visible != manage_passwords_icon_view_->visible(); 1479} 1480 1481void LocationBarView::RefreshTranslateIcon() { 1482 WebContents* web_contents = GetWebContents(); 1483 if (!web_contents || !CommandLine::ForCurrentProcess()->HasSwitch( 1484 switches::kEnableTranslateNewUX)) 1485 return; 1486 LanguageState& language_state = TranslateTabHelper::FromWebContents( 1487 web_contents)->language_state(); 1488 bool enabled = language_state.translate_enabled(); 1489 command_updater()->UpdateCommandEnabled(IDC_TRANSLATE_PAGE, enabled); 1490 translate_icon_view_->SetVisible(enabled); 1491 translate_icon_view_->SetToggled(language_state.IsPageTranslated()); 1492} 1493 1494void LocationBarView::ShowManagePasswordsBubbleIfNeeded() { 1495 DCHECK(manage_passwords_icon_view_); 1496 WebContents* web_contents = GetWebContents(); 1497 if (!web_contents) 1498 return; 1499 manage_passwords_icon_view_->ShowBubbleIfNeeded( 1500 ManagePasswordsBubbleUIController::FromWebContents(web_contents)); 1501} 1502 1503bool LocationBarView::HasValidSuggestText() const { 1504 return suggested_text_view_->visible() && 1505 !suggested_text_view_->size().IsEmpty(); 1506} 1507 1508void LocationBarView::ShowFirstRunBubbleInternal() { 1509#if !defined(OS_CHROMEOS) 1510 // First run bubble doesn't make sense for Chrome OS. 1511 Browser* browser = GetBrowserFromDelegate(delegate_); 1512 if (!browser) 1513 return; // Possible when browser is shutting down. 1514 1515 FirstRunBubble::ShowBubble(browser, GetLocationBarAnchor()); 1516#endif 1517} 1518 1519void LocationBarView::PaintPageActionBackgrounds(gfx::Canvas* canvas) { 1520 WebContents* web_contents = GetWebContents(); 1521 // web_contents may be NULL while the browser is shutting down. 1522 if (!web_contents) 1523 return; 1524 1525 const int32 tab_id = SessionID::IdForTab(web_contents); 1526 const ToolbarModel::SecurityLevel security_level = 1527 GetToolbarModel()->GetSecurityLevel(false); 1528 const SkColor text_color = GetColor(security_level, TEXT); 1529 const SkColor background_color = GetColor(security_level, BACKGROUND); 1530 1531 for (PageActionViews::const_iterator 1532 page_action_view = page_action_views_.begin(); 1533 page_action_view != page_action_views_.end(); 1534 ++page_action_view) { 1535 gfx::Rect bounds = (*page_action_view)->bounds(); 1536 int horizontal_padding = 1537 GetItemPadding() - GetBuiltInHorizontalPaddingForChildViews(); 1538 // Make the bounding rectangle include the whole vertical range of the 1539 // location bar, and the mid-point pixels between adjacent page actions. 1540 // 1541 // For odd horizontal_paddings, "horizontal_padding + 1" includes the 1542 // mid-point between two page actions in the bounding rectangle. For even 1543 // paddings, the +1 is dropped, which is right since there is no pixel at 1544 // the mid-point. 1545 bounds.Inset(-(horizontal_padding + 1) / 2, 0); 1546 location_bar_util::PaintExtensionActionBackground( 1547 *(*page_action_view)->image_view()->page_action(), 1548 tab_id, canvas, bounds, text_color, background_color); 1549 } 1550} 1551 1552void LocationBarView::AccessibilitySetValue(const base::string16& new_value) { 1553 omnibox_view_->SetUserText(new_value, new_value, true); 1554} 1555