Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1424)

Unified Diff: chrome/browser/ui/views/extensions/extension_action_view.h

Issue 431173002: Create ExtensionActionView class (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/views/extensions/extension_action_view.h
diff --git a/chrome/browser/ui/views/extensions/extension_action_view.h b/chrome/browser/ui/views/extensions/extension_action_view.h
new file mode 100644
index 0000000000000000000000000000000000000000..ba9cec06a395185c9c66740fdce647ad89e6a8b3
--- /dev/null
+++ b/chrome/browser/ui/views/extensions/extension_action_view.h
@@ -0,0 +1,201 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_UI_VIEWS_EXTENSIONS_EXTENSION_ACTION_VIEW_H_
+#define CHROME_BROWSER_UI_VIEWS_EXTENSIONS_EXTENSION_ACTION_VIEW_H_
+
+#include "chrome/browser/extensions/extension_action_icon_factory.h"
+#include "chrome/browser/extensions/extension_context_menu_model.h"
+#include "chrome/browser/ui/views/extensions/extension_popup.h"
+#include "ui/gfx/image/image.h"
+#include "ui/views/context_menu_controller.h"
+#include "ui/views/widget/widget_observer.h"
+
+class Browser;
+class ExtensionAction;
+
+namespace content {
+class WebContents;
+}
+
+namespace extensions {
+class Command;
+class Extension;
+}
+
+namespace ui {
+class Accelerator;
+}
+
+namespace views {
+class MenuRunner;
+class View;
+class Widget;
+}
+
+// An abstract "View" for an ExtensionAction (either a BrowserAction or a
+// PageAction). This contains the logic for showing the action's popup and
+// the context menu. This class doesn't subclass View directly, as the
+// implementations for page actions/browser actions are different types of
+// views.
+// All common logic for executing extension actions should go in this class;
sky 2014/08/05 22:29:46 No one is going to subclass this, right?
Devlin 2014/08/05 23:51:09 Correct. :)
+// ViewDelegate classes should only have knowledge relating to the views::View
+// wrapper.
+class ExtensionActionView : public ExtensionActionIconFactory::Observer,
sky 2014/08/05 22:29:46 I don't like naming this class a View if its not a
Devlin 2014/08/05 23:51:10 I know :/ This was a desperate attempt at a name
+ public ExtensionContextMenuModel::PopupDelegate,
+ public views::ContextMenuController,
+ public views::WidgetObserver {
+ public:
+ class ViewDelegate {
sky 2014/08/05 22:29:46 I prefer this to live in its own file and be named
Devlin 2014/08/05 23:51:10 Ah, didn't know that - I think I've only seen dele
+ public:
+ virtual ~ViewDelegate() {}
sky 2014/08/05 22:29:46 make protected so that it's clear the delegate is
Devlin 2014/08/05 23:51:10 Done.
+
+ // Returns |this| as a view. We need this because our subclasses implement
+ // different kinds of views, and inheriting View here is a really bad idea.
+ virtual views::View* GetAsView() = 0;
+
+ // Returns true if this is a nested view.
sky 2014/08/05 22:29:47 What does 'nested' mean here?
Devlin 2014/08/05 23:51:10 Rephrased for clarity and correctness.
+ virtual bool IsNestedView() = 0;
+
+ // Returns the FocusManager to use when registering accelerators.
+ virtual views::FocusManager* GetFocusManagerForAccelerator() = 0;
+
+ // Returns the parent for the associated context menu.
+ virtual views::Widget* GetParentForContextMenu() = 0;
+
+ // Returns the reference view for the extension action's popup.
+ virtual views::View* GetReferenceViewForPopup() = 0;
+
+ // Returns the current web contents.
+ virtual content::WebContents* GetCurrentWebContents() = 0;
+
+ // Hides whatever popup is active (even if it's not this one).
+ virtual void HideActivePopup() = 0;
+
+ // Called when the icon is updated; this is forwarded from the icon factory.
+ virtual void OnIconUpdated() = 0;
+
+ // Called when a popup is shown. See ExecuteAction() for the definition of
+ // |grant_tab_permissions|.
+ virtual void OnPopupShown(bool grant_tab_permissions) {}
+
+ // Does any additional cleanup after the popup is closed.
+ virtual void CleanupPopup() {}
+
+ // Called immediately before the context menu is shown.
+ virtual void OnWillShowContextMenus() {}
+
+ // Called once the context menu has closed.
sky 2014/08/05 22:29:46 Make it clear this may not be called (if the conte
Devlin 2014/08/05 23:51:10 Done.
+ virtual void OnContextMenuDone() {}
+ };
+
+ ExtensionActionView(const extensions::Extension* extension,
+ Browser* browser,
+ ExtensionAction* extension_action,
+ ViewDelegate* delegate);
+ virtual ~ExtensionActionView();
+
+ // ExtensionContextMenuModel::PopupDelegate:
+ virtual void InspectPopup() OVERRIDE;
+
+ // Executes the default extension action (typically showing the popup), and
+ // attributes the action to a user (thus, only use this for actions that
+ // *were* done by the user).
+ void ExecuteActionByUser();
+
+ // Executes the extension action with |show_action|. If
+ // |grant_tab_permissions| is true, this will grant the extension active tab
+ // permissions. Only do this if this was done through a user action (and not
+ // e.g. an API). Returns true if a popup is shown.
+ bool ExecuteAction(ExtensionPopup::ShowAction show_action,
+ bool grant_tab_permissions);
+
+ // Hides the popup, if one is open.
+ void HidePopup();
+
+ // Returns the icon from the |icon_factory_|.
+ gfx::Image GetIcon(int tab_id);
+
+ // Returns the current tab id.
+ int GetCurrentTabId() const;
+
+ // Registers an accelerator for the extension action's command, if one
+ // exists.
+ void RegisterCommand();
+
+ // Unregisters the accelerator for the extension action's command, if one
+ // exists. If |only_if_removed| is true, then this will only unregister if the
+ // command has been removed.
+ void UnregisterCommand(bool only_if_removed);
+
+ // Handles the View::AcceleratorPressed method. Doesn't OVERRIDE because we
+ // don't actually derive from View here.
+ bool AcceleratorPressed(const ui::Accelerator& accelerator);
+
+ const extensions::Extension* extension() const { return extension_; }
+ Browser* browser() { return browser_; }
+ ExtensionAction* extension_action() { return extension_action_; }
+ const ExtensionAction* extension_action() const { return extension_action_; }
+ ExtensionPopup* popup() { return popup_; }
+ bool is_menu_running() const { return menu_runner_.get() != NULL; }
+
+ private:
+ // ExtensionActionIconFactory::Observer:
+ virtual void OnIconUpdated() OVERRIDE;
+
+ // views::WidgetObserver:
+ virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE;
+
+ // views::ContextMenuController:
+ virtual void ShowContextMenuForView(views::View* source,
+ const gfx::Point& point,
+ ui::MenuSourceType source_type) OVERRIDE;
+
+ // Shows the popup for the extension action, given the associated |popup_url|.
+ // Returns true if a popup is successfully shown.
+ bool ShowPopupWithUrl(ExtensionPopup::ShowAction show_action,
+ const GURL& popup_url);
+
+ // Populates |command| with the command associated with |extension|, if one
+ // exists. Returns true if |command| was populated.
+ bool GetExtensionCommand(extensions::Command* command);
+
+ // Cleans up after the popup. If |close_widget| is true, this will call
+ // Widget::Close() on the popup's widget; otherwise it assumes the popup is
+ // already closing.
+ void CleanupPopup(bool close_widget);
+
+ // The extension associated with the action we're displaying.
+ const extensions::Extension* extension_;
+
+ // The corresponding browser.
+ Browser* browser_;
+
+ // The browser action this view represents. The ExtensionAction is not owned
+ // by this class.
+ ExtensionAction* extension_action_;
+
+ // Our delegate.
+ ViewDelegate* delegate_;
+
+ // The object that will be used to get the browser action icon for us.
+ // It may load the icon asynchronously (in which case the initial icon
+ // returned by the factory will be transparent), so we have to observe it for
+ // updates to the icon.
+ ExtensionActionIconFactory icon_factory_;
+
+ // Responsible for running the menu.
+ scoped_ptr<views::MenuRunner> menu_runner_;
+
+ // The browser action's popup, if it is visible; NULL otherwise.
+ ExtensionPopup* popup_;
+
+ // The extension key binding accelerator this extension action is listening
+ // for (to show the popup).
+ scoped_ptr<ui::Accelerator> action_keybinding_;
+
+ DISALLOW_COPY_AND_ASSIGN(ExtensionActionView);
+};
+
+#endif // CHROME_BROWSER_UI_VIEWS_EXTENSIONS_EXTENSION_ACTION_VIEW_H_

Powered by Google App Engine
This is Rietveld 408576698