Chromium Code Reviews| Index: chrome/browser/ui/views/toolbar/browser_action_view.cc |
| diff --git a/chrome/browser/ui/views/toolbar/browser_action_view.cc b/chrome/browser/ui/views/toolbar/browser_action_view.cc |
| index 3f710dd4a01357a5400e8ab6d317c6c6ded95744..bdd05bf70c675c32e4206e8289a6a2f3538fab28 100644 |
| --- a/chrome/browser/ui/views/toolbar/browser_action_view.cc |
| +++ b/chrome/browser/ui/views/toolbar/browser_action_view.cc |
| @@ -6,31 +6,27 @@ |
| #include <string> |
| -#include "base/strings/utf_string_conversions.h" |
| #include "chrome/browser/chrome_notification_types.h" |
| -#include "chrome/browser/extensions/extension_action.h" |
| #include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/sessions/session_tab_helper.h" |
| #include "chrome/browser/themes/theme_service.h" |
| #include "chrome/browser/themes/theme_service_factory.h" |
| #include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h" |
| #include "chrome/browser/ui/view_ids.h" |
| +#include "chrome/browser/ui/views/extensions/extension_action_view_controller.h" |
| #include "chrome/browser/ui/views/frame/browser_view.h" |
| #include "chrome/browser/ui/views/toolbar/browser_actions_container.h" |
| #include "chrome/browser/ui/views/toolbar/toolbar_view.h" |
| #include "chrome/grit/generated_resources.h" |
| -#include "extensions/common/extension.h" |
| -#include "extensions/common/manifest_constants.h" |
| #include "grit/theme_resources.h" |
| #include "ui/accessibility/ax_view_state.h" |
| -#include "ui/base/l10n/l10n_util.h" |
| -#include "ui/base/resource/resource_bundle.h" |
| #include "ui/events/event.h" |
| #include "ui/gfx/image/image_skia.h" |
| #include "ui/gfx/image/image_skia_operations.h" |
| #include "ui/gfx/image/image_skia_source.h" |
| #include "ui/views/controls/button/label_button_border.h" |
| -using extensions::Extension; |
| using views::LabelButtonBorder; |
| namespace { |
| @@ -44,32 +40,25 @@ const int kBorderInset = 4; |
| //////////////////////////////////////////////////////////////////////////////// |
| // BrowserActionView |
| -BrowserActionView::BrowserActionView(const Extension* extension, |
| - ExtensionAction* extension_action, |
| - Browser* browser, |
| - BrowserActionView::Delegate* delegate) |
| +BrowserActionView::BrowserActionView( |
| + scoped_ptr<ToolbarActionViewController> view_controller, |
| + Browser* browser, |
| + BrowserActionView::Delegate* delegate) |
| : MenuButton(this, base::string16(), NULL, false), |
| - view_controller_(new ExtensionActionViewController( |
| - extension, |
| - browser, |
| - extension_action, |
| - this)), |
| + view_controller_(view_controller.Pass()), |
| + browser_(browser), |
| delegate_(delegate), |
| - called_registered_extension_command_(false), |
| - icon_observer_(NULL) { |
| + called_register_command_(false) { |
| set_id(VIEW_ID_BROWSER_ACTION); |
| + view_controller_->SetDelegate(this); |
| SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| - set_context_menu_controller(view_controller_.get()); |
| - set_drag_controller(delegate_); |
| - |
| - content::NotificationSource notification_source = |
| - content::Source<Profile>(browser->profile()->GetOriginalProfile()); |
| - registrar_.Add(this, |
| - extensions::NOTIFICATION_EXTENSION_COMMAND_ADDED, |
| - notification_source); |
| - registrar_.Add(this, |
| - extensions::NOTIFICATION_EXTENSION_COMMAND_REMOVED, |
| - notification_source); |
| + if (view_controller_->GetType() == |
| + ToolbarActionViewController::TYPE_EXTENSION_ACTION) { |
| + // Right now, only support dragging for extension actions (because the |
| + // underlying model doesn't support component actions). |
| + // TODO(devlin): |
|
Finnur
2014/10/17 09:57:02
TODO(devlin): Finish that thought? :)
Devlin
2014/10/17 16:33:33
Haha whoops. :) Done (though this comment actuall
|
| + set_drag_controller(delegate_); |
| + } |
| // We also listen for browser theme changes on linux because a switch from or |
| // to GTK requires that we regrab our browser action images. |
| @@ -87,10 +76,9 @@ BrowserActionView::~BrowserActionView() { |
| void BrowserActionView::ViewHierarchyChanged( |
| const ViewHierarchyChangedDetails& details) { |
| - if (details.is_add && !called_registered_extension_command_ && |
| - GetFocusManager()) { |
| + if (details.is_add && !called_register_command_ && GetFocusManager()) { |
| view_controller_->RegisterCommand(); |
| - called_registered_extension_command_ = true; |
| + called_register_command_ = true; |
| } |
| MenuButton::ViewHierarchyChanged(details); |
| @@ -108,9 +96,8 @@ gfx::Size BrowserActionView::GetPreferredSize() const { |
| void BrowserActionView::PaintChildren(gfx::Canvas* canvas, |
| const views::CullSet& cull_set) { |
| View::PaintChildren(canvas, cull_set); |
| - int tab_id = view_controller_->GetCurrentTabId(); |
| - if (tab_id >= 0) |
| - extension_action()->PaintBadge(canvas, GetLocalBounds(), tab_id); |
| + view_controller_->PaintExtra( |
| + canvas, GetLocalBounds(), GetCurrentWebContents()); |
| } |
| void BrowserActionView::GetAccessibleState(ui::AXViewState* state) { |
| @@ -124,73 +111,44 @@ void BrowserActionView::ButtonPressed(views::Button* sender, |
| } |
| void BrowserActionView::UpdateState() { |
| - int tab_id = view_controller_->GetCurrentTabId(); |
| - if (tab_id < 0) |
| + content::WebContents* web_contents = GetCurrentWebContents(); |
| + if (!web_contents || SessionTabHelper::IdForTab(web_contents) < 0) |
| return; |
| - if (!IsEnabled(tab_id)) |
| + bool enabled = view_controller_->IsEnabled(web_contents); |
| + if (!enabled) |
| SetState(views::CustomButton::STATE_DISABLED); |
| - gfx::ImageSkia icon = *view_controller_->GetIcon(tab_id).ToImageSkia(); |
| + gfx::ImageSkia icon = *view_controller_->GetIcon(web_contents).ToImageSkia(); |
| if (!icon.isNull()) { |
| - if (!extension_action()->GetIsVisible(tab_id)) |
| + if (!enabled) |
| icon = gfx::ImageSkiaOperations::CreateTransparentImage(icon, .25); |
| - ThemeService* theme = ThemeServiceFactory::GetForProfile( |
| - view_controller_->browser()->profile()); |
| + ThemeService* theme = |
| + ThemeServiceFactory::GetForProfile(browser_->profile()); |
| gfx::ImageSkia bg = *theme->GetImageSkiaNamed(IDR_BROWSER_ACTION); |
| SetImage(views::Button::STATE_NORMAL, |
| gfx::ImageSkiaOperations::CreateSuperimposedImage(bg, icon)); |
| } |
| - // If the browser action name is empty, show the extension name instead. |
| - std::string title = extension_action()->GetTitle(tab_id); |
| - base::string16 name = |
| - base::UTF8ToUTF16(title.empty() ? extension()->name() : title); |
| - SetTooltipText(name); |
| - SetAccessibleName(name); |
| + SetTooltipText(view_controller_->GetTooltip(web_contents)); |
| + SetAccessibleName(view_controller_->GetAccessibleName(web_contents)); |
| Layout(); // We need to layout since we may have added an icon as a result. |
| SchedulePaint(); |
| } |
| -bool BrowserActionView::IsPopup() { |
| - int tab_id = view_controller_->GetCurrentTabId(); |
| - return (tab_id < 0) ? false : extension_action()->HasPopup(tab_id); |
| -} |
| - |
| void BrowserActionView::Observe(int type, |
| const content::NotificationSource& source, |
| const content::NotificationDetails& details) { |
| - switch (type) { |
| - case extensions::NOTIFICATION_EXTENSION_COMMAND_ADDED: |
| - case extensions::NOTIFICATION_EXTENSION_COMMAND_REMOVED: { |
| - std::pair<const std::string, const std::string>* payload = |
| - content::Details<std::pair<const std::string, const std::string> >( |
| - details).ptr(); |
| - if (extension()->id() == payload->first && |
| - payload->second == |
| - extensions::manifest_values::kBrowserActionCommandEvent) { |
| - if (type == extensions::NOTIFICATION_EXTENSION_COMMAND_ADDED) |
| - view_controller_->RegisterCommand(); |
| - else |
| - view_controller_->UnregisterCommand(true); |
| - } |
| - break; |
| - } |
| - case chrome::NOTIFICATION_BROWSER_THEME_CHANGED: |
| - UpdateState(); |
| - break; |
| - default: |
| - NOTREACHED(); |
| - break; |
| - } |
| + DCHECK_EQ(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, type); |
| + UpdateState(); |
| } |
| bool BrowserActionView::Activate() { |
| - if (!IsPopup()) |
| + if (!view_controller_->HasPopup(GetCurrentWebContents())) |
| return true; |
| view_controller_->ExecuteActionByUser(); |
| @@ -207,14 +165,15 @@ bool BrowserActionView::Activate() { |
| bool BrowserActionView::OnMousePressed(const ui::MouseEvent& event) { |
| if (!event.IsRightMouseButton()) { |
| - return IsPopup() ? MenuButton::OnMousePressed(event) : |
| - LabelButton::OnMousePressed(event); |
| + return view_controller_->HasPopup(GetCurrentWebContents()) ? |
| + MenuButton::OnMousePressed(event) : LabelButton::OnMousePressed(event); |
| } |
| return false; |
| } |
| void BrowserActionView::OnMouseReleased(const ui::MouseEvent& event) { |
| - if (IsPopup() || view_controller_->is_menu_running()) { |
| + if (view_controller_->HasPopup(GetCurrentWebContents()) || |
| + view_controller_->IsMenuRunning()) { |
| // TODO(erikkay) this never actually gets called (probably because of the |
| // loss of focus). |
| MenuButton::OnMouseReleased(event); |
| @@ -224,19 +183,20 @@ void BrowserActionView::OnMouseReleased(const ui::MouseEvent& event) { |
| } |
| void BrowserActionView::OnMouseExited(const ui::MouseEvent& event) { |
| - if (IsPopup() || view_controller_->is_menu_running()) |
| + if (view_controller_->HasPopup(GetCurrentWebContents()) || |
| + view_controller_->IsMenuRunning()) |
| MenuButton::OnMouseExited(event); |
| else |
| LabelButton::OnMouseExited(event); |
| } |
| bool BrowserActionView::OnKeyReleased(const ui::KeyEvent& event) { |
| - return IsPopup() ? MenuButton::OnKeyReleased(event) : |
| - LabelButton::OnKeyReleased(event); |
| + return view_controller_->HasPopup(GetCurrentWebContents()) ? |
| + MenuButton::OnKeyReleased(event) : LabelButton::OnKeyReleased(event); |
| } |
| void BrowserActionView::OnGestureEvent(ui::GestureEvent* event) { |
| - if (IsPopup()) |
| + if (view_controller_->HasPopup(GetCurrentWebContents())) |
| MenuButton::OnGestureEvent(event); |
| else |
| LabelButton::OnGestureEvent(event); |
| @@ -249,17 +209,11 @@ scoped_ptr<LabelButtonBorder> BrowserActionView::CreateDefaultBorder() const { |
| return border.Pass(); |
| } |
| -bool BrowserActionView::IsEnabled(int tab_id) const { |
| - return view_controller_->extension_action()->GetIsVisible(tab_id); |
| -} |
| - |
| -gfx::ImageSkia BrowserActionView::GetIconWithBadge() { |
| - int tab_id = view_controller_->GetCurrentTabId(); |
| - gfx::Size spacing(0, ToolbarView::kVertSpacing); |
| - gfx::ImageSkia icon = *view_controller_->GetIcon(tab_id).ToImageSkia(); |
| - if (!IsEnabled(tab_id)) |
| - icon = gfx::ImageSkiaOperations::CreateTransparentImage(icon, .25); |
| - return extension_action()->GetIconWithBadge(icon, tab_id, spacing); |
| +ExtensionActionViewController* |
| +BrowserActionView::GetExtensionActionViewController() { |
| + CHECK_EQ(ToolbarActionViewController::TYPE_EXTENSION_ACTION, |
| + view_controller_->GetType()); |
| + return static_cast<ExtensionActionViewController*>(view_controller()); |
| } |
| gfx::ImageSkia BrowserActionView::GetIconForTest() { |
| @@ -268,8 +222,6 @@ gfx::ImageSkia BrowserActionView::GetIconForTest() { |
| void BrowserActionView::OnIconUpdated() { |
| UpdateState(); |
| - if (icon_observer_) |
| - icon_observer_->OnIconUpdated(GetIconWithBadge()); |
| } |
| views::View* BrowserActionView::GetAsView() { |
| @@ -288,15 +240,15 @@ views::Widget* BrowserActionView::GetParentForContextMenu() { |
| // RunMenuAt expects a nested menu to be parented by the same widget as the |
| // already visible menu, in this case the Chrome menu. |
| return delegate_->ShownInsideMenu() ? |
| - BrowserView::GetBrowserViewForBrowser(view_controller_->browser()) |
| + BrowserView::GetBrowserViewForBrowser(browser_) |
| ->toolbar()->app_menu()->GetWidget() : |
| GetWidget(); |
| } |
| -ExtensionActionViewController* |
| +ToolbarActionViewController* |
| BrowserActionView::GetPreferredPopupViewController() { |
| return delegate_->ShownInsideMenu() ? |
| - delegate_->GetMainViewForExtension(extension())->view_controller() : |
| + delegate_->GetMainViewForAction(this)->view_controller() : |
| view_controller(); |
| } |
| @@ -312,7 +264,7 @@ views::MenuButton* BrowserActionView::GetContextMenuButton() { |
| return this; |
| } |
| -content::WebContents* BrowserActionView::GetCurrentWebContents() { |
| +content::WebContents* BrowserActionView::GetCurrentWebContents() const { |
| return delegate_->GetCurrentWebContents(); |
| } |