Bug 500931 - Ensure that weak reference wrappers return the right object when called upon. r=bent sr=jst

This commit is contained in:
Blake Kaplan 2009-07-02 17:00:21 -07:00
parent 3a7c25ffc5
commit 0f90334c43
6 changed files with 134 additions and 9 deletions

View File

@ -97,29 +97,52 @@ NS_IMETHODIMP
xpcJSWeakReference::Get()
{
nsresult rv;
nsXPConnect* xpc = nsXPConnect::GetXPConnect();
if (!xpc) return NS_ERROR_UNEXPECTED;
if (!xpc)
return NS_ERROR_UNEXPECTED;
nsAXPCNativeCallContext* cc = nsnull;
rv = xpc->GetCurrentNativeCallContext(&cc);
NS_ENSURE_SUCCESS(rv, rv);
JSContext *cx;
cc->GetJSContext(&cx);
if (!cx)
return NS_ERROR_UNEXPECTED;
jsval *retval = nsnull;
cc->GetRetValPtr(&retval);
if (!retval) return NS_ERROR_UNEXPECTED;
if (!retval)
return NS_ERROR_UNEXPECTED;
*retval = JSVAL_NULL;
nsCOMPtr<nsIXPConnectWrappedJS> wrappedObj;
if (mWrappedJSObject &&
NS_SUCCEEDED(mWrappedJSObject->QueryReferent(NS_GET_IID(nsIXPConnectWrappedJS), getter_AddRefs(wrappedObj))) &&
wrappedObj) {
JSObject *obj;
wrappedObj->GetJSObject(&obj);
if (obj)
{
// Most users of XPCWrappedJS don't need to worry about
// re-wrapping because things are implicitly rewrapped by
// xpcconvert. However, because we're doing this directly
// through the native call context, we need to call
// nsXPConnect::GetWrapperForObject. But it takes a lot of
// arguments! It turns out that the thisObject hook on XPConnect
// objects does the right thing though, so...
if (obj->map->ops->thisObject &&
!(obj = obj->map->ops->thisObject(cx, obj)))
{
return NS_ERROR_FAILURE;
}
*retval = OBJECT_TO_JSVAL(obj);
}
}
return NS_OK;
}

View File

@ -53,7 +53,7 @@ REQUIRES = xpconnect \
xpconnect_tests \
$(NULL)
DIRS = idl mochitest
DIRS = idl mochitest chrome
ifndef MOZ_ENABLE_LIBXUL
DIRS += components

View File

@ -0,0 +1,51 @@
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is mozilla.org code.
#
# The Initial Developer of the Original Code is
# Mozilla Foundation.
# Portions created by the Initial Developer are Copyright (C) 2008
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#
# Alternatively, the contents of this file may be used under the terms of
# either of the GNU General Public License Version 2 or later (the "GPL"),
# or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
DEPTH = ../../../../..
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
relativesrcdir = js/src/xpconnect/tests/chrome
include $(DEPTH)/config/autoconf.mk
include $(topsrcdir)/config/rules.mk
_CHROME_FILES = \
test_bug500931.xul \
$(NULL)
libs:: $(_CHROME_FILES)
$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/chrome/$(relativesrcdir)

View File

@ -0,0 +1,43 @@
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
type="text/css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=500931
-->
<window title="Mozilla Bug 500931"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/javascript"
src="chrome://mochikit/content/MochiKit/packed.js"></script>
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<!-- test results are displayed in the html:body -->
<body xmlns="http://www.w3.org/1999/xhtml">
<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=500931"
target="_blank">Mozilla Bug 500931</a>
</body>
<!-- test code goes here -->
<script type="application/javascript"><![CDATA[
/** Test for Bug 500931 **/
function go() {
var ifr = document.getElementById("ifr");
var doc = ifr.contentDocument;
ok(doc.toString().indexOf("XPCNativeWrapper") >= 0, "doc is an XPCNativeWrapper");
var weak = Components.utils.getWeakReference(doc);
ok(weak.get().toString().indexOf("XPCNativeWrapper") >= 0, "weak reference returns a wrapper");
SimpleTest.finish();
}
SimpleTest.waitForExplicitFinish();
]]></script>
<iframe type="content"
src="http://example.org/tests/js/src/xpconnect/tests/mochitest/bug500931_helper.html"
onload="go()"
id="ifr">
</iframe>
</window>

View File

@ -44,7 +44,8 @@ relativesrcdir = js/src/xpconnect/tests/mochitest
include $(DEPTH)/config/autoconf.mk
include $(topsrcdir)/config/rules.mk
_TEST_FILES = inner.html \
_TEST_FILES = bug500931_helper.html \
inner.html \
test_bug361111.xul \
test_bug390488.html \
test_bug393269.html \

View File

@ -0,0 +1,7 @@
<html>
<head>
<title>Inner frame for bug 500931 mochitest</title>
</head>
<body>
</body>
</html>