Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 8; -*- */ |
michael@0 | 2 | /* vim: set sw=4 ts=8 et tw=80 : */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "nsInProcessTabChildGlobal.h" |
michael@0 | 8 | #include "nsContentUtils.h" |
michael@0 | 9 | #include "nsIScriptSecurityManager.h" |
michael@0 | 10 | #include "nsIInterfaceRequestorUtils.h" |
michael@0 | 11 | #include "nsIComponentManager.h" |
michael@0 | 12 | #include "nsIServiceManager.h" |
michael@0 | 13 | #include "nsIJSRuntimeService.h" |
michael@0 | 14 | #include "nsComponentManagerUtils.h" |
michael@0 | 15 | #include "nsNetUtil.h" |
michael@0 | 16 | #include "nsScriptLoader.h" |
michael@0 | 17 | #include "nsFrameLoader.h" |
michael@0 | 18 | #include "xpcpublic.h" |
michael@0 | 19 | #include "nsIMozBrowserFrame.h" |
michael@0 | 20 | #include "nsDOMClassInfoID.h" |
michael@0 | 21 | #include "mozilla/EventDispatcher.h" |
michael@0 | 22 | #include "mozilla/dom/StructuredCloneUtils.h" |
michael@0 | 23 | #include "js/StructuredClone.h" |
michael@0 | 24 | |
michael@0 | 25 | using mozilla::dom::StructuredCloneData; |
michael@0 | 26 | using mozilla::dom::StructuredCloneClosure; |
michael@0 | 27 | using namespace mozilla; |
michael@0 | 28 | |
michael@0 | 29 | bool |
michael@0 | 30 | nsInProcessTabChildGlobal::DoSendBlockingMessage(JSContext* aCx, |
michael@0 | 31 | const nsAString& aMessage, |
michael@0 | 32 | const dom::StructuredCloneData& aData, |
michael@0 | 33 | JS::Handle<JSObject *> aCpows, |
michael@0 | 34 | nsIPrincipal* aPrincipal, |
michael@0 | 35 | InfallibleTArray<nsString>* aJSONRetVal, |
michael@0 | 36 | bool aIsSync) |
michael@0 | 37 | { |
michael@0 | 38 | nsTArray<nsCOMPtr<nsIRunnable> > asyncMessages; |
michael@0 | 39 | asyncMessages.SwapElements(mASyncMessages); |
michael@0 | 40 | uint32_t len = asyncMessages.Length(); |
michael@0 | 41 | for (uint32_t i = 0; i < len; ++i) { |
michael@0 | 42 | nsCOMPtr<nsIRunnable> async = asyncMessages[i]; |
michael@0 | 43 | async->Run(); |
michael@0 | 44 | } |
michael@0 | 45 | if (mChromeMessageManager) { |
michael@0 | 46 | SameProcessCpowHolder cpows(js::GetRuntime(aCx), aCpows); |
michael@0 | 47 | nsRefPtr<nsFrameMessageManager> mm = mChromeMessageManager; |
michael@0 | 48 | mm->ReceiveMessage(mOwner, aMessage, true, &aData, &cpows, aPrincipal, |
michael@0 | 49 | aJSONRetVal); |
michael@0 | 50 | } |
michael@0 | 51 | return true; |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | class nsAsyncMessageToParent : public nsSameProcessAsyncMessageBase, |
michael@0 | 55 | public nsRunnable |
michael@0 | 56 | { |
michael@0 | 57 | public: |
michael@0 | 58 | nsAsyncMessageToParent(JSContext* aCx, |
michael@0 | 59 | nsInProcessTabChildGlobal* aTabChild, |
michael@0 | 60 | const nsAString& aMessage, |
michael@0 | 61 | const StructuredCloneData& aData, |
michael@0 | 62 | JS::Handle<JSObject *> aCpows, |
michael@0 | 63 | nsIPrincipal* aPrincipal) |
michael@0 | 64 | : nsSameProcessAsyncMessageBase(aCx, aMessage, aData, aCpows, aPrincipal), |
michael@0 | 65 | mTabChild(aTabChild), mRun(false) |
michael@0 | 66 | { |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | NS_IMETHOD Run() |
michael@0 | 70 | { |
michael@0 | 71 | if (mRun) { |
michael@0 | 72 | return NS_OK; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | mRun = true; |
michael@0 | 76 | mTabChild->mASyncMessages.RemoveElement(this); |
michael@0 | 77 | ReceiveMessage(mTabChild->mOwner, mTabChild->mChromeMessageManager); |
michael@0 | 78 | return NS_OK; |
michael@0 | 79 | } |
michael@0 | 80 | nsRefPtr<nsInProcessTabChildGlobal> mTabChild; |
michael@0 | 81 | // True if this runnable has already been called. This can happen if DoSendSyncMessage |
michael@0 | 82 | // is called while waiting for an asynchronous message send. |
michael@0 | 83 | bool mRun; |
michael@0 | 84 | }; |
michael@0 | 85 | |
michael@0 | 86 | bool |
michael@0 | 87 | nsInProcessTabChildGlobal::DoSendAsyncMessage(JSContext* aCx, |
michael@0 | 88 | const nsAString& aMessage, |
michael@0 | 89 | const StructuredCloneData& aData, |
michael@0 | 90 | JS::Handle<JSObject *> aCpows, |
michael@0 | 91 | nsIPrincipal* aPrincipal) |
michael@0 | 92 | { |
michael@0 | 93 | nsCOMPtr<nsIRunnable> ev = |
michael@0 | 94 | new nsAsyncMessageToParent(aCx, this, aMessage, aData, aCpows, aPrincipal); |
michael@0 | 95 | mASyncMessages.AppendElement(ev); |
michael@0 | 96 | NS_DispatchToCurrentThread(ev); |
michael@0 | 97 | return true; |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | nsInProcessTabChildGlobal::nsInProcessTabChildGlobal(nsIDocShell* aShell, |
michael@0 | 101 | nsIContent* aOwner, |
michael@0 | 102 | nsFrameMessageManager* aChrome) |
michael@0 | 103 | : mDocShell(aShell), mInitialized(false), mLoadingScript(false), |
michael@0 | 104 | mOwner(aOwner), mChromeMessageManager(aChrome) |
michael@0 | 105 | { |
michael@0 | 106 | |
michael@0 | 107 | // If owner corresponds to an <iframe mozbrowser> or <iframe mozapp>, we'll |
michael@0 | 108 | // have to tweak our PreHandleEvent implementation. |
michael@0 | 109 | nsCOMPtr<nsIMozBrowserFrame> browserFrame = do_QueryInterface(mOwner); |
michael@0 | 110 | if (browserFrame) { |
michael@0 | 111 | mIsBrowserOrAppFrame = browserFrame->GetReallyIsBrowserOrApp(); |
michael@0 | 112 | } |
michael@0 | 113 | else { |
michael@0 | 114 | mIsBrowserOrAppFrame = false; |
michael@0 | 115 | } |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | nsInProcessTabChildGlobal::~nsInProcessTabChildGlobal() |
michael@0 | 119 | { |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | /* [notxpcom] boolean markForCC (); */ |
michael@0 | 123 | // This method isn't automatically forwarded safely because it's notxpcom, so |
michael@0 | 124 | // the IDL binding doesn't know what value to return. |
michael@0 | 125 | NS_IMETHODIMP_(bool) |
michael@0 | 126 | nsInProcessTabChildGlobal::MarkForCC() |
michael@0 | 127 | { |
michael@0 | 128 | return mMessageManager ? mMessageManager->MarkForCC() : false; |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | nsresult |
michael@0 | 132 | nsInProcessTabChildGlobal::Init() |
michael@0 | 133 | { |
michael@0 | 134 | #ifdef DEBUG |
michael@0 | 135 | nsresult rv = |
michael@0 | 136 | #endif |
michael@0 | 137 | InitTabChildGlobal(); |
michael@0 | 138 | NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), |
michael@0 | 139 | "Couldn't initialize nsInProcessTabChildGlobal"); |
michael@0 | 140 | mMessageManager = new nsFrameMessageManager(this, |
michael@0 | 141 | nullptr, |
michael@0 | 142 | dom::ipc::MM_CHILD); |
michael@0 | 143 | return NS_OK; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | NS_IMPL_CYCLE_COLLECTION_INHERITED(nsInProcessTabChildGlobal, |
michael@0 | 147 | DOMEventTargetHelper, |
michael@0 | 148 | mMessageManager, |
michael@0 | 149 | mGlobal) |
michael@0 | 150 | |
michael@0 | 151 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsInProcessTabChildGlobal) |
michael@0 | 152 | NS_INTERFACE_MAP_ENTRY(nsIMessageListenerManager) |
michael@0 | 153 | NS_INTERFACE_MAP_ENTRY(nsIMessageSender) |
michael@0 | 154 | NS_INTERFACE_MAP_ENTRY(nsISyncMessageSender) |
michael@0 | 155 | NS_INTERFACE_MAP_ENTRY(nsIContentFrameMessageManager) |
michael@0 | 156 | NS_INTERFACE_MAP_ENTRY(nsIInProcessContentFrameMessageManager) |
michael@0 | 157 | NS_INTERFACE_MAP_ENTRY(nsIScriptObjectPrincipal) |
michael@0 | 158 | NS_INTERFACE_MAP_ENTRY(nsIGlobalObject) |
michael@0 | 159 | NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference) |
michael@0 | 160 | NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(ContentFrameMessageManager) |
michael@0 | 161 | NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper) |
michael@0 | 162 | |
michael@0 | 163 | NS_IMPL_ADDREF_INHERITED(nsInProcessTabChildGlobal, DOMEventTargetHelper) |
michael@0 | 164 | NS_IMPL_RELEASE_INHERITED(nsInProcessTabChildGlobal, DOMEventTargetHelper) |
michael@0 | 165 | |
michael@0 | 166 | NS_IMETHODIMP |
michael@0 | 167 | nsInProcessTabChildGlobal::GetContent(nsIDOMWindow** aContent) |
michael@0 | 168 | { |
michael@0 | 169 | *aContent = nullptr; |
michael@0 | 170 | nsCOMPtr<nsIDOMWindow> window = do_GetInterface(mDocShell); |
michael@0 | 171 | window.swap(*aContent); |
michael@0 | 172 | return NS_OK; |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | NS_IMETHODIMP |
michael@0 | 176 | nsInProcessTabChildGlobal::GetDocShell(nsIDocShell** aDocShell) |
michael@0 | 177 | { |
michael@0 | 178 | NS_IF_ADDREF(*aDocShell = mDocShell); |
michael@0 | 179 | return NS_OK; |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | NS_IMETHODIMP |
michael@0 | 183 | nsInProcessTabChildGlobal::Btoa(const nsAString& aBinaryData, |
michael@0 | 184 | nsAString& aAsciiBase64String) |
michael@0 | 185 | { |
michael@0 | 186 | return nsContentUtils::Btoa(aBinaryData, aAsciiBase64String); |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | NS_IMETHODIMP |
michael@0 | 190 | nsInProcessTabChildGlobal::Atob(const nsAString& aAsciiString, |
michael@0 | 191 | nsAString& aBinaryData) |
michael@0 | 192 | { |
michael@0 | 193 | return nsContentUtils::Atob(aAsciiString, aBinaryData); |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | |
michael@0 | 197 | NS_IMETHODIMP |
michael@0 | 198 | nsInProcessTabChildGlobal::PrivateNoteIntentionalCrash() |
michael@0 | 199 | { |
michael@0 | 200 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | void |
michael@0 | 204 | nsInProcessTabChildGlobal::Disconnect() |
michael@0 | 205 | { |
michael@0 | 206 | // Let the frame scripts know the child is being closed. We do any other |
michael@0 | 207 | // cleanup after the event has been fired. See DelayedDisconnect |
michael@0 | 208 | nsContentUtils::AddScriptRunner( |
michael@0 | 209 | NS_NewRunnableMethod(this, &nsInProcessTabChildGlobal::DelayedDisconnect) |
michael@0 | 210 | ); |
michael@0 | 211 | } |
michael@0 | 212 | |
michael@0 | 213 | void |
michael@0 | 214 | nsInProcessTabChildGlobal::DelayedDisconnect() |
michael@0 | 215 | { |
michael@0 | 216 | // Don't let the event escape |
michael@0 | 217 | mOwner = nullptr; |
michael@0 | 218 | |
michael@0 | 219 | // Fire the "unload" event |
michael@0 | 220 | DOMEventTargetHelper::DispatchTrustedEvent(NS_LITERAL_STRING("unload")); |
michael@0 | 221 | |
michael@0 | 222 | // Continue with the Disconnect cleanup |
michael@0 | 223 | nsCOMPtr<nsPIDOMWindow> win = do_GetInterface(mDocShell); |
michael@0 | 224 | if (win) { |
michael@0 | 225 | MOZ_ASSERT(win->IsOuterWindow()); |
michael@0 | 226 | win->SetChromeEventHandler(win->GetChromeEventHandler()); |
michael@0 | 227 | } |
michael@0 | 228 | mDocShell = nullptr; |
michael@0 | 229 | mChromeMessageManager = nullptr; |
michael@0 | 230 | if (mMessageManager) { |
michael@0 | 231 | static_cast<nsFrameMessageManager*>(mMessageManager.get())->Disconnect(); |
michael@0 | 232 | mMessageManager = nullptr; |
michael@0 | 233 | } |
michael@0 | 234 | if (mListenerManager) { |
michael@0 | 235 | mListenerManager->Disconnect(); |
michael@0 | 236 | } |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | NS_IMETHODIMP_(nsIContent *) |
michael@0 | 240 | nsInProcessTabChildGlobal::GetOwnerContent() |
michael@0 | 241 | { |
michael@0 | 242 | return mOwner; |
michael@0 | 243 | } |
michael@0 | 244 | |
michael@0 | 245 | nsresult |
michael@0 | 246 | nsInProcessTabChildGlobal::PreHandleEvent(EventChainPreVisitor& aVisitor) |
michael@0 | 247 | { |
michael@0 | 248 | aVisitor.mCanHandle = true; |
michael@0 | 249 | |
michael@0 | 250 | if (mIsBrowserOrAppFrame && |
michael@0 | 251 | (!mOwner || !nsContentUtils::IsInChromeDocshell(mOwner->OwnerDoc()))) { |
michael@0 | 252 | if (mOwner) { |
michael@0 | 253 | nsPIDOMWindow* innerWindow = mOwner->OwnerDoc()->GetInnerWindow(); |
michael@0 | 254 | if (innerWindow) { |
michael@0 | 255 | aVisitor.mParentTarget = innerWindow->GetParentTarget(); |
michael@0 | 256 | } |
michael@0 | 257 | } |
michael@0 | 258 | } else { |
michael@0 | 259 | aVisitor.mParentTarget = mOwner; |
michael@0 | 260 | } |
michael@0 | 261 | |
michael@0 | 262 | #ifdef DEBUG |
michael@0 | 263 | if (mOwner) { |
michael@0 | 264 | nsCOMPtr<nsIFrameLoaderOwner> owner = do_QueryInterface(mOwner); |
michael@0 | 265 | nsRefPtr<nsFrameLoader> fl = owner->GetFrameLoader(); |
michael@0 | 266 | if (fl) { |
michael@0 | 267 | NS_ASSERTION(this == fl->GetTabChildGlobalAsEventTarget(), |
michael@0 | 268 | "Wrong event target!"); |
michael@0 | 269 | NS_ASSERTION(fl->mMessageManager == mChromeMessageManager, |
michael@0 | 270 | "Wrong message manager!"); |
michael@0 | 271 | } |
michael@0 | 272 | } |
michael@0 | 273 | #endif |
michael@0 | 274 | |
michael@0 | 275 | return NS_OK; |
michael@0 | 276 | } |
michael@0 | 277 | |
michael@0 | 278 | nsresult |
michael@0 | 279 | nsInProcessTabChildGlobal::InitTabChildGlobal() |
michael@0 | 280 | { |
michael@0 | 281 | nsAutoCString id; |
michael@0 | 282 | id.AssignLiteral("inProcessTabChildGlobal"); |
michael@0 | 283 | nsIURI* uri = mOwner->OwnerDoc()->GetDocumentURI(); |
michael@0 | 284 | if (uri) { |
michael@0 | 285 | nsAutoCString u; |
michael@0 | 286 | uri->GetSpec(u); |
michael@0 | 287 | id.AppendLiteral("?ownedBy="); |
michael@0 | 288 | id.Append(u); |
michael@0 | 289 | } |
michael@0 | 290 | nsISupports* scopeSupports = NS_ISUPPORTS_CAST(EventTarget*, this); |
michael@0 | 291 | NS_ENSURE_STATE(InitTabChildGlobalInternal(scopeSupports, id)); |
michael@0 | 292 | return NS_OK; |
michael@0 | 293 | } |
michael@0 | 294 | |
michael@0 | 295 | class nsAsyncScriptLoad : public nsRunnable |
michael@0 | 296 | { |
michael@0 | 297 | public: |
michael@0 | 298 | nsAsyncScriptLoad(nsInProcessTabChildGlobal* aTabChild, const nsAString& aURL, |
michael@0 | 299 | bool aRunInGlobalScope) |
michael@0 | 300 | : mTabChild(aTabChild), mURL(aURL), mRunInGlobalScope(aRunInGlobalScope) {} |
michael@0 | 301 | |
michael@0 | 302 | NS_IMETHOD Run() |
michael@0 | 303 | { |
michael@0 | 304 | mTabChild->LoadFrameScript(mURL, mRunInGlobalScope); |
michael@0 | 305 | return NS_OK; |
michael@0 | 306 | } |
michael@0 | 307 | nsRefPtr<nsInProcessTabChildGlobal> mTabChild; |
michael@0 | 308 | nsString mURL; |
michael@0 | 309 | bool mRunInGlobalScope; |
michael@0 | 310 | }; |
michael@0 | 311 | |
michael@0 | 312 | void |
michael@0 | 313 | nsInProcessTabChildGlobal::LoadFrameScript(const nsAString& aURL, bool aRunInGlobalScope) |
michael@0 | 314 | { |
michael@0 | 315 | if (!nsContentUtils::IsSafeToRunScript()) { |
michael@0 | 316 | nsContentUtils::AddScriptRunner(new nsAsyncScriptLoad(this, aURL, aRunInGlobalScope)); |
michael@0 | 317 | return; |
michael@0 | 318 | } |
michael@0 | 319 | if (!mInitialized) { |
michael@0 | 320 | mInitialized = true; |
michael@0 | 321 | Init(); |
michael@0 | 322 | } |
michael@0 | 323 | bool tmp = mLoadingScript; |
michael@0 | 324 | mLoadingScript = true; |
michael@0 | 325 | LoadFrameScriptInternal(aURL, aRunInGlobalScope); |
michael@0 | 326 | mLoadingScript = tmp; |
michael@0 | 327 | } |