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 #if defined(MOZILLA_INTERNAL_API)
8 #error This code is NOT for internal Gecko use!
9 #endif  // defined(MOZILLA_INTERNAL_API)
10 
11 #include "AccessibleHandlerControl.h"
12 
13 #include "AccessibleHandler.h"
14 
15 #include "AccessibleEventId.h"
16 
17 #include "mozilla/Move.h"
18 #include "mozilla/RefPtr.h"
19 
20 namespace mozilla {
21 namespace a11y {
22 
23 mscom::SingletonFactory<AccessibleHandlerControl> gControlFactory;
24 
25 namespace detail {
26 
TextChange()27 TextChange::TextChange() : mIA2UniqueId(0), mIsInsert(false), mText() {}
28 
TextChange(long aIA2UniqueId,bool aIsInsert,NotNull<IA2TextSegment * > aText)29 TextChange::TextChange(long aIA2UniqueId, bool aIsInsert,
30                        NotNull<IA2TextSegment*> aText)
31     : mIA2UniqueId(aIA2UniqueId),
32       mIsInsert(aIsInsert),
33       mText{BSTRCopy(aText->text), aText->start, aText->end} {}
34 
TextChange(TextChange && aOther)35 TextChange::TextChange(TextChange&& aOther) : mText() { *this = Move(aOther); }
36 
TextChange(const TextChange & aOther)37 TextChange::TextChange(const TextChange& aOther) : mText() { *this = aOther; }
38 
operator =(TextChange && aOther)39 TextChange& TextChange::operator=(TextChange&& aOther) {
40   mIA2UniqueId = aOther.mIA2UniqueId;
41   mIsInsert = aOther.mIsInsert;
42   aOther.mIA2UniqueId = 0;
43   ::SysFreeString(mText.text);
44   mText = aOther.mText;
45   aOther.mText.text = nullptr;
46   return *this;
47 }
48 
operator =(const TextChange & aOther)49 TextChange& TextChange::operator=(const TextChange& aOther) {
50   mIA2UniqueId = aOther.mIA2UniqueId;
51   mIsInsert = aOther.mIsInsert;
52   ::SysFreeString(mText.text);
53   mText = {BSTRCopy(aOther.mText.text), aOther.mText.start, aOther.mText.end};
54   return *this;
55 }
56 
~TextChange()57 TextChange::~TextChange() { ::SysFreeString(mText.text); }
58 
59 HRESULT
GetOld(long aIA2UniqueId,NotNull<IA2TextSegment * > aOutOldSegment)60 TextChange::GetOld(long aIA2UniqueId, NotNull<IA2TextSegment*> aOutOldSegment) {
61   if (mIsInsert || aIA2UniqueId != mIA2UniqueId) {
62     return S_OK;
63   }
64 
65   return SegCopy(*aOutOldSegment, mText);
66 }
67 
68 HRESULT
GetNew(long aIA2UniqueId,NotNull<IA2TextSegment * > aOutNewSegment)69 TextChange::GetNew(long aIA2UniqueId, NotNull<IA2TextSegment*> aOutNewSegment) {
70   if (!mIsInsert || aIA2UniqueId != mIA2UniqueId) {
71     return S_OK;
72   }
73 
74   return SegCopy(*aOutNewSegment, mText);
75 }
76 
BSTRCopy(const BSTR & aIn)77 /* static */ BSTR TextChange::BSTRCopy(const BSTR& aIn) {
78   return ::SysAllocStringLen(aIn, ::SysStringLen(aIn));
79 }
80 
SegCopy(IA2TextSegment & aDest,const IA2TextSegment & aSrc)81 /* static */ HRESULT TextChange::SegCopy(IA2TextSegment& aDest,
82                                          const IA2TextSegment& aSrc) {
83   aDest = {BSTRCopy(aSrc.text), aSrc.start, aSrc.end};
84   if (aSrc.text && !aDest.text) {
85     return E_OUTOFMEMORY;
86   }
87   if (!::SysStringLen(aDest.text)) {
88     return S_FALSE;
89   }
90   return S_OK;
91 }
92 
93 }  // namespace detail
94 
95 HRESULT
Create(AccessibleHandlerControl ** aOutObject)96 AccessibleHandlerControl::Create(AccessibleHandlerControl** aOutObject) {
97   if (!aOutObject) {
98     return E_INVALIDARG;
99   }
100 
101   RefPtr<AccessibleHandlerControl> ctl(new AccessibleHandlerControl());
102   ctl.forget(aOutObject);
103   return S_OK;
104 }
105 
AccessibleHandlerControl()106 AccessibleHandlerControl::AccessibleHandlerControl()
107     : mIsRegistered(false),
108       mCacheGen(0),
109       mIA2Proxy(mscom::RegisterProxy(L"ia2marshal.dll")),
110       mHandlerProxy(mscom::RegisterProxy()) {
111   MOZ_ASSERT(mIA2Proxy);
112 }
113 
IMPL_IUNKNOWN1(AccessibleHandlerControl,IHandlerControl)114 IMPL_IUNKNOWN1(AccessibleHandlerControl, IHandlerControl)
115 
116 HRESULT
117 AccessibleHandlerControl::Invalidate() {
118   ++mCacheGen;
119   return S_OK;
120 }
121 
122 HRESULT
OnTextChange(long aHwnd,long aIA2UniqueId,VARIANT_BOOL aIsInsert,IA2TextSegment * aText)123 AccessibleHandlerControl::OnTextChange(long aHwnd, long aIA2UniqueId,
124                                        VARIANT_BOOL aIsInsert,
125                                        IA2TextSegment* aText) {
126   if (!aText) {
127     return E_INVALIDARG;
128   }
129 
130   mTextChange = detail::TextChange(aIA2UniqueId, aIsInsert, WrapNotNull(aText));
131   NotifyWinEvent(aIsInsert ? IA2_EVENT_TEXT_INSERTED : IA2_EVENT_TEXT_REMOVED,
132                  reinterpret_cast<HWND>(static_cast<uintptr_t>(aHwnd)),
133                  OBJID_CLIENT, aIA2UniqueId);
134   return S_OK;
135 }
136 
137 HRESULT
GetNewText(long aIA2UniqueId,NotNull<IA2TextSegment * > aOutNewText)138 AccessibleHandlerControl::GetNewText(long aIA2UniqueId,
139                                      NotNull<IA2TextSegment*> aOutNewText) {
140   return mTextChange.GetNew(aIA2UniqueId, aOutNewText);
141 }
142 
143 HRESULT
GetOldText(long aIA2UniqueId,NotNull<IA2TextSegment * > aOutOldText)144 AccessibleHandlerControl::GetOldText(long aIA2UniqueId,
145                                      NotNull<IA2TextSegment*> aOutOldText) {
146   return mTextChange.GetOld(aIA2UniqueId, aOutOldText);
147 }
148 
149 HRESULT
GetHandlerTypeInfo(ITypeInfo ** aOutTypeInfo)150 AccessibleHandlerControl::GetHandlerTypeInfo(ITypeInfo** aOutTypeInfo) {
151   if (!mHandlerProxy) {
152     return E_UNEXPECTED;
153   }
154 
155   return mHandlerProxy->GetTypeInfoForGuid(CLSID_AccessibleHandler,
156                                            aOutTypeInfo);
157 }
158 
159 HRESULT
Register(NotNull<IGeckoBackChannel * > aGecko)160 AccessibleHandlerControl::Register(NotNull<IGeckoBackChannel*> aGecko) {
161   if (mIsRegistered) {
162     return S_OK;
163   }
164 
165   long pid = static_cast<long>(::GetCurrentProcessId());
166   HRESULT hr = aGecko->put_HandlerControl(pid, this);
167   mIsRegistered = SUCCEEDED(hr);
168   MOZ_ASSERT(mIsRegistered);
169   return hr;
170 }
171 
172 }  // namespace a11y
173 }  // namespace mozilla
174