Bug 1828523 [wpt PR 39579] - Only restore dialog focus if focus is in the dialog...
[gecko.git] / dom / webbrowserpersist / WebBrowserPersistDocumentParent.cpp
blob8d08ae5fc5e5087d50c170b49a4d7c2cef762874
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"
17 namespace mozilla {
19 WebBrowserPersistDocumentParent::WebBrowserPersistDocumentParent()
20 : mReflection(nullptr) {}
22 void WebBrowserPersistDocumentParent::SetOnReady(
23 nsIWebBrowserPersistDocumentReceiver* aOnReady) {
24 MOZ_ASSERT(aOnReady);
25 MOZ_ASSERT(!mOnReady);
26 MOZ_ASSERT(!mReflection);
27 mOnReady = aOnReady;
30 void WebBrowserPersistDocumentParent::ActorDestroy(ActorDestroyReason aWhy) {
31 if (mReflection) {
32 mReflection->ActorDestroy();
33 mReflection = nullptr;
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;
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);
66 mOnReady = nullptr;
67 return IPC_OK();
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);
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);
82 return IPC_OK();
85 PWebBrowserPersistResourcesParent*
86 WebBrowserPersistDocumentParent::AllocPWebBrowserPersistResourcesParent() {
87 MOZ_CRASH("Don't use this; construct the actor directly and AddRef.");
88 return nullptr;
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;
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;
109 bool WebBrowserPersistDocumentParent::DeallocPWebBrowserPersistSerializeParent(
110 PWebBrowserPersistSerializeParent* aActor) {
111 delete aActor;
112 return true;
115 } // namespace mozilla