2010-05-18 12:28:37 +00:00
|
|
|
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-05-18 12:28:37 +00:00
|
|
|
#ifndef nsFrameMessageManager_h__
|
|
|
|
#define nsFrameMessageManager_h__
|
|
|
|
|
|
|
|
#include "nsIFrameMessageManager.h"
|
2010-08-16 20:40:04 +00:00
|
|
|
#include "nsIObserver.h"
|
2010-05-18 12:28:37 +00:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsCOMArray.h"
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsIAtom.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "nsTArray.h"
|
2010-08-10 17:18:26 +00:00
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsDataHashtable.h"
|
2010-08-16 20:40:04 +00:00
|
|
|
#include "mozilla/Services.h"
|
|
|
|
#include "nsIObserverService.h"
|
2011-10-11 10:28:46 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2012-06-19 02:30:09 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2010-05-18 12:28:37 +00:00
|
|
|
|
2011-08-02 19:57:48 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
class ContentParent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-18 12:28:37 +00:00
|
|
|
class nsAXPCNativeCallContext;
|
|
|
|
struct JSContext;
|
|
|
|
struct JSObject;
|
|
|
|
|
|
|
|
struct nsMessageListenerInfo
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIFrameMessageListener> mListener;
|
|
|
|
nsCOMPtr<nsIAtom> mMessage;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef bool (*nsLoadScriptCallback)(void* aCallbackData, const nsAString& aURL);
|
|
|
|
typedef bool (*nsSyncMessageCallback)(void* aCallbackData,
|
|
|
|
const nsAString& aMessage,
|
|
|
|
const nsAString& aJSON,
|
2010-11-09 02:49:00 +00:00
|
|
|
InfallibleTArray<nsString>* aJSONRetVal);
|
2010-05-18 12:28:37 +00:00
|
|
|
typedef bool (*nsAsyncMessageCallback)(void* aCallbackData,
|
|
|
|
const nsAString& aMessage,
|
|
|
|
const nsAString& aJSON);
|
|
|
|
|
2012-06-19 02:30:09 +00:00
|
|
|
class nsFrameMessageManager MOZ_FINAL : public nsIContentFrameMessageManager,
|
|
|
|
public nsIChromeFrameMessageManager
|
2010-05-18 12:28:37 +00:00
|
|
|
{
|
|
|
|
public:
|
2011-09-29 06:19:26 +00:00
|
|
|
nsFrameMessageManager(bool aChrome,
|
2010-05-18 12:28:37 +00:00
|
|
|
nsSyncMessageCallback aSyncCallback,
|
|
|
|
nsAsyncMessageCallback aAsyncCallback,
|
|
|
|
nsLoadScriptCallback aLoadScriptCallback,
|
|
|
|
void* aCallbackData,
|
|
|
|
nsFrameMessageManager* aParentManager,
|
2010-06-10 09:26:19 +00:00
|
|
|
JSContext* aContext,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aGlobal = false,
|
|
|
|
bool aProcessManager = false)
|
2010-08-31 18:58:35 +00:00
|
|
|
: mChrome(aChrome), mGlobal(aGlobal), mIsProcessManager(aProcessManager),
|
2012-01-07 19:20:12 +00:00
|
|
|
mHandlingMessage(false), mDisconnected(false), mParentManager(aParentManager),
|
2010-05-18 12:28:37 +00:00
|
|
|
mSyncCallback(aSyncCallback), mAsyncCallback(aAsyncCallback),
|
|
|
|
mLoadScriptCallback(aLoadScriptCallback), mCallbackData(aCallbackData),
|
|
|
|
mContext(aContext)
|
|
|
|
{
|
2010-08-31 18:58:35 +00:00
|
|
|
NS_ASSERTION(mContext || (aChrome && !aParentManager) || aProcessManager,
|
|
|
|
"Should have mContext in non-global/non-process manager!");
|
2010-05-18 12:28:37 +00:00
|
|
|
NS_ASSERTION(aChrome || !aParentManager, "Should not set parent manager!");
|
2010-06-10 09:26:19 +00:00
|
|
|
// This is a bit hackish. When parent manager is global, we want
|
|
|
|
// to attach the window message manager to it immediately.
|
|
|
|
// Is it just the frame message manager which waits until the
|
|
|
|
// content process is running.
|
|
|
|
if (mParentManager && (mCallbackData || IsWindowLevel())) {
|
2010-05-18 12:28:37 +00:00
|
|
|
mParentManager->AddChildManager(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~nsFrameMessageManager()
|
|
|
|
{
|
|
|
|
for (PRInt32 i = mChildManagers.Count(); i > 0; --i) {
|
|
|
|
static_cast<nsFrameMessageManager*>(mChildManagers[i - 1])->
|
2011-10-17 14:59:28 +00:00
|
|
|
Disconnect(false);
|
2010-05-18 12:28:37 +00:00
|
|
|
}
|
2010-08-31 18:58:35 +00:00
|
|
|
if (mIsProcessManager) {
|
|
|
|
if (this == sParentProcessManager) {
|
|
|
|
sParentProcessManager = nsnull;
|
|
|
|
}
|
|
|
|
if (this == sChildProcessManager) {
|
|
|
|
sChildProcessManager = nsnull;
|
2011-10-11 10:28:46 +00:00
|
|
|
delete sPendingSameProcessAsyncMessages;
|
|
|
|
sPendingSameProcessAsyncMessages = nsnull;
|
|
|
|
}
|
|
|
|
if (this == sSameProcessParentManager) {
|
|
|
|
sSameProcessParentManager = nsnull;
|
2010-08-31 18:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
2010-05-18 12:28:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsFrameMessageManager,
|
|
|
|
nsIContentFrameMessageManager)
|
|
|
|
NS_DECL_NSIFRAMEMESSAGEMANAGER
|
2010-08-31 18:58:35 +00:00
|
|
|
NS_DECL_NSISYNCMESSAGESENDER
|
2010-05-18 12:28:37 +00:00
|
|
|
NS_DECL_NSICONTENTFRAMEMESSAGEMANAGER
|
|
|
|
NS_DECL_NSICHROMEFRAMEMESSAGEMANAGER
|
2011-08-02 19:57:48 +00:00
|
|
|
NS_DECL_NSITREEITEMFRAMEMESSAGEMANAGER
|
|
|
|
|
|
|
|
static nsFrameMessageManager*
|
|
|
|
NewProcessMessageManager(mozilla::dom::ContentParent* aProcess);
|
2010-05-18 12:28:37 +00:00
|
|
|
|
|
|
|
nsresult ReceiveMessage(nsISupports* aTarget, const nsAString& aMessage,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aSync, const nsAString& aJSON,
|
2010-05-18 12:28:37 +00:00
|
|
|
JSObject* aObjectsArray,
|
2010-11-09 02:49:00 +00:00
|
|
|
InfallibleTArray<nsString>* aJSONRetVal,
|
2010-06-10 09:26:19 +00:00
|
|
|
JSContext* aContext = nsnull);
|
2010-05-18 12:28:37 +00:00
|
|
|
void AddChildManager(nsFrameMessageManager* aManager,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aLoadScripts = true);
|
2010-05-18 12:28:37 +00:00
|
|
|
void RemoveChildManager(nsFrameMessageManager* aManager)
|
|
|
|
{
|
|
|
|
mChildManagers.RemoveObject(aManager);
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
void Disconnect(bool aRemoveFromParent = true);
|
|
|
|
void SetCallbackData(void* aData, bool aLoadScripts = true);
|
2012-01-26 15:39:23 +00:00
|
|
|
void* GetCallbackData() { return mCallbackData; }
|
2011-10-01 16:14:36 +00:00
|
|
|
void GetParamsForMessage(const jsval& aObject,
|
|
|
|
JSContext* aCx,
|
|
|
|
nsAString& aJSON);
|
2010-05-18 12:28:37 +00:00
|
|
|
nsresult SendAsyncMessageInternal(const nsAString& aMessage,
|
|
|
|
const nsAString& aJSON);
|
|
|
|
JSContext* GetJSContext() { return mContext; }
|
2012-01-07 19:20:26 +00:00
|
|
|
void SetJSContext(JSContext* aCx) { mContext = aCx; }
|
|
|
|
void RemoveFromParent();
|
2010-05-18 12:28:37 +00:00
|
|
|
nsFrameMessageManager* GetParentManager() { return mParentManager; }
|
|
|
|
void SetParentManager(nsFrameMessageManager* aParent)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!mParentManager, "We have parent manager already!");
|
|
|
|
NS_ASSERTION(mChrome, "Should not set parent manager!");
|
|
|
|
mParentManager = aParent;
|
|
|
|
}
|
2011-09-29 06:19:26 +00:00
|
|
|
bool IsGlobal() { return mGlobal; }
|
|
|
|
bool IsWindowLevel() { return mParentManager && mParentManager->IsGlobal(); }
|
2010-08-31 18:58:35 +00:00
|
|
|
|
|
|
|
static nsFrameMessageManager* GetParentProcessManager()
|
|
|
|
{
|
|
|
|
return sParentProcessManager;
|
|
|
|
}
|
|
|
|
static nsFrameMessageManager* GetChildProcessManager()
|
|
|
|
{
|
|
|
|
return sChildProcessManager;
|
|
|
|
}
|
2010-05-18 12:28:37 +00:00
|
|
|
protected:
|
2012-01-07 19:20:12 +00:00
|
|
|
friend class MMListenerRemover;
|
2010-05-18 12:28:37 +00:00
|
|
|
nsTArray<nsMessageListenerInfo> mListeners;
|
|
|
|
nsCOMArray<nsIContentFrameMessageManager> mChildManagers;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mChrome;
|
|
|
|
bool mGlobal;
|
|
|
|
bool mIsProcessManager;
|
2012-01-07 19:20:12 +00:00
|
|
|
bool mHandlingMessage;
|
|
|
|
bool mDisconnected;
|
2010-05-18 12:28:37 +00:00
|
|
|
nsFrameMessageManager* mParentManager;
|
|
|
|
nsSyncMessageCallback mSyncCallback;
|
|
|
|
nsAsyncMessageCallback mAsyncCallback;
|
|
|
|
nsLoadScriptCallback mLoadScriptCallback;
|
|
|
|
void* mCallbackData;
|
|
|
|
JSContext* mContext;
|
|
|
|
nsTArray<nsString> mPendingScripts;
|
2010-08-31 18:58:35 +00:00
|
|
|
public:
|
|
|
|
static nsFrameMessageManager* sParentProcessManager;
|
|
|
|
static nsFrameMessageManager* sChildProcessManager;
|
2011-10-11 10:28:46 +00:00
|
|
|
static nsFrameMessageManager* sSameProcessParentManager;
|
|
|
|
static nsTArray<nsCOMPtr<nsIRunnable> >* sPendingSameProcessAsyncMessages;
|
2010-05-18 12:28:37 +00:00
|
|
|
};
|
|
|
|
|
2011-03-18 04:08:18 +00:00
|
|
|
void
|
|
|
|
ContentScriptErrorReporter(JSContext* aCx,
|
|
|
|
const char* aMessage,
|
|
|
|
JSErrorReport* aReport);
|
|
|
|
|
2010-08-16 20:40:04 +00:00
|
|
|
class nsScriptCacheCleaner;
|
|
|
|
|
2011-09-02 06:46:00 +00:00
|
|
|
struct nsFrameJSScriptExecutorHolder
|
2010-08-10 17:18:26 +00:00
|
|
|
{
|
2011-09-02 06:46:00 +00:00
|
|
|
nsFrameJSScriptExecutorHolder(JSScript* aScript) : mScript(aScript)
|
|
|
|
{ MOZ_COUNT_CTOR(nsFrameJSScriptExecutorHolder); }
|
|
|
|
~nsFrameJSScriptExecutorHolder()
|
|
|
|
{ MOZ_COUNT_DTOR(nsFrameJSScriptExecutorHolder); }
|
|
|
|
JSScript* mScript;
|
2010-08-10 17:18:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class nsFrameScriptExecutor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static void Shutdown();
|
|
|
|
protected:
|
2011-06-16 18:21:08 +00:00
|
|
|
friend class nsFrameScriptCx;
|
|
|
|
nsFrameScriptExecutor() : mCx(nsnull), mCxStackRefCnt(0),
|
2011-10-17 14:59:28 +00:00
|
|
|
mDelayedCxDestroy(false)
|
2011-01-21 22:51:41 +00:00
|
|
|
{ MOZ_COUNT_CTOR(nsFrameScriptExecutor); }
|
|
|
|
~nsFrameScriptExecutor()
|
|
|
|
{ MOZ_COUNT_DTOR(nsFrameScriptExecutor); }
|
2010-08-10 17:18:26 +00:00
|
|
|
void DidCreateCx();
|
|
|
|
// Call this when you want to destroy mCx.
|
|
|
|
void DestroyCx();
|
|
|
|
void LoadFrameScriptInternal(const nsAString& aURL);
|
2011-11-30 15:51:40 +00:00
|
|
|
bool InitTabChildGlobalInternal(nsISupports* aScope);
|
2011-02-02 22:08:13 +00:00
|
|
|
static void Traverse(nsFrameScriptExecutor *tmp,
|
|
|
|
nsCycleCollectionTraversalCallback &cb);
|
2010-08-10 17:18:26 +00:00
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> mGlobal;
|
|
|
|
JSContext* mCx;
|
2011-06-16 18:21:08 +00:00
|
|
|
PRUint32 mCxStackRefCnt;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mDelayedCxDestroy;
|
2010-08-10 17:18:26 +00:00
|
|
|
nsCOMPtr<nsIPrincipal> mPrincipal;
|
2011-09-02 06:46:00 +00:00
|
|
|
static nsDataHashtable<nsStringHashKey, nsFrameJSScriptExecutorHolder*>* sCachedScripts;
|
2010-08-16 20:40:04 +00:00
|
|
|
static nsRefPtr<nsScriptCacheCleaner> sScriptCacheCleaner;
|
|
|
|
};
|
|
|
|
|
2011-06-16 18:21:08 +00:00
|
|
|
class nsFrameScriptCx
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsFrameScriptCx(nsISupports* aOwner, nsFrameScriptExecutor* aExec)
|
|
|
|
: mOwner(aOwner), mExec(aExec)
|
|
|
|
{
|
|
|
|
++(mExec->mCxStackRefCnt);
|
|
|
|
}
|
|
|
|
~nsFrameScriptCx()
|
|
|
|
{
|
|
|
|
if (--(mExec->mCxStackRefCnt) == 0 &&
|
|
|
|
mExec->mDelayedCxDestroy) {
|
|
|
|
mExec->DestroyCx();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsISupports> mOwner;
|
|
|
|
nsFrameScriptExecutor* mExec;
|
|
|
|
};
|
|
|
|
|
2012-06-19 02:30:09 +00:00
|
|
|
class nsScriptCacheCleaner MOZ_FINAL : public nsIObserver
|
2010-08-16 20:40:04 +00:00
|
|
|
{
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
nsScriptCacheCleaner()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> obsSvc = mozilla::services::GetObserverService();
|
|
|
|
if (obsSvc)
|
2011-10-17 14:59:28 +00:00
|
|
|
obsSvc->AddObserver(this, "xpcom-shutdown", false);
|
2010-08-16 20:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP Observe(nsISupports *aSubject,
|
|
|
|
const char *aTopic,
|
|
|
|
const PRUnichar *aData)
|
|
|
|
{
|
|
|
|
nsFrameScriptExecutor::Shutdown();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-08-10 17:18:26 +00:00
|
|
|
};
|
|
|
|
|
2010-05-18 12:28:37 +00:00
|
|
|
#endif
|