WebBrowserPersistDocumentParent.cpp (4046B)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * 3 * This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "WebBrowserPersistDocumentParent.h" 8 9 #include "WebBrowserPersistRemoteDocument.h" 10 #include "WebBrowserPersistResourcesParent.h" 11 #include "WebBrowserPersistSerializeParent.h" 12 #include "mozilla/dom/PContentParent.h" 13 #include "mozilla/ipc/IPCStreamUtils.h" 14 #include "nsIInputStream.h" 15 #include "nsThreadUtils.h" 16 17 namespace mozilla { 18 19 WebBrowserPersistDocumentParent::WebBrowserPersistDocumentParent() 20 : mReflection(nullptr) {} 21 22 void WebBrowserPersistDocumentParent::SetOnReady( 23 nsIWebBrowserPersistDocumentReceiver* aOnReady) { 24 MOZ_ASSERT(aOnReady); 25 MOZ_ASSERT(!mOnReady); 26 MOZ_ASSERT(!mReflection); 27 mOnReady = aOnReady; 28 } 29 30 void WebBrowserPersistDocumentParent::ActorDestroy(ActorDestroyReason aWhy) { 31 if (mReflection) { 32 mReflection->ActorDestroy(); 33 mReflection = nullptr; 34 } 35 if (mOnReady) { 36 // Bug 1202887: If this is part of a subtree destruction, then 37 // anything which could cause another actor in that subtree to 38 // be Send__delete__()ed will cause use-after-free -- such as 39 // dropping the last reference to another document's 40 // WebBrowserPersistRemoteDocument. To avoid that, defer the 41 // callback until after the entire subtree is destroyed. 42 nsCOMPtr<nsIRunnable> errorLater = NewRunnableMethod<nsresult>( 43 "nsIWebBrowserPersistDocumentReceiver::OnError", mOnReady, 44 &nsIWebBrowserPersistDocumentReceiver::OnError, NS_ERROR_FAILURE); 45 NS_DispatchToCurrentThread(errorLater); 46 mOnReady = nullptr; 47 } 48 } 49 50 WebBrowserPersistDocumentParent::~WebBrowserPersistDocumentParent() { 51 MOZ_RELEASE_ASSERT(!mReflection); 52 MOZ_ASSERT(!mOnReady); 53 } 54 55 mozilla::ipc::IPCResult WebBrowserPersistDocumentParent::RecvAttributes( 56 const Attrs& aAttrs, const Maybe<IPCStream>& aPostStream) { 57 // Deserialize the postData unconditionally so that fds aren't leaked. 58 nsCOMPtr<nsIInputStream> postData = 59 mozilla::ipc::DeserializeIPCStream(aPostStream); 60 if (!mOnReady || mReflection) { 61 return IPC_FAIL_NO_REASON(this); 62 } 63 mReflection = new WebBrowserPersistRemoteDocument(this, aAttrs, postData); 64 RefPtr<WebBrowserPersistRemoteDocument> reflection = mReflection; 65 mOnReady->OnDocumentReady(reflection); 66 mOnReady = nullptr; 67 return IPC_OK(); 68 } 69 70 mozilla::ipc::IPCResult WebBrowserPersistDocumentParent::RecvInitFailure( 71 const nsresult& aFailure) { 72 if (!mOnReady || mReflection) { 73 return IPC_FAIL_NO_REASON(this); 74 } 75 mOnReady->OnError(aFailure); 76 mOnReady = nullptr; 77 // Warning: Send__delete__ deallocates this object. 78 IProtocol* mgr = Manager(); 79 if (!Send__delete__(this)) { 80 return IPC_FAIL_NO_REASON(mgr); 81 } 82 return IPC_OK(); 83 } 84 85 PWebBrowserPersistResourcesParent* 86 WebBrowserPersistDocumentParent::AllocPWebBrowserPersistResourcesParent() { 87 MOZ_CRASH("Don't use this; construct the actor directly and AddRef."); 88 return nullptr; 89 } 90 91 bool WebBrowserPersistDocumentParent::DeallocPWebBrowserPersistResourcesParent( 92 PWebBrowserPersistResourcesParent* aActor) { 93 // Turn the ref held by IPC back into an nsRefPtr. 94 RefPtr<WebBrowserPersistResourcesParent> actor = 95 already_AddRefed<WebBrowserPersistResourcesParent>( 96 static_cast<WebBrowserPersistResourcesParent*>(aActor)); 97 return true; 98 } 99 100 PWebBrowserPersistSerializeParent* 101 WebBrowserPersistDocumentParent::AllocPWebBrowserPersistSerializeParent( 102 const WebBrowserPersistURIMap& aMap, 103 const nsACString& aRequestedContentType, const uint32_t& aEncoderFlags, 104 const uint32_t& aWrapColumn) { 105 MOZ_CRASH("Don't use this; construct the actor directly."); 106 return nullptr; 107 } 108 109 bool WebBrowserPersistDocumentParent::DeallocPWebBrowserPersistSerializeParent( 110 PWebBrowserPersistSerializeParent* aActor) { 111 delete aActor; 112 return true; 113 } 114 115 } // namespace mozilla