summaryrefslogtreecommitdiffstats
path: root/accessible/base/nsAccessibilityService.h
blob: 2c452713c2ce32d3f1359c480c5e69af1ceb932e (plain)
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */

#ifndef __nsAccessibilityService_h__
#define __nsAccessibilityService_h__

#include "mozilla/a11y/DocManager.h"
#include "mozilla/a11y/FocusManager.h"
#include "mozilla/a11y/Platform.h"
#include "mozilla/a11y/Role.h"
#include "mozilla/a11y/SelectionManager.h"
#include "mozilla/Preferences.h"

#include "nsIContent.h"
#include "nsIObserver.h"
#include "nsIAccessibleEvent.h"
#include "nsIEventListenerService.h"
#include "nsXULAppAPI.h"
#include "xpcAccessibilityService.h"

class nsImageFrame;
class nsIArray;
class nsITreeView;

namespace mozilla {

class PresShell;
class Monitor;
namespace dom {
class DOMStringList;
class Element;
}  // namespace dom

namespace a11y {

class AccAttributes;
class Accessible;
class ApplicationAccessible;
class xpcAccessibleApplication;

/**
 * Return focus manager.
 */
FocusManager* FocusMgr();

/**
 * Return selection manager.
 */
SelectionManager* SelectionMgr();

/**
 * Returns the application accessible.
 */
ApplicationAccessible* ApplicationAcc();
xpcAccessibleApplication* XPCApplicationAcc();

typedef LocalAccessible*(New_Accessible)(mozilla::dom::Element* aElement,
                                         LocalAccessible* aContext);

// These fields are not `nsStaticAtom* const` because MSVC doesn't like it.
struct MarkupAttrInfo {
  nsStaticAtom* name;
  nsStaticAtom* value;

  nsStaticAtom* DOMAttrName;
  nsStaticAtom* DOMAttrValue;
};

struct MarkupMapInfo {
  const nsStaticAtom* const tag;
  New_Accessible* new_func;
  a11y::role role;
  MarkupAttrInfo attrs[4];
};

struct XULMarkupMapInfo {
  const nsStaticAtom* const tag;
  New_Accessible* new_func;
};

/**
 * PREF_ACCESSIBILITY_FORCE_DISABLED preference change callback.
 */
void PrefChanged(const char* aPref, void* aClosure);

/**
 * Read and normalize PREF_ACCESSIBILITY_FORCE_DISABLED preference.
 */
EPlatformDisabledState ReadPlatformDisabledState();

}  // namespace a11y
}  // namespace mozilla

class nsAccessibilityService final : public mozilla::a11y::DocManager,
                                     public mozilla::a11y::FocusManager,
                                     public mozilla::a11y::SelectionManager,
                                     public nsIListenerChangeListener,
                                     public nsIObserver {
 public:
  typedef mozilla::a11y::LocalAccessible LocalAccessible;
  typedef mozilla::a11y::DocAccessible DocAccessible;

  // nsIListenerChangeListener
  NS_IMETHOD ListenersChanged(nsIArray* aEventChanges) override;

 protected:
  ~nsAccessibilityService();

 public:
  NS_DECL_ISUPPORTS_INHERITED
  NS_DECL_NSIOBSERVER

  LocalAccessible* GetRootDocumentAccessible(mozilla::PresShell* aPresShell,
                                             bool aCanCreate);

  /**
   * Adds/remove ATK root accessible for gtk+ native window to/from children
   * of the application accessible.
   */
  LocalAccessible* AddNativeRootAccessible(void* aAtkAccessible);
  void RemoveNativeRootAccessible(LocalAccessible* aRootAccessible);

  bool HasAccessible(nsINode* aDOMNode);

  /**
   * Get a string equivalent for an accessible role value.
   */
  void GetStringRole(uint32_t aRole, nsAString& aString);

  /**
   * Get a string equivalent for an accessible state/extra state.
   */
  already_AddRefed<mozilla::dom::DOMStringList> GetStringStates(
      uint64_t aStates) const;
  void GetStringStates(uint32_t aState, uint32_t aExtraState,
                       nsISupports** aStringStates);

  /**
   * Get a string equivalent for an accessible event value.
   */
  void GetStringEventType(uint32_t aEventType, nsAString& aString);

  /**
   * Get a string equivalent for an accessible event value.
   */
  void GetStringEventType(uint32_t aEventType, nsACString& aString);

  /**
   * Get a string equivalent for an accessible relation type.
   */
  void GetStringRelationType(uint32_t aRelationType, nsAString& aString);

  // nsAccesibilityService
  /**
   * Notification used to update the accessible tree when new content is
   * inserted.
   */
  void ContentRangeInserted(mozilla::PresShell* aPresShell,
                            nsIContent* aStartChild, nsIContent* aEndChild);

  /**
   * Triggers a re-evaluation of the a11y tree of aContent after the next
   * refresh. This is important because whether we create accessibles may
   * depend on the frame tree / style.
   */
  void ScheduleAccessibilitySubtreeUpdate(mozilla::PresShell* aPresShell,
                                          nsIContent* aStartChild);

  /**
   * Notification used to update the accessible tree when content is removed.
   */
  void ContentRemoved(mozilla::PresShell* aPresShell, nsIContent* aChild);

  /**
   * Notification used to invalidate the isLayoutTable cache.
   */
  void TableLayoutGuessMaybeChanged(mozilla::PresShell* aPresShell,
                                    nsIContent* aContent);

  /**
   * Notifies when a combobox <option> text or label changes.
   */
  void ComboboxOptionMaybeChanged(mozilla::PresShell*,
                                  nsIContent* aMutatingNode);

  void UpdateText(mozilla::PresShell* aPresShell, nsIContent* aContent);

  /**
   * Update XUL:tree accessible tree when treeview is changed.
   */
  void TreeViewChanged(mozilla::PresShell* aPresShell, nsIContent* aContent,
                       nsITreeView* aView);

  /**
   * Notify of input@type="element" value change.
   */
  void RangeValueChanged(mozilla::PresShell* aPresShell, nsIContent* aContent);

  /**
   * Update the image map.
   */
  void UpdateImageMap(nsImageFrame* aImageFrame);

  /**
   * Update the label accessible tree when rendered @value is changed.
   */
  void UpdateLabelValue(mozilla::PresShell* aPresShell, nsIContent* aLabelElm,
                        const nsString& aNewValue);

  /**
   * Notify accessibility that anchor jump has been accomplished to the given
   * target. Used by layout.
   */
  void NotifyOfAnchorJumpTo(nsIContent* aTarget);

  /**
   * Notify that presshell is activated.
   */
  void PresShellActivated(mozilla::PresShell* aPresShell);

  /**
   * Recreate an accessible for the given content node in the presshell.
   */
  void RecreateAccessible(mozilla::PresShell* aPresShell, nsIContent* aContent);

  void FireAccessibleEvent(uint32_t aEvent, LocalAccessible* aTarget);

  void NotifyOfPossibleBoundsChange(mozilla::PresShell* aPresShell,
                                    nsIContent* aContent);

  void NotifyOfComputedStyleChange(mozilla::PresShell* aPresShell,
                                   nsIContent* aContent);

  void NotifyOfTabPanelVisibilityChange(mozilla::PresShell* aPresShell,
                                        mozilla::dom::Element* aPanel,
                                        bool aVisible);

  void NotifyOfResolutionChange(mozilla::PresShell* aPresShell,
                                float aResolution);

  void NotifyOfDevPixelRatioChange(mozilla::PresShell* aPresShell,
                                   int32_t aAppUnitsPerDevPixel);

  // nsAccessibiltiyService

  /**
   * Return true if accessibility service has been shutdown.
   */
  static bool IsShutdown() { return gConsumers == 0; };

  /**
   * Return true if there should be an image accessible for the given element.
   */
  static bool ShouldCreateImgAccessible(mozilla::dom::Element* aElement,
                                        DocAccessible* aDocument);

  /**
   * Creates an accessible for the given DOM node.
   *
   * @param  aNode             [in] the given node
   * @param  aContext          [in] context the accessible is created in
   * @param  aIsSubtreeHidden  [out, optional] indicates whether the node's
   *                             frame and its subtree is hidden
   */
  LocalAccessible* CreateAccessible(nsINode* aNode, LocalAccessible* aContext,
                                    bool* aIsSubtreeHidden = nullptr);

  mozilla::a11y::role MarkupRole(const nsIContent* aContent) const {
    const mozilla::a11y::MarkupMapInfo* markupMap =
        GetMarkupMapInfoFor(aContent);
    return markupMap ? markupMap->role : mozilla::a11y::roles::NOTHING;
  }

  /**
   * Return the associated value for a given attribute if
   * it appears in the MarkupMap. Otherwise, it returns null. This can be
   * called with either an nsIContent or an Accessible.
   */
  template <typename T>
  nsStaticAtom* MarkupAttribute(T aSource, nsStaticAtom* aAtom) const {
    const mozilla::a11y::MarkupMapInfo* markupMap =
        GetMarkupMapInfoFor(aSource);
    if (markupMap) {
      for (size_t i = 0; i < mozilla::ArrayLength(markupMap->attrs); i++) {
        const mozilla::a11y::MarkupAttrInfo* info = markupMap->attrs + i;
        if (info->name == aAtom) {
          return info->value;
        }
      }
    }
    return nullptr;
  }

  /**
   * Set the object attribute defined by markup for the given element.
   */
  void MarkupAttributes(mozilla::a11y::Accessible* aAcc,
                        mozilla::a11y::AccAttributes* aAttributes) const;

  /**
   * A list of possible accessibility service consumers. Accessibility service
   * can only be shut down when there are no remaining consumers.
   *
   * eXPCOM       - accessibility service is used by XPCOM.
   *
   * eMainProcess - accessibility service was started by main process in the
   *                content process.
   *
   * ePlatformAPI - accessibility service is used by the platform api in the
   *                main process.
   */
  enum ServiceConsumer {
    eXPCOM = 1 << 0,
    eMainProcess = 1 << 1,
    ePlatformAPI = 1 << 2,
  };

#if defined(ANDROID)
  static mozilla::Monitor& GetAndroidMonitor();
#endif

 private:
  // nsAccessibilityService creation is controlled by friend
  // GetOrCreateAccService, keep constructors private.
  nsAccessibilityService();
  nsAccessibilityService(const nsAccessibilityService&);
  nsAccessibilityService& operator=(const nsAccessibilityService&);

 private:
  /**
   * Initialize accessibility service.
   */
  bool Init();

  /**
   * Shutdowns accessibility service.
   */
  void Shutdown();

  /**
   * Create an accessible whose type depends on the given frame.
   */
  already_AddRefed<LocalAccessible> CreateAccessibleByFrameType(
      nsIFrame* aFrame, nsIContent* aContent, LocalAccessible* aContext);

  /**
   * Notify observers about change of the accessibility service's consumers.
   */
  void NotifyOfConsumersChange();

  /**
   * Get a JSON string representing the accessibility service consumers.
   */
  void GetConsumers(nsAString& aString);

  /**
   * Set accessibility service consumers.
   */
  void SetConsumers(uint32_t aConsumers, bool aNotify = true);

  /**
   * Unset accessibility service consumers.
   */
  void UnsetConsumers(uint32_t aConsumers);

  /**
   * Reference for accessibility service instance.
   */
  static nsAccessibilityService* gAccessibilityService;

  /**
   * Reference for application accessible instance.
   */
  static mozilla::a11y::ApplicationAccessible* gApplicationAccessible;
  static mozilla::a11y::xpcAccessibleApplication* gXPCApplicationAccessible;

  /**
   * Contains a set of accessibility service consumers.
   */
  static uint32_t gConsumers;

  using MarkupMap = nsTHashMap<nsPtrHashKey<const nsAtom>,
                               const mozilla::a11y::MarkupMapInfo*>;
  MarkupMap mHTMLMarkupMap;
  MarkupMap mMathMLMarkupMap;

  const mozilla::a11y::MarkupMapInfo* GetMarkupMapInfoFor(
      const nsIContent* aContent) const {
    if (aContent->IsHTMLElement()) {
      return mHTMLMarkupMap.Get(aContent->NodeInfo()->NameAtom());
    }
    if (aContent->IsMathMLElement()) {
      return mMathMLMarkupMap.Get(aContent->NodeInfo()->NameAtom());
    }
    // This function can be called by MarkupAttribute, etc. which might in turn
    // be called on a XUL, SVG, etc. element. For example, this can happen
    // with nsAccUtils::SetLiveContainerAttributes.
    return nullptr;
  }

  const mozilla::a11y::MarkupMapInfo* GetMarkupMapInfoFor(
      mozilla::a11y::Accessible* aAcc) const;

  nsTHashMap<nsPtrHashKey<const nsAtom>, const mozilla::a11y::XULMarkupMapInfo*>
      mXULMarkupMap;

  friend nsAccessibilityService* GetAccService();
  friend nsAccessibilityService* GetOrCreateAccService(uint32_t);
  friend void MaybeShutdownAccService(uint32_t);
  friend void mozilla::a11y::PrefChanged(const char*, void*);
  friend mozilla::a11y::FocusManager* mozilla::a11y::FocusMgr();
  friend mozilla::a11y::SelectionManager* mozilla::a11y::SelectionMgr();
  friend mozilla::a11y::ApplicationAccessible* mozilla::a11y::ApplicationAcc();
  friend mozilla::a11y::xpcAccessibleApplication*
  mozilla::a11y::XPCApplicationAcc();
  friend class xpcAccessibilityService;
};

/**
 * Return the accessibility service instance. (Handy global function)
 */
inline nsAccessibilityService* GetAccService() {
  return nsAccessibilityService::gAccessibilityService;
}

/**
 * Return accessibility service instance; creating one if necessary.
 */
nsAccessibilityService* GetOrCreateAccService(
    uint32_t aNewConsumer = nsAccessibilityService::ePlatformAPI);

/**
 * Shutdown accessibility service if needed.
 */
void MaybeShutdownAccService(uint32_t aFormerConsumer);

/**
 * Return true if we're in a content process and not B2G.
 */
inline bool IPCAccessibilityActive() { return XRE_IsContentProcess(); }

/**
 * Map nsIAccessibleEvents constants to strings. Used by
 * nsAccessibilityService::GetStringEventType() method.
 */
static const char kEventTypeNames[][40] = {
    "unknown",                           //
    "show",                              // EVENT_SHOW
    "hide",                              // EVENT_HIDE
    "reorder",                           // EVENT_REORDER
    "active decendent change",           // EVENT_ACTIVE_DECENDENT_CHANGED
    "focus",                             // EVENT_FOCUS
    "state change",                      // EVENT_STATE_CHANGE
    "location change",                   // EVENT_LOCATION_CHANGE
    "name changed",                      // EVENT_NAME_CHANGE
    "description change",                // EVENT_DESCRIPTION_CHANGE
    "value change",                      // EVENT_VALUE_CHANGE
    "help change",                       // EVENT_HELP_CHANGE
    "default action change",             // EVENT_DEFACTION_CHANGE
    "action change",                     // EVENT_ACTION_CHANGE
    "accelerator change",                // EVENT_ACCELERATOR_CHANGE
    "selection",                         // EVENT_SELECTION
    "selection add",                     // EVENT_SELECTION_ADD
    "selection remove",                  // EVENT_SELECTION_REMOVE
    "selection within",                  // EVENT_SELECTION_WITHIN
    "alert",                             // EVENT_ALERT
    "foreground",                        // EVENT_FOREGROUND
    "menu start",                        // EVENT_MENU_START
    "menu end",                          // EVENT_MENU_END
    "menupopup start",                   // EVENT_MENUPOPUP_START
    "menupopup end",                     // EVENT_MENUPOPUP_END
    "capture start",                     // EVENT_CAPTURE_START
    "capture end",                       // EVENT_CAPTURE_END
    "movesize start",                    // EVENT_MOVESIZE_START
    "movesize end",                      // EVENT_MOVESIZE_END
    "contexthelp start",                 // EVENT_CONTEXTHELP_START
    "contexthelp end",                   // EVENT_CONTEXTHELP_END
    "dragdrop start",                    // EVENT_DRAGDROP_START
    "dragdrop end",                      // EVENT_DRAGDROP_END
    "dialog start",                      // EVENT_DIALOG_START
    "dialog end",                        // EVENT_DIALOG_END
    "scrolling start",                   // EVENT_SCROLLING_START
    "scrolling end",                     // EVENT_SCROLLING_END
    "minimize start",                    // EVENT_MINIMIZE_START
    "minimize end",                      // EVENT_MINIMIZE_END
    "document load complete",            // EVENT_DOCUMENT_LOAD_COMPLETE
    "document reload",                   // EVENT_DOCUMENT_RELOAD
    "document load stopped",             // EVENT_DOCUMENT_LOAD_STOPPED
    "document attributes changed",       // EVENT_DOCUMENT_ATTRIBUTES_CHANGED
    "document content changed",          // EVENT_DOCUMENT_CONTENT_CHANGED
    "property changed",                  // EVENT_PROPERTY_CHANGED
    "page changed",                      // EVENT_PAGE_CHANGED
    "text attribute changed",            // EVENT_TEXT_ATTRIBUTE_CHANGED
    "text caret moved",                  // EVENT_TEXT_CARET_MOVED
    "text changed",                      // EVENT_TEXT_CHANGED
    "text inserted",                     // EVENT_TEXT_INSERTED
    "text removed",                      // EVENT_TEXT_REMOVED
    "text updated",                      // EVENT_TEXT_UPDATED
    "text selection changed",            // EVENT_TEXT_SELECTION_CHANGED
    "visible data changed",              // EVENT_VISIBLE_DATA_CHANGED
    "text column changed",               // EVENT_TEXT_COLUMN_CHANGED
    "section changed",                   // EVENT_SECTION_CHANGED
    "table caption changed",             // EVENT_TABLE_CAPTION_CHANGED
    "table model changed",               // EVENT_TABLE_MODEL_CHANGED
    "table summary changed",             // EVENT_TABLE_SUMMARY_CHANGED
    "table row description changed",     // EVENT_TABLE_ROW_DESCRIPTION_CHANGED
    "table row header changed",          // EVENT_TABLE_ROW_HEADER_CHANGED
    "table row insert",                  // EVENT_TABLE_ROW_INSERT
    "table row delete",                  // EVENT_TABLE_ROW_DELETE
    "table row reorder",                 // EVENT_TABLE_ROW_REORDER
    "table column description changed",  // EVENT_TABLE_COLUMN_DESCRIPTION_CHANGED
    "table column header changed",       // EVENT_TABLE_COLUMN_HEADER_CHANGED
    "table column insert",               // EVENT_TABLE_COLUMN_INSERT
    "table column delete",               // EVENT_TABLE_COLUMN_DELETE
    "table column reorder",              // EVENT_TABLE_COLUMN_REORDER
    "window activate",                   // EVENT_WINDOW_ACTIVATE
    "window create",                     // EVENT_WINDOW_CREATE
    "window deactivate",                 // EVENT_WINDOW_DEACTIVATE
    "window destroy",                    // EVENT_WINDOW_DESTROY
    "window maximize",                   // EVENT_WINDOW_MAXIMIZE
    "window minimize",                   // EVENT_WINDOW_MINIMIZE
    "window resize",                     // EVENT_WINDOW_RESIZE
    "window restore",                    // EVENT_WINDOW_RESTORE
    "hyperlink end index changed",       // EVENT_HYPERLINK_END_INDEX_CHANGED
    "hyperlink number of anchors changed",  // EVENT_HYPERLINK_NUMBER_OF_ANCHORS_CHANGED
    "hyperlink selected link changed",  // EVENT_HYPERLINK_SELECTED_LINK_CHANGED
    "hypertext link activated",         // EVENT_HYPERTEXT_LINK_ACTIVATED
    "hypertext link selected",          // EVENT_HYPERTEXT_LINK_SELECTED
    "hyperlink start index changed",    // EVENT_HYPERLINK_START_INDEX_CHANGED
    "hypertext changed",                // EVENT_HYPERTEXT_CHANGED
    "hypertext links count changed",    // EVENT_HYPERTEXT_NLINKS_CHANGED
    "object attribute changed",         // EVENT_OBJECT_ATTRIBUTE_CHANGED
    "virtual cursor changed",           // EVENT_VIRTUALCURSOR_CHANGED
    "text value change",                // EVENT_TEXT_VALUE_CHANGE
    "scrolling",                        // EVENT_SCROLLING
    "announcement",                     // EVENT_ANNOUNCEMENT
    "live region added",                // EVENT_LIVE_REGION_ADDED
    "live region removed",              // EVENT_LIVE_REGION_REMOVED
    "table styling changed",            // EVENT_TABLE_STYLING_CHANGED
    "inner reorder",                    // EVENT_INNER_REORDER
};

#endif