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 "WebBrowserPersistSerializeChild.h"
8 
9 #include <algorithm>
10 
11 #include "nsThreadUtils.h"
12 #include "ipc/IPCMessageUtils.h"
13 
14 namespace mozilla {
15 
NS_IMPL_ISUPPORTS(WebBrowserPersistSerializeChild,nsIWebBrowserPersistWriteCompletion,nsIWebBrowserPersistURIMap,nsIOutputStream)16 NS_IMPL_ISUPPORTS(WebBrowserPersistSerializeChild,
17                   nsIWebBrowserPersistWriteCompletion,
18                   nsIWebBrowserPersistURIMap, nsIOutputStream)
19 
20 WebBrowserPersistSerializeChild::WebBrowserPersistSerializeChild(
21     const WebBrowserPersistURIMap& aMap)
22     : mMap(aMap) {}
23 
24 WebBrowserPersistSerializeChild::~WebBrowserPersistSerializeChild() = default;
25 
26 NS_IMETHODIMP
OnFinish(nsIWebBrowserPersistDocument * aDocument,nsIOutputStream * aStream,const nsACString & aContentType,nsresult aStatus)27 WebBrowserPersistSerializeChild::OnFinish(
28     nsIWebBrowserPersistDocument* aDocument, nsIOutputStream* aStream,
29     const nsACString& aContentType, nsresult aStatus) {
30   MOZ_ASSERT(aStream == this);
31   nsCString contentType(aContentType);
32   Send__delete__(this, contentType, aStatus);
33   return NS_OK;
34 }
35 
36 NS_IMETHODIMP
GetNumMappedURIs(uint32_t * aNum)37 WebBrowserPersistSerializeChild::GetNumMappedURIs(uint32_t* aNum) {
38   *aNum = static_cast<uint32_t>(mMap.mapURIs().Length());
39   return NS_OK;
40 }
41 
42 NS_IMETHODIMP
GetURIMapping(uint32_t aIndex,nsACString & aMapFrom,nsACString & aMapTo)43 WebBrowserPersistSerializeChild::GetURIMapping(uint32_t aIndex,
44                                                nsACString& aMapFrom,
45                                                nsACString& aMapTo) {
46   if (aIndex >= mMap.mapURIs().Length()) {
47     return NS_ERROR_INVALID_ARG;
48   }
49   aMapFrom = mMap.mapURIs()[aIndex].mapFrom();
50   aMapTo = mMap.mapURIs()[aIndex].mapTo();
51   return NS_OK;
52 }
53 
54 NS_IMETHODIMP
GetTargetBaseURI(nsACString & aURI)55 WebBrowserPersistSerializeChild::GetTargetBaseURI(nsACString& aURI) {
56   aURI = mMap.targetBaseURI();
57   return NS_OK;
58 }
59 
60 NS_IMETHODIMP
Close()61 WebBrowserPersistSerializeChild::Close() {
62   NS_WARNING("WebBrowserPersistSerializeChild::Close()");
63   return NS_ERROR_NOT_IMPLEMENTED;
64 }
65 
66 NS_IMETHODIMP
Flush()67 WebBrowserPersistSerializeChild::Flush() {
68   NS_WARNING("WebBrowserPersistSerializeChild::Flush()");
69   return NS_ERROR_NOT_IMPLEMENTED;
70 }
71 
72 NS_IMETHODIMP
Write(const char * aBuf,uint32_t aCount,uint32_t * aWritten)73 WebBrowserPersistSerializeChild::Write(const char* aBuf, uint32_t aCount,
74                                        uint32_t* aWritten) {
75   // Normally an nsIOutputStream would have to be thread-safe, but
76   // nsDocumentEncoder currently doesn't call this off the main
77   // thread (which also means it's difficult to test the
78   // thread-safety code this class doesn't yet have).
79   //
80   // This is *not* an NS_ERROR_NOT_IMPLEMENTED, because at this
81   // point we've probably already misused the non-thread-safe
82   // refcounting.
83   MOZ_RELEASE_ASSERT(NS_IsMainThread(), "Fix this class to be thread-safe.");
84 
85   // Work around bug 1181433 by sending multiple messages if
86   // necessary to write the entire aCount bytes, even though
87   // nsIOutputStream.idl says we're allowed to do a short write.
88   const char* buf = aBuf;
89   uint32_t count = aCount;
90   *aWritten = 0;
91   while (count > 0) {
92     uint32_t toWrite = std::min(IPC::MAX_MESSAGE_SIZE, count);
93     nsTArray<uint8_t> arrayBuf;
94     // It would be nice if this extra copy could be avoided.
95     arrayBuf.AppendElements(buf, toWrite);
96     SendWriteData(Move(arrayBuf));
97     *aWritten += toWrite;
98     buf += toWrite;
99     count -= toWrite;
100   }
101   return NS_OK;
102 }
103 
104 NS_IMETHODIMP
WriteFrom(nsIInputStream * aFrom,uint32_t aCount,uint32_t * aWritten)105 WebBrowserPersistSerializeChild::WriteFrom(nsIInputStream* aFrom,
106                                            uint32_t aCount,
107                                            uint32_t* aWritten) {
108   NS_WARNING("WebBrowserPersistSerializeChild::WriteFrom()");
109   return NS_ERROR_NOT_IMPLEMENTED;
110 }
111 
112 NS_IMETHODIMP
WriteSegments(nsReadSegmentFun aFun,void * aCtx,uint32_t aCount,uint32_t * aWritten)113 WebBrowserPersistSerializeChild::WriteSegments(nsReadSegmentFun aFun,
114                                                void* aCtx, uint32_t aCount,
115                                                uint32_t* aWritten) {
116   NS_WARNING("WebBrowserPersistSerializeChild::WriteSegments()");
117   return NS_ERROR_NOT_IMPLEMENTED;
118 }
119 
120 NS_IMETHODIMP
IsNonBlocking(bool * aNonBlocking)121 WebBrowserPersistSerializeChild::IsNonBlocking(bool* aNonBlocking) {
122   // Writes will never fail with NS_BASE_STREAM_WOULD_BLOCK, so:
123   *aNonBlocking = false;
124   return NS_OK;
125 }
126 
127 }  // namespace mozilla
128