Files
palemoon27/dom/presentation/PresentationSessionTransport.cpp
roytam1 38663d2b9f import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1230163 - White-space copy issue: Add conditional compile also for SeaMonkey. r=ehsan (6c6cf03257)
- Bug 1066663, move call to SetDragState to happen when a window is lowered to avoid the issue with selection being prevented, r=smaug (7a15008ac3)
- Bug 1245452, null check window->GetDocShell return value, r=khuey (77fee59e52)
- Bug 1168398 - Ensure that the presshell is initialized before attempting to scroll something into its view; r=enndeakin (4aba157104)
- Bug 1227461, don't iterate multiple times past the top-level document during tab navigation, prevents hang navigating in page info dialog, r=smaug (0a69f5085d)
- Bug 1263443 - Typo in nsGkAtomList.h; r=smaug (9a0b34d075)
- Bug 1260480 - Remove mozNoMarginBoxes. r=dbaron (fb06487de9)
- Bug 1254378 - Make SpeechSynthesis an event target with a "voiceschanged" event. r=smaug (8d00157559)
- some missing bits to be reapplied (e046af733d)
- Bug 1097398 Part 1 - Move AccessibleCaret default assets to layout/style/. r=nalexander, r=heycam (694c4617d2)
- Bug 1097398 Part 2 - Add preferences to make carets always tilt. r=roc (61ccfa508e)
- Bug 1252368 - Move resource files in layout/style/ into layout/style/res/ directory. r=nalexander (6049fec5ba)
- partial of Bug 1258177 - Make the list-item markers non-selectable. r=bz (9387a5a1f7)
- Bug 1259889 Part 2 - Load html.css lazily for pref changes. r=heycam (16f139a9b4)
- Bug 1258657 Part 1 - Change summary default style to "display: list-item" to match html spec. r=bz (a90a87025c)
- Bug 1258657 Part 2 - Remove SummaryFrame. r=bz (7e555a570e)
- Bug 1236828 part 1 - Apply proper clip state to top layer frames. r=mstange (5f3b55e475)
- Bug 1236828 part 2 - Make frame constructor support absolutely-positioned top layer frame. r=bz (052f7d5d71)
- Bug 1236828 part 3 - Allow setting position: absolute for top layer element and add test. r=heycam (ddf55b2e57)
- Bug 1264533 - Correct the universal selector for summary to include all namespaces. r=bz (4bd53c11cc)
- simple formatting (f19a2351a2)
- Bug 1235321 - Enable using line cursor to optimize reflowing absolute frames. r=bz (35199c6780)
- Bug 1260090 - Remove nsBlockFrameSuper as nsContainerFrame alias. r=dholbert (dbf60b2ef2)
- minor format fix (a057efcf1c)
- minor format fix (ac1f3b6691)
- Bug 1145218 followup - Properly parenthesize macro argument. No review. (6a5d9fcf64)
- Bug 1256727 - Back out the functional parts of cset 59a1ca7d784c now that we have a better fix using empty transactions. r=me (007080dd0a)
- Bug 1256040 - Follow-up fix for nsGridContainerFrame.h/cpp compile errors in non-unified build. r=me (9c08e31572)
- Bug 1243125 - patch 0 - Relax overly-harsh writing mode assertions in nsReflowMetrics size accessors. r=dholbert (3f75521c74)
- Bug 1243125 - patch 1 - Check block size instead of height when detecting truncated frames. r=jfkthame (66a36d7c35)
- Bug 1243125 - patch 2 - Don't ever set NS_FRAME_TRUNCATED for orthogonal flows. r=dholbert (ce4650213c)
- Bug 1243125 - Reftest for floats overflowing container, with diverse writing-modes. r=jfkthame (f621d8d17e)
- Bug 1250674 - Don't print header/footer if larger than user defined margins. r=dbaron (604e4c8f28)
- Bug 1182247 - Only RegisterPluginForGeometryUpdates in nsPluginFrame::EndSwapDocShells if mInstanceOwner is non-null. r=tnikkel (8958766501)
- fix misspatch compared to esr52 (06be2eb39c)
- Bug 1237236 - When collapsing the selection to the end of textframe that ends in a significant newline then set mHint to CARET_ASSOCIATE_AFTER so that the caret is rendered at the start of the next line if any. r=ehsan (1f86bf7a57)
- Bug 1258308 - Lookup the frame (continuation) associated with aParentNode/aOffset instead of assuming it's always the primary frame (follow-up from bug 1237236). r=ehsa (8849eecf94)
- Bug 1259949 - Set the CARET_ASSOCIATE_AFTER hint also when we collapse just after a text node that ends with a significant newline. r=ehsan (065b4f6c9a)
- Bug 1261718. In nsSubDocumentFrame when getting the document from a view go through the view manager because it is more like to succeed. r=mats (508f485aeb)
- Bug 1178060 - Ensure that if there is a document-level apz-aware event listener on a subdocument, we build the nsDisplaySubdocument item to hold that information. r=tn (8e235581a5)
- Bug 1240799 - Apply axis height for mfrac elements without bar. r=fredw (6a5fb4a3fa)
- Bug 1224113 - fix ref count issue during channel close. r=jdm (35c5a01787)
2024-08-02 16:52:24 +08:00

501 lines
13 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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/. */
#include "nsArrayUtils.h"
#include "nsIAsyncStreamCopier.h"
#include "nsIInputStreamPump.h"
#include "nsIMultiplexInputStream.h"
#include "nsIMutableArray.h"
#include "nsIOutputStream.h"
#include "nsIPresentationControlChannel.h"
#include "nsIScriptableInputStream.h"
#include "nsISocketTransport.h"
#include "nsISocketTransportService.h"
#include "nsISupportsPrimitives.h"
#include "nsNetUtil.h"
#include "nsServiceManagerUtils.h"
#include "nsStreamUtils.h"
#include "nsThreadUtils.h"
#include "PresentationSessionTransport.h"
#define BUFFER_SIZE 65536
using namespace mozilla;
using namespace mozilla::dom;
class CopierCallbacks final : public nsIRequestObserver
{
public:
explicit CopierCallbacks(PresentationSessionTransport* aTransport)
: mOwner(aTransport)
{}
NS_DECL_ISUPPORTS
NS_DECL_NSIREQUESTOBSERVER
private:
~CopierCallbacks() {}
RefPtr<PresentationSessionTransport> mOwner;
};
NS_IMPL_ISUPPORTS(CopierCallbacks, nsIRequestObserver)
NS_IMETHODIMP
CopierCallbacks::OnStartRequest(nsIRequest* aRequest, nsISupports* aContext)
{
return NS_OK;
}
NS_IMETHODIMP
CopierCallbacks::OnStopRequest(nsIRequest* aRequest, nsISupports* aContext, nsresult aStatus)
{
mOwner->NotifyCopyComplete(aStatus);
return NS_OK;
}
NS_IMPL_CYCLE_COLLECTION(PresentationSessionTransport, mTransport,
mSocketInputStream, mSocketOutputStream,
mInputStreamPump, mInputStreamScriptable,
mMultiplexStream, mMultiplexStreamCopier, mCallback)
NS_IMPL_CYCLE_COLLECTING_ADDREF(PresentationSessionTransport)
NS_IMPL_CYCLE_COLLECTING_RELEASE(PresentationSessionTransport)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PresentationSessionTransport)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIPresentationSessionTransport)
NS_INTERFACE_MAP_ENTRY(nsIPresentationSessionTransport)
NS_INTERFACE_MAP_ENTRY(nsITransportEventSink)
NS_INTERFACE_MAP_ENTRY(nsIInputStreamCallback)
NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
NS_INTERFACE_MAP_ENTRY(nsIRequestObserver)
NS_INTERFACE_MAP_END
PresentationSessionTransport::PresentationSessionTransport()
: mReadyState(CLOSED)
, mAsyncCopierActive(false)
, mCloseStatus(NS_OK)
, mDataNotificationEnabled(false)
{
}
PresentationSessionTransport::~PresentationSessionTransport()
{
}
NS_IMETHODIMP
PresentationSessionTransport::InitWithSocketTransport(nsISocketTransport* aTransport,
nsIPresentationSessionTransportCallback* aCallback)
{
if (NS_WARN_IF(!aCallback)) {
return NS_ERROR_INVALID_ARG;
}
mCallback = aCallback;
if (NS_WARN_IF(!aTransport)) {
return NS_ERROR_INVALID_ARG;
}
mTransport = aTransport;
nsresult rv = CreateStream();
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
SetReadyState(OPEN);
if (IsReadyToNotifyData()) {
return CreateInputStreamPump();
}
return NS_OK;
}
NS_IMETHODIMP
PresentationSessionTransport::InitWithChannelDescription(nsIPresentationChannelDescription* aDescription,
nsIPresentationSessionTransportCallback* aCallback)
{
if (NS_WARN_IF(!aCallback)) {
return NS_ERROR_INVALID_ARG;
}
mCallback = aCallback;
if (NS_WARN_IF(!aDescription)) {
return NS_ERROR_INVALID_ARG;
}
uint16_t serverPort;
nsresult rv = aDescription->GetTcpPort(&serverPort);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
nsCOMPtr<nsIArray> serverHosts;
rv = aDescription->GetTcpAddress(getter_AddRefs(serverHosts));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
// TODO bug 1148307 Implement PresentationSessionTransport with DataChannel.
// Ultimately we may use all the available addresses. DataChannel appears
// more robust upon handling ICE. And at the first stage Presentation API is
// only exposed on Firefox OS where the first IP appears enough for most
// scenarios.
nsCOMPtr<nsISupportsCString> supportStr = do_QueryElementAt(serverHosts, 0);
if (NS_WARN_IF(!supportStr)) {
return NS_ERROR_INVALID_ARG;
}
nsAutoCString serverHost;
supportStr->GetData(serverHost);
if (serverHost.IsEmpty()) {
return NS_ERROR_INVALID_ARG;
}
SetReadyState(CONNECTING);
nsCOMPtr<nsISocketTransportService> sts =
do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID);
if (NS_WARN_IF(!sts)) {
return NS_ERROR_NOT_AVAILABLE;
}
rv = sts->CreateTransport(nullptr, 0, serverHost, serverPort, nullptr,
getter_AddRefs(mTransport));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
nsCOMPtr<nsIThread> mainThread;
NS_GetMainThread(getter_AddRefs(mainThread));
mTransport->SetEventSink(this, mainThread);
rv = CreateStream();
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
nsresult
PresentationSessionTransport::CreateStream()
{
nsresult rv = mTransport->OpenInputStream(0, 0, 0, getter_AddRefs(mSocketInputStream));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
rv = mTransport->OpenOutputStream(nsITransport::OPEN_UNBUFFERED, 0, 0, getter_AddRefs(mSocketOutputStream));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
// If the other side is not listening, we will get an |onInputStreamReady|
// callback where |available| raises to indicate the connection was refused.
nsCOMPtr<nsIAsyncInputStream> asyncStream = do_QueryInterface(mSocketInputStream);
if (NS_WARN_IF(!asyncStream)) {
return NS_ERROR_NOT_AVAILABLE;
}
nsCOMPtr<nsIThread> mainThread;
NS_GetMainThread(getter_AddRefs(mainThread));
rv = asyncStream->AsyncWait(this, nsIAsyncInputStream::WAIT_CLOSURE_ONLY, 0, mainThread);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
mInputStreamScriptable = do_CreateInstance("@mozilla.org/scriptableinputstream;1", &rv);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
rv = mInputStreamScriptable->Init(mSocketInputStream);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
mMultiplexStream = do_CreateInstance("@mozilla.org/io/multiplex-input-stream;1", &rv);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
mMultiplexStreamCopier = do_CreateInstance("@mozilla.org/network/async-stream-copier;1", &rv);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
nsCOMPtr<nsISocketTransportService> sts =
do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID);
if (NS_WARN_IF(!sts)) {
return NS_ERROR_NOT_AVAILABLE;
}
nsCOMPtr<nsIEventTarget> target = do_QueryInterface(sts);
rv = mMultiplexStreamCopier->Init(mMultiplexStream,
mSocketOutputStream,
target,
true, /* source buffered */
false, /* sink buffered */
BUFFER_SIZE,
false, /* close source */
false); /* close sink */
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
nsresult
PresentationSessionTransport::CreateInputStreamPump()
{
nsresult rv;
mInputStreamPump = do_CreateInstance(NS_INPUTSTREAMPUMP_CONTRACTID, &rv);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
rv = mInputStreamPump->Init(mSocketInputStream, -1, -1, 0, 0, false);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
rv = mInputStreamPump->AsyncRead(this, nullptr);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
NS_IMETHODIMP
PresentationSessionTransport::EnableDataNotification()
{
if (NS_WARN_IF(!mCallback)) {
return NS_ERROR_DOM_INVALID_STATE_ERR;
}
if (mDataNotificationEnabled) {
return NS_OK;
}
mDataNotificationEnabled = true;
if (IsReadyToNotifyData()) {
return CreateInputStreamPump();
}
return NS_OK;
}
NS_IMETHODIMP
PresentationSessionTransport::GetSelfAddress(nsINetAddr** aSelfAddress)
{
if (NS_WARN_IF(!mTransport)) {
return NS_ERROR_DOM_INVALID_STATE_ERR;
}
return mTransport->GetScriptableSelfAddr(aSelfAddress);
}
void
PresentationSessionTransport::EnsureCopying()
{
if (mAsyncCopierActive) {
return;
}
mAsyncCopierActive = true;
RefPtr<CopierCallbacks> callbacks = new CopierCallbacks(this);
NS_WARN_IF(NS_FAILED(mMultiplexStreamCopier->AsyncCopy(callbacks, nullptr)));
}
void
PresentationSessionTransport::NotifyCopyComplete(nsresult aStatus)
{
mAsyncCopierActive = false;
mMultiplexStream->RemoveStream(0);
if (NS_WARN_IF(NS_FAILED(aStatus))) {
if (mReadyState != CLOSED) {
mCloseStatus = aStatus;
SetReadyState(CLOSED);
}
return;
}
uint32_t count;
nsresult rv = mMultiplexStream->GetCount(&count);
if (NS_WARN_IF(NS_FAILED(rv))) {
return;
}
if (count) {
EnsureCopying();
return;
}
if (mReadyState == CLOSING) {
mSocketOutputStream->Close();
mCloseStatus = NS_OK;
SetReadyState(CLOSED);
}
}
NS_IMETHODIMP
PresentationSessionTransport::Send(nsIInputStream* aData)
{
if (NS_WARN_IF(mReadyState != OPEN)) {
return NS_ERROR_DOM_INVALID_STATE_ERR;
}
mMultiplexStream->AppendStream(aData);
EnsureCopying();
return NS_OK;
}
NS_IMETHODIMP
PresentationSessionTransport::Close(nsresult aReason)
{
if (mReadyState == CLOSED || mReadyState == CLOSING) {
return NS_OK;
}
mCloseStatus = aReason;
SetReadyState(CLOSING);
uint32_t count = 0;
mMultiplexStream->GetCount(&count);
if (!count) {
mSocketOutputStream->Close();
}
mSocketInputStream->Close();
mDataNotificationEnabled = false;
return NS_OK;
}
void
PresentationSessionTransport::SetReadyState(ReadyState aReadyState)
{
mReadyState = aReadyState;
if (mReadyState == OPEN && mCallback) {
// Notify the transport channel is ready.
NS_WARN_IF(NS_FAILED(mCallback->NotifyTransportReady()));
} else if (mReadyState == CLOSED && mCallback) {
// Notify the transport channel has been shut down.
NS_WARN_IF(NS_FAILED(mCallback->NotifyTransportClosed(mCloseStatus)));
mCallback = nullptr;
}
}
// nsITransportEventSink
NS_IMETHODIMP
PresentationSessionTransport::OnTransportStatus(nsITransport* aTransport,
nsresult aStatus,
int64_t aProgress,
int64_t aProgressMax)
{
MOZ_ASSERT(NS_IsMainThread());
if (aStatus != NS_NET_STATUS_CONNECTED_TO) {
return NS_OK;
}
SetReadyState(OPEN);
if (IsReadyToNotifyData()) {
return CreateInputStreamPump();
}
return NS_OK;
}
// nsIInputStreamCallback
NS_IMETHODIMP
PresentationSessionTransport::OnInputStreamReady(nsIAsyncInputStream* aStream)
{
MOZ_ASSERT(NS_IsMainThread());
// Only used for detecting if the connection was refused.
uint64_t dummy;
nsresult rv = aStream->Available(&dummy);
if (NS_WARN_IF(NS_FAILED(rv))) {
if (mReadyState != CLOSED) {
mCloseStatus = NS_ERROR_CONNECTION_REFUSED;
SetReadyState(CLOSED);
}
}
return NS_OK;
}
// nsIRequestObserver
NS_IMETHODIMP
PresentationSessionTransport::OnStartRequest(nsIRequest* aRequest,
nsISupports* aContext)
{
// Do nothing.
return NS_OK;
}
NS_IMETHODIMP
PresentationSessionTransport::OnStopRequest(nsIRequest* aRequest,
nsISupports* aContext,
nsresult aStatusCode)
{
MOZ_ASSERT(NS_IsMainThread());
uint32_t count;
nsresult rv = mMultiplexStream->GetCount(&count);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
mInputStreamPump = nullptr;
if (count != 0 && NS_SUCCEEDED(aStatusCode)) {
// If we have some buffered output still, and status is not an error, the
// other side has done a half-close, but we don't want to be in the close
// state until we are done sending everything that was buffered. We also
// don't want to call |NotifyTransportClosed| yet.
return NS_OK;
}
// We call this even if there is no error.
if (mReadyState != CLOSED) {
mCloseStatus = aStatusCode;
SetReadyState(CLOSED);
}
return NS_OK;
}
// nsIStreamListener
NS_IMETHODIMP
PresentationSessionTransport::OnDataAvailable(nsIRequest* aRequest,
nsISupports* aContext,
nsIInputStream* aStream,
uint64_t aOffset,
uint32_t aCount)
{
MOZ_ASSERT(NS_IsMainThread());
if (NS_WARN_IF(!mCallback)) {
return NS_ERROR_NOT_AVAILABLE;
}
nsCString data;
nsresult rv = mInputStreamScriptable->ReadBytes(aCount, data);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
// Pass the incoming data to the listener.
return mCallback->NotifyData(data);
}