Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | 'use strict'; |
michael@0 | 6 | |
michael@0 | 7 | const Ci = Components.interfaces; |
michael@0 | 8 | const Cu = Components.utils; |
michael@0 | 9 | |
michael@0 | 10 | const TEXT_NODE = 3; |
michael@0 | 11 | |
michael@0 | 12 | Cu.import('resource://gre/modules/XPCOMUtils.jsm'); |
michael@0 | 13 | XPCOMUtils.defineLazyModuleGetter(this, 'Services', |
michael@0 | 14 | 'resource://gre/modules/Services.jsm'); |
michael@0 | 15 | XPCOMUtils.defineLazyModuleGetter(this, 'Utils', |
michael@0 | 16 | 'resource://gre/modules/accessibility/Utils.jsm'); |
michael@0 | 17 | XPCOMUtils.defineLazyModuleGetter(this, 'Logger', |
michael@0 | 18 | 'resource://gre/modules/accessibility/Utils.jsm'); |
michael@0 | 19 | XPCOMUtils.defineLazyModuleGetter(this, 'Presentation', |
michael@0 | 20 | 'resource://gre/modules/accessibility/Presentation.jsm'); |
michael@0 | 21 | XPCOMUtils.defineLazyModuleGetter(this, 'TraversalRules', |
michael@0 | 22 | 'resource://gre/modules/accessibility/TraversalRules.jsm'); |
michael@0 | 23 | XPCOMUtils.defineLazyModuleGetter(this, 'Roles', |
michael@0 | 24 | 'resource://gre/modules/accessibility/Constants.jsm'); |
michael@0 | 25 | XPCOMUtils.defineLazyModuleGetter(this, 'Events', |
michael@0 | 26 | 'resource://gre/modules/accessibility/Constants.jsm'); |
michael@0 | 27 | XPCOMUtils.defineLazyModuleGetter(this, 'States', |
michael@0 | 28 | 'resource://gre/modules/accessibility/Constants.jsm'); |
michael@0 | 29 | |
michael@0 | 30 | this.EXPORTED_SYMBOLS = ['EventManager']; |
michael@0 | 31 | |
michael@0 | 32 | this.EventManager = function EventManager(aContentScope) { |
michael@0 | 33 | this.contentScope = aContentScope; |
michael@0 | 34 | this.addEventListener = this.contentScope.addEventListener.bind( |
michael@0 | 35 | this.contentScope); |
michael@0 | 36 | this.removeEventListener = this.contentScope.removeEventListener.bind( |
michael@0 | 37 | this.contentScope); |
michael@0 | 38 | this.sendMsgFunc = this.contentScope.sendAsyncMessage.bind( |
michael@0 | 39 | this.contentScope); |
michael@0 | 40 | this.webProgress = this.contentScope.docShell. |
michael@0 | 41 | QueryInterface(Ci.nsIInterfaceRequestor). |
michael@0 | 42 | getInterface(Ci.nsIWebProgress); |
michael@0 | 43 | }; |
michael@0 | 44 | |
michael@0 | 45 | this.EventManager.prototype = { |
michael@0 | 46 | editState: {}, |
michael@0 | 47 | |
michael@0 | 48 | start: function start() { |
michael@0 | 49 | try { |
michael@0 | 50 | if (!this._started) { |
michael@0 | 51 | Logger.debug('EventManager.start'); |
michael@0 | 52 | |
michael@0 | 53 | this._started = true; |
michael@0 | 54 | |
michael@0 | 55 | AccessibilityEventObserver.addListener(this); |
michael@0 | 56 | |
michael@0 | 57 | this.webProgress.addProgressListener(this, |
michael@0 | 58 | (Ci.nsIWebProgress.NOTIFY_STATE_ALL | |
michael@0 | 59 | Ci.nsIWebProgress.NOTIFY_LOCATION)); |
michael@0 | 60 | this.addEventListener('wheel', this, true); |
michael@0 | 61 | this.addEventListener('scroll', this, true); |
michael@0 | 62 | this.addEventListener('resize', this, true); |
michael@0 | 63 | } |
michael@0 | 64 | this.present(Presentation.tabStateChanged(null, 'newtab')); |
michael@0 | 65 | |
michael@0 | 66 | } catch (x) { |
michael@0 | 67 | Logger.logException(x, 'Failed to start EventManager'); |
michael@0 | 68 | } |
michael@0 | 69 | }, |
michael@0 | 70 | |
michael@0 | 71 | // XXX: Stop is not called when the tab is closed (|TabClose| event is too |
michael@0 | 72 | // late). It is only called when the AccessFu is disabled explicitly. |
michael@0 | 73 | stop: function stop() { |
michael@0 | 74 | if (!this._started) { |
michael@0 | 75 | return; |
michael@0 | 76 | } |
michael@0 | 77 | Logger.debug('EventManager.stop'); |
michael@0 | 78 | AccessibilityEventObserver.removeListener(this); |
michael@0 | 79 | try { |
michael@0 | 80 | this.webProgress.removeProgressListener(this); |
michael@0 | 81 | this.removeEventListener('wheel', this, true); |
michael@0 | 82 | this.removeEventListener('scroll', this, true); |
michael@0 | 83 | this.removeEventListener('resize', this, true); |
michael@0 | 84 | } catch (x) { |
michael@0 | 85 | // contentScope is dead. |
michael@0 | 86 | } finally { |
michael@0 | 87 | this._started = false; |
michael@0 | 88 | } |
michael@0 | 89 | }, |
michael@0 | 90 | |
michael@0 | 91 | handleEvent: function handleEvent(aEvent) { |
michael@0 | 92 | Logger.debug(() => { |
michael@0 | 93 | return ['DOMEvent', aEvent.type]; |
michael@0 | 94 | }); |
michael@0 | 95 | |
michael@0 | 96 | try { |
michael@0 | 97 | switch (aEvent.type) { |
michael@0 | 98 | case 'wheel': |
michael@0 | 99 | { |
michael@0 | 100 | let attempts = 0; |
michael@0 | 101 | let delta = aEvent.deltaX || aEvent.deltaY; |
michael@0 | 102 | this.contentScope.contentControl.autoMove( |
michael@0 | 103 | null, |
michael@0 | 104 | { moveMethod: delta > 0 ? 'moveNext' : 'movePrevious', |
michael@0 | 105 | onScreenOnly: true, noOpIfOnScreen: true, delay: 500 }); |
michael@0 | 106 | break; |
michael@0 | 107 | } |
michael@0 | 108 | case 'scroll': |
michael@0 | 109 | case 'resize': |
michael@0 | 110 | { |
michael@0 | 111 | // the target could be an element, document or window |
michael@0 | 112 | let window = null; |
michael@0 | 113 | if (aEvent.target instanceof Ci.nsIDOMWindow) |
michael@0 | 114 | window = aEvent.target; |
michael@0 | 115 | else if (aEvent.target instanceof Ci.nsIDOMDocument) |
michael@0 | 116 | window = aEvent.target.defaultView; |
michael@0 | 117 | else if (aEvent.target instanceof Ci.nsIDOMElement) |
michael@0 | 118 | window = aEvent.target.ownerDocument.defaultView; |
michael@0 | 119 | this.present(Presentation.viewportChanged(window)); |
michael@0 | 120 | break; |
michael@0 | 121 | } |
michael@0 | 122 | } |
michael@0 | 123 | } catch (x) { |
michael@0 | 124 | Logger.logException(x, 'Error handling DOM event'); |
michael@0 | 125 | } |
michael@0 | 126 | }, |
michael@0 | 127 | |
michael@0 | 128 | handleAccEvent: function handleAccEvent(aEvent) { |
michael@0 | 129 | Logger.debug(() => { |
michael@0 | 130 | return ['A11yEvent', Logger.eventToString(aEvent), |
michael@0 | 131 | Logger.accessibleToString(aEvent.accessible)]; |
michael@0 | 132 | }); |
michael@0 | 133 | |
michael@0 | 134 | // Don't bother with non-content events in firefox. |
michael@0 | 135 | if (Utils.MozBuildApp == 'browser' && |
michael@0 | 136 | aEvent.eventType != Events.VIRTUALCURSOR_CHANGED && |
michael@0 | 137 | // XXX Bug 442005 results in DocAccessible::getDocType returning |
michael@0 | 138 | // NS_ERROR_FAILURE. Checking for aEvent.accessibleDocument.docType == |
michael@0 | 139 | // 'window' does not currently work. |
michael@0 | 140 | (aEvent.accessibleDocument.DOMDocument.doctype && |
michael@0 | 141 | aEvent.accessibleDocument.DOMDocument.doctype.name === 'window')) { |
michael@0 | 142 | return; |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | switch (aEvent.eventType) { |
michael@0 | 146 | case Events.VIRTUALCURSOR_CHANGED: |
michael@0 | 147 | { |
michael@0 | 148 | let pivot = aEvent.accessible. |
michael@0 | 149 | QueryInterface(Ci.nsIAccessibleDocument).virtualCursor; |
michael@0 | 150 | let position = pivot.position; |
michael@0 | 151 | if (position && position.role == Roles.INTERNAL_FRAME) |
michael@0 | 152 | break; |
michael@0 | 153 | let event = aEvent. |
michael@0 | 154 | QueryInterface(Ci.nsIAccessibleVirtualCursorChangeEvent); |
michael@0 | 155 | let reason = event.reason; |
michael@0 | 156 | let oldAccessible = event.oldAccessible; |
michael@0 | 157 | |
michael@0 | 158 | if (this.editState.editing) { |
michael@0 | 159 | aEvent.accessibleDocument.takeFocus(); |
michael@0 | 160 | } |
michael@0 | 161 | this.present( |
michael@0 | 162 | Presentation.pivotChanged(position, oldAccessible, reason, |
michael@0 | 163 | pivot.startOffset, pivot.endOffset)); |
michael@0 | 164 | |
michael@0 | 165 | break; |
michael@0 | 166 | } |
michael@0 | 167 | case Events.STATE_CHANGE: |
michael@0 | 168 | { |
michael@0 | 169 | let event = aEvent.QueryInterface(Ci.nsIAccessibleStateChangeEvent); |
michael@0 | 170 | let state = Utils.getState(event); |
michael@0 | 171 | if (state.contains(States.CHECKED)) { |
michael@0 | 172 | this.present( |
michael@0 | 173 | Presentation. |
michael@0 | 174 | actionInvoked(aEvent.accessible, |
michael@0 | 175 | event.isEnabled ? 'check' : 'uncheck')); |
michael@0 | 176 | } else if (state.contains(States.SELECTED)) { |
michael@0 | 177 | this.present( |
michael@0 | 178 | Presentation. |
michael@0 | 179 | actionInvoked(aEvent.accessible, |
michael@0 | 180 | event.isEnabled ? 'select' : 'unselect')); |
michael@0 | 181 | } |
michael@0 | 182 | break; |
michael@0 | 183 | } |
michael@0 | 184 | case Events.SCROLLING_START: |
michael@0 | 185 | { |
michael@0 | 186 | this.contentScope.contentControl.autoMove(aEvent.accessible); |
michael@0 | 187 | break; |
michael@0 | 188 | } |
michael@0 | 189 | case Events.TEXT_CARET_MOVED: |
michael@0 | 190 | { |
michael@0 | 191 | let acc = aEvent.accessible; |
michael@0 | 192 | let characterCount = acc. |
michael@0 | 193 | QueryInterface(Ci.nsIAccessibleText).characterCount; |
michael@0 | 194 | let caretOffset = aEvent. |
michael@0 | 195 | QueryInterface(Ci.nsIAccessibleCaretMoveEvent).caretOffset; |
michael@0 | 196 | |
michael@0 | 197 | // Update editing state, both for presenter and other things |
michael@0 | 198 | let state = Utils.getState(acc); |
michael@0 | 199 | let editState = { |
michael@0 | 200 | editing: state.contains(States.EDITABLE), |
michael@0 | 201 | multiline: state.contains(States.MULTI_LINE), |
michael@0 | 202 | atStart: caretOffset == 0, |
michael@0 | 203 | atEnd: caretOffset == characterCount |
michael@0 | 204 | }; |
michael@0 | 205 | |
michael@0 | 206 | // Not interesting |
michael@0 | 207 | if (!editState.editing && editState.editing == this.editState.editing) |
michael@0 | 208 | break; |
michael@0 | 209 | |
michael@0 | 210 | if (editState.editing != this.editState.editing) |
michael@0 | 211 | this.present(Presentation.editingModeChanged(editState.editing)); |
michael@0 | 212 | |
michael@0 | 213 | if (editState.editing != this.editState.editing || |
michael@0 | 214 | editState.multiline != this.editState.multiline || |
michael@0 | 215 | editState.atEnd != this.editState.atEnd || |
michael@0 | 216 | editState.atStart != this.editState.atStart) |
michael@0 | 217 | this.sendMsgFunc("AccessFu:Input", editState); |
michael@0 | 218 | |
michael@0 | 219 | this.present(Presentation.textSelectionChanged(acc.getText(0,-1), |
michael@0 | 220 | caretOffset, caretOffset, 0, 0, aEvent.isFromUserInput)); |
michael@0 | 221 | |
michael@0 | 222 | this.editState = editState; |
michael@0 | 223 | break; |
michael@0 | 224 | } |
michael@0 | 225 | case Events.SHOW: |
michael@0 | 226 | { |
michael@0 | 227 | let {liveRegion, isPolite} = this._handleLiveRegion(aEvent, |
michael@0 | 228 | ['additions', 'all']); |
michael@0 | 229 | // Only handle show if it is a relevant live region. |
michael@0 | 230 | if (!liveRegion) { |
michael@0 | 231 | break; |
michael@0 | 232 | } |
michael@0 | 233 | // Show for text is handled by the EVENT_TEXT_INSERTED handler. |
michael@0 | 234 | if (aEvent.accessible.role === Roles.TEXT_LEAF) { |
michael@0 | 235 | break; |
michael@0 | 236 | } |
michael@0 | 237 | this._dequeueLiveEvent(Events.HIDE, liveRegion); |
michael@0 | 238 | this.present(Presentation.liveRegion(liveRegion, isPolite, false)); |
michael@0 | 239 | break; |
michael@0 | 240 | } |
michael@0 | 241 | case Events.HIDE: |
michael@0 | 242 | { |
michael@0 | 243 | let evt = aEvent.QueryInterface(Ci.nsIAccessibleHideEvent); |
michael@0 | 244 | let {liveRegion, isPolite} = this._handleLiveRegion( |
michael@0 | 245 | evt, ['removals', 'all']); |
michael@0 | 246 | if (liveRegion) { |
michael@0 | 247 | // Hide for text is handled by the EVENT_TEXT_REMOVED handler. |
michael@0 | 248 | if (aEvent.accessible.role === Roles.TEXT_LEAF) { |
michael@0 | 249 | break; |
michael@0 | 250 | } |
michael@0 | 251 | this._queueLiveEvent(Events.HIDE, liveRegion, isPolite); |
michael@0 | 252 | } else { |
michael@0 | 253 | let vc = Utils.getVirtualCursor(this.contentScope.content.document); |
michael@0 | 254 | if (vc.position && |
michael@0 | 255 | (Utils.getState(vc.position).contains(States.DEFUNCT) || |
michael@0 | 256 | Utils.isInSubtree(vc.position, aEvent.accessible))) { |
michael@0 | 257 | this.contentScope.contentControl.autoMove( |
michael@0 | 258 | evt.targetPrevSibling || evt.targetParent, |
michael@0 | 259 | { moveToFocused: true, delay: 500 }); |
michael@0 | 260 | } |
michael@0 | 261 | } |
michael@0 | 262 | break; |
michael@0 | 263 | } |
michael@0 | 264 | case Events.TEXT_INSERTED: |
michael@0 | 265 | case Events.TEXT_REMOVED: |
michael@0 | 266 | { |
michael@0 | 267 | let {liveRegion, isPolite} = this._handleLiveRegion(aEvent, |
michael@0 | 268 | ['text', 'all']); |
michael@0 | 269 | if (aEvent.isFromUserInput || liveRegion) { |
michael@0 | 270 | // Handle all text mutations coming from the user or if they happen |
michael@0 | 271 | // on a live region. |
michael@0 | 272 | this._handleText(aEvent, liveRegion, isPolite); |
michael@0 | 273 | } |
michael@0 | 274 | break; |
michael@0 | 275 | } |
michael@0 | 276 | case Events.FOCUS: |
michael@0 | 277 | { |
michael@0 | 278 | // Put vc where the focus is at |
michael@0 | 279 | let acc = aEvent.accessible; |
michael@0 | 280 | let doc = aEvent.accessibleDocument; |
michael@0 | 281 | if (acc.role != Roles.DOCUMENT && doc.role != Roles.CHROME_WINDOW) { |
michael@0 | 282 | this.contentScope.contentControl.autoMove(acc); |
michael@0 | 283 | } |
michael@0 | 284 | break; |
michael@0 | 285 | } |
michael@0 | 286 | case Events.DOCUMENT_LOAD_COMPLETE: |
michael@0 | 287 | { |
michael@0 | 288 | if (aEvent.accessible === aEvent.accessibleDocument) { |
michael@0 | 289 | break; |
michael@0 | 290 | } |
michael@0 | 291 | this.contentScope.contentControl.autoMove( |
michael@0 | 292 | aEvent.accessible, { delay: 500 }); |
michael@0 | 293 | break; |
michael@0 | 294 | } |
michael@0 | 295 | case Events.VALUE_CHANGE: |
michael@0 | 296 | { |
michael@0 | 297 | let position = this.contentScope.contentControl.vc.position; |
michael@0 | 298 | let target = aEvent.accessible; |
michael@0 | 299 | if (position === target || |
michael@0 | 300 | Utils.getEmbeddedControl(position) === target) { |
michael@0 | 301 | this.present(Presentation.valueChanged(target)); |
michael@0 | 302 | } |
michael@0 | 303 | } |
michael@0 | 304 | } |
michael@0 | 305 | }, |
michael@0 | 306 | |
michael@0 | 307 | _handleText: function _handleText(aEvent, aLiveRegion, aIsPolite) { |
michael@0 | 308 | let event = aEvent.QueryInterface(Ci.nsIAccessibleTextChangeEvent); |
michael@0 | 309 | let isInserted = event.isInserted; |
michael@0 | 310 | let txtIface = aEvent.accessible.QueryInterface(Ci.nsIAccessibleText); |
michael@0 | 311 | |
michael@0 | 312 | let text = ''; |
michael@0 | 313 | try { |
michael@0 | 314 | text = txtIface.getText(0, Ci.nsIAccessibleText.TEXT_OFFSET_END_OF_TEXT); |
michael@0 | 315 | } catch (x) { |
michael@0 | 316 | // XXX we might have gotten an exception with of a |
michael@0 | 317 | // zero-length text. If we did, ignore it (bug #749810). |
michael@0 | 318 | if (txtIface.characterCount) { |
michael@0 | 319 | throw x; |
michael@0 | 320 | } |
michael@0 | 321 | } |
michael@0 | 322 | // If there are embedded objects in the text, ignore them. |
michael@0 | 323 | // Assuming changes to the descendants would already be handled by the |
michael@0 | 324 | // show/hide event. |
michael@0 | 325 | let modifiedText = event.modifiedText.replace(/\uFFFC/g, '').trim(); |
michael@0 | 326 | if (!modifiedText) { |
michael@0 | 327 | return; |
michael@0 | 328 | } |
michael@0 | 329 | if (aLiveRegion) { |
michael@0 | 330 | if (aEvent.eventType === Events.TEXT_REMOVED) { |
michael@0 | 331 | this._queueLiveEvent(Events.TEXT_REMOVED, aLiveRegion, aIsPolite, |
michael@0 | 332 | modifiedText); |
michael@0 | 333 | } else { |
michael@0 | 334 | this._dequeueLiveEvent(Events.TEXT_REMOVED, aLiveRegion); |
michael@0 | 335 | this.present(Presentation.liveRegion(aLiveRegion, aIsPolite, false, |
michael@0 | 336 | modifiedText)); |
michael@0 | 337 | } |
michael@0 | 338 | } else { |
michael@0 | 339 | this.present(Presentation.textChanged(isInserted, event.start, |
michael@0 | 340 | event.length, text, modifiedText)); |
michael@0 | 341 | } |
michael@0 | 342 | }, |
michael@0 | 343 | |
michael@0 | 344 | _handleLiveRegion: function _handleLiveRegion(aEvent, aRelevant) { |
michael@0 | 345 | if (aEvent.isFromUserInput) { |
michael@0 | 346 | return {}; |
michael@0 | 347 | } |
michael@0 | 348 | let parseLiveAttrs = function parseLiveAttrs(aAccessible) { |
michael@0 | 349 | let attrs = Utils.getAttributes(aAccessible); |
michael@0 | 350 | if (attrs['container-live']) { |
michael@0 | 351 | return { |
michael@0 | 352 | live: attrs['container-live'], |
michael@0 | 353 | relevant: attrs['container-relevant'] || 'additions text', |
michael@0 | 354 | busy: attrs['container-busy'], |
michael@0 | 355 | atomic: attrs['container-atomic'], |
michael@0 | 356 | memberOf: attrs['member-of'] |
michael@0 | 357 | }; |
michael@0 | 358 | } |
michael@0 | 359 | return null; |
michael@0 | 360 | }; |
michael@0 | 361 | // XXX live attributes are not set for hidden accessibles yet. Need to |
michael@0 | 362 | // climb up the tree to check for them. |
michael@0 | 363 | let getLiveAttributes = function getLiveAttributes(aEvent) { |
michael@0 | 364 | let liveAttrs = parseLiveAttrs(aEvent.accessible); |
michael@0 | 365 | if (liveAttrs) { |
michael@0 | 366 | return liveAttrs; |
michael@0 | 367 | } |
michael@0 | 368 | let parent = aEvent.targetParent; |
michael@0 | 369 | while (parent) { |
michael@0 | 370 | liveAttrs = parseLiveAttrs(parent); |
michael@0 | 371 | if (liveAttrs) { |
michael@0 | 372 | return liveAttrs; |
michael@0 | 373 | } |
michael@0 | 374 | parent = parent.parent |
michael@0 | 375 | } |
michael@0 | 376 | return {}; |
michael@0 | 377 | }; |
michael@0 | 378 | let {live, relevant, busy, atomic, memberOf} = getLiveAttributes(aEvent); |
michael@0 | 379 | // If container-live is not present or is set to |off| ignore the event. |
michael@0 | 380 | if (!live || live === 'off') { |
michael@0 | 381 | return {}; |
michael@0 | 382 | } |
michael@0 | 383 | // XXX: support busy and atomic. |
michael@0 | 384 | |
michael@0 | 385 | // Determine if the type of the mutation is relevant. Default is additions |
michael@0 | 386 | // and text. |
michael@0 | 387 | let isRelevant = Utils.matchAttributeValue(relevant, aRelevant); |
michael@0 | 388 | if (!isRelevant) { |
michael@0 | 389 | return {}; |
michael@0 | 390 | } |
michael@0 | 391 | return { |
michael@0 | 392 | liveRegion: aEvent.accessible, |
michael@0 | 393 | isPolite: live === 'polite' |
michael@0 | 394 | }; |
michael@0 | 395 | }, |
michael@0 | 396 | |
michael@0 | 397 | _dequeueLiveEvent: function _dequeueLiveEvent(aEventType, aLiveRegion) { |
michael@0 | 398 | let domNode = aLiveRegion.DOMNode; |
michael@0 | 399 | if (this._liveEventQueue && this._liveEventQueue.has(domNode)) { |
michael@0 | 400 | let queue = this._liveEventQueue.get(domNode); |
michael@0 | 401 | let nextEvent = queue[0]; |
michael@0 | 402 | if (nextEvent.eventType === aEventType) { |
michael@0 | 403 | Utils.win.clearTimeout(nextEvent.timeoutID); |
michael@0 | 404 | queue.shift(); |
michael@0 | 405 | if (queue.length === 0) { |
michael@0 | 406 | this._liveEventQueue.delete(domNode) |
michael@0 | 407 | } |
michael@0 | 408 | } |
michael@0 | 409 | } |
michael@0 | 410 | }, |
michael@0 | 411 | |
michael@0 | 412 | _queueLiveEvent: function _queueLiveEvent(aEventType, aLiveRegion, aIsPolite, aModifiedText) { |
michael@0 | 413 | if (!this._liveEventQueue) { |
michael@0 | 414 | this._liveEventQueue = new WeakMap(); |
michael@0 | 415 | } |
michael@0 | 416 | let eventHandler = { |
michael@0 | 417 | eventType: aEventType, |
michael@0 | 418 | timeoutID: Utils.win.setTimeout(this.present.bind(this), |
michael@0 | 419 | 20, // Wait for a possible EVENT_SHOW or EVENT_TEXT_INSERTED event. |
michael@0 | 420 | Presentation.liveRegion(aLiveRegion, aIsPolite, true, aModifiedText)) |
michael@0 | 421 | }; |
michael@0 | 422 | |
michael@0 | 423 | let domNode = aLiveRegion.DOMNode; |
michael@0 | 424 | if (this._liveEventQueue.has(domNode)) { |
michael@0 | 425 | this._liveEventQueue.get(domNode).push(eventHandler); |
michael@0 | 426 | } else { |
michael@0 | 427 | this._liveEventQueue.set(domNode, [eventHandler]); |
michael@0 | 428 | } |
michael@0 | 429 | }, |
michael@0 | 430 | |
michael@0 | 431 | present: function present(aPresentationData) { |
michael@0 | 432 | this.sendMsgFunc("AccessFu:Present", aPresentationData); |
michael@0 | 433 | }, |
michael@0 | 434 | |
michael@0 | 435 | onStateChange: function onStateChange(aWebProgress, aRequest, aStateFlags, aStatus) { |
michael@0 | 436 | let tabstate = ''; |
michael@0 | 437 | |
michael@0 | 438 | let loadingState = Ci.nsIWebProgressListener.STATE_TRANSFERRING | |
michael@0 | 439 | Ci.nsIWebProgressListener.STATE_IS_DOCUMENT; |
michael@0 | 440 | let loadedState = Ci.nsIWebProgressListener.STATE_STOP | |
michael@0 | 441 | Ci.nsIWebProgressListener.STATE_IS_NETWORK; |
michael@0 | 442 | |
michael@0 | 443 | if ((aStateFlags & loadingState) == loadingState) { |
michael@0 | 444 | tabstate = 'loading'; |
michael@0 | 445 | } else if ((aStateFlags & loadedState) == loadedState && |
michael@0 | 446 | !aWebProgress.isLoadingDocument) { |
michael@0 | 447 | tabstate = 'loaded'; |
michael@0 | 448 | } |
michael@0 | 449 | |
michael@0 | 450 | if (tabstate) { |
michael@0 | 451 | let docAcc = Utils.AccRetrieval.getAccessibleFor(aWebProgress.DOMWindow.document); |
michael@0 | 452 | this.present(Presentation.tabStateChanged(docAcc, tabstate)); |
michael@0 | 453 | } |
michael@0 | 454 | }, |
michael@0 | 455 | |
michael@0 | 456 | onProgressChange: function onProgressChange() {}, |
michael@0 | 457 | |
michael@0 | 458 | onLocationChange: function onLocationChange(aWebProgress, aRequest, aLocation, aFlags) { |
michael@0 | 459 | let docAcc = Utils.AccRetrieval.getAccessibleFor(aWebProgress.DOMWindow.document); |
michael@0 | 460 | this.present(Presentation.tabStateChanged(docAcc, 'newdoc')); |
michael@0 | 461 | }, |
michael@0 | 462 | |
michael@0 | 463 | onStatusChange: function onStatusChange() {}, |
michael@0 | 464 | |
michael@0 | 465 | onSecurityChange: function onSecurityChange() {}, |
michael@0 | 466 | |
michael@0 | 467 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIWebProgressListener, |
michael@0 | 468 | Ci.nsISupportsWeakReference, |
michael@0 | 469 | Ci.nsISupports, |
michael@0 | 470 | Ci.nsIObserver]) |
michael@0 | 471 | }; |
michael@0 | 472 | |
michael@0 | 473 | const AccessibilityEventObserver = { |
michael@0 | 474 | |
michael@0 | 475 | /** |
michael@0 | 476 | * A WeakMap containing [content, EventManager] pairs. |
michael@0 | 477 | */ |
michael@0 | 478 | eventManagers: new WeakMap(), |
michael@0 | 479 | |
michael@0 | 480 | /** |
michael@0 | 481 | * A total number of registered eventManagers. |
michael@0 | 482 | */ |
michael@0 | 483 | listenerCount: 0, |
michael@0 | 484 | |
michael@0 | 485 | /** |
michael@0 | 486 | * An indicator of an active 'accessible-event' observer. |
michael@0 | 487 | */ |
michael@0 | 488 | started: false, |
michael@0 | 489 | |
michael@0 | 490 | /** |
michael@0 | 491 | * Start an AccessibilityEventObserver. |
michael@0 | 492 | */ |
michael@0 | 493 | start: function start() { |
michael@0 | 494 | if (this.started || this.listenerCount === 0) { |
michael@0 | 495 | return; |
michael@0 | 496 | } |
michael@0 | 497 | Services.obs.addObserver(this, 'accessible-event', false); |
michael@0 | 498 | this.started = true; |
michael@0 | 499 | }, |
michael@0 | 500 | |
michael@0 | 501 | /** |
michael@0 | 502 | * Stop an AccessibilityEventObserver. |
michael@0 | 503 | */ |
michael@0 | 504 | stop: function stop() { |
michael@0 | 505 | if (!this.started) { |
michael@0 | 506 | return; |
michael@0 | 507 | } |
michael@0 | 508 | Services.obs.removeObserver(this, 'accessible-event'); |
michael@0 | 509 | // Clean up all registered event managers. |
michael@0 | 510 | this.eventManagers.clear(); |
michael@0 | 511 | this.listenerCount = 0; |
michael@0 | 512 | this.started = false; |
michael@0 | 513 | }, |
michael@0 | 514 | |
michael@0 | 515 | /** |
michael@0 | 516 | * Register an EventManager and start listening to the |
michael@0 | 517 | * 'accessible-event' messages. |
michael@0 | 518 | * |
michael@0 | 519 | * @param aEventManager EventManager |
michael@0 | 520 | * An EventManager object that was loaded into the specific content. |
michael@0 | 521 | */ |
michael@0 | 522 | addListener: function addListener(aEventManager) { |
michael@0 | 523 | let content = aEventManager.contentScope.content; |
michael@0 | 524 | if (!this.eventManagers.has(content)) { |
michael@0 | 525 | this.listenerCount++; |
michael@0 | 526 | } |
michael@0 | 527 | this.eventManagers.set(content, aEventManager); |
michael@0 | 528 | // Since at least one EventManager was registered, start listening. |
michael@0 | 529 | Logger.debug('AccessibilityEventObserver.addListener. Total:', |
michael@0 | 530 | this.listenerCount); |
michael@0 | 531 | this.start(); |
michael@0 | 532 | }, |
michael@0 | 533 | |
michael@0 | 534 | /** |
michael@0 | 535 | * Unregister an EventManager and, optionally, stop listening to the |
michael@0 | 536 | * 'accessible-event' messages. |
michael@0 | 537 | * |
michael@0 | 538 | * @param aEventManager EventManager |
michael@0 | 539 | * An EventManager object that was stopped in the specific content. |
michael@0 | 540 | */ |
michael@0 | 541 | removeListener: function removeListener(aEventManager) { |
michael@0 | 542 | let content = aEventManager.contentScope.content; |
michael@0 | 543 | if (!this.eventManagers.delete(content)) { |
michael@0 | 544 | return; |
michael@0 | 545 | } |
michael@0 | 546 | this.listenerCount--; |
michael@0 | 547 | Logger.debug('AccessibilityEventObserver.removeListener. Total:', |
michael@0 | 548 | this.listenerCount); |
michael@0 | 549 | if (this.listenerCount === 0) { |
michael@0 | 550 | // If there are no EventManagers registered at the moment, stop listening |
michael@0 | 551 | // to the 'accessible-event' messages. |
michael@0 | 552 | this.stop(); |
michael@0 | 553 | } |
michael@0 | 554 | }, |
michael@0 | 555 | |
michael@0 | 556 | /** |
michael@0 | 557 | * Lookup an EventManager for a specific content. If the EventManager is not |
michael@0 | 558 | * found, walk up the hierarchy of parent windows. |
michael@0 | 559 | * @param content Window |
michael@0 | 560 | * A content Window used to lookup the corresponding EventManager. |
michael@0 | 561 | */ |
michael@0 | 562 | getListener: function getListener(content) { |
michael@0 | 563 | let eventManager = this.eventManagers.get(content); |
michael@0 | 564 | if (eventManager) { |
michael@0 | 565 | return eventManager; |
michael@0 | 566 | } |
michael@0 | 567 | let parent = content.parent; |
michael@0 | 568 | if (parent === content) { |
michael@0 | 569 | // There is no parent or the parent is of a different type. |
michael@0 | 570 | return null; |
michael@0 | 571 | } |
michael@0 | 572 | return this.getListener(parent); |
michael@0 | 573 | }, |
michael@0 | 574 | |
michael@0 | 575 | /** |
michael@0 | 576 | * Handle the 'accessible-event' message. |
michael@0 | 577 | */ |
michael@0 | 578 | observe: function observe(aSubject, aTopic, aData) { |
michael@0 | 579 | if (aTopic !== 'accessible-event') { |
michael@0 | 580 | return; |
michael@0 | 581 | } |
michael@0 | 582 | let event = aSubject.QueryInterface(Ci.nsIAccessibleEvent); |
michael@0 | 583 | if (!event.accessibleDocument) { |
michael@0 | 584 | Logger.warning( |
michael@0 | 585 | 'AccessibilityEventObserver.observe: no accessible document:', |
michael@0 | 586 | Logger.eventToString(event), "accessible:", |
michael@0 | 587 | Logger.accessibleToString(event.accessible)); |
michael@0 | 588 | return; |
michael@0 | 589 | } |
michael@0 | 590 | let content = event.accessibleDocument.window; |
michael@0 | 591 | // Match the content window to its EventManager. |
michael@0 | 592 | let eventManager = this.getListener(content); |
michael@0 | 593 | if (!eventManager || !eventManager._started) { |
michael@0 | 594 | if (Utils.MozBuildApp === 'browser' && |
michael@0 | 595 | !(content instanceof Ci.nsIDOMChromeWindow)) { |
michael@0 | 596 | Logger.warning( |
michael@0 | 597 | 'AccessibilityEventObserver.observe: ignored event:', |
michael@0 | 598 | Logger.eventToString(event), "accessible:", |
michael@0 | 599 | Logger.accessibleToString(event.accessible), "document:", |
michael@0 | 600 | Logger.accessibleToString(event.accessibleDocument)); |
michael@0 | 601 | } |
michael@0 | 602 | return; |
michael@0 | 603 | } |
michael@0 | 604 | try { |
michael@0 | 605 | eventManager.handleAccEvent(event); |
michael@0 | 606 | } catch (x) { |
michael@0 | 607 | Logger.logException(x, 'Error handing accessible event'); |
michael@0 | 608 | } finally { |
michael@0 | 609 | return; |
michael@0 | 610 | } |
michael@0 | 611 | } |
michael@0 | 612 | }; |