js/xpconnect/src/XPCJSWeakReference.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/xpconnect/src/XPCJSWeakReference.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,95 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* vim: set ts=8 sts=4 et sw=4 tw=99: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "xpcprivate.h"
    1.11 +#include "XPCJSWeakReference.h"
    1.12 +
    1.13 +#include "nsContentUtils.h"
    1.14 +
    1.15 +using namespace JS;
    1.16 +
    1.17 +xpcJSWeakReference::xpcJSWeakReference()
    1.18 +{
    1.19 +}
    1.20 +
    1.21 +NS_IMPL_ISUPPORTS(xpcJSWeakReference, xpcIJSWeakReference)
    1.22 +
    1.23 +nsresult xpcJSWeakReference::Init(JSContext* cx, const JS::Value& object)
    1.24 +{
    1.25 +    if (!object.isObject())
    1.26 +        return NS_OK;
    1.27 +
    1.28 +    JS::RootedObject obj(cx, &object.toObject());
    1.29 +
    1.30 +    XPCCallContext ccx(NATIVE_CALLER, cx);
    1.31 +
    1.32 +    // See if the object is a wrapped native that supports weak references.
    1.33 +    nsISupports* supports =
    1.34 +        nsXPConnect::XPConnect()->GetNativeOfWrapper(cx, obj);
    1.35 +    nsCOMPtr<nsISupportsWeakReference> supportsWeakRef =
    1.36 +        do_QueryInterface(supports);
    1.37 +    if (supportsWeakRef) {
    1.38 +        supportsWeakRef->GetWeakReference(getter_AddRefs(mReferent));
    1.39 +        if (mReferent) {
    1.40 +            return NS_OK;
    1.41 +        }
    1.42 +    }
    1.43 +    // If it's not a wrapped native, or it is a wrapped native that does not
    1.44 +    // support weak references, fall back to getting a weak ref to the object.
    1.45 +
    1.46 +    // See if object is a wrapped JSObject.
    1.47 +    nsRefPtr<nsXPCWrappedJS> wrapped;
    1.48 +    nsresult rv = nsXPCWrappedJS::GetNewOrUsed(obj,
    1.49 +                                               NS_GET_IID(nsISupports),
    1.50 +                                               getter_AddRefs(wrapped));
    1.51 +    if (!wrapped) {
    1.52 +        NS_ERROR("can't get nsISupportsWeakReference wrapper for obj");
    1.53 +        return rv;
    1.54 +    }
    1.55 +
    1.56 +    return wrapped->GetWeakReference(getter_AddRefs(mReferent));
    1.57 +}
    1.58 +
    1.59 +NS_IMETHODIMP
    1.60 +xpcJSWeakReference::Get(JSContext* aCx, MutableHandleValue aRetval)
    1.61 +{
    1.62 +    aRetval.setNull();
    1.63 +
    1.64 +    if (!mReferent) {
    1.65 +        return NS_OK;
    1.66 +    }
    1.67 +
    1.68 +    nsCOMPtr<nsISupports> supports = do_QueryReferent(mReferent);
    1.69 +    if (!supports) {
    1.70 +        return NS_OK;
    1.71 +    }
    1.72 +
    1.73 +    nsCOMPtr<nsIXPConnectWrappedJS> wrappedObj = do_QueryInterface(supports);
    1.74 +    if (!wrappedObj) {
    1.75 +        // We have a generic XPCOM object that supports weak references here.
    1.76 +        // Wrap it and pass it out.
    1.77 +        return nsContentUtils::WrapNative(aCx, supports,
    1.78 +                                          &NS_GET_IID(nsISupports),
    1.79 +                                          aRetval);
    1.80 +    }
    1.81 +
    1.82 +    JS::RootedObject obj(aCx, wrappedObj->GetJSObject());
    1.83 +    if (!obj) {
    1.84 +        return NS_OK;
    1.85 +    }
    1.86 +
    1.87 +    // Most users of XPCWrappedJS don't need to worry about
    1.88 +    // re-wrapping because things are implicitly rewrapped by
    1.89 +    // xpcconvert. However, because we're doing this directly
    1.90 +    // through the native call context, we need to call
    1.91 +    // JS_WrapObject().
    1.92 +    if (!JS_WrapObject(aCx, &obj)) {
    1.93 +        return NS_ERROR_FAILURE;
    1.94 +    }
    1.95 +
    1.96 +    aRetval.setObject(*obj);
    1.97 +    return NS_OK;
    1.98 +}

mercurial