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
|
// Copyright (c) 2010 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_tabs_module.h"
#include "base/json/json_writer.h"
#include "base/stl_util-inl.h"
#include "base/string_util.h"
#include "base/values.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/browser_window.h"
#include "chrome/browser/extensions/extension_accessibility_api.h"
#include "chrome/browser/extensions/extension_accessibility_api_constants.h"
#include "chrome/browser/extensions/extension_event_router.h"
#include "chrome/browser/extensions/extension_function_dispatcher.h"
#include "chrome/browser/extensions/extensions_service.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/notification_service.h"
namespace keys = extension_accessibility_api_constants;
// Returns the AccessibilityControlInfo serialized into a JSON string,
// consisting of an array of a single object of type AccessibilityObject,
// as defined in the accessibility extension api's json schema.
std::string ControlInfoToJsonString(const AccessibilityControlInfo* info) {
ListValue args;
DictionaryValue* dict = new DictionaryValue();
info->SerializeToDict(dict);
args.Append(dict);
std::string json_args;
base::JSONWriter::Write(&args, false, &json_args);
return json_args;
}
ExtensionAccessibilityEventRouter*
ExtensionAccessibilityEventRouter::GetInstance() {
return Singleton<ExtensionAccessibilityEventRouter>::get();
}
ExtensionAccessibilityEventRouter::ExtensionAccessibilityEventRouter()
: enabled_(false) {}
ExtensionAccessibilityEventRouter::~ExtensionAccessibilityEventRouter() {
STLDeleteElements(&on_enabled_listeners_);
STLDeleteElements(&on_disabled_listeners_);
}
void ExtensionAccessibilityEventRouter::ObserveProfile(Profile* profile) {
last_focused_control_dict_.Clear();
if (registrar_.IsEmpty()) {
registrar_.Add(this,
NotificationType::ACCESSIBILITY_WINDOW_OPENED,
NotificationService::AllSources());
registrar_.Add(this,
NotificationType::ACCESSIBILITY_WINDOW_CLOSED,
NotificationService::AllSources());
registrar_.Add(this,
NotificationType::ACCESSIBILITY_CONTROL_FOCUSED,
NotificationService::AllSources());
registrar_.Add(this,
NotificationType::ACCESSIBILITY_CONTROL_ACTION,
NotificationService::AllSources());
registrar_.Add(this,
NotificationType::ACCESSIBILITY_TEXT_CHANGED,
NotificationService::AllSources());
registrar_.Add(this,
NotificationType::ACCESSIBILITY_MENU_OPENED,
NotificationService::AllSources());
registrar_.Add(this,
NotificationType::ACCESSIBILITY_MENU_CLOSED,
NotificationService::AllSources());
}
}
void ExtensionAccessibilityEventRouter::Observe(
NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
switch (type.value) {
case NotificationType::ACCESSIBILITY_WINDOW_OPENED:
OnWindowOpened(Details<const AccessibilityWindowInfo>(details).ptr());
break;
case NotificationType::ACCESSIBILITY_WINDOW_CLOSED:
OnWindowClosed(Details<const AccessibilityWindowInfo>(details).ptr());
break;
case NotificationType::ACCESSIBILITY_CONTROL_FOCUSED:
OnControlFocused(Details<const AccessibilityControlInfo>(details).ptr());
break;
case NotificationType::ACCESSIBILITY_CONTROL_ACTION:
OnControlAction(Details<const AccessibilityControlInfo>(details).ptr());
break;
case NotificationType::ACCESSIBILITY_TEXT_CHANGED:
OnTextChanged(Details<const AccessibilityControlInfo>(details).ptr());
break;
case NotificationType::ACCESSIBILITY_MENU_OPENED:
OnMenuOpened(Details<const AccessibilityMenuInfo>(details).ptr());
break;
case NotificationType::ACCESSIBILITY_MENU_CLOSED:
OnMenuClosed(Details<const AccessibilityMenuInfo>(details).ptr());
break;
default:
NOTREACHED();
}
}
void ExtensionAccessibilityEventRouter::SetAccessibilityEnabled(bool enabled) {
if (enabled_ != enabled) {
enabled_ = enabled;
if (enabled_) {
for (unsigned int i = 0; i < on_enabled_listeners_.size(); i++) {
on_enabled_listeners_[i]->Run();
}
} else {
for (unsigned int i = 0; i < on_disabled_listeners_.size(); i++) {
on_disabled_listeners_[i]->Run();
}
}
}
}
bool ExtensionAccessibilityEventRouter::IsAccessibilityEnabled() const {
return enabled_;
}
void ExtensionAccessibilityEventRouter::AddOnEnabledListener(
Callback* callback) {
on_enabled_listeners_.push_back(callback);
}
void ExtensionAccessibilityEventRouter::AddOnDisabledListener(
Callback* callback) {
on_disabled_listeners_.push_back(callback);
}
void ExtensionAccessibilityEventRouter::OnWindowOpened(
const AccessibilityWindowInfo* info) {
std::string json_args = ControlInfoToJsonString(info);
DispatchEvent(info->profile(), keys::kOnWindowOpened, json_args);
}
void ExtensionAccessibilityEventRouter::OnWindowClosed(
const AccessibilityWindowInfo* info) {
std::string json_args = ControlInfoToJsonString(info);
DispatchEvent(info->profile(), keys::kOnWindowClosed, json_args);
}
void ExtensionAccessibilityEventRouter::OnControlFocused(
const AccessibilityControlInfo* info) {
last_focused_control_dict_.Clear();
info->SerializeToDict(&last_focused_control_dict_);
std::string json_args = ControlInfoToJsonString(info);
DispatchEvent(info->profile(), keys::kOnControlFocused, json_args);
}
void ExtensionAccessibilityEventRouter::OnControlAction(
const AccessibilityControlInfo* info) {
std::string json_args = ControlInfoToJsonString(info);
DispatchEvent(info->profile(), keys::kOnControlAction, json_args);
}
void ExtensionAccessibilityEventRouter::OnTextChanged(
const AccessibilityControlInfo* info) {
std::string json_args = ControlInfoToJsonString(info);
DispatchEvent(info->profile(), keys::kOnTextChanged, json_args);
}
void ExtensionAccessibilityEventRouter::OnMenuOpened(
const AccessibilityMenuInfo* info) {
std::string json_args = ControlInfoToJsonString(info);
DispatchEvent(info->profile(), keys::kOnMenuOpened, json_args);
}
void ExtensionAccessibilityEventRouter::OnMenuClosed(
const AccessibilityMenuInfo* info) {
std::string json_args = ControlInfoToJsonString(info);
DispatchEvent(info->profile(), keys::kOnMenuClosed, json_args);
}
void ExtensionAccessibilityEventRouter::DispatchEvent(
Profile* profile,
const char* event_name,
const std::string& json_args) {
if (enabled_ && profile && profile->GetExtensionEventRouter()) {
profile->GetExtensionEventRouter()->DispatchEventToRenderers(
event_name, json_args, NULL, GURL());
}
}
bool SetAccessibilityEnabledFunction::RunImpl() {
bool enabled;
EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(0, &enabled));
ExtensionAccessibilityEventRouter::GetInstance()
->SetAccessibilityEnabled(enabled);
return true;
}
bool GetFocusedControlFunction::RunImpl() {
// Get the serialized dict from the last focused control and return it.
// However, if the dict is empty, that means we haven't seen any focus
// events yet, so return null instead.
ExtensionAccessibilityEventRouter *accessibility_event_router =
ExtensionAccessibilityEventRouter::GetInstance();
DictionaryValue *last_focused_control_dict =
accessibility_event_router->last_focused_control_dict();
if (last_focused_control_dict->size()) {
result_.reset(last_focused_control_dict->DeepCopyWithoutEmptyChildren());
} else {
result_.reset(Value::CreateNullValue());
}
return true;
}
|