/* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 8; -*- */ /* vim: set sw=4 ts=8 et tw=80 : */ /* 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/. */ #include "nsInProcessTabChildGlobal.h" #include "nsContentUtils.h" #include "nsIScriptSecurityManager.h" #include "nsIInterfaceRequestorUtils.h" #include "nsIComponentManager.h" #include "nsIServiceManager.h" #include "nsIJSRuntimeService.h" #include "nsComponentManagerUtils.h" #include "nsNetUtil.h" #include "nsScriptLoader.h" #include "nsFrameLoader.h" #include "xpcpublic.h" #include "nsIMozBrowserFrame.h" #include "nsDOMClassInfoID.h" #include "mozilla/EventDispatcher.h" #include "mozilla/dom/StructuredCloneUtils.h" #include "js/StructuredClone.h" using mozilla::dom::StructuredCloneData; using mozilla::dom::StructuredCloneClosure; using namespace mozilla; bool nsInProcessTabChildGlobal::DoSendBlockingMessage(JSContext* aCx, const nsAString& aMessage, const dom::StructuredCloneData& aData, JS::Handle aCpows, nsIPrincipal* aPrincipal, InfallibleTArray* aJSONRetVal, bool aIsSync) { nsTArray > asyncMessages; asyncMessages.SwapElements(mASyncMessages); uint32_t len = asyncMessages.Length(); for (uint32_t i = 0; i < len; ++i) { nsCOMPtr async = asyncMessages[i]; async->Run(); } if (mChromeMessageManager) { SameProcessCpowHolder cpows(js::GetRuntime(aCx), aCpows); nsRefPtr mm = mChromeMessageManager; mm->ReceiveMessage(mOwner, aMessage, true, &aData, &cpows, aPrincipal, aJSONRetVal); } return true; } class nsAsyncMessageToParent : public nsSameProcessAsyncMessageBase, public nsRunnable { public: nsAsyncMessageToParent(JSContext* aCx, nsInProcessTabChildGlobal* aTabChild, const nsAString& aMessage, const StructuredCloneData& aData, JS::Handle aCpows, nsIPrincipal* aPrincipal) : nsSameProcessAsyncMessageBase(aCx, aMessage, aData, aCpows, aPrincipal), mTabChild(aTabChild), mRun(false) { } NS_IMETHOD Run() { if (mRun) { return NS_OK; } mRun = true; mTabChild->mASyncMessages.RemoveElement(this); ReceiveMessage(mTabChild->mOwner, mTabChild->mChromeMessageManager); return NS_OK; } nsRefPtr mTabChild; // True if this runnable has already been called. This can happen if DoSendSyncMessage // is called while waiting for an asynchronous message send. bool mRun; }; bool nsInProcessTabChildGlobal::DoSendAsyncMessage(JSContext* aCx, const nsAString& aMessage, const StructuredCloneData& aData, JS::Handle aCpows, nsIPrincipal* aPrincipal) { nsCOMPtr ev = new nsAsyncMessageToParent(aCx, this, aMessage, aData, aCpows, aPrincipal); mASyncMessages.AppendElement(ev); NS_DispatchToCurrentThread(ev); return true; } nsInProcessTabChildGlobal::nsInProcessTabChildGlobal(nsIDocShell* aShell, nsIContent* aOwner, nsFrameMessageManager* aChrome) : mDocShell(aShell), mInitialized(false), mLoadingScript(false), mOwner(aOwner), mChromeMessageManager(aChrome) { SetIsNotDOMBinding(); mozilla::HoldJSObjects(this); // If owner corresponds to an