DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (4ab60925635c)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
/* This Source Code Form is subject to the terms of the Mozilla Public
 * 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/. */

"use strict";

ChromeUtils.defineModuleGetter(
  this,
  "Services",
  "resource://gre/modules/Services.jsm"
);
ChromeUtils.defineModuleGetter(
  this,
  "Utils",
  "resource://gre/modules/accessibility/Utils.jsm"
);
ChromeUtils.defineModuleGetter(
  this,
  "Logger",
  "resource://gre/modules/accessibility/Utils.jsm"
);
ChromeUtils.defineModuleGetter(
  this,
  "Events",
  "resource://gre/modules/accessibility/Constants.jsm"
);

var EXPORTED_SYMBOLS = ["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.EventManager.prototype = {
  start: function start() {
    try {
      if (!this._started) {
        Logger.debug("EventManager.start");

        this._started = true;

        AccessibilityEventObserver.addListener(this);

        this._preDialogPosition = new WeakMap();
      }
    } catch (x) {
      Logger.logException(x, "Failed to start 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.debug("EventManager.stop");
    AccessibilityEventObserver.removeListener(this);
    try {
      this._preDialogPosition = new WeakMap();
    } catch (x) {
      // contentScope is dead.
    } finally {
      this._started = false;
    }
  },

  get contentControl() {
    return this.contentScope._jsat_contentControl;
  },

  handleAccEvent: function handleAccEvent(aEvent) {
    Logger.debug(() => {
      return [
        "A11yEvent",
        Logger.eventToString(aEvent),
        Logger.accessibleToString(aEvent.accessible),
      ];
    });

    // Don't bother with non-content events in firefox.
    if (
      Utils.MozBuildApp == "browser" &&
      aEvent.eventType != Events.VIRTUALCURSOR_CHANGED &&
      // XXX Bug 442005 results in DocAccessible::getDocType returning
      // NS_ERROR_FAILURE. Checking for aEvent.accessibleDocument.docType ==
      // 'window' does not currently work.
      (aEvent.accessibleDocument.DOMDocument.doctype &&
        aEvent.accessibleDocument.DOMDocument.doctype.name === "window")
    ) {
      return;
    }

    switch (aEvent.eventType) {
      case Events.TEXT_CARET_MOVED: {
        if (
          aEvent.accessible != aEvent.accessibleDocument &&
          !aEvent.isFromUserInput
        ) {
          // If caret moves in document without direct user
          // we are probably stepping through results in find-in-page.
          let acc = Utils.getTextLeafForOffset(
            aEvent.accessible,
            aEvent.QueryInterface(Ci.nsIAccessibleCaretMoveEvent).caretOffset
          );
          this.contentControl.autoMove(acc);
        }
        break;
      }
      case Events.NAME_CHANGE: {
        // XXX: Port to Android
        break;
      }
      case Events.SCROLLING_START: {
        this.contentControl.autoMove(aEvent.accessible);
        break;
      }
      case Events.SHOW: {
        // XXX: Port to Android
        break;
      }
      case Events.HIDE: {
        // XXX: Port to Android
        break;
      }
      case Events.VALUE_CHANGE: {
        // XXX: Port to Android
        break;
      }
    }
  },

  QueryInterface: ChromeUtils.generateQI([
    Ci.nsISupportsWeakReference,
    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");
    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 = new WeakMap();
    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;
    try {
      content = event.accessibleDocument.window;
    } catch (e) {
      Logger.warning(
        "AccessibilityEventObserver.observe: no window for accessible document:",
        Logger.eventToString(event),
        "accessible:",
        Logger.accessibleToString(event.accessible)
      );
      return;
    }
    // Match the content window to its EventManager.
    let eventManager = this.getListener(content);
    if (!eventManager || !eventManager._started) {
      if (Utils.MozBuildApp === "browser" && !content.isChromeWindow) {
        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.logException(x, "Error handing accessible event");
    }
  },
};