1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/src/nsInProcessTabChildGlobal.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,327 @@ 1.4 +/* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 8; -*- */ 1.5 +/* vim: set sw=4 ts=8 et tw=80 : */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "nsInProcessTabChildGlobal.h" 1.11 +#include "nsContentUtils.h" 1.12 +#include "nsIScriptSecurityManager.h" 1.13 +#include "nsIInterfaceRequestorUtils.h" 1.14 +#include "nsIComponentManager.h" 1.15 +#include "nsIServiceManager.h" 1.16 +#include "nsIJSRuntimeService.h" 1.17 +#include "nsComponentManagerUtils.h" 1.18 +#include "nsNetUtil.h" 1.19 +#include "nsScriptLoader.h" 1.20 +#include "nsFrameLoader.h" 1.21 +#include "xpcpublic.h" 1.22 +#include "nsIMozBrowserFrame.h" 1.23 +#include "nsDOMClassInfoID.h" 1.24 +#include "mozilla/EventDispatcher.h" 1.25 +#include "mozilla/dom/StructuredCloneUtils.h" 1.26 +#include "js/StructuredClone.h" 1.27 + 1.28 +using mozilla::dom::StructuredCloneData; 1.29 +using mozilla::dom::StructuredCloneClosure; 1.30 +using namespace mozilla; 1.31 + 1.32 +bool 1.33 +nsInProcessTabChildGlobal::DoSendBlockingMessage(JSContext* aCx, 1.34 + const nsAString& aMessage, 1.35 + const dom::StructuredCloneData& aData, 1.36 + JS::Handle<JSObject *> aCpows, 1.37 + nsIPrincipal* aPrincipal, 1.38 + InfallibleTArray<nsString>* aJSONRetVal, 1.39 + bool aIsSync) 1.40 +{ 1.41 + nsTArray<nsCOMPtr<nsIRunnable> > asyncMessages; 1.42 + asyncMessages.SwapElements(mASyncMessages); 1.43 + uint32_t len = asyncMessages.Length(); 1.44 + for (uint32_t i = 0; i < len; ++i) { 1.45 + nsCOMPtr<nsIRunnable> async = asyncMessages[i]; 1.46 + async->Run(); 1.47 + } 1.48 + if (mChromeMessageManager) { 1.49 + SameProcessCpowHolder cpows(js::GetRuntime(aCx), aCpows); 1.50 + nsRefPtr<nsFrameMessageManager> mm = mChromeMessageManager; 1.51 + mm->ReceiveMessage(mOwner, aMessage, true, &aData, &cpows, aPrincipal, 1.52 + aJSONRetVal); 1.53 + } 1.54 + return true; 1.55 +} 1.56 + 1.57 +class nsAsyncMessageToParent : public nsSameProcessAsyncMessageBase, 1.58 + public nsRunnable 1.59 +{ 1.60 +public: 1.61 + nsAsyncMessageToParent(JSContext* aCx, 1.62 + nsInProcessTabChildGlobal* aTabChild, 1.63 + const nsAString& aMessage, 1.64 + const StructuredCloneData& aData, 1.65 + JS::Handle<JSObject *> aCpows, 1.66 + nsIPrincipal* aPrincipal) 1.67 + : nsSameProcessAsyncMessageBase(aCx, aMessage, aData, aCpows, aPrincipal), 1.68 + mTabChild(aTabChild), mRun(false) 1.69 + { 1.70 + } 1.71 + 1.72 + NS_IMETHOD Run() 1.73 + { 1.74 + if (mRun) { 1.75 + return NS_OK; 1.76 + } 1.77 + 1.78 + mRun = true; 1.79 + mTabChild->mASyncMessages.RemoveElement(this); 1.80 + ReceiveMessage(mTabChild->mOwner, mTabChild->mChromeMessageManager); 1.81 + return NS_OK; 1.82 + } 1.83 + nsRefPtr<nsInProcessTabChildGlobal> mTabChild; 1.84 + // True if this runnable has already been called. This can happen if DoSendSyncMessage 1.85 + // is called while waiting for an asynchronous message send. 1.86 + bool mRun; 1.87 +}; 1.88 + 1.89 +bool 1.90 +nsInProcessTabChildGlobal::DoSendAsyncMessage(JSContext* aCx, 1.91 + const nsAString& aMessage, 1.92 + const StructuredCloneData& aData, 1.93 + JS::Handle<JSObject *> aCpows, 1.94 + nsIPrincipal* aPrincipal) 1.95 +{ 1.96 + nsCOMPtr<nsIRunnable> ev = 1.97 + new nsAsyncMessageToParent(aCx, this, aMessage, aData, aCpows, aPrincipal); 1.98 + mASyncMessages.AppendElement(ev); 1.99 + NS_DispatchToCurrentThread(ev); 1.100 + return true; 1.101 +} 1.102 + 1.103 +nsInProcessTabChildGlobal::nsInProcessTabChildGlobal(nsIDocShell* aShell, 1.104 + nsIContent* aOwner, 1.105 + nsFrameMessageManager* aChrome) 1.106 +: mDocShell(aShell), mInitialized(false), mLoadingScript(false), 1.107 + mOwner(aOwner), mChromeMessageManager(aChrome) 1.108 +{ 1.109 + 1.110 + // If owner corresponds to an <iframe mozbrowser> or <iframe mozapp>, we'll 1.111 + // have to tweak our PreHandleEvent implementation. 1.112 + nsCOMPtr<nsIMozBrowserFrame> browserFrame = do_QueryInterface(mOwner); 1.113 + if (browserFrame) { 1.114 + mIsBrowserOrAppFrame = browserFrame->GetReallyIsBrowserOrApp(); 1.115 + } 1.116 + else { 1.117 + mIsBrowserOrAppFrame = false; 1.118 + } 1.119 +} 1.120 + 1.121 +nsInProcessTabChildGlobal::~nsInProcessTabChildGlobal() 1.122 +{ 1.123 +} 1.124 + 1.125 +/* [notxpcom] boolean markForCC (); */ 1.126 +// This method isn't automatically forwarded safely because it's notxpcom, so 1.127 +// the IDL binding doesn't know what value to return. 1.128 +NS_IMETHODIMP_(bool) 1.129 +nsInProcessTabChildGlobal::MarkForCC() 1.130 +{ 1.131 + return mMessageManager ? mMessageManager->MarkForCC() : false; 1.132 +} 1.133 + 1.134 +nsresult 1.135 +nsInProcessTabChildGlobal::Init() 1.136 +{ 1.137 +#ifdef DEBUG 1.138 + nsresult rv = 1.139 +#endif 1.140 + InitTabChildGlobal(); 1.141 + NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), 1.142 + "Couldn't initialize nsInProcessTabChildGlobal"); 1.143 + mMessageManager = new nsFrameMessageManager(this, 1.144 + nullptr, 1.145 + dom::ipc::MM_CHILD); 1.146 + return NS_OK; 1.147 +} 1.148 + 1.149 +NS_IMPL_CYCLE_COLLECTION_INHERITED(nsInProcessTabChildGlobal, 1.150 + DOMEventTargetHelper, 1.151 + mMessageManager, 1.152 + mGlobal) 1.153 + 1.154 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsInProcessTabChildGlobal) 1.155 + NS_INTERFACE_MAP_ENTRY(nsIMessageListenerManager) 1.156 + NS_INTERFACE_MAP_ENTRY(nsIMessageSender) 1.157 + NS_INTERFACE_MAP_ENTRY(nsISyncMessageSender) 1.158 + NS_INTERFACE_MAP_ENTRY(nsIContentFrameMessageManager) 1.159 + NS_INTERFACE_MAP_ENTRY(nsIInProcessContentFrameMessageManager) 1.160 + NS_INTERFACE_MAP_ENTRY(nsIScriptObjectPrincipal) 1.161 + NS_INTERFACE_MAP_ENTRY(nsIGlobalObject) 1.162 + NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference) 1.163 + NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(ContentFrameMessageManager) 1.164 +NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper) 1.165 + 1.166 +NS_IMPL_ADDREF_INHERITED(nsInProcessTabChildGlobal, DOMEventTargetHelper) 1.167 +NS_IMPL_RELEASE_INHERITED(nsInProcessTabChildGlobal, DOMEventTargetHelper) 1.168 + 1.169 +NS_IMETHODIMP 1.170 +nsInProcessTabChildGlobal::GetContent(nsIDOMWindow** aContent) 1.171 +{ 1.172 + *aContent = nullptr; 1.173 + nsCOMPtr<nsIDOMWindow> window = do_GetInterface(mDocShell); 1.174 + window.swap(*aContent); 1.175 + return NS_OK; 1.176 +} 1.177 + 1.178 +NS_IMETHODIMP 1.179 +nsInProcessTabChildGlobal::GetDocShell(nsIDocShell** aDocShell) 1.180 +{ 1.181 + NS_IF_ADDREF(*aDocShell = mDocShell); 1.182 + return NS_OK; 1.183 +} 1.184 + 1.185 +NS_IMETHODIMP 1.186 +nsInProcessTabChildGlobal::Btoa(const nsAString& aBinaryData, 1.187 + nsAString& aAsciiBase64String) 1.188 +{ 1.189 + return nsContentUtils::Btoa(aBinaryData, aAsciiBase64String); 1.190 +} 1.191 + 1.192 +NS_IMETHODIMP 1.193 +nsInProcessTabChildGlobal::Atob(const nsAString& aAsciiString, 1.194 + nsAString& aBinaryData) 1.195 +{ 1.196 + return nsContentUtils::Atob(aAsciiString, aBinaryData); 1.197 +} 1.198 + 1.199 + 1.200 +NS_IMETHODIMP 1.201 +nsInProcessTabChildGlobal::PrivateNoteIntentionalCrash() 1.202 +{ 1.203 + return NS_ERROR_NOT_IMPLEMENTED; 1.204 +} 1.205 + 1.206 +void 1.207 +nsInProcessTabChildGlobal::Disconnect() 1.208 +{ 1.209 + // Let the frame scripts know the child is being closed. We do any other 1.210 + // cleanup after the event has been fired. See DelayedDisconnect 1.211 + nsContentUtils::AddScriptRunner( 1.212 + NS_NewRunnableMethod(this, &nsInProcessTabChildGlobal::DelayedDisconnect) 1.213 + ); 1.214 +} 1.215 + 1.216 +void 1.217 +nsInProcessTabChildGlobal::DelayedDisconnect() 1.218 +{ 1.219 + // Don't let the event escape 1.220 + mOwner = nullptr; 1.221 + 1.222 + // Fire the "unload" event 1.223 + DOMEventTargetHelper::DispatchTrustedEvent(NS_LITERAL_STRING("unload")); 1.224 + 1.225 + // Continue with the Disconnect cleanup 1.226 + nsCOMPtr<nsPIDOMWindow> win = do_GetInterface(mDocShell); 1.227 + if (win) { 1.228 + MOZ_ASSERT(win->IsOuterWindow()); 1.229 + win->SetChromeEventHandler(win->GetChromeEventHandler()); 1.230 + } 1.231 + mDocShell = nullptr; 1.232 + mChromeMessageManager = nullptr; 1.233 + if (mMessageManager) { 1.234 + static_cast<nsFrameMessageManager*>(mMessageManager.get())->Disconnect(); 1.235 + mMessageManager = nullptr; 1.236 + } 1.237 + if (mListenerManager) { 1.238 + mListenerManager->Disconnect(); 1.239 + } 1.240 +} 1.241 + 1.242 +NS_IMETHODIMP_(nsIContent *) 1.243 +nsInProcessTabChildGlobal::GetOwnerContent() 1.244 +{ 1.245 + return mOwner; 1.246 +} 1.247 + 1.248 +nsresult 1.249 +nsInProcessTabChildGlobal::PreHandleEvent(EventChainPreVisitor& aVisitor) 1.250 +{ 1.251 + aVisitor.mCanHandle = true; 1.252 + 1.253 + if (mIsBrowserOrAppFrame && 1.254 + (!mOwner || !nsContentUtils::IsInChromeDocshell(mOwner->OwnerDoc()))) { 1.255 + if (mOwner) { 1.256 + nsPIDOMWindow* innerWindow = mOwner->OwnerDoc()->GetInnerWindow(); 1.257 + if (innerWindow) { 1.258 + aVisitor.mParentTarget = innerWindow->GetParentTarget(); 1.259 + } 1.260 + } 1.261 + } else { 1.262 + aVisitor.mParentTarget = mOwner; 1.263 + } 1.264 + 1.265 +#ifdef DEBUG 1.266 + if (mOwner) { 1.267 + nsCOMPtr<nsIFrameLoaderOwner> owner = do_QueryInterface(mOwner); 1.268 + nsRefPtr<nsFrameLoader> fl = owner->GetFrameLoader(); 1.269 + if (fl) { 1.270 + NS_ASSERTION(this == fl->GetTabChildGlobalAsEventTarget(), 1.271 + "Wrong event target!"); 1.272 + NS_ASSERTION(fl->mMessageManager == mChromeMessageManager, 1.273 + "Wrong message manager!"); 1.274 + } 1.275 + } 1.276 +#endif 1.277 + 1.278 + return NS_OK; 1.279 +} 1.280 + 1.281 +nsresult 1.282 +nsInProcessTabChildGlobal::InitTabChildGlobal() 1.283 +{ 1.284 + nsAutoCString id; 1.285 + id.AssignLiteral("inProcessTabChildGlobal"); 1.286 + nsIURI* uri = mOwner->OwnerDoc()->GetDocumentURI(); 1.287 + if (uri) { 1.288 + nsAutoCString u; 1.289 + uri->GetSpec(u); 1.290 + id.AppendLiteral("?ownedBy="); 1.291 + id.Append(u); 1.292 + } 1.293 + nsISupports* scopeSupports = NS_ISUPPORTS_CAST(EventTarget*, this); 1.294 + NS_ENSURE_STATE(InitTabChildGlobalInternal(scopeSupports, id)); 1.295 + return NS_OK; 1.296 +} 1.297 + 1.298 +class nsAsyncScriptLoad : public nsRunnable 1.299 +{ 1.300 +public: 1.301 + nsAsyncScriptLoad(nsInProcessTabChildGlobal* aTabChild, const nsAString& aURL, 1.302 + bool aRunInGlobalScope) 1.303 + : mTabChild(aTabChild), mURL(aURL), mRunInGlobalScope(aRunInGlobalScope) {} 1.304 + 1.305 + NS_IMETHOD Run() 1.306 + { 1.307 + mTabChild->LoadFrameScript(mURL, mRunInGlobalScope); 1.308 + return NS_OK; 1.309 + } 1.310 + nsRefPtr<nsInProcessTabChildGlobal> mTabChild; 1.311 + nsString mURL; 1.312 + bool mRunInGlobalScope; 1.313 +}; 1.314 + 1.315 +void 1.316 +nsInProcessTabChildGlobal::LoadFrameScript(const nsAString& aURL, bool aRunInGlobalScope) 1.317 +{ 1.318 + if (!nsContentUtils::IsSafeToRunScript()) { 1.319 + nsContentUtils::AddScriptRunner(new nsAsyncScriptLoad(this, aURL, aRunInGlobalScope)); 1.320 + return; 1.321 + } 1.322 + if (!mInitialized) { 1.323 + mInitialized = true; 1.324 + Init(); 1.325 + } 1.326 + bool tmp = mLoadingScript; 1.327 + mLoadingScript = true; 1.328 + LoadFrameScriptInternal(aURL, aRunInGlobalScope); 1.329 + mLoadingScript = tmp; 1.330 +}