This source file includes following definitions.
- RegisterAccelerator
- UnregisterAccelerator
- UnregisterAccelerators
- SetShortcutHandlingSuspended
- IsShortcutHandlingSuspended
- NotifyKeyPressed
#include "chrome/browser/extensions/global_shortcut_listener.h"
#include "base/logging.h"
#include "content/public/browser/browser_thread.h"
#include "ui/base/accelerators/accelerator.h"
using content::BrowserThread;
namespace extensions {
GlobalShortcutListener::GlobalShortcutListener()
: shortcut_handling_suspended_(false) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}
GlobalShortcutListener::~GlobalShortcutListener() {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(accelerator_map_.empty());
}
bool GlobalShortcutListener::RegisterAccelerator(
const ui::Accelerator& accelerator, Observer* observer) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (IsShortcutHandlingSuspended())
return false;
AcceleratorMap::const_iterator it = accelerator_map_.find(accelerator);
if (it != accelerator_map_.end()) {
return false;
}
if (!RegisterAcceleratorImpl(accelerator)) {
return false;
}
if (accelerator_map_.empty())
StartListening();
accelerator_map_[accelerator] = observer;
return true;
}
void GlobalShortcutListener::UnregisterAccelerator(
const ui::Accelerator& accelerator, Observer* observer) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (IsShortcutHandlingSuspended())
return;
AcceleratorMap::iterator it = accelerator_map_.find(accelerator);
DCHECK(it != accelerator_map_.end());
DCHECK(it->second == observer);
UnregisterAcceleratorImpl(accelerator);
accelerator_map_.erase(it);
if (accelerator_map_.empty())
StopListening();
}
void GlobalShortcutListener::UnregisterAccelerators(Observer* observer) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (IsShortcutHandlingSuspended())
return;
AcceleratorMap::iterator it = accelerator_map_.begin();
while (it != accelerator_map_.end()) {
if (it->second == observer) {
AcceleratorMap::iterator to_remove = it++;
UnregisterAccelerator(to_remove->first, observer);
} else {
++it;
}
}
}
void GlobalShortcutListener::SetShortcutHandlingSuspended(bool suspended) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (shortcut_handling_suspended_ == suspended)
return;
shortcut_handling_suspended_ = suspended;
for (AcceleratorMap::iterator it = accelerator_map_.begin();
it != accelerator_map_.end();
++it) {
if (shortcut_handling_suspended_)
UnregisterAcceleratorImpl(it->first);
else
RegisterAcceleratorImpl(it->first);
}
}
bool GlobalShortcutListener::IsShortcutHandlingSuspended() const {
return shortcut_handling_suspended_;
}
void GlobalShortcutListener::NotifyKeyPressed(
const ui::Accelerator& accelerator) {
AcceleratorMap::iterator iter = accelerator_map_.find(accelerator);
if (iter == accelerator_map_.end()) {
NOTREACHED();
return;
}
iter->second->OnKeyPressed(accelerator);
}
}