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.
4 
5 #ifndef CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_ACTION_VIEW_CONTROLLER_H_
6 #define CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_ACTION_VIEW_CONTROLLER_H_
7 
8 #include "base/strings/string16.h"
9 #include "ui/gfx/image/image.h"
10 
11 namespace content {
12 class WebContents;
13 }
14 
15 namespace gfx {
16 class Size;
17 }
18 
19 namespace ui {
20 class MenuModel;
21 }
22 
23 class ToolbarActionViewDelegate;
24 
25 // The basic controller class for an action that is shown on the toolbar -
26 // an extension action (like browser actions) or a component action (like
27 // Media Router).
28 class ToolbarActionViewController {
29  public:
30   // The status of the extension's interaction for the page. This is independent
31   // of the action's clickability.
32   enum class PageInteractionStatus {
33     // The extension cannot run on the page.
34     kNone,
35     // The extension would like access to the page, but is pending user
36     // approval.
37     kPending,
38     // The extension has permission to run on the page.
39     kActive,
40   };
41 
42   // The source for the action invocation. Used in UMA; do not reorder or delete
43   // entries.
44   enum class InvocationSource {
45     // The action was invoked from a command (keyboard shortcut).
46     kCommand = 0,
47 
48     // The action was invoked by the user activating (via mouse or keyboard)
49     // the button in the toolbar.
50     kToolbarButton = 1,
51 
52     // The action was invoked by the user activating (via mouse or keyboard)
53     // the entry in the Extensions Menu.
54     kMenuEntry = 2,
55 
56     // The action was invoked by the user activiating (via mouse or keyboard)
57     // the entry in the legacy overflow (3-dot) menu.
58     // TODO(devlin): Remove this entry when the extensions menu fully launches.
59     kLegacyOverflowedEntry = 3,
60 
61     // The action was invoked programmatically via an API.
62     kApi = 4,
63 
64     kMaxValue = kApi,
65   };
66 
~ToolbarActionViewController()67   virtual ~ToolbarActionViewController() {}
68 
69   // Returns the unique ID of this particular action. For extensions, this is
70   // the extension id; for component actions, this is the name of the component.
71   virtual std::string GetId() const = 0;
72 
73   // Sets the view delegate, which can handle most of the front-end logic.
74   virtual void SetDelegate(ToolbarActionViewDelegate* delegate) = 0;
75 
76   // Returns the icon to use for the given |web_contents| and |size|.
77   virtual gfx::Image GetIcon(content::WebContents* web_contents,
78                              const gfx::Size& size) = 0;
79 
80   // Returns the name of the action, which can be separate from the accessible
81   // name or name for the tooltip.
82   virtual base::string16 GetActionName() const = 0;
83 
84   // Returns the accessible name to use for the given |web_contents|.
85   // May be passed null, or a |web_contents| that returns -1 for
86   // |sessions::SessionTabHelper::IdForTab(..)|.
87   virtual base::string16 GetAccessibleName(content::WebContents* web_contents)
88       const = 0;
89 
90   // Returns the tooltip to use for the given |web_contents|.
91   virtual base::string16 GetTooltip(content::WebContents* web_contents)
92       const = 0;
93 
94   // Returns true if the action should be enabled on the given |web_contents|.
95   virtual bool IsEnabled(content::WebContents* web_contents) const = 0;
96 
97   // Returns true if the action has a popup for the given |web_contents|.
98   virtual bool HasPopup(content::WebContents* web_contents) const = 0;
99 
100   // Returns whether there is currently a popup visible.
101   virtual bool IsShowingPopup() const = 0;
102 
103   // Hides the current popup, if one is visible.
104   virtual void HidePopup() = 0;
105 
106   // Returns the native view for the popup, if one is active.
107   virtual gfx::NativeView GetPopupNativeView() = 0;
108 
109   // Returns the context menu model, or null if no context menu should be shown.
110   virtual ui::MenuModel* GetContextMenu() = 0;
111 
112   // Called when a context menu is shown so the controller can perform any
113   // necessary setup.
OnContextMenuShown()114   virtual void OnContextMenuShown() {}
115 
116   // Called when a context menu has closed so the controller can perform any
117   // necessary cleanup.
OnContextMenuClosed()118   virtual void OnContextMenuClosed() {}
119 
120   // Executes the default action (which is typically showing the popup). If
121   // |by_user| is true, then this was through a direct user action (as oppposed
122   // to, e.g., an API call).
123   // Returns true if a popup is shown.
124   virtual bool ExecuteAction(bool by_user, InvocationSource source) = 0;
125 
126   // Updates the current state of the action.
127   virtual void UpdateState() = 0;
128 
129   // Returns true if clicking on an otherwise-disabled action should open the
130   // context menu.
131   virtual bool DisabledClickOpensMenu() const = 0;
132 
133   // Registers an accelerator. Called when the view is added to a widget.
RegisterCommand()134   virtual void RegisterCommand() {}
135 
136   // Unregisters an accelerator. Called when the view is removed from a widget.
UnregisterCommand()137   virtual void UnregisterCommand() {}
138 
139   // Returns the PageInteractionStatus for the current page.
140   virtual PageInteractionStatus GetPageInteractionStatus(
141       content::WebContents* web_contents) const = 0;
142 };
143 
144 #endif  // CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_ACTION_VIEW_CONTROLLER_H_
145