| 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..45199a15080e697dbea7b69728014599971f6116 100644
|
| --- a/chrome/browser/ui/views/toolbar/browser_action_view.cc
|
| +++ b/chrome/browser/ui/views/toolbar/browser_action_view.cc
|
| @@ -6,10 +6,9 @@
|
|
|
| #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"
|
| @@ -18,12 +17,8 @@
|
| #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"
|
| @@ -44,32 +39,24 @@ 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,
|
| + Type type,
|
| + 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()),
|
| + type_(type),
|
| + 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 (type_ == TYPE_EXTENSION_ACTION) {
|
| + set_context_menu_controller(GetExtensionActionViewController());
|
| + 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 +74,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 +94,7 @@ 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(), GetCurrentTabId());
|
| }
|
|
|
| void BrowserActionView::GetAccessibleState(ui::AXViewState* state) {
|
| @@ -124,73 +108,44 @@ void BrowserActionView::ButtonPressed(views::Button* sender,
|
| }
|
|
|
| void BrowserActionView::UpdateState() {
|
| - int tab_id = view_controller_->GetCurrentTabId();
|
| + int tab_id = GetCurrentTabId();
|
| if (tab_id < 0)
|
| return;
|
|
|
| - if (!IsEnabled(tab_id))
|
| + bool enabled = view_controller_->IsEnabled(tab_id);
|
| + if (!enabled)
|
| SetState(views::CustomButton::STATE_DISABLED);
|
|
|
| gfx::ImageSkia icon = *view_controller_->GetIcon(tab_id).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(tab_id));
|
| + SetAccessibleName(view_controller_->GetAccessibleName(tab_id));
|
|
|
| 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(GetCurrentTabId()))
|
| return true;
|
|
|
| view_controller_->ExecuteActionByUser();
|
| @@ -207,14 +162,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(GetCurrentTabId()) ?
|
| + 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(GetCurrentTabId()) ||
|
| + view_controller_->IsMenuRunning()) {
|
| // TODO(erikkay) this never actually gets called (probably because of the
|
| // loss of focus).
|
| MenuButton::OnMouseReleased(event);
|
| @@ -224,19 +180,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(GetCurrentTabId()) ||
|
| + 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(GetCurrentTabId()) ?
|
| + MenuButton::OnKeyReleased(event) : LabelButton::OnKeyReleased(event);
|
| }
|
|
|
| void BrowserActionView::OnGestureEvent(ui::GestureEvent* event) {
|
| - if (IsPopup())
|
| + if (view_controller_->HasPopup(GetCurrentTabId()))
|
| MenuButton::OnGestureEvent(event);
|
| else
|
| LabelButton::OnGestureEvent(event);
|
| @@ -249,17 +206,10 @@ 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() {
|
| + DCHECK_EQ(TYPE_EXTENSION_ACTION, type_);
|
| + return static_cast<ExtensionActionViewController*>(view_controller());
|
| }
|
|
|
| gfx::ImageSkia BrowserActionView::GetIconForTest() {
|
| @@ -268,8 +218,6 @@ gfx::ImageSkia BrowserActionView::GetIconForTest() {
|
|
|
| void BrowserActionView::OnIconUpdated() {
|
| UpdateState();
|
| - if (icon_observer_)
|
| - icon_observer_->OnIconUpdated(GetIconWithBadge());
|
| }
|
|
|
| views::View* BrowserActionView::GetAsView() {
|
| @@ -288,15 +236,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,8 +260,9 @@ views::MenuButton* BrowserActionView::GetContextMenuButton() {
|
| return this;
|
| }
|
|
|
| -content::WebContents* BrowserActionView::GetCurrentWebContents() {
|
| - return delegate_->GetCurrentWebContents();
|
| +int BrowserActionView::GetCurrentTabId() const {
|
| + content::WebContents* web_contents = delegate_->GetCurrentWebContents();
|
| + return web_contents ? SessionTabHelper::IdForTab(web_contents) : -1;
|
| }
|
|
|
| void BrowserActionView::HideActivePopup() {
|
|
|