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/lock/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 ~DefaultEventDelegate() override
;
33 void BroadcastEvent(scoped_ptr
<Event
> event
) override
;
34 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::OnBrailleDisplayStateChanged(
64 const DisplayState
& display_state
) {
65 scoped_ptr
<Event
> event(
66 new Event(events::BRAILLE_DISPLAY_PRIVATE_ON_DISPLAY_STATE_CHANGED
,
67 OnDisplayStateChanged::kEventName
,
68 OnDisplayStateChanged::Create(display_state
)));
69 event_delegate_
->BroadcastEvent(event
.Pass());
72 void BrailleDisplayPrivateAPI::OnBrailleKeyEvent(const KeyEvent
& key_event
) {
73 // Key events only go to extensions of the active profile.
74 if (!IsProfileActive())
76 scoped_ptr
<Event
> event(
77 new Event(events::BRAILLE_DISPLAY_PRIVATE_ON_KEY_EVENT
,
78 OnKeyEvent::kEventName
, OnKeyEvent::Create(key_event
)));
79 event_delegate_
->BroadcastEvent(event
.Pass());
82 bool BrailleDisplayPrivateAPI::IsProfileActive() {
83 #if defined(OS_CHROMEOS)
84 Profile
* active_profile
;
85 chromeos::ScreenLocker
* screen_locker
=
86 chromeos::ScreenLocker::default_screen_locker();
87 if (screen_locker
&& screen_locker
->locked()) {
88 active_profile
= chromeos::ProfileHelper::GetSigninProfile();
90 // Since we are creating one instance per profile / user, we should be fine
91 // comparing against the active user. That said - if we ever change that,
92 // this code will need to be changed.
93 active_profile
= ProfileManager::GetActiveUserProfile();
95 return profile_
->IsSameProfile(active_profile
);
96 #else // !defined(OS_CHROMEOS)
101 void BrailleDisplayPrivateAPI::SetEventDelegateForTest(
102 scoped_ptr
<EventDelegate
> delegate
) {
103 event_delegate_
= delegate
.Pass();
106 void BrailleDisplayPrivateAPI::OnListenerAdded(
107 const EventListenerInfo
& details
) {
108 BrailleController
* braille_controller
= BrailleController::GetInstance();
109 if (!scoped_observer_
.IsObserving(braille_controller
))
110 scoped_observer_
.Add(braille_controller
);
113 void BrailleDisplayPrivateAPI::OnListenerRemoved(
114 const EventListenerInfo
& details
) {
115 BrailleController
* braille_controller
= BrailleController::GetInstance();
116 if (!event_delegate_
->HasListener() &&
117 scoped_observer_
.IsObserving(braille_controller
)) {
118 scoped_observer_
.Remove(braille_controller
);
122 BrailleDisplayPrivateAPI::DefaultEventDelegate::DefaultEventDelegate(
123 EventRouter::Observer
* observer
, Profile
* profile
)
124 : observer_(observer
), profile_(profile
) {
125 EventRouter
* event_router
= EventRouter::Get(profile_
);
126 event_router
->RegisterObserver(observer_
, OnDisplayStateChanged::kEventName
);
127 event_router
->RegisterObserver(observer_
, OnKeyEvent::kEventName
);
130 BrailleDisplayPrivateAPI::DefaultEventDelegate::~DefaultEventDelegate() {
131 EventRouter::Get(profile_
)->UnregisterObserver(observer_
);
134 void BrailleDisplayPrivateAPI::DefaultEventDelegate::BroadcastEvent(
135 scoped_ptr
<Event
> event
) {
136 EventRouter::Get(profile_
)->BroadcastEvent(event
.Pass());
139 bool BrailleDisplayPrivateAPI::DefaultEventDelegate::HasListener() {
140 EventRouter
* event_router
= EventRouter::Get(profile_
);
141 return (event_router
->HasEventListener(OnDisplayStateChanged::kEventName
) ||
142 event_router
->HasEventListener(OnKeyEvent::kEventName
));
146 bool BrailleDisplayPrivateGetDisplayStateFunction::Prepare() {
150 void BrailleDisplayPrivateGetDisplayStateFunction::Work() {
152 BrailleController::GetInstance()->GetDisplayState()->ToValue().release());
155 bool BrailleDisplayPrivateGetDisplayStateFunction::Respond() {
159 BrailleDisplayPrivateWriteDotsFunction::
160 BrailleDisplayPrivateWriteDotsFunction() {
163 BrailleDisplayPrivateWriteDotsFunction::
164 ~BrailleDisplayPrivateWriteDotsFunction() {
167 bool BrailleDisplayPrivateWriteDotsFunction::Prepare() {
168 params_
= WriteDots::Params::Create(*args_
);
169 EXTENSION_FUNCTION_VALIDATE(params_
);
173 void BrailleDisplayPrivateWriteDotsFunction::Work() {
174 BrailleController::GetInstance()->WriteDots(params_
->cells
);
177 bool BrailleDisplayPrivateWriteDotsFunction::Respond() {
181 } // namespace extensions