summaryrefslogtreecommitdiffstats
path: root/accessible/ipc/win/handler/HandlerChildEnumerator.cpp
blob: 2062387bf67f8d31b523e5f4a5a31d8a73c062cd (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

#if defined(MOZILLA_INTERNAL_API)
#  error This code is NOT for internal Gecko use!
#endif  // defined(MOZILLA_INTERNAL_API)

#include "HandlerChildEnumerator.h"
#include "HandlerTextLeaf.h"
#include "mozilla/Assertions.h"

namespace mozilla {
namespace a11y {

HandlerChildEnumerator::HandlerChildEnumerator(
    AccessibleHandler* aHandler, IGeckoBackChannel* aGeckoBackChannel)
    : mHandler(aHandler),
      mGeckoBackChannel(aGeckoBackChannel),
      mChildCount(0),
      mNextChild(0) {
  MOZ_ASSERT(aHandler);
  MOZ_ASSERT(aGeckoBackChannel);
}

HandlerChildEnumerator::HandlerChildEnumerator(
    const HandlerChildEnumerator& aEnumerator)
    : mHandler(aEnumerator.mHandler),
      mGeckoBackChannel(aEnumerator.mGeckoBackChannel),
      mChildCount(aEnumerator.mChildCount),
      mNextChild(aEnumerator.mNextChild) {
  if (mChildCount == 0) {
    return;
  }
  mChildren = MakeUnique<VARIANT[]>(mChildCount);
  CopyMemory(mChildren.get(), aEnumerator.mChildren.get(),
             sizeof(VARIANT) * mChildCount);
  for (ULONG index = 0; index < mChildCount; ++index) {
    mChildren[index].pdispVal->AddRef();
  }
}

HandlerChildEnumerator::~HandlerChildEnumerator() { ClearCache(); }

void HandlerChildEnumerator::ClearCache() {
  if (!mChildren) {
    return;
  }

  for (ULONG index = 0; index < mChildCount; ++index) {
    mChildren[index].pdispVal->Release();
  }

  mChildren = nullptr;
  mChildCount = 0;
}

HRESULT
HandlerChildEnumerator::MaybeCacheChildren() {
  if (mChildren) {
    // Already cached.
    return S_OK;
  }

  AccChildData* children;
  HRESULT hr = mGeckoBackChannel->get_AllChildren(&children, &mChildCount);
  if (FAILED(hr)) {
    mChildCount = 0;
    ClearCache();
    return hr;
  }

  HWND hwnd = nullptr;
  hr = mHandler->get_windowHandle(&hwnd);
  MOZ_ASSERT(SUCCEEDED(hr));

  RefPtr<IDispatch> parent;
  hr = mHandler->QueryInterface(IID_IDispatch, getter_AddRefs(parent));
  MOZ_ASSERT(SUCCEEDED(hr));

  mChildren = MakeUnique<VARIANT[]>(mChildCount);
  for (ULONG index = 0; index < mChildCount; ++index) {
    AccChildData& data = children[index];
    VARIANT& child = mChildren[index];
    if (data.mAccessible) {
      RefPtr<IDispatch> disp;
      hr =
          data.mAccessible->QueryInterface(IID_IDispatch, getter_AddRefs(disp));
      data.mAccessible->Release();
      MOZ_ASSERT(SUCCEEDED(hr));
      if (FAILED(hr)) {
        child.vt = VT_EMPTY;
        continue;
      }
      child.vt = VT_DISPATCH;
      disp.forget(&child.pdispVal);
    } else {
      // Text leaf.
      RefPtr<IDispatch> leaf(new HandlerTextLeaf(parent, index, hwnd, data));
      child.vt = VT_DISPATCH;
      leaf.forget(&child.pdispVal);
    }
  }

  ::CoTaskMemFree(children);
  return S_OK;
}

IMPL_IUNKNOWN_QUERY_HEAD(HandlerChildEnumerator)
IMPL_IUNKNOWN_QUERY_IFACE(IEnumVARIANT)
IMPL_IUNKNOWN_QUERY_TAIL_AGGREGATED(mHandler)

/*** IEnumVARIANT ***/

HRESULT
HandlerChildEnumerator::Clone(IEnumVARIANT** aPpEnum) {
  RefPtr<HandlerChildEnumerator> newEnum(new HandlerChildEnumerator(*this));
  newEnum.forget(aPpEnum);
  return S_OK;
}

HRESULT
HandlerChildEnumerator::Next(ULONG aCelt, VARIANT* aRgVar,
                             ULONG* aPCeltFetched) {
  if (!aRgVar || aCelt == 0) {
    return E_INVALIDARG;
  }

  HRESULT hr = MaybeCacheChildren();
  if (FAILED(hr)) {
    return hr;
  }

  for (ULONG index = 0; index < aCelt; ++index) {
    if (mNextChild >= mChildCount) {
      // Less elements remaining than were requested.
      if (aPCeltFetched) {
        *aPCeltFetched = index;
      }
      return S_FALSE;
    }
    aRgVar[index] = mChildren[mNextChild];
    aRgVar[index].pdispVal->AddRef();
    ++mNextChild;
  }
  *aPCeltFetched = aCelt;
  return S_OK;
}

HRESULT
HandlerChildEnumerator::Reset() {
  mNextChild = 0;
  ClearCache();
  return S_OK;
}

HRESULT
HandlerChildEnumerator::Skip(ULONG aCelt) {
  mNextChild += aCelt;
  if (mNextChild > mChildCount) {
    // Less elements remaining than the client requested to skip.
    return S_FALSE;
  }
  return S_OK;
}

}  // namespace a11y
}  // namespace mozilla