2010-05-18 05:28:37 -07:00
|
|
|
/* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 8; -*- */
|
|
|
|
/* vim: set sw=4 ts=8 et tw=80 : */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Content App.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* The Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2010
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsInProcessTabChildGlobal.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
#include "nsEventDispatcher.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIJSRuntimeService.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsScriptLoader.h"
|
|
|
|
#include "nsIJSContextStack.h"
|
|
|
|
#include "nsFrameLoader.h"
|
2010-08-12 09:47:22 -07:00
|
|
|
#include "nsIPrivateDOMEvent.h"
|
2011-01-14 14:03:21 -08:00
|
|
|
#include "xpcpublic.h"
|
2010-05-18 05:28:37 -07:00
|
|
|
|
|
|
|
bool SendSyncMessageToParent(void* aCallbackData,
|
|
|
|
const nsAString& aMessage,
|
|
|
|
const nsAString& aJSON,
|
2010-11-08 18:49:00 -08:00
|
|
|
InfallibleTArray<nsString>* aJSONRetVal)
|
2010-05-18 05:28:37 -07:00
|
|
|
{
|
|
|
|
nsInProcessTabChildGlobal* tabChild =
|
|
|
|
static_cast<nsInProcessTabChildGlobal*>(aCallbackData);
|
2010-06-11 04:14:01 -07:00
|
|
|
nsCOMPtr<nsIContent> owner = tabChild->mOwner;
|
|
|
|
nsTArray<nsCOMPtr<nsIRunnable> > asyncMessages;
|
|
|
|
asyncMessages.SwapElements(tabChild->mASyncMessages);
|
|
|
|
PRUint32 len = asyncMessages.Length();
|
2010-07-19 13:36:49 -07:00
|
|
|
for (PRUint32 i = 0; i < len; ++i) {
|
2010-06-11 04:14:01 -07:00
|
|
|
nsCOMPtr<nsIRunnable> async = asyncMessages[i];
|
2010-05-18 05:28:37 -07:00
|
|
|
async->Run();
|
|
|
|
}
|
|
|
|
if (tabChild->mChromeMessageManager) {
|
2010-06-11 04:14:01 -07:00
|
|
|
tabChild->mChromeMessageManager->ReceiveMessage(owner, aMessage, PR_TRUE,
|
2010-05-18 05:28:37 -07:00
|
|
|
aJSON, nsnull, aJSONRetVal);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
class nsAsyncMessageToParent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsAsyncMessageToParent(nsInProcessTabChildGlobal* aTabChild,
|
|
|
|
const nsAString& aMessage, const nsAString& aJSON)
|
|
|
|
: mTabChild(aTabChild), mMessage(aMessage), mJSON(aJSON) {}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2010-06-11 04:14:01 -07:00
|
|
|
mTabChild->mASyncMessages.RemoveElement(this);
|
2010-05-18 05:28:37 -07:00
|
|
|
if (mTabChild->mChromeMessageManager) {
|
|
|
|
mTabChild->mChromeMessageManager->ReceiveMessage(mTabChild->mOwner, mMessage,
|
|
|
|
PR_FALSE,
|
|
|
|
mJSON, nsnull, nsnull);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsRefPtr<nsInProcessTabChildGlobal> mTabChild;
|
|
|
|
nsString mMessage;
|
|
|
|
nsString mJSON;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool SendAsyncMessageToParent(void* aCallbackData,
|
|
|
|
const nsAString& aMessage,
|
|
|
|
const nsAString& aJSON)
|
|
|
|
{
|
|
|
|
nsInProcessTabChildGlobal* tabChild =
|
|
|
|
static_cast<nsInProcessTabChildGlobal*>(aCallbackData);
|
2010-06-11 04:14:01 -07:00
|
|
|
nsCOMPtr<nsIRunnable> ev =
|
|
|
|
new nsAsyncMessageToParent(tabChild, aMessage, aJSON);
|
|
|
|
tabChild->mASyncMessages.AppendElement(ev);
|
2010-05-18 05:28:37 -07:00
|
|
|
NS_DispatchToCurrentThread(ev);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsInProcessTabChildGlobal::nsInProcessTabChildGlobal(nsIDocShell* aShell,
|
|
|
|
nsIContent* aOwner,
|
|
|
|
nsFrameMessageManager* aChrome)
|
2010-08-10 10:18:26 -07:00
|
|
|
: mDocShell(aShell), mInitialized(PR_FALSE), mLoadingScript(PR_FALSE),
|
2010-05-18 05:28:37 -07:00
|
|
|
mDelayedDisconnect(PR_FALSE), mOwner(aOwner), mChromeMessageManager(aChrome)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsInProcessTabChildGlobal::~nsInProcessTabChildGlobal()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!mCx, "Couldn't release JSContext?!?");
|
2010-08-11 06:59:00 -07:00
|
|
|
|
|
|
|
if (mListenerManager) {
|
|
|
|
mListenerManager->Disconnect();
|
|
|
|
}
|
2010-05-18 05:28:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsInProcessTabChildGlobal::Init()
|
|
|
|
{
|
2011-04-14 05:04:12 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
nsresult rv =
|
|
|
|
#endif
|
|
|
|
InitTabChildGlobal();
|
2010-05-18 05:28:37 -07:00
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
|
|
|
|
"Couldn't initialize nsInProcessTabChildGlobal");
|
|
|
|
mMessageManager = new nsFrameMessageManager(PR_FALSE,
|
|
|
|
SendSyncMessageToParent,
|
|
|
|
SendAsyncMessageToParent,
|
|
|
|
nsnull,
|
|
|
|
this,
|
|
|
|
nsnull,
|
|
|
|
mCx);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsInProcessTabChildGlobal)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsInProcessTabChildGlobal,
|
|
|
|
nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mMessageManager)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mGlobal)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsInProcessTabChildGlobal,
|
|
|
|
nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mMessageManager)
|
2011-02-02 14:08:13 -08:00
|
|
|
nsFrameScriptExecutor::Traverse(tmp, cb);
|
2010-05-18 05:28:37 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsInProcessTabChildGlobal)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIFrameMessageManager)
|
2010-08-31 11:58:35 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISyncMessageSender)
|
2010-05-18 05:28:37 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIContentFrameMessageManager)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIInProcessContentFrameMessageManager)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIScriptContextPrincipal)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIScriptObjectPrincipal)
|
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(ContentFrameMessageManager)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(nsInProcessTabChildGlobal, nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsInProcessTabChildGlobal, nsDOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsInProcessTabChildGlobal::GetContent(nsIDOMWindow** aContent)
|
|
|
|
{
|
|
|
|
*aContent = nsnull;
|
|
|
|
nsCOMPtr<nsIDOMWindow> window = do_GetInterface(mDocShell);
|
|
|
|
window.swap(*aContent);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsInProcessTabChildGlobal::GetDocShell(nsIDocShell** aDocShell)
|
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*aDocShell = mDocShell);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-24 05:58:21 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsInProcessTabChildGlobal::PrivateNoteIntentionalCrash()
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2010-05-18 05:28:37 -07:00
|
|
|
void
|
|
|
|
nsInProcessTabChildGlobal::Disconnect()
|
|
|
|
{
|
2010-08-12 09:47:22 -07:00
|
|
|
// Let the frame scripts know the child is being closed. We do any other
|
|
|
|
// cleanup after the event has been fired. See DelayedDisconnect
|
|
|
|
nsContentUtils::AddScriptRunner(
|
|
|
|
NS_NewRunnableMethod(this, &nsInProcessTabChildGlobal::DelayedDisconnect)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsInProcessTabChildGlobal::DelayedDisconnect()
|
|
|
|
{
|
|
|
|
// Don't let the event escape
|
|
|
|
mOwner = nsnull;
|
|
|
|
|
|
|
|
// Fire the "unload" event
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
|
|
|
NS_NewDOMEvent(getter_AddRefs(event), nsnull, nsnull);
|
|
|
|
if (event) {
|
|
|
|
event->InitEvent(NS_LITERAL_STRING("unload"), PR_FALSE, PR_FALSE);
|
|
|
|
nsCOMPtr<nsIPrivateDOMEvent> privateEvent(do_QueryInterface(event));
|
|
|
|
privateEvent->SetTrusted(PR_TRUE);
|
|
|
|
|
|
|
|
PRBool dummy;
|
|
|
|
nsDOMEventTargetHelper::DispatchEvent(event, &dummy);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Continue with the Disconnect cleanup
|
2010-06-16 04:42:42 -07:00
|
|
|
nsCOMPtr<nsIDOMWindow> win = do_GetInterface(mDocShell);
|
|
|
|
nsCOMPtr<nsPIDOMWindow> pwin = do_QueryInterface(win);
|
|
|
|
if (pwin) {
|
|
|
|
pwin->SetChromeEventHandler(pwin->GetChromeEventHandler());
|
|
|
|
}
|
2010-05-18 05:28:37 -07:00
|
|
|
mDocShell = nsnull;
|
|
|
|
mChromeMessageManager = nsnull;
|
|
|
|
if (mMessageManager) {
|
|
|
|
static_cast<nsFrameMessageManager*>(mMessageManager.get())->Disconnect();
|
|
|
|
mMessageManager = nsnull;
|
|
|
|
}
|
2010-08-23 01:44:43 -07:00
|
|
|
if (mListenerManager) {
|
|
|
|
mListenerManager->Disconnect();
|
|
|
|
}
|
|
|
|
|
2010-05-18 05:28:37 -07:00
|
|
|
if (!mLoadingScript) {
|
|
|
|
if (mCx) {
|
2010-08-10 10:18:26 -07:00
|
|
|
DestroyCx();
|
2010-05-18 05:28:37 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mDelayedDisconnect = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(nsIContent *)
|
|
|
|
nsInProcessTabChildGlobal::GetOwnerContent()
|
|
|
|
{
|
|
|
|
return mOwner;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsInProcessTabChildGlobal::PreHandleEvent(nsEventChainPreVisitor& aVisitor)
|
|
|
|
{
|
|
|
|
aVisitor.mCanHandle = PR_TRUE;
|
|
|
|
aVisitor.mParentTarget = mOwner;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (mOwner) {
|
|
|
|
nsCOMPtr<nsIFrameLoaderOwner> owner = do_QueryInterface(mOwner);
|
|
|
|
nsRefPtr<nsFrameLoader> fl = owner->GetFrameLoader();
|
|
|
|
if (fl) {
|
|
|
|
NS_ASSERTION(this == fl->GetTabChildGlobalAsEventTarget(),
|
|
|
|
"Wrong event target!");
|
|
|
|
NS_ASSERTION(fl->mMessageManager == mChromeMessageManager,
|
|
|
|
"Wrong message manager!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsInProcessTabChildGlobal::InitTabChildGlobal()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIJSRuntimeService> runtimeSvc =
|
|
|
|
do_GetService("@mozilla.org/js/xpc/RuntimeService;1");
|
|
|
|
NS_ENSURE_STATE(runtimeSvc);
|
|
|
|
|
|
|
|
JSRuntime* rt = nsnull;
|
|
|
|
runtimeSvc->GetRuntime(&rt);
|
|
|
|
NS_ENSURE_STATE(rt);
|
|
|
|
|
|
|
|
JSContext* cx = JS_NewContext(rt, 8192);
|
|
|
|
NS_ENSURE_STATE(cx);
|
|
|
|
|
|
|
|
mCx = cx;
|
|
|
|
|
|
|
|
nsContentUtils::XPConnect()->SetSecurityManagerForJSContext(cx, nsContentUtils::GetSecurityManager(), 0);
|
|
|
|
nsContentUtils::GetSecurityManager()->GetSystemPrincipal(getter_AddRefs(mPrincipal));
|
|
|
|
|
2010-11-14 23:08:36 -08:00
|
|
|
JS_SetNativeStackQuota(cx, 128 * sizeof(size_t) * 1024);
|
2010-05-18 05:28:37 -07:00
|
|
|
|
|
|
|
JS_SetOptions(cx, JS_GetOptions(cx) | JSOPTION_JIT | JSOPTION_ANONFUNFIX | JSOPTION_PRIVATE_IS_NSISUPPORTS);
|
|
|
|
JS_SetVersion(cx, JSVERSION_LATEST);
|
2011-03-17 21:08:18 -07:00
|
|
|
JS_SetErrorReporter(cx, ContentScriptErrorReporter);
|
2010-05-18 05:28:37 -07:00
|
|
|
|
2011-01-14 14:03:21 -08:00
|
|
|
xpc_LocalizeContext(cx);
|
|
|
|
|
2010-05-18 05:28:37 -07:00
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
nsIXPConnect* xpc = nsContentUtils::XPConnect();
|
|
|
|
const PRUint32 flags = nsIXPConnect::INIT_JS_STANDARD_CLASSES |
|
|
|
|
/*nsIXPConnect::OMIT_COMPONENTS_OBJECT ? |*/
|
|
|
|
nsIXPConnect::FLAG_SYSTEM_GLOBAL_OBJECT;
|
|
|
|
|
|
|
|
nsISupports* scopeSupports =
|
|
|
|
NS_ISUPPORTS_CAST(nsPIDOMEventTarget*, this);
|
|
|
|
JS_SetContextPrivate(cx, scopeSupports);
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
xpc->InitClassesWithNewWrappedGlobal(cx, scopeSupports,
|
2010-07-19 13:36:49 -07:00
|
|
|
NS_GET_IID(nsISupports),
|
2010-10-26 23:57:31 -07:00
|
|
|
GetPrincipal(), nsnull,
|
2010-07-19 13:36:49 -07:00
|
|
|
flags, getter_AddRefs(mGlobal));
|
2010-05-18 05:28:37 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
JSObject* global = nsnull;
|
|
|
|
rv = mGlobal->GetJSObject(&global);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
JS_SetGlobalObject(cx, global);
|
2010-08-10 10:18:26 -07:00
|
|
|
DidCreateCx();
|
2010-05-18 05:28:37 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-19 07:48:17 -07:00
|
|
|
class nsAsyncScriptLoad : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsAsyncScriptLoad(nsInProcessTabChildGlobal* aTabChild, const nsAString& aURL)
|
|
|
|
: mTabChild(aTabChild), mURL(aURL) {}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
mTabChild->LoadFrameScript(mURL);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsRefPtr<nsInProcessTabChildGlobal> mTabChild;
|
|
|
|
nsString mURL;
|
|
|
|
};
|
|
|
|
|
2010-05-18 05:28:37 -07:00
|
|
|
void
|
|
|
|
nsInProcessTabChildGlobal::LoadFrameScript(const nsAString& aURL)
|
|
|
|
{
|
2011-05-19 07:48:17 -07:00
|
|
|
if (!nsContentUtils::IsSafeToRunScript()) {
|
|
|
|
nsContentUtils::AddScriptRunner(new nsAsyncScriptLoad(this, aURL));
|
|
|
|
return;
|
|
|
|
}
|
2010-05-18 05:28:37 -07:00
|
|
|
if (!mInitialized) {
|
|
|
|
mInitialized = PR_TRUE;
|
|
|
|
Init();
|
|
|
|
}
|
2010-08-10 10:18:26 -07:00
|
|
|
PRBool tmp = mLoadingScript;
|
|
|
|
mLoadingScript = PR_TRUE;
|
|
|
|
LoadFrameScriptInternal(aURL);
|
|
|
|
mLoadingScript = tmp;
|
2010-05-18 05:28:37 -07:00
|
|
|
if (!mLoadingScript && mDelayedDisconnect) {
|
|
|
|
mDelayedDisconnect = PR_FALSE;
|
|
|
|
Disconnect();
|
|
|
|
}
|
|
|
|
}
|