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