forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nsWindowRoot.cpp
279 lines (237 loc) · 8.17 KB
/
nsWindowRoot.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
/* -*- 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/. */
#include "nsCOMPtr.h"
#include "nsWindowRoot.h"
#include "nsPIDOMWindow.h"
#include "nsEventListenerManager.h"
#include "nsPresContext.h"
#include "nsLayoutCID.h"
#include "nsContentCID.h"
#include "nsString.h"
#include "nsEventDispatcher.h"
#include "nsGUIEvent.h"
#include "nsGlobalWindow.h"
#include "nsFocusManager.h"
#include "nsIDOMHTMLInputElement.h"
#include "nsIDOMHTMLTextAreaElement.h"
#include "nsIControllers.h"
#include "nsCycleCollectionParticipant.h"
#ifdef MOZ_XUL
#include "nsIDOMXULElement.h"
#endif
static NS_DEFINE_CID(kEventListenerManagerCID, NS_EVENTLISTENERMANAGER_CID);
nsWindowRoot::nsWindowRoot(nsPIDOMWindow* aWindow)
{
mWindow = aWindow;
}
nsWindowRoot::~nsWindowRoot()
{
if (mListenerManager) {
mListenerManager->Disconnect();
}
}
NS_IMPL_CYCLE_COLLECTION_CLASS(nsWindowRoot)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsWindowRoot)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NATIVE_MEMBER(mListenerManager,
nsEventListenerManager)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mPopupNode)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mParent)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsWindowRoot)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mListenerManager)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mPopupNode)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mParent)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsWindowRoot)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMEventTarget)
NS_INTERFACE_MAP_ENTRY(nsPIWindowRoot)
NS_INTERFACE_MAP_ENTRY(nsIDOMEventTarget)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsWindowRoot)
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsWindowRoot)
NS_IMPL_DOMTARGET_DEFAULTS(nsWindowRoot)
NS_IMETHODIMP
nsWindowRoot::RemoveEventListener(const nsAString& aType, nsIDOMEventListener* aListener, bool aUseCapture)
{
nsRefPtr<nsEventListenerManager> elm = GetListenerManager(false);
if (elm) {
elm->RemoveEventListener(aType, aListener, aUseCapture);
}
return NS_OK;
}
NS_IMPL_REMOVE_SYSTEM_EVENT_LISTENER(nsWindowRoot)
NS_IMETHODIMP
nsWindowRoot::DispatchEvent(nsIDOMEvent* aEvt, bool *aRetVal)
{
nsEventStatus status = nsEventStatus_eIgnore;
nsresult rv = nsEventDispatcher::DispatchDOMEvent(
static_cast<nsIDOMEventTarget*>(this), nullptr, aEvt, nullptr, &status);
*aRetVal = (status != nsEventStatus_eConsumeNoDefault);
return rv;
}
nsresult
nsWindowRoot::DispatchDOMEvent(nsEvent* aEvent,
nsIDOMEvent* aDOMEvent,
nsPresContext* aPresContext,
nsEventStatus* aEventStatus)
{
return nsEventDispatcher::DispatchDOMEvent(static_cast<nsIDOMEventTarget*>(this),
aEvent, aDOMEvent,
aPresContext, aEventStatus);
}
NS_IMETHODIMP
nsWindowRoot::AddEventListener(const nsAString& aType,
nsIDOMEventListener *aListener,
bool aUseCapture, bool aWantsUntrusted,
uint8_t aOptionalArgc)
{
NS_ASSERTION(!aWantsUntrusted || aOptionalArgc > 1,
"Won't check if this is chrome, you want to set "
"aWantsUntrusted to false or make the aWantsUntrusted "
"explicit by making optional_argc non-zero.");
nsEventListenerManager* elm = GetListenerManager(true);
NS_ENSURE_STATE(elm);
elm->AddEventListener(aType, aListener, aUseCapture, aWantsUntrusted);
return NS_OK;
}
NS_IMETHODIMP
nsWindowRoot::AddSystemEventListener(const nsAString& aType,
nsIDOMEventListener *aListener,
bool aUseCapture,
bool aWantsUntrusted,
uint8_t aOptionalArgc)
{
NS_ASSERTION(!aWantsUntrusted || aOptionalArgc > 1,
"Won't check if this is chrome, you want to set "
"aWantsUntrusted to false or make the aWantsUntrusted "
"explicit by making optional_argc non-zero.");
return NS_AddSystemEventListener(this, aType, aListener, aUseCapture,
aWantsUntrusted);
}
nsEventListenerManager*
nsWindowRoot::GetListenerManager(bool aCreateIfNotFound)
{
if (!mListenerManager && aCreateIfNotFound) {
mListenerManager =
new nsEventListenerManager(static_cast<nsIDOMEventTarget*>(this));
}
return mListenerManager;
}
nsIScriptContext*
nsWindowRoot::GetContextForEventHandlers(nsresult* aRv)
{
*aRv = NS_OK;
return nullptr;
}
nsresult
nsWindowRoot::PreHandleEvent(nsEventChainPreVisitor& aVisitor)
{
aVisitor.mCanHandle = true;
aVisitor.mForceContentDispatch = true; //FIXME! Bug 329119
// To keep mWindow alive
aVisitor.mItemData = static_cast<nsISupports *>(mWindow);
aVisitor.mParentTarget = mParent;
return NS_OK;
}
nsresult
nsWindowRoot::PostHandleEvent(nsEventChainPostVisitor& aVisitor)
{
return NS_OK;
}
nsPIDOMWindow*
nsWindowRoot::GetWindow()
{
return mWindow;
}
nsresult
nsWindowRoot::GetControllers(nsIControllers** aResult)
{
*aResult = nullptr;
// XXX: we should fix this so there's a generic interface that
// describes controllers, so this code would have no special
// knowledge of what object might have controllers.
nsCOMPtr<nsPIDOMWindow> focusedWindow;
nsIContent* focusedContent =
nsFocusManager::GetFocusedDescendant(mWindow, true, getter_AddRefs(focusedWindow));
if (focusedContent) {
#ifdef MOZ_XUL
nsCOMPtr<nsIDOMXULElement> xulElement(do_QueryInterface(focusedContent));
if (xulElement)
return xulElement->GetControllers(aResult);
#endif
nsCOMPtr<nsIDOMHTMLTextAreaElement> htmlTextArea =
do_QueryInterface(focusedContent);
if (htmlTextArea)
return htmlTextArea->GetControllers(aResult);
nsCOMPtr<nsIDOMHTMLInputElement> htmlInputElement =
do_QueryInterface(focusedContent);
if (htmlInputElement)
return htmlInputElement->GetControllers(aResult);
if (focusedContent->IsEditable() && focusedWindow)
return focusedWindow->GetControllers(aResult);
}
else {
nsCOMPtr<nsIDOMWindow> domWindow = do_QueryInterface(focusedWindow);
if (domWindow)
return domWindow->GetControllers(aResult);
}
return NS_OK;
}
nsresult
nsWindowRoot::GetControllerForCommand(const char * aCommand,
nsIController** _retval)
{
NS_ENSURE_ARG_POINTER(_retval);
*_retval = nullptr;
{
nsCOMPtr<nsIControllers> controllers;
GetControllers(getter_AddRefs(controllers));
if (controllers) {
nsCOMPtr<nsIController> controller;
controllers->GetControllerForCommand(aCommand, getter_AddRefs(controller));
if (controller) {
controller.forget(_retval);
return NS_OK;
}
}
}
nsCOMPtr<nsPIDOMWindow> focusedWindow;
nsFocusManager::GetFocusedDescendant(mWindow, true, getter_AddRefs(focusedWindow));
while (focusedWindow) {
nsCOMPtr<nsIControllers> controllers;
focusedWindow->GetControllers(getter_AddRefs(controllers));
if (controllers) {
nsCOMPtr<nsIController> controller;
controllers->GetControllerForCommand(aCommand,
getter_AddRefs(controller));
if (controller) {
controller.forget(_retval);
return NS_OK;
}
}
// XXXndeakin P3 is this casting safe?
nsGlobalWindow *win = static_cast<nsGlobalWindow*>(focusedWindow.get());
focusedWindow = win->GetPrivateParent();
}
return NS_OK;
}
nsIDOMNode*
nsWindowRoot::GetPopupNode()
{
return mPopupNode;
}
void
nsWindowRoot::SetPopupNode(nsIDOMNode* aNode)
{
mPopupNode = aNode;
}
///////////////////////////////////////////////////////////////////////////////////
nsresult
NS_NewWindowRoot(nsPIDOMWindow* aWindow, nsIDOMEventTarget** aResult)
{
*aResult = new nsWindowRoot(aWindow);
NS_ADDREF(*aResult);
return NS_OK;
}