js/xpconnect/src/XPCJSWeakReference.cpp

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* vim: set ts=8 sts=4 et sw=4 tw=99: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "xpcprivate.h"
michael@0 8 #include "XPCJSWeakReference.h"
michael@0 9
michael@0 10 #include "nsContentUtils.h"
michael@0 11
michael@0 12 using namespace JS;
michael@0 13
michael@0 14 xpcJSWeakReference::xpcJSWeakReference()
michael@0 15 {
michael@0 16 }
michael@0 17
michael@0 18 NS_IMPL_ISUPPORTS(xpcJSWeakReference, xpcIJSWeakReference)
michael@0 19
michael@0 20 nsresult xpcJSWeakReference::Init(JSContext* cx, const JS::Value& object)
michael@0 21 {
michael@0 22 if (!object.isObject())
michael@0 23 return NS_OK;
michael@0 24
michael@0 25 JS::RootedObject obj(cx, &object.toObject());
michael@0 26
michael@0 27 XPCCallContext ccx(NATIVE_CALLER, cx);
michael@0 28
michael@0 29 // See if the object is a wrapped native that supports weak references.
michael@0 30 nsISupports* supports =
michael@0 31 nsXPConnect::XPConnect()->GetNativeOfWrapper(cx, obj);
michael@0 32 nsCOMPtr<nsISupportsWeakReference> supportsWeakRef =
michael@0 33 do_QueryInterface(supports);
michael@0 34 if (supportsWeakRef) {
michael@0 35 supportsWeakRef->GetWeakReference(getter_AddRefs(mReferent));
michael@0 36 if (mReferent) {
michael@0 37 return NS_OK;
michael@0 38 }
michael@0 39 }
michael@0 40 // If it's not a wrapped native, or it is a wrapped native that does not
michael@0 41 // support weak references, fall back to getting a weak ref to the object.
michael@0 42
michael@0 43 // See if object is a wrapped JSObject.
michael@0 44 nsRefPtr<nsXPCWrappedJS> wrapped;
michael@0 45 nsresult rv = nsXPCWrappedJS::GetNewOrUsed(obj,
michael@0 46 NS_GET_IID(nsISupports),
michael@0 47 getter_AddRefs(wrapped));
michael@0 48 if (!wrapped) {
michael@0 49 NS_ERROR("can't get nsISupportsWeakReference wrapper for obj");
michael@0 50 return rv;
michael@0 51 }
michael@0 52
michael@0 53 return wrapped->GetWeakReference(getter_AddRefs(mReferent));
michael@0 54 }
michael@0 55
michael@0 56 NS_IMETHODIMP
michael@0 57 xpcJSWeakReference::Get(JSContext* aCx, MutableHandleValue aRetval)
michael@0 58 {
michael@0 59 aRetval.setNull();
michael@0 60
michael@0 61 if (!mReferent) {
michael@0 62 return NS_OK;
michael@0 63 }
michael@0 64
michael@0 65 nsCOMPtr<nsISupports> supports = do_QueryReferent(mReferent);
michael@0 66 if (!supports) {
michael@0 67 return NS_OK;
michael@0 68 }
michael@0 69
michael@0 70 nsCOMPtr<nsIXPConnectWrappedJS> wrappedObj = do_QueryInterface(supports);
michael@0 71 if (!wrappedObj) {
michael@0 72 // We have a generic XPCOM object that supports weak references here.
michael@0 73 // Wrap it and pass it out.
michael@0 74 return nsContentUtils::WrapNative(aCx, supports,
michael@0 75 &NS_GET_IID(nsISupports),
michael@0 76 aRetval);
michael@0 77 }
michael@0 78
michael@0 79 JS::RootedObject obj(aCx, wrappedObj->GetJSObject());
michael@0 80 if (!obj) {
michael@0 81 return NS_OK;
michael@0 82 }
michael@0 83
michael@0 84 // Most users of XPCWrappedJS don't need to worry about
michael@0 85 // re-wrapping because things are implicitly rewrapped by
michael@0 86 // xpcconvert. However, because we're doing this directly
michael@0 87 // through the native call context, we need to call
michael@0 88 // JS_WrapObject().
michael@0 89 if (!JS_WrapObject(aCx, &obj)) {
michael@0 90 return NS_ERROR_FAILURE;
michael@0 91 }
michael@0 92
michael@0 93 aRetval.setObject(*obj);
michael@0 94 return NS_OK;
michael@0 95 }

mercurial