/src/mozilla-central/netwerk/protocol/http/AltDataOutputStreamChild.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include "mozilla/net/AltDataOutputStreamChild.h" |
2 | | #include "mozilla/Unused.h" |
3 | | #include "nsIInputStream.h" |
4 | | |
5 | | namespace mozilla { |
6 | | namespace net { |
7 | | |
8 | | NS_IMPL_ADDREF(AltDataOutputStreamChild) |
9 | | |
10 | | NS_IMETHODIMP_(MozExternalRefCountType) AltDataOutputStreamChild::Release() |
11 | 0 | { |
12 | 0 | MOZ_ASSERT(0 != mRefCnt, "dup release"); |
13 | 0 | MOZ_ASSERT(NS_IsMainThread(), "Main thread only"); |
14 | 0 | --mRefCnt; |
15 | 0 | NS_LOG_RELEASE(this, mRefCnt, "AltDataOutputStreamChild"); |
16 | 0 |
|
17 | 0 | if (mRefCnt == 1 && mIPCOpen) { |
18 | 0 | // The only reference left is the IPDL one. After the parent replies back |
19 | 0 | // with a DeleteSelf message, the child will call Send__delete__(this), |
20 | 0 | // decrementing the ref count and triggering the destructor. |
21 | 0 | SendDeleteSelf(); |
22 | 0 | return 1; |
23 | 0 | } |
24 | 0 | |
25 | 0 | if (mRefCnt == 0) { |
26 | 0 | mRefCnt = 1; /* stabilize */ |
27 | 0 | delete this; |
28 | 0 | return 0; |
29 | 0 | } |
30 | 0 | return mRefCnt; |
31 | 0 | } |
32 | | |
33 | 0 | NS_INTERFACE_MAP_BEGIN(AltDataOutputStreamChild) |
34 | 0 | NS_INTERFACE_MAP_ENTRY(nsIOutputStream) |
35 | 0 | NS_INTERFACE_MAP_ENTRY(nsISupports) |
36 | 0 | NS_INTERFACE_MAP_END |
37 | | |
38 | | AltDataOutputStreamChild::AltDataOutputStreamChild() |
39 | | : mIPCOpen(false) |
40 | | , mError(NS_OK) |
41 | 0 | { |
42 | 0 | MOZ_ASSERT(NS_IsMainThread(), "Main thread only"); |
43 | 0 | } |
44 | | |
45 | | void |
46 | | AltDataOutputStreamChild::AddIPDLReference() |
47 | 0 | { |
48 | 0 | MOZ_ASSERT(!mIPCOpen, "Attempt to retain more than one IPDL reference"); |
49 | 0 | mIPCOpen = true; |
50 | 0 | AddRef(); |
51 | 0 | } |
52 | | |
53 | | void |
54 | | AltDataOutputStreamChild::ReleaseIPDLReference() |
55 | 0 | { |
56 | 0 | MOZ_ASSERT(mIPCOpen, "Attempt to release nonexistent IPDL reference"); |
57 | 0 | mIPCOpen = false; |
58 | 0 | Release(); |
59 | 0 | } |
60 | | |
61 | | bool |
62 | | AltDataOutputStreamChild::WriteDataInChunks(const nsCString& data) |
63 | 0 | { |
64 | 0 | const uint32_t kChunkSize = 128*1024; |
65 | 0 | uint32_t next = std::min(data.Length(), kChunkSize); |
66 | 0 | for (uint32_t i = 0; i < data.Length(); |
67 | 0 | i = next, next = std::min(data.Length(), next + kChunkSize)) { |
68 | 0 | nsCString chunk(Substring(data, i, kChunkSize)); |
69 | 0 | if (mIPCOpen && !SendWriteData(chunk)) { |
70 | 0 | mIPCOpen = false; |
71 | 0 | return false; |
72 | 0 | } |
73 | 0 | } |
74 | 0 | return true; |
75 | 0 | } |
76 | | |
77 | | NS_IMETHODIMP |
78 | | AltDataOutputStreamChild::Close() |
79 | 0 | { |
80 | 0 | if (!mIPCOpen) { |
81 | 0 | return NS_ERROR_NOT_AVAILABLE; |
82 | 0 | } |
83 | 0 | if (NS_FAILED(mError)) { |
84 | 0 | return mError; |
85 | 0 | } |
86 | 0 | Unused << SendClose(); |
87 | 0 | return NS_OK; |
88 | 0 | } |
89 | | |
90 | | NS_IMETHODIMP |
91 | | AltDataOutputStreamChild::Flush() |
92 | 0 | { |
93 | 0 | if (!mIPCOpen) { |
94 | 0 | return NS_ERROR_NOT_AVAILABLE; |
95 | 0 | } |
96 | 0 | if (NS_FAILED(mError)) { |
97 | 0 | return mError; |
98 | 0 | } |
99 | 0 | |
100 | 0 | // This is a no-op |
101 | 0 | return NS_OK; |
102 | 0 | } |
103 | | |
104 | | NS_IMETHODIMP |
105 | | AltDataOutputStreamChild::Write(const char * aBuf, uint32_t aCount, uint32_t *_retval) |
106 | 0 | { |
107 | 0 | if (!mIPCOpen) { |
108 | 0 | return NS_ERROR_NOT_AVAILABLE; |
109 | 0 | } |
110 | 0 | if (NS_FAILED(mError)) { |
111 | 0 | return mError; |
112 | 0 | } |
113 | 0 | if (WriteDataInChunks(nsCString(aBuf, aCount))) { |
114 | 0 | *_retval = aCount; |
115 | 0 | return NS_OK; |
116 | 0 | } |
117 | 0 | return NS_ERROR_FAILURE; |
118 | 0 | } |
119 | | |
120 | | NS_IMETHODIMP |
121 | | AltDataOutputStreamChild::WriteFrom(nsIInputStream *aFromStream, uint32_t aCount, uint32_t *_retval) |
122 | 0 | { |
123 | 0 | return NS_ERROR_NOT_IMPLEMENTED; |
124 | 0 | } |
125 | | |
126 | | NS_IMETHODIMP |
127 | | AltDataOutputStreamChild::WriteSegments(nsReadSegmentFun aReader, void *aClosure, uint32_t aCount, uint32_t *_retval) |
128 | 0 | { |
129 | 0 | return NS_ERROR_NOT_IMPLEMENTED; |
130 | 0 | } |
131 | | |
132 | | NS_IMETHODIMP |
133 | | AltDataOutputStreamChild::IsNonBlocking(bool *_retval) |
134 | 0 | { |
135 | 0 | *_retval = false; |
136 | 0 | return NS_OK; |
137 | 0 | } |
138 | | |
139 | | mozilla::ipc::IPCResult |
140 | | AltDataOutputStreamChild::RecvError(const nsresult& err) |
141 | 0 | { |
142 | 0 | mError = err; |
143 | 0 | return IPC_OK(); |
144 | 0 | } |
145 | | |
146 | | mozilla::ipc::IPCResult |
147 | | AltDataOutputStreamChild::RecvDeleteSelf() |
148 | 0 | { |
149 | 0 | PAltDataOutputStreamChild::Send__delete__(this); |
150 | 0 | return IPC_OK(); |
151 | 0 | } |
152 | | |
153 | | } // namespace net |
154 | | } // namespace mozilla |