1 // Copyright (c) 2012 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_EXTENSIONS_API_EXTENSION_ACTION_EXTENSION_ACTION_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_EXTENSION_ACTION_EXTENSION_ACTION_API_H_
10 #include "base/observer_list.h"
11 #include "chrome/browser/extensions/chrome_extension_function.h"
12 #include "chrome/browser/extensions/extension_action.h"
13 #include "content/public/browser/notification_observer.h"
14 #include "content/public/browser/notification_registrar.h"
15 #include "extensions/browser/browser_context_keyed_api_factory.h"
16 #include "extensions/browser/extension_event_histogram_value.h"
17 #include "third_party/skia/include/core/SkColor.h"
20 class DictionaryValue
;
28 namespace extensions
{
31 class ExtensionActionAPI
: public BrowserContextKeyedAPI
{
35 // Called when there is a change to the given |extension_action|.
36 // |web_contents| is the web contents that was affected, and
37 // |browser_context| is the associated BrowserContext. (The latter is
38 // included because ExtensionActionAPI is shared between normal and
39 // incognito contexts, so |browser_context| may not equal
40 // |browser_context_|.)
41 virtual void OnExtensionActionUpdated(
42 ExtensionAction
* extension_action
,
43 content::WebContents
* web_contents
,
44 content::BrowserContext
* browser_context
);
46 // Called when there is a change to the extension action's visibility.
47 virtual void OnExtensionActionVisibilityChanged(
48 const std::string
& extension_id
,
51 // Called when the page actions have been refreshed do to a possible change
52 // in count or visibility.
53 virtual void OnPageActionsUpdated(content::WebContents
* web_contents
);
55 // Called when the ExtensionActionAPI is shutting down, giving observers a
56 // chance to unregister themselves if there is not a definitive lifecycle.
57 virtual void OnExtensionActionAPIShuttingDown();
63 explicit ExtensionActionAPI(content::BrowserContext
* context
);
64 ~ExtensionActionAPI() override
;
66 // Convenience method to get the instance for a profile.
67 static ExtensionActionAPI
* Get(content::BrowserContext
* context
);
69 static BrowserContextKeyedAPIFactory
<ExtensionActionAPI
>*
72 // Add or remove observers.
73 void AddObserver(Observer
* observer
);
74 void RemoveObserver(Observer
* observer
);
76 bool GetBrowserActionVisibility(const std::string
& extension_id
);
77 void SetBrowserActionVisibility(const std::string
& extension_id
,
80 // BrowserContextKeyedAPI implementation.
81 // Executes the action of the given |extension| on the |browser|'s active
82 // web contents. If |grant_tab_permissions| is true, this will also grant
83 // activeTab to the extension (so this should only be done if this is through
84 // a direct user action). Returns the action that should be taken.
85 ExtensionAction::ShowAction
ExecuteExtensionAction(
86 const Extension
* extension
,
88 bool grant_active_tab_permissions
);
90 // Opens the popup for the given |extension| in the given |browser|'s window.
91 // If |grant_active_tab_permissions| is true, this grants the extension
92 // activeTab (so this should only be done if this is through a direct user
94 bool ShowExtensionActionPopup(const Extension
* extension
,
96 bool grant_active_tab_permissions
);
98 // Returns true if the given |extension| wants to run on the tab pointed to
100 bool ExtensionWantsToRun(const Extension
* extension
,
101 content::WebContents
* web_contents
);
103 // Notifies that there has been a change in the given |extension_action|.
104 void NotifyChange(ExtensionAction
* extension_action
,
105 content::WebContents
* web_contents
,
106 content::BrowserContext
* browser_context
);
108 // Clears the values for all ExtensionActions for the tab associated with the
109 // given |web_contents| (and signals that page actions changed).
110 void ClearAllValuesForTab(content::WebContents
* web_contents
);
112 // Notifies that the current set of page actions for |web_contents| has
113 // changed, and signals the browser to update.
114 void NotifyPageActionsChanged(content::WebContents
* web_contents
);
116 void set_prefs_for_testing(ExtensionPrefs
* prefs
) {
117 extension_prefs_
= prefs
;
121 friend class BrowserContextKeyedAPIFactory
<ExtensionActionAPI
>;
123 // Returns the associated extension prefs.
124 ExtensionPrefs
* GetExtensionPrefs();
126 // The DispatchEvent methods forward events to the |context|'s event router.
127 void DispatchEventToExtension(content::BrowserContext
* context
,
128 const std::string
& extension_id
,
129 events::HistogramValue histogram_value
,
130 const std::string
& event_name
,
131 scoped_ptr
<base::ListValue
> event_args
);
133 // Called when either a browser or page action is executed. Figures out which
134 // event to send based on what the extension wants.
135 void ExtensionActionExecuted(const ExtensionAction
& extension_action
,
136 content::WebContents
* web_contents
);
138 // BrowserContextKeyedAPI implementation.
139 void Shutdown() override
;
140 static const char* service_name() { return "ExtensionActionAPI"; }
141 static const bool kServiceRedirectedInIncognito
= true;
143 base::ObserverList
<Observer
> observers_
;
145 content::BrowserContext
* browser_context_
;
147 ExtensionPrefs
* extension_prefs_
;
149 DISALLOW_COPY_AND_ASSIGN(ExtensionActionAPI
);
152 // Implementation of the browserAction and pageAction APIs.
154 // Divergent behaviour between the two is minimal (pageAction has required
155 // tabIds while browserAction's are optional, they have different internal
156 // browser notification requirements, and not all functions are defined for all
158 class ExtensionActionFunction
: public ChromeSyncExtensionFunction
{
160 static bool ParseCSSColorString(const std::string
& color_string
,
164 ExtensionActionFunction();
165 ~ExtensionActionFunction() override
;
166 bool RunSync() override
;
167 virtual bool RunExtensionAction() = 0;
169 bool ExtractDataFromArguments();
171 bool SetVisible(bool visible
);
173 // All the extension action APIs take a single argument called details that
175 base::DictionaryValue
* details_
;
177 // The tab id the extension action function should apply to, if any, or
178 // kDefaultTabId if none was specified.
181 // WebContents for |tab_id_| if one exists.
182 content::WebContents
* contents_
;
184 // The extension action for the current extension.
185 ExtensionAction
* extension_action_
;
189 // Implementations of each extension action API.
191 // pageAction and browserAction bindings are created for these by extending them
192 // then declaring an EXTENSION_FUNCTION_NAME.
196 class ExtensionActionShowFunction
: public ExtensionActionFunction
{
198 ~ExtensionActionShowFunction() override
{}
199 bool RunExtensionAction() override
;
203 class ExtensionActionHideFunction
: public ExtensionActionFunction
{
205 ~ExtensionActionHideFunction() override
{}
206 bool RunExtensionAction() override
;
210 class ExtensionActionSetIconFunction
: public ExtensionActionFunction
{
212 ~ExtensionActionSetIconFunction() override
{}
213 bool RunExtensionAction() override
;
217 class ExtensionActionSetTitleFunction
: public ExtensionActionFunction
{
219 ~ExtensionActionSetTitleFunction() override
{}
220 bool RunExtensionAction() override
;
224 class ExtensionActionSetPopupFunction
: public ExtensionActionFunction
{
226 ~ExtensionActionSetPopupFunction() override
{}
227 bool RunExtensionAction() override
;
231 class ExtensionActionSetBadgeTextFunction
: public ExtensionActionFunction
{
233 ~ExtensionActionSetBadgeTextFunction() override
{}
234 bool RunExtensionAction() override
;
237 // setBadgeBackgroundColor
238 class ExtensionActionSetBadgeBackgroundColorFunction
239 : public ExtensionActionFunction
{
241 ~ExtensionActionSetBadgeBackgroundColorFunction() override
{}
242 bool RunExtensionAction() override
;
246 class ExtensionActionGetTitleFunction
: public ExtensionActionFunction
{
248 ~ExtensionActionGetTitleFunction() override
{}
249 bool RunExtensionAction() override
;
253 class ExtensionActionGetPopupFunction
: public ExtensionActionFunction
{
255 ~ExtensionActionGetPopupFunction() override
{}
256 bool RunExtensionAction() override
;
260 class ExtensionActionGetBadgeTextFunction
: public ExtensionActionFunction
{
262 ~ExtensionActionGetBadgeTextFunction() override
{}
263 bool RunExtensionAction() override
;
266 // getBadgeBackgroundColor
267 class ExtensionActionGetBadgeBackgroundColorFunction
268 : public ExtensionActionFunction
{
270 ~ExtensionActionGetBadgeBackgroundColorFunction() override
{}
271 bool RunExtensionAction() override
;
275 // browserAction.* aliases for supported browserAction APIs.
278 class BrowserActionSetIconFunction
: public ExtensionActionSetIconFunction
{
280 DECLARE_EXTENSION_FUNCTION("browserAction.setIcon", BROWSERACTION_SETICON
)
283 ~BrowserActionSetIconFunction() override
{}
286 class BrowserActionSetTitleFunction
: public ExtensionActionSetTitleFunction
{
288 DECLARE_EXTENSION_FUNCTION("browserAction.setTitle", BROWSERACTION_SETTITLE
)
291 ~BrowserActionSetTitleFunction() override
{}
294 class BrowserActionSetPopupFunction
: public ExtensionActionSetPopupFunction
{
296 DECLARE_EXTENSION_FUNCTION("browserAction.setPopup", BROWSERACTION_SETPOPUP
)
299 ~BrowserActionSetPopupFunction() override
{}
302 class BrowserActionGetTitleFunction
: public ExtensionActionGetTitleFunction
{
304 DECLARE_EXTENSION_FUNCTION("browserAction.getTitle", BROWSERACTION_GETTITLE
)
307 ~BrowserActionGetTitleFunction() override
{}
310 class BrowserActionGetPopupFunction
: public ExtensionActionGetPopupFunction
{
312 DECLARE_EXTENSION_FUNCTION("browserAction.getPopup", BROWSERACTION_GETPOPUP
)
315 ~BrowserActionGetPopupFunction() override
{}
318 class BrowserActionSetBadgeTextFunction
319 : public ExtensionActionSetBadgeTextFunction
{
321 DECLARE_EXTENSION_FUNCTION("browserAction.setBadgeText",
322 BROWSERACTION_SETBADGETEXT
)
325 ~BrowserActionSetBadgeTextFunction() override
{}
328 class BrowserActionSetBadgeBackgroundColorFunction
329 : public ExtensionActionSetBadgeBackgroundColorFunction
{
331 DECLARE_EXTENSION_FUNCTION("browserAction.setBadgeBackgroundColor",
332 BROWSERACTION_SETBADGEBACKGROUNDCOLOR
)
335 ~BrowserActionSetBadgeBackgroundColorFunction() override
{}
338 class BrowserActionGetBadgeTextFunction
339 : public ExtensionActionGetBadgeTextFunction
{
341 DECLARE_EXTENSION_FUNCTION("browserAction.getBadgeText",
342 BROWSERACTION_GETBADGETEXT
)
345 ~BrowserActionGetBadgeTextFunction() override
{}
348 class BrowserActionGetBadgeBackgroundColorFunction
349 : public ExtensionActionGetBadgeBackgroundColorFunction
{
351 DECLARE_EXTENSION_FUNCTION("browserAction.getBadgeBackgroundColor",
352 BROWSERACTION_GETBADGEBACKGROUNDCOLOR
)
355 ~BrowserActionGetBadgeBackgroundColorFunction() override
{}
358 class BrowserActionEnableFunction
: public ExtensionActionShowFunction
{
360 DECLARE_EXTENSION_FUNCTION("browserAction.enable", BROWSERACTION_ENABLE
)
363 ~BrowserActionEnableFunction() override
{}
366 class BrowserActionDisableFunction
: public ExtensionActionHideFunction
{
368 DECLARE_EXTENSION_FUNCTION("browserAction.disable", BROWSERACTION_DISABLE
)
371 ~BrowserActionDisableFunction() override
{}
374 class BrowserActionOpenPopupFunction
: public ChromeAsyncExtensionFunction
,
375 public content::NotificationObserver
{
377 DECLARE_EXTENSION_FUNCTION("browserAction.openPopup",
378 BROWSERACTION_OPEN_POPUP
)
379 BrowserActionOpenPopupFunction();
382 ~BrowserActionOpenPopupFunction() override
{}
384 // ExtensionFunction:
385 bool RunAsync() override
;
387 void Observe(int type
,
388 const content::NotificationSource
& source
,
389 const content::NotificationDetails
& details
) override
;
390 void OpenPopupTimedOut();
392 content::NotificationRegistrar registrar_
;
395 DISALLOW_COPY_AND_ASSIGN(BrowserActionOpenPopupFunction
);
398 } // namespace extensions
401 // pageAction.* aliases for supported pageAction APIs.
404 class PageActionShowFunction
: public extensions::ExtensionActionShowFunction
{
406 DECLARE_EXTENSION_FUNCTION("pageAction.show", PAGEACTION_SHOW
)
409 ~PageActionShowFunction() override
{}
412 class PageActionHideFunction
: public extensions::ExtensionActionHideFunction
{
414 DECLARE_EXTENSION_FUNCTION("pageAction.hide", PAGEACTION_HIDE
)
417 ~PageActionHideFunction() override
{}
420 class PageActionSetIconFunction
421 : public extensions::ExtensionActionSetIconFunction
{
423 DECLARE_EXTENSION_FUNCTION("pageAction.setIcon", PAGEACTION_SETICON
)
426 ~PageActionSetIconFunction() override
{}
429 class PageActionSetTitleFunction
430 : public extensions::ExtensionActionSetTitleFunction
{
432 DECLARE_EXTENSION_FUNCTION("pageAction.setTitle", PAGEACTION_SETTITLE
)
435 ~PageActionSetTitleFunction() override
{}
438 class PageActionSetPopupFunction
439 : public extensions::ExtensionActionSetPopupFunction
{
441 DECLARE_EXTENSION_FUNCTION("pageAction.setPopup", PAGEACTION_SETPOPUP
)
444 ~PageActionSetPopupFunction() override
{}
447 class PageActionGetTitleFunction
448 : public extensions::ExtensionActionGetTitleFunction
{
450 DECLARE_EXTENSION_FUNCTION("pageAction.getTitle", PAGEACTION_GETTITLE
)
453 ~PageActionGetTitleFunction() override
{}
456 class PageActionGetPopupFunction
457 : public extensions::ExtensionActionGetPopupFunction
{
459 DECLARE_EXTENSION_FUNCTION("pageAction.getPopup", PAGEACTION_GETPOPUP
)
462 ~PageActionGetPopupFunction() override
{}
465 #endif // CHROME_BROWSER_EXTENSIONS_API_EXTENSION_ACTION_EXTENSION_ACTION_API_H_