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 "chrome/browser/extensions/extension_action_icon_factory.h"
9 #include "chrome/browser/extensions/extension_context_menu_model.h"
10 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h"
11 #include "ui/gfx/image/image.h"
14 class ExtensionAction;
15 class ExtensionActionPlatformDelegate;
18 namespace extensions {
21 class ExtensionRegistry;
24 // The platform-independent controller for an ExtensionAction that is shown on
25 // the toolbar (such as a page or browser action).
26 // Since this class doesn't own the extension or extension action in question,
27 // be sure to check for validity using ExtensionIsValid() before using those
28 // members (see also comments above ExtensionIsValid()).
29 class ExtensionActionViewController
30 : public ToolbarActionViewController,
31 public ExtensionActionIconFactory::Observer,
32 public ExtensionContextMenuModel::PopupDelegate {
34 // The different options for showing a popup.
35 enum PopupShowAction { SHOW_POPUP, SHOW_POPUP_AND_INSPECT };
37 ExtensionActionViewController(const extensions::Extension* extension,
39 ExtensionAction* extension_action);
40 ~ExtensionActionViewController() override;
42 // ToolbarActionViewController:
43 const std::string& GetId() const override;
44 void SetDelegate(ToolbarActionViewDelegate* delegate) override;
45 gfx::Image GetIcon(content::WebContents* web_contents) override;
46 gfx::ImageSkia GetIconWithBadge() override;
47 base::string16 GetActionName() const override;
48 base::string16 GetAccessibleName(content::WebContents* web_contents) const
50 base::string16 GetTooltip(content::WebContents* web_contents) const override;
51 bool IsEnabled(content::WebContents* web_contents) const override;
52 bool HasPopup(content::WebContents* web_contents) const override;
53 void HidePopup() override;
54 gfx::NativeView GetPopupNativeView() override;
55 bool IsMenuRunning() const override;
56 bool CanDrag() const override;
57 bool ExecuteAction(bool by_user) override;
58 void PaintExtra(gfx::Canvas* canvas,
59 const gfx::Rect& bounds,
60 content::WebContents* web_contents) const override;
61 void RegisterCommand() override;
63 // ExtensionContextMenuModel::PopupDelegate:
64 void InspectPopup() override;
66 // Populates |command| with the command associated with |extension|, if one
67 // exists. Returns true if |command| was populated.
68 bool GetExtensionCommand(extensions::Command* command);
70 const extensions::Extension* extension() const { return extension_; }
71 Browser* browser() { return browser_; }
72 ExtensionAction* extension_action() { return extension_action_; }
73 const ExtensionAction* extension_action() const { return extension_action_; }
74 ToolbarActionViewDelegate* view_delegate() { return view_delegate_; }
76 void set_icon_observer(ExtensionActionIconFactory::Observer* icon_observer) {
77 icon_observer_ = icon_observer;
81 // ExtensionActionIconFactory::Observer:
82 void OnIconUpdated() override;
84 // Checks if the associated |extension| is still valid by checking its
85 // status in the registry. Since the OnExtensionUnloaded() notifications are
86 // not in a deterministic order, it's possible that the view tries to refresh
87 // itself before we're notified to remove it.
88 bool ExtensionIsValid() const;
90 // Executes the extension action with |show_action|. If
91 // |grant_tab_permissions| is true, this will grant the extension active tab
92 // permissions. Only do this if this was done through a user action (and not
93 // e.g. an API). Returns true if a popup is shown.
94 bool ExecuteAction(PopupShowAction show_action, bool grant_tab_permissions);
96 // Shows the popup for the extension action, given the associated |popup_url|.
97 // |grant_tab_permissions| is true if active tab permissions should be given
98 // to the extension; this is only true if the popup is opened through a user
100 // Returns true if a popup is successfully shown.
101 bool ShowPopupWithUrl(PopupShowAction show_action,
102 const GURL& popup_url,
103 bool grant_tab_permissions);
105 // The extension associated with the action we're displaying.
106 const extensions::Extension* extension_;
108 // The corresponding browser.
111 // The browser action this view represents. The ExtensionAction is not owned
113 ExtensionAction* extension_action_;
115 // Our view delegate.
116 ToolbarActionViewDelegate* view_delegate_;
118 // The delegate to handle platform-specific implementations.
119 scoped_ptr<ExtensionActionPlatformDelegate> platform_delegate_;
121 // The object that will be used to get the browser action icon for us.
122 // It may load the icon asynchronously (in which case the initial icon
123 // returned by the factory will be transparent), so we have to observe it for
124 // updates to the icon.
125 ExtensionActionIconFactory icon_factory_;
127 // An additional observer that we need to notify when the icon of the button
129 ExtensionActionIconFactory::Observer* icon_observer_;
131 // The associated ExtensionRegistry; cached for quick checking.
132 extensions::ExtensionRegistry* extension_registry_;
134 DISALLOW_COPY_AND_ASSIGN(ExtensionActionViewController);
137 #endif // CHROME_BROWSER_UI_EXTENSIONS_EXTENSION_ACTION_VIEW_CONTROLLER_H_