Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/accessible/generic/DocAccessible.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "Accessible-inl.h"
8
#include "AccIterator.h"
9
#include "DocAccessible-inl.h"
10
#include "DocAccessibleChild.h"
11
#include "HTMLImageMapAccessible.h"
12
#include "nsAccCache.h"
13
#include "nsAccessiblePivot.h"
14
#include "nsAccUtils.h"
15
#include "nsEventShell.h"
16
#include "nsTextEquivUtils.h"
17
#include "Role.h"
18
#include "RootAccessible.h"
19
#include "TreeWalker.h"
20
#include "xpcAccessibleDocument.h"
21
22
#include "nsContentUtils.h"
23
#include "nsIMutableArray.h"
24
#include "nsICommandManager.h"
25
#include "nsIDocShell.h"
26
#include "nsIDocument.h"
27
#include "nsPIDOMWindow.h"
28
#include "nsIEditingSession.h"
29
#include "nsIFrame.h"
30
#include "nsIInterfaceRequestorUtils.h"
31
#include "nsImageFrame.h"
32
#include "nsIPersistentProperties2.h"
33
#include "nsIPresShell.h"
34
#include "nsIServiceManager.h"
35
#include "nsViewManager.h"
36
#include "nsIScrollableFrame.h"
37
#include "nsUnicharUtils.h"
38
#include "nsIURI.h"
39
#include "nsIWebNavigation.h"
40
#include "nsFocusManager.h"
41
#include "mozilla/ArrayUtils.h"
42
#include "mozilla/Assertions.h"
43
#include "mozilla/EventStateManager.h"
44
#include "mozilla/EventStates.h"
45
#include "mozilla/HTMLEditor.h"
46
#include "mozilla/TextEditor.h"
47
#include "mozilla/dom/TabChild.h"
48
#include "mozilla/dom/DocumentType.h"
49
#include "mozilla/dom/Element.h"
50
#include "mozilla/dom/MutationEventBinding.h"
51
52
using namespace mozilla;
53
using namespace mozilla::a11y;
54
55
////////////////////////////////////////////////////////////////////////////////
56
// Static member initialization
57
58
static nsStaticAtom** kRelationAttrs[] =
59
{
60
  &nsGkAtoms::aria_labelledby,
61
  &nsGkAtoms::aria_describedby,
62
  &nsGkAtoms::aria_details,
63
  &nsGkAtoms::aria_owns,
64
  &nsGkAtoms::aria_controls,
65
  &nsGkAtoms::aria_flowto,
66
  &nsGkAtoms::aria_errormessage,
67
  &nsGkAtoms::_for,
68
  &nsGkAtoms::control
69
};
70
71
static const uint32_t kRelationAttrsLen = ArrayLength(kRelationAttrs);
72
73
////////////////////////////////////////////////////////////////////////////////
74
// Constructor/desctructor
75
76
DocAccessible::
77
  DocAccessible(nsIDocument* aDocument, nsIPresShell* aPresShell) :
78
    // XXX don't pass a document to the Accessible constructor so that we don't
79
    // set mDoc until our vtable is fully setup.  If we set mDoc before setting
80
    // up the vtable we will call Accessible::AddRef() but not the overrides of
81
    // it for subclasses.  It is important to call those overrides to avoid
82
    // confusing leak checking machinary.
83
  HyperTextAccessibleWrap(nullptr, nullptr),
84
  // XXX aaronl should we use an algorithm for the initial cache size?
85
  mAccessibleCache(kDefaultCacheLength),
86
  mNodeToAccessibleMap(kDefaultCacheLength),
87
  mDocumentNode(aDocument),
88
  mScrollPositionChangedTicks(0),
89
  mLoadState(eTreeConstructionPending), mDocFlags(0), mLoadEventType(0),
90
  mARIAAttrOldValue{nullptr}, mVirtualCursor(nullptr),
91
  mPresShell(aPresShell), mIPCDoc(nullptr)
92
0
{
93
0
  mGenericTypes |= eDocument;
94
0
  mStateFlags |= eNotNodeMapEntry;
95
0
  mDoc = this;
96
0
97
0
  MOZ_ASSERT(mPresShell, "should have been given a pres shell");
98
0
  mPresShell->SetDocAccessible(this);
99
0
100
0
  // If this is a XUL Document, it should not implement nsHyperText
101
0
  if (mDocumentNode && mDocumentNode->IsXULDocument())
102
0
    mGenericTypes &= ~eHyperText;
103
0
}
104
105
DocAccessible::~DocAccessible()
106
0
{
107
0
  NS_ASSERTION(!mPresShell, "LastRelease was never called!?!");
108
0
}
109
110
111
////////////////////////////////////////////////////////////////////////////////
112
// nsISupports
113
114
NS_IMPL_CYCLE_COLLECTION_CLASS(DocAccessible)
115
116
0
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(DocAccessible, Accessible)
117
0
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mNotificationController)
118
0
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mVirtualCursor)
119
0
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mChildDocuments)
120
0
  for (auto iter = tmp->mDependentIDsHash.Iter(); !iter.Done(); iter.Next()) {
121
0
    AttrRelProviderArray* providers = iter.UserData();
122
0
123
0
    for (int32_t jdx = providers->Length() - 1; jdx >= 0; jdx--) {
124
0
      NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(
125
0
        cb, "content of dependent ids hash entry of document accessible");
126
0
127
0
      AttrRelProvider* provider = (*providers)[jdx];
128
0
      cb.NoteXPCOMChild(provider->mContent);
129
0
130
0
      NS_ASSERTION(provider->mContent->IsInUncomposedDoc(),
131
0
                   "Referred content is not in document!");
132
0
    }
133
0
  }
134
0
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAccessibleCache)
135
0
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAnchorJumpElm)
136
0
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mInvalidationList)
137
0
  for (auto it = tmp->mARIAOwnsHash.ConstIter(); !it.Done(); it.Next()) {
138
0
    nsTArray<RefPtr<Accessible> >* ar = it.UserData();
139
0
    for (uint32_t i = 0; i < ar->Length(); i++) {
140
0
      NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb,
141
0
                                         "mARIAOwnsHash entry item");
142
0
      cb.NoteXPCOMChild(ar->ElementAt(i));
143
0
    }
144
0
  }
145
0
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
146
147
0
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(DocAccessible, Accessible)
148
0
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mNotificationController)
149
0
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mVirtualCursor)
150
0
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mChildDocuments)
151
0
  tmp->mDependentIDsHash.Clear();
152
0
  tmp->mNodeToAccessibleMap.Clear();
153
0
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mAccessibleCache)
154
0
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mAnchorJumpElm)
155
0
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mInvalidationList)
156
0
  tmp->mARIAOwnsHash.Clear();
157
0
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
158
159
0
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DocAccessible)
160
0
  NS_INTERFACE_MAP_ENTRY(nsIDocumentObserver)
161
0
  NS_INTERFACE_MAP_ENTRY(nsIMutationObserver)
162
0
  NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
163
0
  NS_INTERFACE_MAP_ENTRY(nsIObserver)
164
0
  NS_INTERFACE_MAP_ENTRY(nsIAccessiblePivotObserver)
165
0
NS_INTERFACE_MAP_END_INHERITING(HyperTextAccessible)
166
167
NS_IMPL_ADDREF_INHERITED(DocAccessible, HyperTextAccessible)
168
NS_IMPL_RELEASE_INHERITED(DocAccessible, HyperTextAccessible)
169
170
////////////////////////////////////////////////////////////////////////////////
171
// nsIAccessible
172
173
ENameValueFlag
174
DocAccessible::Name(nsString& aName) const
175
0
{
176
0
  aName.Truncate();
177
0
178
0
  if (mParent) {
179
0
    mParent->Name(aName); // Allow owning iframe to override the name
180
0
  }
181
0
  if (aName.IsEmpty()) {
182
0
    // Allow name via aria-labelledby or title attribute
183
0
    Accessible::Name(aName);
184
0
  }
185
0
  if (aName.IsEmpty()) {
186
0
    Title(aName); // Try title element
187
0
  }
188
0
  if (aName.IsEmpty()) {   // Last resort: use URL
189
0
    URL(aName);
190
0
  }
191
0
192
0
  return eNameOK;
193
0
}
194
195
// Accessible public method
196
role
197
DocAccessible::NativeRole() const
198
0
{
199
0
  nsCOMPtr<nsIDocShell> docShell = nsCoreUtils::GetDocShellFor(mDocumentNode);
200
0
  if (docShell) {
201
0
    nsCOMPtr<nsIDocShellTreeItem> sameTypeRoot;
202
0
    docShell->GetSameTypeRootTreeItem(getter_AddRefs(sameTypeRoot));
203
0
    int32_t itemType = docShell->ItemType();
204
0
    if (sameTypeRoot == docShell) {
205
0
      // Root of content or chrome tree
206
0
      if (itemType == nsIDocShellTreeItem::typeChrome)
207
0
        return roles::CHROME_WINDOW;
208
0
209
0
      if (itemType == nsIDocShellTreeItem::typeContent) {
210
0
#ifdef MOZ_XUL
211
0
        if (mDocumentNode && mDocumentNode->IsXULDocument())
212
0
          return roles::APPLICATION;
213
0
#endif
214
0
        return roles::DOCUMENT;
215
0
      }
216
0
    }
217
0
    else if (itemType == nsIDocShellTreeItem::typeContent) {
218
0
      return roles::DOCUMENT;
219
0
    }
220
0
  }
221
0
222
0
  return roles::PANE; // Fall back;
223
0
}
224
225
void
226
DocAccessible::Description(nsString& aDescription)
227
0
{
228
0
  if (mParent)
229
0
    mParent->Description(aDescription);
230
0
231
0
  if (HasOwnContent() && aDescription.IsEmpty()) {
232
0
    nsTextEquivUtils::
233
0
      GetTextEquivFromIDRefs(this, nsGkAtoms::aria_describedby,
234
0
                             aDescription);
235
0
  }
236
0
}
237
238
// Accessible public method
239
uint64_t
240
DocAccessible::NativeState() const
241
0
{
242
0
  // Document is always focusable.
243
0
  uint64_t state = states::FOCUSABLE; // keep in sync with NativeInteractiveState() impl
244
0
  if (FocusMgr()->IsFocused(this))
245
0
    state |= states::FOCUSED;
246
0
247
0
  // Expose stale state until the document is ready (DOM is loaded and tree is
248
0
  // constructed).
249
0
  if (!HasLoadState(eReady))
250
0
    state |= states::STALE;
251
0
252
0
  // Expose state busy until the document and all its subdocuments is completely
253
0
  // loaded.
254
0
  if (!HasLoadState(eCompletelyLoaded))
255
0
    state |= states::BUSY;
256
0
257
0
  nsIFrame* frame = GetFrame();
258
0
  if (!frame ||
259
0
      !frame->IsVisibleConsideringAncestors(nsIFrame::VISIBILITY_CROSS_CHROME_CONTENT_BOUNDARY)) {
260
0
    state |= states::INVISIBLE | states::OFFSCREEN;
261
0
  }
262
0
263
0
  RefPtr<TextEditor> textEditor = GetEditor();
264
0
  state |= textEditor ? states::EDITABLE : states::READONLY;
265
0
266
0
  return state;
267
0
}
268
269
uint64_t
270
DocAccessible::NativeInteractiveState() const
271
0
{
272
0
  // Document is always focusable.
273
0
  return states::FOCUSABLE;
274
0
}
275
276
bool
277
DocAccessible::NativelyUnavailable() const
278
0
{
279
0
  return false;
280
0
}
281
282
// Accessible public method
283
void
284
DocAccessible::ApplyARIAState(uint64_t* aState) const
285
0
{
286
0
  // Grab states from content element.
287
0
  if (mContent)
288
0
    Accessible::ApplyARIAState(aState);
289
0
290
0
  // Allow iframe/frame etc. to have final state override via ARIA.
291
0
  if (mParent)
292
0
    mParent->ApplyARIAState(aState);
293
0
}
294
295
already_AddRefed<nsIPersistentProperties>
296
DocAccessible::Attributes()
297
0
{
298
0
  nsCOMPtr<nsIPersistentProperties> attributes =
299
0
    HyperTextAccessibleWrap::Attributes();
300
0
301
0
  // No attributes if document is not attached to the tree or if it's a root
302
0
  // document.
303
0
  if (!mParent || IsRoot())
304
0
    return attributes.forget();
305
0
306
0
  // Override ARIA object attributes from outerdoc.
307
0
  aria::AttrIterator attribIter(mParent->GetContent());
308
0
  nsAutoString name, value, unused;
309
0
  while(attribIter.Next(name, value))
310
0
    attributes->SetStringProperty(NS_ConvertUTF16toUTF8(name), value, unused);
311
0
312
0
  return attributes.forget();
313
0
}
314
315
Accessible*
316
DocAccessible::FocusedChild()
317
0
{
318
0
  // Return an accessible for the current global focus, which does not have to
319
0
  // be contained within the current document.
320
0
  return FocusMgr()->FocusedAccessible();
321
0
}
322
323
void
324
DocAccessible::TakeFocus() const
325
0
{
326
0
  // Focus the document.
327
0
  nsFocusManager* fm = nsFocusManager::GetFocusManager();
328
0
  RefPtr<dom::Element> newFocus;
329
0
  AutoHandlingUserInputStatePusher inputStatePusher(true, nullptr, mDocumentNode);
330
0
  fm->MoveFocus(mDocumentNode->GetWindow(), nullptr,
331
0
                nsFocusManager::MOVEFOCUS_ROOT, 0, getter_AddRefs(newFocus));
332
0
}
333
334
// HyperTextAccessible method
335
already_AddRefed<TextEditor>
336
DocAccessible::GetEditor() const
337
0
{
338
0
  // Check if document is editable (designMode="on" case). Otherwise check if
339
0
  // the html:body (for HTML document case) or document element is editable.
340
0
  if (!mDocumentNode->HasFlag(NODE_IS_EDITABLE) &&
341
0
      (!mContent || !mContent->HasFlag(NODE_IS_EDITABLE)))
342
0
    return nullptr;
343
0
344
0
  nsCOMPtr<nsIDocShell> docShell = mDocumentNode->GetDocShell();
345
0
  if (!docShell) {
346
0
    return nullptr;
347
0
  }
348
0
349
0
  nsCOMPtr<nsIEditingSession> editingSession;
350
0
  docShell->GetEditingSession(getter_AddRefs(editingSession));
351
0
  if (!editingSession)
352
0
    return nullptr; // No editing session interface
353
0
354
0
  RefPtr<HTMLEditor> htmlEditor =
355
0
    editingSession->GetHTMLEditorForWindow(mDocumentNode->GetWindow());
356
0
  if (!htmlEditor) {
357
0
    return nullptr;
358
0
  }
359
0
360
0
  bool isEditable = false;
361
0
  htmlEditor->GetIsDocumentEditable(&isEditable);
362
0
  if (isEditable) {
363
0
    return htmlEditor.forget();
364
0
  }
365
0
366
0
  return nullptr;
367
0
}
368
369
// DocAccessible public method
370
371
void
372
DocAccessible::URL(nsAString& aURL) const
373
0
{
374
0
  nsCOMPtr<nsISupports> container = mDocumentNode->GetContainer();
375
0
  nsCOMPtr<nsIWebNavigation> webNav(do_GetInterface(container));
376
0
  nsAutoCString theURL;
377
0
  if (webNav) {
378
0
    nsCOMPtr<nsIURI> pURI;
379
0
    webNav->GetCurrentURI(getter_AddRefs(pURI));
380
0
    if (pURI)
381
0
      pURI->GetSpec(theURL);
382
0
  }
383
0
  CopyUTF8toUTF16(theURL, aURL);
384
0
}
385
386
void
387
DocAccessible::DocType(nsAString& aType) const
388
0
{
389
0
#ifdef MOZ_XUL
390
0
  if (mDocumentNode->IsXULDocument()) {
391
0
    aType.AssignLiteral("window"); // doctype not implemented for XUL at time of writing - causes assertion
392
0
    return;
393
0
  }
394
0
#endif
395
0
  dom::DocumentType* docType = mDocumentNode->GetDoctype();
396
0
  if (docType)
397
0
    docType->GetPublicId(aType);
398
0
}
399
400
////////////////////////////////////////////////////////////////////////////////
401
// Accessible
402
403
void
404
DocAccessible::Init()
405
0
{
406
0
#ifdef A11Y_LOG
407
0
  if (logging::IsEnabled(logging::eDocCreate))
408
0
    logging::DocCreate("document initialize", mDocumentNode, this);
409
0
#endif
410
0
411
0
  // Initialize notification controller.
412
0
  mNotificationController = new NotificationController(this, mPresShell);
413
0
414
0
  // Mark the document accessible as loaded if its DOM document was loaded at
415
0
  // this point (this can happen because a11y is started late or DOM document
416
0
  // having no container was loaded.
417
0
  if (mDocumentNode->GetReadyStateEnum() == nsIDocument::READYSTATE_COMPLETE)
418
0
    mLoadState |= eDOMLoaded;
419
0
420
0
  AddEventListeners();
421
0
}
422
423
void
424
DocAccessible::Shutdown()
425
0
{
426
0
  if (!mPresShell) // already shutdown
427
0
    return;
428
0
429
0
#ifdef A11Y_LOG
430
0
  if (logging::IsEnabled(logging::eDocDestroy))
431
0
    logging::DocDestroy("document shutdown", mDocumentNode, this);
432
0
#endif
433
0
434
0
  // Mark the document as shutdown before AT is notified about the document
435
0
  // removal from its container (valid for root documents on ATK and due to
436
0
  // some reason for MSAA, refer to bug 757392 for details).
437
0
  mStateFlags |= eIsDefunct;
438
0
439
0
  if (mNotificationController) {
440
0
    mNotificationController->Shutdown();
441
0
    mNotificationController = nullptr;
442
0
  }
443
0
444
0
  RemoveEventListeners();
445
0
446
0
  if (mParent) {
447
0
    DocAccessible* parentDocument = mParent->Document();
448
0
    if (parentDocument)
449
0
      parentDocument->RemoveChildDocument(this);
450
0
451
0
    mParent->RemoveChild(this);
452
0
    MOZ_ASSERT(!mParent, "Parent has to be null!");
453
0
  }
454
0
455
0
  // Walk the array backwards because child documents remove themselves from the
456
0
  // array as they are shutdown.
457
0
  int32_t childDocCount = mChildDocuments.Length();
458
0
  for (int32_t idx = childDocCount - 1; idx >= 0; idx--)
459
0
    mChildDocuments[idx]->Shutdown();
460
0
461
0
  mChildDocuments.Clear();
462
0
463
0
  // XXX thinking about ordering?
464
0
  if (mIPCDoc) {
465
0
    MOZ_ASSERT(IPCAccessibilityActive());
466
0
    mIPCDoc->Shutdown();
467
0
    MOZ_ASSERT(!mIPCDoc);
468
0
  }
469
0
470
0
  if (mVirtualCursor) {
471
0
    mVirtualCursor->RemoveObserver(this);
472
0
    mVirtualCursor = nullptr;
473
0
  }
474
0
475
0
  mPresShell->SetDocAccessible(nullptr);
476
0
  mPresShell = nullptr;  // Avoid reentrancy
477
0
478
0
  mDependentIDsHash.Clear();
479
0
  mNodeToAccessibleMap.Clear();
480
0
481
0
  for (auto iter = mAccessibleCache.Iter(); !iter.Done(); iter.Next()) {
482
0
    Accessible* accessible = iter.Data();
483
0
    MOZ_ASSERT(accessible);
484
0
    if (accessible && !accessible->IsDefunct()) {
485
0
      // Unlink parent to avoid its cleaning overhead in shutdown.
486
0
      accessible->mParent = nullptr;
487
0
      accessible->Shutdown();
488
0
    }
489
0
    iter.Remove();
490
0
  }
491
0
492
0
  HyperTextAccessibleWrap::Shutdown();
493
0
494
0
  GetAccService()->NotifyOfDocumentShutdown(this, mDocumentNode);
495
0
  mDocumentNode = nullptr;
496
0
}
497
498
nsIFrame*
499
DocAccessible::GetFrame() const
500
0
{
501
0
  nsIFrame* root = nullptr;
502
0
  if (mPresShell)
503
0
    root = mPresShell->GetRootFrame();
504
0
505
0
  return root;
506
0
}
507
508
// DocAccessible protected member
509
nsRect
510
DocAccessible::RelativeBounds(nsIFrame** aRelativeFrame) const
511
0
{
512
0
  *aRelativeFrame = GetFrame();
513
0
514
0
  nsIDocument *document = mDocumentNode;
515
0
  nsIDocument *parentDoc = nullptr;
516
0
517
0
  nsRect bounds;
518
0
  while (document) {
519
0
    nsIPresShell *presShell = document->GetShell();
520
0
    if (!presShell)
521
0
      return nsRect();
522
0
523
0
    nsRect scrollPort;
524
0
    nsIScrollableFrame* sf = presShell->GetRootScrollFrameAsScrollable();
525
0
    if (sf) {
526
0
      scrollPort = sf->GetScrollPortRect();
527
0
    } else {
528
0
      nsIFrame* rootFrame = presShell->GetRootFrame();
529
0
      if (!rootFrame)
530
0
        return nsRect();
531
0
532
0
      scrollPort = rootFrame->GetRect();
533
0
    }
534
0
535
0
    if (parentDoc) {  // After first time thru loop
536
0
      // XXXroc bogus code! scrollPort is relative to the viewport of
537
0
      // this document, but we're intersecting rectangles derived from
538
0
      // multiple documents and assuming they're all in the same coordinate
539
0
      // system. See bug 514117.
540
0
      bounds.IntersectRect(scrollPort, bounds);
541
0
    }
542
0
    else {  // First time through loop
543
0
      bounds = scrollPort;
544
0
    }
545
0
546
0
    document = parentDoc = document->GetParentDocument();
547
0
  }
548
0
549
0
  return bounds;
550
0
}
551
552
// DocAccessible protected member
553
nsresult
554
DocAccessible::AddEventListeners()
555
0
{
556
0
  nsCOMPtr<nsIDocShell> docShell(mDocumentNode->GetDocShell());
557
0
558
0
  // We want to add a command observer only if the document is content and has
559
0
  // an editor.
560
0
  if (docShell->ItemType() == nsIDocShellTreeItem::typeContent) {
561
0
    nsCOMPtr<nsICommandManager> commandManager = docShell->GetCommandManager();
562
0
    if (commandManager)
563
0
      commandManager->AddCommandObserver(this, "obs_documentCreated");
564
0
  }
565
0
566
0
  SelectionMgr()->AddDocSelectionListener(mPresShell);
567
0
568
0
  // Add document observer.
569
0
  mDocumentNode->AddObserver(this);
570
0
  return NS_OK;
571
0
}
572
573
// DocAccessible protected member
574
nsresult
575
DocAccessible::RemoveEventListeners()
576
0
{
577
0
  // Remove listeners associated with content documents
578
0
  // Remove scroll position listener
579
0
  RemoveScrollListener();
580
0
581
0
  NS_ASSERTION(mDocumentNode, "No document during removal of listeners.");
582
0
583
0
  if (mDocumentNode) {
584
0
    mDocumentNode->RemoveObserver(this);
585
0
586
0
    nsCOMPtr<nsIDocShell> docShell(mDocumentNode->GetDocShell());
587
0
    NS_ASSERTION(docShell, "doc should support nsIDocShellTreeItem.");
588
0
589
0
    if (docShell) {
590
0
      if (docShell->ItemType() == nsIDocShellTreeItem::typeContent) {
591
0
        nsCOMPtr<nsICommandManager> commandManager = docShell->GetCommandManager();
592
0
        if (commandManager) {
593
0
          commandManager->RemoveCommandObserver(this, "obs_documentCreated");
594
0
        }
595
0
      }
596
0
    }
597
0
  }
598
0
599
0
  if (mScrollWatchTimer) {
600
0
    mScrollWatchTimer->Cancel();
601
0
    mScrollWatchTimer = nullptr;
602
0
    NS_RELEASE_THIS(); // Kung fu death grip
603
0
  }
604
0
605
0
  SelectionMgr()->RemoveDocSelectionListener(mPresShell);
606
0
  return NS_OK;
607
0
}
608
609
void
610
DocAccessible::ScrollTimerCallback(nsITimer* aTimer, void* aClosure)
611
0
{
612
0
  DocAccessible* docAcc = reinterpret_cast<DocAccessible*>(aClosure);
613
0
614
0
  if (docAcc) {
615
0
    docAcc->DispatchScrollingEvent(nsIAccessibleEvent::EVENT_SCROLLING_END);
616
0
617
0
    if (docAcc->mScrollWatchTimer) {
618
0
      docAcc->mScrollWatchTimer = nullptr;
619
0
      NS_RELEASE(docAcc); // Release kung fu death grip
620
0
    }
621
0
  }
622
0
}
623
624
////////////////////////////////////////////////////////////////////////////////
625
// nsIScrollPositionListener
626
627
void
628
DocAccessible::ScrollPositionDidChange(nscoord aX, nscoord aY)
629
0
{
630
0
  const uint32_t kScrollEventInterval = 100;
631
0
  TimeStamp timestamp = TimeStamp::Now();
632
0
  if (mLastScrollingDispatch.IsNull() ||
633
0
      (timestamp - mLastScrollingDispatch).ToMilliseconds() >= kScrollEventInterval) {
634
0
    DispatchScrollingEvent(nsIAccessibleEvent::EVENT_SCROLLING);
635
0
    mLastScrollingDispatch = timestamp;
636
0
  }
637
0
638
0
  // If timer callback is still pending, push it 100ms into the future.
639
0
  // When scrolling ends and we don't fire this callback anymore, the
640
0
  // timer callback will fire and dispatch an EVENT_SCROLLING_END.
641
0
  if (mScrollWatchTimer) {
642
0
    mScrollWatchTimer->SetDelay(kScrollEventInterval);
643
0
  }
644
0
  else {
645
0
    NS_NewTimerWithFuncCallback(getter_AddRefs(mScrollWatchTimer),
646
0
                                ScrollTimerCallback,
647
0
                                this,
648
0
                                kScrollEventInterval,
649
0
                                nsITimer::TYPE_ONE_SHOT,
650
0
                                "a11y::DocAccessible::ScrollPositionDidChange");
651
0
    if (mScrollWatchTimer) {
652
0
      NS_ADDREF_THIS(); // Kung fu death grip
653
0
    }
654
0
  }
655
0
}
656
657
////////////////////////////////////////////////////////////////////////////////
658
// nsIObserver
659
660
NS_IMETHODIMP
661
DocAccessible::Observe(nsISupports* aSubject, const char* aTopic,
662
                       const char16_t* aData)
663
0
{
664
0
  if (!nsCRT::strcmp(aTopic,"obs_documentCreated")) {
665
0
    // State editable will now be set, readonly is now clear
666
0
    // Normally we only fire delayed events created from the node, not an
667
0
    // accessible object. See the AccStateChangeEvent constructor for details
668
0
    // about this exceptional case.
669
0
    RefPtr<AccEvent> event =
670
0
      new AccStateChangeEvent(this, states::EDITABLE, true);
671
0
    FireDelayedEvent(event);
672
0
  }
673
0
674
0
  return NS_OK;
675
0
}
676
677
////////////////////////////////////////////////////////////////////////////////
678
// nsIAccessiblePivotObserver
679
680
NS_IMETHODIMP
681
DocAccessible::OnPivotChanged(nsIAccessiblePivot* aPivot,
682
                              nsIAccessible* aOldAccessible,
683
                              int32_t aOldStart, int32_t aOldEnd,
684
                              nsIAccessible* aNewAccessible,
685
                              int32_t aNewStart, int32_t aNewEnd,
686
                              PivotMoveReason aReason,
687
                              TextBoundaryType aBoundaryType,
688
                              bool aIsFromUserInput)
689
0
{
690
0
  RefPtr<AccEvent> event =
691
0
    new AccVCChangeEvent(
692
0
      this, (aOldAccessible ? aOldAccessible->ToInternalAccessible() : nullptr),
693
0
      aOldStart, aOldEnd,
694
0
      (aNewAccessible ? aNewAccessible->ToInternalAccessible() : nullptr),
695
0
      aNewStart, aNewEnd,
696
0
      aReason, aBoundaryType,
697
0
      aIsFromUserInput ? eFromUserInput : eNoUserInput);
698
0
  nsEventShell::FireEvent(event);
699
0
700
0
  return NS_OK;
701
0
}
702
703
////////////////////////////////////////////////////////////////////////////////
704
// nsIDocumentObserver
705
706
NS_IMPL_NSIDOCUMENTOBSERVER_CORE_STUB(DocAccessible)
707
NS_IMPL_NSIDOCUMENTOBSERVER_LOAD_STUB(DocAccessible)
708
709
void
710
DocAccessible::AttributeWillChange(dom::Element* aElement,
711
                                   int32_t aNameSpaceID,
712
                                   nsAtom* aAttribute,
713
                                   int32_t aModType,
714
                                   const nsAttrValue* aNewValue)
715
0
{
716
0
  Accessible* accessible = GetAccessible(aElement);
717
0
  if (!accessible) {
718
0
    if (aElement != mContent)
719
0
      return;
720
0
721
0
    accessible = this;
722
0
  }
723
0
724
0
  // Update dependent IDs cache. Take care of elements that are accessible
725
0
  // because dependent IDs cache doesn't contain IDs from non accessible
726
0
  // elements.
727
0
  if (aModType != dom::MutationEvent_Binding::ADDITION)
728
0
    RemoveDependentIDsFor(accessible, aAttribute);
729
0
730
0
  if (aAttribute == nsGkAtoms::id) {
731
0
    RelocateARIAOwnedIfNeeded(aElement);
732
0
  }
733
0
734
0
  // Store the ARIA attribute old value so that it can be used after
735
0
  // attribute change. Note, we assume there's no nested ARIA attribute
736
0
  // changes. If this happens then we should end up with keeping a stack of
737
0
  // old values.
738
0
739
0
  // XXX TODO: bugs 472142, 472143.
740
0
  // Here we will want to cache whatever attribute values we are interested
741
0
  // in, such as the existence of aria-pressed for button (so we know if we
742
0
  // need to newly expose it as a toggle button) etc.
743
0
  if (aAttribute == nsGkAtoms::aria_checked ||
744
0
      aAttribute == nsGkAtoms::aria_pressed) {
745
0
    mARIAAttrOldValue = (aModType != dom::MutationEvent_Binding::ADDITION) ?
746
0
      nsAccUtils::GetARIAToken(aElement, aAttribute) : nullptr;
747
0
    return;
748
0
  }
749
0
750
0
  if (aAttribute == nsGkAtoms::aria_disabled ||
751
0
      aAttribute == nsGkAtoms::disabled)
752
0
    mStateBitWasOn = accessible->Unavailable();
753
0
}
754
755
void
756
DocAccessible::NativeAnonymousChildListChange(nsIContent* aContent,
757
                                              bool aIsRemove)
758
0
{
759
0
}
760
761
void
762
DocAccessible::AttributeChanged(dom::Element* aElement,
763
                                int32_t aNameSpaceID, nsAtom* aAttribute,
764
                                int32_t aModType,
765
                                const nsAttrValue* aOldValue)
766
0
{
767
0
  NS_ASSERTION(!IsDefunct(),
768
0
               "Attribute changed called on defunct document accessible!");
769
0
770
0
  // Proceed even if the element is not accessible because element may become
771
0
  // accessible if it gets certain attribute.
772
0
  if (UpdateAccessibleOnAttrChange(aElement, aAttribute))
773
0
    return;
774
0
775
0
  // Update the accessible tree on aria-hidden change. Make sure to not create
776
0
  // a tree under aria-hidden='true'.
777
0
  if (aAttribute == nsGkAtoms::aria_hidden) {
778
0
    if (aria::HasDefinedARIAHidden(aElement)) {
779
0
      ContentRemoved(aElement);
780
0
    }
781
0
    else {
782
0
      ContentInserted(aElement->GetFlattenedTreeParent(),
783
0
                      aElement, aElement->GetNextSibling());
784
0
    }
785
0
    return;
786
0
  }
787
0
788
0
  // Ignore attribute change if the element doesn't have an accessible (at all
789
0
  // or still) iff the element is not a root content of this document accessible
790
0
  // (which is treated as attribute change on this document accessible).
791
0
  // Note: we don't bail if all the content hasn't finished loading because
792
0
  // these attributes are changing for a loaded part of the content.
793
0
  Accessible* accessible = GetAccessible(aElement);
794
0
  if (!accessible) {
795
0
    if (mContent != aElement)
796
0
      return;
797
0
798
0
    accessible = this;
799
0
  }
800
0
801
0
  MOZ_ASSERT(accessible->IsBoundToParent() || accessible->IsDoc(),
802
0
             "DOM attribute change on an accessible detached from the tree");
803
0
804
0
  // Fire accessible events iff there's an accessible, otherwise we consider
805
0
  // the accessible state wasn't changed, i.e. its state is initial state.
806
0
  AttributeChangedImpl(accessible, aNameSpaceID, aAttribute);
807
0
808
0
  // Update dependent IDs cache. Take care of accessible elements because no
809
0
  // accessible element means either the element is not accessible at all or
810
0
  // its accessible will be created later. It doesn't make sense to keep
811
0
  // dependent IDs for non accessible elements. For the second case we'll update
812
0
  // dependent IDs cache when its accessible is created.
813
0
  if (aModType == dom::MutationEvent_Binding::MODIFICATION ||
814
0
      aModType == dom::MutationEvent_Binding::ADDITION) {
815
0
    AddDependentIDsFor(accessible, aAttribute);
816
0
  }
817
0
}
818
819
// DocAccessible protected member
820
void
821
DocAccessible::AttributeChangedImpl(Accessible* aAccessible,
822
                                    int32_t aNameSpaceID, nsAtom* aAttribute)
823
0
{
824
0
  // Fire accessible event after short timer, because we need to wait for
825
0
  // DOM attribute & resulting layout to actually change. Otherwise,
826
0
  // assistive technology will retrieve the wrong state/value/selection info.
827
0
828
0
  // XXX todo
829
0
  // We still need to handle special HTML cases here
830
0
  // For example, if an <img>'s usemap attribute is modified
831
0
  // Otherwise it may just be a state change, for example an object changing
832
0
  // its visibility
833
0
  //
834
0
  // XXX todo: report aria state changes for "undefined" literal value changes
835
0
  // filed as bug 472142
836
0
  //
837
0
  // XXX todo:  invalidate accessible when aria state changes affect exposed role
838
0
  // filed as bug 472143
839
0
840
0
  // Universal boolean properties that don't require a role. Fire the state
841
0
  // change when disabled or aria-disabled attribute is set.
842
0
  // Note. Checking the XUL or HTML namespace would not seem to gain us
843
0
  // anything, because disabled attribute really is going to mean the same
844
0
  // thing in any namespace.
845
0
  // Note. We use the attribute instead of the disabled state bit because
846
0
  // ARIA's aria-disabled does not affect the disabled state bit.
847
0
  if (aAttribute == nsGkAtoms::disabled ||
848
0
      aAttribute == nsGkAtoms::aria_disabled) {
849
0
    // Do nothing if state wasn't changed (like @aria-disabled was removed but
850
0
    // @disabled is still presented).
851
0
    if (aAccessible->Unavailable() == mStateBitWasOn)
852
0
      return;
853
0
854
0
    RefPtr<AccEvent> enabledChangeEvent =
855
0
      new AccStateChangeEvent(aAccessible, states::ENABLED, mStateBitWasOn);
856
0
    FireDelayedEvent(enabledChangeEvent);
857
0
858
0
    RefPtr<AccEvent> sensitiveChangeEvent =
859
0
      new AccStateChangeEvent(aAccessible, states::SENSITIVE, mStateBitWasOn);
860
0
    FireDelayedEvent(sensitiveChangeEvent);
861
0
    return;
862
0
  }
863
0
864
0
  // Check for namespaced ARIA attribute
865
0
  if (aNameSpaceID == kNameSpaceID_None) {
866
0
    // Check for hyphenated aria-foo property?
867
0
    if (StringBeginsWith(nsDependentAtomString(aAttribute),
868
0
                         NS_LITERAL_STRING("aria-"))) {
869
0
      ARIAAttributeChanged(aAccessible, aAttribute);
870
0
    }
871
0
  }
872
0
873
0
  // Fire name change and description change events. XXX: it's not complete and
874
0
  // dupes the code logic of accessible name and description calculation, we do
875
0
  // that for performance reasons.
876
0
  if (aAttribute == nsGkAtoms::aria_label) {
877
0
    FireDelayedEvent(nsIAccessibleEvent::EVENT_NAME_CHANGE, aAccessible);
878
0
    return;
879
0
  }
880
0
881
0
  if (aAttribute == nsGkAtoms::aria_describedby) {
882
0
    FireDelayedEvent(nsIAccessibleEvent::EVENT_DESCRIPTION_CHANGE, aAccessible);
883
0
    return;
884
0
  }
885
0
886
0
  dom::Element* elm = aAccessible->GetContent()->AsElement();
887
0
  if (aAttribute == nsGkAtoms::aria_labelledby &&
888
0
      !elm->HasAttr(kNameSpaceID_None, nsGkAtoms::aria_label)) {
889
0
    FireDelayedEvent(nsIAccessibleEvent::EVENT_NAME_CHANGE, aAccessible);
890
0
    return;
891
0
  }
892
0
893
0
  if (aAttribute == nsGkAtoms::alt &&
894
0
      !elm->HasAttr(kNameSpaceID_None, nsGkAtoms::aria_label) &&
895
0
      !elm->HasAttr(kNameSpaceID_None, nsGkAtoms::aria_labelledby)) {
896
0
    FireDelayedEvent(nsIAccessibleEvent::EVENT_NAME_CHANGE, aAccessible);
897
0
    return;
898
0
  }
899
0
900
0
  if (aAttribute == nsGkAtoms::title) {
901
0
    if (!elm->HasAttr(kNameSpaceID_None, nsGkAtoms::aria_label) &&
902
0
        !elm->HasAttr(kNameSpaceID_None, nsGkAtoms::aria_labelledby) &&
903
0
        !elm->HasAttr(kNameSpaceID_None, nsGkAtoms::alt)) {
904
0
      FireDelayedEvent(nsIAccessibleEvent::EVENT_NAME_CHANGE, aAccessible);
905
0
      return;
906
0
    }
907
0
908
0
    if (!elm->HasAttr(kNameSpaceID_None, nsGkAtoms::aria_describedby))
909
0
      FireDelayedEvent(nsIAccessibleEvent::EVENT_DESCRIPTION_CHANGE, aAccessible);
910
0
911
0
    return;
912
0
  }
913
0
914
0
  if (aAttribute == nsGkAtoms::aria_busy) {
915
0
    bool isOn = elm->AttrValueIs(aNameSpaceID, aAttribute, nsGkAtoms::_true,
916
0
                                 eCaseMatters);
917
0
    RefPtr<AccEvent> event =
918
0
      new AccStateChangeEvent(aAccessible, states::BUSY, isOn);
919
0
    FireDelayedEvent(event);
920
0
    return;
921
0
  }
922
0
923
0
  if (aAttribute == nsGkAtoms::id) {
924
0
    RelocateARIAOwnedIfNeeded(elm);
925
0
    ARIAActiveDescendantIDMaybeMoved(elm);
926
0
  }
927
0
928
0
  // ARIA or XUL selection
929
0
  if ((aAccessible->GetContent()->IsXULElement() &&
930
0
       aAttribute == nsGkAtoms::selected) ||
931
0
      aAttribute == nsGkAtoms::aria_selected) {
932
0
    Accessible* widget =
933
0
      nsAccUtils::GetSelectableContainer(aAccessible, aAccessible->State());
934
0
    if (widget) {
935
0
      AccSelChangeEvent::SelChangeType selChangeType =
936
0
        elm->AttrValueIs(aNameSpaceID, aAttribute, nsGkAtoms::_true, eCaseMatters) ?
937
0
          AccSelChangeEvent::eSelectionAdd : AccSelChangeEvent::eSelectionRemove;
938
0
939
0
      RefPtr<AccEvent> event =
940
0
        new AccSelChangeEvent(widget, aAccessible, selChangeType);
941
0
      FireDelayedEvent(event);
942
0
    }
943
0
944
0
    return;
945
0
  }
946
0
947
0
  if (aAttribute == nsGkAtoms::contenteditable) {
948
0
    RefPtr<AccEvent> editableChangeEvent =
949
0
      new AccStateChangeEvent(aAccessible, states::EDITABLE);
950
0
    FireDelayedEvent(editableChangeEvent);
951
0
    return;
952
0
  }
953
0
954
0
  if (aAttribute == nsGkAtoms::value) {
955
0
    if (aAccessible->IsProgress())
956
0
      FireDelayedEvent(nsIAccessibleEvent::EVENT_VALUE_CHANGE, aAccessible);
957
0
  }
958
0
}
959
960
// DocAccessible protected member
961
void
962
DocAccessible::ARIAAttributeChanged(Accessible* aAccessible, nsAtom* aAttribute)
963
0
{
964
0
  // Note: For universal/global ARIA states and properties we don't care if
965
0
  // there is an ARIA role present or not.
966
0
967
0
  if (aAttribute == nsGkAtoms::aria_required) {
968
0
    RefPtr<AccEvent> event =
969
0
      new AccStateChangeEvent(aAccessible, states::REQUIRED);
970
0
    FireDelayedEvent(event);
971
0
    return;
972
0
  }
973
0
974
0
  if (aAttribute == nsGkAtoms::aria_invalid) {
975
0
    RefPtr<AccEvent> event =
976
0
      new AccStateChangeEvent(aAccessible, states::INVALID);
977
0
    FireDelayedEvent(event);
978
0
    return;
979
0
  }
980
0
981
0
  // The activedescendant universal property redirects accessible focus events
982
0
  // to the element with the id that activedescendant points to. Make sure
983
0
  // the tree up to date before processing. In other words, when a node has just
984
0
  // been inserted, the tree won't be up to date yet, so we must always schedule
985
0
  // an async notification so that a newly inserted node will be present in
986
0
  // the tree.
987
0
  if (aAttribute == nsGkAtoms::aria_activedescendant) {
988
0
    mNotificationController->ScheduleNotification<DocAccessible, Accessible>
989
0
      (this, &DocAccessible::ARIAActiveDescendantChanged, aAccessible);
990
0
    return;
991
0
  }
992
0
993
0
  // We treat aria-expanded as a global ARIA state for historical reasons
994
0
  if (aAttribute == nsGkAtoms::aria_expanded) {
995
0
    RefPtr<AccEvent> event =
996
0
      new AccStateChangeEvent(aAccessible, states::EXPANDED);
997
0
    FireDelayedEvent(event);
998
0
    return;
999
0
  }
1000
0
1001
0
  // For aria attributes like drag and drop changes we fire a generic attribute
1002
0
  // change event; at least until native API comes up with a more meaningful event.
1003
0
  uint8_t attrFlags = aria::AttrCharacteristicsFor(aAttribute);
1004
0
  if (!(attrFlags & ATTR_BYPASSOBJ)) {
1005
0
    RefPtr<AccEvent> event =
1006
0
      new AccObjectAttrChangedEvent(aAccessible, aAttribute);
1007
0
    FireDelayedEvent(event);
1008
0
  }
1009
0
1010
0
  dom::Element* elm = aAccessible->GetContent()->AsElement();
1011
0
1012
0
  if (aAttribute == nsGkAtoms::aria_checked ||
1013
0
      (aAccessible->IsButton() &&
1014
0
       aAttribute == nsGkAtoms::aria_pressed)) {
1015
0
    const uint64_t kState = (aAttribute == nsGkAtoms::aria_checked) ?
1016
0
                            states::CHECKED : states::PRESSED;
1017
0
    RefPtr<AccEvent> event = new AccStateChangeEvent(aAccessible, kState);
1018
0
    FireDelayedEvent(event);
1019
0
1020
0
    bool wasMixed = (mARIAAttrOldValue == nsGkAtoms::mixed);
1021
0
    bool isMixed = elm->AttrValueIs(kNameSpaceID_None, aAttribute,
1022
0
                                    nsGkAtoms::mixed, eCaseMatters);
1023
0
    if (isMixed != wasMixed) {
1024
0
      RefPtr<AccEvent> event =
1025
0
        new AccStateChangeEvent(aAccessible, states::MIXED, isMixed);
1026
0
      FireDelayedEvent(event);
1027
0
    }
1028
0
    return;
1029
0
  }
1030
0
1031
0
  if (aAttribute == nsGkAtoms::aria_readonly) {
1032
0
    RefPtr<AccEvent> event =
1033
0
      new AccStateChangeEvent(aAccessible, states::READONLY);
1034
0
    FireDelayedEvent(event);
1035
0
    return;
1036
0
  }
1037
0
1038
0
  // Fire text value change event whenever aria-valuetext is changed.
1039
0
  if (aAttribute == nsGkAtoms::aria_valuetext) {
1040
0
    FireDelayedEvent(nsIAccessibleEvent::EVENT_TEXT_VALUE_CHANGE, aAccessible);
1041
0
    return;
1042
0
  }
1043
0
1044
0
  // Fire numeric value change event when aria-valuenow is changed and
1045
0
  // aria-valuetext is empty
1046
0
  if (aAttribute == nsGkAtoms::aria_valuenow &&
1047
0
      (!elm->HasAttr(kNameSpaceID_None, nsGkAtoms::aria_valuetext) ||
1048
0
       elm->AttrValueIs(kNameSpaceID_None, nsGkAtoms::aria_valuetext,
1049
0
                        nsGkAtoms::_empty, eCaseMatters))) {
1050
0
    FireDelayedEvent(nsIAccessibleEvent::EVENT_VALUE_CHANGE, aAccessible);
1051
0
    return;
1052
0
  }
1053
0
1054
0
  if (aAttribute == nsGkAtoms::aria_current) {
1055
0
    RefPtr<AccEvent> event =
1056
0
      new AccStateChangeEvent(aAccessible, states::CURRENT);
1057
0
    FireDelayedEvent(event);
1058
0
    return;
1059
0
  }
1060
0
1061
0
  if (aAttribute == nsGkAtoms::aria_owns) {
1062
0
    mNotificationController->ScheduleRelocation(aAccessible);
1063
0
  }
1064
0
}
1065
1066
void
1067
DocAccessible::ARIAActiveDescendantChanged(Accessible* aAccessible)
1068
0
{
1069
0
  nsIContent* elm = aAccessible->GetContent();
1070
0
  if (elm && elm->IsElement() && aAccessible->IsActiveWidget()) {
1071
0
    nsAutoString id;
1072
0
    if (elm->AsElement()->GetAttr(kNameSpaceID_None,
1073
0
                                  nsGkAtoms::aria_activedescendant,
1074
0
                                  id)) {
1075
0
      dom::Element* activeDescendantElm = elm->OwnerDoc()->GetElementById(id);
1076
0
      if (activeDescendantElm) {
1077
0
        Accessible* activeDescendant = GetAccessible(activeDescendantElm);
1078
0
        if (activeDescendant) {
1079
0
          FocusMgr()->ActiveItemChanged(activeDescendant, false);
1080
0
#ifdef A11Y_LOG
1081
0
          if (logging::IsEnabled(logging::eFocus))
1082
0
            logging::ActiveItemChangeCausedBy("ARIA activedescedant changed",
1083
0
                                              activeDescendant);
1084
0
#endif
1085
0
          return;
1086
0
        }
1087
0
      }
1088
0
    }
1089
0
1090
0
    // aria-activedescendant was cleared or changed to a non-existent node.
1091
0
    // Move focus back to the element itself.
1092
0
    FocusMgr()->ActiveItemChanged(aAccessible, false);
1093
0
#ifdef A11Y_LOG
1094
0
    if (logging::IsEnabled(logging::eFocus)) {
1095
0
      logging::ActiveItemChangeCausedBy("ARIA activedescedant cleared",
1096
0
                                        aAccessible);
1097
0
    }
1098
0
#endif
1099
0
  }
1100
0
}
1101
1102
void
1103
DocAccessible::ContentAppended(nsIContent* aFirstNewContent)
1104
0
{
1105
0
}
1106
1107
void
1108
DocAccessible::ContentStateChanged(nsIDocument* aDocument,
1109
                                   nsIContent* aContent,
1110
                                   EventStates aStateMask)
1111
0
{
1112
0
  Accessible* accessible = GetAccessible(aContent);
1113
0
  if (!accessible)
1114
0
    return;
1115
0
1116
0
  if (aStateMask.HasState(NS_EVENT_STATE_CHECKED)) {
1117
0
    Accessible* widget = accessible->ContainerWidget();
1118
0
    if (widget && widget->IsSelect()) {
1119
0
      AccSelChangeEvent::SelChangeType selChangeType =
1120
0
        aContent->AsElement()->State().HasState(NS_EVENT_STATE_CHECKED) ?
1121
0
          AccSelChangeEvent::eSelectionAdd : AccSelChangeEvent::eSelectionRemove;
1122
0
      RefPtr<AccEvent> event =
1123
0
        new AccSelChangeEvent(widget, accessible, selChangeType);
1124
0
      FireDelayedEvent(event);
1125
0
      return;
1126
0
    }
1127
0
1128
0
    RefPtr<AccEvent> event =
1129
0
      new AccStateChangeEvent(accessible, states::CHECKED,
1130
0
                              aContent->AsElement()->State().HasState(NS_EVENT_STATE_CHECKED));
1131
0
    FireDelayedEvent(event);
1132
0
  }
1133
0
1134
0
  if (aStateMask.HasState(NS_EVENT_STATE_INVALID)) {
1135
0
    RefPtr<AccEvent> event =
1136
0
      new AccStateChangeEvent(accessible, states::INVALID, true);
1137
0
    FireDelayedEvent(event);
1138
0
  }
1139
0
1140
0
  if (aStateMask.HasState(NS_EVENT_STATE_VISITED)) {
1141
0
    RefPtr<AccEvent> event =
1142
0
      new AccStateChangeEvent(accessible, states::TRAVERSED, true);
1143
0
    FireDelayedEvent(event);
1144
0
  }
1145
0
}
1146
1147
void
1148
DocAccessible::DocumentStatesChanged(nsIDocument* aDocument,
1149
                                     EventStates aStateMask)
1150
0
{
1151
0
}
1152
1153
void
1154
DocAccessible::CharacterDataWillChange(nsIContent* aContent,
1155
                                       const CharacterDataChangeInfo&)
1156
0
{
1157
0
}
1158
1159
void
1160
DocAccessible::CharacterDataChanged(nsIContent* aContent,
1161
                                    const CharacterDataChangeInfo&)
1162
0
{
1163
0
}
1164
1165
void
1166
DocAccessible::ContentInserted(nsIContent* aChild)
1167
0
{
1168
0
}
1169
1170
void
1171
DocAccessible::ContentRemoved(nsIContent* aChildNode,
1172
                              nsIContent* aPreviousSiblingNode)
1173
0
{
1174
0
#ifdef A11Y_LOG
1175
0
  if (logging::IsEnabled(logging::eTree)) {
1176
0
    logging::MsgBegin("TREE", "DOM content removed; doc: %p", this);
1177
0
    logging::Node("container node", aChildNode->GetParent());
1178
0
    logging::Node("content node", aChildNode);
1179
0
    logging::MsgEnd();
1180
0
  }
1181
0
#endif
1182
0
  // This one and content removal notification from layout may result in
1183
0
  // double processing of same subtrees. If it pops up in profiling, then
1184
0
  // consider reusing a document node cache to reject these notifications early.
1185
0
  ContentRemoved(aChildNode);
1186
0
}
1187
1188
void
1189
DocAccessible::ParentChainChanged(nsIContent* aContent)
1190
0
{
1191
0
}
1192
1193
1194
////////////////////////////////////////////////////////////////////////////////
1195
// Accessible
1196
1197
#ifdef A11Y_LOG
1198
nsresult
1199
DocAccessible::HandleAccEvent(AccEvent* aEvent)
1200
0
{
1201
0
  if (logging::IsEnabled(logging::eDocLoad))
1202
0
    logging::DocLoadEventHandled(aEvent);
1203
0
1204
0
  return HyperTextAccessible::HandleAccEvent(aEvent);
1205
0
}
1206
#endif
1207
1208
////////////////////////////////////////////////////////////////////////////////
1209
// Public members
1210
1211
void*
1212
DocAccessible::GetNativeWindow() const
1213
0
{
1214
0
  if (!mPresShell)
1215
0
    return nullptr;
1216
0
1217
0
  nsViewManager* vm = mPresShell->GetViewManager();
1218
0
  if (!vm)
1219
0
    return nullptr;
1220
0
1221
0
  nsCOMPtr<nsIWidget> widget;
1222
0
  vm->GetRootWidget(getter_AddRefs(widget));
1223
0
  if (widget)
1224
0
    return widget->GetNativeData(NS_NATIVE_WINDOW);
1225
0
1226
0
  return nullptr;
1227
0
}
1228
1229
Accessible*
1230
DocAccessible::GetAccessibleByUniqueIDInSubtree(void* aUniqueID)
1231
0
{
1232
0
  Accessible* child = GetAccessibleByUniqueID(aUniqueID);
1233
0
  if (child)
1234
0
    return child;
1235
0
1236
0
  uint32_t childDocCount = mChildDocuments.Length();
1237
0
  for (uint32_t childDocIdx= 0; childDocIdx < childDocCount; childDocIdx++) {
1238
0
    DocAccessible* childDocument = mChildDocuments.ElementAt(childDocIdx);
1239
0
    child = childDocument->GetAccessibleByUniqueIDInSubtree(aUniqueID);
1240
0
    if (child)
1241
0
      return child;
1242
0
  }
1243
0
1244
0
  return nullptr;
1245
0
}
1246
1247
Accessible*
1248
DocAccessible::GetAccessibleOrContainer(nsINode* aNode,
1249
                                        int aARIAHiddenFlag) const
1250
0
{
1251
0
  if (!aNode || !aNode->GetComposedDoc())
1252
0
    return nullptr;
1253
0
1254
0
  for (nsINode* currNode = aNode; currNode;
1255
0
       currNode = currNode->GetFlattenedTreeParentNode()) {
1256
0
1257
0
    // No container if is inside of aria-hidden subtree.
1258
0
    if (aARIAHiddenFlag == eNoContainerIfARIAHidden && currNode->IsElement() &&
1259
0
        aria::HasDefinedARIAHidden(currNode->AsElement())) {
1260
0
      return nullptr;
1261
0
    }
1262
0
1263
0
    if (Accessible* accessible = GetAccessible(currNode)) {
1264
0
      return accessible;
1265
0
    }
1266
0
  }
1267
0
1268
0
  return nullptr;
1269
0
}
1270
1271
Accessible*
1272
DocAccessible::GetAccessibleOrDescendant(nsINode* aNode) const
1273
0
{
1274
0
  Accessible* acc = GetAccessible(aNode);
1275
0
  if (acc)
1276
0
    return acc;
1277
0
1278
0
  acc = GetContainerAccessible(aNode);
1279
0
  if (acc) {
1280
0
    uint32_t childCnt = acc->ChildCount();
1281
0
    for (uint32_t idx = 0; idx < childCnt; idx++) {
1282
0
      Accessible* child = acc->GetChildAt(idx);
1283
0
      for (nsIContent* elm = child->GetContent();
1284
0
           elm && elm != acc->GetContent();
1285
0
           elm = elm->GetFlattenedTreeParent()) {
1286
0
        if (elm == aNode)
1287
0
          return child;
1288
0
      }
1289
0
    }
1290
0
  }
1291
0
1292
0
  return nullptr;
1293
0
}
1294
1295
void
1296
DocAccessible::BindToDocument(Accessible* aAccessible,
1297
                              const nsRoleMapEntry* aRoleMapEntry)
1298
0
{
1299
0
  // Put into DOM node cache.
1300
0
  if (aAccessible->IsNodeMapEntry())
1301
0
    mNodeToAccessibleMap.Put(aAccessible->GetNode(), aAccessible);
1302
0
1303
0
  // Put into unique ID cache.
1304
0
  mAccessibleCache.Put(aAccessible->UniqueID(), aAccessible);
1305
0
1306
0
  aAccessible->SetRoleMapEntry(aRoleMapEntry);
1307
0
1308
0
  if (aAccessible->HasOwnContent()) {
1309
0
    AddDependentIDsFor(aAccessible);
1310
0
1311
0
    nsIContent* content = aAccessible->GetContent();
1312
0
    if (content->IsElement() &&
1313
0
        content->AsElement()->HasAttr(kNameSpaceID_None, nsGkAtoms::aria_owns)) {
1314
0
      mNotificationController->ScheduleRelocation(aAccessible);
1315
0
    }
1316
0
  }
1317
0
}
1318
1319
void
1320
DocAccessible::UnbindFromDocument(Accessible* aAccessible)
1321
0
{
1322
0
  NS_ASSERTION(mAccessibleCache.GetWeak(aAccessible->UniqueID()),
1323
0
               "Unbinding the unbound accessible!");
1324
0
1325
0
  // Fire focus event on accessible having DOM focus if active item was removed
1326
0
  // from the tree.
1327
0
  if (FocusMgr()->IsActiveItem(aAccessible)) {
1328
0
    FocusMgr()->ActiveItemChanged(nullptr);
1329
0
#ifdef A11Y_LOG
1330
0
          if (logging::IsEnabled(logging::eFocus))
1331
0
            logging::ActiveItemChangeCausedBy("tree shutdown", aAccessible);
1332
0
#endif
1333
0
  }
1334
0
1335
0
  // Remove an accessible from node-to-accessible map if it exists there.
1336
0
  if (aAccessible->IsNodeMapEntry() &&
1337
0
      mNodeToAccessibleMap.Get(aAccessible->GetNode()) == aAccessible)
1338
0
    mNodeToAccessibleMap.Remove(aAccessible->GetNode());
1339
0
1340
0
  aAccessible->mStateFlags |= eIsNotInDocument;
1341
0
1342
0
  // Update XPCOM part.
1343
0
  xpcAccessibleDocument* xpcDoc = GetAccService()->GetCachedXPCDocument(this);
1344
0
  if (xpcDoc)
1345
0
    xpcDoc->NotifyOfShutdown(aAccessible);
1346
0
1347
0
  void* uniqueID = aAccessible->UniqueID();
1348
0
1349
0
  NS_ASSERTION(!aAccessible->IsDefunct(), "Shutdown the shutdown accessible!");
1350
0
  aAccessible->Shutdown();
1351
0
1352
0
  mAccessibleCache.Remove(uniqueID);
1353
0
}
1354
1355
void
1356
DocAccessible::ContentInserted(nsIContent* aContainerNode,
1357
                               nsIContent* aStartChildNode,
1358
                               nsIContent* aEndChildNode)
1359
0
{
1360
0
  // Ignore content insertions until we constructed accessible tree. Otherwise
1361
0
  // schedule tree update on content insertion after layout.
1362
0
  if (mNotificationController && HasLoadState(eTreeConstructed)) {
1363
0
    // Update the whole tree of this document accessible when the container is
1364
0
    // null (document element is inserted or removed).
1365
0
    Accessible* container = aContainerNode ?
1366
0
      AccessibleOrTrueContainer(aContainerNode) : this;
1367
0
    if (container) {
1368
0
      // Ignore notification if the container node is no longer in the DOM tree.
1369
0
      mNotificationController->ScheduleContentInsertion(container,
1370
0
                                                        aStartChildNode,
1371
0
                                                        aEndChildNode);
1372
0
    }
1373
0
  }
1374
0
}
1375
1376
void
1377
DocAccessible::RecreateAccessible(nsIContent* aContent)
1378
0
{
1379
0
#ifdef A11Y_LOG
1380
0
  if (logging::IsEnabled(logging::eTree)) {
1381
0
    logging::MsgBegin("TREE", "accessible recreated");
1382
0
    logging::Node("content", aContent);
1383
0
    logging::MsgEnd();
1384
0
  }
1385
0
#endif
1386
0
1387
0
  // XXX: we shouldn't recreate whole accessible subtree, instead we should
1388
0
  // subclass hide and show events to handle them separately and implement their
1389
0
  // coalescence with normal hide and show events. Note, in this case they
1390
0
  // should be coalesced with normal show/hide events.
1391
0
1392
0
  nsIContent* parent = aContent->GetFlattenedTreeParent();
1393
0
  ContentRemoved(aContent);
1394
0
  ContentInserted(parent, aContent, aContent->GetNextSibling());
1395
0
}
1396
1397
void
1398
DocAccessible::ProcessInvalidationList()
1399
0
{
1400
0
  // Invalidate children of container accessible for each element in
1401
0
  // invalidation list. Allow invalidation list insertions while container
1402
0
  // children are recached.
1403
0
  for (uint32_t idx = 0; idx < mInvalidationList.Length(); idx++) {
1404
0
    nsIContent* content = mInvalidationList[idx];
1405
0
    if (!HasAccessible(content) && content->HasID()) {
1406
0
      Accessible* container = GetContainerAccessible(content);
1407
0
      if (container) {
1408
0
        // Check if the node is a target of aria-owns, and if so, don't process
1409
0
        // it here and let DoARIAOwnsRelocation process it.
1410
0
        AttrRelProviderArray* list =
1411
0
          mDependentIDsHash.Get(nsDependentAtomString(content->GetID()));
1412
0
        bool shouldProcess = !!list;
1413
0
        if (shouldProcess) {
1414
0
          for (uint32_t idx = 0; idx < list->Length(); idx++) {
1415
0
            if (list->ElementAt(idx)->mRelAttr == nsGkAtoms::aria_owns) {
1416
0
              shouldProcess = false;
1417
0
              break;
1418
0
            }
1419
0
          }
1420
0
1421
0
          if (shouldProcess) {
1422
0
            ProcessContentInserted(container, content);
1423
0
          }
1424
0
        }
1425
0
      }
1426
0
    }
1427
0
  }
1428
0
1429
0
  mInvalidationList.Clear();
1430
0
}
1431
1432
Accessible*
1433
DocAccessible::GetAccessibleEvenIfNotInMap(nsINode* aNode) const
1434
0
{
1435
0
if (!aNode->IsContent() || !aNode->AsContent()->IsHTMLElement(nsGkAtoms::area))
1436
0
    return GetAccessible(aNode);
1437
0
1438
0
  // XXX Bug 135040, incorrect when multiple images use the same map.
1439
0
  nsIFrame* frame = aNode->AsContent()->GetPrimaryFrame();
1440
0
  nsImageFrame* imageFrame = do_QueryFrame(frame);
1441
0
  if (imageFrame) {
1442
0
    Accessible* parent = GetAccessible(imageFrame->GetContent());
1443
0
    if (parent) {
1444
0
      Accessible* area =
1445
0
        parent->AsImageMap()->GetChildAccessibleFor(aNode);
1446
0
      if (area)
1447
0
        return area;
1448
0
1449
0
      return nullptr;
1450
0
    }
1451
0
  }
1452
0
1453
0
  return GetAccessible(aNode);
1454
0
}
1455
1456
////////////////////////////////////////////////////////////////////////////////
1457
// Protected members
1458
1459
void
1460
DocAccessible::NotifyOfLoading(bool aIsReloading)
1461
0
{
1462
0
  // Mark the document accessible as loading, if it stays alive then we'll mark
1463
0
  // it as loaded when we receive proper notification.
1464
0
  mLoadState &= ~eDOMLoaded;
1465
0
1466
0
  if (!IsLoadEventTarget())
1467
0
    return;
1468
0
1469
0
  if (aIsReloading && !mLoadEventType) {
1470
0
    // Fire reload and state busy events on existing document accessible while
1471
0
    // event from user input flag can be calculated properly and accessible
1472
0
    // is alive. When new document gets loaded then this one is destroyed.
1473
0
    RefPtr<AccEvent> reloadEvent =
1474
0
      new AccEvent(nsIAccessibleEvent::EVENT_DOCUMENT_RELOAD, this);
1475
0
    nsEventShell::FireEvent(reloadEvent);
1476
0
  }
1477
0
1478
0
  // Fire state busy change event. Use delayed event since we don't care
1479
0
  // actually if event isn't delivered when the document goes away like a shot.
1480
0
  RefPtr<AccEvent> stateEvent =
1481
0
    new AccStateChangeEvent(this, states::BUSY, true);
1482
0
  FireDelayedEvent(stateEvent);
1483
0
}
1484
1485
void
1486
DocAccessible::DoInitialUpdate()
1487
0
{
1488
0
  if (nsCoreUtils::IsTabDocument(mDocumentNode)) {
1489
0
    mDocFlags |= eTabDocument;
1490
0
    if (IPCAccessibilityActive()) {
1491
0
      nsIDocShell* docShell = mDocumentNode->GetDocShell();
1492
0
      if (RefPtr<dom::TabChild> tabChild = dom::TabChild::GetFrom(docShell)) {
1493
0
        DocAccessibleChild* ipcDoc = new DocAccessibleChild(this, tabChild);
1494
0
        SetIPCDoc(ipcDoc);
1495
0
        if (IsRoot()) {
1496
0
          tabChild->SetTopLevelDocAccessibleChild(ipcDoc);
1497
0
        }
1498
0
1499
#if defined(XP_WIN)
1500
        IAccessibleHolder holder(CreateHolderFromAccessible(WrapNotNull(this)));
1501
        MOZ_ASSERT(!holder.IsNull());
1502
        int32_t childID = AccessibleWrap::GetChildIDFor(this);
1503
#else
1504
        int32_t holder = 0, childID = 0;
1505
0
#endif
1506
0
        tabChild->SendPDocAccessibleConstructor(ipcDoc, nullptr, 0, childID,
1507
0
                                                holder);
1508
0
      }
1509
0
    }
1510
0
  }
1511
0
1512
0
  mLoadState |= eTreeConstructed;
1513
0
1514
0
  // Set up a root element and ARIA role mapping.
1515
0
  UpdateRootElIfNeeded();
1516
0
1517
0
  // Build initial tree.
1518
0
  CacheChildrenInSubtree(this);
1519
0
#ifdef A11Y_LOG
1520
0
  if (logging::IsEnabled(logging::eVerbose)) {
1521
0
    logging::Tree("TREE", "Initial subtree", this);
1522
0
  }
1523
0
#endif
1524
0
1525
0
  // Fire reorder event after the document tree is constructed. Note, since
1526
0
  // this reorder event is processed by parent document then events targeted to
1527
0
  // this document may be fired prior to this reorder event. If this is
1528
0
  // a problem then consider to keep event processing per tab document.
1529
0
  if (!IsRoot()) {
1530
0
    RefPtr<AccReorderEvent> reorderEvent = new AccReorderEvent(Parent());
1531
0
    ParentDocument()->FireDelayedEvent(reorderEvent);
1532
0
  }
1533
0
1534
0
  if (IPCAccessibilityActive()) {
1535
0
    DocAccessibleChild* ipcDoc = IPCDoc();
1536
0
    MOZ_ASSERT(ipcDoc);
1537
0
    if (ipcDoc) {
1538
0
      for (auto idx = 0U; idx < mChildren.Length(); idx++) {
1539
0
        ipcDoc->InsertIntoIpcTree(this, mChildren.ElementAt(idx), idx);
1540
0
      }
1541
0
    }
1542
0
  }
1543
0
}
1544
1545
void
1546
DocAccessible::ProcessLoad()
1547
0
{
1548
0
  mLoadState |= eCompletelyLoaded;
1549
0
1550
0
#ifdef A11Y_LOG
1551
0
  if (logging::IsEnabled(logging::eDocLoad))
1552
0
    logging::DocCompleteLoad(this, IsLoadEventTarget());
1553
0
#endif
1554
0
1555
0
  // Do not fire document complete/stop events for root chrome document
1556
0
  // accessibles and for frame/iframe documents because
1557
0
  // a) screen readers start working on focus event in the case of root chrome
1558
0
  // documents
1559
0
  // b) document load event on sub documents causes screen readers to act is if
1560
0
  // entire page is reloaded.
1561
0
  if (!IsLoadEventTarget())
1562
0
    return;
1563
0
1564
0
  // Fire complete/load stopped if the load event type is given.
1565
0
  if (mLoadEventType) {
1566
0
    RefPtr<AccEvent> loadEvent = new AccEvent(mLoadEventType, this);
1567
0
    FireDelayedEvent(loadEvent);
1568
0
1569
0
    mLoadEventType = 0;
1570
0
  }
1571
0
1572
0
  // Fire busy state change event.
1573
0
  RefPtr<AccEvent> stateEvent =
1574
0
    new AccStateChangeEvent(this, states::BUSY, false);
1575
0
  FireDelayedEvent(stateEvent);
1576
0
}
1577
1578
void
1579
DocAccessible::AddDependentIDsFor(Accessible* aRelProvider, nsAtom* aRelAttr)
1580
0
{
1581
0
  dom::Element* relProviderEl = aRelProvider->Elm();
1582
0
  if (!relProviderEl)
1583
0
    return;
1584
0
1585
0
  for (uint32_t idx = 0; idx < kRelationAttrsLen; idx++) {
1586
0
    nsAtom* relAttr = *kRelationAttrs[idx];
1587
0
    if (aRelAttr && aRelAttr != relAttr)
1588
0
      continue;
1589
0
1590
0
    if (relAttr == nsGkAtoms::_for) {
1591
0
      if (!relProviderEl->IsAnyOfHTMLElements(nsGkAtoms::label,
1592
0
                                               nsGkAtoms::output))
1593
0
        continue;
1594
0
1595
0
    } else if (relAttr == nsGkAtoms::control) {
1596
0
      if (!relProviderEl->IsAnyOfXULElements(nsGkAtoms::label,
1597
0
                                              nsGkAtoms::description))
1598
0
        continue;
1599
0
    }
1600
0
1601
0
    IDRefsIterator iter(this, relProviderEl, relAttr);
1602
0
    while (true) {
1603
0
      const nsDependentSubstring id = iter.NextID();
1604
0
      if (id.IsEmpty())
1605
0
        break;
1606
0
1607
0
      nsIContent* dependentContent = iter.GetElem(id);
1608
0
      if (relAttr == nsGkAtoms::aria_owns && dependentContent &&
1609
0
          !aRelProvider->IsAcceptableChild(dependentContent))
1610
0
        continue;
1611
0
1612
0
      AttrRelProviderArray* providers = mDependentIDsHash.Get(id);
1613
0
      if (!providers) {
1614
0
        providers = new AttrRelProviderArray();
1615
0
        if (providers) {
1616
0
          mDependentIDsHash.Put(id, providers);
1617
0
        }
1618
0
      }
1619
0
1620
0
      if (providers) {
1621
0
        AttrRelProvider* provider =
1622
0
          new AttrRelProvider(relAttr, relProviderEl);
1623
0
        if (provider) {
1624
0
          providers->AppendElement(provider);
1625
0
1626
0
          // We've got here during the children caching. If the referenced
1627
0
          // content is not accessible then store it to pend its container
1628
0
          // children invalidation (this happens immediately after the caching
1629
0
          // is finished).
1630
0
          if (dependentContent) {
1631
0
            if (!HasAccessible(dependentContent)) {
1632
0
              mInvalidationList.AppendElement(dependentContent);
1633
0
            }
1634
0
          }
1635
0
        }
1636
0
      }
1637
0
    }
1638
0
1639
0
    // If the relation attribute is given then we don't have anything else to
1640
0
    // check.
1641
0
    if (aRelAttr)
1642
0
      break;
1643
0
  }
1644
0
1645
0
  // Make sure to schedule the tree update if needed.
1646
0
  mNotificationController->ScheduleProcessing();
1647
0
}
1648
1649
void
1650
DocAccessible::RemoveDependentIDsFor(Accessible* aRelProvider,
1651
                                     nsAtom* aRelAttr)
1652
0
{
1653
0
  dom::Element* relProviderElm = aRelProvider->Elm();
1654
0
  if (!relProviderElm)
1655
0
    return;
1656
0
1657
0
  for (uint32_t idx = 0; idx < kRelationAttrsLen; idx++) {
1658
0
    nsAtom* relAttr = *kRelationAttrs[idx];
1659
0
    if (aRelAttr && aRelAttr != *kRelationAttrs[idx])
1660
0
      continue;
1661
0
1662
0
    IDRefsIterator iter(this, relProviderElm, relAttr);
1663
0
    while (true) {
1664
0
      const nsDependentSubstring id = iter.NextID();
1665
0
      if (id.IsEmpty())
1666
0
        break;
1667
0
1668
0
      AttrRelProviderArray* providers = mDependentIDsHash.Get(id);
1669
0
      if (providers) {
1670
0
        for (uint32_t jdx = 0; jdx < providers->Length(); ) {
1671
0
          AttrRelProvider* provider = (*providers)[jdx];
1672
0
          if (provider->mRelAttr == relAttr &&
1673
0
              provider->mContent == relProviderElm)
1674
0
            providers->RemoveElement(provider);
1675
0
          else
1676
0
            jdx++;
1677
0
        }
1678
0
        if (providers->Length() == 0)
1679
0
          mDependentIDsHash.Remove(id);
1680
0
      }
1681
0
    }
1682
0
1683
0
    // If the relation attribute is given then we don't have anything else to
1684
0
    // check.
1685
0
    if (aRelAttr)
1686
0
      break;
1687
0
  }
1688
0
}
1689
1690
bool
1691
DocAccessible::UpdateAccessibleOnAttrChange(dom::Element* aElement,
1692
                                            nsAtom* aAttribute)
1693
0
{
1694
0
  if (aAttribute == nsGkAtoms::role) {
1695
0
    // It is common for js libraries to set the role on the body element after
1696
0
    // the document has loaded. In this case we just update the role map entry.
1697
0
    if (mContent == aElement) {
1698
0
      SetRoleMapEntry(aria::GetRoleMap(aElement));
1699
0
      if (mIPCDoc) {
1700
0
        mIPCDoc->SendRoleChangedEvent(Role());
1701
0
      }
1702
0
1703
0
      return true;
1704
0
    }
1705
0
1706
0
    // Recreate the accessible when role is changed because we might require a
1707
0
    // different accessible class for the new role or the accessible may expose
1708
0
    // a different sets of interfaces (COM restriction).
1709
0
    RecreateAccessible(aElement);
1710
0
1711
0
    return true;
1712
0
  }
1713
0
1714
0
  if (aAttribute == nsGkAtoms::href) {
1715
0
    // Not worth the expense to ensure which namespace these are in. It doesn't
1716
0
    // kill use to recreate the accessible even if the attribute was used in
1717
0
    // the wrong namespace or an element that doesn't support it.
1718
0
1719
0
    // Make sure the accessible is recreated asynchronously to allow the content
1720
0
    // to handle the attribute change.
1721
0
    RecreateAccessible(aElement);
1722
0
    return true;
1723
0
  }
1724
0
1725
0
  if (aAttribute == nsGkAtoms::aria_multiselectable &&
1726
0
      aElement->HasAttr(kNameSpaceID_None, nsGkAtoms::role)) {
1727
0
    // This affects whether the accessible supports SelectAccessible.
1728
0
    // COM says we cannot change what interfaces are supported on-the-fly,
1729
0
    // so invalidate this object. A new one will be created on demand.
1730
0
    RecreateAccessible(aElement);
1731
0
1732
0
    return true;
1733
0
  }
1734
0
1735
0
  return false;
1736
0
}
1737
1738
void
1739
DocAccessible::UpdateRootElIfNeeded()
1740
0
{
1741
0
  dom::Element* rootEl = mDocumentNode->GetBodyElement();
1742
0
  if (!rootEl) {
1743
0
    rootEl = mDocumentNode->GetRootElement();
1744
0
  }
1745
0
  if (rootEl != mContent) {
1746
0
    mContent = rootEl;
1747
0
    SetRoleMapEntry(aria::GetRoleMap(rootEl));
1748
0
    if (mIPCDoc) {
1749
0
      mIPCDoc->SendRoleChangedEvent(Role());
1750
0
    }
1751
0
  }
1752
0
}
1753
1754
/**
1755
 * Content insertion helper.
1756
 */
1757
class InsertIterator final
1758
{
1759
public:
1760
  InsertIterator(Accessible* aContext,
1761
                 const nsTArray<nsCOMPtr<nsIContent> >* aNodes) :
1762
    mChild(nullptr), mChildBefore(nullptr), mWalker(aContext),
1763
    mNodes(aNodes), mNodesIdx(0)
1764
0
  {
1765
0
    MOZ_ASSERT(aContext, "No context");
1766
0
    MOZ_ASSERT(aNodes, "No nodes to search for accessible elements");
1767
0
    MOZ_COUNT_CTOR(InsertIterator);
1768
0
  }
1769
0
  ~InsertIterator() { MOZ_COUNT_DTOR(InsertIterator); }
1770
1771
0
  Accessible* Context() const { return mWalker.Context(); }
1772
0
  Accessible* Child() const { return mChild; }
1773
0
  Accessible* ChildBefore() const { return mChildBefore; }
1774
0
  DocAccessible* Document() const { return mWalker.Document(); }
1775
1776
  /**
1777
   * Iterates to a next accessible within the inserted content.
1778
   */
1779
  bool Next();
1780
1781
  void Rejected()
1782
0
  {
1783
0
    mChild = nullptr;
1784
0
    mChildBefore = nullptr;
1785
0
  }
1786
1787
private:
1788
  Accessible* mChild;
1789
  Accessible* mChildBefore;
1790
  TreeWalker mWalker;
1791
1792
  const nsTArray<nsCOMPtr<nsIContent> >* mNodes;
1793
  uint32_t mNodesIdx;
1794
};
1795
1796
bool
1797
InsertIterator::Next()
1798
0
{
1799
0
  if (mNodesIdx > 0) {
1800
0
    Accessible* nextChild = mWalker.Next();
1801
0
    if (nextChild) {
1802
0
      mChildBefore = mChild;
1803
0
      mChild = nextChild;
1804
0
      return true;
1805
0
    }
1806
0
  }
1807
0
1808
0
  while (mNodesIdx < mNodes->Length()) {
1809
0
    // Ignore nodes that are not contained by the container anymore.
1810
0
1811
0
    // The container might be changed, for example, because of the subsequent
1812
0
    // overlapping content insertion (i.e. other content was inserted between
1813
0
    // this inserted content and its container or the content was reinserted
1814
0
    // into different container of unrelated part of tree). To avoid a double
1815
0
    // processing of the content insertion ignore this insertion notification.
1816
0
    // Note, the inserted content might be not in tree at all at this point
1817
0
    // what means there's no container. Ignore the insertion too.
1818
0
    nsIContent* prevNode = mNodes->SafeElementAt(mNodesIdx - 1);
1819
0
    nsIContent* node = mNodes->ElementAt(mNodesIdx++);
1820
0
    Accessible* container = Document()->
1821
0
      AccessibleOrTrueContainer(node, DocAccessible::eNoContainerIfARIAHidden);
1822
0
    if (container != Context()) {
1823
0
      continue;
1824
0
    }
1825
0
1826
0
    // HTML comboboxes have no-content list accessible as an intermediate
1827
0
    // containing all options.
1828
0
    if (container->IsHTMLCombobox()) {
1829
0
      container = container->FirstChild();
1830
0
    }
1831
0
1832
0
    if (!container->IsAcceptableChild(node)) {
1833
0
      continue;
1834
0
    }
1835
0
1836
0
#ifdef A11Y_LOG
1837
0
    logging::TreeInfo("traversing an inserted node", logging::eVerbose,
1838
0
                      "container", container, "node", node);
1839
0
#endif
1840
0
1841
0
    // If inserted nodes are siblings then just move the walker next.
1842
0
    if (mChild && prevNode && prevNode->GetNextSibling() == node) {
1843
0
      Accessible* nextChild = mWalker.Scope(node);
1844
0
      if (nextChild) {
1845
0
        mChildBefore = mChild;
1846
0
        mChild = nextChild;
1847
0
        return true;
1848
0
      }
1849
0
    }
1850
0
    else {
1851
0
      TreeWalker finder(container);
1852
0
      if (finder.Seek(node)) {
1853
0
        mChild = mWalker.Scope(node);
1854
0
        if (mChild) {
1855
0
          MOZ_ASSERT(!mChild->IsRelocated(), "child cannot be aria owned");
1856
0
          mChildBefore = finder.Prev();
1857
0
          return true;
1858
0
        }
1859
0
      }
1860
0
    }
1861
0
  }
1862
0
1863
0
  return false;
1864
0
}
1865
1866
void
1867
DocAccessible::ProcessContentInserted(Accessible* aContainer,
1868
                                      const nsTArray<nsCOMPtr<nsIContent> >* aNodes)
1869
0
{
1870
0
  // Process insertions if the container accessible is still in tree.
1871
0
  if (!aContainer->IsInDocument()) {
1872
0
    return;
1873
0
  }
1874
0
1875
0
  // If new root content has been inserted then update it.
1876
0
  if (aContainer == this) {
1877
0
    UpdateRootElIfNeeded();
1878
0
  }
1879
0
1880
0
  InsertIterator iter(aContainer, aNodes);
1881
0
  if (!iter.Next()) {
1882
0
    return;
1883
0
  }
1884
0
1885
0
#ifdef A11Y_LOG
1886
0
  logging::TreeInfo("children before insertion", logging::eVerbose,
1887
0
                    aContainer);
1888
0
#endif
1889
0
1890
0
  TreeMutation mt(aContainer);
1891
0
  do {
1892
0
    Accessible* parent = iter.Child()->Parent();
1893
0
    if (parent) {
1894
0
      if (parent != aContainer) {
1895
0
#ifdef A11Y_LOG
1896
0
        logging::TreeInfo("stealing accessible", 0,
1897
0
                          "old parent", parent, "new parent",
1898
0
                          aContainer, "child", iter.Child(), nullptr);
1899
0
#endif
1900
0
        MOZ_ASSERT_UNREACHABLE("stealing accessible");
1901
0
        continue;
1902
0
      }
1903
0
1904
0
#ifdef A11Y_LOG
1905
0
      logging::TreeInfo("binding to same parent", logging::eVerbose,
1906
0
                        "parent", aContainer, "child", iter.Child(), nullptr);
1907
0
#endif
1908
0
      continue;
1909
0
    }
1910
0
1911
0
    if (aContainer->InsertAfter(iter.Child(), iter.ChildBefore())) {
1912
0
#ifdef A11Y_LOG
1913
0
      logging::TreeInfo("accessible was inserted", 0,
1914
0
                        "container", aContainer, "child", iter.Child(), nullptr);
1915
0
#endif
1916
0
1917
0
      CreateSubtree(iter.Child());
1918
0
      mt.AfterInsertion(iter.Child());
1919
0
      continue;
1920
0
    }
1921
0
1922
0
    MOZ_ASSERT_UNREACHABLE("accessible was rejected");
1923
0
    iter.Rejected();
1924
0
  } while (iter.Next());
1925
0
1926
0
  mt.Done();
1927
0
1928
0
#ifdef A11Y_LOG
1929
0
  logging::TreeInfo("children after insertion", logging::eVerbose,
1930
0
                    aContainer);
1931
0
#endif
1932
0
1933
0
  FireEventsOnInsertion(aContainer);
1934
0
}
1935
1936
void
1937
DocAccessible::ProcessContentInserted(Accessible* aContainer, nsIContent* aNode)
1938
0
{
1939
0
  if (!aContainer->IsInDocument()) {
1940
0
    return;
1941
0
  }
1942
0
1943
0
#ifdef A11Y_LOG
1944
0
  logging::TreeInfo("children before insertion", logging::eVerbose, aContainer);
1945
0
#endif
1946
0
1947
0
#ifdef A11Y_LOG
1948
0
  logging::TreeInfo("traversing an inserted node", logging::eVerbose,
1949
0
                    "container", aContainer, "node", aNode);
1950
0
#endif
1951
0
1952
0
  TreeWalker walker(aContainer);
1953
0
  if (aContainer->IsAcceptableChild(aNode) && walker.Seek(aNode)) {
1954
0
    Accessible* child = GetAccessible(aNode);
1955
0
    if (!child) {
1956
0
      child = GetAccService()->CreateAccessible(aNode, aContainer);
1957
0
    }
1958
0
1959
0
    if (child) {
1960
0
      TreeMutation mt(aContainer);
1961
0
      if (!aContainer->InsertAfter(child, walker.Prev())) {
1962
0
        return;
1963
0
      }
1964
0
      CreateSubtree(child);
1965
0
      mt.AfterInsertion(child);
1966
0
      mt.Done();
1967
0
1968
0
      FireEventsOnInsertion(aContainer);
1969
0
    }
1970
0
  }
1971
0
1972
0
#ifdef A11Y_LOG
1973
0
  logging::TreeInfo("children after insertion", logging::eVerbose, aContainer);
1974
0
#endif
1975
0
}
1976
1977
void
1978
DocAccessible::FireEventsOnInsertion(Accessible* aContainer)
1979
0
{
1980
0
  // Check to see if change occurred inside an alert, and fire an EVENT_ALERT
1981
0
  // if it did.
1982
0
  if (aContainer->IsAlert() || aContainer->IsInsideAlert()) {
1983
0
    Accessible* ancestor = aContainer;
1984
0
    do {
1985
0
      if (ancestor->IsAlert()) {
1986
0
        FireDelayedEvent(nsIAccessibleEvent::EVENT_ALERT, ancestor);
1987
0
        break;
1988
0
      }
1989
0
    }
1990
0
    while ((ancestor = ancestor->Parent()));
1991
0
  }
1992
0
}
1993
1994
void
1995
DocAccessible::ContentRemoved(Accessible* aChild)
1996
0
{
1997
0
  Accessible* parent = aChild->Parent();
1998
0
  MOZ_DIAGNOSTIC_ASSERT(parent, "Unattached accessible from tree");
1999
0
2000
0
#ifdef A11Y_LOG
2001
0
  logging::TreeInfo("process content removal", 0,
2002
0
                    "container", parent, "child", aChild, nullptr);
2003
0
#endif
2004
0
2005
0
  // XXX: event coalescence may kill us
2006
0
  RefPtr<Accessible> kungFuDeathGripChild(aChild);
2007
0
2008
0
  TreeMutation mt(parent);
2009
0
  mt.BeforeRemoval(aChild);
2010
0
2011
0
  if (aChild->IsDefunct()) {
2012
0
    MOZ_ASSERT_UNREACHABLE("Event coalescence killed the accessible");
2013
0
    mt.Done();
2014
0
    return;
2015
0
  }
2016
0
2017
0
  MOZ_DIAGNOSTIC_ASSERT(aChild->Parent(), "Alive but unparented #1");
2018
0
2019
0
  if (aChild->IsRelocated()) {
2020
0
    nsTArray<RefPtr<Accessible> >* owned = mARIAOwnsHash.Get(parent);
2021
0
    MOZ_ASSERT(owned, "IsRelocated flag is out of sync with mARIAOwnsHash");
2022
0
    owned->RemoveElement(aChild);
2023
0
    if (owned->Length() == 0) {
2024
0
      mARIAOwnsHash.Remove(parent);
2025
0
    }
2026
0
  }
2027
0
  MOZ_DIAGNOSTIC_ASSERT(aChild->Parent(), "Unparented #2");
2028
0
  parent->RemoveChild(aChild);
2029
0
  UncacheChildrenInSubtree(aChild);
2030
0
2031
0
  mt.Done();
2032
0
}
2033
2034
void
2035
DocAccessible::ContentRemoved(nsIContent* aContentNode)
2036
0
{
2037
0
  // If child node is not accessible then look for its accessible children.
2038
0
  Accessible* acc = GetAccessible(aContentNode);
2039
0
  if (acc) {
2040
0
    ContentRemoved(acc);
2041
0
  }
2042
0
2043
0
  dom::AllChildrenIterator iter =
2044
0
    dom::AllChildrenIterator(aContentNode, nsIContent::eAllChildren, true);
2045
0
  while (nsIContent* childNode = iter.GetNextChild()) {
2046
0
    ContentRemoved(childNode);
2047
0
  }
2048
0
}
2049
2050
bool
2051
DocAccessible::RelocateARIAOwnedIfNeeded(nsIContent* aElement)
2052
0
{
2053
0
  if (!aElement->HasID())
2054
0
    return false;
2055
0
2056
0
  AttrRelProviderArray* list =
2057
0
    mDependentIDsHash.Get(nsDependentAtomString(aElement->GetID()));
2058
0
  if (list) {
2059
0
    for (uint32_t idx = 0; idx < list->Length(); idx++) {
2060
0
      if (list->ElementAt(idx)->mRelAttr == nsGkAtoms::aria_owns) {
2061
0
        Accessible* owner = GetAccessible(list->ElementAt(idx)->mContent);
2062
0
        if (owner) {
2063
0
          mNotificationController->ScheduleRelocation(owner);
2064
0
          return true;
2065
0
        }
2066
0
      }
2067
0
    }
2068
0
  }
2069
0
2070
0
  return false;
2071
0
}
2072
2073
void
2074
DocAccessible::DoARIAOwnsRelocation(Accessible* aOwner)
2075
0
{
2076
0
  MOZ_ASSERT(aOwner, "aOwner must be a valid pointer");
2077
0
  MOZ_ASSERT(aOwner->Elm(), "aOwner->Elm() must be a valid pointer");
2078
0
2079
0
#ifdef A11Y_LOG
2080
0
  logging::TreeInfo("aria owns relocation", logging::eVerbose, aOwner);
2081
0
#endif
2082
0
2083
0
  nsTArray<RefPtr<Accessible> >* owned = mARIAOwnsHash.LookupOrAdd(aOwner);
2084
0
2085
0
  IDRefsIterator iter(this, aOwner->Elm(), nsGkAtoms::aria_owns);
2086
0
  uint32_t idx = 0;
2087
0
  while (nsIContent* childEl = iter.NextElem()) {
2088
0
    Accessible* child = GetAccessible(childEl);
2089
0
    auto insertIdx = aOwner->ChildCount() - owned->Length() + idx;
2090
0
2091
0
    // Make an attempt to create an accessible if it wasn't created yet.
2092
0
    if (!child) {
2093
0
      // An owned child cannot be an ancestor of the owner.
2094
0
      if (nsContentUtils::ContentIsDescendantOf(aOwner->Elm(), childEl)) {
2095
0
        continue;
2096
0
      }
2097
0
2098
0
      if (aOwner->IsAcceptableChild(childEl)) {
2099
0
        child = GetAccService()->CreateAccessible(childEl, aOwner);
2100
0
        if (child) {
2101
0
          TreeMutation imut(aOwner);
2102
0
          aOwner->InsertChildAt(insertIdx, child);
2103
0
          imut.AfterInsertion(child);
2104
0
          imut.Done();
2105
0
2106
0
          child->SetRelocated(true);
2107
0
          owned->InsertElementAt(idx, child);
2108
0
          idx++;
2109
0
2110
0
          // Create subtree before adjusting the insertion index, since subtree
2111
0
          // creation may alter children in the container.
2112
0
          CreateSubtree(child);
2113
0
          FireEventsOnInsertion(aOwner);
2114
0
        }
2115
0
      }
2116
0
      continue;
2117
0
    }
2118
0
2119
0
#ifdef A11Y_LOG
2120
0
  logging::TreeInfo("aria owns traversal", logging::eVerbose,
2121
0
                    "candidate", child, nullptr);
2122
0
#endif
2123
0
2124
0
    if (owned->IndexOf(child) < idx) {
2125
0
      continue; // ignore second entry of same ID
2126
0
    }
2127
0
2128
0
    // Same child on same position, no change.
2129
0
    if (child->Parent() == aOwner) {
2130
0
      int32_t indexInParent = child->IndexInParent();
2131
0
2132
0
      // The child is being placed in its current index,
2133
0
      // eg. aria-owns='id1 id2 id3' is changed to aria-owns='id3 id2 id1'.
2134
0
      if (indexInParent == static_cast<int32_t>(insertIdx)) {
2135
0
        MOZ_ASSERT(child->IsRelocated(),
2136
0
                   "A child, having an index in parent from aria ownded indices range, has to be aria owned");
2137
0
        MOZ_ASSERT(owned->ElementAt(idx) == child,
2138
0
                   "Unexpected child in ARIA owned array");
2139
0
        idx++;
2140
0
        continue;
2141
0
      }
2142
0
2143
0
      // The child is being inserted directly after its current index,
2144
0
      // resulting in a no-move case. This will happen when a parent aria-owns
2145
0
      // its last ordinal child:
2146
0
      // <ul aria-owns='id2'><li id='id1'></li><li id='id2'></li></ul>
2147
0
      if (indexInParent == static_cast<int32_t>(insertIdx) - 1) {
2148
0
        MOZ_ASSERT(!child->IsRelocated(), "Child should be in its ordinal position");
2149
0
        child->SetRelocated(true);
2150
0
        owned->InsertElementAt(idx, child);
2151
0
        idx++;
2152
0
        continue;
2153
0
      }
2154
0
    }
2155
0
2156
0
    MOZ_ASSERT(owned->SafeElementAt(idx) != child, "Already in place!");
2157
0
2158
0
    // A new child is found, check for loops.
2159
0
    if (child->Parent() != aOwner) {
2160
0
      // Child is aria-owned by another container, skip.
2161
0
      if (child->IsRelocated()) {
2162
0
        continue;
2163
0
      }
2164
0
2165
0
      Accessible* parent = aOwner;
2166
0
      while (parent && parent != child && !parent->IsDoc()) {
2167
0
        parent = parent->Parent();
2168
0
      }
2169
0
      // A referred child cannot be a parent of the owner.
2170
0
      if (parent == child) {
2171
0
        continue;
2172
0
      }
2173
0
    }
2174
0
2175
0
    if (MoveChild(child, aOwner, insertIdx)) {
2176
0
      child->SetRelocated(true);
2177
0
      MOZ_ASSERT(owned == mARIAOwnsHash.Get(aOwner));
2178
0
      owned = mARIAOwnsHash.LookupOrAdd(aOwner);
2179
0
      owned->InsertElementAt(idx, child);
2180
0
      idx++;
2181
0
    }
2182
0
  }
2183
0
2184
0
  // Put back children that are not seized anymore.
2185
0
  PutChildrenBack(owned, idx);
2186
0
  if (owned->Length() == 0) {
2187
0
    mARIAOwnsHash.Remove(aOwner);
2188
0
  }
2189
0
}
2190
2191
void
2192
DocAccessible::PutChildrenBack(nsTArray<RefPtr<Accessible> >* aChildren,
2193
                               uint32_t aStartIdx)
2194
0
{
2195
0
  MOZ_ASSERT(aStartIdx <= aChildren->Length(), "Wrong removal index");
2196
0
2197
0
  for (auto idx = aStartIdx; idx < aChildren->Length(); idx++) {
2198
0
    Accessible* child = aChildren->ElementAt(idx);
2199
0
    if (!child->IsInDocument()) {
2200
0
      continue;
2201
0
    }
2202
0
2203
0
    // Remove the child from the owner
2204
0
    Accessible* owner = child->Parent();
2205
0
    if (!owner) {
2206
0
      NS_ERROR("Cannot put the child back. No parent, a broken tree.");
2207
0
      continue;
2208
0
    }
2209
0
2210
0
#ifdef A11Y_LOG
2211
0
    logging::TreeInfo("aria owns put child back", 0,
2212
0
                      "old parent", owner, "child", child, nullptr);
2213
0
#endif
2214
0
2215
0
    // Unset relocated flag to find an insertion point for the child.
2216
0
    child->SetRelocated(false);
2217
0
2218
0
    nsIContent* content = child->GetContent();
2219
0
    int32_t idxInParent = -1;
2220
0
    Accessible* origContainer =
2221
0
      AccessibleOrTrueContainer(content->GetFlattenedTreeParentNode());
2222
0
    if (origContainer) {
2223
0
      TreeWalker walker(origContainer);
2224
0
      if (walker.Seek(content)) {
2225
0
        Accessible* prevChild = walker.Prev();
2226
0
        if (prevChild) {
2227
0
          idxInParent = prevChild->IndexInParent() + 1;
2228
0
          MOZ_DIAGNOSTIC_ASSERT(origContainer == prevChild->Parent(), "Broken tree");
2229
0
          origContainer = prevChild->Parent();
2230
0
        }
2231
0
        else {
2232
0
          idxInParent = 0;
2233
0
        }
2234
0
      }
2235
0
    }
2236
0
2237
0
    // The child may have already be in its ordinal place for 2 reasons:
2238
0
    // 1. It was the last ordinal child, and the first aria-owned child.
2239
0
    //    given:      <ul id="list" aria-owns="b"><li id="a"></li><li id="b"></li></ul>
2240
0
    //    after load: $("list").setAttribute("aria-owns", "");
2241
0
    // 2. The preceding adopted children were just reclaimed, eg:
2242
0
    //    given:      <ul id="list"><li id="b"></li></ul>
2243
0
    //    after load: $("list").setAttribute("aria-owns", "a b");
2244
0
    //    later:      $("list").setAttribute("aria-owns", "");
2245
0
    if (origContainer != owner || child->IndexInParent() != idxInParent) {
2246
0
      DebugOnly<bool> moved = MoveChild(child, origContainer, idxInParent);
2247
0
      MOZ_ASSERT(moved, "Failed to put child back.");
2248
0
    } else {
2249
0
      MOZ_ASSERT(!child->PrevSibling() || !child->PrevSibling()->IsRelocated(),
2250
0
                 "No relocated child should appear before this one");
2251
0
      MOZ_ASSERT(!child->NextSibling() || child->NextSibling()->IsRelocated(),
2252
0
                 "No ordinal child should appear after this one");
2253
0
    }
2254
0
  }
2255
0
2256
0
  aChildren->RemoveElementsAt(aStartIdx, aChildren->Length() - aStartIdx);
2257
0
}
2258
2259
bool
2260
DocAccessible::MoveChild(Accessible* aChild, Accessible* aNewParent,
2261
                         int32_t aIdxInParent)
2262
0
{
2263
0
  MOZ_ASSERT(aChild, "No child");
2264
0
  MOZ_ASSERT(aChild->Parent(), "No parent");
2265
0
  MOZ_ASSERT(aIdxInParent <= static_cast<int32_t>(aNewParent->ChildCount()),
2266
0
             "Wrong insertion point for a moving child");
2267
0
2268
0
  Accessible* curParent = aChild->Parent();
2269
0
2270
0
  if (!aNewParent->IsAcceptableChild(aChild->GetContent())) {
2271
0
    return false;
2272
0
  }
2273
0
2274
0
#ifdef A11Y_LOG
2275
0
  logging::TreeInfo("move child", 0,
2276
0
                    "old parent", curParent, "new parent", aNewParent,
2277
0
                    "child", aChild, nullptr);
2278
0
#endif
2279
0
2280
0
  // Forget aria-owns info in case of ARIA owned element. The caller is expected
2281
0
  // to update it if needed.
2282
0
  if (aChild->IsRelocated()) {
2283
0
    aChild->SetRelocated(false);
2284
0
    nsTArray<RefPtr<Accessible> >* owned = mARIAOwnsHash.Get(curParent);
2285
0
    MOZ_ASSERT(owned, "IsRelocated flag is out of sync with mARIAOwnsHash");
2286
0
    owned->RemoveElement(aChild);
2287
0
    if (owned->Length() == 0) {
2288
0
      mARIAOwnsHash.Remove(curParent);
2289
0
    }
2290
0
  }
2291
0
2292
0
  NotificationController::MoveGuard mguard(mNotificationController);
2293
0
2294
0
  if (curParent == aNewParent) {
2295
0
    MOZ_ASSERT(aChild->IndexInParent() != aIdxInParent, "No move case");
2296
0
    curParent->MoveChild(aIdxInParent, aChild);
2297
0
2298
0
#ifdef A11Y_LOG
2299
0
    logging::TreeInfo("move child: parent tree after",
2300
0
                      logging::eVerbose, curParent);
2301
0
#endif
2302
0
    return true;
2303
0
  }
2304
0
2305
0
  MOZ_ASSERT(aIdxInParent <= static_cast<int32_t>(aNewParent->ChildCount()),
2306
0
             "Wrong insertion point for a moving child");
2307
0
2308
0
  // If the child cannot be re-inserted into the tree, then make sure to remove
2309
0
  // it from its present parent and then shutdown it.
2310
0
  bool hasInsertionPoint = (aIdxInParent != -1) ||
2311
0
    (aIdxInParent <= static_cast<int32_t>(aNewParent->ChildCount()));
2312
0
2313
0
  TreeMutation rmut(curParent);
2314
0
  rmut.BeforeRemoval(aChild, hasInsertionPoint && TreeMutation::kNoShutdown);
2315
0
  curParent->RemoveChild(aChild);
2316
0
  rmut.Done();
2317
0
2318
0
  // No insertion point for the child.
2319
0
  if (!hasInsertionPoint) {
2320
0
    return true;
2321
0
  }
2322
0
2323
0
  TreeMutation imut(aNewParent);
2324
0
  aNewParent->InsertChildAt(aIdxInParent, aChild);
2325
0
  imut.AfterInsertion(aChild);
2326
0
  imut.Done();
2327
0
2328
0
#ifdef A11Y_LOG
2329
0
  logging::TreeInfo("move child: old parent tree after",
2330
0
                    logging::eVerbose, curParent);
2331
0
  logging::TreeInfo("move child: new parent tree after",
2332
0
                    logging::eVerbose, aNewParent);
2333
0
#endif
2334
0
2335
0
  return true;
2336
0
}
2337
2338
2339
void
2340
DocAccessible::CacheChildrenInSubtree(Accessible* aRoot,
2341
                                      Accessible** aFocusedAcc)
2342
0
{
2343
0
  // If the accessible is focused then report a focus event after all related
2344
0
  // mutation events.
2345
0
  if (aFocusedAcc && !*aFocusedAcc &&
2346
0
      FocusMgr()->HasDOMFocus(aRoot->GetContent()))
2347
0
    *aFocusedAcc = aRoot;
2348
0
2349
0
  Accessible* root = aRoot->IsHTMLCombobox() ? aRoot->FirstChild() : aRoot;
2350
0
  if (root->KidsFromDOM()) {
2351
0
    TreeMutation mt(root, TreeMutation::kNoEvents);
2352
0
    TreeWalker walker(root);
2353
0
    while (Accessible* child = walker.Next()) {
2354
0
      if (child->IsBoundToParent()) {
2355
0
        MoveChild(child, root, root->ChildCount());
2356
0
        continue;
2357
0
      }
2358
0
2359
0
      root->AppendChild(child);
2360
0
      mt.AfterInsertion(child);
2361
0
2362
0
      CacheChildrenInSubtree(child, aFocusedAcc);
2363
0
    }
2364
0
    mt.Done();
2365
0
  }
2366
0
2367
0
  // Fire events for ARIA elements.
2368
0
  if (!aRoot->HasARIARole()) {
2369
0
    return;
2370
0
  }
2371
0
2372
0
  // XXX: we should delay document load complete event if the ARIA document
2373
0
  // has aria-busy.
2374
0
  roles::Role role = aRoot->ARIARole();
2375
0
  if (!aRoot->IsDoc() && (role == roles::DIALOG || role == roles::NON_NATIVE_DOCUMENT)) {
2376
0
    FireDelayedEvent(nsIAccessibleEvent::EVENT_DOCUMENT_LOAD_COMPLETE, aRoot);
2377
0
  }
2378
0
}
2379
2380
void
2381
DocAccessible::UncacheChildrenInSubtree(Accessible* aRoot)
2382
0
{
2383
0
  aRoot->mStateFlags |= eIsNotInDocument;
2384
0
  RemoveDependentIDsFor(aRoot);
2385
0
2386
0
  nsTArray<RefPtr<Accessible> >* owned = mARIAOwnsHash.Get(aRoot);
2387
0
  uint32_t count = aRoot->ContentChildCount();
2388
0
  for (uint32_t idx = 0; idx < count; idx++) {
2389
0
    Accessible* child = aRoot->ContentChildAt(idx);
2390
0
2391
0
    if (child->IsRelocated()) {
2392
0
      MOZ_ASSERT(owned, "IsRelocated flag is out of sync with mARIAOwnsHash");
2393
0
      owned->RemoveElement(child);
2394
0
      if (owned->Length() == 0) {
2395
0
        mARIAOwnsHash.Remove(aRoot);
2396
0
        owned = nullptr;
2397
0
      }
2398
0
    }
2399
0
2400
0
    // Removing this accessible from the document doesn't mean anything about
2401
0
    // accessibles for subdocuments, so skip removing those from the tree.
2402
0
    if (!child->IsDoc()) {
2403
0
      UncacheChildrenInSubtree(child);
2404
0
    }
2405
0
  }
2406
0
2407
0
  if (aRoot->IsNodeMapEntry() &&
2408
0
      mNodeToAccessibleMap.Get(aRoot->GetNode()) == aRoot)
2409
0
    mNodeToAccessibleMap.Remove(aRoot->GetNode());
2410
0
}
2411
2412
void
2413
DocAccessible::ShutdownChildrenInSubtree(Accessible* aAccessible)
2414
0
{
2415
0
  // Traverse through children and shutdown them before this accessible. When
2416
0
  // child gets shutdown then it removes itself from children array of its
2417
0
  //parent. Use jdx index to process the cases if child is not attached to the
2418
0
  // parent and as result doesn't remove itself from its children.
2419
0
  uint32_t count = aAccessible->ContentChildCount();
2420
0
  for (uint32_t idx = 0, jdx = 0; idx < count; idx++) {
2421
0
    Accessible* child = aAccessible->ContentChildAt(jdx);
2422
0
    if (!child->IsBoundToParent()) {
2423
0
      NS_ERROR("Parent refers to a child, child doesn't refer to parent!");
2424
0
      jdx++;
2425
0
    }
2426
0
2427
0
    // Don't cross document boundaries. The outerdoc shutdown takes care about
2428
0
    // its subdocument.
2429
0
    if (!child->IsDoc())
2430
0
      ShutdownChildrenInSubtree(child);
2431
0
  }
2432
0
2433
0
  UnbindFromDocument(aAccessible);
2434
0
}
2435
2436
bool
2437
DocAccessible::IsLoadEventTarget() const
2438
0
{
2439
0
  nsCOMPtr<nsIDocShellTreeItem> treeItem = mDocumentNode->GetDocShell();
2440
0
  NS_ASSERTION(treeItem, "No document shell for document!");
2441
0
2442
0
  nsCOMPtr<nsIDocShellTreeItem> parentTreeItem;
2443
0
  treeItem->GetParent(getter_AddRefs(parentTreeItem));
2444
0
2445
0
  // Not a root document.
2446
0
  if (parentTreeItem) {
2447
0
    // Return true if it's either:
2448
0
    // a) tab document;
2449
0
    nsCOMPtr<nsIDocShellTreeItem> rootTreeItem;
2450
0
    treeItem->GetRootTreeItem(getter_AddRefs(rootTreeItem));
2451
0
    if (parentTreeItem == rootTreeItem)
2452
0
      return true;
2453
0
2454
0
    // b) frame/iframe document and its parent document is not in loading state
2455
0
    // Note: we can get notifications while document is loading (and thus
2456
0
    // while there's no parent document yet).
2457
0
    DocAccessible* parentDoc = ParentDocument();
2458
0
    return parentDoc && parentDoc->HasLoadState(eCompletelyLoaded);
2459
0
  }
2460
0
2461
0
  // It's content (not chrome) root document.
2462
0
  return (treeItem->ItemType() == nsIDocShellTreeItem::typeContent);
2463
0
}
2464
2465
void
2466
DocAccessible::DispatchScrollingEvent(uint32_t aEventType)
2467
0
{
2468
0
  nsIScrollableFrame* sf = mPresShell->GetRootScrollFrameAsScrollable();
2469
0
  if (!sf) {
2470
0
    return;
2471
0
  }
2472
0
2473
0
  int32_t appUnitsPerDevPixel = mPresShell->GetPresContext()->AppUnitsPerDevPixel();
2474
0
  LayoutDevicePoint scrollPoint = LayoutDevicePoint::FromAppUnits(
2475
0
    sf->GetScrollPosition(), appUnitsPerDevPixel) * mPresShell->GetResolution();
2476
0
2477
0
  LayoutDeviceRect scrollRange = LayoutDeviceRect::FromAppUnits(
2478
0
    sf->GetScrollRange(), appUnitsPerDevPixel);
2479
0
  scrollRange.ScaleRoundOut(mPresShell->GetResolution());
2480
0
2481
0
  RefPtr<AccEvent> event = new AccScrollingEvent(aEventType, this,
2482
0
                                                 scrollPoint.x, scrollPoint.y,
2483
0
                                                 scrollRange.width,
2484
0
                                                 scrollRange.height);
2485
0
2486
0
  nsEventShell::FireEvent(event);
2487
0
}
2488
2489
void
2490
DocAccessible::ARIAActiveDescendantIDMaybeMoved(dom::Element* aElm)
2491
0
{
2492
0
  nsINode* focusNode = FocusMgr()->FocusedDOMNode();
2493
0
  // The focused element must be within this document.
2494
0
  if (!focusNode || focusNode->OwnerDoc() != mDocumentNode) {
2495
0
    return;
2496
0
  }
2497
0
2498
0
  dom::Element* focusElm = nullptr;
2499
0
  if (focusNode == mDocumentNode) {
2500
0
    // The document is focused, so look for aria-activedescendant on the
2501
0
    // body/root.
2502
0
    focusElm = Elm();
2503
0
    if (!focusElm) {
2504
0
      return;
2505
0
    }
2506
0
  } else {
2507
0
    MOZ_ASSERT(focusNode->IsElement());
2508
0
    focusElm = focusNode->AsElement();
2509
0
  }
2510
0
2511
0
  // Check if the focus has aria-activedescendant and whether
2512
0
  // it refers to the id just set on aElm.
2513
0
  nsAutoString id;
2514
0
  aElm->GetAttr(kNameSpaceID_None, nsGkAtoms::id, id);
2515
0
  if (!focusElm->AttrValueIs(kNameSpaceID_None,
2516
0
      nsGkAtoms::aria_activedescendant, id, eCaseMatters)) {
2517
0
    return;
2518
0
  }
2519
0
2520
0
  // The aria-activedescendant target has probably changed.
2521
0
  Accessible* acc = GetAccessibleEvenIfNotInMapOrContainer(focusNode);
2522
0
  if (!acc) {
2523
0
    return;
2524
0
  }
2525
0
2526
0
  // The active descendant might have just been inserted and may not be in the
2527
0
  // tree yet. Therefore, schedule this async to ensure the tree is up to date.
2528
0
  mNotificationController->ScheduleNotification<DocAccessible, Accessible>
2529
0
    (this, &DocAccessible::ARIAActiveDescendantChanged, acc);
2530
0
}