1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/extension_keybinding_registry.h"
#include "base/values.h"
#include "chrome/browser/extensions/active_tab_permission_granter.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/command.h"
#include "content/public/browser/browser_context.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/notification_types.h"
#include "extensions/common/extension_set.h"
#include "extensions/common/manifest_constants.h"
namespace {
const char kOnCommandEventName[] = "commands.onCommand";
} // namespace
namespace extensions {
ExtensionKeybindingRegistry::ExtensionKeybindingRegistry(
content::BrowserContext* context,
ExtensionFilter extension_filter,
Delegate* delegate)
: browser_context_(context),
extension_filter_(extension_filter),
delegate_(delegate),
extension_registry_observer_(this) {
extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_));
Profile* profile = Profile::FromBrowserContext(browser_context_);
registrar_.Add(this,
extensions::NOTIFICATION_EXTENSION_COMMAND_ADDED,
content::Source<Profile>(profile->GetOriginalProfile()));
registrar_.Add(this,
extensions::NOTIFICATION_EXTENSION_COMMAND_REMOVED,
content::Source<Profile>(profile->GetOriginalProfile()));
}
ExtensionKeybindingRegistry::~ExtensionKeybindingRegistry() {
}
void ExtensionKeybindingRegistry::RemoveExtensionKeybinding(
const Extension* extension,
const std::string& command_name) {
EventTargets::iterator it = event_targets_.begin();
while (it != event_targets_.end()) {
TargetList& target_list = it->second;
TargetList::iterator target = target_list.begin();
while (target != target_list.end()) {
if (target->first == extension->id() &&
(command_name.empty() || command_name == target->second))
target = target_list.erase(target);
else
target++;
}
EventTargets::iterator old = it++;
if (target_list.empty()) {
// Let each platform-specific implementation get a chance to clean up.
RemoveExtensionKeybindingImpl(old->first, command_name);
event_targets_.erase(old);
// If a specific command_name was requested, it has now been deleted so no
// further work is required.
if (!command_name.empty())
break;
}
}
}
void ExtensionKeybindingRegistry::Init() {
ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context_);
if (!registry)
return; // ExtensionRegistry can be null during testing.
for (const scoped_refptr<const extensions::Extension>& extension :
registry->enabled_extensions())
if (ExtensionMatchesFilter(extension.get()))
AddExtensionKeybindings(extension.get(), std::string());
}
bool ExtensionKeybindingRegistry::ShouldIgnoreCommand(
const std::string& command) const {
return command == manifest_values::kPageActionCommandEvent ||
command == manifest_values::kBrowserActionCommandEvent;
}
bool ExtensionKeybindingRegistry::NotifyEventTargets(
const ui::Accelerator& accelerator) {
return ExecuteCommands(accelerator, std::string());
}
void ExtensionKeybindingRegistry::CommandExecuted(
const std::string& extension_id, const std::string& command) {
const Extension* extension = ExtensionRegistry::Get(browser_context_)
->enabled_extensions()
.GetByID(extension_id);
if (!extension)
return;
// Grant before sending the event so that the permission is granted before
// the extension acts on the command. NOTE: The Global Commands handler does
// not set the delegate as it deals only with named commands (not page/browser
// actions that are associated with the current page directly).
ActiveTabPermissionGranter* granter =
delegate_ ? delegate_->GetActiveTabPermissionGranter() : NULL;
if (granter)
granter->GrantIfRequested(extension);
scoped_ptr<base::ListValue> args(new base::ListValue());
args->Append(new base::StringValue(command));
scoped_ptr<Event> event(new Event(kOnCommandEventName, args.Pass()));
event->restrict_to_browser_context = browser_context_;
event->user_gesture = EventRouter::USER_GESTURE_ENABLED;
EventRouter::Get(browser_context_)
->DispatchEventToExtension(extension_id, event.Pass());
}
bool ExtensionKeybindingRegistry::IsAcceleratorRegistered(
const ui::Accelerator& accelerator) const {
return event_targets_.find(accelerator) != event_targets_.end();
}
void ExtensionKeybindingRegistry::AddEventTarget(
const ui::Accelerator& accelerator,
const std::string& extension_id,
const std::string& command_name) {
event_targets_[accelerator].push_back(
std::make_pair(extension_id, command_name));
// Shortcuts except media keys have only one target in the list. See comment
// about |event_targets_|.
if (!extensions::Command::IsMediaKey(accelerator))
DCHECK_EQ(1u, event_targets_[accelerator].size());
}
bool ExtensionKeybindingRegistry::GetFirstTarget(
const ui::Accelerator& accelerator,
std::string* extension_id,
std::string* command_name) const {
EventTargets::const_iterator targets = event_targets_.find(accelerator);
if (targets == event_targets_.end())
return false;
DCHECK(!targets->second.empty());
TargetList::const_iterator first_target = targets->second.begin();
*extension_id = first_target->first;
*command_name = first_target->second;
return true;
}
bool ExtensionKeybindingRegistry::IsEventTargetsEmpty() const {
return event_targets_.empty();
}
void ExtensionKeybindingRegistry::ExecuteCommand(
const std::string& extension_id,
const ui::Accelerator& accelerator) {
ExecuteCommands(accelerator, extension_id);
}
void ExtensionKeybindingRegistry::OnExtensionLoaded(
content::BrowserContext* browser_context,
const Extension* extension) {
if (ExtensionMatchesFilter(extension))
AddExtensionKeybindings(extension, std::string());
}
void ExtensionKeybindingRegistry::OnExtensionUnloaded(
content::BrowserContext* browser_context,
const Extension* extension,
UnloadedExtensionInfo::Reason reason) {
if (ExtensionMatchesFilter(extension))
RemoveExtensionKeybinding(extension, std::string());
}
void ExtensionKeybindingRegistry::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case extensions::NOTIFICATION_EXTENSION_COMMAND_ADDED:
case extensions::NOTIFICATION_EXTENSION_COMMAND_REMOVED: {
std::pair<const std::string, const std::string>* payload =
content::Details<std::pair<const std::string, const std::string> >(
details).ptr();
const Extension* extension = ExtensionRegistry::Get(browser_context_)
->enabled_extensions()
.GetByID(payload->first);
// During install and uninstall the extension won't be found. We'll catch
// those events above, with the LOADED/UNLOADED, so we ignore this event.
if (!extension)
return;
if (ExtensionMatchesFilter(extension)) {
if (type == extensions::NOTIFICATION_EXTENSION_COMMAND_ADDED)
AddExtensionKeybindings(extension, payload->second);
else
RemoveExtensionKeybinding(extension, payload->second);
}
break;
}
default:
NOTREACHED();
break;
}
}
bool ExtensionKeybindingRegistry::ExtensionMatchesFilter(
const extensions::Extension* extension)
{
switch (extension_filter_) {
case ALL_EXTENSIONS:
return true;
case PLATFORM_APPS_ONLY:
return extension->is_platform_app();
default:
NOTREACHED();
}
return false;
}
bool ExtensionKeybindingRegistry::ExecuteCommands(
const ui::Accelerator& accelerator,
const std::string& extension_id) {
EventTargets::iterator targets = event_targets_.find(accelerator);
if (targets == event_targets_.end() || targets->second.empty())
return false;
bool executed = false;
for (TargetList::const_iterator it = targets->second.begin();
it != targets->second.end(); it++) {
if (!extensions::EventRouter::Get(browser_context_)
->ExtensionHasEventListener(it->first, kOnCommandEventName))
continue;
if (extension_id.empty() || it->first == extension_id) {
CommandExecuted(it->first, it->second);
executed = true;
}
}
return executed;
}
} // namespace extensions
|