OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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. |
| 4 |
| 5 #include "chrome/browser/extensions/extension_tabs_module.h" |
| 6 |
| 7 #include "base/json/json_writer.h" |
| 8 #include "base/string_util.h" |
| 9 #include "base/values.h" |
| 10 #include "chrome/browser/browser.h" |
| 11 #include "chrome/browser/browser_list.h" |
| 12 #include "chrome/browser/browser_window.h" |
| 13 #include "chrome/browser/extensions/extension_accessibility_api.h" |
| 14 #include "chrome/browser/extensions/extension_accessibility_api_constants.h" |
| 15 #include "chrome/browser/extensions/extension_function_dispatcher.h" |
| 16 #include "chrome/browser/extensions/extension_message_service.h" |
| 17 #include "chrome/browser/extensions/extensions_service.h" |
| 18 #include "chrome/browser/profile.h" |
| 19 #include "chrome/common/extensions/extension.h" |
| 20 |
| 21 namespace keys = extension_accessibility_api_constants; |
| 22 |
| 23 // Returns the AccessibilityControlInfo serialized into a JSON string, |
| 24 // consisting of an array of a single object of type AccessibilityObject, |
| 25 // as defined in the accessibility extension api's json schema. |
| 26 std::string ControlInfoToJsonString(const AccessibilityControlInfo* info) { |
| 27 ListValue args; |
| 28 DictionaryValue* dict = new DictionaryValue(); |
| 29 info->SerializeToDict(dict); |
| 30 args.Append(dict); |
| 31 std::string json_args; |
| 32 base::JSONWriter::Write(&args, false, &json_args); |
| 33 return json_args; |
| 34 } |
| 35 |
| 36 ExtensionAccessibilityEventRouter* |
| 37 ExtensionAccessibilityEventRouter::GetInstance() { |
| 38 return Singleton<ExtensionAccessibilityEventRouter>::get(); |
| 39 } |
| 40 |
| 41 void ExtensionAccessibilityEventRouter::ObserveProfile(Profile* profile) { |
| 42 last_focused_control_dict_.Clear(); |
| 43 |
| 44 if (registrar_.IsEmpty()) { |
| 45 registrar_.Add(this, |
| 46 NotificationType::ACCESSIBILITY_WINDOW_OPENED, |
| 47 NotificationService::AllSources()); |
| 48 registrar_.Add(this, |
| 49 NotificationType::ACCESSIBILITY_WINDOW_CLOSED, |
| 50 NotificationService::AllSources()); |
| 51 registrar_.Add(this, |
| 52 NotificationType::ACCESSIBILITY_CONTROL_FOCUSED, |
| 53 NotificationService::AllSources()); |
| 54 registrar_.Add(this, |
| 55 NotificationType::ACCESSIBILITY_CONTROL_ACTION, |
| 56 NotificationService::AllSources()); |
| 57 registrar_.Add(this, |
| 58 NotificationType::ACCESSIBILITY_TEXT_CHANGED, |
| 59 NotificationService::AllSources()); |
| 60 } |
| 61 } |
| 62 |
| 63 void ExtensionAccessibilityEventRouter::Observe( |
| 64 NotificationType type, |
| 65 const NotificationSource& source, |
| 66 const NotificationDetails& details) { |
| 67 switch (type.value) { |
| 68 case NotificationType::ACCESSIBILITY_WINDOW_OPENED: |
| 69 OnWindowOpened(Details<const AccessibilityWindowInfo>(details).ptr()); |
| 70 break; |
| 71 case NotificationType::ACCESSIBILITY_WINDOW_CLOSED: |
| 72 OnWindowClosed(Details<const AccessibilityWindowInfo>(details).ptr()); |
| 73 break; |
| 74 case NotificationType::ACCESSIBILITY_CONTROL_FOCUSED: |
| 75 OnControlFocused(Details<const AccessibilityControlInfo>(details).ptr()); |
| 76 break; |
| 77 case NotificationType::ACCESSIBILITY_CONTROL_ACTION: |
| 78 OnControlAction(Details<const AccessibilityControlInfo>(details).ptr()); |
| 79 break; |
| 80 case NotificationType::ACCESSIBILITY_TEXT_CHANGED: |
| 81 OnTextChanged(Details<const AccessibilityControlInfo>(details).ptr()); |
| 82 break; |
| 83 default: |
| 84 NOTREACHED(); |
| 85 } |
| 86 } |
| 87 |
| 88 void ExtensionAccessibilityEventRouter::SetAccessibilityEnabled(bool enabled) { |
| 89 if (enabled_ != enabled) { |
| 90 enabled_ = enabled; |
| 91 if (enabled_) { |
| 92 for (unsigned int i = 0; i < on_enabled_listeners_.size(); i++) { |
| 93 on_enabled_listeners_[i]->Run(); |
| 94 } |
| 95 } else { |
| 96 for (unsigned int i = 0; i < on_disabled_listeners_.size(); i++) { |
| 97 on_disabled_listeners_[i]->Run(); |
| 98 } |
| 99 } |
| 100 } |
| 101 } |
| 102 |
| 103 bool ExtensionAccessibilityEventRouter::IsAccessibilityEnabled() const { |
| 104 return enabled_; |
| 105 } |
| 106 |
| 107 void ExtensionAccessibilityEventRouter::AddOnEnabledListener( |
| 108 Callback* callback) { |
| 109 on_enabled_listeners_.push_back(callback); |
| 110 } |
| 111 |
| 112 void ExtensionAccessibilityEventRouter::AddOnDisabledListener( |
| 113 Callback* callback) { |
| 114 on_disabled_listeners_.push_back(callback); |
| 115 } |
| 116 |
| 117 void ExtensionAccessibilityEventRouter::OnWindowOpened( |
| 118 const AccessibilityWindowInfo* info) { |
| 119 std::string json_args = ControlInfoToJsonString(info); |
| 120 DispatchEvent(info->profile(), keys::kOnWindowOpened, json_args); |
| 121 } |
| 122 |
| 123 void ExtensionAccessibilityEventRouter::OnWindowClosed( |
| 124 const AccessibilityWindowInfo* info) { |
| 125 std::string json_args = ControlInfoToJsonString(info); |
| 126 DispatchEvent(info->profile(), keys::kOnWindowClosed, json_args); |
| 127 } |
| 128 |
| 129 void ExtensionAccessibilityEventRouter::OnControlFocused( |
| 130 const AccessibilityControlInfo* info) { |
| 131 last_focused_control_dict_.Clear(); |
| 132 info->SerializeToDict(&last_focused_control_dict_); |
| 133 std::string json_args = ControlInfoToJsonString(info); |
| 134 DispatchEvent(info->profile(), keys::kOnControlFocused, json_args); |
| 135 } |
| 136 |
| 137 void ExtensionAccessibilityEventRouter::OnControlAction( |
| 138 const AccessibilityControlInfo* info) { |
| 139 std::string json_args = ControlInfoToJsonString(info); |
| 140 DispatchEvent(info->profile(), keys::kOnControlAction, json_args); |
| 141 } |
| 142 |
| 143 void ExtensionAccessibilityEventRouter::OnTextChanged( |
| 144 const AccessibilityControlInfo* info) { |
| 145 std::string json_args = ControlInfoToJsonString(info); |
| 146 DispatchEvent(info->profile(), keys::kOnTextChanged, json_args); |
| 147 } |
| 148 |
| 149 void ExtensionAccessibilityEventRouter::DispatchEvent( |
| 150 Profile* profile, |
| 151 const char* event_name, |
| 152 const std::string& json_args) { |
| 153 if (enabled_ && profile && profile->GetExtensionMessageService()) { |
| 154 profile->GetExtensionMessageService()-> |
| 155 DispatchEventToRenderers(event_name, json_args); |
| 156 } |
| 157 } |
| 158 |
| 159 bool SetAccessibilityEnabledFunction::RunImpl() { |
| 160 bool enabled; |
| 161 EXTENSION_FUNCTION_VALIDATE(args_->GetAsBoolean(&enabled)); |
| 162 ExtensionAccessibilityEventRouter::GetInstance() |
| 163 ->SetAccessibilityEnabled(enabled); |
| 164 return true; |
| 165 } |
| 166 |
| 167 bool GetFocusedControlFunction::RunImpl() { |
| 168 // Get the serialized dict from the last focused control and return it. |
| 169 // However, if the dict is empty, that means we haven't seen any focus |
| 170 // events yet, so return null instead. |
| 171 ExtensionAccessibilityEventRouter *accessibility_event_router = |
| 172 ExtensionAccessibilityEventRouter::GetInstance(); |
| 173 DictionaryValue *last_focused_control_dict = |
| 174 accessibility_event_router->last_focused_control_dict(); |
| 175 if (last_focused_control_dict->size()) { |
| 176 result_.reset(last_focused_control_dict->DeepCopyWithoutEmptyChildren()); |
| 177 } else { |
| 178 result_.reset(Value::CreateNullValue()); |
| 179 } |
| 180 return true; |
| 181 } |
OLD | NEW |