Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
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 "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"
16 :
17 : namespace mozilla {
18 :
19 0 : WebBrowserPersistDocumentParent::WebBrowserPersistDocumentParent()
20 0 : : mReflection(nullptr)
21 : {
22 0 : }
23 :
24 : void
25 0 : WebBrowserPersistDocumentParent::SetOnReady(nsIWebBrowserPersistDocumentReceiver* aOnReady)
26 : {
27 0 : MOZ_ASSERT(aOnReady);
28 0 : MOZ_ASSERT(!mOnReady);
29 0 : MOZ_ASSERT(!mReflection);
30 0 : mOnReady = aOnReady;
31 0 : }
32 :
33 : void
34 0 : WebBrowserPersistDocumentParent::ActorDestroy(ActorDestroyReason aWhy)
35 : {
36 0 : if (mReflection) {
37 0 : mReflection->ActorDestroy();
38 0 : mReflection = nullptr;
39 : }
40 0 : if (mOnReady) {
41 : // Bug 1202887: If this is part of a subtree destruction, then
42 : // anything which could cause another actor in that subtree to
43 : // be Send__delete__()ed will cause use-after-free -- such as
44 : // dropping the last reference to another document's
45 : // WebBrowserPersistRemoteDocument. To avoid that, defer the
46 : // callback until after the entire subtree is destroyed.
47 0 : nsCOMPtr<nsIRunnable> errorLater = NewRunnableMethod<nsresult>(
48 : "nsIWebBrowserPersistDocumentReceiver::OnError",
49 : mOnReady,
50 : &nsIWebBrowserPersistDocumentReceiver::OnError,
51 0 : NS_ERROR_FAILURE);
52 0 : NS_DispatchToCurrentThread(errorLater);
53 0 : mOnReady = nullptr;
54 : }
55 0 : }
56 :
57 0 : WebBrowserPersistDocumentParent::~WebBrowserPersistDocumentParent()
58 : {
59 0 : MOZ_RELEASE_ASSERT(!mReflection);
60 0 : MOZ_ASSERT(!mOnReady);
61 0 : }
62 :
63 : mozilla::ipc::IPCResult
64 0 : WebBrowserPersistDocumentParent::RecvAttributes(const Attrs& aAttrs,
65 : const OptionalIPCStream& aPostStream)
66 : {
67 : // Deserialize the postData unconditionally so that fds aren't leaked.
68 0 : nsCOMPtr<nsIInputStream> postData = mozilla::ipc::DeserializeIPCStream(aPostStream);
69 0 : if (!mOnReady || mReflection) {
70 0 : return IPC_FAIL_NO_REASON(this);
71 : }
72 0 : mReflection = new WebBrowserPersistRemoteDocument(this, aAttrs, postData);
73 0 : RefPtr<WebBrowserPersistRemoteDocument> reflection = mReflection;
74 0 : mOnReady->OnDocumentReady(reflection);
75 0 : mOnReady = nullptr;
76 0 : return IPC_OK();
77 : }
78 :
79 : mozilla::ipc::IPCResult
80 0 : WebBrowserPersistDocumentParent::RecvInitFailure(const nsresult& aFailure)
81 : {
82 0 : if (!mOnReady || mReflection) {
83 0 : return IPC_FAIL_NO_REASON(this);
84 : }
85 0 : mOnReady->OnError(aFailure);
86 0 : mOnReady = nullptr;
87 : // Warning: Send__delete__ deallocates this object.
88 0 : IProtocol* mgr = Manager();
89 0 : if (!Send__delete__(this)) {
90 0 : return IPC_FAIL_NO_REASON(mgr);
91 : }
92 0 : return IPC_OK();
93 : }
94 :
95 : PWebBrowserPersistResourcesParent*
96 0 : WebBrowserPersistDocumentParent::AllocPWebBrowserPersistResourcesParent()
97 : {
98 0 : MOZ_CRASH("Don't use this; construct the actor directly and AddRef.");
99 : return nullptr;
100 : }
101 :
102 : bool
103 0 : WebBrowserPersistDocumentParent::DeallocPWebBrowserPersistResourcesParent(PWebBrowserPersistResourcesParent* aActor)
104 : {
105 : // Turn the ref held by IPC back into an nsRefPtr.
106 : RefPtr<WebBrowserPersistResourcesParent> actor =
107 0 : already_AddRefed<WebBrowserPersistResourcesParent>(
108 0 : static_cast<WebBrowserPersistResourcesParent*>(aActor));
109 0 : return true;
110 : }
111 :
112 : PWebBrowserPersistSerializeParent*
113 0 : WebBrowserPersistDocumentParent::AllocPWebBrowserPersistSerializeParent(
114 : const WebBrowserPersistURIMap& aMap,
115 : const nsCString& aRequestedContentType,
116 : const uint32_t& aEncoderFlags,
117 : const uint32_t& aWrapColumn)
118 : {
119 0 : MOZ_CRASH("Don't use this; construct the actor directly.");
120 : return nullptr;
121 : }
122 :
123 : bool
124 0 : WebBrowserPersistDocumentParent::DeallocPWebBrowserPersistSerializeParent(PWebBrowserPersistSerializeParent* aActor)
125 : {
126 0 : delete aActor;
127 0 : return true;
128 : }
129 :
130 : } // namespace mozilla
|