Bug 857946 - [AccessFu] enabling accessible content after closing all tabs. Adding a per process Accessibility event observer. r=eeejay

This commit is contained in:
Yura Zenevich 2013-05-21 14:16:49 -04:00
parent df4472c441
commit 14261d72c9
5 changed files with 255 additions and 78 deletions

View File

@ -112,10 +112,12 @@ this.AccessFu = {
TouchAdapter.start();
Services.obs.addObserver(this, 'remote-browser-frame-shown', false);
Services.obs.addObserver(this, 'in-process-browser-or-app-frame-shown', false);
Services.obs.addObserver(this, 'Accessibility:NextObject', false);
Services.obs.addObserver(this, 'Accessibility:PreviousObject', false);
Services.obs.addObserver(this, 'Accessibility:Focus', false);
Utils.win.addEventListener('TabOpen', this);
Utils.win.addEventListener('TabClose', this);
Utils.win.addEventListener('TabSelect', this);
if (this.readyCallback) {
@ -147,9 +149,11 @@ this.AccessFu = {
TouchAdapter.stop();
Utils.win.removeEventListener('TabOpen', this);
Utils.win.removeEventListener('TabClose', this);
Utils.win.removeEventListener('TabSelect', this);
Services.obs.removeObserver(this, 'remote-browser-frame-shown');
Services.obs.removeObserver(this, 'in-process-browser-or-app-frame-shown');
Services.obs.removeObserver(this, 'Accessibility:NextObject');
Services.obs.removeObserver(this, 'Accessibility:PreviousObject');
Services.obs.removeObserver(this, 'Accessibility:Focus');
@ -280,6 +284,7 @@ this.AccessFu = {
}
break;
case 'remote-browser-frame-shown':
case 'in-process-browser-or-app-frame-shown':
{
let mm = aSubject.QueryInterface(Ci.nsIFrameLoader).messageManager;
this._handleMessageManager(mm);
@ -310,6 +315,16 @@ this.AccessFu = {
this._handleMessageManager(mm);
break;
}
case 'TabClose':
{
let mm = Utils.getMessageManager(aEvent.target);
let mmIndex = this._processedMessageManagers.indexOf(mm);
if (mmIndex > -1) {
this._removeMessageListeners(mm);
this._processedMessageManagers.splice(mmIndex, 1);
}
break;
}
case 'TabSelect':
{
if (this._focused) {

View File

@ -2,33 +2,58 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
var Cc = Components.classes;
var Ci = Components.interfaces;
var Cu = Components.utils;
var Cr = Components.results;
'use strict';
const Ci = Components.interfaces;
const Cu = Components.utils;
Cu.import('resource://gre/modules/accessibility/Utils.jsm');
Cu.import('resource://gre/modules/accessibility/Presentation.jsm');
Cu.import('resource://gre/modules/accessibility/TraversalRules.jsm');
Cu.import('resource://gre/modules/Services.jsm');
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
XPCOMUtils.defineLazyModuleGetter(this, 'Services',
'resource://gre/modules/Services.jsm');
XPCOMUtils.defineLazyModuleGetter(this, 'Utils',
'resource://gre/modules/accessibility/Utils.jsm');
XPCOMUtils.defineLazyModuleGetter(this, 'Logger',
'resource://gre/modules/accessibility/Utils.jsm');
XPCOMUtils.defineLazyModuleGetter(this, 'Presentation',
'resource://gre/modules/accessibility/Presentation.jsm');
XPCOMUtils.defineLazyModuleGetter(this, 'TraversalRules',
'resource://gre/modules/accessibility/TraversalRules.jsm');
this.EXPORTED_SYMBOLS = ['EventManager'];
this.EventManager = {
this.EventManager = function EventManager(aContentScope) {
this.contentScope = aContentScope;
this.addEventListener = this.contentScope.addEventListener.bind(
this.contentScope);
this.removeEventListener = this.contentScope.removeEventListener.bind(
this.contentScope);
this.sendMsgFunc = this.contentScope.sendAsyncMessage.bind(
this.contentScope);
this.webProgress = this.contentScope.docShell.
QueryInterface(Ci.nsIInterfaceRequestor).
getInterface(Ci.nsIWebProgress);
};
this.EventManager.prototype = {
editState: {},
start: function start(aSendMsgFunc) {
start: function start() {
try {
if (!this._started) {
this.sendMsgFunc = aSendMsgFunc || function() {};
Logger.info('EventManager.start', Utils.MozBuildApp);
this._started = true;
Services.obs.addObserver(this, 'accessible-event', false);
}
AccessibilityEventObserver.addListener(this);
this.webProgress.addProgressListener(this,
(Ci.nsIWebProgress.NOTIFY_STATE_ALL |
Ci.nsIWebProgress.NOTIFY_LOCATION));
this.addEventListener('scroll', this, true);
this.addEventListener('resize', this, true);
// XXX: Ideally this would be an a11y event. Bug #742280.
this.addEventListener('DOMActivate', this, true);
}
this.present(Presentation.tabStateChanged(null, 'newtab'));
} catch (x) {
@ -37,13 +62,25 @@ this.EventManager = {
}
},
// XXX: Stop is not called when the tab is closed (|TabClose| event is too
// late). It is only called when the AccessFu is disabled explicitly.
stop: function stop() {
if (!this._started) {
return;
}
Logger.info('EventManager.stop', Utils.MozBuildApp);
Services.obs.removeObserver(this, 'accessible-event');
this._started = false;
AccessibilityEventObserver.removeListener(this);
try {
this.webProgress.removeProgressListener(this);
this.removeEventListener('scroll', this, true);
this.removeEventListener('resize', this, true);
// XXX: Ideally this would be an a11y event. Bug #742280.
this.removeEventListener('DOMActivate', this, true);
} catch (x) {
// contentScope is dead.
} finally {
this._started = false;
}
},
handleEvent: function handleEvent(aEvent) {
@ -85,21 +122,6 @@ this.EventManager = {
}
},
observe: function observe(aSubject, aTopic, aData) {
switch (aTopic) {
case 'accessible-event':
var event;
try {
event = aSubject.QueryInterface(Ci.nsIAccessibleEvent);
this.handleAccEvent(event);
} catch (x) {
Logger.error('Error handing accessible event');
Logger.logException(x);
return;
}
}
},
handleAccEvent: function handleAccEvent(aEvent) {
if (Logger.logLevel >= Logger.DEBUG)
Logger.debug('A11yEvent', Logger.eventToString(aEvent),
@ -227,11 +249,6 @@ this.EventManager = {
this.sendMsgFunc("AccessFu:Present", aPresentationData);
},
presentVirtualCursorPosition: function presentVirtualCursorPosition(aVirtualCursor) {
this.present(Presentation.pivotChanged(aVirtualCursor.position, null,
Ci.nsIAccessiblePivot.REASON_NONE));
},
onStateChange: function onStateChange(aWebProgress, aRequest, aStateFlags, aStatus) {
let tabstate = '';
@ -269,3 +286,145 @@ this.EventManager = {
Ci.nsISupports,
Ci.nsIObserver])
};
const AccessibilityEventObserver = {
/**
* A WeakMap containing [content, EventManager] pairs.
*/
eventManagers: new WeakMap(),
/**
* A total number of registered eventManagers.
*/
listenerCount: 0,
/**
* An indicator of an active 'accessible-event' observer.
*/
started: false,
/**
* Start an AccessibilityEventObserver.
*/
start: function start() {
if (this.started || this.listenerCount === 0) {
return;
}
Services.obs.addObserver(this, 'accessible-event', false);
this.started = true;
},
/**
* Stop an AccessibilityEventObserver.
*/
stop: function stop() {
if (!this.started) {
return;
}
Services.obs.removeObserver(this, 'accessible-event');
// Clean up all registered event managers.
this.eventManagers.clear();
this.listenerCount = 0;
this.started = false;
},
/**
* Register an EventManager and start listening to the
* 'accessible-event' messages.
*
* @param aEventManager EventManager
* An EventManager object that was loaded into the specific content.
*/
addListener: function addListener(aEventManager) {
let content = aEventManager.contentScope.content;
if (!this.eventManagers.has(content)) {
this.listenerCount++;
}
this.eventManagers.set(content, aEventManager);
// Since at least one EventManager was registered, start listening.
Logger.debug('AccessibilityEventObserver.addListener. Total:',
this.listenerCount);
this.start();
},
/**
* Unregister an EventManager and, optionally, stop listening to the
* 'accessible-event' messages.
*
* @param aEventManager EventManager
* An EventManager object that was stopped in the specific content.
*/
removeListener: function removeListener(aEventManager) {
let content = aEventManager.contentScope.content;
if (!this.eventManagers.delete(content)) {
return;
}
this.listenerCount--;
Logger.debug('AccessibilityEventObserver.removeListener. Total:',
this.listenerCount);
if (this.listenerCount === 0) {
// If there are no EventManagers registered at the moment, stop listening
// to the 'accessible-event' messages.
this.stop();
}
},
/**
* Lookup an EventManager for a specific content. If the EventManager is not
* found, walk up the hierarchy of parent windows.
* @param content Window
* A content Window used to lookup the corresponding EventManager.
*/
getListener: function getListener(content) {
let eventManager = this.eventManagers.get(content);
if (eventManager) {
return eventManager;
}
let parent = content.parent;
if (parent === content) {
// There is no parent or the parent is of a different type.
return null;
}
return this.getListener(parent);
},
/**
* Handle the 'accessible-event' message.
*/
observe: function observe(aSubject, aTopic, aData) {
if (aTopic !== 'accessible-event') {
return;
}
let event = aSubject.QueryInterface(Ci.nsIAccessibleEvent);
if (!event.accessibleDocument) {
Logger.warning(
'AccessibilityEventObserver.observe: no accessible document:',
Logger.eventToString(event), "accessible:",
Logger.accessibleToString(event.accessible));
return;
}
let content = event.accessibleDocument.window;
// Match the content window to its EventManager.
let eventManager = this.getListener(content);
if (!eventManager || !eventManager._started) {
if (Utils.MozBuildApp === 'browser' &&
!(content instanceof Ci.nsIDOMChromeWindow)) {
Logger.warning(
'AccessibilityEventObserver.observe: ignored event:',
Logger.eventToString(event), "accessible:",
Logger.accessibleToString(event.accessible), "document:",
Logger.accessibleToString(event.accessibleDocument));
}
return;
}
try {
eventManager.handleAccEvent(event);
} catch (x) {
Logger.error('Error handing accessible event');
Logger.logException(x);
} finally {
return;
}
}
};

View File

@ -37,6 +37,9 @@ this.Utils = {
},
get win() {
if (!this._win) {
return null;
}
return this._win.get();
},
@ -90,6 +93,9 @@ this.Utils = {
},
get BrowserApp() {
if (!this.win) {
return null;
}
switch (this.MozBuildApp) {
case 'mobile/android':
return this.win.BrowserApp;
@ -103,6 +109,9 @@ this.Utils = {
},
get CurrentBrowser() {
if (!this.BrowserApp) {
return null;
}
if (this.MozBuildApp == 'b2g')
return this.BrowserApp.contentBrowser;
return this.BrowserApp.selectedBrowser;
@ -228,9 +237,10 @@ this.Logger = {
logException: function logException(aException) {
try {
this.error(
aException.message,
'(' + aException.fileName + ':' + aException.lineNumber + ')');
let args = [aException.message];
args.push.apply(args, aException.stack ? ['\n', aException.stack] :
['(' + aException.fileName + ':' + aException.lineNumber + ')']);
this.error.apply(this, args);
} catch (x) {
this.error(x);
}

View File

@ -2,18 +2,25 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
var Cc = Components.classes;
var Ci = Components.interfaces;
var Cu = Components.utils;
var Cr = Components.results;
let Ci = Components.interfaces;
let Cu = Components.utils;
Cu.import('resource://gre/modules/accessibility/Utils.jsm');
Cu.import('resource://gre/modules/accessibility/EventManager.jsm');
Cu.import('resource://gre/modules/accessibility/TraversalRules.jsm');
Cu.import('resource://gre/modules/Services.jsm');
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
XPCOMUtils.defineLazyModuleGetter(this, 'Logger',
'resource://gre/modules/accessibility/Utils.jsm');
XPCOMUtils.defineLazyModuleGetter(this, 'Presentation',
'resource://gre/modules/accessibility/Presentation.jsm');
XPCOMUtils.defineLazyModuleGetter(this, 'TraversalRules',
'resource://gre/modules/accessibility/TraversalRules.jsm');
XPCOMUtils.defineLazyModuleGetter(this, 'Utils',
'resource://gre/modules/accessibility/Utils.jsm');
XPCOMUtils.defineLazyModuleGetter(this, 'EventManager',
'resource://gre/modules/accessibility/EventManager.jsm');
Logger.debug('content-script.js');
let eventManager = null;
function virtualCursorControl(aMessage) {
if (Logger.logLevel >= Logger.DEBUG)
Logger.debug(aMessage.name, JSON.stringify(aMessage.json));
@ -58,8 +65,10 @@ function virtualCursorControl(aMessage) {
if (!forwardMessage(vc, aMessage)) {
if (!vc.position && aMessage.json.move)
vc.moveFirst(TraversalRules.Simple);
else
EventManager.presentVirtualCursorPosition(vc);
else {
sendAsyncMessage('AccessFu:Present', Presentation.pivotChanged(
vc.position, null, Ci.nsIAccessiblePivot.REASON_NONE));
}
}
break;
@ -124,14 +133,14 @@ function activateCurrent(aMessage) {
let node = aAccessible.DOMNode || aAccessible.parent.DOMNode;
function dispatchMouseEvent(aEventType) {
let evt = content.document.createEvent("MouseEvents");
let evt = content.document.createEvent('MouseEvents');
evt.initMouseEvent(aEventType, true, true, content,
x, y, 0, 0, 0, false, false, false, false, 0, null);
node.dispatchEvent(evt);
}
dispatchMouseEvent("mousedown");
dispatchMouseEvent("mouseup");
dispatchMouseEvent('mousedown');
dispatchMouseEvent('mouseup');
}
}
@ -234,20 +243,10 @@ addMessageListener(
addMessageListener('AccessFu:Activate', activateCurrent);
addMessageListener('AccessFu:Scroll', scroll);
EventManager.start(
function sendMessage(aName, aDetails) {
sendAsyncMessage(aName, aDetails);
});
docShell.QueryInterface(Ci.nsIInterfaceRequestor).
getInterface(Ci.nsIWebProgress).
addProgressListener(EventManager,
(Ci.nsIWebProgress.NOTIFY_STATE_ALL |
Ci.nsIWebProgress.NOTIFY_LOCATION));
addEventListener('scroll', EventManager, true);
addEventListener('resize', EventManager, true);
// XXX: Ideally this would be an a11y event. Bug #742280.
addEventListener('DOMActivate', EventManager, true);
if (!eventManager) {
eventManager = new EventManager(this);
}
eventManager.start();
});
addMessageListener(
@ -259,15 +258,7 @@ addMessageListener(
removeMessageListener('AccessFu:Activate', activateCurrent);
removeMessageListener('AccessFu:Scroll', scroll);
EventManager.stop();
docShell.QueryInterface(Ci.nsIInterfaceRequestor).
getInterface(Ci.nsIWebProgress).
removeProgressListener(EventManager);
removeEventListener('scroll', EventManager, true);
removeEventListener('resize', EventManager, true);
// XXX: Ideally this would be an a11y event. Bug #742280.
removeEventListener('DOMActivate', EventManager, true);
eventManager.stop();
});
sendAsyncMessage('AccessFu:Ready');

View File

@ -38,21 +38,23 @@
// Firs listen for initial 'EventManager.start' and disable AccessFu.
var initialStartListener = makeEventManagerListener("EventManager.start",
function () {
ok(EventManager._started, "EventManager was started.");
ok(true, "EventManager was started.");
Services.console.registerListener(stopListener);
AccessFu._disable();
});
// Listen for 'EventManager.stop' and enable AccessFu again.
var stopListener = makeEventManagerListener("EventManager.stop",
function () {
isnot(EventManager._started, true, "EventManager was stopped.");
ok(true, "EventManager was stopped.");
isnot(AccessFu._enabled, true, "AccessFu was disabled.");
Services.console.registerListener(finalStartListener);
AccessFu._enable();
});
// Make sure EventManager is started again.
var finalStartListener = makeEventManagerListener("EventManager.start",
function () {
ok(EventManager._started, "EventManager was started again.");
ok(true, "EventManager was started again.");
ok(AccessFu._enabled, "AccessFu was enabled again.");
AccessFuTest.finish();
});