AccessibilityControllerMac.mm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (C) 2011 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #import "config.h"
  31. #import "AccessibilityCommonMac.h"
  32. #import "AccessibilityController.h"
  33. #import "AccessibilityNotificationHandler.h"
  34. #import "InjectedBundle.h"
  35. #import "InjectedBundlePage.h"
  36. #import <JavaScriptCore/JSRetainPtr.h>
  37. #import <JavaScriptCore/JSStringRef.h>
  38. #import <JavaScriptCore/JSStringRefCF.h>
  39. #import <WebKit2/WKBundle.h>
  40. #import <WebKit2/WKBundlePage.h>
  41. #import <WebKit2/WKBundlePagePrivate.h>
  42. namespace WTR {
  43. bool AccessibilityController::addNotificationListener(JSValueRef functionCallback)
  44. {
  45. if (!functionCallback)
  46. return false;
  47. if (m_globalNotificationHandler)
  48. return false;
  49. m_globalNotificationHandler = [[AccessibilityNotificationHandler alloc] init];
  50. [m_globalNotificationHandler.get() setCallback:functionCallback];
  51. [m_globalNotificationHandler.get() startObserving];
  52. return true;
  53. }
  54. bool AccessibilityController::removeNotificationListener()
  55. {
  56. ASSERT(m_globalNotificationHandler);
  57. [m_globalNotificationHandler.get() stopObserving];
  58. m_globalNotificationHandler.clear();
  59. return true;
  60. }
  61. void AccessibilityController::logAccessibilityEvents()
  62. {
  63. }
  64. void AccessibilityController::resetToConsistentState()
  65. {
  66. if (m_globalNotificationHandler)
  67. removeNotificationListener();
  68. }
  69. static id findAccessibleObjectById(id obj, NSString *idAttribute)
  70. {
  71. BEGIN_AX_OBJC_EXCEPTIONS
  72. id objIdAttribute = [obj accessibilityAttributeValue:@"AXDRTElementIdAttribute"];
  73. if ([objIdAttribute isKindOfClass:[NSString class]] && [objIdAttribute isEqualToString:idAttribute])
  74. return obj;
  75. END_AX_OBJC_EXCEPTIONS
  76. NSArray *children = [obj accessibilityAttributeValue:NSAccessibilityChildrenAttribute];
  77. NSUInteger childrenCount = [children count];
  78. for (NSUInteger i = 0; i < childrenCount; ++i) {
  79. id result = findAccessibleObjectById([children objectAtIndex:i], idAttribute);
  80. if (result)
  81. return result;
  82. }
  83. return 0;
  84. }
  85. PassRefPtr<AccessibilityUIElement> AccessibilityController::accessibleElementById(JSStringRef idAttribute)
  86. {
  87. WKBundlePageRef page = InjectedBundle::shared().page()->page();
  88. id root = static_cast<PlatformUIElement>(WKAccessibilityRootObject(page));
  89. id result = findAccessibleObjectById(root, [NSString stringWithJSStringRef:idAttribute]);
  90. if (result)
  91. return AccessibilityUIElement::create(result);
  92. return 0;
  93. }
  94. } // namespace WTR