2011-10-01 16:14:36 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2010-05-18 12:28:37 +00:00
|
|
|
/* ***** 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.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
2011-10-01 16:14:36 +00:00
|
|
|
* the Mozilla Foundation.
|
2010-05-18 12:28:37 +00:00
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2010
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2011-10-01 16:14:36 +00:00
|
|
|
* Ms2ger <ms2ger@gmail.com>
|
2010-05-18 12:28:37 +00:00
|
|
|
*
|
|
|
|
* 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 ***** */
|
|
|
|
|
2010-08-31 18:58:35 +00:00
|
|
|
#include "ContentChild.h"
|
|
|
|
#include "ContentParent.h"
|
2010-05-18 12:28:37 +00:00
|
|
|
#include "nsFrameMessageManager.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "nsJSUtils.h"
|
2010-08-10 17:18:26 +00:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsScriptLoader.h"
|
|
|
|
#include "nsIJSContextStack.h"
|
2010-08-31 18:58:35 +00:00
|
|
|
#include "nsIXULRuntime.h"
|
2011-03-18 04:08:18 +00:00
|
|
|
#include "nsIScriptError.h"
|
|
|
|
#include "nsIConsoleService.h"
|
2011-05-19 14:48:17 +00:00
|
|
|
#include "nsIProtocolHandler.h"
|
2011-11-30 15:51:40 +00:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
|
|
|
#include "nsIJSRuntimeService.h"
|
|
|
|
#include "xpcpublic.h"
|
2010-08-31 18:58:35 +00:00
|
|
|
|
2011-11-10 00:27:08 +00:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include <android/log.h>
|
|
|
|
#endif
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
static bool
|
2010-08-31 18:58:35 +00:00
|
|
|
IsChromeProcess()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIXULRuntime> rt = do_GetService("@mozilla.org/xre/runtime;1");
|
2010-09-01 10:19:52 +00:00
|
|
|
if (!rt)
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2010-09-01 10:19:52 +00:00
|
|
|
|
2010-08-31 18:58:35 +00:00
|
|
|
PRUint32 type;
|
|
|
|
rt->GetProcessType(&type);
|
|
|
|
return type == nsIXULRuntime::PROCESS_TYPE_DEFAULT;
|
|
|
|
}
|
2010-05-18 12:28:37 +00:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsFrameMessageManager)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsFrameMessageManager)
|
|
|
|
PRUint32 count = tmp->mListeners.Length();
|
|
|
|
for (PRUint32 i = 0; i < count; i++) {
|
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mListeners[i] mListener");
|
|
|
|
cb.NoteXPCOMChild(tmp->mListeners[i].mListener.get());
|
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMARRAY(mChildManagers)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsFrameMessageManager)
|
|
|
|
tmp->mListeners.Clear();
|
|
|
|
for (PRInt32 i = tmp->mChildManagers.Count(); i > 0; --i) {
|
|
|
|
static_cast<nsFrameMessageManager*>(tmp->mChildManagers[i - 1])->
|
2011-10-17 14:59:28 +00:00
|
|
|
Disconnect(false);
|
2010-05-18 12:28:37 +00:00
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMARRAY(mChildManagers)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsFrameMessageManager)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIContentFrameMessageManager)
|
2010-06-10 09:26:19 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AGGREGATED(nsIFrameMessageManager,
|
|
|
|
(mChrome ?
|
|
|
|
static_cast<nsIFrameMessageManager*>(
|
|
|
|
static_cast<nsIChromeFrameMessageManager*>(this)) :
|
|
|
|
static_cast<nsIFrameMessageManager*>(
|
|
|
|
static_cast<nsIContentFrameMessageManager*>(this))))
|
2010-08-31 18:58:35 +00:00
|
|
|
/* nsIContentFrameMessageManager is accessible only in TabChildGlobal. */
|
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIContentFrameMessageManager,
|
|
|
|
!mChrome && !mIsProcessManager)
|
|
|
|
/* Message managers in child process support nsISyncMessageSender. */
|
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsISyncMessageSender, !mChrome)
|
2011-08-02 19:57:48 +00:00
|
|
|
/* Message managers in chrome process support nsITreeItemFrameMessageManager. */
|
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsITreeItemFrameMessageManager, mChrome)
|
2010-08-31 18:58:35 +00:00
|
|
|
/* Process message manager doesn't support nsIChromeFrameMessageManager. */
|
|
|
|
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIChromeFrameMessageManager,
|
|
|
|
mChrome && !mIsProcessManager)
|
2010-05-18 12:28:37 +00:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2011-03-06 11:11:31 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsFrameMessageManager)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsFrameMessageManager)
|
2010-05-18 12:28:37 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFrameMessageManager::AddMessageListener(const nsAString& aMessage,
|
|
|
|
nsIFrameMessageListener* aListener)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAtom> message = do_GetAtom(aMessage);
|
|
|
|
PRUint32 len = mListeners.Length();
|
|
|
|
for (PRUint32 i = 0; i < len; ++i) {
|
|
|
|
if (mListeners[i].mMessage == message &&
|
|
|
|
mListeners[i].mListener == aListener) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nsMessageListenerInfo* entry = mListeners.AppendElement();
|
|
|
|
NS_ENSURE_TRUE(entry, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
entry->mMessage = message;
|
|
|
|
entry->mListener = aListener;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFrameMessageManager::RemoveMessageListener(const nsAString& aMessage,
|
|
|
|
nsIFrameMessageListener* aListener)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAtom> message = do_GetAtom(aMessage);
|
|
|
|
PRUint32 len = mListeners.Length();
|
|
|
|
for (PRUint32 i = 0; i < len; ++i) {
|
|
|
|
if (mListeners[i].mMessage == message &&
|
|
|
|
mListeners[i].mListener == aListener) {
|
|
|
|
mListeners.RemoveElementAt(i);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFrameMessageManager::LoadFrameScript(const nsAString& aURL,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aAllowDelayedLoad)
|
2010-05-18 12:28:37 +00:00
|
|
|
{
|
2010-06-10 09:26:19 +00:00
|
|
|
if (aAllowDelayedLoad) {
|
|
|
|
if (IsGlobal() || IsWindowLevel()) {
|
|
|
|
// Cache for future windows or frames
|
|
|
|
mPendingScripts.AppendElement(aURL);
|
|
|
|
} else if (!mCallbackData) {
|
|
|
|
// We're frame message manager, which isn't connected yet.
|
|
|
|
mPendingScripts.AppendElement(aURL);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-05-18 12:28:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mCallbackData) {
|
|
|
|
#ifdef DEBUG_smaug
|
|
|
|
printf("Will load %s \n", NS_ConvertUTF16toUTF8(aURL).get());
|
|
|
|
#endif
|
|
|
|
NS_ENSURE_TRUE(mLoadScriptCallback(mCallbackData, aURL), NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
2010-06-10 09:26:19 +00:00
|
|
|
for (PRInt32 i = 0; i < mChildManagers.Count(); ++i) {
|
|
|
|
nsRefPtr<nsFrameMessageManager> mm =
|
|
|
|
static_cast<nsFrameMessageManager*>(mChildManagers[i]);
|
2010-05-18 12:28:37 +00:00
|
|
|
if (mm) {
|
2011-10-17 14:59:28 +00:00
|
|
|
// Use false here, so that child managers don't cache the script, which
|
2010-06-10 09:26:19 +00:00
|
|
|
// is already cached in the parent.
|
2011-10-17 14:59:28 +00:00
|
|
|
mm->LoadFrameScript(aURL, false);
|
2010-05-18 12:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-08-24 10:16:29 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFrameMessageManager::RemoveDelayedFrameScript(const nsAString& aURL)
|
|
|
|
{
|
|
|
|
mPendingScripts.RemoveElement(aURL);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-05-18 12:28:37 +00:00
|
|
|
static JSBool
|
|
|
|
JSONCreator(const jschar* aBuf, uint32 aLen, void* aData)
|
|
|
|
{
|
|
|
|
nsAString* result = static_cast<nsAString*>(aData);
|
2011-10-01 16:14:36 +00:00
|
|
|
result->Append(static_cast<const PRUnichar*>(aBuf),
|
|
|
|
static_cast<PRUint32>(aLen));
|
|
|
|
return true;
|
2010-05-18 12:28:37 +00:00
|
|
|
}
|
|
|
|
|
2011-10-01 16:14:36 +00:00
|
|
|
void
|
|
|
|
nsFrameMessageManager::GetParamsForMessage(const jsval& aObject,
|
|
|
|
JSContext* aCx,
|
2010-05-18 12:28:37 +00:00
|
|
|
nsAString& aJSON)
|
|
|
|
{
|
|
|
|
aJSON.Truncate();
|
2011-10-01 16:14:36 +00:00
|
|
|
JSAutoRequest ar(aCx);
|
|
|
|
jsval v = aObject;
|
|
|
|
JS_Stringify(aCx, &v, nsnull, JSVAL_NULL, JSONCreator, &aJSON);
|
2010-05-18 12:28:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-10-01 16:14:36 +00:00
|
|
|
nsFrameMessageManager::SendSyncMessage(const nsAString& aMessageName,
|
|
|
|
const jsval& aObject,
|
|
|
|
JSContext* aCx,
|
|
|
|
PRUint8 aArgc,
|
|
|
|
jsval* aRetval)
|
2010-05-18 12:28:37 +00:00
|
|
|
{
|
2010-06-10 09:26:19 +00:00
|
|
|
NS_ASSERTION(!IsGlobal(), "Should not call SendSyncMessage in chrome");
|
|
|
|
NS_ASSERTION(!IsWindowLevel(), "Should not call SendSyncMessage in chrome");
|
|
|
|
NS_ASSERTION(!mParentManager, "Should not have parent manager in content!");
|
2011-10-01 16:14:36 +00:00
|
|
|
*aRetval = JSVAL_VOID;
|
2010-05-18 12:28:37 +00:00
|
|
|
if (mSyncCallback) {
|
|
|
|
NS_ENSURE_TRUE(mCallbackData, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
nsString json;
|
2011-10-01 16:14:36 +00:00
|
|
|
if (aArgc >= 2) {
|
|
|
|
GetParamsForMessage(aObject, aCx, json);
|
|
|
|
}
|
2010-11-09 02:49:00 +00:00
|
|
|
InfallibleTArray<nsString> retval;
|
2011-10-01 16:14:36 +00:00
|
|
|
if (mSyncCallback(mCallbackData, aMessageName, json, &retval)) {
|
|
|
|
JSAutoRequest ar(aCx);
|
2010-05-18 12:28:37 +00:00
|
|
|
PRUint32 len = retval.Length();
|
2011-10-01 16:14:36 +00:00
|
|
|
JSObject* dataArray = JS_NewArrayObject(aCx, len, NULL);
|
2010-05-18 12:28:37 +00:00
|
|
|
NS_ENSURE_TRUE(dataArray, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
for (PRUint32 i = 0; i < len; ++i) {
|
2011-10-01 16:14:36 +00:00
|
|
|
if (retval[i].IsEmpty()) {
|
2010-07-15 14:33:35 +00:00
|
|
|
continue;
|
2011-10-01 16:14:36 +00:00
|
|
|
}
|
2010-07-15 14:33:35 +00:00
|
|
|
|
2010-05-18 12:28:37 +00:00
|
|
|
jsval ret = JSVAL_VOID;
|
2011-10-01 16:14:36 +00:00
|
|
|
if (!JS_ParseJSON(aCx, static_cast<const jschar*>(retval[i].get()),
|
|
|
|
retval[i].Length(), &ret)) {
|
2011-03-21 18:42:06 +00:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2010-05-18 12:28:37 +00:00
|
|
|
}
|
2011-10-01 16:14:36 +00:00
|
|
|
NS_ENSURE_TRUE(JS_SetElement(aCx, dataArray, i, &ret), NS_ERROR_OUT_OF_MEMORY);
|
2010-05-18 12:28:37 +00:00
|
|
|
}
|
|
|
|
|
2011-10-01 16:14:36 +00:00
|
|
|
*aRetval = OBJECT_TO_JSVAL(dataArray);
|
2010-05-18 12:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsFrameMessageManager::SendAsyncMessageInternal(const nsAString& aMessage,
|
|
|
|
const nsAString& aJSON)
|
|
|
|
{
|
|
|
|
if (mAsyncCallback) {
|
|
|
|
NS_ENSURE_TRUE(mCallbackData, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
mAsyncCallback(mCallbackData, aMessage, aJSON);
|
|
|
|
}
|
|
|
|
PRInt32 len = mChildManagers.Count();
|
|
|
|
for (PRInt32 i = 0; i < len; ++i) {
|
|
|
|
static_cast<nsFrameMessageManager*>(mChildManagers[i])->
|
|
|
|
SendAsyncMessageInternal(aMessage, aJSON);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-10-01 16:14:36 +00:00
|
|
|
nsFrameMessageManager::SendAsyncMessage(const nsAString& aMessageName,
|
|
|
|
const jsval& aObject,
|
|
|
|
JSContext* aCx,
|
|
|
|
PRUint8 aArgc)
|
2010-05-18 12:28:37 +00:00
|
|
|
{
|
|
|
|
nsString json;
|
2011-10-01 16:14:36 +00:00
|
|
|
if (aArgc >= 2) {
|
|
|
|
GetParamsForMessage(aObject, aCx, json);
|
|
|
|
}
|
|
|
|
return SendAsyncMessageInternal(aMessageName, json);
|
2010-05-18 12:28:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFrameMessageManager::Dump(const nsAString& aStr)
|
|
|
|
{
|
2011-11-10 00:27:08 +00:00
|
|
|
#ifdef ANDROID
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, "Gecko", NS_ConvertUTF16toUTF8(aStr).get());
|
|
|
|
#endif
|
2010-05-18 12:28:37 +00:00
|
|
|
fputs(NS_ConvertUTF16toUTF8(aStr).get(), stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-24 13:58:21 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFrameMessageManager::PrivateNoteIntentionalCrash()
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2010-05-18 12:28:37 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFrameMessageManager::GetContent(nsIDOMWindow** aContent)
|
|
|
|
{
|
|
|
|
*aContent = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFrameMessageManager::GetDocShell(nsIDocShell** aDocShell)
|
|
|
|
{
|
|
|
|
*aDocShell = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-08-02 19:57:48 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFrameMessageManager::GetChildCount(PRUint32* aChildCount)
|
|
|
|
{
|
|
|
|
*aChildCount = static_cast<PRUint32>(mChildManagers.Count());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFrameMessageManager::GetChildAt(PRUint32 aIndex,
|
|
|
|
nsITreeItemFrameMessageManager** aMM)
|
|
|
|
{
|
|
|
|
*aMM = nsnull;
|
|
|
|
nsCOMPtr<nsITreeItemFrameMessageManager> mm =
|
|
|
|
do_QueryInterface(mChildManagers.SafeObjectAt(static_cast<PRUint32>(aIndex)));
|
|
|
|
mm.swap(*aMM);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-06-25 09:52:00 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFrameMessageManager::Btoa(const nsAString& aBinaryData,
|
|
|
|
nsAString& aAsciiBase64String)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFrameMessageManager::Atob(const nsAString& aAsciiString,
|
|
|
|
nsAString& aBinaryData)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-07 19:20:12 +00:00
|
|
|
class MMListenerRemover
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MMListenerRemover(nsFrameMessageManager* aMM)
|
|
|
|
: mMM(aMM), mWasHandlingMessage(aMM->mHandlingMessage)
|
|
|
|
{
|
|
|
|
mMM->mHandlingMessage = true;
|
|
|
|
}
|
|
|
|
~MMListenerRemover()
|
|
|
|
{
|
|
|
|
if (!mWasHandlingMessage) {
|
|
|
|
mMM->mHandlingMessage = false;
|
|
|
|
if (mMM->mDisconnected) {
|
|
|
|
mMM->mListeners.Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mWasHandlingMessage;
|
|
|
|
nsRefPtr<nsFrameMessageManager> mMM;
|
|
|
|
};
|
|
|
|
|
2010-05-18 12:28:37 +00:00
|
|
|
nsresult
|
|
|
|
nsFrameMessageManager::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)
|
2010-05-18 12:28:37 +00:00
|
|
|
{
|
2010-06-10 09:26:19 +00:00
|
|
|
JSContext* ctx = mContext ? mContext : aContext;
|
2010-08-31 18:58:35 +00:00
|
|
|
if (!ctx) {
|
|
|
|
nsContentUtils::ThreadJSContextStack()->GetSafeJSContext(&ctx);
|
|
|
|
}
|
2010-05-18 12:28:37 +00:00
|
|
|
if (mListeners.Length()) {
|
|
|
|
nsCOMPtr<nsIAtom> name = do_GetAtom(aMessage);
|
2012-01-07 19:20:12 +00:00
|
|
|
MMListenerRemover lr(this);
|
2010-05-18 12:28:37 +00:00
|
|
|
|
|
|
|
for (PRUint32 i = 0; i < mListeners.Length(); ++i) {
|
|
|
|
if (mListeners[i].mMessage == name) {
|
|
|
|
nsCOMPtr<nsIXPConnectWrappedJS> wrappedJS =
|
|
|
|
do_QueryInterface(mListeners[i].mListener);
|
|
|
|
if (!wrappedJS) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
JSObject* object = nsnull;
|
|
|
|
wrappedJS->GetJSObject(&object);
|
|
|
|
if (!object) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
nsCxPusher pusher;
|
2011-10-17 14:59:28 +00:00
|
|
|
NS_ENSURE_STATE(pusher.Push(ctx, false));
|
2010-05-18 12:28:37 +00:00
|
|
|
|
2010-06-10 09:26:19 +00:00
|
|
|
JSAutoRequest ar(ctx);
|
2010-05-18 12:28:37 +00:00
|
|
|
|
2011-05-12 10:26:05 +00:00
|
|
|
JSAutoEnterCompartment ac;
|
|
|
|
if (!ac.enter(ctx, object))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2010-05-18 12:28:37 +00:00
|
|
|
// The parameter for the listener function.
|
2010-06-10 09:26:19 +00:00
|
|
|
JSObject* param = JS_NewObject(ctx, NULL, NULL, NULL);
|
2010-05-18 12:28:37 +00:00
|
|
|
NS_ENSURE_TRUE(param, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
jsval targetv;
|
2010-06-10 09:26:19 +00:00
|
|
|
nsContentUtils::WrapNative(ctx,
|
2011-05-12 10:26:05 +00:00
|
|
|
JS_GetGlobalForObject(ctx, object),
|
2011-10-17 14:59:28 +00:00
|
|
|
aTarget, &targetv, nsnull, true);
|
2010-05-18 12:28:37 +00:00
|
|
|
|
|
|
|
// To keep compatibility with e10s message manager,
|
|
|
|
// define empty objects array.
|
|
|
|
if (!aObjectsArray) {
|
|
|
|
// Because we want JS messages to have always the same properties,
|
|
|
|
// create array even if len == 0.
|
2010-07-23 23:08:59 +00:00
|
|
|
aObjectsArray = JS_NewArrayObject(ctx, 0, NULL);
|
2010-05-18 12:28:37 +00:00
|
|
|
if (!aObjectsArray) {
|
2011-02-08 22:05:11 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2010-05-18 12:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-11 08:23:08 +00:00
|
|
|
JS::AutoValueRooter objectsv(ctx);
|
2011-06-23 16:46:38 +00:00
|
|
|
objectsv.set(OBJECT_TO_JSVAL(aObjectsArray));
|
|
|
|
if (!JS_WrapValue(ctx, objectsv.jsval_addr()))
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2010-05-18 12:28:37 +00:00
|
|
|
jsval json = JSVAL_NULL;
|
2010-11-26 14:11:14 +00:00
|
|
|
if (!aJSON.IsEmpty()) {
|
2011-10-01 16:14:36 +00:00
|
|
|
if (!JS_ParseJSON(ctx, static_cast<const jschar*>(PromiseFlatString(aJSON).get()),
|
|
|
|
aJSON.Length(), &json)) {
|
2011-03-21 18:42:06 +00:00
|
|
|
json = JSVAL_NULL;
|
2010-05-18 12:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
JSString* jsMessage =
|
2010-06-10 09:26:19 +00:00
|
|
|
JS_NewUCStringCopyN(ctx,
|
2011-10-01 16:14:36 +00:00
|
|
|
static_cast<const jschar*>(PromiseFlatString(aMessage).get()),
|
2010-05-18 12:28:37 +00:00
|
|
|
aMessage.Length());
|
|
|
|
NS_ENSURE_TRUE(jsMessage, NS_ERROR_OUT_OF_MEMORY);
|
2010-06-10 09:26:19 +00:00
|
|
|
JS_DefineProperty(ctx, param, "target", targetv, NULL, NULL, JSPROP_ENUMERATE);
|
|
|
|
JS_DefineProperty(ctx, param, "name",
|
2010-05-18 12:28:37 +00:00
|
|
|
STRING_TO_JSVAL(jsMessage), NULL, NULL, JSPROP_ENUMERATE);
|
2010-06-10 09:26:19 +00:00
|
|
|
JS_DefineProperty(ctx, param, "sync",
|
2010-05-18 12:28:37 +00:00
|
|
|
BOOLEAN_TO_JSVAL(aSync), NULL, NULL, JSPROP_ENUMERATE);
|
2010-06-10 09:26:19 +00:00
|
|
|
JS_DefineProperty(ctx, param, "json", json, NULL, NULL, JSPROP_ENUMERATE);
|
2011-06-23 16:46:38 +00:00
|
|
|
JS_DefineProperty(ctx, param, "objects", objectsv.jsval_value(), NULL, NULL, JSPROP_ENUMERATE);
|
2010-05-18 12:28:37 +00:00
|
|
|
|
|
|
|
jsval thisValue = JSVAL_VOID;
|
|
|
|
|
|
|
|
jsval funval = JSVAL_VOID;
|
2011-07-27 20:21:53 +00:00
|
|
|
if (JS_ObjectIsCallable(ctx, object)) {
|
2010-05-18 12:28:37 +00:00
|
|
|
// If the listener is a JS function:
|
|
|
|
funval = OBJECT_TO_JSVAL(object);
|
2010-06-10 09:26:19 +00:00
|
|
|
|
|
|
|
// A small hack to get 'this' value right on content side where
|
|
|
|
// messageManager is wrapped in TabChildGlobal.
|
|
|
|
nsCOMPtr<nsISupports> defaultThisValue;
|
|
|
|
if (mChrome) {
|
2011-08-30 21:45:31 +00:00
|
|
|
defaultThisValue = do_QueryObject(this);
|
2010-06-10 09:26:19 +00:00
|
|
|
} else {
|
|
|
|
defaultThisValue = aTarget;
|
|
|
|
}
|
|
|
|
nsContentUtils::WrapNative(ctx,
|
2011-05-12 10:26:05 +00:00
|
|
|
JS_GetGlobalForObject(ctx, object),
|
2011-10-17 14:59:28 +00:00
|
|
|
defaultThisValue, &thisValue, nsnull, true);
|
2010-05-18 12:28:37 +00:00
|
|
|
} else {
|
|
|
|
// If the listener is a JS object which has receiveMessage function:
|
2010-06-10 09:26:19 +00:00
|
|
|
NS_ENSURE_STATE(JS_GetProperty(ctx, object, "receiveMessage",
|
2010-05-18 12:28:37 +00:00
|
|
|
&funval) &&
|
|
|
|
JSVAL_IS_OBJECT(funval) &&
|
|
|
|
!JSVAL_IS_NULL(funval));
|
|
|
|
JSObject* funobject = JSVAL_TO_OBJECT(funval);
|
2011-07-27 20:21:53 +00:00
|
|
|
NS_ENSURE_STATE(JS_ObjectIsCallable(ctx, funobject));
|
2010-05-18 12:28:37 +00:00
|
|
|
thisValue = OBJECT_TO_JSVAL(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
jsval rval = JSVAL_VOID;
|
|
|
|
|
2012-01-11 08:23:08 +00:00
|
|
|
JS::AutoValueRooter argv(ctx);
|
2010-07-15 06:19:36 +00:00
|
|
|
argv.set(OBJECT_TO_JSVAL(param));
|
2010-05-18 12:28:37 +00:00
|
|
|
|
2010-10-10 22:46:16 +00:00
|
|
|
{
|
|
|
|
JSAutoEnterCompartment tac;
|
|
|
|
|
|
|
|
JSObject* thisObject = JSVAL_TO_OBJECT(thisValue);
|
|
|
|
|
|
|
|
if (!tac.enter(ctx, thisObject) ||
|
2011-05-30 18:59:37 +00:00
|
|
|
!JS_WrapValue(ctx, argv.jsval_addr()))
|
2010-10-10 22:46:16 +00:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
JS_CallFunctionValue(ctx, thisObject,
|
|
|
|
funval, 1, argv.jsval_addr(), &rval);
|
|
|
|
if (aJSONRetVal) {
|
|
|
|
nsString json;
|
2011-03-29 02:27:15 +00:00
|
|
|
if (JS_Stringify(ctx, &rval, nsnull, JSVAL_NULL,
|
2010-10-10 22:46:16 +00:00
|
|
|
JSONCreator, &json)) {
|
|
|
|
aJSONRetVal->AppendElement(json);
|
|
|
|
}
|
2010-05-18 12:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-16 18:21:08 +00:00
|
|
|
nsRefPtr<nsFrameMessageManager> kungfuDeathGrip = mParentManager;
|
2010-05-18 12:28:37 +00:00
|
|
|
return mParentManager ? mParentManager->ReceiveMessage(aTarget, aMessage,
|
|
|
|
aSync, aJSON, aObjectsArray,
|
2010-06-10 09:26:19 +00:00
|
|
|
aJSONRetVal, mContext) : NS_OK;
|
2010-05-18 12:28:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsFrameMessageManager::AddChildManager(nsFrameMessageManager* aManager,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aLoadScripts)
|
2010-05-18 12:28:37 +00:00
|
|
|
{
|
|
|
|
mChildManagers.AppendObject(aManager);
|
|
|
|
if (aLoadScripts) {
|
2010-06-10 09:26:19 +00:00
|
|
|
nsRefPtr<nsFrameMessageManager> kungfuDeathGrip = this;
|
|
|
|
nsRefPtr<nsFrameMessageManager> kungfuDeathGrip2 = aManager;
|
|
|
|
// We have parent manager if we're a window message manager.
|
|
|
|
// In that case we want to load the pending scripts from global
|
|
|
|
// message manager.
|
|
|
|
if (mParentManager) {
|
|
|
|
nsRefPtr<nsFrameMessageManager> globalMM = mParentManager;
|
|
|
|
for (PRUint32 i = 0; i < globalMM->mPendingScripts.Length(); ++i) {
|
2011-10-17 14:59:28 +00:00
|
|
|
aManager->LoadFrameScript(globalMM->mPendingScripts[i], false);
|
2010-06-10 09:26:19 +00:00
|
|
|
}
|
|
|
|
}
|
2010-05-18 12:28:37 +00:00
|
|
|
for (PRUint32 i = 0; i < mPendingScripts.Length(); ++i) {
|
2011-10-17 14:59:28 +00:00
|
|
|
aManager->LoadFrameScript(mPendingScripts[i], false);
|
2010-05-18 12:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-29 06:19:26 +00:00
|
|
|
nsFrameMessageManager::SetCallbackData(void* aData, bool aLoadScripts)
|
2010-05-18 12:28:37 +00:00
|
|
|
{
|
|
|
|
if (aData && mCallbackData != aData) {
|
|
|
|
mCallbackData = aData;
|
|
|
|
// First load global scripts by adding this to parent manager.
|
|
|
|
if (mParentManager) {
|
|
|
|
mParentManager->AddChildManager(this, aLoadScripts);
|
|
|
|
}
|
|
|
|
if (aLoadScripts) {
|
|
|
|
for (PRUint32 i = 0; i < mPendingScripts.Length(); ++i) {
|
2011-10-17 14:59:28 +00:00
|
|
|
LoadFrameScript(mPendingScripts[i], false);
|
2010-05-18 12:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-07 19:20:26 +00:00
|
|
|
void
|
|
|
|
nsFrameMessageManager::RemoveFromParent()
|
|
|
|
{
|
|
|
|
if (mParentManager) {
|
|
|
|
mParentManager->RemoveChildManager(this);
|
|
|
|
}
|
|
|
|
mParentManager = nsnull;
|
|
|
|
mCallbackData = nsnull;
|
|
|
|
mContext = nsnull;
|
|
|
|
}
|
|
|
|
|
2010-05-18 12:28:37 +00:00
|
|
|
void
|
2011-09-29 06:19:26 +00:00
|
|
|
nsFrameMessageManager::Disconnect(bool aRemoveFromParent)
|
2010-05-18 12:28:37 +00:00
|
|
|
{
|
|
|
|
if (mParentManager && aRemoveFromParent) {
|
|
|
|
mParentManager->RemoveChildManager(this);
|
|
|
|
}
|
2012-01-07 19:20:12 +00:00
|
|
|
mDisconnected = true;
|
2010-05-18 12:28:37 +00:00
|
|
|
mParentManager = nsnull;
|
|
|
|
mCallbackData = nsnull;
|
2010-06-10 09:26:19 +00:00
|
|
|
mContext = nsnull;
|
2012-01-07 19:20:12 +00:00
|
|
|
if (!mHandlingMessage) {
|
|
|
|
mListeners.Clear();
|
|
|
|
}
|
2010-06-10 09:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewGlobalMessageManager(nsIChromeFrameMessageManager** aResult)
|
|
|
|
{
|
2010-08-31 18:58:35 +00:00
|
|
|
NS_ENSURE_TRUE(IsChromeProcess(), NS_ERROR_NOT_AVAILABLE);
|
2011-10-17 14:59:28 +00:00
|
|
|
nsFrameMessageManager* mm = new nsFrameMessageManager(true,
|
2010-06-10 09:26:19 +00:00
|
|
|
nsnull,
|
|
|
|
nsnull,
|
|
|
|
nsnull,
|
|
|
|
nsnull,
|
|
|
|
nsnull,
|
|
|
|
nsnull,
|
2011-10-17 14:59:28 +00:00
|
|
|
true);
|
2010-06-10 09:26:19 +00:00
|
|
|
NS_ENSURE_TRUE(mm, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return CallQueryInterface(mm, aResult);
|
2010-05-18 12:28:37 +00:00
|
|
|
}
|
2010-08-10 17:18:26 +00:00
|
|
|
|
2011-03-18 04:08:18 +00:00
|
|
|
void
|
|
|
|
ContentScriptErrorReporter(JSContext* aCx,
|
|
|
|
const char* aMessage,
|
|
|
|
JSErrorReport* aReport)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIScriptError> scriptError =
|
|
|
|
do_CreateInstance(NS_SCRIPTERROR_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsAutoString message, filename, line;
|
|
|
|
PRUint32 lineNumber, columnNumber, flags, errorNumber;
|
|
|
|
|
|
|
|
if (aReport) {
|
|
|
|
if (aReport->ucmessage) {
|
|
|
|
message.Assign(reinterpret_cast<const PRUnichar*>(aReport->ucmessage));
|
|
|
|
}
|
|
|
|
filename.AssignWithConversion(aReport->filename);
|
|
|
|
line.Assign(reinterpret_cast<const PRUnichar*>(aReport->uclinebuf));
|
|
|
|
lineNumber = aReport->lineno;
|
|
|
|
columnNumber = aReport->uctokenptr - aReport->uclinebuf;
|
|
|
|
flags = aReport->flags;
|
|
|
|
errorNumber = aReport->errorNumber;
|
|
|
|
} else {
|
|
|
|
lineNumber = columnNumber = errorNumber = 0;
|
|
|
|
flags = nsIScriptError::errorFlag | nsIScriptError::exceptionFlag;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.IsEmpty()) {
|
|
|
|
message.AssignWithConversion(aMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = scriptError->Init(message.get(), filename.get(), line.get(),
|
|
|
|
lineNumber, columnNumber, flags,
|
|
|
|
"Message manager content script");
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIConsoleService> consoleService =
|
|
|
|
do_GetService(NS_CONSOLESERVICE_CONTRACTID);
|
|
|
|
if (consoleService) {
|
|
|
|
(void) consoleService->LogMessage(scriptError);
|
|
|
|
}
|
2011-07-08 20:15:58 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// Print it to stderr as well, for the benefit of those invoking
|
|
|
|
// mozilla with -console.
|
|
|
|
nsCAutoString error;
|
|
|
|
error.Assign("JavaScript ");
|
|
|
|
if (JSREPORT_IS_STRICT(flags)) {
|
|
|
|
error.Append("strict ");
|
|
|
|
}
|
|
|
|
if (JSREPORT_IS_WARNING(flags)) {
|
|
|
|
error.Append("warning: ");
|
|
|
|
} else {
|
|
|
|
error.Append("error: ");
|
|
|
|
}
|
|
|
|
error.Append(aReport->filename);
|
|
|
|
error.Append(", line ");
|
|
|
|
error.AppendInt(lineNumber, 10);
|
|
|
|
error.Append(": ");
|
|
|
|
if (aReport->ucmessage) {
|
|
|
|
AppendUTF16toUTF8(reinterpret_cast<const PRUnichar*>(aReport->ucmessage),
|
|
|
|
error);
|
|
|
|
} else {
|
|
|
|
error.Append(aMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "%s\n", error.get());
|
|
|
|
fflush(stderr);
|
|
|
|
#endif
|
2011-03-18 04:08:18 +00:00
|
|
|
}
|
|
|
|
|
2011-09-02 06:46:00 +00:00
|
|
|
nsDataHashtable<nsStringHashKey, nsFrameJSScriptExecutorHolder*>*
|
2010-08-10 17:18:26 +00:00
|
|
|
nsFrameScriptExecutor::sCachedScripts = nsnull;
|
2010-08-16 20:05:42 +00:00
|
|
|
nsRefPtr<nsScriptCacheCleaner> nsFrameScriptExecutor::sScriptCacheCleaner;
|
2010-08-10 17:18:26 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
nsFrameScriptExecutor::DidCreateCx()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mCx, "Should have mCx!");
|
|
|
|
if (!sCachedScripts) {
|
|
|
|
sCachedScripts =
|
2011-09-02 06:46:00 +00:00
|
|
|
new nsDataHashtable<nsStringHashKey, nsFrameJSScriptExecutorHolder*>;
|
2010-08-10 17:18:26 +00:00
|
|
|
sCachedScripts->Init();
|
2010-08-16 20:05:42 +00:00
|
|
|
|
|
|
|
sScriptCacheCleaner = new nsScriptCacheCleaner();
|
2010-08-10 17:18:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsFrameScriptExecutor::DestroyCx()
|
|
|
|
{
|
2011-06-16 18:21:08 +00:00
|
|
|
if (mCxStackRefCnt) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mDelayedCxDestroy = true;
|
2011-06-16 18:21:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-10-17 14:59:28 +00:00
|
|
|
mDelayedCxDestroy = false;
|
2011-06-16 18:21:08 +00:00
|
|
|
if (mCx) {
|
|
|
|
nsIXPConnect* xpc = nsContentUtils::XPConnect();
|
|
|
|
if (xpc) {
|
2011-10-17 14:59:28 +00:00
|
|
|
xpc->ReleaseJSContext(mCx, true);
|
2011-06-16 18:21:08 +00:00
|
|
|
} else {
|
|
|
|
JS_DestroyContext(mCx);
|
|
|
|
}
|
2010-08-10 17:18:26 +00:00
|
|
|
}
|
|
|
|
mCx = nsnull;
|
2010-08-23 08:44:43 +00:00
|
|
|
mGlobal = nsnull;
|
2010-08-10 17:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static PLDHashOperator
|
|
|
|
CachedScriptUnrooter(const nsAString& aKey,
|
2011-09-02 06:46:00 +00:00
|
|
|
nsFrameJSScriptExecutorHolder*& aData,
|
2010-08-10 17:18:26 +00:00
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
JSContext* cx = static_cast<JSContext*>(aUserArg);
|
2011-09-02 06:46:00 +00:00
|
|
|
JS_RemoveScriptRoot(cx, &(aData->mScript));
|
2011-01-21 22:51:41 +00:00
|
|
|
delete aData;
|
2010-08-10 17:18:26 +00:00
|
|
|
return PL_DHASH_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
|
|
|
nsFrameScriptExecutor::Shutdown()
|
|
|
|
{
|
|
|
|
if (sCachedScripts) {
|
|
|
|
JSContext* cx = nsnull;
|
|
|
|
nsContentUtils::ThreadJSContextStack()->GetSafeJSContext(&cx);
|
|
|
|
if (cx) {
|
|
|
|
#ifdef DEBUG_smaug
|
|
|
|
printf("Will clear cached frame manager scripts!\n");
|
|
|
|
#endif
|
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
NS_ASSERTION(sCachedScripts != nsnull, "Need cached scripts");
|
|
|
|
sCachedScripts->Enumerate(CachedScriptUnrooter, cx);
|
|
|
|
} else {
|
|
|
|
NS_WARNING("No context available. Leaking cached scripts!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
delete sCachedScripts;
|
|
|
|
sCachedScripts = nsnull;
|
2010-08-16 20:05:42 +00:00
|
|
|
|
|
|
|
sScriptCacheCleaner = nsnull;
|
2010-08-10 17:18:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsFrameScriptExecutor::LoadFrameScriptInternal(const nsAString& aURL)
|
|
|
|
{
|
2011-02-16 17:11:27 +00:00
|
|
|
if (!mGlobal || !mCx || !sCachedScripts) {
|
2010-08-10 17:18:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-02 06:46:00 +00:00
|
|
|
nsFrameJSScriptExecutorHolder* holder = sCachedScripts->Get(aURL);
|
2010-08-10 17:18:26 +00:00
|
|
|
if (holder) {
|
|
|
|
nsContentUtils::ThreadJSContextStack()->Push(mCx);
|
|
|
|
{
|
|
|
|
// Need to scope JSAutoRequest to happen after Push but before Pop,
|
|
|
|
// at least for now. See bug 584673.
|
|
|
|
JSAutoRequest ar(mCx);
|
|
|
|
JSObject* global = nsnull;
|
|
|
|
mGlobal->GetJSObject(&global);
|
|
|
|
if (global) {
|
2011-09-02 06:46:00 +00:00
|
|
|
(void) JS_ExecuteScript(mCx, global, holder->mScript, nsnull);
|
2010-08-10 17:18:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
JSContext* unused;
|
|
|
|
nsContentUtils::ThreadJSContextStack()->Pop(&unused);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString url = NS_ConvertUTF16toUTF8(aURL);
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uri), url);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
2011-05-19 14:48:17 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool hasFlags;
|
2011-05-19 14:48:17 +00:00
|
|
|
rv = NS_URIChainHasFlags(uri,
|
|
|
|
nsIProtocolHandler::URI_IS_LOCAL_RESOURCE,
|
|
|
|
&hasFlags);
|
|
|
|
if (NS_FAILED(rv) || !hasFlags) {
|
|
|
|
NS_WARNING("Will not load a frame script!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-10 17:18:26 +00:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
NS_NewChannel(getter_AddRefs(channel), uri);
|
|
|
|
if (!channel) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> input;
|
|
|
|
channel->Open(getter_AddRefs(input));
|
|
|
|
nsString dataString;
|
2011-04-07 00:17:56 +00:00
|
|
|
PRUint32 avail = 0;
|
|
|
|
if (input && NS_SUCCEEDED(input->Available(&avail)) && avail) {
|
|
|
|
nsCString buffer;
|
|
|
|
if (NS_FAILED(NS_ReadInputStreamToString(input, buffer, avail))) {
|
|
|
|
return;
|
2010-08-10 17:18:26 +00:00
|
|
|
}
|
2011-04-07 00:17:56 +00:00
|
|
|
nsScriptLoader::ConvertToUTF16(channel, (PRUint8*)buffer.get(), avail,
|
2010-08-10 17:18:26 +00:00
|
|
|
EmptyString(), nsnull, dataString);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dataString.IsEmpty()) {
|
|
|
|
nsContentUtils::ThreadJSContextStack()->Push(mCx);
|
|
|
|
{
|
|
|
|
// Need to scope JSAutoRequest to happen after Push but before Pop,
|
|
|
|
// at least for now. See bug 584673.
|
|
|
|
JSAutoRequest ar(mCx);
|
|
|
|
JSObject* global = nsnull;
|
|
|
|
mGlobal->GetJSObject(&global);
|
|
|
|
if (global) {
|
|
|
|
JSPrincipals* jsprin = nsnull;
|
|
|
|
mPrincipal->GetJSPrincipals(mCx, &jsprin);
|
2011-03-25 21:31:14 +00:00
|
|
|
|
|
|
|
uint32 oldopts = JS_GetOptions(mCx);
|
|
|
|
JS_SetOptions(mCx, oldopts | JSOPTION_NO_SCRIPT_RVAL);
|
|
|
|
|
2011-09-02 06:46:00 +00:00
|
|
|
JSScript* script =
|
2010-08-10 17:18:26 +00:00
|
|
|
JS_CompileUCScriptForPrincipals(mCx, nsnull, jsprin,
|
|
|
|
(jschar*)dataString.get(),
|
|
|
|
dataString.Length(),
|
|
|
|
url.get(), 1);
|
|
|
|
|
2011-03-25 21:31:14 +00:00
|
|
|
JS_SetOptions(mCx, oldopts);
|
|
|
|
|
2011-09-02 06:46:00 +00:00
|
|
|
if (script) {
|
2010-08-10 17:18:26 +00:00
|
|
|
nsCAutoString scheme;
|
|
|
|
uri->GetScheme(scheme);
|
|
|
|
// We don't cache data: scripts!
|
|
|
|
if (!scheme.EqualsLiteral("data")) {
|
2011-09-02 06:46:00 +00:00
|
|
|
nsFrameJSScriptExecutorHolder* holder =
|
|
|
|
new nsFrameJSScriptExecutorHolder(script);
|
2010-08-10 17:18:26 +00:00
|
|
|
// Root the object also for caching.
|
2011-09-02 06:46:00 +00:00
|
|
|
JS_AddNamedScriptRoot(mCx, &(holder->mScript),
|
2010-08-10 17:18:26 +00:00
|
|
|
"Cached message manager script");
|
|
|
|
sCachedScripts->Put(aURL, holder);
|
|
|
|
}
|
2011-09-02 06:46:00 +00:00
|
|
|
(void) JS_ExecuteScript(mCx, global, script, nsnull);
|
2010-08-10 17:18:26 +00:00
|
|
|
}
|
|
|
|
//XXX Argh, JSPrincipals are manually refcounted!
|
|
|
|
JSPRINCIPALS_DROP(mCx, jsprin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
JSContext* unused;
|
|
|
|
nsContentUtils::ThreadJSContextStack()->Pop(&unused);
|
|
|
|
}
|
|
|
|
}
|
2010-08-16 20:05:42 +00:00
|
|
|
|
2011-11-30 15:51:40 +00:00
|
|
|
bool
|
|
|
|
nsFrameScriptExecutor::InitTabChildGlobalInternal(nsISupports* aScope)
|
|
|
|
{
|
|
|
|
|
|
|
|
nsCOMPtr<nsIJSRuntimeService> runtimeSvc =
|
|
|
|
do_GetService("@mozilla.org/js/xpc/RuntimeService;1");
|
|
|
|
NS_ENSURE_TRUE(runtimeSvc, false);
|
|
|
|
|
|
|
|
JSRuntime* rt = nsnull;
|
|
|
|
runtimeSvc->GetRuntime(&rt);
|
|
|
|
NS_ENSURE_TRUE(rt, false);
|
|
|
|
|
|
|
|
JSContext* cx = JS_NewContext(rt, 8192);
|
|
|
|
NS_ENSURE_TRUE(cx, false);
|
|
|
|
|
|
|
|
mCx = cx;
|
|
|
|
|
|
|
|
nsContentUtils::GetSecurityManager()->GetSystemPrincipal(getter_AddRefs(mPrincipal));
|
|
|
|
|
|
|
|
JS_SetOptions(cx, JS_GetOptions(cx) | JSOPTION_PRIVATE_IS_NSISUPPORTS);
|
|
|
|
JS_SetVersion(cx, JSVERSION_LATEST);
|
|
|
|
JS_SetErrorReporter(cx, ContentScriptErrorReporter);
|
|
|
|
|
|
|
|
xpc_LocalizeContext(cx);
|
|
|
|
|
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
nsIXPConnect* xpc = nsContentUtils::XPConnect();
|
|
|
|
const PRUint32 flags = nsIXPConnect::INIT_JS_STANDARD_CLASSES |
|
|
|
|
nsIXPConnect::FLAG_SYSTEM_GLOBAL_OBJECT;
|
|
|
|
|
|
|
|
|
|
|
|
JS_SetContextPrivate(cx, aScope);
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
xpc->InitClassesWithNewWrappedGlobal(cx, aScope,
|
|
|
|
NS_GET_IID(nsISupports),
|
|
|
|
mPrincipal, nsnull,
|
|
|
|
flags, getter_AddRefs(mGlobal));
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
|
|
|
|
JSObject* global = nsnull;
|
|
|
|
rv = mGlobal->GetJSObject(&global);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
JS_SetGlobalObject(cx, global);
|
|
|
|
DidCreateCx();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-02-02 22:08:13 +00:00
|
|
|
// static
|
|
|
|
void
|
|
|
|
nsFrameScriptExecutor::Traverse(nsFrameScriptExecutor *tmp,
|
|
|
|
nsCycleCollectionTraversalCallback &cb)
|
|
|
|
{
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mGlobal)
|
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mCx");
|
|
|
|
nsContentUtils::XPConnect()->NoteJSContext(tmp->mCx, cb);
|
|
|
|
}
|
|
|
|
|
2010-08-24 04:50:40 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(nsScriptCacheCleaner, nsIObserver)
|
2010-08-16 20:05:42 +00:00
|
|
|
|
2010-08-31 18:58:35 +00:00
|
|
|
nsFrameMessageManager* nsFrameMessageManager::sChildProcessManager = nsnull;
|
|
|
|
nsFrameMessageManager* nsFrameMessageManager::sParentProcessManager = nsnull;
|
2011-10-11 10:28:46 +00:00
|
|
|
nsFrameMessageManager* nsFrameMessageManager::sSameProcessParentManager = nsnull;
|
|
|
|
nsTArray<nsCOMPtr<nsIRunnable> >* nsFrameMessageManager::sPendingSameProcessAsyncMessages = nsnull;
|
2010-08-31 18:58:35 +00:00
|
|
|
|
|
|
|
bool SendAsyncMessageToChildProcess(void* aCallbackData,
|
|
|
|
const nsAString& aMessage,
|
|
|
|
const nsAString& aJSON)
|
|
|
|
{
|
|
|
|
mozilla::dom::ContentParent* cp =
|
2011-08-02 19:57:48 +00:00
|
|
|
static_cast<mozilla::dom::ContentParent*>(aCallbackData);
|
2010-08-31 18:58:35 +00:00
|
|
|
NS_WARN_IF_FALSE(cp, "No child process!");
|
|
|
|
if (cp) {
|
|
|
|
return cp->SendAsyncMessage(nsString(aMessage), nsString(aJSON));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-10-11 10:28:46 +00:00
|
|
|
class nsAsyncMessageToSameProcessChild : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsAsyncMessageToSameProcessChild(const nsAString& aMessage, const nsAString& aJSON)
|
|
|
|
: mMessage(aMessage), mJSON(aJSON) {}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
if (nsFrameMessageManager::sChildProcessManager) {
|
|
|
|
nsRefPtr<nsFrameMessageManager> ppm = nsFrameMessageManager::sChildProcessManager;
|
|
|
|
ppm->ReceiveMessage(static_cast<nsIContentFrameMessageManager*>(ppm.get()), mMessage,
|
2011-10-17 14:59:28 +00:00
|
|
|
false, mJSON, nsnull, nsnull);
|
2011-10-11 10:28:46 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsString mMessage;
|
|
|
|
nsString mJSON;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool SendAsyncMessageToSameProcessChild(void* aCallbackData,
|
|
|
|
const nsAString& aMessage,
|
|
|
|
const nsAString& aJSON)
|
|
|
|
{
|
|
|
|
nsRefPtr<nsIRunnable> ev =
|
|
|
|
new nsAsyncMessageToSameProcessChild(aMessage, aJSON);
|
|
|
|
NS_DispatchToCurrentThread(ev);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-31 18:58:35 +00:00
|
|
|
bool SendSyncMessageToParentProcess(void* aCallbackData,
|
|
|
|
const nsAString& aMessage,
|
|
|
|
const nsAString& aJSON,
|
2010-11-09 02:49:00 +00:00
|
|
|
InfallibleTArray<nsString>* aJSONRetVal)
|
2010-08-31 18:58:35 +00:00
|
|
|
{
|
|
|
|
mozilla::dom::ContentChild* cc =
|
|
|
|
mozilla::dom::ContentChild::GetSingleton();
|
|
|
|
if (cc) {
|
|
|
|
return
|
|
|
|
cc->SendSyncMessage(nsString(aMessage), nsString(aJSON), aJSONRetVal);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-10-11 10:28:46 +00:00
|
|
|
bool SendSyncMessageToSameProcessParent(void* aCallbackData,
|
|
|
|
const nsAString& aMessage,
|
|
|
|
const nsAString& aJSON,
|
|
|
|
InfallibleTArray<nsString>* aJSONRetVal)
|
|
|
|
{
|
|
|
|
nsTArray<nsCOMPtr<nsIRunnable> > asyncMessages;
|
|
|
|
if (nsFrameMessageManager::sPendingSameProcessAsyncMessages) {
|
|
|
|
asyncMessages.SwapElements(*nsFrameMessageManager::sPendingSameProcessAsyncMessages);
|
|
|
|
PRUint32 len = asyncMessages.Length();
|
|
|
|
for (PRUint32 i = 0; i < len; ++i) {
|
|
|
|
nsCOMPtr<nsIRunnable> async = asyncMessages[i];
|
|
|
|
async->Run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nsFrameMessageManager::sSameProcessParentManager) {
|
|
|
|
nsRefPtr<nsFrameMessageManager> ppm = nsFrameMessageManager::sSameProcessParentManager;
|
|
|
|
ppm->ReceiveMessage(static_cast<nsIContentFrameMessageManager*>(ppm.get()), aMessage,
|
2011-10-17 14:59:28 +00:00
|
|
|
true, aJSON, nsnull, aJSONRetVal);
|
2011-10-11 10:28:46 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-31 18:58:35 +00:00
|
|
|
bool SendAsyncMessageToParentProcess(void* aCallbackData,
|
|
|
|
const nsAString& aMessage,
|
|
|
|
const nsAString& aJSON)
|
|
|
|
{
|
|
|
|
mozilla::dom::ContentChild* cc =
|
|
|
|
mozilla::dom::ContentChild::GetSingleton();
|
|
|
|
if (cc) {
|
|
|
|
return cc->SendAsyncMessage(nsString(aMessage), nsString(aJSON));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-10-11 10:28:46 +00:00
|
|
|
class nsAsyncMessageToSameProcessParent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsAsyncMessageToSameProcessParent(const nsAString& aMessage, const nsAString& aJSON)
|
|
|
|
: mMessage(aMessage), mJSON(aJSON) {}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
if (nsFrameMessageManager::sPendingSameProcessAsyncMessages) {
|
|
|
|
nsFrameMessageManager::sPendingSameProcessAsyncMessages->RemoveElement(this);
|
|
|
|
}
|
|
|
|
if (nsFrameMessageManager::sSameProcessParentManager) {
|
|
|
|
nsRefPtr<nsFrameMessageManager> ppm = nsFrameMessageManager::sSameProcessParentManager;
|
2011-10-17 14:59:28 +00:00
|
|
|
ppm->ReceiveMessage(static_cast<nsIContentFrameMessageManager*>(ppm.get()), mMessage, false,
|
2011-10-11 10:28:46 +00:00
|
|
|
mJSON, nsnull, nsnull);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsString mMessage;
|
|
|
|
nsString mJSON;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool SendAsyncMessageToSameProcessParent(void* aCallbackData,
|
|
|
|
const nsAString& aMessage,
|
|
|
|
const nsAString& aJSON)
|
|
|
|
{
|
|
|
|
if (!nsFrameMessageManager::sPendingSameProcessAsyncMessages) {
|
|
|
|
nsFrameMessageManager::sPendingSameProcessAsyncMessages = new nsTArray<nsCOMPtr<nsIRunnable> >;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIRunnable> ev =
|
|
|
|
new nsAsyncMessageToSameProcessParent(aMessage, aJSON);
|
|
|
|
nsFrameMessageManager::sPendingSameProcessAsyncMessages->AppendElement(ev);
|
|
|
|
NS_DispatchToCurrentThread(ev);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-08-02 19:57:48 +00:00
|
|
|
// This creates the global parent process message manager.
|
2010-08-31 18:58:35 +00:00
|
|
|
nsresult
|
|
|
|
NS_NewParentProcessMessageManager(nsIFrameMessageManager** aResult)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!nsFrameMessageManager::sParentProcessManager,
|
|
|
|
"Re-creating sParentProcessManager");
|
|
|
|
NS_ENSURE_TRUE(IsChromeProcess(), NS_ERROR_NOT_AVAILABLE);
|
2011-10-17 14:59:28 +00:00
|
|
|
nsRefPtr<nsFrameMessageManager> mm = new nsFrameMessageManager(true,
|
2011-10-11 10:28:46 +00:00
|
|
|
nsnull,
|
|
|
|
nsnull,
|
|
|
|
nsnull,
|
|
|
|
nsnull,
|
|
|
|
nsnull,
|
|
|
|
nsnull,
|
2011-10-17 14:59:28 +00:00
|
|
|
false,
|
|
|
|
true);
|
2010-08-31 18:58:35 +00:00
|
|
|
NS_ENSURE_TRUE(mm, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
nsFrameMessageManager::sParentProcessManager = mm;
|
2011-10-11 10:28:46 +00:00
|
|
|
nsFrameMessageManager::NewProcessMessageManager(nsnull); // Create same process message manager.
|
2010-08-31 18:58:35 +00:00
|
|
|
return CallQueryInterface(mm, aResult);
|
|
|
|
}
|
|
|
|
|
2011-08-02 19:57:48 +00:00
|
|
|
nsFrameMessageManager*
|
|
|
|
nsFrameMessageManager::NewProcessMessageManager(mozilla::dom::ContentParent* aProcess)
|
|
|
|
{
|
|
|
|
if (!nsFrameMessageManager::sParentProcessManager) {
|
|
|
|
nsCOMPtr<nsIFrameMessageManager> dummy;
|
|
|
|
NS_NewParentProcessMessageManager(getter_AddRefs(dummy));
|
|
|
|
}
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
nsFrameMessageManager* mm = new nsFrameMessageManager(true,
|
2011-08-02 19:57:48 +00:00
|
|
|
nsnull,
|
2011-10-11 10:28:46 +00:00
|
|
|
aProcess ? SendAsyncMessageToChildProcess
|
|
|
|
: SendAsyncMessageToSameProcessChild,
|
2011-08-02 19:57:48 +00:00
|
|
|
nsnull,
|
2011-10-11 10:28:46 +00:00
|
|
|
aProcess ? static_cast<void*>(aProcess)
|
|
|
|
: static_cast<void*>(&nsFrameMessageManager::sChildProcessManager),
|
2011-08-02 19:57:48 +00:00
|
|
|
nsFrameMessageManager::sParentProcessManager,
|
|
|
|
nsnull,
|
2011-10-17 14:59:28 +00:00
|
|
|
false,
|
|
|
|
true);
|
2011-10-11 10:28:46 +00:00
|
|
|
if (!aProcess) {
|
|
|
|
sSameProcessParentManager = mm;
|
|
|
|
}
|
2011-08-02 19:57:48 +00:00
|
|
|
return mm;
|
|
|
|
}
|
2010-08-31 18:58:35 +00:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewChildProcessMessageManager(nsISyncMessageSender** aResult)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!nsFrameMessageManager::sChildProcessManager,
|
|
|
|
"Re-creating sChildProcessManager");
|
2012-02-14 16:17:19 +00:00
|
|
|
bool isChrome = IsChromeProcess();
|
2011-10-17 14:59:28 +00:00
|
|
|
nsFrameMessageManager* mm = new nsFrameMessageManager(false,
|
2011-10-11 10:28:46 +00:00
|
|
|
isChrome ? SendSyncMessageToSameProcessParent
|
|
|
|
: SendSyncMessageToParentProcess,
|
|
|
|
isChrome ? SendAsyncMessageToSameProcessParent
|
|
|
|
: SendAsyncMessageToParentProcess,
|
2010-08-31 18:58:35 +00:00
|
|
|
nsnull,
|
|
|
|
&nsFrameMessageManager::sChildProcessManager,
|
|
|
|
nsnull,
|
|
|
|
nsnull,
|
2011-10-17 14:59:28 +00:00
|
|
|
false,
|
|
|
|
true);
|
2010-08-31 18:58:35 +00:00
|
|
|
NS_ENSURE_TRUE(mm, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
nsFrameMessageManager::sChildProcessManager = mm;
|
|
|
|
return CallQueryInterface(mm, aResult);
|
|
|
|
}
|
2012-01-26 15:39:23 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
nsFrameMessageManager::MarkForCC()
|
|
|
|
{
|
|
|
|
PRUint32 len = mListeners.Length();
|
|
|
|
for (PRUint32 i = 0; i < len; ++i) {
|
|
|
|
nsCOMPtr<nsIXPConnectWrappedJS> wjs =
|
|
|
|
do_QueryInterface(mListeners[i].mListener);
|
|
|
|
xpc_UnmarkGrayObject(wjs);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|