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_COMMANDS_COMMAND_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_COMMANDS_COMMAND_SERVICE_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_API_COMMANDS_COMMAND_SERVICE_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_COMMANDS_COMMAND_SERVICE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/scoped_observer.h" |
11 #include "chrome/common/extensions/command.h" | 12 #include "chrome/common/extensions/command.h" |
12 #include "content/public/browser/notification_details.h" | |
13 #include "content/public/browser/notification_observer.h" | |
14 #include "content/public/browser/notification_registrar.h" | |
15 #include "content/public/browser/notification_source.h" | |
16 #include "extensions/browser/browser_context_keyed_api_factory.h" | 13 #include "extensions/browser/browser_context_keyed_api_factory.h" |
| 14 #include "extensions/browser/extension_registry_observer.h" |
17 #include "extensions/common/extension.h" | 15 #include "extensions/common/extension.h" |
18 | 16 |
19 class Profile; | 17 class Profile; |
20 | 18 |
21 namespace base { | 19 namespace base { |
22 class DictionaryValue; | 20 class DictionaryValue; |
23 } | 21 } |
24 | 22 |
25 namespace content { | 23 namespace content { |
26 class BrowserContext; | 24 class BrowserContext; |
27 } | 25 } |
28 | 26 |
29 namespace ui { | 27 namespace ui { |
30 class Accelerator; | 28 class Accelerator; |
31 } | 29 } |
32 | 30 |
33 namespace user_prefs { | 31 namespace user_prefs { |
34 class PrefRegistrySyncable; | 32 class PrefRegistrySyncable; |
35 } | 33 } |
36 | 34 |
37 namespace extensions { | 35 namespace extensions { |
| 36 class ExtensionRegistry; |
38 | 37 |
39 // This service keeps track of preferences related to extension commands | 38 // This service keeps track of preferences related to extension commands |
40 // (assigning initial keybindings on install and removing them on deletion | 39 // (assigning initial keybindings on install and removing them on deletion |
41 // and answers questions related to which commands are active. | 40 // and answers questions related to which commands are active. |
42 class CommandService : public BrowserContextKeyedAPI, | 41 class CommandService : public BrowserContextKeyedAPI, |
43 public content::NotificationObserver { | 42 public ExtensionRegistryObserver { |
44 public: | 43 public: |
45 // An enum specifying whether to fetch all extension commands or only active | 44 // An enum specifying whether to fetch all extension commands or only active |
46 // ones. | 45 // ones. |
47 enum QueryType { | 46 enum QueryType { |
48 ALL, | 47 ALL, |
49 ACTIVE_ONLY, | 48 ACTIVE_ONLY, |
50 }; | 49 }; |
51 | 50 |
52 // An enum specifying whether the command is global in scope or not. Global | 51 // An enum specifying whether the command is global in scope or not. Global |
53 // commands -- unlike regular commands -- have a global keyboard hook | 52 // commands -- unlike regular commands -- have a global keyboard hook |
(...skipping 19 matching lines...) Expand all Loading... |
73 virtual ~CommandService(); | 72 virtual ~CommandService(); |
74 | 73 |
75 // BrowserContextKeyedAPI implementation. | 74 // BrowserContextKeyedAPI implementation. |
76 static BrowserContextKeyedAPIFactory<CommandService>* GetFactoryInstance(); | 75 static BrowserContextKeyedAPIFactory<CommandService>* GetFactoryInstance(); |
77 | 76 |
78 // Convenience method to get the CommandService for a profile. | 77 // Convenience method to get the CommandService for a profile. |
79 static CommandService* Get(content::BrowserContext* context); | 78 static CommandService* Get(content::BrowserContext* context); |
80 | 79 |
81 // Returns true if |extension| is permitted to and does remove the bookmark | 80 // Returns true if |extension| is permitted to and does remove the bookmark |
82 // shortcut key. | 81 // shortcut key. |
83 static bool RemovesBookmarkShortcut(const extensions::Extension* extension); | 82 static bool RemovesBookmarkShortcut(const Extension* extension); |
84 | 83 |
85 // Returns true if |extension| is permitted to and does remove the bookmark | 84 // Returns true if |extension| is permitted to and does remove the bookmark |
86 // open pages shortcut key. | 85 // open pages shortcut key. |
87 static bool RemovesBookmarkOpenPagesShortcut( | 86 static bool RemovesBookmarkOpenPagesShortcut(const Extension* extension); |
88 const extensions::Extension* extension); | |
89 | 87 |
90 // Gets the command (if any) for the browser action of an extension given | 88 // Gets the command (if any) for the browser action of an extension given |
91 // its |extension_id|. The function consults the master list to see if | 89 // its |extension_id|. The function consults the master list to see if |
92 // the command is active. Returns false if the extension has no browser | 90 // the command is active. Returns false if the extension has no browser |
93 // action. Returns false if the command is not active and |type| requested | 91 // action. Returns false if the command is not active and |type| requested |
94 // is ACTIVE_ONLY. |command| contains the command found and |active| (if not | 92 // is ACTIVE_ONLY. |command| contains the command found and |active| (if not |
95 // NULL) contains whether |command| is active. | 93 // NULL) contains whether |command| is active. |
96 bool GetBrowserActionCommand(const std::string& extension_id, | 94 bool GetBrowserActionCommand(const std::string& extension_id, |
97 QueryType type, | 95 QueryType type, |
98 extensions::Command* command, | 96 Command* command, |
99 bool* active) const; | 97 bool* active) const; |
100 | 98 |
101 // Gets the command (if any) for the page action of an extension given | 99 // Gets the command (if any) for the page action of an extension given |
102 // its |extension_id|. The function consults the master list to see if | 100 // its |extension_id|. The function consults the master list to see if |
103 // the command is active. Returns false if the extension has no page | 101 // the command is active. Returns false if the extension has no page |
104 // action. Returns false if the command is not active and |type| requested | 102 // action. Returns false if the command is not active and |type| requested |
105 // is ACTIVE_ONLY. |command| contains the command found and |active| (if not | 103 // is ACTIVE_ONLY. |command| contains the command found and |active| (if not |
106 // NULL) contains whether |command| is active. | 104 // NULL) contains whether |command| is active. |
107 bool GetPageActionCommand(const std::string& extension_id, | 105 bool GetPageActionCommand(const std::string& extension_id, |
108 QueryType type, | 106 QueryType type, |
109 extensions::Command* command, | 107 Command* command, |
110 bool* active) const; | 108 bool* active) const; |
111 | 109 |
112 // Gets the active named commands (if any) for the extension with | 110 // Gets the active named commands (if any) for the extension with |
113 // |extension_id|. The function consults the master list to see if the | 111 // |extension_id|. The function consults the master list to see if the |
114 // commands are active. Returns an empty map if the extension has no named | 112 // commands are active. Returns an empty map if the extension has no named |
115 // commands of the right |scope| or no such active named commands when |type| | 113 // commands of the right |scope| or no such active named commands when |type| |
116 // requested is ACTIVE_ONLY. | 114 // requested is ACTIVE_ONLY. |
117 bool GetNamedCommands(const std::string& extension_id, | 115 bool GetNamedCommands(const std::string& extension_id, |
118 QueryType type, | 116 QueryType type, |
119 CommandScope scope, | 117 CommandScope scope, |
120 extensions::CommandMap* command_map) const; | 118 CommandMap* command_map) const; |
121 | 119 |
122 // Records a keybinding |accelerator| as active for an extension with id | 120 // Records a keybinding |accelerator| as active for an extension with id |
123 // |extension_id| and command with the name |command_name|. If | 121 // |extension_id| and command with the name |command_name|. If |
124 // |allow_overrides| is false, the keybinding must be free for the change to | 122 // |allow_overrides| is false, the keybinding must be free for the change to |
125 // be recorded (as determined by the master list in |user_prefs|). If | 123 // be recorded (as determined by the master list in |user_prefs|). If |
126 // |allow_overwrites| is true, any previously recorded keybinding for this | 124 // |allow_overwrites| is true, any previously recorded keybinding for this |
127 // |accelerator| will be overwritten. If |global| is true, the command will | 125 // |accelerator| will be overwritten. If |global| is true, the command will |
128 // be registered as a global command (be active even when Chrome does not have | 126 // be registered as a global command (be active even when Chrome does not have |
129 // focus. Returns true if the change was successfully recorded. | 127 // focus. Returns true if the change was successfully recorded. |
130 bool AddKeybindingPref(const ui::Accelerator& accelerator, | 128 bool AddKeybindingPref(const ui::Accelerator& accelerator, |
(...skipping 26 matching lines...) Expand all Loading... |
157 // |extension_id| . Returns an empty Command object (with keycode | 155 // |extension_id| . Returns an empty Command object (with keycode |
158 // VKEY_UNKNOWN) if the command is not found. | 156 // VKEY_UNKNOWN) if the command is not found. |
159 Command FindCommandByName(const std::string& extension_id, | 157 Command FindCommandByName(const std::string& extension_id, |
160 const std::string& command) const; | 158 const std::string& command) const; |
161 | 159 |
162 // If the extension with |extension_id| binds a command to |accelerator|, | 160 // If the extension with |extension_id| binds a command to |accelerator|, |
163 // returns true and assigns *|command| and *|command_type| to the command and | 161 // returns true and assigns *|command| and *|command_type| to the command and |
164 // its type if non-NULL. | 162 // its type if non-NULL. |
165 bool GetBoundExtensionCommand(const std::string& extension_id, | 163 bool GetBoundExtensionCommand(const std::string& extension_id, |
166 const ui::Accelerator& accelerator, | 164 const ui::Accelerator& accelerator, |
167 extensions::Command* command, | 165 Command* command, |
168 ExtensionCommandType* command_type) const; | 166 ExtensionCommandType* command_type) const; |
169 | 167 |
170 // Returns true if |extension| is permitted to and does override the bookmark | 168 // Returns true if |extension| is permitted to and does override the bookmark |
171 // shortcut key. | 169 // shortcut key. |
172 bool OverridesBookmarkShortcut(const extensions::Extension* extension) const; | 170 bool OverridesBookmarkShortcut(const Extension* extension) const; |
173 | |
174 // Overridden from content::NotificationObserver. | |
175 virtual void Observe(int type, | |
176 const content::NotificationSource& source, | |
177 const content::NotificationDetails& details) OVERRIDE; | |
178 | 171 |
179 private: | 172 private: |
180 friend class BrowserContextKeyedAPIFactory<CommandService>; | 173 friend class BrowserContextKeyedAPIFactory<CommandService>; |
181 | 174 |
182 // BrowserContextKeyedAPI implementation. | 175 // BrowserContextKeyedAPI implementation. |
183 static const char* service_name() { | 176 static const char* service_name() { |
184 return "CommandService"; | 177 return "CommandService"; |
185 } | 178 } |
186 static const bool kServiceRedirectedInIncognito = true; | 179 static const bool kServiceRedirectedInIncognito = true; |
187 | 180 |
| 181 // ExtensionRegistryObserver. |
| 182 virtual void OnExtensionWillBeInstalled( |
| 183 content::BrowserContext* browser_context, |
| 184 const Extension* extension, |
| 185 bool is_update, |
| 186 bool from_ephemeral, |
| 187 const std::string& old_name) OVERRIDE; |
| 188 virtual void OnExtensionUninstalled(content::BrowserContext* browser_context, |
| 189 const Extension* extension) OVERRIDE; |
| 190 |
188 // Assigns initial keybinding for a given |extension|'s page action, browser | 191 // Assigns initial keybinding for a given |extension|'s page action, browser |
189 // action and named commands. In each case, if the suggested keybinding is | 192 // action and named commands. In each case, if the suggested keybinding is |
190 // free, it will be taken by this extension. If not, that keybinding request | 193 // free, it will be taken by this extension. If not, that keybinding request |
191 // is ignored. |user_pref| is the PrefService used to record the new | 194 // is ignored. |user_pref| is the PrefService used to record the new |
192 // keybinding assignment. | 195 // keybinding assignment. |
193 void AssignInitialKeybindings(const extensions::Extension* extension); | 196 void AssignInitialKeybindings(const Extension* extension); |
194 | 197 |
195 bool GetExtensionActionCommand(const std::string& extension_id, | 198 bool GetExtensionActionCommand(const std::string& extension_id, |
196 QueryType query_type, | 199 QueryType query_type, |
197 extensions::Command* command, | 200 Command* command, |
198 bool* active, | 201 bool* active, |
199 ExtensionCommandType action_type) const; | 202 ExtensionCommandType action_type) const; |
200 | 203 |
201 // The content notification registrar for listening to extension events. | |
202 content::NotificationRegistrar registrar_; | |
203 | |
204 // A weak pointer to the profile we are associated with. Not owned by us. | 204 // A weak pointer to the profile we are associated with. Not owned by us. |
205 Profile* profile_; | 205 Profile* profile_; |
206 | 206 |
| 207 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
| 208 extension_registry_observer_; |
| 209 |
207 DISALLOW_COPY_AND_ASSIGN(CommandService); | 210 DISALLOW_COPY_AND_ASSIGN(CommandService); |
208 }; | 211 }; |
209 | 212 |
210 template <> | 213 template <> |
211 void | 214 void |
212 BrowserContextKeyedAPIFactory<CommandService>::DeclareFactoryDependencies(); | 215 BrowserContextKeyedAPIFactory<CommandService>::DeclareFactoryDependencies(); |
213 | 216 |
214 } // namespace extensions | 217 } // namespace extensions |
215 | 218 |
216 #endif // CHROME_BROWSER_EXTENSIONS_API_COMMANDS_COMMAND_SERVICE_H_ | 219 #endif // CHROME_BROWSER_EXTENSIONS_API_COMMANDS_COMMAND_SERVICE_H_ |
OLD | NEW |