summaryrefslogtreecommitdiffstats
path: root/accessible/ipc/ProxyAccessibleBase.h
blob: 6c51b9769942caa51eee6399f76ff61b18652a46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_a11y_ProxyAccessibleBase_h
#define mozilla_a11y_ProxyAccessibleBase_h

#include "mozilla/a11y/Role.h"
#include "nsIAccessibleText.h"
#include "nsIAccessibleTypes.h"
#include "Accessible.h"
#include "nsString.h"
#include "nsTArray.h"
#include "nsRect.h"
#include "Accessible.h"

namespace mozilla {
namespace a11y {

class Accessible;
class Attribute;
class DocAccessibleParent;
class ProxyAccessible;
enum class RelationType;

enum Interfaces
{
  HYPERTEXT = 1,
  HYPERLINK = 1 << 1,
  IMAGE = 1 << 2,
  VALUE = 1 << 3,
  TABLE = 1 << 4,
  TABLECELL = 1 << 5,
  DOCUMENT = 1 << 6,
  SELECTION = 1 << 7,
  ACTION = 1 << 8,
};

template <class Derived>
class ProxyAccessibleBase
{
public:
  ~ProxyAccessibleBase()
  {
    MOZ_ASSERT(!mWrapper);
  }

  void AddChildAt(uint32_t aIdx, Derived* aChild)
  { mChildren.InsertElementAt(aIdx, aChild); }

  uint32_t ChildrenCount() const { return mChildren.Length(); }
  Derived* ChildAt(uint32_t aIdx) const { return mChildren[aIdx]; }
  Derived* FirstChild() const
    { return mChildren.Length() ? mChildren[0] : nullptr; }
  Derived* LastChild() const
    { return mChildren.Length() ? mChildren[mChildren.Length() - 1] : nullptr; }
  Derived* PrevSibling() const
  {
    size_t idx = IndexInParent();
    return idx > 0 ? Parent()->mChildren[idx - 1] : nullptr;
  }
  Derived* NextSibling() const
  {
    size_t idx = IndexInParent();
    return idx + 1 < Parent()->mChildren.Length() ? Parent()->mChildren[idx + 1]
    : nullptr;
  }

  // XXX evaluate if this is fast enough.
  size_t IndexInParent() const { return
    Parent()->mChildren.IndexOf(static_cast<const Derived*>(this)); }
  uint32_t EmbeddedChildCount() const;
  int32_t IndexOfEmbeddedChild(const Derived* aChild);
  Derived* EmbeddedChildAt(size_t aChildIdx);
  bool MustPruneChildren() const;

  void Shutdown();

  void SetChildDoc(DocAccessibleParent* aChildDoc);
  void ClearChildDoc(DocAccessibleParent* aChildDoc);

  /**
   * Remove The given child.
   */
  void RemoveChild(Derived* aChild)
    { mChildren.RemoveElement(aChild); }

  /**
   * Return the proxy for the parent of the wrapped accessible.
   */
  Derived* Parent() const { return mParent; }

  Accessible* OuterDocOfRemoteBrowser() const;

  /**
   * Get the role of the accessible we're proxying.
   */
  role Role() const { return mRole; }

  /**
   * Return true if this is an embedded object.
   */
  bool IsEmbeddedObject() const
  {
    role role = Role();
    return role != roles::TEXT_LEAF &&
           role != roles::WHITESPACE &&
           role != roles::STATICTEXT;
  }

  /**
   * Allow the platform to store a pointers worth of data on us.
   */
  uintptr_t GetWrapper() const { return mWrapper; }
  void SetWrapper(uintptr_t aWrapper) { mWrapper = aWrapper; }

  /*
   * Return the ID of the accessible being proxied.
   */
  uint64_t ID() const { return mID; }

  /**
   * Return the document containing this proxy, or the proxy itself if it is a
   * document.
   */
  DocAccessibleParent* Document() const { return mDoc; }

  /**
   * Return true if this proxy is a DocAccessibleParent.
   */
  bool IsDoc() const { return mIsDoc; }
  DocAccessibleParent* AsDoc() const { return IsDoc() ? mDoc : nullptr; }

  // XXX checking mRole alone may not result in same behavior as Accessibles
  // due to ARIA roles. See bug 1210477.
  inline bool IsTable() const
  {
    return mRole == roles::TABLE || mRole == roles::MATHML_TABLE;
  }
  inline bool IsTableRow() const
  {
    return (mRole == roles::ROW ||
        mRole == roles::MATHML_TABLE_ROW ||
        mRole == roles::MATHML_LABELED_ROW);
  }
  inline bool IsTableCell() const
  {
    return (mRole == roles::CELL ||
        mRole == roles::COLUMNHEADER ||
        mRole == roles::ROWHEADER ||
        mRole == roles::GRID_CELL ||
        mRole == roles::MATHML_CELL);
  }

protected:
  ProxyAccessibleBase(uint64_t aID, Derived* aParent,
                      DocAccessibleParent* aDoc, role aRole,
                      uint32_t aInterfaces)
    : mParent(aParent)
    , mDoc(aDoc)
    , mWrapper(0)
    , mID(aID)
    , mRole(aRole)
    , mOuterDoc(false)
    , mIsDoc(false)
    , mHasValue(aInterfaces & Interfaces::VALUE)
    , mIsHyperLink(aInterfaces & Interfaces::HYPERLINK)
    , mIsHyperText(aInterfaces & Interfaces::HYPERTEXT)
  {
  }

  explicit ProxyAccessibleBase(DocAccessibleParent* aThisAsDoc) :
    mParent(nullptr), mDoc(aThisAsDoc), mWrapper(0), mID(0),
    mRole(roles::DOCUMENT), mOuterDoc(false), mIsDoc(true), mHasValue(false),
    mIsHyperLink(false), mIsHyperText(false)
  {}

protected:
  Derived* mParent;

private:
  friend Derived;

  nsTArray<Derived*> mChildren;
  DocAccessibleParent* mDoc;
  uintptr_t mWrapper;
  uint64_t mID;

protected:
  // XXX DocAccessibleParent gets to change this to change the role of
  // documents.
  role mRole : 27;

private:
  bool mOuterDoc : 1;

public:
  const bool mIsDoc: 1;
  const bool mHasValue: 1;
  const bool mIsHyperLink: 1;
  const bool mIsHyperText: 1;
};

extern template class ProxyAccessibleBase<ProxyAccessible>;

}
}

#endif