AccessibilityNotificationHandler.mm 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "InjectedBundle.h"
  32. #import "InjectedBundlePage.h"
  33. #import "JSWrapper.h"
  34. #import "AccessibilityNotificationHandler.h"
  35. #import "AccessibilityUIElement.h"
  36. #import <JavaScriptCore/JSRetainPtr.h>
  37. #import <JavaScriptCore/JSStringRef.h>
  38. #import <JavaScriptCore/JSStringRefCF.h>
  39. #import <WebKit2/WKBundleFrame.h>
  40. #import <wtf/RetainPtr.h>
  41. @interface NSObject (WebAccessibilityObjectWrapperAdditions)
  42. + (void)accessibilitySetShouldRepostNotifications:(BOOL)repost;
  43. @end
  44. @interface NSString (JSStringRefAdditions)
  45. - (JSStringRef)createJSStringRef;
  46. @end
  47. @implementation NSString (JSStringRefAdditions)
  48. - (JSStringRef)createJSStringRef
  49. {
  50. return JSStringCreateWithCFString((CFStringRef)self);
  51. }
  52. @end
  53. @implementation AccessibilityNotificationHandler
  54. - (id)init
  55. {
  56. if (!(self = [super init]))
  57. return nil;
  58. return self;
  59. }
  60. - (void)setPlatformElement:(id)platformElement
  61. {
  62. m_platformElement = platformElement;
  63. }
  64. - (void)dealloc
  65. {
  66. [self stopObserving];
  67. WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(WTR::InjectedBundle::shared().page()->page());
  68. JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
  69. JSValueUnprotect(context, m_notificationFunctionCallback);
  70. m_notificationFunctionCallback = 0;
  71. [super dealloc];
  72. }
  73. - (void)setCallback:(JSValueRef)callback
  74. {
  75. if (!callback)
  76. return;
  77. WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(WTR::InjectedBundle::shared().page()->page());
  78. JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
  79. if (m_notificationFunctionCallback)
  80. JSValueUnprotect(context, m_notificationFunctionCallback);
  81. m_notificationFunctionCallback = callback;
  82. JSValueProtect(context, m_notificationFunctionCallback);
  83. }
  84. - (void)startObserving
  85. {
  86. // Once we start requesting notifications, it's on for the duration of the program.
  87. // This is to avoid any race conditions between tests turning this flag on and off. Instead
  88. // AccessibilityNotificationHandler can ignore events it doesn't care about.
  89. id webAccessibilityObjectWrapperClass = NSClassFromString(@"WebAccessibilityObjectWrapper");
  90. ASSERT(webAccessibilityObjectWrapperClass);
  91. [webAccessibilityObjectWrapperClass accessibilitySetShouldRepostNotifications:YES];
  92. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_notificationReceived:) name:@"AXDRTNotification" object:nil];
  93. }
  94. - (void)stopObserving
  95. {
  96. [[NSNotificationCenter defaultCenter] removeObserver:self];
  97. }
  98. - (void)_notificationReceived:(NSNotification *)notification
  99. {
  100. NSString *notificationName = [[notification userInfo] objectForKey:@"notificationName"];
  101. if (!notificationName)
  102. return;
  103. if (m_platformElement && m_platformElement != [notification object])
  104. return;
  105. WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(WTR::InjectedBundle::shared().page()->page());
  106. JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame);
  107. JSRetainPtr<JSStringRef> jsNotification(Adopt, [notificationName createJSStringRef]);
  108. JSValueRef notificationNameArgument = JSValueMakeString(context, jsNotification.get());
  109. if (m_platformElement) {
  110. // Listener for one element just gets one argument, the notification name.
  111. JSObjectCallAsFunction(context, const_cast<JSObjectRef>(m_notificationFunctionCallback), 0, 1, &notificationNameArgument, 0);
  112. } else {
  113. // A global listener gets the element and the notification name as arguments.
  114. JSValueRef arguments[2];
  115. arguments[0] = toJS(context, WTF::getPtr(WTR::AccessibilityUIElement::create([notification object])));
  116. arguments[1] = notificationNameArgument;
  117. JSObjectCallAsFunction(context, const_cast<JSObjectRef>(m_notificationFunctionCallback), 0, 2, arguments, 0);
  118. }
  119. }
  120. @end