diff --git a/content/base/src/nsDocument.cpp b/content/base/src/nsDocument.cpp index 1bce72c9d4fd..fd6567e71b60 100644 --- a/content/base/src/nsDocument.cpp +++ b/content/base/src/nsDocument.cpp @@ -1731,14 +1731,45 @@ nsDocument::StyleRuleRemoved(nsIStyleSheet* aStyleSheet, nsIStyleRule* aStyleRul nsresult nsDocument::GetScriptObject(nsIScriptContext *aContext, void** aScriptObject) { nsresult res = NS_OK; - nsIScriptGlobalObject *global = aContext->GetGlobalObject(); + nsCOMPtr global; if (nsnull == mScriptObject) { - res = NS_NewScriptDocument(aContext, (nsISupports *)(nsIDOMDocument *)this, global, (void**)&mScriptObject); + // XXX We make the (possibly erroneous) assumption that the first + // presentation shell represents the "primary view" of the document + // and that the JS parent chain should incorporate just that view. + // This is done for lack of a better model when we have multiple + // views. + nsIPresShell* shell = (nsIPresShell*) mPresShells.ElementAt(0); + if (shell) { + nsCOMPtr cx; + shell->GetPresContext(getter_AddRefs(cx)); + nsCOMPtr container; + + res = cx->GetContainer(getter_AddRefs(container)); + if (NS_SUCCEEDED(res) && container) { + nsCOMPtr sco = do_QueryInterface(container); + if (sco) { + res = sco->GetScriptGlobalObject(getter_AddRefs(global)); + } + } + } + // XXX If we can't find a view, parent to the calling context's + // global object. This may not be right either, but we need + // something. + else { + global = getter_AddRefs(aContext->GetGlobalObject()); + } + + if (NS_SUCCEEDED(res)) { + res = NS_NewScriptDocument(aContext, + (nsISupports *)(nsIDOMDocument *)this, + (nsISupports *)global, + (void**)&mScriptObject); + } } + *aScriptObject = mScriptObject; - NS_RELEASE(global); return res; } diff --git a/content/html/document/src/nsHTMLDocument.cpp b/content/html/document/src/nsHTMLDocument.cpp index 04d47dca2797..564aae69aa8d 100644 --- a/content/html/document/src/nsHTMLDocument.cpp +++ b/content/html/document/src/nsHTMLDocument.cpp @@ -2316,14 +2316,45 @@ NS_IMETHODIMP nsHTMLDocument::GetScriptObject(nsIScriptContext *aContext, void** aScriptObject) { nsresult res = NS_OK; - nsIScriptGlobalObject *global = aContext->GetGlobalObject(); + nsCOMPtr global; if (nsnull == mScriptObject) { - res = NS_NewScriptHTMLDocument(aContext, (nsISupports *)(nsIDOMHTMLDocument *)this, (nsISupports *)global, (void**)&mScriptObject); + // XXX We make the (possibly erroneous) assumption that the first + // presentation shell represents the "primary view" of the document + // and that the JS parent chain should incorporate just that view. + // This is done for lack of a better model when we have multiple + // views. + nsIPresShell* shell = (nsIPresShell*) mPresShells.ElementAt(0); + if (shell) { + nsCOMPtr cx; + shell->GetPresContext(getter_AddRefs(cx)); + nsCOMPtr container; + + res = cx->GetContainer(getter_AddRefs(container)); + if (NS_SUCCEEDED(res) && container) { + nsCOMPtr sco = do_QueryInterface(container); + if (sco) { + res = sco->GetScriptGlobalObject(getter_AddRefs(global)); + } + } + } + // XXX If we can't find a view, parent to the calling context's + // global object. This may not be right either, but we need + // something. + else { + global = getter_AddRefs(aContext->GetGlobalObject()); + } + + if (NS_SUCCEEDED(res)) { + res = NS_NewScriptHTMLDocument(aContext, + (nsISupports *)(nsIDOMHTMLDocument *)this, + (nsISupports *)global, + (void**)&mScriptObject); + } } + *aScriptObject = mScriptObject; - NS_RELEASE(global); return res; } diff --git a/layout/base/src/nsDocument.cpp b/layout/base/src/nsDocument.cpp index 1bce72c9d4fd..fd6567e71b60 100644 --- a/layout/base/src/nsDocument.cpp +++ b/layout/base/src/nsDocument.cpp @@ -1731,14 +1731,45 @@ nsDocument::StyleRuleRemoved(nsIStyleSheet* aStyleSheet, nsIStyleRule* aStyleRul nsresult nsDocument::GetScriptObject(nsIScriptContext *aContext, void** aScriptObject) { nsresult res = NS_OK; - nsIScriptGlobalObject *global = aContext->GetGlobalObject(); + nsCOMPtr global; if (nsnull == mScriptObject) { - res = NS_NewScriptDocument(aContext, (nsISupports *)(nsIDOMDocument *)this, global, (void**)&mScriptObject); + // XXX We make the (possibly erroneous) assumption that the first + // presentation shell represents the "primary view" of the document + // and that the JS parent chain should incorporate just that view. + // This is done for lack of a better model when we have multiple + // views. + nsIPresShell* shell = (nsIPresShell*) mPresShells.ElementAt(0); + if (shell) { + nsCOMPtr cx; + shell->GetPresContext(getter_AddRefs(cx)); + nsCOMPtr container; + + res = cx->GetContainer(getter_AddRefs(container)); + if (NS_SUCCEEDED(res) && container) { + nsCOMPtr sco = do_QueryInterface(container); + if (sco) { + res = sco->GetScriptGlobalObject(getter_AddRefs(global)); + } + } + } + // XXX If we can't find a view, parent to the calling context's + // global object. This may not be right either, but we need + // something. + else { + global = getter_AddRefs(aContext->GetGlobalObject()); + } + + if (NS_SUCCEEDED(res)) { + res = NS_NewScriptDocument(aContext, + (nsISupports *)(nsIDOMDocument *)this, + (nsISupports *)global, + (void**)&mScriptObject); + } } + *aScriptObject = mScriptObject; - NS_RELEASE(global); return res; } diff --git a/layout/html/document/src/nsHTMLDocument.cpp b/layout/html/document/src/nsHTMLDocument.cpp index 04d47dca2797..564aae69aa8d 100644 --- a/layout/html/document/src/nsHTMLDocument.cpp +++ b/layout/html/document/src/nsHTMLDocument.cpp @@ -2316,14 +2316,45 @@ NS_IMETHODIMP nsHTMLDocument::GetScriptObject(nsIScriptContext *aContext, void** aScriptObject) { nsresult res = NS_OK; - nsIScriptGlobalObject *global = aContext->GetGlobalObject(); + nsCOMPtr global; if (nsnull == mScriptObject) { - res = NS_NewScriptHTMLDocument(aContext, (nsISupports *)(nsIDOMHTMLDocument *)this, (nsISupports *)global, (void**)&mScriptObject); + // XXX We make the (possibly erroneous) assumption that the first + // presentation shell represents the "primary view" of the document + // and that the JS parent chain should incorporate just that view. + // This is done for lack of a better model when we have multiple + // views. + nsIPresShell* shell = (nsIPresShell*) mPresShells.ElementAt(0); + if (shell) { + nsCOMPtr cx; + shell->GetPresContext(getter_AddRefs(cx)); + nsCOMPtr container; + + res = cx->GetContainer(getter_AddRefs(container)); + if (NS_SUCCEEDED(res) && container) { + nsCOMPtr sco = do_QueryInterface(container); + if (sco) { + res = sco->GetScriptGlobalObject(getter_AddRefs(global)); + } + } + } + // XXX If we can't find a view, parent to the calling context's + // global object. This may not be right either, but we need + // something. + else { + global = getter_AddRefs(aContext->GetGlobalObject()); + } + + if (NS_SUCCEEDED(res)) { + res = NS_NewScriptHTMLDocument(aContext, + (nsISupports *)(nsIDOMHTMLDocument *)this, + (nsISupports *)global, + (void**)&mScriptObject); + } } + *aScriptObject = mScriptObject; - NS_RELEASE(global); return res; }