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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_CONTEXT_MENU_MODEL_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_CONTEXT_MENU_MODEL_H_
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"
15 class ExtensionAction;
22 namespace extensions {
24 class ContextMenuMatcher;
25 class ExtensionContextMenuModelTest;
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 {
45 // Type of action the extension icon represents.
46 enum ActionType { NO_ACTION = 0, BROWSER_ACTION, PAGE_ACTION };
48 // Delegate to handle showing an ExtensionAction popup.
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;
57 virtual ~PopupDelegate() {}
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,
66 PopupDelegate* delegate);
68 // Create a menu model for the given extension, without support
69 // for the "Inspect Popup" command.
70 ExtensionContextMenuModel(const extensions::Extension* extension,
73 // SimpleMenuModel::Delegate overrides.
74 bool IsCommandIdChecked(int command_id) const override;
75 bool IsCommandIdEnabled(int command_id) const override;
76 bool GetAcceleratorForCommandId(int command_id,
77 ui::Accelerator* accelerator) override;
78 void ExecuteCommand(int command_id, int event_flags) override;
80 // ExtensionUninstallDialog::Delegate:
81 void ExtensionUninstallAccepted() override;
82 void ExtensionUninstallCanceled() override;
85 friend class base::RefCounted<ExtensionContextMenuModel>;
86 friend class extensions::ExtensionContextMenuModelTest;
88 ~ExtensionContextMenuModel() override;
90 void InitMenu(const extensions::Extension* extension);
92 // Gets the extension we are displaying the menu for. Returns NULL if the
93 // extension has been uninstalled and no longer exists.
94 const extensions::Extension* GetExtension() const;
96 // Returns the active web contents.
97 content::WebContents* GetActiveWebContents() const;
99 // Appends the extension's context menu items.
100 void AppendExtensionItems();
102 // A copy of the extension's id.
103 std::string extension_id_;
105 // The extension action of the extension we are displaying the menu for (if
106 // it has one, otherwise NULL).
107 ExtensionAction* extension_action_;
113 // The delegate which handles the 'inspect popup' menu command (or NULL).
114 PopupDelegate* delegate_;
116 // The type of extension action to which this context menu is attached.
117 ActionType action_type_;
119 // Keeps track of the extension uninstall dialog.
120 scoped_ptr<extensions::ExtensionUninstallDialog> extension_uninstall_dialog_;
122 // Menu matcher for context menu items specified by the extension.
123 scoped_ptr<extensions::ContextMenuMatcher> extension_items_;
125 // Number of extension items in this menu. Used for testing.
126 int extension_items_count_;
128 DISALLOW_COPY_AND_ASSIGN(ExtensionContextMenuModel);
131 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_CONTEXT_MENU_MODEL_H_