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 "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"
14 
15 #include "nsDebug.h"
16 #include "nsIPrincipal.h"
17 #include "nsISHEntry.h"
18 
19 namespace mozilla {
20 
NS_IMPL_ISUPPORTS(WebBrowserPersistRemoteDocument,nsIWebBrowserPersistDocument)21 NS_IMPL_ISUPPORTS(WebBrowserPersistRemoteDocument, nsIWebBrowserPersistDocument)
22 
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!");
32   }
33 
34   net::CookieJarSettings::Deserialize(mAttrs.cookieJarSettings(),
35                                       getter_AddRefs(mCookieJarSettings));
36 }
37 
~WebBrowserPersistRemoteDocument()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).
43   }
44   MOZ_ASSERT(!mActor);
45 }
46 
ActorDestroy(void)47 void WebBrowserPersistRemoteDocument::ActorDestroy(void) { mActor = nullptr; }
48 
49 NS_IMETHODIMP
GetIsClosed(bool * aIsClosed)50 WebBrowserPersistRemoteDocument::GetIsClosed(bool* aIsClosed) {
51   *aIsClosed = !mActor;
52   return NS_OK;
53 }
54 
55 NS_IMETHODIMP
GetIsPrivate(bool * aIsPrivate)56 WebBrowserPersistRemoteDocument::GetIsPrivate(bool* aIsPrivate) {
57   *aIsPrivate = mAttrs.isPrivate();
58   return NS_OK;
59 }
60 
61 NS_IMETHODIMP
GetDocumentURI(nsACString & aURISpec)62 WebBrowserPersistRemoteDocument::GetDocumentURI(nsACString& aURISpec) {
63   aURISpec = mAttrs.documentURI();
64   return NS_OK;
65 }
66 
67 NS_IMETHODIMP
GetBaseURI(nsACString & aURISpec)68 WebBrowserPersistRemoteDocument::GetBaseURI(nsACString& aURISpec) {
69   aURISpec = mAttrs.baseURI();
70   return NS_OK;
71 }
72 
73 NS_IMETHODIMP
GetContentType(nsACString & aContentType)74 WebBrowserPersistRemoteDocument::GetContentType(nsACString& aContentType) {
75   aContentType = mAttrs.contentType();
76   return NS_OK;
77 }
78 
79 NS_IMETHODIMP
GetCharacterSet(nsACString & aCharSet)80 WebBrowserPersistRemoteDocument::GetCharacterSet(nsACString& aCharSet) {
81   aCharSet = mAttrs.characterSet();
82   return NS_OK;
83 }
84 
85 NS_IMETHODIMP
GetTitle(nsAString & aTitle)86 WebBrowserPersistRemoteDocument::GetTitle(nsAString& aTitle) {
87   aTitle = mAttrs.title();
88   return NS_OK;
89 }
90 
91 NS_IMETHODIMP
GetReferrerInfo(nsIReferrerInfo ** aReferrerInfo)92 WebBrowserPersistRemoteDocument::GetReferrerInfo(
93     nsIReferrerInfo** aReferrerInfo) {
94   *aReferrerInfo = mAttrs.referrerInfo();
95   NS_IF_ADDREF(*aReferrerInfo);
96   return NS_OK;
97 }
98 
99 NS_IMETHODIMP
GetCookieJarSettings(nsICookieJarSettings ** aCookieJarSettings)100 WebBrowserPersistRemoteDocument::GetCookieJarSettings(
101     nsICookieJarSettings** aCookieJarSettings) {
102   nsCOMPtr<nsICookieJarSettings> cookieJarSettings = mCookieJarSettings;
103   cookieJarSettings.forget(aCookieJarSettings);
104   return NS_OK;
105 }
106 
107 NS_IMETHODIMP
GetContentDisposition(nsAString & aDisp)108 WebBrowserPersistRemoteDocument::GetContentDisposition(nsAString& aDisp) {
109   aDisp = mAttrs.contentDisposition();
110   return NS_OK;
111 }
112 
113 NS_IMETHODIMP
GetCacheKey(uint32_t * aCacheKey)114 WebBrowserPersistRemoteDocument::GetCacheKey(uint32_t* aCacheKey) {
115   *aCacheKey = mAttrs.sessionHistoryCacheKey();
116   return NS_OK;
117 }
118 
119 NS_IMETHODIMP
GetPersistFlags(uint32_t * aFlags)120 WebBrowserPersistRemoteDocument::GetPersistFlags(uint32_t* aFlags) {
121   *aFlags = mAttrs.persistFlags();
122   return NS_OK;
123 }
124 
125 NS_IMETHODIMP
SetPersistFlags(uint32_t aFlags)126 WebBrowserPersistRemoteDocument::SetPersistFlags(uint32_t aFlags) {
127   if (!mActor) {
128     return NS_ERROR_FAILURE;
129   }
130   if (!mActor->SendSetPersistFlags(aFlags)) {
131     return NS_ERROR_FAILURE;
132   }
133   mAttrs.persistFlags() = aFlags;
134   return NS_OK;
135 }
136 
137 NS_IMETHODIMP
GetPostData(nsIInputStream ** aStream)138 WebBrowserPersistRemoteDocument::GetPostData(nsIInputStream** aStream) {
139   nsCOMPtr<nsIInputStream> stream = mPostData;
140   stream.forget(aStream);
141   return NS_OK;
142 }
143 
144 NS_IMETHODIMP
GetPrincipal(nsIPrincipal ** aPrincipal)145 WebBrowserPersistRemoteDocument::GetPrincipal(nsIPrincipal** aPrincipal) {
146   nsCOMPtr<nsIPrincipal> nodePrincipal = mPrincipal;
147   nodePrincipal.forget(aPrincipal);
148   return NS_OK;
149 }
150 
151 NS_IMETHODIMP
ReadResources(nsIWebBrowserPersistResourceVisitor * aVisitor)152 WebBrowserPersistRemoteDocument::ReadResources(
153     nsIWebBrowserPersistResourceVisitor* aVisitor) {
154   if (!mActor) {
155     return NS_ERROR_FAILURE;
156   }
157   RefPtr<WebBrowserPersistResourcesParent> subActor =
158       new WebBrowserPersistResourcesParent(this, aVisitor);
159   return mActor->SendPWebBrowserPersistResourcesConstructor(
160              subActor.forget().take())
161              ? NS_OK
162              : NS_ERROR_FAILURE;
163 }
164 
165 NS_IMETHODIMP
WriteContent(nsIOutputStream * aStream,nsIWebBrowserPersistURIMap * aMap,const nsACString & aRequestedContentType,uint32_t aEncoderFlags,uint32_t aWrapColumn,nsIWebBrowserPersistWriteCompletion * aCompletion)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;
172   }
173 
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);
187     }
188   }
189 
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;
197 }
198 
199 // Forcing WebBrowserPersistRemoteDocument to implement GetHistory is the
200 // easiest way to ensure that we can call GetHistory in
201 // WebBrowserPersistDocumentChild::Start
GetHistory()202 already_AddRefed<nsISHEntry> WebBrowserPersistRemoteDocument::GetHistory() {
203   MOZ_CRASH("We should not call GetHistory on WebBrowserPersistRemoteDocument");
204   return nullptr;
205 }
206 
207 }  // namespace mozilla
208