Home | History | Annotate | Download | only in accessibility
      1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_ACCESSIBILITY_ACCESSIBILITY_EXTENSION_API_H_
      6 #define CHROME_BROWSER_ACCESSIBILITY_ACCESSIBILITY_EXTENSION_API_H_
      7 
      8 #include <string>
      9 
     10 #include "base/compiler_specific.h"
     11 #include "base/memory/singleton.h"
     12 #include "base/values.h"
     13 #include "chrome/browser/accessibility/accessibility_events.h"
     14 #include "chrome/browser/extensions/chrome_extension_function.h"
     15 #include "ui/base/accessibility/accessibility_types.h"
     16 
     17 namespace extensions {
     18 class ExtensionHost;
     19 } // namespace extensions
     20 
     21 // Observes the profile and routes accessibility notifications as events
     22 // to the extension system.
     23 class ExtensionAccessibilityEventRouter {
     24  public:
     25   typedef base::Callback<void(ui::AccessibilityTypes::Event,
     26                               const AccessibilityControlInfo*)>
     27       ControlEventCallback;
     28   // Single instance of the event router.
     29   static ExtensionAccessibilityEventRouter* GetInstance();
     30 
     31   // Get the dict representing the last control that received an
     32   // OnControlFocus event.
     33   DictionaryValue* last_focused_control_dict() {
     34     return &last_focused_control_dict_;
     35   }
     36 
     37   // Accessibility support is disabled until an extension expicitly enables
     38   // it, so that this extension api has no impact on Chrome's performance
     39   // otherwise.  These methods handle enabling, disabling, and querying the
     40   // status.
     41   void SetAccessibilityEnabled(bool enabled);
     42   bool IsAccessibilityEnabled() const;
     43 
     44   // Set and remove callbacks (used for testing, to confirm that events are
     45   // getting through).
     46   void SetControlEventCallbackForTesting(ControlEventCallback callback);
     47   void ClearControlEventCallback();
     48 
     49   // Route a window-related accessibility event.
     50   void HandleWindowEvent(ui::AccessibilityTypes::Event event,
     51                          const AccessibilityWindowInfo* info);
     52 
     53   // Route a menu-related accessibility event.
     54   void HandleMenuEvent(ui::AccessibilityTypes::Event event,
     55                        const AccessibilityMenuInfo* info);
     56 
     57   // Route a control-related accessibility event.
     58   void HandleControlEvent(ui::AccessibilityTypes::Event event,
     59                           const AccessibilityControlInfo* info);
     60 
     61   void OnChromeVoxLoadStateChanged(
     62       Profile* profile,
     63       bool loading,
     64       bool make_announcements);
     65 
     66   static void DispatchEventToChromeVox(
     67       Profile* profile,
     68       const char* event_name,
     69       scoped_ptr<base::ListValue> event_args);
     70 
     71  private:
     72   friend struct DefaultSingletonTraits<ExtensionAccessibilityEventRouter>;
     73 
     74   ExtensionAccessibilityEventRouter();
     75   virtual ~ExtensionAccessibilityEventRouter();
     76 
     77   void OnWindowOpened(const AccessibilityWindowInfo* details);
     78   void OnControlFocused(const AccessibilityControlInfo* details);
     79   void OnControlAction(const AccessibilityControlInfo* details);
     80   void OnTextChanged(const AccessibilityControlInfo* details);
     81   void OnMenuOpened(const AccessibilityMenuInfo* details);
     82   void OnMenuClosed(const AccessibilityMenuInfo* details);
     83 
     84   void DispatchEvent(Profile* profile,
     85                      const char* event_name,
     86                      scoped_ptr<base::ListValue> event_args);
     87 
     88   DictionaryValue last_focused_control_dict_;
     89 
     90   bool enabled_;
     91 
     92   // For testing.
     93   ControlEventCallback control_event_callback_;
     94 
     95   DISALLOW_COPY_AND_ASSIGN(ExtensionAccessibilityEventRouter);
     96 };
     97 
     98 // API function that enables or disables accessibility support.  Event
     99 // listeners are only installed when accessibility support is enabled, to
    100 // minimize the impact.
    101 class AccessibilitySetAccessibilityEnabledFunction
    102     : public ChromeSyncExtensionFunction {
    103   virtual ~AccessibilitySetAccessibilityEnabledFunction() {}
    104   virtual bool RunImpl() OVERRIDE;
    105   DECLARE_EXTENSION_FUNCTION(
    106       "experimental.accessibility.setAccessibilityEnabled",
    107       EXPERIMENTAL_ACCESSIBILITY_SETACCESSIBILITYENABLED)
    108 };
    109 
    110 // API function that enables or disables web content accessibility support.
    111 class AccessibilitySetNativeAccessibilityEnabledFunction
    112     : public ChromeSyncExtensionFunction {
    113   virtual ~AccessibilitySetNativeAccessibilityEnabledFunction() {}
    114   virtual bool RunImpl() OVERRIDE;
    115   DECLARE_EXTENSION_FUNCTION(
    116       "experimental.accessibility.setNativeAccessibilityEnabled",
    117       EXPERIMENTAL_ACCESSIBILITY_SETNATIVEACCESSIBILITYENABLED)
    118 };
    119 
    120 // API function that returns the most recent focused control.
    121 class AccessibilityGetFocusedControlFunction
    122     : public ChromeSyncExtensionFunction {
    123   virtual ~AccessibilityGetFocusedControlFunction() {}
    124   virtual bool RunImpl() OVERRIDE;
    125   DECLARE_EXTENSION_FUNCTION(
    126       "experimental.accessibility.getFocusedControl",
    127       EXPERIMENTAL_ACCESSIBILITY_GETFOCUSEDCONTROL)
    128 };
    129 
    130 // API function that returns alerts being shown on the give tab.
    131 class AccessibilityGetAlertsForTabFunction
    132     : public ChromeSyncExtensionFunction {
    133   virtual ~AccessibilityGetAlertsForTabFunction() {}
    134   virtual bool RunImpl() OVERRIDE;
    135   DECLARE_EXTENSION_FUNCTION(
    136       "experimental.accessibility.getAlertsForTab",
    137       EXPERIMENTAL_ACCESSIBILITY_GETALERTSFORTAB)
    138 };
    139 
    140 #endif  // CHROME_BROWSER_ACCESSIBILITY_ACCESSIBILITY_EXTENSION_API_H_
    141