1 // Copyright 2013 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.
5 #include "chrome/browser/extensions/api/braille_display_private/braille_display_private_api.h"
7 #include "base/lazy_instance.h"
8 #include "chrome/browser/extensions/api/braille_display_private/braille_controller.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/profiles/profile_manager.h"
12 #if defined(OS_CHROMEOS)
13 #include "chrome/browser/chromeos/login/screen_locker.h"
14 #include "chrome/browser/chromeos/profiles/profile_helper.h"
17 namespace OnDisplayStateChanged =
18 extensions::api::braille_display_private::OnDisplayStateChanged;
19 namespace OnKeyEvent = extensions::api::braille_display_private::OnKeyEvent;
20 namespace WriteDots = extensions::api::braille_display_private::WriteDots;
21 using extensions::api::braille_display_private::DisplayState;
22 using extensions::api::braille_display_private::KeyEvent;
23 using extensions::api::braille_display_private::BrailleController;
25 namespace extensions {
27 class BrailleDisplayPrivateAPI::DefaultEventDelegate
28 : public BrailleDisplayPrivateAPI::EventDelegate {
30 DefaultEventDelegate(EventRouter::Observer* observer, Profile* profile);
31 virtual ~DefaultEventDelegate();
33 virtual void BroadcastEvent(scoped_ptr<Event> event) OVERRIDE;
34 virtual bool HasListener() OVERRIDE;
37 EventRouter::Observer* observer_;
41 BrailleDisplayPrivateAPI::BrailleDisplayPrivateAPI(
42 content::BrowserContext* context)
43 : profile_(Profile::FromBrowserContext(context)),
44 scoped_observer_(this),
45 event_delegate_(new DefaultEventDelegate(this, profile_)) {}
47 BrailleDisplayPrivateAPI::~BrailleDisplayPrivateAPI() {
50 void BrailleDisplayPrivateAPI::Shutdown() {
53 static base::LazyInstance<
54 BrowserContextKeyedAPIFactory<BrailleDisplayPrivateAPI> > g_factory =
55 LAZY_INSTANCE_INITIALIZER;
58 BrowserContextKeyedAPIFactory<BrailleDisplayPrivateAPI>*
59 BrailleDisplayPrivateAPI::GetFactoryInstance() {
60 return g_factory.Pointer();
63 void BrailleDisplayPrivateAPI::OnDisplayStateChanged(
64 const DisplayState& display_state) {
65 scoped_ptr<Event> event(new Event(
66 OnDisplayStateChanged::kEventName,
67 OnDisplayStateChanged::Create(display_state)));
68 event_delegate_->BroadcastEvent(event.Pass());
71 void BrailleDisplayPrivateAPI::OnKeyEvent(
72 const KeyEvent& key_event) {
73 // Key events only go to extensions of the active profile.
74 if (!IsProfileActive())
76 scoped_ptr<Event> event(new Event(
77 OnKeyEvent::kEventName, OnKeyEvent::Create(key_event)));
78 event_delegate_->BroadcastEvent(event.Pass());
81 bool BrailleDisplayPrivateAPI::IsProfileActive() {
82 #if defined(OS_CHROMEOS)
83 Profile* active_profile;
84 chromeos::ScreenLocker* screen_locker =
85 chromeos::ScreenLocker::default_screen_locker();
86 if (screen_locker && screen_locker->locked()) {
87 active_profile = chromeos::ProfileHelper::GetSigninProfile();
89 // Since we are creating one instance per profile / user, we should be fine
90 // comparing against the active user. That said - if we ever change that,
91 // this code will need to be changed.
92 active_profile = ProfileManager::GetActiveUserProfile();
94 return profile_->IsSameProfile(active_profile);
95 #else // !defined(OS_CHROMEOS)
100 void BrailleDisplayPrivateAPI::SetEventDelegateForTest(
101 scoped_ptr<EventDelegate> delegate) {
102 event_delegate_ = delegate.Pass();
105 void BrailleDisplayPrivateAPI::OnListenerAdded(
106 const EventListenerInfo& details) {
107 BrailleController* braille_controller = BrailleController::GetInstance();
108 if (!scoped_observer_.IsObserving(braille_controller))
109 scoped_observer_.Add(braille_controller);
112 void BrailleDisplayPrivateAPI::OnListenerRemoved(
113 const EventListenerInfo& details) {
114 BrailleController* braille_controller = BrailleController::GetInstance();
115 if (!event_delegate_->HasListener() &&
116 scoped_observer_.IsObserving(braille_controller)) {
117 scoped_observer_.Remove(braille_controller);
121 BrailleDisplayPrivateAPI::DefaultEventDelegate::DefaultEventDelegate(
122 EventRouter::Observer* observer, Profile* profile)
123 : observer_(observer), profile_(profile) {
124 EventRouter* event_router = EventRouter::Get(profile_);
125 event_router->RegisterObserver(observer_, OnDisplayStateChanged::kEventName);
126 event_router->RegisterObserver(observer_, OnKeyEvent::kEventName);
129 BrailleDisplayPrivateAPI::DefaultEventDelegate::~DefaultEventDelegate() {
130 EventRouter::Get(profile_)->UnregisterObserver(observer_);
133 void BrailleDisplayPrivateAPI::DefaultEventDelegate::BroadcastEvent(
134 scoped_ptr<Event> event) {
135 EventRouter::Get(profile_)->BroadcastEvent(event.Pass());
138 bool BrailleDisplayPrivateAPI::DefaultEventDelegate::HasListener() {
139 EventRouter* event_router = EventRouter::Get(profile_);
140 return (event_router->HasEventListener(OnDisplayStateChanged::kEventName) ||
141 event_router->HasEventListener(OnKeyEvent::kEventName));
145 bool BrailleDisplayPrivateGetDisplayStateFunction::Prepare() {
149 void BrailleDisplayPrivateGetDisplayStateFunction::Work() {
151 BrailleController::GetInstance()->GetDisplayState()->ToValue().release());
154 bool BrailleDisplayPrivateGetDisplayStateFunction::Respond() {
158 BrailleDisplayPrivateWriteDotsFunction::
159 BrailleDisplayPrivateWriteDotsFunction() {
162 BrailleDisplayPrivateWriteDotsFunction::
163 ~BrailleDisplayPrivateWriteDotsFunction() {
166 bool BrailleDisplayPrivateWriteDotsFunction::Prepare() {
167 params_ = WriteDots::Params::Create(*args_);
168 EXTENSION_FUNCTION_VALIDATE(params_);
172 void BrailleDisplayPrivateWriteDotsFunction::Work() {
173 BrailleController::GetInstance()->WriteDots(params_->cells);
176 bool BrailleDisplayPrivateWriteDotsFunction::Respond() {
180 } // namespace extensions