forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nsDocAccessible.cpp
2057 lines (1731 loc) · 66.5 KB
/
nsDocAccessible.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 2003
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Original Author: Aaron Leventhal (aaronl@netscape.com)
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "AccIterator.h"
#include "States.h"
#include "nsAccCache.h"
#include "nsAccessibilityAtoms.h"
#include "nsAccessibilityService.h"
#include "nsAccTreeWalker.h"
#include "nsAccUtils.h"
#include "nsRootAccessible.h"
#include "nsTextEquivUtils.h"
#include "nsIMutableArray.h"
#include "nsICommandManager.h"
#include "nsIDocShell.h"
#include "nsIDocShellTreeItem.h"
#include "nsIDocument.h"
#include "nsIDOMAttr.h"
#include "nsIDOMCharacterData.h"
#include "nsIDOMDocument.h"
#include "nsIDOMDocumentType.h"
#include "nsIDOMXULDocument.h"
#include "nsIDOMMutationEvent.h"
#include "nsPIDOMWindow.h"
#include "nsIDOMXULPopupElement.h"
#include "nsIEditingSession.h"
#include "nsEventStateManager.h"
#include "nsIFrame.h"
#include "nsHTMLSelectAccessible.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsINameSpaceManager.h"
#include "nsIPresShell.h"
#include "nsIServiceManager.h"
#include "nsIViewManager.h"
#include "nsIScrollableFrame.h"
#include "nsUnicharUtils.h"
#include "nsIURI.h"
#include "nsIWebNavigation.h"
#include "nsFocusManager.h"
#include "mozilla/dom/Element.h"
#ifdef MOZ_XUL
#include "nsIXULDocument.h"
#endif
namespace dom = mozilla::dom;
using namespace mozilla::a11y;
////////////////////////////////////////////////////////////////////////////////
// Static member initialization
static nsIAtom** kRelationAttrs[] =
{
&nsAccessibilityAtoms::aria_labelledby,
&nsAccessibilityAtoms::aria_describedby,
&nsAccessibilityAtoms::aria_owns,
&nsAccessibilityAtoms::aria_controls,
&nsAccessibilityAtoms::aria_flowto,
&nsAccessibilityAtoms::_for,
&nsAccessibilityAtoms::control
};
static const PRUint32 kRelationAttrsLen = NS_ARRAY_LENGTH(kRelationAttrs);
////////////////////////////////////////////////////////////////////////////////
// Constructor/desctructor
nsDocAccessible::
nsDocAccessible(nsIDocument *aDocument, nsIContent *aRootContent,
nsIWeakReference *aShell) :
nsHyperTextAccessibleWrap(aRootContent, aShell),
mDocument(aDocument), mScrollPositionChangedTicks(0),
mLoadState(eTreeConstructionPending), mLoadEventType(0)
{
mFlags |= eDocAccessible;
mDependentIDsHash.Init();
// XXX aaronl should we use an algorithm for the initial cache size?
mAccessibleCache.Init(kDefaultCacheSize);
mNodeToAccessibleMap.Init(kDefaultCacheSize);
// If this is a XUL Document, it should not implement nsHyperText
if (mDocument && mDocument->IsXUL())
mFlags &= ~eHyperTextAccessible;
// For GTK+ native window, we do nothing here.
if (!mDocument)
return;
// nsAccDocManager creates document accessible when scrollable frame is
// available already, it should be safe time to add scroll listener.
AddScrollListener();
}
nsDocAccessible::~nsDocAccessible()
{
}
////////////////////////////////////////////////////////////////////////////////
// nsISupports
NS_IMPL_CYCLE_COLLECTION_CLASS(nsDocAccessible)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsDocAccessible, nsAccessible)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mDocument)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NATIVE_MEMBER(mNotificationController,
NotificationController)
PRUint32 i, length = tmp->mChildDocuments.Length();
for (i = 0; i < length; ++i) {
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR_AMBIGUOUS(mChildDocuments[i],
nsIAccessible)
}
CycleCollectorTraverseCache(tmp->mAccessibleCache, &cb);
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsDocAccessible, nsAccessible)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mDocument)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mNotificationController)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSTARRAY(mChildDocuments)
tmp->mDependentIDsHash.Clear();
tmp->mNodeToAccessibleMap.Clear();
ClearCache(tmp->mAccessibleCache);
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsDocAccessible)
NS_INTERFACE_MAP_STATIC_AMBIGUOUS(nsDocAccessible)
NS_INTERFACE_MAP_ENTRY(nsIAccessibleDocument)
NS_INTERFACE_MAP_ENTRY(nsIDocumentObserver)
NS_INTERFACE_MAP_ENTRY(nsIMutationObserver)
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_INTERFACE_MAP_ENTRY(nsIObserver)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIAccessibleDocument)
foundInterface = 0;
nsresult status;
if (!foundInterface) {
// HTML document accessible must inherit from nsHyperTextAccessible to get
// support text interfaces. XUL document accessible doesn't need this.
// However at some point we may push <body> to implement the interfaces and
// return nsDocAccessible to inherit from nsAccessibleWrap.
status = IsHyperText() ?
nsHyperTextAccessible::QueryInterface(aIID,
(void**)&foundInterface) :
nsAccessible::QueryInterface(aIID, (void**)&foundInterface);
} else {
NS_ADDREF(foundInterface);
status = NS_OK;
}
*aInstancePtr = foundInterface;
return status;
}
NS_IMPL_ADDREF_INHERITED(nsDocAccessible, nsHyperTextAccessible)
NS_IMPL_RELEASE_INHERITED(nsDocAccessible, nsHyperTextAccessible)
////////////////////////////////////////////////////////////////////////////////
// nsIAccessible
NS_IMETHODIMP
nsDocAccessible::GetName(nsAString& aName)
{
nsresult rv = NS_OK;
aName.Truncate();
if (mParent) {
rv = mParent->GetName(aName); // Allow owning iframe to override the name
}
if (aName.IsEmpty()) {
// Allow name via aria-labelledby or title attribute
rv = nsAccessible::GetName(aName);
}
if (aName.IsEmpty()) {
rv = GetTitle(aName); // Try title element
}
if (aName.IsEmpty()) { // Last resort: use URL
rv = GetURL(aName);
}
return rv;
}
// nsAccessible public method
PRUint32
nsDocAccessible::NativeRole()
{
nsCOMPtr<nsIDocShellTreeItem> docShellTreeItem =
nsCoreUtils::GetDocShellTreeItemFor(mDocument);
if (docShellTreeItem) {
nsCOMPtr<nsIDocShellTreeItem> sameTypeRoot;
docShellTreeItem->GetSameTypeRootTreeItem(getter_AddRefs(sameTypeRoot));
PRInt32 itemType;
docShellTreeItem->GetItemType(&itemType);
if (sameTypeRoot == docShellTreeItem) {
// Root of content or chrome tree
if (itemType == nsIDocShellTreeItem::typeChrome)
return nsIAccessibleRole::ROLE_CHROME_WINDOW;
if (itemType == nsIDocShellTreeItem::typeContent) {
#ifdef MOZ_XUL
nsCOMPtr<nsIXULDocument> xulDoc(do_QueryInterface(mDocument));
if (xulDoc)
return nsIAccessibleRole::ROLE_APPLICATION;
#endif
return nsIAccessibleRole::ROLE_DOCUMENT;
}
}
else if (itemType == nsIDocShellTreeItem::typeContent) {
return nsIAccessibleRole::ROLE_DOCUMENT;
}
}
return nsIAccessibleRole::ROLE_PANE; // Fall back;
}
// nsAccessible public method
void
nsDocAccessible::SetRoleMapEntry(nsRoleMapEntry* aRoleMapEntry)
{
NS_ASSERTION(mDocument, "No document during initialization!");
if (!mDocument)
return;
mRoleMapEntry = aRoleMapEntry;
nsIDocument *parentDoc = mDocument->GetParentDocument();
if (!parentDoc)
return; // No parent document for the root document
// Allow use of ARIA role from outer to override
nsIContent *ownerContent = parentDoc->FindContentForSubDocument(mDocument);
if (ownerContent) {
nsRoleMapEntry *roleMapEntry = nsAccUtils::GetRoleMapEntry(ownerContent);
if (roleMapEntry)
mRoleMapEntry = roleMapEntry; // Override
}
}
void
nsDocAccessible::Description(nsString& aDescription)
{
if (mParent)
mParent->Description(aDescription);
if (aDescription.IsEmpty())
nsTextEquivUtils::
GetTextEquivFromIDRefs(this, nsAccessibilityAtoms::aria_describedby,
aDescription);
}
// nsAccessible public method
PRUint64
nsDocAccessible::NativeState()
{
// The root content of the document might be removed so that mContent is
// out of date.
PRUint64 state = (mContent->GetCurrentDoc() == mDocument) ?
0 : states::STALE;
#ifdef MOZ_XUL
nsCOMPtr<nsIXULDocument> xulDoc(do_QueryInterface(mDocument));
if (!xulDoc)
#endif
{
// XXX Need to invent better check to see if doc is focusable,
// which it should be if it is scrollable. A XUL document could be focusable.
// See bug 376803.
state |= states::FOCUSABLE;
if (gLastFocusedNode == mDocument)
state |= states::FOCUSED;
}
// Expose stale state until the document is ready (DOM is loaded and tree is
// constructed).
if (!HasLoadState(eReady))
state |= states::STALE;
// Expose state busy until the document and all its subdocuments is completely
// loaded.
if (!HasLoadState(eCompletelyLoaded))
state |= states::BUSY;
nsIFrame* frame = GetFrame();
if (!frame || !nsCoreUtils::CheckVisibilityInParentChain(frame)) {
state |= states::INVISIBLE | states::OFFSCREEN;
}
nsCOMPtr<nsIEditor> editor;
GetAssociatedEditor(getter_AddRefs(editor));
state |= editor ? states::EDITABLE : states::READONLY;
return state;
}
// nsAccessible public method
void
nsDocAccessible::ApplyARIAState(PRUint64* aState)
{
// Combine with states from outer doc
//
nsAccessible::ApplyARIAState(aState);
// Allow iframe/frame etc. to have final state override via ARIA
if (mParent)
mParent->ApplyARIAState(aState);
}
NS_IMETHODIMP
nsDocAccessible::GetAttributes(nsIPersistentProperties **aAttributes)
{
nsAccessible::GetAttributes(aAttributes);
if (mParent) {
mParent->GetAttributes(aAttributes); // Add parent attributes (override inner)
}
return NS_OK;
}
nsAccessible*
nsDocAccessible::FocusedChild()
{
// XXXndeakin P3 accessibility shouldn't be caching the focus
// Return an accessible for the current global focus, which does not have to
// be contained within the current document.
return gLastFocusedNode ? GetAccService()->GetAccessible(gLastFocusedNode) :
nsnull;
}
NS_IMETHODIMP nsDocAccessible::TakeFocus()
{
if (IsDefunct())
return NS_ERROR_FAILURE;
PRUint64 state = NativeState();
if (0 == (state & states::FOCUSABLE)) {
return NS_ERROR_FAILURE; // Not focusable
}
// Focus the document.
nsCOMPtr<nsIFocusManager> fm = do_GetService(FOCUSMANAGER_CONTRACTID);
NS_ENSURE_STATE(fm);
nsCOMPtr<nsIDOMElement> newFocus;
return fm->MoveFocus(mDocument->GetWindow(), nsnull,
nsIFocusManager::MOVEFOCUS_ROOT, 0,
getter_AddRefs(newFocus));
}
////////////////////////////////////////////////////////////////////////////////
// nsIAccessibleDocument
NS_IMETHODIMP nsDocAccessible::GetURL(nsAString& aURL)
{
if (IsDefunct())
return NS_ERROR_FAILURE;
nsCOMPtr<nsISupports> container = mDocument->GetContainer();
nsCOMPtr<nsIWebNavigation> webNav(do_GetInterface(container));
nsCAutoString theURL;
if (webNav) {
nsCOMPtr<nsIURI> pURI;
webNav->GetCurrentURI(getter_AddRefs(pURI));
if (pURI)
pURI->GetSpec(theURL);
}
CopyUTF8toUTF16(theURL, aURL);
return NS_OK;
}
NS_IMETHODIMP
nsDocAccessible::GetTitle(nsAString& aTitle)
{
nsCOMPtr<nsIDOMDocument> domDocument = do_QueryInterface(mDocument);
if (!domDocument) {
return NS_ERROR_FAILURE;
}
return domDocument->GetTitle(aTitle);
}
NS_IMETHODIMP
nsDocAccessible::GetMimeType(nsAString& aMimeType)
{
nsCOMPtr<nsIDOMDocument> domDocument = do_QueryInterface(mDocument);
if (!domDocument) {
return NS_ERROR_FAILURE;
}
return domDocument->GetContentType(aMimeType);
}
NS_IMETHODIMP nsDocAccessible::GetDocType(nsAString& aDocType)
{
nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(mDocument));
nsCOMPtr<nsIDOMDocumentType> docType;
#ifdef MOZ_XUL
nsCOMPtr<nsIXULDocument> xulDoc(do_QueryInterface(mDocument));
if (xulDoc) {
aDocType.AssignLiteral("window"); // doctype not implemented for XUL at time of writing - causes assertion
return NS_OK;
} else
#endif
if (domDoc && NS_SUCCEEDED(domDoc->GetDoctype(getter_AddRefs(docType))) && docType) {
return docType->GetPublicId(aDocType);
}
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP nsDocAccessible::GetNameSpaceURIForID(PRInt16 aNameSpaceID, nsAString& aNameSpaceURI)
{
if (mDocument) {
nsCOMPtr<nsINameSpaceManager> nameSpaceManager =
do_GetService(NS_NAMESPACEMANAGER_CONTRACTID);
if (nameSpaceManager)
return nameSpaceManager->GetNameSpaceURI(aNameSpaceID, aNameSpaceURI);
}
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP nsDocAccessible::GetWindowHandle(void **aWindow)
{
NS_ENSURE_ARG_POINTER(aWindow);
*aWindow = GetNativeWindow();
return NS_OK;
}
NS_IMETHODIMP nsDocAccessible::GetWindow(nsIDOMWindow **aDOMWin)
{
*aDOMWin = nsnull;
if (!mDocument) {
return NS_ERROR_FAILURE; // Accessible is Shutdown()
}
*aDOMWin = mDocument->GetWindow();
if (!*aDOMWin)
return NS_ERROR_FAILURE; // No DOM Window
NS_ADDREF(*aDOMWin);
return NS_OK;
}
NS_IMETHODIMP
nsDocAccessible::GetDOMDocument(nsIDOMDocument **aDOMDocument)
{
NS_ENSURE_ARG_POINTER(aDOMDocument);
*aDOMDocument = nsnull;
if (mDocument)
CallQueryInterface(mDocument, aDOMDocument);
return NS_OK;
}
NS_IMETHODIMP
nsDocAccessible::GetParentDocument(nsIAccessibleDocument** aDocument)
{
NS_ENSURE_ARG_POINTER(aDocument);
*aDocument = nsnull;
if (!IsDefunct())
NS_IF_ADDREF(*aDocument = ParentDocument());
return NS_OK;
}
NS_IMETHODIMP
nsDocAccessible::GetChildDocumentCount(PRUint32* aCount)
{
NS_ENSURE_ARG_POINTER(aCount);
*aCount = 0;
if (!IsDefunct())
*aCount = ChildDocumentCount();
return NS_OK;
}
NS_IMETHODIMP
nsDocAccessible::GetChildDocumentAt(PRUint32 aIndex,
nsIAccessibleDocument** aDocument)
{
NS_ENSURE_ARG_POINTER(aDocument);
*aDocument = nsnull;
if (IsDefunct())
return NS_OK;
NS_IF_ADDREF(*aDocument = GetChildDocumentAt(aIndex));
return *aDocument ? NS_OK : NS_ERROR_INVALID_ARG;
}
// nsIAccessibleHyperText method
NS_IMETHODIMP nsDocAccessible::GetAssociatedEditor(nsIEditor **aEditor)
{
NS_ENSURE_ARG_POINTER(aEditor);
*aEditor = nsnull;
if (IsDefunct())
return NS_ERROR_FAILURE;
// Check if document is editable (designMode="on" case). Otherwise check if
// the html:body (for HTML document case) or document element is editable.
if (!mDocument->HasFlag(NODE_IS_EDITABLE) &&
!mContent->HasFlag(NODE_IS_EDITABLE))
return NS_OK;
nsCOMPtr<nsISupports> container = mDocument->GetContainer();
nsCOMPtr<nsIEditingSession> editingSession(do_GetInterface(container));
if (!editingSession)
return NS_OK; // No editing session interface
nsCOMPtr<nsIEditor> editor;
editingSession->GetEditorForWindow(mDocument->GetWindow(), getter_AddRefs(editor));
if (!editor) {
return NS_OK;
}
PRBool isEditable;
editor->GetIsDocumentEditable(&isEditable);
if (isEditable) {
NS_ADDREF(*aEditor = editor);
}
return NS_OK;
}
// nsDocAccessible public method
nsAccessible*
nsDocAccessible::GetAccessible(nsINode* aNode) const
{
nsAccessible* accessible = mNodeToAccessibleMap.Get(aNode);
// No accessible in the cache, check if the given ID is unique ID of this
// document accessible.
if (!accessible) {
if (GetNode() != aNode)
return nsnull;
accessible = const_cast<nsDocAccessible*>(this);
}
#ifdef DEBUG
// All cached accessible nodes should be in the parent
// It will assert if not all the children were created
// when they were first cached, and no invalidation
// ever corrected parent accessible's child cache.
nsAccessible* parent = accessible->Parent();
if (parent)
parent->TestChildCache(accessible);
#endif
return accessible;
}
////////////////////////////////////////////////////////////////////////////////
// nsAccessNode
PRBool
nsDocAccessible::Init()
{
NS_LOG_ACCDOCCREATE_FOR("document initialize", mDocument, this)
// Initialize notification controller.
nsCOMPtr<nsIPresShell> shell(GetPresShell());
mNotificationController = new NotificationController(this, shell);
if (!mNotificationController)
return PR_FALSE;
// Mark the document accessible as loaded if its DOM document was loaded at
// this point (this can happen because a11y is started late or DOM document
// having no container was loaded.
if (mDocument->GetReadyStateEnum() == nsIDocument::READYSTATE_COMPLETE)
mLoadState |= eDOMLoaded;
AddEventListeners();
return PR_TRUE;
}
void
nsDocAccessible::Shutdown()
{
if (!mWeakShell) // already shutdown
return;
NS_LOG_ACCDOCDESTROY_FOR("document shutdown", mDocument, this)
if (mNotificationController) {
mNotificationController->Shutdown();
mNotificationController = nsnull;
}
RemoveEventListeners();
// Mark the document as shutdown before AT is notified about the document
// removal from its container (valid for root documents on ATK).
nsCOMPtr<nsIDocument> kungFuDeathGripDoc = mDocument;
mDocument = nsnull;
if (mParent) {
nsDocAccessible* parentDocument = mParent->GetDocAccessible();
if (parentDocument)
parentDocument->RemoveChildDocument(this);
mParent->RemoveChild(this);
}
// Walk the array backwards because child documents remove themselves from the
// array as they are shutdown.
PRInt32 childDocCount = mChildDocuments.Length();
for (PRInt32 idx = childDocCount - 1; idx >= 0; idx--)
mChildDocuments[idx]->Shutdown();
mChildDocuments.Clear();
mWeakShell = nsnull; // Avoid reentrancy
mDependentIDsHash.Clear();
mNodeToAccessibleMap.Clear();
ClearCache(mAccessibleCache);
nsHyperTextAccessibleWrap::Shutdown();
GetAccService()->NotifyOfDocumentShutdown(kungFuDeathGripDoc);
}
nsIFrame*
nsDocAccessible::GetFrame() const
{
nsCOMPtr<nsIPresShell> shell(do_QueryReferent(mWeakShell));
nsIFrame* root = nsnull;
if (shell)
root = shell->GetRootFrame();
return root;
}
bool
nsDocAccessible::IsDefunct() const
{
return nsHyperTextAccessibleWrap::IsDefunct() || !mDocument;
}
// nsDocAccessible protected member
void nsDocAccessible::GetBoundsRect(nsRect& aBounds, nsIFrame** aRelativeFrame)
{
*aRelativeFrame = GetFrame();
nsIDocument *document = mDocument;
nsIDocument *parentDoc = nsnull;
while (document) {
nsIPresShell *presShell = document->GetShell();
if (!presShell) {
return;
}
nsRect scrollPort;
nsIScrollableFrame* sf = presShell->GetRootScrollFrameAsScrollableExternal();
if (sf) {
scrollPort = sf->GetScrollPortRect();
} else {
nsIFrame* rootFrame = presShell->GetRootFrame();
if (!rootFrame) {
return;
}
scrollPort = rootFrame->GetRect();
}
if (parentDoc) { // After first time thru loop
// XXXroc bogus code! scrollPort is relative to the viewport of
// this document, but we're intersecting rectangles derived from
// multiple documents and assuming they're all in the same coordinate
// system. See bug 514117.
aBounds.IntersectRect(scrollPort, aBounds);
}
else { // First time through loop
aBounds = scrollPort;
}
document = parentDoc = document->GetParentDocument();
}
}
// nsDocAccessible protected member
nsresult nsDocAccessible::AddEventListeners()
{
// 1) Set up scroll position listener
// 2) Check for editor and listen for changes to editor
nsCOMPtr<nsIPresShell> presShell(GetPresShell());
NS_ENSURE_TRUE(presShell, NS_ERROR_FAILURE);
nsCOMPtr<nsISupports> container = mDocument->GetContainer();
nsCOMPtr<nsIDocShellTreeItem> docShellTreeItem(do_QueryInterface(container));
NS_ENSURE_TRUE(docShellTreeItem, NS_ERROR_FAILURE);
// Make sure we're a content docshell
// We don't want to listen to chrome progress
PRInt32 itemType;
docShellTreeItem->GetItemType(&itemType);
PRBool isContent = (itemType == nsIDocShellTreeItem::typeContent);
if (isContent) {
// We're not an editor yet, but we might become one
nsCOMPtr<nsICommandManager> commandManager = do_GetInterface(docShellTreeItem);
if (commandManager) {
commandManager->AddCommandObserver(this, "obs_documentCreated");
}
}
nsCOMPtr<nsIDocShellTreeItem> rootTreeItem;
docShellTreeItem->GetRootTreeItem(getter_AddRefs(rootTreeItem));
if (rootTreeItem) {
nsRootAccessible* rootAccessible = RootAccessible();
NS_ENSURE_TRUE(rootAccessible, NS_ERROR_FAILURE);
nsRefPtr<nsCaretAccessible> caretAccessible = rootAccessible->GetCaretAccessible();
if (caretAccessible) {
caretAccessible->AddDocSelectionListener(presShell);
}
}
// add document observer
mDocument->AddObserver(this);
return NS_OK;
}
// nsDocAccessible protected member
nsresult nsDocAccessible::RemoveEventListeners()
{
// Remove listeners associated with content documents
// Remove scroll position listener
RemoveScrollListener();
NS_ASSERTION(mDocument, "No document during removal of listeners.");
if (mDocument) {
mDocument->RemoveObserver(this);
nsCOMPtr<nsISupports> container = mDocument->GetContainer();
nsCOMPtr<nsIDocShellTreeItem> docShellTreeItem(do_QueryInterface(container));
NS_ASSERTION(docShellTreeItem, "doc should support nsIDocShellTreeItem.");
if (docShellTreeItem) {
PRInt32 itemType;
docShellTreeItem->GetItemType(&itemType);
if (itemType == nsIDocShellTreeItem::typeContent) {
nsCOMPtr<nsICommandManager> commandManager = do_GetInterface(docShellTreeItem);
if (commandManager) {
commandManager->RemoveCommandObserver(this, "obs_documentCreated");
}
}
}
}
if (mScrollWatchTimer) {
mScrollWatchTimer->Cancel();
mScrollWatchTimer = nsnull;
NS_RELEASE_THIS(); // Kung fu death grip
}
nsRootAccessible* rootAccessible = RootAccessible();
if (rootAccessible) {
nsRefPtr<nsCaretAccessible> caretAccessible = rootAccessible->GetCaretAccessible();
if (caretAccessible) {
// Don't use GetPresShell() which can call Shutdown() if it sees dead pres shell
nsCOMPtr<nsIPresShell> presShell(do_QueryReferent(mWeakShell));
caretAccessible->RemoveDocSelectionListener(presShell);
}
}
return NS_OK;
}
void nsDocAccessible::ScrollTimerCallback(nsITimer *aTimer, void *aClosure)
{
nsDocAccessible *docAcc = reinterpret_cast<nsDocAccessible*>(aClosure);
if (docAcc && docAcc->mScrollPositionChangedTicks &&
++docAcc->mScrollPositionChangedTicks > 2) {
// Whenever scroll position changes, mScrollPositionChangeTicks gets reset to 1
// We only want to fire accessibilty scroll event when scrolling stops or pauses
// Therefore, we wait for no scroll events to occur between 2 ticks of this timer
// That indicates a pause in scrolling, so we fire the accessibilty scroll event
nsEventShell::FireEvent(nsIAccessibleEvent::EVENT_SCROLLING_END, docAcc);
docAcc->mScrollPositionChangedTicks = 0;
if (docAcc->mScrollWatchTimer) {
docAcc->mScrollWatchTimer->Cancel();
docAcc->mScrollWatchTimer = nsnull;
NS_RELEASE(docAcc); // Release kung fu death grip
}
}
}
// nsDocAccessible protected member
void nsDocAccessible::AddScrollListener()
{
nsCOMPtr<nsIPresShell> presShell(do_QueryReferent(mWeakShell));
if (!presShell)
return;
nsIScrollableFrame* sf = presShell->GetRootScrollFrameAsScrollableExternal();
if (sf) {
sf->AddScrollPositionListener(this);
NS_LOG_ACCDOCCREATE_TEXT("add scroll listener")
}
}
// nsDocAccessible protected member
void nsDocAccessible::RemoveScrollListener()
{
nsCOMPtr<nsIPresShell> presShell(do_QueryReferent(mWeakShell));
if (!presShell)
return;
nsIScrollableFrame* sf = presShell->GetRootScrollFrameAsScrollableExternal();
if (sf) {
sf->RemoveScrollPositionListener(this);
}
}
////////////////////////////////////////////////////////////////////////////////
// nsIScrollPositionListener
void nsDocAccessible::ScrollPositionDidChange(nscoord aX, nscoord aY)
{
// Start new timer, if the timer cycles at least 1 full cycle without more scroll position changes,
// then the ::Notify() method will fire the accessibility event for scroll position changes
const PRUint32 kScrollPosCheckWait = 50;
if (mScrollWatchTimer) {
mScrollWatchTimer->SetDelay(kScrollPosCheckWait); // Create new timer, to avoid leaks
}
else {
mScrollWatchTimer = do_CreateInstance("@mozilla.org/timer;1");
if (mScrollWatchTimer) {
NS_ADDREF_THIS(); // Kung fu death grip
mScrollWatchTimer->InitWithFuncCallback(ScrollTimerCallback, this,
kScrollPosCheckWait,
nsITimer::TYPE_REPEATING_SLACK);
}
}
mScrollPositionChangedTicks = 1;
}
////////////////////////////////////////////////////////////////////////////////
// nsIObserver
NS_IMETHODIMP nsDocAccessible::Observe(nsISupports *aSubject, const char *aTopic,
const PRUnichar *aData)
{
if (!nsCRT::strcmp(aTopic,"obs_documentCreated")) {
// State editable will now be set, readonly is now clear
// Normally we only fire delayed events created from the node, not an
// accessible object. See the AccStateChangeEvent constructor for details
// about this exceptional case.
nsRefPtr<AccEvent> event =
new AccStateChangeEvent(this, states::EDITABLE, PR_TRUE);
FireDelayedAccessibleEvent(event);
}
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
// nsIDocumentObserver
NS_IMPL_NSIDOCUMENTOBSERVER_CORE_STUB(nsDocAccessible)
NS_IMPL_NSIDOCUMENTOBSERVER_LOAD_STUB(nsDocAccessible)
NS_IMPL_NSIDOCUMENTOBSERVER_STYLE_STUB(nsDocAccessible)
void
nsDocAccessible::AttributeWillChange(nsIDocument *aDocument,
dom::Element* aElement,
PRInt32 aNameSpaceID,
nsIAtom* aAttribute, PRInt32 aModType)
{
nsAccessible* accessible = GetAccessible(aElement);
if (!accessible) {
if (aElement != mContent)
return;
accessible = this;
}
// Update dependent IDs cache. Take care of elements that are accessible
// because dependent IDs cache doesn't contain IDs from non accessible
// elements.
if (aModType != nsIDOMMutationEvent::ADDITION)
RemoveDependentIDsFor(accessible, aAttribute);
// Store the ARIA attribute old value so that it can be used after
// attribute change. Note, we assume there's no nested ARIA attribute
// changes. If this happens then we should end up with keeping a stack of
// old values.
// XXX TODO: bugs 472142, 472143.
// Here we will want to cache whatever attribute values we are interested
// in, such as the existence of aria-pressed for button (so we know if we
// need to newly expose it as a toggle button) etc.
if (aAttribute == nsAccessibilityAtoms::aria_checked ||
aAttribute == nsAccessibilityAtoms::aria_pressed) {
mARIAAttrOldValue = (aModType != nsIDOMMutationEvent::ADDITION) ?
nsAccUtils::GetARIAToken(aElement, aAttribute) : nsnull;
}
}
void
nsDocAccessible::AttributeChanged(nsIDocument *aDocument,
dom::Element* aElement,
PRInt32 aNameSpaceID, nsIAtom* aAttribute,
PRInt32 aModType)
{
NS_ASSERTION(!IsDefunct(),
"Attribute changed called on defunct document accessible!");
// Proceed even if the element is not accessible because element may become
// accessible if it gets certain attribute.
if (UpdateAccessibleOnAttrChange(aElement, aAttribute))
return;
// Ignore attribute change if the element doesn't have an accessible (at all
// or still) iff the element is not a root content of this document accessible
// (which is treated as attribute change on this document accessible).
// Note: we don't bail if all the content hasn't finished loading because
// these attributes are changing for a loaded part of the content.
nsAccessible* accessible = GetAccessible(aElement);
if (!accessible) {
if (mContent != aElement)
return;
accessible = this;
}
// Fire accessible events iff there's an accessible, otherwise we consider
// the accessible state wasn't changed, i.e. its state is initial state.
AttributeChangedImpl(aElement, aNameSpaceID, aAttribute);
// Update dependent IDs cache. Take care of accessible elements because no
// accessible element means either the element is not accessible at all or
// its accessible will be created later. It doesn't make sense to keep
// dependent IDs for non accessible elements. For the second case we'll update
// dependent IDs cache when its accessible is created.
if (aModType == nsIDOMMutationEvent::MODIFICATION ||
aModType == nsIDOMMutationEvent::ADDITION) {
AddDependentIDsFor(accessible, aAttribute);
}
}
// nsDocAccessible protected member
void
nsDocAccessible::AttributeChangedImpl(nsIContent* aContent, PRInt32 aNameSpaceID, nsIAtom* aAttribute)
{
// Fire accessible event after short timer, because we need to wait for
// DOM attribute & resulting layout to actually change. Otherwise,
// assistive technology will retrieve the wrong state/value/selection info.
// XXX todo
// We still need to handle special HTML cases here