OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_EXTENSION_ACTION_EXTENSION_ACTION_API_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_EXTENSION_ACTION_EXTENSION_ACTION_API_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_API_EXTENSION_ACTION_EXTENSION_ACTION_API_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_EXTENSION_ACTION_EXTENSION_ACTION_API_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/memory/weak_ptr.h" | |
11 #include "base/observer_list.h" | 10 #include "base/observer_list.h" |
12 #include "base/scoped_observer.h" | |
13 #include "chrome/browser/extensions/chrome_extension_function.h" | 11 #include "chrome/browser/extensions/chrome_extension_function.h" |
14 #include "chrome/browser/extensions/extension_action.h" | 12 #include "chrome/browser/extensions/extension_action.h" |
15 #include "content/public/browser/notification_observer.h" | 13 #include "content/public/browser/notification_observer.h" |
16 #include "content/public/browser/notification_registrar.h" | 14 #include "content/public/browser/notification_registrar.h" |
17 #include "extensions/browser/browser_context_keyed_api_factory.h" | 15 #include "extensions/browser/browser_context_keyed_api_factory.h" |
18 #include "extensions/browser/extension_registry_observer.h" | 16 #include "third_party/skia/include/core/SkColor.h" |
19 | 17 |
20 namespace base { | 18 namespace base { |
21 class DictionaryValue; | 19 class DictionaryValue; |
22 } | 20 } |
23 | 21 |
24 namespace content { | 22 namespace content { |
25 class BrowserContext; | 23 class BrowserContext; |
26 class WebContents; | 24 class WebContents; |
27 } | 25 } |
28 | 26 |
29 namespace extensions { | 27 namespace extensions { |
30 class ExtensionPrefs; | 28 class ExtensionPrefs; |
31 class ExtensionRegistry; | |
32 class TabHelper; | |
33 | 29 |
34 class ExtensionActionAPI : public BrowserContextKeyedAPI { | 30 class ExtensionActionAPI : public BrowserContextKeyedAPI { |
35 public: | 31 public: |
36 class Observer { | 32 class Observer { |
37 public: | 33 public: |
38 // Called when there is a change to the given |extension_action|. | 34 // Called when there is a change to the given |extension_action|. |
39 // |web_contents| is the web contents that was affected, and | 35 // |web_contents| is the web contents that was affected, and |
40 // |browser_context| is the associated BrowserContext. (The latter is | 36 // |browser_context| is the associated BrowserContext. (The latter is |
41 // included because ExtensionActionAPI is shared between normal and | 37 // included because ExtensionActionAPI is shared between normal and |
42 // incognito contexts, so |browser_context| may not equal | 38 // incognito contexts, so |browser_context| may not equal |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 static const char* service_name() { return "ExtensionActionAPI"; } | 115 static const char* service_name() { return "ExtensionActionAPI"; } |
120 static const bool kServiceRedirectedInIncognito = true; | 116 static const bool kServiceRedirectedInIncognito = true; |
121 | 117 |
122 ObserverList<Observer> observers_; | 118 ObserverList<Observer> observers_; |
123 | 119 |
124 content::BrowserContext* browser_context_; | 120 content::BrowserContext* browser_context_; |
125 | 121 |
126 DISALLOW_COPY_AND_ASSIGN(ExtensionActionAPI); | 122 DISALLOW_COPY_AND_ASSIGN(ExtensionActionAPI); |
127 }; | 123 }; |
128 | 124 |
129 // This class manages reading and writing browser action values from storage. | |
130 class ExtensionActionStorageManager | |
131 : public ExtensionActionAPI::Observer, | |
132 public ExtensionRegistryObserver, | |
133 public base::SupportsWeakPtr<ExtensionActionStorageManager> { | |
134 public: | |
135 explicit ExtensionActionStorageManager(Profile* profile); | |
136 virtual ~ExtensionActionStorageManager(); | |
137 | |
138 private: | |
139 // ExtensionActionAPI::Observer: | |
140 virtual void OnExtensionActionUpdated( | |
141 ExtensionAction* extension_action, | |
142 content::WebContents* web_contents, | |
143 content::BrowserContext* browser_context) OVERRIDE; | |
144 virtual void OnExtensionActionAPIShuttingDown() OVERRIDE; | |
145 | |
146 // ExtensionRegistryObserver: | |
147 virtual void OnExtensionLoaded(content::BrowserContext* browser_context, | |
148 const Extension* extension) OVERRIDE; | |
149 | |
150 // Reads/Writes the ExtensionAction's default values to/from storage. | |
151 void WriteToStorage(ExtensionAction* extension_action); | |
152 void ReadFromStorage( | |
153 const std::string& extension_id, scoped_ptr<base::Value> value); | |
154 | |
155 Profile* profile_; | |
156 | |
157 ScopedObserver<ExtensionActionAPI, ExtensionActionAPI::Observer> | |
158 extension_action_observer_; | |
159 | |
160 // Listen to extension loaded notification. | |
161 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> | |
162 extension_registry_observer_; | |
163 }; | |
164 | |
165 // Implementation of the browserAction and pageAction APIs. | 125 // Implementation of the browserAction and pageAction APIs. |
166 // | 126 // |
167 // Divergent behaviour between the two is minimal (pageAction has required | 127 // Divergent behaviour between the two is minimal (pageAction has required |
168 // tabIds while browserAction's are optional, they have different internal | 128 // tabIds while browserAction's are optional, they have different internal |
169 // browser notification requirements, and not all functions are defined for all | 129 // browser notification requirements, and not all functions are defined for all |
170 // APIs). | 130 // APIs). |
171 class ExtensionActionFunction : public ChromeSyncExtensionFunction { | 131 class ExtensionActionFunction : public ChromeSyncExtensionFunction { |
172 public: | 132 public: |
173 static bool ParseCSSColorString(const std::string& color_string, | 133 static bool ParseCSSColorString(const std::string& color_string, |
174 SkColor* result); | 134 SkColor* result); |
175 | 135 |
176 protected: | 136 protected: |
177 ExtensionActionFunction(); | 137 ExtensionActionFunction(); |
178 virtual ~ExtensionActionFunction(); | 138 virtual ~ExtensionActionFunction(); |
179 virtual bool RunSync() OVERRIDE; | 139 virtual bool RunSync() OVERRIDE; |
180 virtual bool RunExtensionAction() = 0; | 140 virtual bool RunExtensionAction() = 0; |
181 | 141 |
182 bool ExtractDataFromArguments(); | 142 bool ExtractDataFromArguments(); |
183 void NotifyChange(); | 143 void NotifyChange(); |
184 bool SetVisible(bool visible); | 144 bool SetVisible(bool visible); |
185 | 145 |
186 // Extension-related information for |tab_id_|. | |
187 // CHECK-fails if there is no tab. | |
188 extensions::TabHelper& tab_helper() const; | |
189 | |
190 // All the extension action APIs take a single argument called details that | 146 // All the extension action APIs take a single argument called details that |
191 // is a dictionary. | 147 // is a dictionary. |
192 base::DictionaryValue* details_; | 148 base::DictionaryValue* details_; |
193 | 149 |
194 // The tab id the extension action function should apply to, if any, or | 150 // The tab id the extension action function should apply to, if any, or |
195 // kDefaultTabId if none was specified. | 151 // kDefaultTabId if none was specified. |
196 int tab_id_; | 152 int tab_id_; |
197 | 153 |
198 // WebContents for |tab_id_| if one exists. | 154 // WebContents for |tab_id_| if one exists. |
199 content::WebContents* contents_; | 155 content::WebContents* contents_; |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 class PageActionGetPopupFunction | 429 class PageActionGetPopupFunction |
474 : public extensions::ExtensionActionGetPopupFunction { | 430 : public extensions::ExtensionActionGetPopupFunction { |
475 public: | 431 public: |
476 DECLARE_EXTENSION_FUNCTION("pageAction.getPopup", PAGEACTION_GETPOPUP) | 432 DECLARE_EXTENSION_FUNCTION("pageAction.getPopup", PAGEACTION_GETPOPUP) |
477 | 433 |
478 protected: | 434 protected: |
479 virtual ~PageActionGetPopupFunction() {} | 435 virtual ~PageActionGetPopupFunction() {} |
480 }; | 436 }; |
481 | 437 |
482 #endif // CHROME_BROWSER_EXTENSIONS_API_EXTENSION_ACTION_EXTENSION_ACTION_API_H
_ | 438 #endif // CHROME_BROWSER_EXTENSIONS_API_EXTENSION_ACTION_EXTENSION_ACTION_API_H
_ |
OLD | NEW |