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" | 7 #include "chrome/browser/extensions/extension_service.h" |
8 #include "chrome/browser/extensions/extension_system.h" | 8 #include "chrome/browser/extensions/extension_system.h" |
9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/common/chrome_notification_types.h" | 10 #include "chrome/common/chrome_notification_types.h" |
11 #include "chrome/common/extensions/extension_manifest_constants.h" | 11 #include "chrome/common/extensions/extension_manifest_constants.h" |
12 #include "chrome/common/extensions/extension_set.h" | 12 #include "chrome/common/extensions/extension_set.h" |
13 | 13 |
14 namespace extensions { | 14 namespace extensions { |
15 | 15 |
16 ExtensionKeybindingRegistry::ExtensionKeybindingRegistry(Profile* profile) | 16 ExtensionKeybindingRegistry::ExtensionKeybindingRegistry( |
17 : profile_(profile) { | 17 Profile* profile, ExtensionFilter extension_filter) |
| 18 : profile_(profile), extension_filter_(extension_filter) { |
18 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 19 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
19 content::Source<Profile>(profile->GetOriginalProfile())); | 20 content::Source<Profile>(profile->GetOriginalProfile())); |
20 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 21 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
21 content::Source<Profile>(profile->GetOriginalProfile())); | 22 content::Source<Profile>(profile->GetOriginalProfile())); |
22 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED, | 23 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED, |
23 content::Source<Profile>(profile->GetOriginalProfile())); | 24 content::Source<Profile>(profile->GetOriginalProfile())); |
24 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED, | 25 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED, |
25 content::Source<Profile>(profile->GetOriginalProfile())); | 26 content::Source<Profile>(profile->GetOriginalProfile())); |
26 } | 27 } |
27 | 28 |
28 ExtensionKeybindingRegistry::~ExtensionKeybindingRegistry() { | 29 ExtensionKeybindingRegistry::~ExtensionKeybindingRegistry() { |
29 } | 30 } |
30 | 31 |
31 void ExtensionKeybindingRegistry::Init() { | 32 void ExtensionKeybindingRegistry::Init() { |
32 ExtensionService* service = profile_->GetExtensionService(); | 33 ExtensionService* service = profile_->GetExtensionService(); |
33 if (!service) | 34 if (!service) |
34 return; // ExtensionService can be null during testing. | 35 return; // ExtensionService can be null during testing. |
35 | 36 |
36 const ExtensionSet* extensions = service->extensions(); | 37 const ExtensionSet* extensions = service->extensions(); |
37 ExtensionSet::const_iterator iter = extensions->begin(); | 38 ExtensionSet::const_iterator iter = extensions->begin(); |
38 for (; iter != extensions->end(); ++iter) | 39 for (; iter != extensions->end(); ++iter) |
39 AddExtensionKeybinding(*iter, std::string()); | 40 if (ExtensionMatchesFilter(*iter)) |
| 41 AddExtensionKeybinding(*iter, std::string()); |
40 } | 42 } |
41 | 43 |
42 bool ExtensionKeybindingRegistry::ShouldIgnoreCommand( | 44 bool ExtensionKeybindingRegistry::ShouldIgnoreCommand( |
43 const std::string& command) const { | 45 const std::string& command) const { |
44 return command == extension_manifest_values::kPageActionCommandEvent || | 46 return command == extension_manifest_values::kPageActionCommandEvent || |
45 command == extension_manifest_values::kBrowserActionCommandEvent || | 47 command == extension_manifest_values::kBrowserActionCommandEvent || |
46 command == extension_manifest_values::kScriptBadgeCommandEvent; | 48 command == extension_manifest_values::kScriptBadgeCommandEvent; |
47 } | 49 } |
48 | 50 |
49 void ExtensionKeybindingRegistry::Observe( | 51 void ExtensionKeybindingRegistry::Observe( |
50 int type, | 52 int type, |
51 const content::NotificationSource& source, | 53 const content::NotificationSource& source, |
52 const content::NotificationDetails& details) { | 54 const content::NotificationDetails& details) { |
53 switch (type) { | 55 switch (type) { |
54 case chrome::NOTIFICATION_EXTENSION_LOADED: | 56 case chrome::NOTIFICATION_EXTENSION_LOADED: { |
55 AddExtensionKeybinding( | 57 const extensions::Extension* extension = |
56 content::Details<const extensions::Extension>(details).ptr(), | 58 content::Details<const extensions::Extension>(details).ptr(); |
57 std::string()); | 59 if (ExtensionMatchesFilter(extension)) |
| 60 AddExtensionKeybinding(extension, std::string()); |
58 break; | 61 break; |
59 case chrome::NOTIFICATION_EXTENSION_UNLOADED: | 62 } |
60 RemoveExtensionKeybinding( | 63 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { |
61 content::Details<UnloadedExtensionInfo>(details)->extension, | 64 const extensions::Extension* extension = |
62 std::string()); | 65 content::Details<UnloadedExtensionInfo>(details)->extension; |
| 66 if (ExtensionMatchesFilter(extension)) |
| 67 RemoveExtensionKeybinding(extension, std::string()); |
63 break; | 68 break; |
| 69 } |
64 case chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED: | 70 case chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED: |
65 case chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED: { | 71 case chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED: { |
66 std::pair<const std::string, const std::string>* payload = | 72 std::pair<const std::string, const std::string>* payload = |
67 content::Details<std::pair<const std::string, const std::string> >( | 73 content::Details<std::pair<const std::string, const std::string> >( |
68 details).ptr(); | 74 details).ptr(); |
69 | 75 |
70 const extensions::Extension* extension = | 76 const extensions::Extension* extension = |
71 ExtensionSystem::Get(profile_)->extension_service()-> | 77 ExtensionSystem::Get(profile_)->extension_service()-> |
72 extensions()->GetByID(payload->first); | 78 extensions()->GetByID(payload->first); |
73 // During install and uninstall the extension won't be found. We'll catch | 79 // During install and uninstall the extension won't be found. We'll catch |
74 // those events above, with the LOADED/UNLOADED, so we ignore this event. | 80 // those events above, with the LOADED/UNLOADED, so we ignore this event. |
75 if (!extension) | 81 if (!extension) |
76 return; | 82 return; |
77 | 83 |
78 if (type == chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED) | 84 if (ExtensionMatchesFilter(extension)) { |
79 AddExtensionKeybinding(extension, payload->second); | 85 if (type == chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED) |
80 else | 86 AddExtensionKeybinding(extension, payload->second); |
81 RemoveExtensionKeybinding(extension, payload->second); | 87 else |
| 88 RemoveExtensionKeybinding(extension, payload->second); |
| 89 } |
82 break; | 90 break; |
83 } | 91 } |
84 default: | 92 default: |
85 NOTREACHED(); | 93 NOTREACHED(); |
86 break; | 94 break; |
87 } | 95 } |
88 } | 96 } |
89 | 97 |
| 98 bool ExtensionKeybindingRegistry::ExtensionMatchesFilter( |
| 99 const extensions::Extension* extension) |
| 100 { |
| 101 switch (extension_filter_) { |
| 102 case ALL_EXTENSIONS: |
| 103 return true; |
| 104 case PLATFORM_APP_ONLY: |
| 105 return extension->is_platform_app(); |
| 106 default: |
| 107 NOTREACHED(); |
| 108 } |
| 109 return false; |
| 110 } |
| 111 |
90 } // namespace extensions | 112 } // namespace extensions |
OLD | NEW |