| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_GTK_ACCESSIBILITY_EVENT_ROUTER_GTK_H_ | 5 #ifndef CHROME_BROWSER_GTK_ACCESSIBILITY_EVENT_ROUTER_GTK_H_ |
| 6 #define CHROME_BROWSER_GTK_ACCESSIBILITY_EVENT_ROUTER_GTK_H_ | 6 #define CHROME_BROWSER_GTK_ACCESSIBILITY_EVENT_ROUTER_GTK_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <gtk/gtk.h> | 9 #include "chrome/browser/ui/gtk/accessibility_event_router_gtk.h" |
| 10 | 10 // TODO(msw): remove this file once all includes have been updated. |
| 11 #include <string> | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "base/basictypes.h" | |
| 15 #include "base/gtest_prod_util.h" | |
| 16 #include "base/hash_tables.h" | |
| 17 #include "base/singleton.h" | |
| 18 #include "base/task.h" | |
| 19 #include "chrome/browser/accessibility_events.h" | |
| 20 | |
| 21 class Profile; | |
| 22 #if defined (TOOLKIT_VIEWS) | |
| 23 namespace views { | |
| 24 class NativeTextfieldGtk; | |
| 25 } | |
| 26 #endif | |
| 27 | |
| 28 // Allows us to use (GtkWidget*) in a hash_map with gcc. | |
| 29 namespace __gnu_cxx { | |
| 30 template<> | |
| 31 struct hash<GtkWidget*> { | |
| 32 size_t operator()(GtkWidget* widget) const { | |
| 33 return reinterpret_cast<size_t>(widget); | |
| 34 } | |
| 35 }; | |
| 36 } // namespace __gnu_cxx | |
| 37 | |
| 38 // Struct to keep track of event listener hook ids to remove them later. | |
| 39 struct InstalledHook { | |
| 40 InstalledHook(guint _signal_id, gulong _hook_id) | |
| 41 : signal_id(_signal_id), hook_id(_hook_id) { } | |
| 42 guint signal_id; | |
| 43 gulong hook_id; | |
| 44 }; | |
| 45 | |
| 46 // NOTE: This class is part of the Accessibility Extension API, which lets | |
| 47 // extensions receive accessibility events. It's distinct from code that | |
| 48 // implements platform accessibility APIs like MSAA or ATK. | |
| 49 // | |
| 50 // Singleton class that adds a signal emission hook to many gtk events, and | |
| 51 // then sends an accessibility notification whenever a relevant event is | |
| 52 // sent to an accessible control. | |
| 53 // | |
| 54 // Gtk widgets are not accessible by default. When you register a root widget, | |
| 55 // that widget and all of its descendants will start sending accessibility | |
| 56 // event notifications. You can then override the default behavior for | |
| 57 // specific descendants using other methods. | |
| 58 // | |
| 59 // You can use Profile::PauseAccessibilityEvents to prevent a flurry | |
| 60 // of accessibility events when a window is being created or initialized. | |
| 61 class AccessibilityEventRouterGtk { | |
| 62 public: | |
| 63 // Internal information about a particular widget to override the | |
| 64 // information we get directly from gtk. | |
| 65 struct WidgetInfo { | |
| 66 WidgetInfo() : refcount(0) { } | |
| 67 | |
| 68 // The number of times that AddWidgetNameOverride has been called on this | |
| 69 // widget. When RemoveWidget has been called an equal number of | |
| 70 // times and the refcount reaches zero, this entry will be deleted. | |
| 71 int refcount; | |
| 72 | |
| 73 // If nonempty, will use this name instead of the widget's label. | |
| 74 std::string name; | |
| 75 }; | |
| 76 | |
| 77 // Internal information about a root widget | |
| 78 struct RootWidgetInfo { | |
| 79 RootWidgetInfo() : refcount(0), profile(NULL) { } | |
| 80 | |
| 81 // The number of times that AddRootWidget has been called on this | |
| 82 // widget. When RemoveRootWidget has been called an equal number of | |
| 83 // times and the refcount reaches zero, this entry will be deleted. | |
| 84 int refcount; | |
| 85 | |
| 86 // The profile associated with this root widget; accessibility | |
| 87 // notifications for any descendant of this root widget will get routed | |
| 88 // to this profile. | |
| 89 Profile* profile; | |
| 90 }; | |
| 91 | |
| 92 // Get the single instance of this class. | |
| 93 static AccessibilityEventRouterGtk* GetInstance(); | |
| 94 | |
| 95 // Start sending accessibility events for this widget and all of its | |
| 96 // descendants. Notifications will go to the specified profile. | |
| 97 // Uses reference counting, so it's safe to call this twice on the | |
| 98 // same widget, as long as each call is paired with a call to | |
| 99 // RemoveRootWidget. | |
| 100 void AddRootWidget(GtkWidget* root_widget, Profile* profile); | |
| 101 | |
| 102 // Stop sending accessibility events for this widget and all of its | |
| 103 // descendants. | |
| 104 void RemoveRootWidget(GtkWidget* root_widget); | |
| 105 | |
| 106 // Use the following string as the name of this widget, instead of the | |
| 107 // gtk label associated with the widget. Must be paired with a call to | |
| 108 // RemoveWidgetNameOverride. | |
| 109 void AddWidgetNameOverride(GtkWidget* widget, std::string name); | |
| 110 | |
| 111 // Forget widget name override. Must be paired with a call to | |
| 112 // AddWidgetNameOverride (uses reference counting). | |
| 113 void RemoveWidgetNameOverride(GtkWidget* widget); | |
| 114 | |
| 115 // | |
| 116 // The following methods are only for use by gtk signal handlers. | |
| 117 // | |
| 118 | |
| 119 // Called by the signal handler. Checks the type of the widget and | |
| 120 // calls one of the more specific Send*Notification methods, below. | |
| 121 void DispatchAccessibilityNotification( | |
| 122 GtkWidget* widget, NotificationType type); | |
| 123 | |
| 124 // Post a task to call DispatchAccessibilityNotification the next time | |
| 125 // through the event loop. | |
| 126 void PostDispatchAccessibilityNotification( | |
| 127 GtkWidget* widget, NotificationType type); | |
| 128 | |
| 129 private: | |
| 130 AccessibilityEventRouterGtk(); | |
| 131 virtual ~AccessibilityEventRouterGtk(); | |
| 132 | |
| 133 // Given a widget, determine if it's the descendant of a root widget | |
| 134 // that's mapped to a profile and if so, if it's marked as accessible. | |
| 135 void FindWidget(GtkWidget* widget, Profile** profile, bool* is_accessible); | |
| 136 | |
| 137 // Return the name of a widget. | |
| 138 std::string GetWidgetName(GtkWidget* widget); | |
| 139 | |
| 140 // Each of these methods constructs an AccessibilityControlInfo object | |
| 141 // and sends a notification of a specific accessibility event. | |
| 142 void SendButtonNotification( | |
| 143 GtkWidget* widget, NotificationType type, Profile* profile); | |
| 144 void SendCheckboxNotification( | |
| 145 GtkWidget* widget, NotificationType type, Profile* profile); | |
| 146 void SendComboBoxNotification( | |
| 147 GtkWidget* widget, NotificationType type, Profile* profile); | |
| 148 void SendListBoxNotification( | |
| 149 GtkWidget* widget, NotificationType type, Profile* profile); | |
| 150 void SendMenuItemNotification( | |
| 151 GtkWidget* widget, NotificationType type, Profile* profile); | |
| 152 void SendRadioButtonNotification( | |
| 153 GtkWidget* widget, NotificationType type, Profile* profile); | |
| 154 void SendTabNotification( | |
| 155 GtkWidget* widget, NotificationType type, Profile* profile); | |
| 156 void SendEntryNotification( | |
| 157 GtkWidget* widget, NotificationType type, Profile* profile); | |
| 158 void SendTextViewNotification( | |
| 159 GtkWidget* widget, NotificationType type, Profile* profile); | |
| 160 | |
| 161 bool IsPassword(GtkWidget* widget); | |
| 162 void InstallEventListeners(); | |
| 163 void RemoveEventListeners(); | |
| 164 | |
| 165 // Start and stop listening to signals. | |
| 166 void StartListening(); | |
| 167 void StopListening(); | |
| 168 | |
| 169 // Add a signal emission hook for one particular signal name and | |
| 170 // widget type, and save the hook_id in installed_hooks so we can | |
| 171 // remove it later. | |
| 172 void InstallEventListener( | |
| 173 const char *signal_name, | |
| 174 GType widget_type, | |
| 175 GSignalEmissionHook hook_func); | |
| 176 | |
| 177 friend struct DefaultSingletonTraits<AccessibilityEventRouterGtk>; | |
| 178 | |
| 179 // The set of all root widgets; only descendants of these will generate | |
| 180 // accessibility notifications. | |
| 181 base::hash_map<GtkWidget*, RootWidgetInfo> root_widget_info_map_; | |
| 182 | |
| 183 // Extra information about specific widgets. | |
| 184 base::hash_map<GtkWidget*, WidgetInfo> widget_info_map_; | |
| 185 | |
| 186 // Installed event listener hook ids so we can remove them later. | |
| 187 std::vector<InstalledHook> installed_hooks_; | |
| 188 | |
| 189 // True if we are currently listening to signals. | |
| 190 bool listening_; | |
| 191 | |
| 192 // The profile associated with the most recent window event - used to | |
| 193 // figure out where to route a few events that can't be directly traced | |
| 194 // to a window with a profile (like menu events). | |
| 195 Profile* most_recent_profile_; | |
| 196 | |
| 197 // The most recent focused widget. | |
| 198 GtkWidget* most_recent_widget_; | |
| 199 | |
| 200 // Used to schedule invocations of StartListening() and to defer handling | |
| 201 // of some events until the next time through the event loop. | |
| 202 ScopedRunnableMethodFactory<AccessibilityEventRouterGtk> method_factory_; | |
| 203 | |
| 204 friend class AccessibilityEventRouterGtkTest; | |
| 205 FRIEND_TEST_ALL_PREFIXES(AccessibilityEventRouterGtkTest, AddRootWidgetTwice); | |
| 206 }; | |
| 207 | 11 |
| 208 #endif // CHROME_BROWSER_GTK_ACCESSIBILITY_EVENT_ROUTER_GTK_H_ | 12 #endif // CHROME_BROWSER_GTK_ACCESSIBILITY_EVENT_ROUTER_GTK_H_ |
| OLD | NEW |