2019-07-05 20:05:57 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2016-04-11 06:17:02 +03:00
|
|
|
#include "mozilla/net/AltDataOutputStreamChild.h"
|
|
|
|
#include "mozilla/Unused.h"
|
|
|
|
#include "nsIInputStream.h"
|
2019-02-27 23:50:48 +03:00
|
|
|
#include "nsStreamUtils.h"
|
2016-04-11 06:17:02 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(AltDataOutputStreamChild)
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType) AltDataOutputStreamChild::Release() {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(0 != mRefCnt, "dup release");
|
2016-04-11 06:17:02 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Main thread only");
|
|
|
|
--mRefCnt;
|
|
|
|
NS_LOG_RELEASE(this, mRefCnt, "AltDataOutputStreamChild");
|
|
|
|
|
|
|
|
if (mRefCnt == 1 && mIPCOpen) {
|
2017-05-11 22:35:37 +03:00
|
|
|
// The only reference left is the IPDL one. After the parent replies back
|
|
|
|
// with a DeleteSelf message, the child will call Send__delete__(this),
|
|
|
|
// decrementing the ref count and triggering the destructor.
|
|
|
|
SendDeleteSelf();
|
|
|
|
return 1;
|
2016-04-11 06:17:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mRefCnt == 0) {
|
|
|
|
mRefCnt = 1; /* stabilize */
|
|
|
|
delete this;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return mRefCnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(AltDataOutputStreamChild)
|
2019-02-27 23:50:48 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIAsyncOutputStream)
|
2016-04-11 06:17:02 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIOutputStream)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
AltDataOutputStreamChild::AltDataOutputStreamChild()
|
2019-02-27 23:50:48 +03:00
|
|
|
: mIPCOpen(false), mError(NS_OK), mCallbackFlags(0) {
|
2016-04-11 06:17:02 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Main thread only");
|
|
|
|
}
|
|
|
|
|
|
|
|
void AltDataOutputStreamChild::AddIPDLReference() {
|
|
|
|
MOZ_ASSERT(!mIPCOpen, "Attempt to retain more than one IPDL reference");
|
|
|
|
mIPCOpen = true;
|
|
|
|
AddRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AltDataOutputStreamChild::ReleaseIPDLReference() {
|
|
|
|
MOZ_ASSERT(mIPCOpen, "Attempt to release nonexistent IPDL reference");
|
|
|
|
mIPCOpen = false;
|
2019-02-27 23:50:48 +03:00
|
|
|
|
|
|
|
if (mCallback) {
|
|
|
|
NotifyListener();
|
|
|
|
}
|
|
|
|
|
2016-04-11 06:17:02 +03:00
|
|
|
Release();
|
|
|
|
}
|
|
|
|
|
2018-09-25 19:56:16 +03:00
|
|
|
bool AltDataOutputStreamChild::WriteDataInChunks(
|
|
|
|
const nsDependentCSubstring& data) {
|
2016-04-11 06:17:02 +03:00
|
|
|
const uint32_t kChunkSize = 128 * 1024;
|
|
|
|
uint32_t next = std::min(data.Length(), kChunkSize);
|
|
|
|
for (uint32_t i = 0; i < data.Length();
|
|
|
|
i = next, next = std::min(data.Length(), next + kChunkSize)) {
|
|
|
|
nsCString chunk(Substring(data, i, kChunkSize));
|
|
|
|
if (mIPCOpen && !SendWriteData(chunk)) {
|
|
|
|
mIPCOpen = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-02-27 23:50:48 +03:00
|
|
|
AltDataOutputStreamChild::Close() { return CloseWithStatus(NS_OK); }
|
2016-04-11 06:17:02 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AltDataOutputStreamChild::Flush() {
|
|
|
|
if (!mIPCOpen) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(mError)) {
|
|
|
|
return mError;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a no-op
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AltDataOutputStreamChild::Write(const char* aBuf, uint32_t aCount,
|
|
|
|
uint32_t* _retval) {
|
|
|
|
if (!mIPCOpen) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(mError)) {
|
|
|
|
return mError;
|
|
|
|
}
|
2018-09-25 19:56:16 +03:00
|
|
|
if (WriteDataInChunks(nsDependentCSubstring(aBuf, aCount))) {
|
2016-04-11 06:17:02 +03:00
|
|
|
*_retval = aCount;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AltDataOutputStreamChild::WriteFrom(nsIInputStream* aFromStream,
|
|
|
|
uint32_t aCount, uint32_t* _retval) {
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AltDataOutputStreamChild::WriteSegments(nsReadSegmentFun aReader,
|
|
|
|
void* aClosure, uint32_t aCount,
|
|
|
|
uint32_t* _retval) {
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AltDataOutputStreamChild::IsNonBlocking(bool* _retval) {
|
|
|
|
*_retval = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::ipc::IPCResult AltDataOutputStreamChild::RecvError(
|
|
|
|
const nsresult& err) {
|
|
|
|
mError = err;
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2016-04-11 06:17:02 +03:00
|
|
|
}
|
|
|
|
|
2017-05-11 22:35:37 +03:00
|
|
|
mozilla::ipc::IPCResult AltDataOutputStreamChild::RecvDeleteSelf() {
|
|
|
|
PAltDataOutputStreamChild::Send__delete__(this);
|
|
|
|
return IPC_OK();
|
|
|
|
}
|
2016-04-11 06:17:02 +03:00
|
|
|
|
2019-02-27 23:50:48 +03:00
|
|
|
// nsIAsyncOutputStream
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AltDataOutputStreamChild::CloseWithStatus(nsresult aStatus) {
|
|
|
|
if (!mIPCOpen) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(mError)) {
|
|
|
|
return mError;
|
|
|
|
}
|
|
|
|
Unused << SendClose(aStatus);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AltDataOutputStreamChild::AsyncWait(nsIOutputStreamCallback* aCallback,
|
|
|
|
uint32_t aFlags, uint32_t aRequestedCount,
|
|
|
|
nsIEventTarget* aEventTarget) {
|
|
|
|
mCallback = aCallback;
|
|
|
|
mCallbackFlags = aFlags;
|
|
|
|
mCallbackTarget = aEventTarget;
|
|
|
|
|
|
|
|
if (!mCallback) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The stream is blocking so it is writable at any time
|
|
|
|
if (!mIPCOpen || !(aFlags & WAIT_CLOSURE_ONLY)) {
|
|
|
|
NotifyListener();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AltDataOutputStreamChild::NotifyListener() {
|
|
|
|
MOZ_ASSERT(mCallback);
|
|
|
|
|
|
|
|
if (!mCallbackTarget) {
|
|
|
|
mCallbackTarget = GetMainThreadEventTarget();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIOutputStreamCallback> asyncCallback =
|
|
|
|
NS_NewOutputStreamReadyEvent(mCallback, mCallbackTarget);
|
|
|
|
|
|
|
|
mCallback = nullptr;
|
|
|
|
mCallbackTarget = nullptr;
|
|
|
|
|
|
|
|
asyncCallback->OnOutputStreamReady(this);
|
|
|
|
}
|
|
|
|
|
2016-04-11 06:17:02 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|