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 #include "chrome/browser/extensions/extension_keybinding_registry.h" | 5 #include "chrome/browser/extensions/extension_keybinding_registry.h" |
6 | 6 |
7 #include "chrome/browser/extensions/extension_service.h" | |
8 #include "chrome/browser/extensions/extension_system.h" | |
7 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
8 #include "chrome/common/chrome_notification_types.h" | 10 #include "chrome/common/chrome_notification_types.h" |
9 #include "chrome/common/extensions/extension_manifest_constants.h" | 11 #include "chrome/common/extensions/extension_manifest_constants.h" |
10 #include "chrome/common/extensions/extension_set.h" | 12 #include "chrome/common/extensions/extension_set.h" |
11 #include "chrome/browser/extensions/extension_service.h" | |
12 | 13 |
13 namespace extensions { | 14 namespace extensions { |
14 | 15 |
15 ExtensionKeybindingRegistry::ExtensionKeybindingRegistry(Profile* profile) | 16 ExtensionKeybindingRegistry::ExtensionKeybindingRegistry(Profile* profile) |
16 : profile_(profile) { | 17 : profile_(profile) { |
17 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 18 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
18 content::Source<Profile>(profile->GetOriginalProfile())); | 19 content::Source<Profile>(profile->GetOriginalProfile())); |
19 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 20 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
20 content::Source<Profile>(profile->GetOriginalProfile())); | 21 content::Source<Profile>(profile->GetOriginalProfile())); |
22 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED, | |
23 content::Source<Profile>(profile->GetOriginalProfile())); | |
24 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED, | |
25 content::Source<Profile>(profile->GetOriginalProfile())); | |
21 } | 26 } |
22 | 27 |
23 ExtensionKeybindingRegistry::~ExtensionKeybindingRegistry() { | 28 ExtensionKeybindingRegistry::~ExtensionKeybindingRegistry() { |
24 } | 29 } |
25 | 30 |
26 void ExtensionKeybindingRegistry::Init() { | 31 void ExtensionKeybindingRegistry::Init() { |
27 ExtensionService* service = profile_->GetExtensionService(); | 32 ExtensionService* service = profile_->GetExtensionService(); |
28 if (!service) | 33 if (!service) |
29 return; // ExtensionService can be null during testing. | 34 return; // ExtensionService can be null during testing. |
30 | 35 |
31 const ExtensionSet* extensions = service->extensions(); | 36 const ExtensionSet* extensions = service->extensions(); |
32 ExtensionSet::const_iterator iter = extensions->begin(); | 37 ExtensionSet::const_iterator iter = extensions->begin(); |
33 for (; iter != extensions->end(); ++iter) | 38 for (; iter != extensions->end(); ++iter) |
34 AddExtensionKeybinding(*iter); | 39 AddExtensionKeybinding(*iter, std::string()); |
35 } | 40 } |
36 | 41 |
37 bool ExtensionKeybindingRegistry::ShouldIgnoreCommand( | 42 bool ExtensionKeybindingRegistry::ShouldIgnoreCommand( |
38 const std::string& command) const { | 43 const std::string& command) const { |
39 return command == extension_manifest_values::kPageActionKeybindingEvent || | 44 return command == extension_manifest_values::kPageActionKeybindingEvent || |
40 command == extension_manifest_values::kBrowserActionKeybindingEvent; | 45 command == extension_manifest_values::kBrowserActionKeybindingEvent; |
41 } | 46 } |
42 | 47 |
43 void ExtensionKeybindingRegistry::Observe( | 48 void ExtensionKeybindingRegistry::Observe( |
44 int type, | 49 int type, |
45 const content::NotificationSource& source, | 50 const content::NotificationSource& source, |
46 const content::NotificationDetails& details) { | 51 const content::NotificationDetails& details) { |
47 switch (type) { | 52 switch (type) { |
48 case chrome::NOTIFICATION_EXTENSION_LOADED: | 53 case chrome::NOTIFICATION_EXTENSION_LOADED: |
49 AddExtensionKeybinding( | 54 AddExtensionKeybinding( |
50 content::Details<const extensions::Extension>(details).ptr()); | 55 content::Details<const extensions::Extension>(details).ptr(), |
56 std::string()); | |
51 break; | 57 break; |
52 case chrome::NOTIFICATION_EXTENSION_UNLOADED: | 58 case chrome::NOTIFICATION_EXTENSION_UNLOADED: |
53 RemoveExtensionKeybinding( | 59 RemoveExtensionKeybinding( |
54 content::Details<UnloadedExtensionInfo>(details)->extension); | 60 content::Details<UnloadedExtensionInfo>(details)->extension, |
61 std::string()); | |
55 break; | 62 break; |
63 case chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED: | |
64 case chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED: { | |
65 std::pair<const std::string, const std::string>* payload = | |
66 content::Details<std::pair<const std::string, const std::string> >( | |
67 details).ptr(); | |
68 | |
69 const extensions::Extension* extension = | |
70 ExtensionSystem::Get(profile_)->extension_service()-> | |
71 extensions()->GetByID(payload->first); | |
72 // During install and uninstall the extension won't be found. We'll catch | |
73 // those events above, with the LOADED/UNLOADED, so we ignore this event. | |
74 if (!extension) | |
75 return; | |
Yoyo Zhou
2012/06/28 22:40:51
ShouldIgnoreCommand could go here instead of in th
Finnur
2012/06/28 22:49:01
No, this is actually deliberate. The Views impleme
| |
76 | |
77 if (type == chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED) | |
78 AddExtensionKeybinding(extension, payload->second); | |
79 else | |
80 RemoveExtensionKeybinding(extension, payload->second); | |
81 break; | |
82 } | |
56 default: | 83 default: |
57 NOTREACHED(); | 84 NOTREACHED(); |
58 break; | 85 break; |
59 } | 86 } |
60 } | 87 } |
61 | 88 |
62 } // namespace extensions | 89 } // namespace extensions |
OLD | NEW |