[Extensions] Make extension message bubble factory platform-abstract
[chromium-blink-merge.git] / chrome / browser / ui / views / toolbar / toolbar_view.cc
blobdf70ca44a09a23a539a0f0b654a5b3dd11a9b069
1 // Copyright 2013 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.
5 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
7 #include <algorithm>
9 #include "base/command_line.h"
10 #include "base/i18n/number_formatting.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/trace_event/trace_event.h"
14 #include "chrome/app/chrome_command_ids.h"
15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/command_updater.h"
17 #include "chrome/browser/extensions/extension_commands_global_registry.h"
18 #include "chrome/browser/extensions/extension_util.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/themes/theme_service.h"
21 #include "chrome/browser/ui/browser.h"
22 #include "chrome/browser/ui/browser_command_controller.h"
23 #include "chrome/browser/ui/browser_commands.h"
24 #include "chrome/browser/ui/browser_content_setting_bubble_model_delegate.h"
25 #include "chrome/browser/ui/browser_instant_controller.h"
26 #include "chrome/browser/ui/browser_tabstrip.h"
27 #include "chrome/browser/ui/browser_window.h"
28 #include "chrome/browser/ui/global_error/global_error_service.h"
29 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
30 #include "chrome/browser/ui/omnibox/omnibox_view.h"
31 #include "chrome/browser/ui/tabs/tab_strip_model.h"
32 #include "chrome/browser/ui/toolbar/wrench_menu_model.h"
33 #include "chrome/browser/ui/view_ids.h"
34 #include "chrome/browser/ui/views/extensions/extension_popup.h"
35 #include "chrome/browser/ui/views/frame/browser_view.h"
36 #include "chrome/browser/ui/views/location_bar/page_action_image_view.h"
37 #include "chrome/browser/ui/views/location_bar/page_action_with_badge_view.h"
38 #include "chrome/browser/ui/views/location_bar/star_view.h"
39 #include "chrome/browser/ui/views/location_bar/translate_icon_view.h"
40 #include "chrome/browser/ui/views/outdated_upgrade_bubble_view.h"
41 #include "chrome/browser/ui/views/toolbar/back_button.h"
42 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h"
43 #include "chrome/browser/ui/views/toolbar/home_button.h"
44 #include "chrome/browser/ui/views/toolbar/reload_button.h"
45 #include "chrome/browser/ui/views/toolbar/toolbar_button.h"
46 #include "chrome/browser/ui/views/toolbar/wrench_menu.h"
47 #include "chrome/browser/ui/views/toolbar/wrench_toolbar_button.h"
48 #include "chrome/common/chrome_switches.h"
49 #include "chrome/common/pref_names.h"
50 #include "chrome/grit/chromium_strings.h"
51 #include "chrome/grit/generated_resources.h"
52 #include "content/public/browser/browser_accessibility_state.h"
53 #include "content/public/browser/notification_service.h"
54 #include "content/public/browser/render_view_host.h"
55 #include "content/public/browser/user_metrics.h"
56 #include "content/public/browser/web_contents.h"
57 #include "grit/theme_resources.h"
58 #include "ui/accessibility/ax_view_state.h"
59 #include "ui/base/l10n/l10n_util.h"
60 #include "ui/base/theme_provider.h"
61 #include "ui/base/window_open_disposition.h"
62 #include "ui/compositor/layer.h"
63 #include "ui/gfx/canvas.h"
64 #include "ui/gfx/image/canvas_image_source.h"
65 #include "ui/keyboard/keyboard_controller.h"
66 #include "ui/native_theme/native_theme_aura.h"
67 #include "ui/views/controls/menu/menu_listener.h"
68 #include "ui/views/focus/view_storage.h"
69 #include "ui/views/view_targeter.h"
70 #include "ui/views/widget/tooltip_manager.h"
71 #include "ui/views/widget/widget.h"
72 #include "ui/views/window/non_client_view.h"
74 #if defined(OS_WIN)
75 #include "chrome/browser/recovery/recovery_install_global_error_factory.h"
76 #include "chrome/browser/ui/views/conflicting_module_view_win.h"
77 #include "chrome/browser/ui/views/critical_notification_bubble_view.h"
78 #endif
80 #if !defined(OS_CHROMEOS)
81 #include "chrome/browser/signin/signin_global_error_factory.h"
82 #include "chrome/browser/sync/sync_global_error_factory.h"
83 #endif
85 #if defined(USE_ASH)
86 #include "ash/shell.h"
87 #endif
89 using base::UserMetricsAction;
90 using content::WebContents;
92 namespace {
94 // The edge graphics have some built-in spacing/shadowing, so we have to adjust
95 // our spacing to make it match.
96 const int kLeftEdgeSpacing = 3;
97 const int kRightEdgeSpacing = 2;
99 // Ash doesn't use a rounded content area and its top edge has an extra shadow.
100 const int kContentShadowHeightAsh = 2;
102 // Non-ash uses a rounded content area with no shadow in the assets.
103 const int kContentShadowHeight = 0;
105 #if !defined(OS_CHROMEOS)
106 bool HasAshShell() {
107 #if defined(USE_ASH)
108 return ash::Shell::HasInstance();
109 #else
110 return false;
111 #endif // USE_ASH
113 #endif // OS_CHROMEOS
115 } // namespace
117 // static
118 const char ToolbarView::kViewClassName[] = "ToolbarView";
120 ////////////////////////////////////////////////////////////////////////////////
121 // ToolbarView, public:
123 ToolbarView::ToolbarView(Browser* browser)
124 : back_(NULL),
125 forward_(NULL),
126 reload_(NULL),
127 home_(NULL),
128 location_bar_(NULL),
129 browser_actions_(NULL),
130 app_menu_(NULL),
131 browser_(browser),
132 badge_controller_(browser->profile(), this) {
133 set_id(VIEW_ID_TOOLBAR);
135 SetEventTargeter(
136 scoped_ptr<views::ViewTargeter>(new views::ViewTargeter(this)));
138 chrome::AddCommandObserver(browser_, IDC_BACK, this);
139 chrome::AddCommandObserver(browser_, IDC_FORWARD, this);
140 chrome::AddCommandObserver(browser_, IDC_RELOAD, this);
141 chrome::AddCommandObserver(browser_, IDC_HOME, this);
142 chrome::AddCommandObserver(browser_, IDC_LOAD_NEW_TAB_PAGE, this);
144 display_mode_ = DISPLAYMODE_LOCATION;
145 if (browser->SupportsWindowFeature(Browser::FEATURE_TABSTRIP))
146 display_mode_ = DISPLAYMODE_NORMAL;
148 if (OutdatedUpgradeBubbleView::IsAvailable()) {
149 registrar_.Add(this, chrome::NOTIFICATION_OUTDATED_INSTALL,
150 content::NotificationService::AllSources());
151 registrar_.Add(this, chrome::NOTIFICATION_OUTDATED_INSTALL_NO_AU,
152 content::NotificationService::AllSources());
154 #if defined(OS_WIN)
155 registrar_.Add(this, chrome::NOTIFICATION_CRITICAL_UPGRADE_INSTALLED,
156 content::NotificationService::AllSources());
157 #endif
160 ToolbarView::~ToolbarView() {
161 // NOTE: Don't remove the command observers here. This object gets destroyed
162 // after the Browser (which owns the CommandUpdater), so the CommandUpdater is
163 // already gone.
166 void ToolbarView::Init() {
167 GetWidget()->AddObserver(this);
169 back_ = new BackButton(this, new BackForwardMenuModel(
170 browser_, BackForwardMenuModel::BACKWARD_MENU));
171 back_->set_triggerable_event_flags(
172 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_MIDDLE_MOUSE_BUTTON);
173 back_->set_tag(IDC_BACK);
174 back_->SetTooltipText(l10n_util::GetStringUTF16(IDS_TOOLTIP_BACK));
175 back_->SetAccessibleName(l10n_util::GetStringUTF16(IDS_ACCNAME_BACK));
176 back_->set_id(VIEW_ID_BACK_BUTTON);
177 back_->Init();
179 forward_ = new ToolbarButton(this, new BackForwardMenuModel(
180 browser_, BackForwardMenuModel::FORWARD_MENU));
181 forward_->set_triggerable_event_flags(
182 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_MIDDLE_MOUSE_BUTTON);
183 forward_->set_tag(IDC_FORWARD);
184 forward_->SetTooltipText(l10n_util::GetStringUTF16(IDS_TOOLTIP_FORWARD));
185 forward_->SetAccessibleName(l10n_util::GetStringUTF16(IDS_ACCNAME_FORWARD));
186 forward_->set_id(VIEW_ID_FORWARD_BUTTON);
187 forward_->Init();
189 location_bar_ = new LocationBarView(
190 browser_, browser_->profile(),
191 browser_->command_controller()->command_updater(), this,
192 display_mode_ == DISPLAYMODE_LOCATION);
194 reload_ = new ReloadButton(browser_->command_controller()->command_updater());
195 reload_->set_triggerable_event_flags(
196 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_MIDDLE_MOUSE_BUTTON);
197 reload_->set_tag(IDC_RELOAD);
198 reload_->SetAccessibleName(l10n_util::GetStringUTF16(IDS_ACCNAME_RELOAD));
199 reload_->set_id(VIEW_ID_RELOAD_BUTTON);
200 reload_->Init();
202 home_ = new HomeButton(this, browser_);
203 home_->set_triggerable_event_flags(
204 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_MIDDLE_MOUSE_BUTTON);
205 home_->set_tag(IDC_HOME);
206 home_->SetTooltipText(l10n_util::GetStringUTF16(IDS_TOOLTIP_HOME));
207 home_->SetAccessibleName(l10n_util::GetStringUTF16(IDS_ACCNAME_HOME));
208 home_->set_id(VIEW_ID_HOME_BUTTON);
209 home_->Init();
211 browser_actions_ = new BrowserActionsContainer(
212 browser_,
213 NULL); // No master container for this one (it is master).
215 app_menu_ = new WrenchToolbarButton(this);
216 app_menu_->EnableCanvasFlippingForRTLUI(true);
217 app_menu_->SetAccessibleName(l10n_util::GetStringUTF16(IDS_ACCNAME_APP));
218 app_menu_->SetTooltipText(l10n_util::GetStringUTF16(IDS_APPMENU_TOOLTIP));
219 app_menu_->set_id(VIEW_ID_APP_MENU);
221 // Always add children in order from left to right, for accessibility.
222 AddChildView(back_);
223 AddChildView(forward_);
224 AddChildView(reload_);
225 AddChildView(home_);
226 AddChildView(location_bar_);
227 AddChildView(browser_actions_);
228 AddChildView(app_menu_);
230 LoadImages();
232 // Start global error services now so we badge the menu correctly.
233 #if !defined(OS_CHROMEOS)
234 if (!HasAshShell()) {
235 SigninGlobalErrorFactory::GetForProfile(browser_->profile());
236 #if !defined(OS_ANDROID)
237 SyncGlobalErrorFactory::GetForProfile(browser_->profile());
238 #endif
241 #if defined(OS_WIN)
242 RecoveryInstallGlobalErrorFactory::GetForProfile(browser_->profile());
243 #endif
244 #endif // OS_CHROMEOS
246 // Add any necessary badges to the menu item based on the system state.
247 // Do this after |app_menu_| has been added as a bubble may be shown that
248 // needs the widget (widget found by way of app_menu_->GetWidget()).
249 badge_controller_.UpdateDelegate();
251 location_bar_->Init();
253 show_home_button_.Init(prefs::kShowHomeButton,
254 browser_->profile()->GetPrefs(),
255 base::Bind(&ToolbarView::OnShowHomeButtonChanged,
256 base::Unretained(this)));
258 browser_actions_->Init();
260 // Accessibility specific tooltip text.
261 if (content::BrowserAccessibilityState::GetInstance()->
262 IsAccessibleBrowser()) {
263 back_->SetTooltipText(
264 l10n_util::GetStringUTF16(IDS_ACCNAME_TOOLTIP_BACK));
265 forward_->SetTooltipText(
266 l10n_util::GetStringUTF16(IDS_ACCNAME_TOOLTIP_FORWARD));
270 void ToolbarView::OnWidgetActivationChanged(views::Widget* widget,
271 bool active) {
272 extensions::ExtensionCommandsGlobalRegistry* registry =
273 extensions::ExtensionCommandsGlobalRegistry::Get(browser_->profile());
274 if (active) {
275 registry->set_registry_for_active_window(
276 browser_actions_->extension_keybinding_registry());
277 } else if (registry->registry_for_active_window() ==
278 browser_actions_->extension_keybinding_registry()) {
279 registry->set_registry_for_active_window(nullptr);
283 void ToolbarView::Update(WebContents* tab) {
284 if (location_bar_)
285 location_bar_->Update(tab);
286 if (browser_actions_)
287 browser_actions_->RefreshToolbarActionViews();
288 if (reload_)
289 reload_->set_menu_enabled(chrome::IsDebuggerAttachedToCurrentTab(browser_));
292 void ToolbarView::ResetTabState(WebContents* tab) {
293 if (location_bar_)
294 location_bar_->ResetTabState(tab);
297 void ToolbarView::SetPaneFocusAndFocusAppMenu() {
298 SetPaneFocus(app_menu_);
301 bool ToolbarView::IsAppMenuFocused() {
302 return app_menu_->HasFocus();
305 void ToolbarView::AddMenuListener(views::MenuListener* listener) {
306 menu_listeners_.AddObserver(listener);
309 void ToolbarView::RemoveMenuListener(views::MenuListener* listener) {
310 menu_listeners_.RemoveObserver(listener);
313 views::View* ToolbarView::GetBookmarkBubbleAnchor() {
314 views::View* star_view = location_bar()->star_view();
315 return (star_view && star_view->visible()) ? star_view : app_menu_;
318 views::View* ToolbarView::GetTranslateBubbleAnchor() {
319 views::View* translate_icon_view = location_bar()->translate_icon_view();
320 return (translate_icon_view && translate_icon_view->visible()) ?
321 translate_icon_view : app_menu_;
324 void ToolbarView::ExecuteExtensionCommand(
325 const extensions::Extension* extension,
326 const extensions::Command& command) {
327 browser_actions_->ExecuteExtensionCommand(extension, command);
330 void ToolbarView::ShowAppMenu(bool for_drop) {
331 if (wrench_menu_.get() && wrench_menu_->IsShowing())
332 return;
334 #if defined(USE_AURA)
335 if (keyboard::KeyboardController::GetInstance() &&
336 keyboard::KeyboardController::GetInstance()->keyboard_visible()) {
337 keyboard::KeyboardController::GetInstance()->HideKeyboard(
338 keyboard::KeyboardController::HIDE_REASON_AUTOMATIC);
340 #endif
342 wrench_menu_.reset(
343 new WrenchMenu(browser_, for_drop ? WrenchMenu::FOR_DROP : 0));
344 wrench_menu_model_.reset(new WrenchMenuModel(this, browser_));
345 wrench_menu_->Init(wrench_menu_model_.get());
347 FOR_EACH_OBSERVER(views::MenuListener, menu_listeners_, OnMenuOpened());
349 wrench_menu_->RunMenu(app_menu_);
352 ////////////////////////////////////////////////////////////////////////////////
353 // ToolbarView, AccessiblePaneView overrides:
355 bool ToolbarView::SetPaneFocus(views::View* initial_focus) {
356 if (!AccessiblePaneView::SetPaneFocus(initial_focus))
357 return false;
359 location_bar_->SetShowFocusRect(true);
360 return true;
363 void ToolbarView::GetAccessibleState(ui::AXViewState* state) {
364 state->role = ui::AX_ROLE_TOOLBAR;
365 state->name = l10n_util::GetStringUTF16(IDS_ACCNAME_TOOLBAR);
368 ////////////////////////////////////////////////////////////////////////////////
369 // ToolbarView, Menu::Delegate overrides:
371 bool ToolbarView::GetAcceleratorInfo(int id, ui::Accelerator* accel) {
372 return GetWidget()->GetAccelerator(id, accel);
375 ////////////////////////////////////////////////////////////////////////////////
376 // ToolbarView, views::MenuButtonListener implementation:
378 void ToolbarView::OnMenuButtonClicked(views::View* source,
379 const gfx::Point& point) {
380 TRACE_EVENT0("views", "ToolbarView::OnMenuButtonClicked");
381 DCHECK_EQ(VIEW_ID_APP_MENU, source->id());
382 ShowAppMenu(false); // Not for drop.
385 ////////////////////////////////////////////////////////////////////////////////
386 // ToolbarView, LocationBarView::Delegate implementation:
388 WebContents* ToolbarView::GetWebContents() {
389 return browser_->tab_strip_model()->GetActiveWebContents();
392 ToolbarModel* ToolbarView::GetToolbarModel() {
393 return browser_->toolbar_model();
396 const ToolbarModel* ToolbarView::GetToolbarModel() const {
397 return browser_->toolbar_model();
400 InstantController* ToolbarView::GetInstant() {
401 return browser_->instant_controller() ?
402 browser_->instant_controller()->instant() : NULL;
405 ContentSettingBubbleModelDelegate*
406 ToolbarView::GetContentSettingBubbleModelDelegate() {
407 return browser_->content_setting_bubble_model_delegate();
410 void ToolbarView::ShowWebsiteSettings(content::WebContents* web_contents,
411 const GURL& url,
412 const content::SSLStatus& ssl) {
413 chrome::ShowWebsiteSettings(browser_, web_contents, url, ssl);
416 views::Widget* ToolbarView::CreateViewsBubble(
417 views::BubbleDelegateView* bubble_delegate) {
418 return views::BubbleDelegateView::CreateBubble(bubble_delegate);
421 PageActionImageView* ToolbarView::CreatePageActionImageView(
422 LocationBarView* owner, ExtensionAction* action) {
423 return new PageActionImageView(owner, action, browser_);
426 ////////////////////////////////////////////////////////////////////////////////
427 // ToolbarView, CommandObserver implementation:
429 void ToolbarView::EnabledStateChangedForCommand(int id, bool enabled) {
430 views::Button* button = NULL;
431 switch (id) {
432 case IDC_BACK:
433 button = back_;
434 break;
435 case IDC_FORWARD:
436 button = forward_;
437 break;
438 case IDC_RELOAD:
439 button = reload_;
440 break;
441 case IDC_HOME:
442 button = home_;
443 break;
445 if (button)
446 button->SetEnabled(enabled);
449 ////////////////////////////////////////////////////////////////////////////////
450 // ToolbarView, views::Button::ButtonListener implementation:
452 void ToolbarView::ButtonPressed(views::Button* sender,
453 const ui::Event& event) {
454 chrome::ExecuteCommandWithDisposition(
455 browser_, sender->tag(), ui::DispositionFromEventFlags(event.flags()));
458 ////////////////////////////////////////////////////////////////////////////////
459 // ToolbarView, content::NotificationObserver implementation:
461 void ToolbarView::Observe(int type,
462 const content::NotificationSource& source,
463 const content::NotificationDetails& details) {
464 switch (type) {
465 case chrome::NOTIFICATION_OUTDATED_INSTALL:
466 ShowOutdatedInstallNotification(true);
467 break;
468 case chrome::NOTIFICATION_OUTDATED_INSTALL_NO_AU:
469 ShowOutdatedInstallNotification(false);
470 break;
471 #if defined(OS_WIN)
472 case chrome::NOTIFICATION_CRITICAL_UPGRADE_INSTALLED:
473 ShowCriticalNotification();
474 break;
475 #endif
476 default:
477 NOTREACHED();
481 ////////////////////////////////////////////////////////////////////////////////
482 // ToolbarView, ui::AcceleratorProvider implementation:
484 bool ToolbarView::GetAcceleratorForCommandId(int command_id,
485 ui::Accelerator* accelerator) {
486 return GetWidget()->GetAccelerator(command_id, accelerator);
489 ////////////////////////////////////////////////////////////////////////////////
490 // ToolbarView, views::View overrides:
492 gfx::Size ToolbarView::GetPreferredSize() const {
493 gfx::Size size(location_bar_->GetPreferredSize());
494 if (is_display_mode_normal()) {
495 int content_width = kLeftEdgeSpacing + back_->GetPreferredSize().width() +
496 forward_->GetPreferredSize().width() +
497 reload_->GetPreferredSize().width() +
498 (show_home_button_.GetValue() ? home_->GetPreferredSize().width() : 0) +
499 kStandardSpacing + browser_actions_->GetPreferredSize().width() +
500 app_menu_->GetPreferredSize().width() + kRightEdgeSpacing;
501 size.Enlarge(content_width, 0);
503 return SizeForContentSize(size);
506 gfx::Size ToolbarView::GetMinimumSize() const {
507 gfx::Size size(location_bar_->GetMinimumSize());
508 if (is_display_mode_normal()) {
509 int content_width = kLeftEdgeSpacing + back_->GetMinimumSize().width() +
510 forward_->GetMinimumSize().width() + reload_->GetMinimumSize().width() +
511 (show_home_button_.GetValue() ? home_->GetMinimumSize().width() : 0) +
512 kStandardSpacing + browser_actions_->GetMinimumSize().width() +
513 app_menu_->GetMinimumSize().width() + kRightEdgeSpacing;
514 size.Enlarge(content_width, 0);
516 return SizeForContentSize(size);
519 void ToolbarView::Layout() {
520 // If we have not been initialized yet just do nothing.
521 if (back_ == NULL)
522 return;
524 if (!is_display_mode_normal()) {
525 location_bar_->SetBounds(0, PopupTopSpacing(), width(),
526 location_bar_->GetPreferredSize().height());
527 return;
530 // We assume all child elements except the location bar are the same height.
531 // Set child_y such that buttons appear vertically centered. We put any excess
532 // padding above the buttons.
533 int child_height =
534 std::min(back_->GetPreferredSize().height(), height());
535 int child_y = (height() - child_height + 1) / 2;
537 // If the window is maximized, we extend the back button to the left so that
538 // clicking on the left-most pixel will activate the back button.
539 // TODO(abarth): If the window becomes maximized but is not resized,
540 // then Layout() might not be called and the back button
541 // will be slightly the wrong size. We should force a
542 // Layout() in this case.
543 // http://crbug.com/5540
544 bool maximized = browser_->window() && browser_->window()->IsMaximized();
545 int back_width = back_->GetPreferredSize().width();
546 if (maximized) {
547 back_->SetBounds(0, child_y, back_width + kLeftEdgeSpacing, child_height);
548 back_->SetLeadingMargin(kLeftEdgeSpacing);
549 } else {
550 back_->SetBounds(kLeftEdgeSpacing, child_y, back_width, child_height);
551 back_->SetLeadingMargin(0);
553 int next_element_x = back_->bounds().right();
555 forward_->SetBounds(next_element_x, child_y,
556 forward_->GetPreferredSize().width(), child_height);
557 next_element_x = forward_->bounds().right();
559 reload_->SetBounds(next_element_x, child_y,
560 reload_->GetPreferredSize().width(), child_height);
561 next_element_x = reload_->bounds().right();
563 if (show_home_button_.GetValue() ||
564 (browser_->is_app() && extensions::util::IsNewBookmarkAppsEnabled())) {
565 home_->SetVisible(true);
566 home_->SetBounds(next_element_x, child_y,
567 home_->GetPreferredSize().width(), child_height);
568 } else {
569 home_->SetVisible(false);
570 home_->SetBounds(next_element_x, child_y, 0, child_height);
572 next_element_x = home_->bounds().right() + kStandardSpacing;
574 int browser_actions_width = browser_actions_->GetPreferredSize().width();
575 int app_menu_width = app_menu_->GetPreferredSize().width();
576 int available_width = std::max(0, width() - kRightEdgeSpacing -
577 app_menu_width - browser_actions_width - next_element_x);
579 int location_height = location_bar_->GetPreferredSize().height();
580 int location_y = (height() - location_height + 1) / 2;
581 location_bar_->SetBounds(next_element_x, location_y,
582 std::max(available_width, 0), location_height);
583 next_element_x = location_bar_->bounds().right();
585 browser_actions_->SetBounds(
586 next_element_x, child_y, browser_actions_width, child_height);
587 next_element_x = browser_actions_->bounds().right();
589 // The browser actions need to do a layout explicitly, because when an
590 // extension is loaded/unloaded/changed, BrowserActionContainer removes and
591 // re-adds everything, regardless of whether it has a page action. For a
592 // page action, browser action bounds do not change, as a result of which
593 // SetBounds does not do a layout at all.
594 // TODO(sidchat): Rework the above behavior so that explicit layout is not
595 // required.
596 browser_actions_->Layout();
598 // Extend the app menu to the screen's right edge in maximized mode just like
599 // we extend the back button to the left edge.
600 if (maximized)
601 app_menu_width += kRightEdgeSpacing;
602 app_menu_->SetBounds(next_element_x, child_y, app_menu_width, child_height);
605 void ToolbarView::OnPaint(gfx::Canvas* canvas) {
606 View::OnPaint(canvas);
608 if (is_display_mode_normal())
609 return;
611 // For glass, we need to draw a black line below the location bar to separate
612 // it from the content area. For non-glass, the NonClientView draws the
613 // toolbar background below the location bar for us.
614 // NOTE: Keep this in sync with BrowserView::GetInfoBarSeparatorColor()!
615 if (GetWidget()->ShouldWindowContentsBeTransparent())
616 canvas->FillRect(gfx::Rect(0, height() - 1, width(), 1), SK_ColorBLACK);
619 void ToolbarView::OnThemeChanged() {
620 LoadImages();
623 const char* ToolbarView::GetClassName() const {
624 return kViewClassName;
627 bool ToolbarView::AcceleratorPressed(const ui::Accelerator& accelerator) {
628 const views::View* focused_view = focus_manager()->GetFocusedView();
629 if (focused_view && (focused_view->id() == VIEW_ID_OMNIBOX))
630 return false; // Let the omnibox handle all accelerator events.
631 return AccessiblePaneView::AcceleratorPressed(accelerator);
634 bool ToolbarView::IsWrenchMenuShowing() const {
635 return wrench_menu_.get() && wrench_menu_->IsShowing();
638 bool ToolbarView::ShouldPaintBackground() const {
639 return display_mode_ == DISPLAYMODE_NORMAL;
642 ////////////////////////////////////////////////////////////////////////////////
643 // ToolbarView, protected:
645 // Override this so that when the user presses F6 to rotate toolbar panes,
646 // the location bar gets focus, not the first control in the toolbar - and
647 // also so that it selects all content in the location bar.
648 bool ToolbarView::SetPaneFocusAndFocusDefault() {
649 if (!location_bar_->HasFocus()) {
650 SetPaneFocus(location_bar_);
651 location_bar_->FocusLocation(true);
652 return true;
655 if (!AccessiblePaneView::SetPaneFocusAndFocusDefault())
656 return false;
657 browser_->window()->RotatePaneFocus(true);
658 return true;
661 void ToolbarView::RemovePaneFocus() {
662 AccessiblePaneView::RemovePaneFocus();
663 location_bar_->SetShowFocusRect(false);
666 ////////////////////////////////////////////////////////////////////////////////
667 // ToolbarView, private:
669 // views::ViewTargeterDelegate:
670 bool ToolbarView::DoesIntersectRect(const views::View* target,
671 const gfx::Rect& rect) const {
672 CHECK_EQ(target, this);
674 // Fall through to the tab strip above us if none of |rect| intersects
675 // with this view (intersection with the top shadow edge does not
676 // count as intersection with this view).
677 if (rect.bottom() < content_shadow_height())
678 return false;
679 // Otherwise let our superclass take care of it.
680 return ViewTargeterDelegate::DoesIntersectRect(this, rect);
683 void ToolbarView::UpdateBadgeSeverity(WrenchMenuBadgeController::BadgeType type,
684 WrenchIconPainter::Severity severity,
685 bool animate) {
686 // Showing the bubble requires |app_menu_| to be in a widget. See comment
687 // in ConflictingModuleView for details.
688 DCHECK(app_menu_->GetWidget());
690 base::string16 accname_app = l10n_util::GetStringUTF16(IDS_ACCNAME_APP);
691 if (type == WrenchMenuBadgeController::BADGE_TYPE_UPGRADE_NOTIFICATION) {
692 accname_app = l10n_util::GetStringFUTF16(
693 IDS_ACCNAME_APP_UPGRADE_RECOMMENDED, accname_app);
695 app_menu_->SetAccessibleName(accname_app);
696 app_menu_->SetSeverity(severity, animate);
698 // Keep track of whether we were showing the badge before, so we don't send
699 // multiple UMA events for example when multiple Chrome windows are open.
700 static bool incompatibility_badge_showing = false;
701 // Save the old value before resetting it.
702 bool was_showing = incompatibility_badge_showing;
703 incompatibility_badge_showing = false;
705 if (type == WrenchMenuBadgeController::BADGE_TYPE_INCOMPATIBILITY_WARNING) {
706 if (!was_showing) {
707 content::RecordAction(UserMetricsAction("ConflictBadge"));
708 #if defined(OS_WIN)
709 ConflictingModuleView::MaybeShow(browser_, app_menu_);
710 #endif
712 incompatibility_badge_showing = true;
713 return;
717 int ToolbarView::PopupTopSpacing() const {
718 const int kPopupTopSpacingNonGlass = 3;
719 return GetWidget()->ShouldWindowContentsBeTransparent() ?
720 0 : kPopupTopSpacingNonGlass;
723 gfx::Size ToolbarView::SizeForContentSize(gfx::Size size) const {
724 if (is_display_mode_normal()) {
725 gfx::ImageSkia* normal_background =
726 GetThemeProvider()->GetImageSkiaNamed(IDR_CONTENT_TOP_CENTER);
727 size.SetToMax(
728 gfx::Size(0, normal_background->height() - content_shadow_height()));
729 } else if (size.height() == 0) {
730 // Location mode with a 0 height location bar. If on ash, expand by one
731 // pixel to show a border in the title bar, otherwise leave the size as zero
732 // height.
733 const int kAshBorderSpacing = 1;
734 if (browser_->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH)
735 size.Enlarge(0, kAshBorderSpacing);
736 } else {
737 const int kPopupBottomSpacingGlass = 1;
738 const int kPopupBottomSpacingNonGlass = 2;
739 size.Enlarge(
741 PopupTopSpacing() + (GetWidget()->ShouldWindowContentsBeTransparent() ?
742 kPopupBottomSpacingGlass : kPopupBottomSpacingNonGlass));
744 return size;
747 void ToolbarView::LoadImages() {
748 ui::ThemeProvider* tp = GetThemeProvider();
750 back_->SetImage(views::Button::STATE_NORMAL,
751 *(tp->GetImageSkiaNamed(IDR_BACK)));
752 back_->SetImage(views::Button::STATE_DISABLED,
753 *(tp->GetImageSkiaNamed(IDR_BACK_D)));
755 forward_->SetImage(views::Button::STATE_NORMAL,
756 *(tp->GetImageSkiaNamed(IDR_FORWARD)));
757 forward_->SetImage(views::Button::STATE_DISABLED,
758 *(tp->GetImageSkiaNamed(IDR_FORWARD_D)));
760 reload_->LoadImages();
762 home_->SetImage(views::Button::STATE_NORMAL,
763 *(tp->GetImageSkiaNamed(IDR_HOME)));
766 void ToolbarView::ShowCriticalNotification() {
767 #if defined(OS_WIN)
768 CriticalNotificationBubbleView* bubble_delegate =
769 new CriticalNotificationBubbleView(app_menu_);
770 views::BubbleDelegateView::CreateBubble(bubble_delegate)->Show();
771 #endif
774 void ToolbarView::ShowOutdatedInstallNotification(bool auto_update_enabled) {
775 if (OutdatedUpgradeBubbleView::IsAvailable()) {
776 OutdatedUpgradeBubbleView::ShowBubble(
777 app_menu_, browser_, auto_update_enabled);
781 void ToolbarView::OnShowHomeButtonChanged() {
782 Layout();
783 SchedulePaint();
786 int ToolbarView::content_shadow_height() const {
787 return browser_->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH ?
788 kContentShadowHeightAsh : kContentShadowHeight;