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