Bug 1874684 - Part 4: Prefer const references instead of copying Instant values....
[gecko.git] / dom / webbrowserpersist / WebBrowserPersistSerializeChild.cpp
blob8c052f6eca2d8ec9f138986b946200f7e14380ec
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 "WebBrowserPersistSerializeChild.h"
9 #include <algorithm>
11 #include "nsThreadUtils.h"
13 namespace mozilla {
15 NS_IMPL_ISUPPORTS(WebBrowserPersistSerializeChild,
16 nsIWebBrowserPersistWriteCompletion,
17 nsIWebBrowserPersistURIMap, nsIOutputStream)
19 WebBrowserPersistSerializeChild::WebBrowserPersistSerializeChild(
20 const WebBrowserPersistURIMap& aMap)
21 : mMap(aMap) {}
23 WebBrowserPersistSerializeChild::~WebBrowserPersistSerializeChild() = default;
25 NS_IMETHODIMP
26 WebBrowserPersistSerializeChild::OnFinish(
27 nsIWebBrowserPersistDocument* aDocument, nsIOutputStream* aStream,
28 const nsACString& aContentType, nsresult aStatus) {
29 MOZ_ASSERT(aStream == this);
30 nsCString contentType(aContentType);
31 Send__delete__(this, contentType, aStatus);
32 return NS_OK;
35 NS_IMETHODIMP
36 WebBrowserPersistSerializeChild::GetNumMappedURIs(uint32_t* aNum) {
37 *aNum = static_cast<uint32_t>(mMap.mapURIs().Length());
38 return NS_OK;
41 NS_IMETHODIMP
42 WebBrowserPersistSerializeChild::GetURIMapping(uint32_t aIndex,
43 nsACString& aMapFrom,
44 nsACString& aMapTo) {
45 if (aIndex >= mMap.mapURIs().Length()) {
46 return NS_ERROR_INVALID_ARG;
48 aMapFrom = mMap.mapURIs()[aIndex].mapFrom();
49 aMapTo = mMap.mapURIs()[aIndex].mapTo();
50 return NS_OK;
53 NS_IMETHODIMP
54 WebBrowserPersistSerializeChild::GetTargetBaseURI(nsACString& aURI) {
55 aURI = mMap.targetBaseURI();
56 return NS_OK;
59 NS_IMETHODIMP
60 WebBrowserPersistSerializeChild::Close() {
61 NS_WARNING("WebBrowserPersistSerializeChild::Close()");
62 return NS_ERROR_NOT_IMPLEMENTED;
65 NS_IMETHODIMP
66 WebBrowserPersistSerializeChild::Flush() {
67 NS_WARNING("WebBrowserPersistSerializeChild::Flush()");
68 return NS_ERROR_NOT_IMPLEMENTED;
71 NS_IMETHODIMP
72 WebBrowserPersistSerializeChild::StreamStatus() {
73 // XXX: This stream doesn't appear to have a closed state.
74 return NS_OK;
77 NS_IMETHODIMP
78 WebBrowserPersistSerializeChild::Write(const char* aBuf, uint32_t aCount,
79 uint32_t* aWritten) {
80 // Normally an nsIOutputStream would have to be thread-safe, but
81 // nsDocumentEncoder currently doesn't call this off the main
82 // thread (which also means it's difficult to test the
83 // thread-safety code this class doesn't yet have).
85 // This is *not* an NS_ERROR_NOT_IMPLEMENTED, because at this
86 // point we've probably already misused the non-thread-safe
87 // refcounting.
88 MOZ_RELEASE_ASSERT(NS_IsMainThread(), "Fix this class to be thread-safe.");
90 // Limit the message size to 64k because large messages are
91 // potentially bad for the latency of other messages on the same channel.
92 static const uint32_t kMaxWrite = 65536;
94 // Work around bug 1181433 by sending multiple messages if
95 // necessary to write the entire aCount bytes, even though
96 // nsIOutputStream.idl says we're allowed to do a short write.
97 const char* buf = aBuf;
98 uint32_t count = aCount;
99 *aWritten = 0;
100 while (count > 0) {
101 uint32_t toWrite = std::min(kMaxWrite, count);
102 nsTArray<uint8_t> arrayBuf;
103 // It would be nice if this extra copy could be avoided.
104 arrayBuf.AppendElements(buf, toWrite);
105 SendWriteData(std::move(arrayBuf));
106 *aWritten += toWrite;
107 buf += toWrite;
108 count -= toWrite;
110 return NS_OK;
113 NS_IMETHODIMP
114 WebBrowserPersistSerializeChild::WriteFrom(nsIInputStream* aFrom,
115 uint32_t aCount,
116 uint32_t* aWritten) {
117 NS_WARNING("WebBrowserPersistSerializeChild::WriteFrom()");
118 return NS_ERROR_NOT_IMPLEMENTED;
121 NS_IMETHODIMP
122 WebBrowserPersistSerializeChild::WriteSegments(nsReadSegmentFun aFun,
123 void* aCtx, uint32_t aCount,
124 uint32_t* aWritten) {
125 NS_WARNING("WebBrowserPersistSerializeChild::WriteSegments()");
126 return NS_ERROR_NOT_IMPLEMENTED;
129 NS_IMETHODIMP
130 WebBrowserPersistSerializeChild::IsNonBlocking(bool* aNonBlocking) {
131 // Writes will never fail with NS_BASE_STREAM_WOULD_BLOCK, so:
132 *aNonBlocking = false;
133 return NS_OK;
136 } // namespace mozilla