• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_ACCESSIBILITY_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_ACCESSIBILITY_API_H_
7 #pragma once
8 
9 #include <string>
10 #include <vector>
11 
12 #include "base/callback.h"
13 #include "base/memory/singleton.h"
14 #include "base/values.h"
15 #include "chrome/browser/accessibility_events.h"
16 #include "chrome/browser/extensions/extension_function.h"
17 #include "content/common/notification_observer.h"
18 #include "content/common/notification_registrar.h"
19 
20 // Observes the profile and routes accessibility notifications as events
21 // to the extension system.
22 class ExtensionAccessibilityEventRouter : public NotificationObserver {
23  public:
24   // Single instance of the event router.
25   static ExtensionAccessibilityEventRouter* GetInstance();
26 
27   // Safe to call multiple times.
28   void ObserveProfile(Profile* profile);
29 
30   // Get the dict representing the last control that received an
31   // OnControlFocus event.
last_focused_control_dict()32   DictionaryValue* last_focused_control_dict() {
33     return &last_focused_control_dict_;
34   }
35 
36   // Accessibility support is disabled until an extension expicitly enables
37   // it, so that this extension api has no impact on Chrome's performance
38   // otherwise.  These methods handle enabling, disabling, querying the
39   // status, and installing callbacks to execute when accessibility support
40   // is enabled or disabled.
41   void SetAccessibilityEnabled(bool enabled);
42   bool IsAccessibilityEnabled() const;
43   typedef Callback0::Type Callback;
44   void AddOnEnabledListener(Callback* callback);
45   void AddOnDisabledListener(Callback* callback);
46 
47  private:
48   friend struct DefaultSingletonTraits<ExtensionAccessibilityEventRouter>;
49 
50   ExtensionAccessibilityEventRouter();
51   virtual ~ExtensionAccessibilityEventRouter();
52 
53   // NotificationObserver::Observe.
54   virtual void Observe(NotificationType type,
55                        const NotificationSource& source,
56                        const NotificationDetails& details);
57 
58   void OnWindowOpened(const AccessibilityWindowInfo* details);
59   void OnWindowClosed(const AccessibilityWindowInfo* details);
60   void OnControlFocused(const AccessibilityControlInfo* details);
61   void OnControlAction(const AccessibilityControlInfo* details);
62   void OnTextChanged(const AccessibilityControlInfo* details);
63   void OnMenuOpened(const AccessibilityMenuInfo* details);
64   void OnMenuClosed(const AccessibilityMenuInfo* details);
65 
66   void DispatchEvent(Profile* profile,
67                      const char* event_name,
68                      const std::string& json_args);
69 
70   // Used for tracking registrations to history service notifications.
71   NotificationRegistrar registrar_;
72 
73   DictionaryValue last_focused_control_dict_;
74 
75   bool enabled_;
76   std::vector<Callback*> on_enabled_listeners_;
77   std::vector<Callback*> on_disabled_listeners_;
78 
79   DISALLOW_COPY_AND_ASSIGN(ExtensionAccessibilityEventRouter);
80 };
81 
82 // API function that enables or disables accessibility support.  Event
83 // listeners are only installed when accessibility support is enabled, to
84 // minimize the impact.
85 class SetAccessibilityEnabledFunction : public SyncExtensionFunction {
86   virtual ~SetAccessibilityEnabledFunction() {}
87   virtual bool RunImpl();
88   DECLARE_EXTENSION_FUNCTION_NAME(
89       "experimental.accessibility.setAccessibilityEnabled")
90 };
91 
92 // API function that returns the most recent focused control.
93 class GetFocusedControlFunction : public SyncExtensionFunction {
94   virtual ~GetFocusedControlFunction() {}
95   virtual bool RunImpl();
96   DECLARE_EXTENSION_FUNCTION_NAME(
97       "experimental.accessibility.getFocusedControl")
98 };
99 
100 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_ACCESSIBILITY_API_H_
101