summaryrefslogtreecommitdiffstats
path: root/accessible/ipc/win/handler/AccessibleHandlerControl.cpp
blob: 0d0bd4d71d94e5b31b097dc323101e59064edd25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* -*- 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 "AccessibleHandlerControl.h"

#include <utility>

#include "AccessibleEventId.h"
#include "AccessibleHandler.h"
#include "mozilla/RefPtr.h"

namespace mozilla {
namespace a11y {

mscom::SingletonFactory<AccessibleHandlerControl> gControlFactory;

namespace detail {

TextChange::TextChange() : mIA2UniqueId(0), mIsInsert(false), mText() {}

TextChange::TextChange(long aIA2UniqueId, bool aIsInsert,
                       NotNull<IA2TextSegment*> aText)
    : mIA2UniqueId(aIA2UniqueId),
      mIsInsert(aIsInsert),
      mText{BSTRCopy(aText->text), aText->start, aText->end} {}

TextChange::TextChange(TextChange&& aOther) : mText() {
  *this = std::move(aOther);
}

TextChange::TextChange(const TextChange& aOther) : mText() { *this = aOther; }

TextChange& TextChange::operator=(TextChange&& aOther) {
  mIA2UniqueId = aOther.mIA2UniqueId;
  mIsInsert = aOther.mIsInsert;
  aOther.mIA2UniqueId = 0;
  ::SysFreeString(mText.text);
  mText = aOther.mText;
  aOther.mText.text = nullptr;
  return *this;
}

TextChange& TextChange::operator=(const TextChange& aOther) {
  mIA2UniqueId = aOther.mIA2UniqueId;
  mIsInsert = aOther.mIsInsert;
  ::SysFreeString(mText.text);
  mText = {BSTRCopy(aOther.mText.text), aOther.mText.start, aOther.mText.end};
  return *this;
}

TextChange::~TextChange() { ::SysFreeString(mText.text); }

HRESULT
TextChange::GetOld(long aIA2UniqueId, NotNull<IA2TextSegment*> aOutOldSegment) {
  if (mIsInsert || aIA2UniqueId != mIA2UniqueId) {
    return S_OK;
  }

  return SegCopy(*aOutOldSegment, mText);
}

HRESULT
TextChange::GetNew(long aIA2UniqueId, NotNull<IA2TextSegment*> aOutNewSegment) {
  if (!mIsInsert || aIA2UniqueId != mIA2UniqueId) {
    return S_OK;
  }

  return SegCopy(*aOutNewSegment, mText);
}

/* static */
BSTR TextChange::BSTRCopy(const BSTR& aIn) {
  return ::SysAllocStringLen(aIn, ::SysStringLen(aIn));
}

/* static */
HRESULT TextChange::SegCopy(IA2TextSegment& aDest, const IA2TextSegment& aSrc) {
  aDest = {BSTRCopy(aSrc.text), aSrc.start, aSrc.end};
  if (aSrc.text && !aDest.text) {
    return E_OUTOFMEMORY;
  }
  if (!::SysStringLen(aDest.text)) {
    return S_FALSE;
  }
  return S_OK;
}

}  // namespace detail

HRESULT
AccessibleHandlerControl::Create(AccessibleHandlerControl** aOutObject) {
  if (!aOutObject) {
    return E_INVALIDARG;
  }

  RefPtr<AccessibleHandlerControl> ctl(new AccessibleHandlerControl());
  ctl.forget(aOutObject);
  return S_OK;
}

AccessibleHandlerControl::AccessibleHandlerControl()
    : mIsRegistered(false),
      mCacheGen(0),
      mIA2Proxy(mscom::RegisterProxy(L"ia2marshal.dll")),
      mHandlerProxy(mscom::RegisterProxy()) {
  MOZ_ASSERT(mIA2Proxy);
}

IMPL_IUNKNOWN1(AccessibleHandlerControl, IHandlerControl)

HRESULT
AccessibleHandlerControl::Invalidate() {
  ++mCacheGen;
  // We can't just call mAccessibleCache.clear() because doing so would release
  // remote objects, making remote COM calls. Since this is an STA, an incoming
  // COM call might be handled which might marshal an AccessibleHandler,
  // which in turn might add itself to mAccessibleCache. Since we'd be in the
  // middle of mutating mAccessibleCache, that might cause a crash. Instead,
  // swap mAccessibleCache into a temporary map first, which will empty
  // mAccessibleCache without releasing remote objects. Once mAccessibleCache
  // is empty, it's safe to let the temporary map be destroyed when it goes
  // out of scope. Remote calls will be made, but nothing will re-enter
  // the temporary map while it's being destroyed.
  AccessibleCache oldCache;
  mAccessibleCache.swap(oldCache);
  return S_OK;
}

HRESULT
AccessibleHandlerControl::OnTextChange(long aHwnd, long aIA2UniqueId,
                                       VARIANT_BOOL aIsInsert,
                                       IA2TextSegment* aText) {
  if (!aText) {
    return E_INVALIDARG;
  }

  mTextChange = detail::TextChange(aIA2UniqueId, aIsInsert, WrapNotNull(aText));
  NotifyWinEvent(aIsInsert ? IA2_EVENT_TEXT_INSERTED : IA2_EVENT_TEXT_REMOVED,
                 reinterpret_cast<HWND>(static_cast<uintptr_t>(aHwnd)),
                 OBJID_CLIENT, aIA2UniqueId);
  return S_OK;
}

HRESULT
AccessibleHandlerControl::GetNewText(long aIA2UniqueId,
                                     NotNull<IA2TextSegment*> aOutNewText) {
  return mTextChange.GetNew(aIA2UniqueId, aOutNewText);
}

HRESULT
AccessibleHandlerControl::GetOldText(long aIA2UniqueId,
                                     NotNull<IA2TextSegment*> aOutOldText) {
  return mTextChange.GetOld(aIA2UniqueId, aOutOldText);
}

HRESULT
AccessibleHandlerControl::GetHandlerTypeInfo(ITypeInfo** aOutTypeInfo) {
  if (!mHandlerProxy) {
    return E_UNEXPECTED;
  }

  return mHandlerProxy->GetTypeInfoForGuid(CLSID_AccessibleHandler,
                                           aOutTypeInfo);
}

HRESULT
AccessibleHandlerControl::Register(NotNull<IGeckoBackChannel*> aGecko) {
  if (mIsRegistered) {
    return S_OK;
  }

  long pid = static_cast<long>(::GetCurrentProcessId());
  HRESULT hr = aGecko->put_HandlerControl(pid, this);
  mIsRegistered = SUCCEEDED(hr);
  MOZ_ASSERT(mIsRegistered);
  return hr;
}

void AccessibleHandlerControl::CacheAccessible(long aUniqueId,
                                               AccessibleHandler* aAccessible) {
  MOZ_ASSERT(aUniqueId && aAccessible);
  mAccessibleCache[aUniqueId] = aAccessible;
}

HRESULT AccessibleHandlerControl::GetCachedAccessible(
    long aUniqueId, AccessibleHandler** aAccessible) {
  MOZ_ASSERT(aUniqueId && aAccessible);
  auto it = mAccessibleCache.find(aUniqueId);
  if (it == mAccessibleCache.end()) {
    return E_INVALIDARG;
  }
  RefPtr<AccessibleHandler> ref = it->second;
  ref.forget(aAccessible);
  return S_OK;
}

HRESULT AccessibleHandlerControl::SuppressA11yForClipboardCopy() {
  mA11yClipboardCopySuppressionStartTime = ::GetTickCount();
  return S_OK;
}

bool AccessibleHandlerControl::IsA11ySuppressedForClipboardCopy() {
  // Must be kept in sync with kSuppressTimeout in
  // accessible/windows/msaa/Compatibility.cpp.
  constexpr DWORD kSuppressTimeout = 1500;  // ms
  if (!mA11yClipboardCopySuppressionStartTime) {
    return false;
  }
  return ::GetTickCount() - mA11yClipboardCopySuppressionStartTime <
         kSuppressTimeout;
}

}  // namespace a11y
}  // namespace mozilla