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 extensions {
     19 class Extension;
     20 }
     21 
     22 // The context menu model for extension icons.
     23 class ExtensionContextMenuModel
     24     : public base::RefCounted<ExtensionContextMenuModel>,
     25       public ui::SimpleMenuModel,
     26       public ui::SimpleMenuModel::Delegate,
     27       public extensions::ExtensionUninstallDialog::Delegate {
     28  public:
     29   enum MenuEntries {
     30     NAME = 0,
     31     CONFIGURE,
     32     HIDE,
     33     UNINSTALL,
     34     MANAGE,
     35     INSPECT_POPUP
     36   };
     37 
     38   // Delegate to handle showing an ExtensionAction popup.
     39   class PopupDelegate {
     40    public:
     41     // Called when the user selects the menu item which requests that the
     42     // popup be shown and inspected.
     43     virtual void InspectPopup(ExtensionAction* action) = 0;
     44 
     45    protected:
     46     virtual ~PopupDelegate() {}
     47   };
     48 
     49   // Creates a menu model for the given extension. If
     50   // prefs::kExtensionsUIDeveloperMode is enabled then a menu item
     51   // will be shown for "Inspect Popup" which, when selected, will cause
     52   // ShowPopupForDevToolsWindow() to be called on |delegate|.
     53   ExtensionContextMenuModel(const extensions::Extension* extension,
     54                             Browser* browser,
     55                             PopupDelegate* delegate);
     56 
     57   // Create a menu model for the given extension, without support
     58   // for the "Inspect Popup" command.
     59   ExtensionContextMenuModel(const extensions::Extension* extension,
     60                             Browser* browser);
     61 
     62   // SimpleMenuModel::Delegate overrides.
     63   virtual bool IsCommandIdChecked(int command_id) const OVERRIDE;
     64   virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
     65   virtual bool GetAcceleratorForCommandId(
     66       int command_id,
     67       ui::Accelerator* accelerator) OVERRIDE;
     68   virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE;
     69 
     70   // ExtensionUninstallDialog::Delegate:
     71   virtual void ExtensionUninstallAccepted() OVERRIDE;
     72   virtual void ExtensionUninstallCanceled() OVERRIDE;
     73 
     74  private:
     75   friend class base::RefCounted<ExtensionContextMenuModel>;
     76   virtual ~ExtensionContextMenuModel();
     77 
     78   void InitMenu(const extensions::Extension* extension);
     79 
     80   // Gets the extension we are displaying the menu for. Returns NULL if the
     81   // extension has been uninstalled and no longer exists.
     82   const extensions::Extension* GetExtension() const;
     83 
     84   // A copy of the extension's id.
     85   std::string extension_id_;
     86 
     87   // The extension action of the extension we are displaying the menu for (if
     88   // it has one, otherwise NULL).
     89   ExtensionAction* extension_action_;
     90 
     91   Browser* browser_;
     92 
     93   Profile* profile_;
     94 
     95   // The delegate which handles the 'inspect popup' menu command (or NULL).
     96   PopupDelegate* delegate_;
     97 
     98   // Keeps track of the extension uninstall dialog.
     99   scoped_ptr<extensions::ExtensionUninstallDialog> extension_uninstall_dialog_;
    100 
    101   DISALLOW_COPY_AND_ASSIGN(ExtensionContextMenuModel);
    102 };
    103 
    104 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_CONTEXT_MENU_MODEL_H_
    105