1 // Copyright 2014 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_UI_EXTENSIONS_EXTENSION_ACTION_VIEW_CONTROLLER_H_
6 #define CHROME_BROWSER_UI_EXTENSIONS_EXTENSION_ACTION_VIEW_CONTROLLER_H_
8 #include "base/memory/weak_ptr.h"
9 #include "base/scoped_observer.h"
10 #include "chrome/browser/extensions/extension_action_icon_factory.h"
11 #include "chrome/browser/extensions/extension_context_menu_model.h"
12 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h"
13 #include "extensions/browser/extension_host_observer.h"
14 #include "ui/gfx/image/image.h"
17 class ExtensionAction
;
18 class ExtensionActionPlatformDelegate
;
20 class ToolbarActionsBar
;
22 namespace extensions
{
25 class ExtensionRegistry
;
26 class ExtensionViewHost
;
29 // The platform-independent controller for an ExtensionAction that is shown on
30 // the toolbar (such as a page or browser action).
31 // Since this class doesn't own the extension or extension action in question,
32 // be sure to check for validity using ExtensionIsValid() before using those
33 // members (see also comments above ExtensionIsValid()).
34 class ExtensionActionViewController
35 : public ToolbarActionViewController
,
36 public ExtensionActionIconFactory::Observer
,
37 public ExtensionContextMenuModel::PopupDelegate
,
38 public extensions::ExtensionHostObserver
{
40 // The different options for showing a popup.
41 enum PopupShowAction
{ SHOW_POPUP
, SHOW_POPUP_AND_INSPECT
};
43 ExtensionActionViewController(const extensions::Extension
* extension
,
45 ExtensionAction
* extension_action
,
46 ToolbarActionsBar
* toolbar_actions_bar
);
47 ~ExtensionActionViewController() override
;
49 // ToolbarActionViewController:
50 const std::string
& GetId() const override
;
51 void SetDelegate(ToolbarActionViewDelegate
* delegate
) override
;
52 gfx::Image
GetIcon(content::WebContents
* web_contents
) override
;
53 gfx::ImageSkia
GetIconWithBadge() override
;
54 base::string16
GetActionName() const override
;
55 base::string16
GetAccessibleName(content::WebContents
* web_contents
) const
57 base::string16
GetTooltip(content::WebContents
* web_contents
) const override
;
58 bool IsEnabled(content::WebContents
* web_contents
) const override
;
59 bool WantsToRun(content::WebContents
* web_contents
) const override
;
60 bool HasPopup(content::WebContents
* web_contents
) const override
;
61 void HidePopup() override
;
62 gfx::NativeView
GetPopupNativeView() override
;
63 ui::MenuModel
* GetContextMenu() override
;
64 void OnContextMenuClosed() override
;
65 bool CanDrag() const override
;
66 bool ExecuteAction(bool by_user
) override
;
67 void UpdateState() override
;
68 void PaintExtra(gfx::Canvas
* canvas
,
69 const gfx::Rect
& bounds
,
70 content::WebContents
* web_contents
) const override
;
71 void RegisterCommand() override
;
73 // ExtensionContextMenuModel::PopupDelegate:
74 void InspectPopup() override
;
76 // Closes the active popup (whether it was this action's popup or not).
77 void HideActivePopup();
80 // Populates |command| with the command associated with |extension|, if one
81 // exists. Returns true if |command| was populated.
82 bool GetExtensionCommand(extensions::Command
* command
);
84 const extensions::Extension
* extension() const { return extension_
.get(); }
85 Browser
* browser() { return browser_
; }
86 ExtensionAction
* extension_action() { return extension_action_
; }
87 const ExtensionAction
* extension_action() const { return extension_action_
; }
88 ToolbarActionViewDelegate
* view_delegate() { return view_delegate_
; }
89 bool is_showing_popup() const { return popup_host_
!= nullptr; }
91 void set_icon_observer(ExtensionActionIconFactory::Observer
* icon_observer
) {
92 icon_observer_
= icon_observer
;
96 // ExtensionActionIconFactory::Observer:
97 void OnIconUpdated() override
;
99 // ExtensionHostObserver:
100 void OnExtensionHostDestroyed(const extensions::ExtensionHost
* host
) override
;
102 // Checks if the associated |extension| is still valid by checking its
103 // status in the registry. Since the OnExtensionUnloaded() notifications are
104 // not in a deterministic order, it's possible that the view tries to refresh
105 // itself before we're notified to remove it.
106 bool ExtensionIsValid() const;
108 // In some cases (such as when an action is shown in a menu), a substitute
109 // ToolbarActionViewController should be used for showing popups. This
110 // returns the preferred controller.
111 ExtensionActionViewController
* GetPreferredPopupViewController();
113 // Executes the extension action with |show_action|. If
114 // |grant_tab_permissions| is true, this will grant the extension active tab
115 // permissions. Only do this if this was done through a user action (and not
116 // e.g. an API). Returns true if a popup is shown.
117 bool ExecuteAction(PopupShowAction show_action
, bool grant_tab_permissions
);
119 // Begins the process of showing the popup for the extension action, given the
120 // associated |popup_url|. |grant_tab_permissions| is true if active tab
121 // permissions should be given to the extension; this is only true if the
122 // popup is opened through a user action.
123 // The popup may not be shown synchronously if the extension is hidden and
124 // first needs to slide itself out.
125 // Returns true if a popup will be shown.
126 bool TriggerPopupWithUrl(PopupShowAction show_action
,
127 const GURL
& popup_url
,
128 bool grant_tab_permissions
);
130 // Shows the popup with the given |host|.
131 void ShowPopup(scoped_ptr
<extensions::ExtensionViewHost
> host
,
132 bool grant_tab_permissions
,
133 PopupShowAction show_action
);
135 // Handles cleanup after the popup closes.
136 void OnPopupClosed();
138 // The extension associated with the action we're displaying.
139 scoped_refptr
<const extensions::Extension
> extension_
;
141 // The corresponding browser.
144 // The browser action this view represents. The ExtensionAction is not owned
146 ExtensionAction
* extension_action_
;
148 // The owning ToolbarActionsBar, if any. This will be null if this is a
149 // page action without the toolbar redesign turned on.
150 // TODO(devlin): Would this be better behind a delegate interface? On the one
151 // hand, it's odd for this class to know about ToolbarActionsBar, but on the
152 // other, yet-another-delegate-class might just confuse things.
153 ToolbarActionsBar
* toolbar_actions_bar_
;
155 // The extension popup's host if the popup is visible; null otherwise.
156 extensions::ExtensionViewHost
* popup_host_
;
158 // The context menu model for the extension.
159 scoped_refptr
<ExtensionContextMenuModel
> context_menu_model_
;
161 // Our view delegate.
162 ToolbarActionViewDelegate
* view_delegate_
;
164 // The delegate to handle platform-specific implementations.
165 scoped_ptr
<ExtensionActionPlatformDelegate
> platform_delegate_
;
167 // The object that will be used to get the browser action icon for us.
168 // It may load the icon asynchronously (in which case the initial icon
169 // returned by the factory will be transparent), so we have to observe it for
170 // updates to the icon.
171 ExtensionActionIconFactory icon_factory_
;
173 // An additional observer that we need to notify when the icon of the button
175 ExtensionActionIconFactory::Observer
* icon_observer_
;
177 // The associated ExtensionRegistry; cached for quick checking.
178 extensions::ExtensionRegistry
* extension_registry_
;
180 ScopedObserver
<extensions::ExtensionHost
, extensions::ExtensionHostObserver
>
181 popup_host_observer_
;
183 base::WeakPtrFactory
<ExtensionActionViewController
> weak_factory_
;
185 DISALLOW_COPY_AND_ASSIGN(ExtensionActionViewController
);
188 #endif // CHROME_BROWSER_UI_EXTENSIONS_EXTENSION_ACTION_VIEW_CONTROLLER_H_