Bug 734029 - Move nsJSContext::{Set,Create}OuterObject to nsGlobalWindow; r=bholley

This commit is contained in:
Ms2ger 2012-03-24 09:18:21 +01:00
parent 01c6973dbd
commit aafc6a951d
5 changed files with 72 additions and 84 deletions

View File

@ -77,6 +77,7 @@
#include "jsapi.h" // for JSAutoRequest
#include "jsdbgapi.h" // for JS_ClearWatchPointsForObject
#include "jsfriendapi.h" // for JS_GetGlobalForFrame
#include "jswrapper.h"
#include "nsReadableUtils.h"
#include "nsDOMClassInfo.h"
#include "nsJSEnvironment.h"
@ -799,6 +800,21 @@ nsPIDOMWindow::~nsPIDOMWindow() {}
// nsOuterWindowProxy: Outer Window Proxy
//*****************************************************************************
class nsOuterWindowProxy : public js::Wrapper
{
public:
nsOuterWindowProxy() : js::Wrapper(0) {}
virtual bool isOuterWindow() {
return true;
}
JSString *obj_toString(JSContext *cx, JSObject *wrapper);
void finalize(JSContext *cx, JSObject *proxy);
static nsOuterWindowProxy singleton;
};
JSString *
nsOuterWindowProxy::obj_toString(JSContext *cx, JSObject *proxy)
{
@ -822,8 +838,8 @@ nsOuterWindowProxy::finalize(JSContext *cx, JSObject *proxy)
nsOuterWindowProxy
nsOuterWindowProxy::singleton;
JSObject *
NS_NewOuterWindowProxy(JSContext *cx, JSObject *parent)
static JSObject*
NewOuterWindowProxy(JSContext *cx, JSObject *parent)
{
JSAutoEnterCompartment ac;
if (!ac.enter(cx, parent)) {
@ -1882,6 +1898,46 @@ ReparentWaiverWrappers(JSDHashTable *table, JSDHashEntryHdr *hdr,
return JS_DHASH_NEXT;
}
nsresult
nsGlobalWindow::CreateOuterObject(nsGlobalWindow* aNewInner)
{
mContext->SetGlobalObject(this);
JSContext* cx = mContext->GetNativeContext();
if (IsChromeWindow()) {
// Always enable E4X for XUL and other chrome content -- there is no
// need to preserve the <!-- script hiding hack from JS-in-HTML daze
// (introduced in 1995 for graceful script degradation in Netscape 1,
// Mosaic, and other pre-JS browsers).
JS_SetOptions(cx, JS_GetOptions(cx) | JSOPTION_XML);
}
JSObject* outer = NewOuterWindowProxy(cx, aNewInner->FastGetGlobalJSObject());
if (!outer) {
return NS_ERROR_FAILURE;
}
js::SetProxyExtra(outer, 0,
js::PrivateValue(static_cast<nsIScriptGlobalObject*>(this)));
return SetOuterObject(cx, outer);
}
nsresult
nsGlobalWindow::SetOuterObject(JSContext* aCx, JSObject* aOuterObject)
{
// Force our context's global object to be the outer.
// NB: JS_SetGlobalObject sets aCx->compartment.
JS_SetGlobalObject(aCx, aOuterObject);
// Set up the prototype for the outer object.
JSObject* inner = JS_GetParent(aOuterObject);
JS_SetPrototype(aCx, aOuterObject, JS_GetPrototype(inner));
return NS_OK;
}
nsresult
nsGlobalWindow::SetNewDocument(nsIDocument* aDocument,
nsISupports* aState,
@ -2097,14 +2153,13 @@ nsGlobalWindow::SetNewDocument(nsIDocument* aDocument,
mInnerWindow = newInnerWindow;
if (!mJSObject) {
mContext->CreateOuterObject(this, newInnerWindow);
CreateOuterObject(newInnerWindow);
mContext->DidInitializeContext();
mJSObject = mContext->GetNativeGlobal();
SetWrapper(mJSObject);
} else {
JSObject *outerObject =
NS_NewOuterWindowProxy(cx, newInnerWindow->mJSObject);
JSObject *outerObject = NewOuterWindowProxy(cx, newInnerWindow->mJSObject);
if (!outerObject) {
NS_ERROR("out of memory");
return NS_ERROR_FAILURE;
@ -2133,7 +2188,7 @@ nsGlobalWindow::SetNewDocument(nsIDocument* aDocument,
JS_SetParent(cx, mJSObject, newInnerWindow->mJSObject);
mContext->SetOuterObject(mJSObject);
SetOuterObject(cx, mJSObject);
JSCompartment *compartment = js::GetObjectCompartment(mJSObject);
xpc::CompartmentPrivate *priv =

View File

@ -102,7 +102,6 @@
// JS includes
#include "jsapi.h"
#include "jswrapper.h"
#define DEFAULT_HOME_PAGE "www.mozilla.org"
#define PREF_BROWSER_STARTUP_HOMEPAGE "browser.startup.homepage"
@ -231,26 +230,6 @@ private:
nsAutoRefCnt mRefCnt;
};
//*****************************************************************************
// nsOuterWindow: Outer Window Proxy
//*****************************************************************************
class nsOuterWindowProxy : public js::Wrapper
{
public:
nsOuterWindowProxy() : js::Wrapper((unsigned)0) {}
virtual bool isOuterWindow() {
return true;
}
JSString *obj_toString(JSContext *cx, JSObject *wrapper);
void finalize(JSContext *cx, JSObject *proxy);
static nsOuterWindowProxy singleton;
};
JSObject *NS_NewOuterWindowProxy(JSContext *cx, JSObject *parent);
//*****************************************************************************
// nsGlobalWindow: Global Object for Scripting
//*****************************************************************************
@ -843,6 +822,9 @@ protected:
inline PRInt32 DOMMinTimeoutValue() const;
nsresult CreateOuterObject(nsGlobalWindow* aNewInner);
nsresult SetOuterObject(JSContext* aCx, JSObject* aOuterObject);
// When adding new member variables, be careful not to create cycles
// through JavaScript. If there is any chance that a member variable
// could own objects that are implemented in JavaScript, then those

View File

@ -95,6 +95,8 @@ public:
/* Get the ID of this language. */
virtual PRUint32 GetScriptTypeID() = 0;
virtual void SetGlobalObject(nsIScriptGlobalObject* aGlobalObject) = 0;
/**
* Compile and execute a script.
*
@ -317,19 +319,6 @@ public:
**/
virtual nsresult InitContext() = 0;
/**
* Creates the outer window for this context.
*
* @param aGlobalObject The script global object to use as our global.
*/
virtual nsresult CreateOuterObject(nsIScriptGlobalObject *aGlobalObject,
nsIScriptGlobalObject *aCurrentInner) = 0;
/**
* Given an outer object, updates this context with that outer object.
*/
virtual nsresult SetOuterObject(JSObject* aOuterObject) = 0;
/**
* Prepares this context for use with the current inner window for the
* context's global object. This must be called after CreateOuterObject.

View File

@ -2108,47 +2108,6 @@ nsJSContext::InitContext()
return NS_OK;
}
nsresult
nsJSContext::CreateOuterObject(nsIScriptGlobalObject *aGlobalObject,
nsIScriptGlobalObject *aCurrentInner)
{
mGlobalObjectRef = aGlobalObject;
nsCOMPtr<nsIDOMChromeWindow> chromeWindow(do_QueryInterface(aGlobalObject));
if (chromeWindow) {
// Always enable E4X for XUL and other chrome content -- there is no
// need to preserve the <!-- script hiding hack from JS-in-HTML daze
// (introduced in 1995 for graceful script degradation in Netscape 1,
// Mosaic, and other pre-JS browsers).
JS_SetOptions(mContext, JS_GetOptions(mContext) | JSOPTION_XML);
}
JSObject *outer =
NS_NewOuterWindowProxy(mContext, aCurrentInner->GetGlobalJSObject());
if (!outer) {
return NS_ERROR_FAILURE;
}
js::SetProxyExtra(outer, 0, js::PrivateValue(aGlobalObject));
return SetOuterObject(outer);
}
nsresult
nsJSContext::SetOuterObject(JSObject* aOuterObject)
{
// Force our context's global object to be the outer.
// NB: JS_SetGlobalObject sets mContext->compartment.
JS_SetGlobalObject(mContext, aOuterObject);
// Set up the prototype for the outer object.
JSObject *inner = JS_GetParent(aOuterObject);
JS_SetPrototype(mContext, aOuterObject, JS_GetPrototype(inner));
return NS_OK;
}
nsresult
nsJSContext::InitOuterWindow()
{

View File

@ -39,6 +39,7 @@
#include "nsIScriptContext.h"
#include "nsIScriptRuntime.h"
#include "nsIScriptGlobalObject.h"
#include "nsCOMPtr.h"
#include "jsapi.h"
#include "jsfriendapi.h"
@ -76,6 +77,11 @@ public:
virtual PRUint32 GetScriptTypeID()
{ return nsIProgrammingLanguage::JAVASCRIPT; }
virtual void SetGlobalObject(nsIScriptGlobalObject* aGlobalObject)
{
mGlobalObjectRef = aGlobalObject;
}
virtual nsresult EvaluateString(const nsAString& aScript,
JSObject* aScopeObject,
nsIPrincipal *principal,
@ -143,9 +149,6 @@ public:
virtual nsresult ConnectToInner(nsIScriptGlobalObject *aNewInner,
JSObject *aOuterGlobal);
virtual nsresult InitContext();
virtual nsresult CreateOuterObject(nsIScriptGlobalObject *aGlobalObject,
nsIScriptGlobalObject *aCurrentInner);
virtual nsresult SetOuterObject(JSObject* aOuterObject);
virtual nsresult InitOuterWindow();
virtual bool IsContextInitialized();