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