Backout 30bfb150da06 (bug 449315) due to unit test timeouts.
[wine-gecko.git] / dom / src / events / nsJSEventListener.cpp
blob5d8cd234a07390f0b3192cd95bb8e35528bbde0c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
37 #include "nsJSEventListener.h"
38 #include "nsJSUtils.h"
39 #include "nsString.h"
40 #include "nsReadableUtils.h"
41 #include "nsIServiceManager.h"
42 #include "nsIScriptSecurityManager.h"
43 #include "nsIScriptContext.h"
44 #include "nsIScriptGlobalObject.h"
45 #include "nsIScriptRuntime.h"
46 #include "nsIXPConnect.h"
47 #include "nsIPrivateDOMEvent.h"
48 #include "nsGUIEvent.h"
49 #include "nsContentUtils.h"
50 #include "nsDOMScriptObjectHolder.h"
51 #include "nsIMutableArray.h"
52 #include "nsVariant.h"
55 #ifdef NS_DEBUG
56 #include "nsIJSContextStack.h"
57 #include "nsDOMJSUtils.h"
59 #include "nspr.h" // PR_fprintf
61 class EventListenerCounter
63 public:
64 ~EventListenerCounter() {
68 static EventListenerCounter sEventListenerCounter;
69 #endif
72 * nsJSEventListener implementation
74 nsJSEventListener::nsJSEventListener(nsIScriptContext *aContext,
75 void *aScopeObject,
76 nsISupports *aTarget)
77 : nsIJSEventListener(aContext, aScopeObject, aTarget),
78 mReturnResult(nsReturnResult_eNotSet)
80 // aScopeObject is the inner window's JS object, which we need to lock
81 // until we are done with it.
82 NS_ASSERTION(aScopeObject && aContext,
83 "EventListener with no context or scope?");
84 nsContentUtils::HoldScriptObject(aContext->GetScriptTypeID(), this,
85 &NS_CYCLE_COLLECTION_NAME(nsJSEventListener),
86 aScopeObject, PR_FALSE);
89 nsJSEventListener::~nsJSEventListener()
91 if (mContext)
92 nsContentUtils::DropScriptObjects(mContext->GetScriptTypeID(), this,
93 &NS_CYCLE_COLLECTION_NAME(nsJSEventListener));
96 NS_IMPL_CYCLE_COLLECTION_CLASS(nsJSEventListener)
97 NS_IMPL_CYCLE_COLLECTION_ROOT_BEGIN(nsJSEventListener)
98 if (tmp->mContext &&
99 tmp->mContext->GetScriptTypeID() == nsIProgrammingLanguage::JAVASCRIPT) {
100 NS_DROP_JS_OBJECTS(tmp, nsJSEventListener);
101 tmp->mScopeObject = nsnull;
103 NS_IMPL_CYCLE_COLLECTION_ROOT_END
104 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsJSEventListener)
105 NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mTarget)
106 if (tmp->mContext) {
107 if (tmp->mScopeObject) {
108 nsContentUtils::DropScriptObjects(tmp->mContext->GetScriptTypeID(), tmp,
109 &NS_CYCLE_COLLECTION_NAME(nsJSEventListener));
110 tmp->mScopeObject = nsnull;
112 NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mContext)
114 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
115 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsJSEventListener)
116 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mTarget)
117 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mContext)
118 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
119 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
121 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(nsJSEventListener)
122 NS_IMPL_CYCLE_COLLECTION_TRACE_MEMBER_CALLBACK(tmp->mContext->GetScriptTypeID(),
123 mScopeObject)
124 NS_IMPL_CYCLE_COLLECTION_TRACE_END
126 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsJSEventListener)
127 NS_INTERFACE_MAP_ENTRY(nsIDOMEventListener)
128 NS_INTERFACE_MAP_ENTRY(nsIJSEventListener)
129 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMEventListener)
130 NS_INTERFACE_MAP_END
132 NS_IMPL_CYCLE_COLLECTING_ADDREF_AMBIGUOUS(nsJSEventListener, nsIDOMEventListener)
133 NS_IMPL_CYCLE_COLLECTING_RELEASE_AMBIGUOUS(nsJSEventListener, nsIDOMEventListener)
135 //static nsString onPrefix = "on";
137 void
138 nsJSEventListener::SetEventName(nsIAtom* aName)
140 mEventName = aName;
143 nsresult
144 nsJSEventListener::HandleEvent(nsIDOMEvent* aEvent)
146 nsresult rv;
147 nsCOMPtr<nsIArray> iargv;
148 nsAutoString eventString;
149 nsCOMPtr<nsIAtom> atomName;
151 if (!mEventName) {
152 if (NS_OK != aEvent->GetType(eventString)) {
153 //JS can't handle this event yet or can't handle it at all
154 return NS_OK;
156 //if (mReturnResult == nsReturnResult_eNotSet) {
157 if (eventString.EqualsLiteral("error") ||
158 eventString.EqualsLiteral("mouseover")) {
159 mReturnResult = nsReturnResult_eReverseReturnResult;
161 else {
162 mReturnResult = nsReturnResult_eDoNotReverseReturnResult;
165 eventString.Assign(NS_LITERAL_STRING("on") + eventString);
166 atomName = do_GetAtom(eventString);
168 else {
169 mEventName->ToString(eventString);
170 atomName = mEventName;
174 nsScriptObjectHolder funcval(mContext);
175 rv = mContext->GetBoundEventHandler(mTarget, mScopeObject, atomName,
176 funcval);
177 NS_ENSURE_SUCCESS(rv, rv);
178 if (!funcval)
179 return NS_OK;
181 PRBool handledScriptError = PR_FALSE;
182 if (eventString.EqualsLiteral("onerror")) {
183 nsCOMPtr<nsIPrivateDOMEvent> priv(do_QueryInterface(aEvent));
184 NS_ENSURE_TRUE(priv, NS_ERROR_UNEXPECTED);
186 nsEvent *event = priv->GetInternalNSEvent();
187 if (event->message == NS_LOAD_ERROR &&
188 event->eventStructType == NS_SCRIPT_ERROR_EVENT) {
189 nsScriptErrorEvent *scriptEvent =
190 static_cast<nsScriptErrorEvent*>(event);
191 // Create a temp argv for the error event.
192 nsCOMPtr<nsIMutableArray> tempargv =
193 do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
194 if (NS_FAILED(rv)) return rv;
195 // Append the event args.
196 nsCOMPtr<nsIWritableVariant>
197 var(do_CreateInstance(NS_VARIANT_CONTRACTID, &rv));
198 NS_ENSURE_SUCCESS(rv, rv);
199 rv = var->SetAsWString(scriptEvent->errorMsg);
200 NS_ENSURE_SUCCESS(rv, rv);
201 rv = tempargv->AppendElement(var, PR_FALSE);
202 NS_ENSURE_SUCCESS(rv, rv);
203 // filename
204 var = do_CreateInstance(NS_VARIANT_CONTRACTID, &rv);
205 NS_ENSURE_SUCCESS(rv, rv);
206 rv = var->SetAsWString(scriptEvent->fileName);
207 NS_ENSURE_SUCCESS(rv, rv);
208 rv = tempargv->AppendElement(var, PR_FALSE);
209 NS_ENSURE_SUCCESS(rv, rv);
210 // line number
211 var = do_CreateInstance(NS_VARIANT_CONTRACTID, &rv);
212 NS_ENSURE_SUCCESS(rv, rv);
213 rv = var->SetAsUint32(scriptEvent->lineNr);
214 NS_ENSURE_SUCCESS(rv, rv);
215 rv = tempargv->AppendElement(var, PR_FALSE);
216 NS_ENSURE_SUCCESS(rv, rv);
218 // And set the real argv
219 iargv = do_QueryInterface(tempargv);
221 handledScriptError = PR_TRUE;
225 if (!handledScriptError) {
226 nsCOMPtr<nsIMutableArray> tempargv =
227 do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
228 if (NS_FAILED(rv)) return rv;
229 NS_ENSURE_TRUE(tempargv != nsnull, NS_ERROR_OUT_OF_MEMORY);
230 rv = tempargv->AppendElement(aEvent, PR_FALSE);
231 NS_ENSURE_SUCCESS(rv, rv);
232 iargv = do_QueryInterface(tempargv);
235 // mContext is the same context which event listener manager pushes
236 // to JS context stack.
237 #ifdef NS_DEBUG
238 JSContext* cx = nsnull;
239 nsCOMPtr<nsIJSContextStack> stack =
240 do_GetService("@mozilla.org/js/xpc/ContextStack;1");
241 NS_ASSERTION(stack && NS_SUCCEEDED(stack->Peek(&cx)) && cx &&
242 GetScriptContextFromJSContext(cx) == mContext,
243 "JSEventListener has wrong script context?");
244 #endif
245 nsCOMPtr<nsIVariant> vrv;
246 rv = mContext->CallEventHandler(mTarget, mScopeObject, funcval, iargv,
247 getter_AddRefs(vrv));
249 if (NS_SUCCEEDED(rv)) {
250 PRUint16 dataType = nsIDataType::VTYPE_VOID;
251 if (vrv)
252 vrv->GetDataType(&dataType);
253 if (eventString.EqualsLiteral("onbeforeunload")) {
254 nsCOMPtr<nsIPrivateDOMEvent> priv(do_QueryInterface(aEvent));
255 NS_ENSURE_TRUE(priv, NS_ERROR_UNEXPECTED);
257 nsEvent *event = priv->GetInternalNSEvent();
258 NS_ENSURE_TRUE(event && event->message == NS_BEFORE_PAGE_UNLOAD,
259 NS_ERROR_UNEXPECTED);
261 nsBeforePageUnloadEvent *beforeUnload =
262 static_cast<nsBeforePageUnloadEvent *>(event);
264 if (dataType != nsIDataType::VTYPE_VOID) {
265 aEvent->PreventDefault();
267 // Set the text in the beforeUnload event as long as it wasn't
268 // already set (through event.returnValue, which takes
269 // precedence over a value returned from a JS function in IE)
270 if ((dataType == nsIDataType::VTYPE_DOMSTRING ||
271 dataType == nsIDataType::VTYPE_CHAR_STR ||
272 dataType == nsIDataType::VTYPE_WCHAR_STR ||
273 dataType == nsIDataType::VTYPE_STRING_SIZE_IS ||
274 dataType == nsIDataType::VTYPE_WSTRING_SIZE_IS ||
275 dataType == nsIDataType::VTYPE_CSTRING ||
276 dataType == nsIDataType::VTYPE_ASTRING)
277 && beforeUnload->text.IsEmpty()) {
278 vrv->GetAsDOMString(beforeUnload->text);
281 } else if (dataType == nsIDataType::VTYPE_BOOL) {
282 // If the handler returned false and its sense is not reversed,
283 // or the handler returned true and its sense is reversed from
284 // the usual (false means cancel), then prevent default.
285 PRBool brv;
286 if (NS_SUCCEEDED(vrv->GetAsBool(&brv)) &&
287 brv == (mReturnResult == nsReturnResult_eReverseReturnResult)) {
288 aEvent->PreventDefault();
293 return rv;
297 * Factory functions
300 nsresult
301 NS_NewJSEventListener(nsIScriptContext *aContext, void *aScopeObject,
302 nsISupports*aTarget, nsIDOMEventListener ** aReturn)
304 nsJSEventListener* it =
305 new nsJSEventListener(aContext, aScopeObject, aTarget);
306 if (!it) {
307 return NS_ERROR_OUT_OF_MEMORY;
309 NS_ADDREF(*aReturn = it);
311 return NS_OK;