OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_COMMANDS_EXTENSION_COMMAND_SERVICE_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_API_COMMANDS_EXTENSION_COMMAND_SERVICE_H_ | |
7 #pragma once | |
8 | |
9 #include <string> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "chrome/browser/profiles/profile_keyed_service.h" | |
13 #include "chrome/common/extensions/extension.h" | |
14 #include "chrome/common/extensions/extension_commands.h" | |
15 #include "content/public/browser/notification_details.h" | |
16 #include "content/public/browser/notification_observer.h" | |
17 #include "content/public/browser/notification_registrar.h" | |
18 #include "content/public/browser/notification_source.h" | |
19 | |
20 class PrefService; | |
21 class Profile; | |
22 | |
23 namespace base { | |
24 class DictionaryValue; | |
25 } | |
26 | |
27 namespace ui { | |
28 class Accelerator; | |
29 } | |
30 | |
31 namespace extensions { | |
32 | |
33 // This service keeps track of preferences related to extension commands | |
34 // (assigning initial keybindings on install and removing them on deletion | |
35 // and answers questions related to which commands are active. | |
36 class ExtensionCommandService : public ProfileKeyedService, | |
37 public content::NotificationObserver { | |
38 public: | |
39 // An enum specifying whether to fetch all extension commands or only active | |
40 // ones. | |
41 enum QueryType { | |
42 ALL, | |
43 ACTIVE_ONLY, | |
44 }; | |
45 | |
46 // Register prefs for keybinding. | |
47 static void RegisterUserPrefs(PrefService* user_prefs); | |
48 | |
49 // Constructs an ExtensionCommandService object for the given profile. | |
50 explicit ExtensionCommandService(Profile* profile); | |
51 virtual ~ExtensionCommandService(); | |
52 | |
53 // Gets the keybinding (if any) for the browser action of an extension given | |
54 // its |extension_id|. The function consults the master list to see if | |
55 // the keybinding is active. Returns NULL if the extension has no browser | |
56 // action. Returns NULL if the keybinding is not active and |type| requested | |
57 // is ACTIVE_ONLY. | |
58 const extensions::Command* GetBrowserActionCommand( | |
59 const std::string& extension_id, QueryType type); | |
60 | |
61 // Gets the keybinding (if any) for the page action of an extension given | |
62 // its |extension_id|. The function consults the master list to see if | |
63 // the keybinding is active. Returns NULL if the extension has no page | |
64 // action. Returns NULL if the keybinding is not active and |type| requested | |
65 // is ACTIVE_ONLY. | |
66 const extensions::Command* GetPageActionCommand( | |
67 const std::string& extension_id, QueryType type); | |
68 | |
69 // Gets the active keybinding (if any) for the named commands of an extension | |
70 // given its |extension_id|. The function consults the master list to see if | |
71 // the keybinding is active. Returns an empty map if the extension has no | |
72 // named commands or no active named commands when |type| requested is | |
73 // ACTIVE_ONLY. | |
74 extensions::CommandMap GetNamedCommands( | |
75 const std::string& extension_id, QueryType type); | |
76 | |
77 // Checks to see if a keybinding |accelerator| for a given |command_name| in | |
78 // an extension with id |extension_id| is registered as active (by consulting | |
79 // the master list in |user_prefs|). | |
80 bool IsKeybindingActive(const ui::Accelerator& accelerator, | |
81 const std::string& extension_id, | |
82 const std::string& command_name) const; | |
83 | |
84 // Records a keybinding |accelerator| as active for an extension with id | |
85 // |extension_id| and command with the name |command_name|. If | |
86 // |allow_overrides| is false, the keybinding must be free for the change to | |
87 // be recorded (as determined by the master list in |user_prefs|). If | |
88 // |allow_overwrites| is true, any previously recorded keybinding for this | |
89 // |accelerator| will be overwritten. Returns true if the change was | |
90 // successfully recorded. | |
91 bool AddKeybindingPref(const ui::Accelerator& accelerator, | |
92 std::string extension_id, | |
93 std::string command_name, | |
94 bool allow_overrides); | |
95 | |
96 // Overridden from content::NotificationObserver. | |
97 virtual void Observe(int type, | |
98 const content::NotificationSource& source, | |
99 const content::NotificationDetails& details) OVERRIDE; | |
100 | |
101 private: | |
102 // Assigns initial keybinding for a given |extension|'s page action, browser | |
103 // action and named commands. In each case, if the suggested keybinding is | |
104 // free, it will be taken by this extension. If not, that keybinding request | |
105 // is ignored. |user_pref| is the PrefService used to record the new | |
106 // keybinding assignment. | |
107 void AssignInitialKeybindings(const extensions::Extension* extension); | |
108 | |
109 // Removes all keybindings for a given extension by its |extension_id|. | |
110 void RemoveKeybindingPrefs(std::string extension_id); | |
111 | |
112 // The content notification registrar for listening to extension events. | |
113 content::NotificationRegistrar registrar_; | |
114 | |
115 // A weak pointer to the profile we are associated with. Not owned by us. | |
116 Profile* profile_; | |
117 | |
118 DISALLOW_COPY_AND_ASSIGN(ExtensionCommandService); | |
119 }; | |
120 | |
121 } // namespace extensions | |
122 | |
123 #endif // CHROME_BROWSER_EXTENSIONS_API_COMMANDS_EXTENSION_COMMAND_SERVICE_H_ | |
OLD | NEW |