[email protected] | e0a760e | 2012-04-17 04:49:33 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 7e9f4a8 | 2010-03-22 22:36:20 | [diff] [blame] | 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 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 8 | #include <memory> |
[email protected] | f681c78 | 2010-11-19 11:19:39 | [diff] [blame] | 9 | #include <string> |
| 10 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 11 | #include "base/memory/raw_ptr.h" |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame] | 12 | #include "third_party/abseil-cpp/absl/types/optional.h" |
[email protected] | 44cbd9e | 2011-01-14 15:49:40 | [diff] [blame] | 13 | #include "ui/base/models/simple_menu_model.h" |
[email protected] | 7e9f4a8 | 2010-03-22 22:36:20 | [diff] [blame] | 14 | |
| 15 | class Browser; |
[email protected] | 7e9f4a8 | 2010-03-22 22:36:20 | [diff] [blame] | 16 | class Profile; |
| 17 | |
[email protected] | e167058 | 2014-08-15 23:05:41 | [diff] [blame] | 18 | namespace content { |
| 19 | class WebContents; |
| 20 | } |
| 21 | |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 22 | namespace extensions { |
[email protected] | 69e1c12d | 2014-08-13 08:25:34 | [diff] [blame] | 23 | class ContextMenuMatcher; |
Devlin Cronin | 720a0fb | 2020-07-22 01:31:38 | [diff] [blame] | 24 | class Extension; |
| 25 | class ExtensionAction; |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 26 | |
[email protected] | ddaaaa1 | 2013-01-29 22:52:52 | [diff] [blame] | 27 | // The context menu model for extension icons. |
rdevlin.cronin | 2b74ad8 | 2015-09-17 22:15:54 | [diff] [blame] | 28 | class ExtensionContextMenuModel : public ui::SimpleMenuModel, |
| 29 | public ui::SimpleMenuModel::Delegate { |
[email protected] | 7e9f4a8 | 2010-03-22 22:36:20 | [diff] [blame] | 30 | public: |
[email protected] | 6518715 | 2012-06-02 13:14:14 | [diff] [blame] | 31 | enum MenuEntries { |
Devlin Cronin | 94aa4fb | 2018-09-21 05:24:00 | [diff] [blame] | 32 | HOME_PAGE = 0, |
| 33 | OPTIONS, |
[email protected] | b87c48d | 2014-08-21 20:44:04 | [diff] [blame] | 34 | TOGGLE_VISIBILITY, |
[email protected] | 6518715 | 2012-06-02 13:14:14 | [diff] [blame] | 35 | UNINSTALL, |
Devlin Cronin | 94aa4fb | 2018-09-21 05:24:00 | [diff] [blame] | 36 | MANAGE_EXTENSIONS, |
[email protected] | e167058 | 2014-08-15 23:05:41 | [diff] [blame] | 37 | INSPECT_POPUP, |
Devlin Cronin | c40a10c1 | 2018-11-30 22:10:37 | [diff] [blame] | 38 | PAGE_ACCESS_CANT_ACCESS, |
rdevlin.cronin | b2e7ac0 | 2015-10-29 17:05:56 | [diff] [blame] | 39 | PAGE_ACCESS_SUBMENU, |
| 40 | PAGE_ACCESS_RUN_ON_CLICK, |
| 41 | PAGE_ACCESS_RUN_ON_SITE, |
| 42 | PAGE_ACCESS_RUN_ON_ALL_SITES, |
Devlin Cronin | 35a3d94 | 2018-09-17 22:36:37 | [diff] [blame] | 43 | PAGE_ACCESS_LEARN_MORE, |
Karan Bhatia | 6b9706d | 2018-09-25 00:46:24 | [diff] [blame] | 44 | // NOTE: If you update this, you probably need to update the |
| 45 | // ContextMenuAction enum below. |
| 46 | }; |
| 47 | |
| 48 | // A separate enum to indicate the action taken on the menu. We have two |
| 49 | // enums (this and MenuEntries above) to avoid needing to have a single one |
| 50 | // with both action-specific values (like kNoAction) and menu-specific values |
| 51 | // (like PAGE_ACCESS_SUBMENU). |
| 52 | // These values are persisted to logs. Entries should not be renumbered and |
| 53 | // numeric values should never be reused. New values should be added before |
| 54 | // kMaxValue. |
| 55 | enum class ContextMenuAction { |
| 56 | kNoAction = 0, |
| 57 | kCustomCommand = 1, |
| 58 | kHomePage = 2, |
| 59 | kOptions = 3, |
| 60 | kToggleVisibility = 4, |
| 61 | kUninstall = 5, |
| 62 | kManageExtensions = 6, |
| 63 | kInspectPopup = 7, |
| 64 | kPageAccessRunOnClick = 8, |
| 65 | kPageAccessRunOnSite = 9, |
| 66 | kPageAccessRunOnAllSites = 10, |
| 67 | kPageAccessLearnMore = 11, |
| 68 | kMaxValue = kPageAccessLearnMore, |
[email protected] | 6518715 | 2012-06-02 13:14:14 | [diff] [blame] | 69 | }; |
| 70 | |
Emilia Paz | 498abfc | 2022-01-20 17:51:42 | [diff] [blame] | 71 | // Location where the context menu is open from. |
| 72 | enum class ContextMenuSource { kToolbarAction = 0, kMenuItem = 1 }; |
| 73 | |
Devlin Cronin | bff8e317 | 2020-09-01 11:02:11 | [diff] [blame] | 74 | // The current visibility of the extension; this affects the "pin" / "unpin" |
rdevlin.cronin | 6540a51 | 2015-04-30 20:58:31 | [diff] [blame] | 75 | // strings in the menu. |
Devlin Cronin | bff8e317 | 2020-09-01 11:02:11 | [diff] [blame] | 76 | // TODO(devlin): Rename this "PinState" when we finish removing the old UI |
| 77 | // bits. |
rdevlin.cronin | 6540a51 | 2015-04-30 20:58:31 | [diff] [blame] | 78 | enum ButtonVisibility { |
Devlin Cronin | bff8e317 | 2020-09-01 11:02:11 | [diff] [blame] | 79 | // The extension is pinned on the toolbar. |
| 80 | PINNED, |
| 81 | // The extension is temporarily visible on the toolbar, as for showing a |
| 82 | // popup. |
rdevlin.cronin | 6540a51 | 2015-04-30 20:58:31 | [diff] [blame] | 83 | TRANSITIVELY_VISIBLE, |
Devlin Cronin | bff8e317 | 2020-09-01 11:02:11 | [diff] [blame] | 84 | // The extension is not pinned (and is shown in the extensions menu). |
| 85 | UNPINNED, |
rdevlin.cronin | 6540a51 | 2015-04-30 20:58:31 | [diff] [blame] | 86 | }; |
| 87 | |
[email protected] | c82526da | 2012-06-20 00:29:07 | [diff] [blame] | 88 | // Delegate to handle showing an ExtensionAction popup. |
| 89 | class PopupDelegate { |
| 90 | public: |
| 91 | // Called when the user selects the menu item which requests that the |
| 92 | // popup be shown and inspected. |
[email protected] | fbabd74 | 2014-07-31 03:23:23 | [diff] [blame] | 93 | // The delegate should know which popup to display. |
| 94 | virtual void InspectPopup() = 0; |
[email protected] | c82526da | 2012-06-20 00:29:07 | [diff] [blame] | 95 | |
| 96 | protected: |
| 97 | virtual ~PopupDelegate() {} |
| 98 | }; |
| 99 | |
[email protected] | ddaaaa1 | 2013-01-29 22:52:52 | [diff] [blame] | 100 | // Creates a menu model for the given extension. If |
[email protected] | c82526da | 2012-06-20 00:29:07 | [diff] [blame] | 101 | // prefs::kExtensionsUIDeveloperMode is enabled then a menu item |
| 102 | // will be shown for "Inspect Popup" which, when selected, will cause |
| 103 | // ShowPopupForDevToolsWindow() to be called on |delegate|. |
rdevlin.cronin | 2b74ad8 | 2015-09-17 22:15:54 | [diff] [blame] | 104 | ExtensionContextMenuModel(const Extension* extension, |
[email protected] | c82526da | 2012-06-20 00:29:07 | [diff] [blame] | 105 | Browser* browser, |
rdevlin.cronin | 6540a51 | 2015-04-30 20:58:31 | [diff] [blame] | 106 | ButtonVisibility visibility, |
Eric Willigers | df4c300 | 2019-06-14 21:02:07 | [diff] [blame] | 107 | PopupDelegate* delegate, |
Emilia Paz | 498abfc | 2022-01-20 17:51:42 | [diff] [blame] | 108 | bool can_show_icon_in_toolbar, |
| 109 | ContextMenuSource source); |
Peter Boström | 6316db8 | 2021-09-24 16:15:11 | [diff] [blame] | 110 | |
| 111 | ExtensionContextMenuModel(const ExtensionContextMenuModel&) = delete; |
| 112 | ExtensionContextMenuModel& operator=(const ExtensionContextMenuModel&) = |
| 113 | delete; |
| 114 | |
rdevlin.cronin | 2b74ad8 | 2015-09-17 22:15:54 | [diff] [blame] | 115 | ~ExtensionContextMenuModel() override; |
[email protected] | c82526da | 2012-06-20 00:29:07 | [diff] [blame] | 116 | |
rdevlin.cronin | a0ea3c2 | 2015-09-25 19:33:09 | [diff] [blame] | 117 | // SimpleMenuModel::Delegate: |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 118 | bool IsCommandIdChecked(int command_id) const override; |
catmullings | ff2cdbc | 2017-08-22 22:07:02 | [diff] [blame] | 119 | bool IsCommandIdVisible(int command_id) const override; |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 120 | bool IsCommandIdEnabled(int command_id) const override; |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 121 | void ExecuteCommand(int command_id, int event_flags) override; |
Karan Bhatia | 6b9706d | 2018-09-25 00:46:24 | [diff] [blame] | 122 | void OnMenuWillShow(ui::SimpleMenuModel* source) override; |
| 123 | void MenuClosed(ui::SimpleMenuModel* source) override; |
[email protected] | 7e9f4a8 | 2010-03-22 22:36:20 | [diff] [blame] | 124 | |
rdevlin.cronin | b2e7ac0 | 2015-10-29 17:05:56 | [diff] [blame] | 125 | ui::SimpleMenuModel* page_access_submenu_for_testing() { |
| 126 | return page_access_submenu_.get(); |
| 127 | } |
| 128 | |
[email protected] | 7e9f4a8 | 2010-03-22 22:36:20 | [diff] [blame] | 129 | private: |
rdevlin.cronin | 2b74ad8 | 2015-09-17 22:15:54 | [diff] [blame] | 130 | void InitMenu(const Extension* extension, ButtonVisibility button_visibility); |
[email protected] | 7e9f4a8 | 2010-03-22 22:36:20 | [diff] [blame] | 131 | |
rdevlin.cronin | b2e7ac0 | 2015-10-29 17:05:56 | [diff] [blame] | 132 | void CreatePageAccessSubmenu(const Extension* extension); |
| 133 | |
Devlin Cronin | c02de65 | 2018-12-08 02:51:03 | [diff] [blame] | 134 | // Returns true if the given page access command is enabled in the menu. |
| 135 | bool IsPageAccessCommandEnabled(const Extension& extension, |
Devlin Cronin | c02de65 | 2018-12-08 02:51:03 | [diff] [blame] | 136 | int command_id) const; |
| 137 | |
rdevlin.cronin | b2e7ac0 | 2015-10-29 17:05:56 | [diff] [blame] | 138 | void HandlePageAccessCommand(int command_id, |
| 139 | const Extension* extension) const; |
| 140 | |
Tim Judkins | c394b4b9 | 2020-07-17 19:50:48 | [diff] [blame] | 141 | // Logs a user action when an option is selected in the page access section of |
| 142 | // the context menu. |
| 143 | void LogPageAccessAction(int command_id) const; |
| 144 | |
[email protected] | 9e685e5 | 2010-10-22 19:45:30 | [diff] [blame] | 145 | // Gets the extension we are displaying the menu for. Returns NULL if the |
| 146 | // extension has been uninstalled and no longer exists. |
rdevlin.cronin | 2b74ad8 | 2015-09-17 22:15:54 | [diff] [blame] | 147 | const Extension* GetExtension() const; |
[email protected] | 9e685e5 | 2010-10-22 19:45:30 | [diff] [blame] | 148 | |
[email protected] | e167058 | 2014-08-15 23:05:41 | [diff] [blame] | 149 | // Returns the active web contents. |
| 150 | content::WebContents* GetActiveWebContents() const; |
| 151 | |
[email protected] | 69e1c12d | 2014-08-13 08:25:34 | [diff] [blame] | 152 | // Appends the extension's context menu items. |
| 153 | void AppendExtensionItems(); |
| 154 | |
[email protected] | 9e685e5 | 2010-10-22 19:45:30 | [diff] [blame] | 155 | // A copy of the extension's id. |
| 156 | std::string extension_id_; |
[email protected] | 7e9f4a8 | 2010-03-22 22:36:20 | [diff] [blame] | 157 | |
rdevlin.cronin | 0515a3b | 2015-05-14 20:50:52 | [diff] [blame] | 158 | // Whether the menu is for a component extension. |
| 159 | bool is_component_; |
| 160 | |
[email protected] | ddaaaa1 | 2013-01-29 22:52:52 | [diff] [blame] | 161 | // The extension action of the extension we are displaying the menu for (if |
| 162 | // it has one, otherwise NULL). |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 163 | raw_ptr<ExtensionAction> extension_action_; |
[email protected] | 7e9f4a8 | 2010-03-22 22:36:20 | [diff] [blame] | 164 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 165 | const raw_ptr<Browser> browser_; |
[email protected] | 7e9f4a8 | 2010-03-22 22:36:20 | [diff] [blame] | 166 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 167 | raw_ptr<Profile> profile_; |
[email protected] | 7e9f4a8 | 2010-03-22 22:36:20 | [diff] [blame] | 168 | |
[email protected] | c82526da | 2012-06-20 00:29:07 | [diff] [blame] | 169 | // The delegate which handles the 'inspect popup' menu command (or NULL). |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 170 | raw_ptr<PopupDelegate> delegate_; |
[email protected] | c82526da | 2012-06-20 00:29:07 | [diff] [blame] | 171 | |
rdevlin.cronin | 9a90651 | 2015-10-12 19:53:08 | [diff] [blame] | 172 | // The visibility of the button at the time the menu opened. |
| 173 | ButtonVisibility button_visibility_; |
| 174 | |
Eric Willigers | df4c300 | 2019-06-14 21:02:07 | [diff] [blame] | 175 | const bool can_show_icon_in_toolbar_; |
| 176 | |
[email protected] | 69e1c12d | 2014-08-13 08:25:34 | [diff] [blame] | 177 | // Menu matcher for context menu items specified by the extension. |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 178 | std::unique_ptr<ContextMenuMatcher> extension_items_; |
[email protected] | 69e1c12d | 2014-08-13 08:25:34 | [diff] [blame] | 179 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 180 | std::unique_ptr<ui::SimpleMenuModel> page_access_submenu_; |
rdevlin.cronin | b2e7ac0 | 2015-10-29 17:05:56 | [diff] [blame] | 181 | |
Karan Bhatia | 6b9706d | 2018-09-25 00:46:24 | [diff] [blame] | 182 | // The action taken by the menu. Has a valid value when the menu is being |
| 183 | // shown. |
Anton Bikineev | 46bbb97 | 2021-05-15 17:53:53 | [diff] [blame] | 184 | absl::optional<ContextMenuAction> action_taken_; |
Emilia Paz | 498abfc | 2022-01-20 17:51:42 | [diff] [blame] | 185 | |
| 186 | ContextMenuSource source_; |
[email protected] | 7e9f4a8 | 2010-03-22 22:36:20 | [diff] [blame] | 187 | }; |
| 188 | |
rdevlin.cronin | 2b74ad8 | 2015-09-17 22:15:54 | [diff] [blame] | 189 | } // namespace extensions |
| 190 | |
[email protected] | 7e9f4a8 | 2010-03-22 22:36:20 | [diff] [blame] | 191 | #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_CONTEXT_MENU_MODEL_H_ |