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