Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2012 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.
      4 
      5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_CONTEXT_MENU_MODEL_H_
      6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_CONTEXT_MENU_MODEL_H_
      7 
      8 #include <string>
      9 
     10 #include "base/memory/scoped_ptr.h"
     11 #include "chrome/browser/extensions/extension_uninstall_dialog.h"
     12 #include "ui/base/models/simple_menu_model.h"
     13 
     14 class Browser;
     15 class ExtensionAction;
     16 class Profile;
     17 
     18 namespace content {
     19 class WebContents;
     20 }
     21 
     22 namespace extensions {
     23 class Extension;
     24 class ContextMenuMatcher;
     25 class ExtensionContextMenuModelTest;
     26 }
     27 
     28 // The context menu model for extension icons.
     29 class ExtensionContextMenuModel
     30     : public base::RefCounted<ExtensionContextMenuModel>,
     31       public ui::SimpleMenuModel,
     32       public ui::SimpleMenuModel::Delegate,
     33       public extensions::ExtensionUninstallDialog::Delegate {
     34  public:
     35   enum MenuEntries {
     36     NAME = 0,
     37     CONFIGURE,
     38     TOGGLE_VISIBILITY,
     39     UNINSTALL,
     40     MANAGE,
     41     INSPECT_POPUP,
     42     ALWAYS_RUN
     43   };
     44 
     45   // Type of action the extension icon represents.
     46   enum ActionType { NO_ACTION = 0, BROWSER_ACTION, PAGE_ACTION };
     47 
     48   // Delegate to handle showing an ExtensionAction popup.
     49   class PopupDelegate {
     50    public:
     51     // Called when the user selects the menu item which requests that the
     52     // popup be shown and inspected.
     53     // The delegate should know which popup to display.
     54     virtual void InspectPopup() = 0;
     55 
     56    protected:
     57     virtual ~PopupDelegate() {}
     58   };
     59 
     60   // Creates a menu model for the given extension. If
     61   // prefs::kExtensionsUIDeveloperMode is enabled then a menu item
     62   // will be shown for "Inspect Popup" which, when selected, will cause
     63   // ShowPopupForDevToolsWindow() to be called on |delegate|.
     64   ExtensionContextMenuModel(const extensions::Extension* extension,
     65                             Browser* browser,
     66                             PopupDelegate* delegate);
     67 
     68   // Create a menu model for the given extension, without support
     69   // for the "Inspect Popup" command.
     70   ExtensionContextMenuModel(const extensions::Extension* extension,
     71                             Browser* browser);
     72 
     73   // SimpleMenuModel::Delegate overrides.
     74   virtual bool IsCommandIdChecked(int command_id) const OVERRIDE;
     75   virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
     76   virtual bool GetAcceleratorForCommandId(
     77       int command_id,
     78       ui::Accelerator* accelerator) OVERRIDE;
     79   virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE;
     80 
     81   // ExtensionUninstallDialog::Delegate:
     82   virtual void ExtensionUninstallAccepted() OVERRIDE;
     83   virtual void ExtensionUninstallCanceled() OVERRIDE;
     84 
     85  private:
     86   friend class base::RefCounted<ExtensionContextMenuModel>;
     87   friend class extensions::ExtensionContextMenuModelTest;
     88 
     89   virtual ~ExtensionContextMenuModel();
     90 
     91   void InitMenu(const extensions::Extension* extension);
     92 
     93   // Gets the extension we are displaying the menu for. Returns NULL if the
     94   // extension has been uninstalled and no longer exists.
     95   const extensions::Extension* GetExtension() const;
     96 
     97   // Returns the active web contents.
     98   content::WebContents* GetActiveWebContents() const;
     99 
    100   // Appends the extension's context menu items.
    101   void AppendExtensionItems();
    102 
    103   // A copy of the extension's id.
    104   std::string extension_id_;
    105 
    106   // The extension action of the extension we are displaying the menu for (if
    107   // it has one, otherwise NULL).
    108   ExtensionAction* extension_action_;
    109 
    110   Browser* browser_;
    111 
    112   Profile* profile_;
    113 
    114   // The delegate which handles the 'inspect popup' menu command (or NULL).
    115   PopupDelegate* delegate_;
    116 
    117   // The type of extension action to which this context menu is attached.
    118   ActionType action_type_;
    119 
    120   // Keeps track of the extension uninstall dialog.
    121   scoped_ptr<extensions::ExtensionUninstallDialog> extension_uninstall_dialog_;
    122 
    123   // Menu matcher for context menu items specified by the extension.
    124   scoped_ptr<extensions::ContextMenuMatcher> extension_items_;
    125 
    126   // Number of extension items in this menu. Used for testing.
    127   int extension_items_count_;
    128 
    129   DISALLOW_COPY_AND_ASSIGN(ExtensionContextMenuModel);
    130 };
    131 
    132 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_CONTEXT_MENU_MODEL_H_
    133