Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [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_UI_EXTENSIONS_EXTENSION_ACTION_VIEW_CONTROLLER_H_ |
| 6 | #define CHROME_BROWSER_UI_EXTENSIONS_EXTENSION_ACTION_VIEW_CONTROLLER_H_ |
| 7 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 8 | #include "base/memory/raw_ptr.h" |
rdevlin.cronin | fb6edb4 | 2015-04-28 18:23:17 | [diff] [blame] | 9 | #include "base/memory/weak_ptr.h" |
Sigurdur Asgeirsson | 70fd5db | 2021-04-28 19:10:35 | [diff] [blame] | 10 | #include "base/scoped_observation.h" |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 11 | #include "chrome/browser/extensions/extension_action_icon_factory.h" |
| 12 | #include "chrome/browser/extensions/extension_context_menu_model.h" |
Emilia Paz | 04ed53e | 2022-01-25 03:09:11 | [diff] [blame] | 13 | #include "chrome/browser/extensions/site_permissions_helper.h" |
Emilia Paz | c313724 | 2022-08-19 20:44:32 | [diff] [blame] | 14 | #include "chrome/browser/ui/toolbar/toolbar_action_hover_card_types.h" |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 15 | #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h" |
Evan Stade | 75872a6 | 2019-09-06 21:17:38 | [diff] [blame] | 16 | #include "extensions/browser/extension_host.h" |
rdevlin.cronin | 42efb7dd | 2015-02-11 17:50:52 | [diff] [blame] | 17 | #include "extensions/browser/extension_host_observer.h" |
Devlin Cronin | a208ee1 | 2021-04-28 20:43:31 | [diff] [blame] | 18 | #include "extensions/common/extension.h" |
| 19 | #include "extensions/common/extension_id.h" |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 20 | |
| 21 | class Browser; |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 22 | class ExtensionActionPlatformDelegate; |
rdevlin.cronin | fbfcca3 | 2015-07-09 00:28:35 | [diff] [blame] | 23 | class IconWithBadgeImageSource; |
Peter Boström | 91a80e8 | 2019-05-06 16:02:53 | [diff] [blame] | 24 | class ExtensionsContainer; |
Devlin Cronin | 248ca59 | 2022-02-08 21:04:25 | [diff] [blame] | 25 | enum class PopupShowAction; |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 26 | |
| 27 | namespace extensions { |
| 28 | class Command; |
| 29 | class Extension; |
Devlin Cronin | 720a0fb | 2020-07-22 01:31:38 | [diff] [blame] | 30 | class ExtensionAction; |
rdevlin.cronin | 65115365 | 2014-11-15 00:34:48 | [diff] [blame] | 31 | class ExtensionRegistry; |
rdevlin.cronin | b43d48e | 2015-01-29 17:51:41 | [diff] [blame] | 32 | class ExtensionViewHost; |
Emilia Paz | 04ed53e | 2022-01-25 03:09:11 | [diff] [blame] | 33 | class SitePermissionsHelper; |
Emilia Paz | d0b78d84 | 2023-02-02 02:47:46 | [diff] [blame] | 34 | } // namespace extensions |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 35 | |
Emilia Paz | 1545449 | 2023-01-31 00:08:13 | [diff] [blame] | 36 | namespace ui { |
| 37 | class ImageModel; |
| 38 | } |
| 39 | |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 40 | // The platform-independent controller for an ExtensionAction that is shown on |
| 41 | // the toolbar (such as a page or browser action). |
rdevlin.cronin | 65115365 | 2014-11-15 00:34:48 | [diff] [blame] | 42 | // Since this class doesn't own the extension or extension action in question, |
| 43 | // be sure to check for validity using ExtensionIsValid() before using those |
| 44 | // members (see also comments above ExtensionIsValid()). |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 45 | class ExtensionActionViewController |
| 46 | : public ToolbarActionViewController, |
| 47 | public ExtensionActionIconFactory::Observer, |
rdevlin.cronin | 2b74ad8 | 2015-09-17 22:15:54 | [diff] [blame] | 48 | public extensions::ExtensionContextMenuModel::PopupDelegate, |
rdevlin.cronin | 42efb7dd | 2015-02-11 17:50:52 | [diff] [blame] | 49 | public extensions::ExtensionHostObserver { |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 50 | public: |
Devlin Cronin | a208ee1 | 2021-04-28 20:43:31 | [diff] [blame] | 51 | static std::unique_ptr<ExtensionActionViewController> Create( |
| 52 | const extensions::ExtensionId& extension_id, |
| 53 | Browser* browser, |
Devlin Cronin | 1c1b6ef6 | 2021-05-04 20:57:30 | [diff] [blame] | 54 | ExtensionsContainer* extensions_container); |
Devlin Cronin | a208ee1 | 2021-04-28 20:43:31 | [diff] [blame] | 55 | |
Emilia Paz | 640b8d8 | 2021-10-30 04:11:32 | [diff] [blame] | 56 | // Returns whether any of `actions` given have access to the `web_contents`. |
| 57 | static bool AnyActionHasCurrentSiteAccess( |
| 58 | const std::vector<std::unique_ptr<ToolbarActionViewController>>& actions, |
| 59 | content::WebContents* web_contents); |
| 60 | |
Peter Boström | 53c6c595 | 2021-09-17 09:41:26 | [diff] [blame] | 61 | ExtensionActionViewController(const ExtensionActionViewController&) = delete; |
| 62 | ExtensionActionViewController& operator=( |
| 63 | const ExtensionActionViewController&) = delete; |
| 64 | |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 65 | ~ExtensionActionViewController() override; |
| 66 | |
| 67 | // ToolbarActionViewController: |
apacible | 3f73e9b3 | 2015-08-21 21:21:08 | [diff] [blame] | 68 | std::string GetId() const override; |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 69 | void SetDelegate(ToolbarActionViewDelegate* delegate) override; |
Emilia Paz | 1545449 | 2023-01-31 00:08:13 | [diff] [blame] | 70 | ui::ImageModel GetIcon(content::WebContents* web_contents, |
| 71 | const gfx::Size& size) override; |
Jan Wilken Dörrie | 3f97e29 | 2021-03-11 18:07:14 | [diff] [blame] | 72 | std::u16string GetActionName() const override; |
| 73 | std::u16string GetAccessibleName( |
| 74 | content::WebContents* web_contents) const override; |
| 75 | std::u16string GetTooltip(content::WebContents* web_contents) const override; |
Emilia Paz | 1fccfe6 | 2022-09-03 00:27:59 | [diff] [blame] | 76 | ToolbarActionViewController::HoverCardState GetHoverCardState( |
| 77 | content::WebContents* web_contents) const override; |
Emilia Paz | 04ed53e | 2022-01-25 03:09:11 | [diff] [blame] | 78 | extensions::SitePermissionsHelper::SiteInteraction GetSiteInteraction( |
Peter Boström | 251aebee | 2019-04-25 18:52:57 | [diff] [blame] | 79 | content::WebContents* web_contents) const override; |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 80 | bool IsEnabled(content::WebContents* web_contents) const override; |
Peter Kasting | 16ad82b8 | 2018-12-26 20:11:47 | [diff] [blame] | 81 | bool IsShowingPopup() const override; |
Emilia Paz | 2bf91af2 | 2023-03-03 02:13:24 | [diff] [blame] | 82 | bool ShouldShowSiteAccessRequestInToolbar( |
Emilia Paz | b7d2f93 | 2022-04-07 00:40:56 | [diff] [blame] | 83 | content::WebContents* web_contents) const override; |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 84 | void HidePopup() override; |
| 85 | gfx::NativeView GetPopupNativeView() override; |
Emilia Paz | 498abfc | 2022-01-20 17:51:42 | [diff] [blame] | 86 | ui::MenuModel* GetContextMenu( |
| 87 | extensions::ExtensionContextMenuModel::ContextMenuSource |
| 88 | context_menu_source) override; |
Caroline Rising | 9999a61 | 2020-02-24 23:41:50 | [diff] [blame] | 89 | void OnContextMenuShown() override; |
rdevlin.cronin | a81394e | 2015-04-30 20:16:56 | [diff] [blame] | 90 | void OnContextMenuClosed() override; |
Devlin Cronin | 072b6bf1 | 2022-02-08 19:40:21 | [diff] [blame] | 91 | void ExecuteUserAction(InvocationSource source) override; |
Devlin Cronin | 1d72257 | 2022-02-09 21:19:32 | [diff] [blame] | 92 | void TriggerPopupForAPI(ShowPopupCallback callback) override; |
rdevlin.cronin | 1b9a3a1 | 2014-11-13 17:15:11 | [diff] [blame] | 93 | void UpdateState() override; |
Emilia Paz | c313724 | 2022-08-19 20:44:32 | [diff] [blame] | 94 | void UpdateHoverCard(ToolbarActionView* action_view, |
| 95 | ToolbarActionHoverCardUpdateType update_type) override; |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 96 | void RegisterCommand() override; |
Peter Kasting | 8ac405b | 2020-05-25 10:54:18 | [diff] [blame] | 97 | void UnregisterCommand() override; |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 98 | |
| 99 | // ExtensionContextMenuModel::PopupDelegate: |
| 100 | void InspectPopup() override; |
| 101 | |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 102 | // Populates |command| with the command associated with |extension|, if one |
| 103 | // exists. Returns true if |command| was populated. |
Devlin Cronin | 722a178 | 2019-12-27 19:24:46 | [diff] [blame] | 104 | bool GetExtensionCommand(extensions::Command* command) const; |
| 105 | |
| 106 | // Returns true if this controller can handle accelerators (i.e., keyboard |
| 107 | // commands) on the currently-active WebContents. |
| 108 | // This must only be called if the extension has an associated command. |
| 109 | // TODO(devlin): Move accelerator logic out of the platform delegate and into |
| 110 | // this class. |
| 111 | bool CanHandleAccelerators() const; |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 112 | |
rdevlin.cronin | 75c54672 | 2015-05-06 00:26:04 | [diff] [blame] | 113 | const extensions::Extension* extension() const { return extension_.get(); } |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 114 | Browser* browser() { return browser_; } |
Devlin Cronin | 720a0fb | 2020-07-22 01:31:38 | [diff] [blame] | 115 | extensions::ExtensionAction* extension_action() { return extension_action_; } |
| 116 | const extensions::ExtensionAction* extension_action() const { |
| 117 | return extension_action_; |
| 118 | } |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 119 | ToolbarActionViewDelegate* view_delegate() { return view_delegate_; } |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 120 | |
dcheng | 9603ab9 | 2016-04-08 04:17:32 | [diff] [blame] | 121 | std::unique_ptr<IconWithBadgeImageSource> GetIconImageSourceForTesting( |
rdevlin.cronin | fbfcca3 | 2015-07-09 00:28:35 | [diff] [blame] | 122 | content::WebContents* web_contents, |
| 123 | const gfx::Size& size); |
rdevlin.cronin | fbfcca3 | 2015-07-09 00:28:35 | [diff] [blame] | 124 | |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 125 | private: |
Devlin Cronin | a208ee1 | 2021-04-28 20:43:31 | [diff] [blame] | 126 | // New instances should be instantiated with Create(). |
| 127 | ExtensionActionViewController( |
| 128 | scoped_refptr<const extensions::Extension> extension, |
| 129 | Browser* browser, |
| 130 | extensions::ExtensionAction* extension_action, |
| 131 | extensions::ExtensionRegistry* extension_registry, |
Devlin Cronin | 1c1b6ef6 | 2021-05-04 20:57:30 | [diff] [blame] | 132 | ExtensionsContainer* extensions_container); |
Devlin Cronin | a208ee1 | 2021-04-28 20:43:31 | [diff] [blame] | 133 | |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 134 | // ExtensionActionIconFactory::Observer: |
| 135 | void OnIconUpdated() override; |
| 136 | |
rdevlin.cronin | 42efb7dd | 2015-02-11 17:50:52 | [diff] [blame] | 137 | // ExtensionHostObserver: |
Fabian Sommer | fc5b6f05 | 2020-06-04 19:39:01 | [diff] [blame] | 138 | void OnExtensionHostDestroyed(extensions::ExtensionHost* host) override; |
rdevlin.cronin | b43d48e | 2015-01-29 17:51:41 | [diff] [blame] | 139 | |
rdevlin.cronin | 65115365 | 2014-11-15 00:34:48 | [diff] [blame] | 140 | // Checks if the associated |extension| is still valid by checking its |
| 141 | // status in the registry. Since the OnExtensionUnloaded() notifications are |
| 142 | // not in a deterministic order, it's possible that the view tries to refresh |
| 143 | // itself before we're notified to remove it. |
| 144 | bool ExtensionIsValid() const; |
| 145 | |
rdevlin.cronin | 5eb91b2 | 2015-04-23 19:39:05 | [diff] [blame] | 146 | // In some cases (such as when an action is shown in a menu), a substitute |
| 147 | // ToolbarActionViewController should be used for showing popups. This |
| 148 | // returns the preferred controller. |
| 149 | ExtensionActionViewController* GetPreferredPopupViewController(); |
| 150 | |
Devlin Cronin | 072b6bf1 | 2022-02-08 19:40:21 | [diff] [blame] | 151 | // Begins the process of showing the popup for the extension action on the |
| 152 | // current web contents. |by_user| is true if popup is being triggered by a |
| 153 | // user action. |
rdevlin.cronin | fb6edb4 | 2015-04-28 18:23:17 | [diff] [blame] | 154 | // The popup may not be shown synchronously if the extension is hidden and |
| 155 | // first needs to slide itself out. |
Devlin Cronin | 1d72257 | 2022-02-09 21:19:32 | [diff] [blame] | 156 | void TriggerPopup(PopupShowAction show_action, |
| 157 | bool by_user, |
| 158 | ShowPopupCallback callback); |
rdevlin.cronin | fb6edb4 | 2015-04-28 18:23:17 | [diff] [blame] | 159 | |
| 160 | // Shows the popup with the given |host|. |
dcheng | 9603ab9 | 2016-04-08 04:17:32 | [diff] [blame] | 161 | void ShowPopup(std::unique_ptr<extensions::ExtensionViewHost> host, |
rdevlin.cronin | fb6edb4 | 2015-04-28 18:23:17 | [diff] [blame] | 162 | bool grant_tab_permissions, |
Devlin Cronin | 1d72257 | 2022-02-09 21:19:32 | [diff] [blame] | 163 | PopupShowAction show_action, |
| 164 | ShowPopupCallback callback); |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 165 | |
rdevlin.cronin | b43d48e | 2015-01-29 17:51:41 | [diff] [blame] | 166 | // Handles cleanup after the popup closes. |
| 167 | void OnPopupClosed(); |
| 168 | |
rdevlin.cronin | fbfcca3 | 2015-07-09 00:28:35 | [diff] [blame] | 169 | // Returns the image source for the icon. |
dcheng | 9603ab9 | 2016-04-08 04:17:32 | [diff] [blame] | 170 | std::unique_ptr<IconWithBadgeImageSource> GetIconImageSource( |
rdevlin.cronin | fbfcca3 | 2015-07-09 00:28:35 | [diff] [blame] | 171 | content::WebContents* web_contents, |
Karan Bhatia | faee83d | 2018-07-19 06:20:59 | [diff] [blame] | 172 | const gfx::Size& size); |
rdevlin.cronin | fbfcca3 | 2015-07-09 00:28:35 | [diff] [blame] | 173 | |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 174 | // The extension associated with the action we're displaying. |
rdevlin.cronin | 75c54672 | 2015-05-06 00:26:04 | [diff] [blame] | 175 | scoped_refptr<const extensions::Extension> extension_; |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 176 | |
| 177 | // The corresponding browser. |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 178 | const raw_ptr<Browser> browser_; |
Peter Boström | 4d71c39 | 2019-04-10 00:21:04 | [diff] [blame] | 179 | |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 180 | // The browser action this view represents. The ExtensionAction is not owned |
| 181 | // by this class. |
Ali Hijazi | 4d4e2409 | 2022-10-20 22:59:31 | [diff] [blame] | 182 | const raw_ptr<extensions::ExtensionAction, DanglingUntriaged> |
| 183 | extension_action_; |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 184 | |
Peter Boström | 91a80e8 | 2019-05-06 16:02:53 | [diff] [blame] | 185 | // The corresponding ExtensionsContainer on the toolbar. |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 186 | const raw_ptr<ExtensionsContainer> extensions_container_; |
rdevlin.cronin | 5eb91b2 | 2015-04-23 19:39:05 | [diff] [blame] | 187 | |
rdevlin.cronin | b43d48e | 2015-01-29 17:51:41 | [diff] [blame] | 188 | // The extension popup's host if the popup is visible; null otherwise. |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 189 | raw_ptr<extensions::ExtensionViewHost> popup_host_; |
rdevlin.cronin | b43d48e | 2015-01-29 17:51:41 | [diff] [blame] | 190 | |
Devlin Cronin | 04f7738 | 2022-08-18 00:03:51 | [diff] [blame] | 191 | // Whether the toolbar action has opened an active popup. This is unique from |
| 192 | // `popup_host_` since `popup_host_` may be non-null even if the popup hasn't |
| 193 | // opened yet if we're waiting on other UI to be ready (e.g. the action to |
| 194 | // slide out in the toolbar). |
| 195 | bool has_opened_popup_ = false; |
| 196 | |
rdevlin.cronin | c10ac9e | 2015-01-16 21:10:08 | [diff] [blame] | 197 | // The context menu model for the extension. |
dcheng | 9603ab9 | 2016-04-08 04:17:32 | [diff] [blame] | 198 | std::unique_ptr<extensions::ExtensionContextMenuModel> context_menu_model_; |
rdevlin.cronin | c10ac9e | 2015-01-16 21:10:08 | [diff] [blame] | 199 | |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 200 | // Our view delegate. |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 201 | raw_ptr<ToolbarActionViewDelegate> view_delegate_; |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 202 | |
| 203 | // The delegate to handle platform-specific implementations. |
dcheng | 9603ab9 | 2016-04-08 04:17:32 | [diff] [blame] | 204 | std::unique_ptr<ExtensionActionPlatformDelegate> platform_delegate_; |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 205 | |
| 206 | // The object that will be used to get the browser action icon for us. |
| 207 | // It may load the icon asynchronously (in which case the initial icon |
| 208 | // returned by the factory will be transparent), so we have to observe it for |
| 209 | // updates to the icon. |
| 210 | ExtensionActionIconFactory icon_factory_; |
| 211 | |
rdevlin.cronin | 65115365 | 2014-11-15 00:34:48 | [diff] [blame] | 212 | // The associated ExtensionRegistry; cached for quick checking. |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 213 | raw_ptr<extensions::ExtensionRegistry> extension_registry_; |
rdevlin.cronin | 65115365 | 2014-11-15 00:34:48 | [diff] [blame] | 214 | |
Sigurdur Asgeirsson | 70fd5db | 2021-04-28 19:10:35 | [diff] [blame] | 215 | base::ScopedObservation<extensions::ExtensionHost, |
| 216 | extensions::ExtensionHostObserver> |
| 217 | popup_host_observation_{this}; |
rdevlin.cronin | b43d48e | 2015-01-29 17:51:41 | [diff] [blame] | 218 | |
Jeremy Roman | 495db68 | 2019-07-12 16:03:24 | [diff] [blame] | 219 | base::WeakPtrFactory<ExtensionActionViewController> weak_factory_{this}; |
rdevlin.cronin | 0953074 | 2014-11-03 19:23:28 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | #endif // CHROME_BROWSER_UI_EXTENSIONS_EXTENSION_ACTION_VIEW_CONTROLLER_H_ |