JSObject * WrapperFactory::WrapForSameCompartment(JSContext *cx, HandleObject objArg) { RootedObject obj(cx, objArg); MOZ_ASSERT(js::IsObjectInContextCompartment(obj, cx)); // NB: The contract of WrapForSameCompartment says that |obj| may or may not // be a security wrapper. These checks implicitly handle the security // wrapper case. // Outerize if necessary. This, in combination with the check in // PrepareForUnwrapping, means that calling JS_Wrap* always outerizes. obj = JS_ObjectToOuterObject(cx, obj); NS_ENSURE_TRUE(obj, nullptr); if (dom::GetSameCompartmentWrapperForDOMBinding(*obj.address())) { return obj; } MOZ_ASSERT(!dom::IsDOMObject(obj)); if (!IS_WN_WRAPPER(obj)) return obj; // Extract the WN. It should exist. XPCWrappedNative *wn = static_cast<XPCWrappedNative *>(xpc_GetJSPrivate(obj)); MOZ_ASSERT(wn, "Trying to wrap a dead WN!"); // The WN knows what to do. RootedObject wrapper(cx, wn->GetSameCompartmentSecurityWrapper(cx)); MOZ_ASSERT_IF(wrapper != obj && IsComponentsObject(js::UncheckedUnwrap(obj)), !Wrapper::wrapperHandler(wrapper)->isSafeToUnwrap()); return wrapper; }