Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / extension_action / extension_action_api.h
blob079b8905855f7a8c8e324dbb4c30bbfd9e91003f
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_
8 #include <string>
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"
19 namespace base {
20 class DictionaryValue;
23 namespace content {
24 class BrowserContext;
25 class WebContents;
28 namespace extensions {
29 class ExtensionPrefs;
31 class ExtensionActionAPI : public BrowserContextKeyedAPI {
32 public:
33 class Observer {
34 public:
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,
49 bool is_now_visible);
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();
59 protected:
60 virtual ~Observer();
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>*
70 GetFactoryInstance();
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,
78 bool visible);
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,
87 Browser* browser,
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
93 // action).
94 bool ShowExtensionActionPopup(const Extension* extension,
95 Browser* browser,
96 bool grant_active_tab_permissions);
98 // Returns true if the given |extension| wants to run on the tab pointed to
99 // by |web_contents|.
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;
120 private:
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
157 // APIs).
158 class ExtensionActionFunction : public ChromeSyncExtensionFunction {
159 public:
160 static bool ParseCSSColorString(const std::string& color_string,
161 SkColor* result);
163 protected:
164 ExtensionActionFunction();
165 ~ExtensionActionFunction() override;
166 bool RunSync() override;
167 virtual bool RunExtensionAction() = 0;
169 bool ExtractDataFromArguments();
170 void NotifyChange();
171 bool SetVisible(bool visible);
173 // All the extension action APIs take a single argument called details that
174 // is a dictionary.
175 base::DictionaryValue* details_;
177 // The tab id the extension action function should apply to, if any, or
178 // kDefaultTabId if none was specified.
179 int tab_id_;
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.
195 // show
196 class ExtensionActionShowFunction : public ExtensionActionFunction {
197 protected:
198 ~ExtensionActionShowFunction() override {}
199 bool RunExtensionAction() override;
202 // hide
203 class ExtensionActionHideFunction : public ExtensionActionFunction {
204 protected:
205 ~ExtensionActionHideFunction() override {}
206 bool RunExtensionAction() override;
209 // setIcon
210 class ExtensionActionSetIconFunction : public ExtensionActionFunction {
211 protected:
212 ~ExtensionActionSetIconFunction() override {}
213 bool RunExtensionAction() override;
216 // setTitle
217 class ExtensionActionSetTitleFunction : public ExtensionActionFunction {
218 protected:
219 ~ExtensionActionSetTitleFunction() override {}
220 bool RunExtensionAction() override;
223 // setPopup
224 class ExtensionActionSetPopupFunction : public ExtensionActionFunction {
225 protected:
226 ~ExtensionActionSetPopupFunction() override {}
227 bool RunExtensionAction() override;
230 // setBadgeText
231 class ExtensionActionSetBadgeTextFunction : public ExtensionActionFunction {
232 protected:
233 ~ExtensionActionSetBadgeTextFunction() override {}
234 bool RunExtensionAction() override;
237 // setBadgeBackgroundColor
238 class ExtensionActionSetBadgeBackgroundColorFunction
239 : public ExtensionActionFunction {
240 protected:
241 ~ExtensionActionSetBadgeBackgroundColorFunction() override {}
242 bool RunExtensionAction() override;
245 // getTitle
246 class ExtensionActionGetTitleFunction : public ExtensionActionFunction {
247 protected:
248 ~ExtensionActionGetTitleFunction() override {}
249 bool RunExtensionAction() override;
252 // getPopup
253 class ExtensionActionGetPopupFunction : public ExtensionActionFunction {
254 protected:
255 ~ExtensionActionGetPopupFunction() override {}
256 bool RunExtensionAction() override;
259 // getBadgeText
260 class ExtensionActionGetBadgeTextFunction : public ExtensionActionFunction {
261 protected:
262 ~ExtensionActionGetBadgeTextFunction() override {}
263 bool RunExtensionAction() override;
266 // getBadgeBackgroundColor
267 class ExtensionActionGetBadgeBackgroundColorFunction
268 : public ExtensionActionFunction {
269 protected:
270 ~ExtensionActionGetBadgeBackgroundColorFunction() override {}
271 bool RunExtensionAction() override;
275 // browserAction.* aliases for supported browserAction APIs.
278 class BrowserActionSetIconFunction : public ExtensionActionSetIconFunction {
279 public:
280 DECLARE_EXTENSION_FUNCTION("browserAction.setIcon", BROWSERACTION_SETICON)
282 protected:
283 ~BrowserActionSetIconFunction() override {}
286 class BrowserActionSetTitleFunction : public ExtensionActionSetTitleFunction {
287 public:
288 DECLARE_EXTENSION_FUNCTION("browserAction.setTitle", BROWSERACTION_SETTITLE)
290 protected:
291 ~BrowserActionSetTitleFunction() override {}
294 class BrowserActionSetPopupFunction : public ExtensionActionSetPopupFunction {
295 public:
296 DECLARE_EXTENSION_FUNCTION("browserAction.setPopup", BROWSERACTION_SETPOPUP)
298 protected:
299 ~BrowserActionSetPopupFunction() override {}
302 class BrowserActionGetTitleFunction : public ExtensionActionGetTitleFunction {
303 public:
304 DECLARE_EXTENSION_FUNCTION("browserAction.getTitle", BROWSERACTION_GETTITLE)
306 protected:
307 ~BrowserActionGetTitleFunction() override {}
310 class BrowserActionGetPopupFunction : public ExtensionActionGetPopupFunction {
311 public:
312 DECLARE_EXTENSION_FUNCTION("browserAction.getPopup", BROWSERACTION_GETPOPUP)
314 protected:
315 ~BrowserActionGetPopupFunction() override {}
318 class BrowserActionSetBadgeTextFunction
319 : public ExtensionActionSetBadgeTextFunction {
320 public:
321 DECLARE_EXTENSION_FUNCTION("browserAction.setBadgeText",
322 BROWSERACTION_SETBADGETEXT)
324 protected:
325 ~BrowserActionSetBadgeTextFunction() override {}
328 class BrowserActionSetBadgeBackgroundColorFunction
329 : public ExtensionActionSetBadgeBackgroundColorFunction {
330 public:
331 DECLARE_EXTENSION_FUNCTION("browserAction.setBadgeBackgroundColor",
332 BROWSERACTION_SETBADGEBACKGROUNDCOLOR)
334 protected:
335 ~BrowserActionSetBadgeBackgroundColorFunction() override {}
338 class BrowserActionGetBadgeTextFunction
339 : public ExtensionActionGetBadgeTextFunction {
340 public:
341 DECLARE_EXTENSION_FUNCTION("browserAction.getBadgeText",
342 BROWSERACTION_GETBADGETEXT)
344 protected:
345 ~BrowserActionGetBadgeTextFunction() override {}
348 class BrowserActionGetBadgeBackgroundColorFunction
349 : public ExtensionActionGetBadgeBackgroundColorFunction {
350 public:
351 DECLARE_EXTENSION_FUNCTION("browserAction.getBadgeBackgroundColor",
352 BROWSERACTION_GETBADGEBACKGROUNDCOLOR)
354 protected:
355 ~BrowserActionGetBadgeBackgroundColorFunction() override {}
358 class BrowserActionEnableFunction : public ExtensionActionShowFunction {
359 public:
360 DECLARE_EXTENSION_FUNCTION("browserAction.enable", BROWSERACTION_ENABLE)
362 protected:
363 ~BrowserActionEnableFunction() override {}
366 class BrowserActionDisableFunction : public ExtensionActionHideFunction {
367 public:
368 DECLARE_EXTENSION_FUNCTION("browserAction.disable", BROWSERACTION_DISABLE)
370 protected:
371 ~BrowserActionDisableFunction() override {}
374 class BrowserActionOpenPopupFunction : public ChromeAsyncExtensionFunction,
375 public content::NotificationObserver {
376 public:
377 DECLARE_EXTENSION_FUNCTION("browserAction.openPopup",
378 BROWSERACTION_OPEN_POPUP)
379 BrowserActionOpenPopupFunction();
381 private:
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_;
393 bool response_sent_;
395 DISALLOW_COPY_AND_ASSIGN(BrowserActionOpenPopupFunction);
398 } // namespace extensions
401 // pageAction.* aliases for supported pageAction APIs.
404 class PageActionShowFunction : public extensions::ExtensionActionShowFunction {
405 public:
406 DECLARE_EXTENSION_FUNCTION("pageAction.show", PAGEACTION_SHOW)
408 protected:
409 ~PageActionShowFunction() override {}
412 class PageActionHideFunction : public extensions::ExtensionActionHideFunction {
413 public:
414 DECLARE_EXTENSION_FUNCTION("pageAction.hide", PAGEACTION_HIDE)
416 protected:
417 ~PageActionHideFunction() override {}
420 class PageActionSetIconFunction
421 : public extensions::ExtensionActionSetIconFunction {
422 public:
423 DECLARE_EXTENSION_FUNCTION("pageAction.setIcon", PAGEACTION_SETICON)
425 protected:
426 ~PageActionSetIconFunction() override {}
429 class PageActionSetTitleFunction
430 : public extensions::ExtensionActionSetTitleFunction {
431 public:
432 DECLARE_EXTENSION_FUNCTION("pageAction.setTitle", PAGEACTION_SETTITLE)
434 protected:
435 ~PageActionSetTitleFunction() override {}
438 class PageActionSetPopupFunction
439 : public extensions::ExtensionActionSetPopupFunction {
440 public:
441 DECLARE_EXTENSION_FUNCTION("pageAction.setPopup", PAGEACTION_SETPOPUP)
443 protected:
444 ~PageActionSetPopupFunction() override {}
447 class PageActionGetTitleFunction
448 : public extensions::ExtensionActionGetTitleFunction {
449 public:
450 DECLARE_EXTENSION_FUNCTION("pageAction.getTitle", PAGEACTION_GETTITLE)
452 protected:
453 ~PageActionGetTitleFunction() override {}
456 class PageActionGetPopupFunction
457 : public extensions::ExtensionActionGetPopupFunction {
458 public:
459 DECLARE_EXTENSION_FUNCTION("pageAction.getPopup", PAGEACTION_GETPOPUP)
461 protected:
462 ~PageActionGetPopupFunction() override {}
465 #endif // CHROME_BROWSER_EXTENSIONS_API_EXTENSION_ACTION_EXTENSION_ACTION_API_H_