Bug 1828523 [wpt PR 39579] - Only restore dialog focus if focus is in the dialog...
[gecko.git] / dom / webbrowserpersist / WebBrowserPersistRemoteDocument.cpp
bloba4e7c96c857939b794507ac72ebf2517005e728c
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 "WebBrowserPersistRemoteDocument.h"
8 #include "WebBrowserPersistDocumentParent.h"
9 #include "WebBrowserPersistResourcesParent.h"
10 #include "WebBrowserPersistSerializeParent.h"
11 #include "mozilla/Unused.h"
12 #include "mozilla/ipc/BackgroundUtils.h"
13 #include "mozilla/net/CookieJarSettings.h"
15 #include "nsDebug.h"
16 #include "nsIPrincipal.h"
17 #include "nsISHEntry.h"
19 namespace mozilla {
21 NS_IMPL_ISUPPORTS(WebBrowserPersistRemoteDocument, nsIWebBrowserPersistDocument)
23 WebBrowserPersistRemoteDocument ::WebBrowserPersistRemoteDocument(
24 WebBrowserPersistDocumentParent* aActor, const Attrs& aAttrs,
25 nsIInputStream* aPostData)
26 : mActor(aActor), mAttrs(aAttrs), mPostData(aPostData) {
27 auto principalOrErr = ipc::PrincipalInfoToPrincipal(mAttrs.principal());
28 if (principalOrErr.isOk()) {
29 mPrincipal = principalOrErr.unwrap();
30 } else {
31 NS_WARNING("Failed to obtain principal!");
34 net::CookieJarSettings::Deserialize(mAttrs.cookieJarSettings(),
35 getter_AddRefs(mCookieJarSettings));
38 WebBrowserPersistRemoteDocument::~WebBrowserPersistRemoteDocument() {
39 if (mActor) {
40 Unused << WebBrowserPersistDocumentParent::Send__delete__(mActor);
41 // That will call mActor->ActorDestroy, which calls this->ActorDestroy
42 // (whether or not the IPC send succeeds).
44 MOZ_ASSERT(!mActor);
47 void WebBrowserPersistRemoteDocument::ActorDestroy(void) { mActor = nullptr; }
49 NS_IMETHODIMP
50 WebBrowserPersistRemoteDocument::GetIsClosed(bool* aIsClosed) {
51 *aIsClosed = !mActor;
52 return NS_OK;
55 NS_IMETHODIMP
56 WebBrowserPersistRemoteDocument::GetIsPrivate(bool* aIsPrivate) {
57 *aIsPrivate = mAttrs.isPrivate();
58 return NS_OK;
61 NS_IMETHODIMP
62 WebBrowserPersistRemoteDocument::GetDocumentURI(nsACString& aURISpec) {
63 aURISpec = mAttrs.documentURI();
64 return NS_OK;
67 NS_IMETHODIMP
68 WebBrowserPersistRemoteDocument::GetBaseURI(nsACString& aURISpec) {
69 aURISpec = mAttrs.baseURI();
70 return NS_OK;
73 NS_IMETHODIMP
74 WebBrowserPersistRemoteDocument::GetContentType(nsACString& aContentType) {
75 aContentType = mAttrs.contentType();
76 return NS_OK;
79 NS_IMETHODIMP
80 WebBrowserPersistRemoteDocument::GetCharacterSet(nsACString& aCharSet) {
81 aCharSet = mAttrs.characterSet();
82 return NS_OK;
85 NS_IMETHODIMP
86 WebBrowserPersistRemoteDocument::GetTitle(nsAString& aTitle) {
87 aTitle = mAttrs.title();
88 return NS_OK;
91 NS_IMETHODIMP
92 WebBrowserPersistRemoteDocument::GetReferrerInfo(
93 nsIReferrerInfo** aReferrerInfo) {
94 *aReferrerInfo = mAttrs.referrerInfo();
95 NS_IF_ADDREF(*aReferrerInfo);
96 return NS_OK;
99 NS_IMETHODIMP
100 WebBrowserPersistRemoteDocument::GetCookieJarSettings(
101 nsICookieJarSettings** aCookieJarSettings) {
102 nsCOMPtr<nsICookieJarSettings> cookieJarSettings = mCookieJarSettings;
103 cookieJarSettings.forget(aCookieJarSettings);
104 return NS_OK;
107 NS_IMETHODIMP
108 WebBrowserPersistRemoteDocument::GetContentDisposition(nsAString& aDisp) {
109 aDisp = mAttrs.contentDisposition();
110 return NS_OK;
113 NS_IMETHODIMP
114 WebBrowserPersistRemoteDocument::GetCacheKey(uint32_t* aCacheKey) {
115 *aCacheKey = mAttrs.sessionHistoryCacheKey();
116 return NS_OK;
119 NS_IMETHODIMP
120 WebBrowserPersistRemoteDocument::GetPersistFlags(uint32_t* aFlags) {
121 *aFlags = mAttrs.persistFlags();
122 return NS_OK;
125 NS_IMETHODIMP
126 WebBrowserPersistRemoteDocument::SetPersistFlags(uint32_t aFlags) {
127 if (!mActor) {
128 return NS_ERROR_FAILURE;
130 if (!mActor->SendSetPersistFlags(aFlags)) {
131 return NS_ERROR_FAILURE;
133 mAttrs.persistFlags() = aFlags;
134 return NS_OK;
137 NS_IMETHODIMP
138 WebBrowserPersistRemoteDocument::GetPostData(nsIInputStream** aStream) {
139 nsCOMPtr<nsIInputStream> stream = mPostData;
140 stream.forget(aStream);
141 return NS_OK;
144 NS_IMETHODIMP
145 WebBrowserPersistRemoteDocument::GetPrincipal(nsIPrincipal** aPrincipal) {
146 nsCOMPtr<nsIPrincipal> nodePrincipal = mPrincipal;
147 nodePrincipal.forget(aPrincipal);
148 return NS_OK;
151 NS_IMETHODIMP
152 WebBrowserPersistRemoteDocument::ReadResources(
153 nsIWebBrowserPersistResourceVisitor* aVisitor) {
154 if (!mActor) {
155 return NS_ERROR_FAILURE;
157 RefPtr<WebBrowserPersistResourcesParent> subActor =
158 new WebBrowserPersistResourcesParent(this, aVisitor);
159 return mActor->SendPWebBrowserPersistResourcesConstructor(
160 subActor.forget().take())
161 ? NS_OK
162 : NS_ERROR_FAILURE;
165 NS_IMETHODIMP
166 WebBrowserPersistRemoteDocument::WriteContent(
167 nsIOutputStream* aStream, nsIWebBrowserPersistURIMap* aMap,
168 const nsACString& aRequestedContentType, uint32_t aEncoderFlags,
169 uint32_t aWrapColumn, nsIWebBrowserPersistWriteCompletion* aCompletion) {
170 if (!mActor) {
171 return NS_ERROR_FAILURE;
174 nsresult rv;
175 WebBrowserPersistURIMap map;
176 uint32_t numMappedURIs;
177 if (aMap) {
178 rv = aMap->GetTargetBaseURI(map.targetBaseURI());
179 NS_ENSURE_SUCCESS(rv, rv);
180 rv = aMap->GetNumMappedURIs(&numMappedURIs);
181 NS_ENSURE_SUCCESS(rv, rv);
182 for (uint32_t i = 0; i < numMappedURIs; ++i) {
183 WebBrowserPersistURIMapEntry& nextEntry =
184 *(map.mapURIs().AppendElement());
185 rv = aMap->GetURIMapping(i, nextEntry.mapFrom(), nextEntry.mapTo());
186 NS_ENSURE_SUCCESS(rv, rv);
190 auto* subActor =
191 new WebBrowserPersistSerializeParent(this, aStream, aCompletion);
192 nsCString requestedContentType(aRequestedContentType); // Sigh.
193 return mActor->SendPWebBrowserPersistSerializeConstructor(
194 subActor, map, requestedContentType, aEncoderFlags, aWrapColumn)
195 ? NS_OK
196 : NS_ERROR_FAILURE;
199 // Forcing WebBrowserPersistRemoteDocument to implement GetHistory is the
200 // easiest way to ensure that we can call GetHistory in
201 // WebBrowserPersistDocumentChild::Start
202 already_AddRefed<nsISHEntry> WebBrowserPersistRemoteDocument::GetHistory() {
203 MOZ_CRASH("We should not call GetHistory on WebBrowserPersistRemoteDocument");
204 return nullptr;
207 } // namespace mozilla