1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 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/. */
7 #include "WebBrowserPersistDocumentParent.h"
9 #include "mozilla/ipc/IPCStreamUtils.h"
10 #include "mozilla/dom/PContentParent.h"
11 #include "nsIInputStream.h"
12 #include "nsThreadUtils.h"
13 #include "WebBrowserPersistResourcesParent.h"
14 #include "WebBrowserPersistSerializeParent.h"
15 #include "WebBrowserPersistRemoteDocument.h"
19 WebBrowserPersistDocumentParent::WebBrowserPersistDocumentParent()
20 : mReflection(nullptr) {}
22 void WebBrowserPersistDocumentParent::SetOnReady(
23 nsIWebBrowserPersistDocumentReceiver
* aOnReady
) {
25 MOZ_ASSERT(!mOnReady
);
26 MOZ_ASSERT(!mReflection
);
30 void WebBrowserPersistDocumentParent::ActorDestroy(ActorDestroyReason aWhy
) {
32 mReflection
->ActorDestroy();
33 mReflection
= nullptr;
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
);
50 WebBrowserPersistDocumentParent::~WebBrowserPersistDocumentParent() {
51 MOZ_RELEASE_ASSERT(!mReflection
);
52 MOZ_ASSERT(!mOnReady
);
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);
63 mReflection
= new WebBrowserPersistRemoteDocument(this, aAttrs
, postData
);
64 RefPtr
<WebBrowserPersistRemoteDocument
> reflection
= mReflection
;
65 mOnReady
->OnDocumentReady(reflection
);
70 mozilla::ipc::IPCResult
WebBrowserPersistDocumentParent::RecvInitFailure(
71 const nsresult
& aFailure
) {
72 if (!mOnReady
|| mReflection
) {
73 return IPC_FAIL_NO_REASON(this);
75 mOnReady
->OnError(aFailure
);
77 // Warning: Send__delete__ deallocates this object.
78 IProtocol
* mgr
= Manager();
79 if (!Send__delete__(this)) {
80 return IPC_FAIL_NO_REASON(mgr
);
85 PWebBrowserPersistResourcesParent
*
86 WebBrowserPersistDocumentParent::AllocPWebBrowserPersistResourcesParent() {
87 MOZ_CRASH("Don't use this; construct the actor directly and AddRef.");
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
));
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.");
109 bool WebBrowserPersistDocumentParent::DeallocPWebBrowserPersistSerializeParent(
110 PWebBrowserPersistSerializeParent
* aActor
) {
115 } // namespace mozilla