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