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