2009-08-18 20:20:03 +00:00
|
|
|
/* -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 8 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* 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/. */
|
|
|
|
|
2009-08-18 20:20:03 +00:00
|
|
|
|
2009-09-09 14:04:09 +00:00
|
|
|
#include "BrowserStreamParent.h"
|
2014-12-29 23:14:11 +00:00
|
|
|
#include "PluginAsyncSurrogate.h"
|
2009-09-09 06:31:35 +00:00
|
|
|
#include "PluginInstanceParent.h"
|
2011-10-19 14:46:24 +00:00
|
|
|
#include "nsNPAPIPlugin.h"
|
2009-08-18 20:20:03 +00:00
|
|
|
|
2015-10-28 13:44:19 +00:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2016-08-23 04:09:32 +00:00
|
|
|
#include "mozilla/Unused.h"
|
2010-04-27 05:43:03 +00:00
|
|
|
|
2010-02-23 21:45:00 +00:00
|
|
|
// How much data are we willing to send across the wire
|
|
|
|
// in one chunk?
|
2015-01-06 17:40:15 +00:00
|
|
|
static const int32_t kSendDataChunk = 0xffff;
|
2010-02-23 21:45:00 +00:00
|
|
|
|
2009-08-18 20:20:03 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace plugins {
|
|
|
|
|
2009-09-09 14:04:09 +00:00
|
|
|
BrowserStreamParent::BrowserStreamParent(PluginInstanceParent* npp,
|
|
|
|
NPStream* stream)
|
2009-08-18 20:20:03 +00:00
|
|
|
: mNPP(npp)
|
|
|
|
, mStream(stream)
|
2014-12-29 23:14:11 +00:00
|
|
|
, mDeferredDestroyReason(NPRES_DONE)
|
|
|
|
, mState(INITIALIZING)
|
2009-08-18 20:20:03 +00:00
|
|
|
{
|
2009-09-21 14:51:35 +00:00
|
|
|
mStream->pdata = static_cast<AStream*>(this);
|
2014-12-29 23:14:11 +00:00
|
|
|
nsNPAPIStreamWrapper* wrapper =
|
|
|
|
reinterpret_cast<nsNPAPIStreamWrapper*>(mStream->ndata);
|
|
|
|
if (wrapper) {
|
|
|
|
mStreamListener = wrapper->GetStreamListener();
|
|
|
|
}
|
2009-08-18 20:20:03 +00:00
|
|
|
}
|
|
|
|
|
2009-09-23 23:00:23 +00:00
|
|
|
BrowserStreamParent::~BrowserStreamParent()
|
|
|
|
{
|
2015-04-20 23:04:33 +00:00
|
|
|
mStream->pdata = nullptr;
|
2009-11-02 16:48:52 +00:00
|
|
|
}
|
2009-09-23 23:00:23 +00:00
|
|
|
|
2014-05-02 18:44:13 +00:00
|
|
|
void
|
|
|
|
BrowserStreamParent::ActorDestroy(ActorDestroyReason aWhy)
|
|
|
|
{
|
|
|
|
// Implement me! Bug 1005159
|
|
|
|
}
|
|
|
|
|
2014-12-29 23:14:11 +00:00
|
|
|
bool
|
|
|
|
BrowserStreamParent::RecvAsyncNPP_NewStreamResult(const NPError& rv,
|
|
|
|
const uint16_t& stype)
|
|
|
|
{
|
|
|
|
PLUGIN_LOG_DEBUG_FUNCTION;
|
|
|
|
PluginAsyncSurrogate* surrogate = mNPP->GetAsyncSurrogate();
|
|
|
|
MOZ_ASSERT(surrogate);
|
|
|
|
surrogate->AsyncCallArriving();
|
|
|
|
if (mState == DEFERRING_DESTROY) {
|
|
|
|
// We've been asked to destroy ourselves before init was complete.
|
|
|
|
mState = DYING;
|
2015-11-02 05:53:26 +00:00
|
|
|
Unused << SendNPP_DestroyStream(mDeferredDestroyReason);
|
2014-12-29 23:14:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
NPError error = rv;
|
|
|
|
if (error == NPERR_NO_ERROR) {
|
2015-04-20 23:04:33 +00:00
|
|
|
if (!mStreamListener) {
|
2014-12-29 23:14:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-04-20 23:04:33 +00:00
|
|
|
if (mStreamListener->SetStreamType(stype)) {
|
2014-12-29 23:14:11 +00:00
|
|
|
mState = ALIVE;
|
|
|
|
} else {
|
|
|
|
error = NPERR_GENERIC_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error != NPERR_NO_ERROR) {
|
2015-02-22 06:07:24 +00:00
|
|
|
surrogate->DestroyAsyncStream(mStream);
|
2015-11-02 05:53:26 +00:00
|
|
|
Unused << PBrowserStreamParent::Send__delete__(this);
|
2014-12-29 23:14:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-09-17 23:09:20 +00:00
|
|
|
bool
|
2009-09-09 14:04:09 +00:00
|
|
|
BrowserStreamParent::AnswerNPN_RequestRead(const IPCByteRanges& ranges,
|
|
|
|
NPError* result)
|
2009-08-18 20:20:03 +00:00
|
|
|
{
|
2009-12-18 22:22:51 +00:00
|
|
|
PLUGIN_LOG_DEBUG_FUNCTION;
|
2009-09-22 21:06:00 +00:00
|
|
|
|
2010-02-23 21:45:00 +00:00
|
|
|
switch (mState) {
|
2014-12-29 23:14:11 +00:00
|
|
|
case INITIALIZING:
|
|
|
|
NS_ERROR("Requesting a read before initialization has completed");
|
|
|
|
*result = NPERR_GENERIC_ERROR;
|
|
|
|
return false;
|
|
|
|
|
2010-02-23 21:45:00 +00:00
|
|
|
case ALIVE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DYING:
|
|
|
|
*result = NPERR_GENERIC_ERROR;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NS_ERROR("Unexpected state");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-18 20:20:03 +00:00
|
|
|
if (!mStream)
|
2009-09-17 23:09:20 +00:00
|
|
|
return false;
|
2009-08-18 20:20:03 +00:00
|
|
|
|
2014-12-17 17:57:00 +00:00
|
|
|
if (ranges.Length() > INT32_MAX)
|
2009-09-19 19:24:24 +00:00
|
|
|
return false;
|
2009-08-18 20:20:03 +00:00
|
|
|
|
2015-10-28 13:44:19 +00:00
|
|
|
UniquePtr<NPByteRange[]> rp(new NPByteRange[ranges.Length()]);
|
2014-12-17 17:57:00 +00:00
|
|
|
for (uint32_t i = 0; i < ranges.Length(); ++i) {
|
2009-08-18 20:20:03 +00:00
|
|
|
rp[i].offset = ranges[i].offset;
|
|
|
|
rp[i].length = ranges[i].length;
|
|
|
|
rp[i].next = &rp[i + 1];
|
|
|
|
}
|
2014-12-17 17:57:00 +00:00
|
|
|
rp[ranges.Length() - 1].next = nullptr;
|
2009-08-18 20:20:03 +00:00
|
|
|
|
2015-10-28 13:44:19 +00:00
|
|
|
*result = mNPP->mNPNIface->requestread(mStream, rp.get());
|
2009-09-22 21:06:00 +00:00
|
|
|
return true;
|
2009-08-18 20:20:03 +00:00
|
|
|
}
|
|
|
|
|
2010-03-06 16:08:19 +00:00
|
|
|
bool
|
2010-02-23 21:45:00 +00:00
|
|
|
BrowserStreamParent::RecvNPN_DestroyStream(const NPReason& reason)
|
2010-03-06 16:08:19 +00:00
|
|
|
{
|
2010-02-23 21:45:00 +00:00
|
|
|
switch (mState) {
|
|
|
|
case ALIVE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DYING:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NS_ERROR("Unexpected state");
|
|
|
|
return false;
|
2016-01-22 15:58:49 +00:00
|
|
|
}
|
2010-02-23 21:45:00 +00:00
|
|
|
|
|
|
|
mNPP->mNPNIface->destroystream(mNPP->mNPP, mStream, reason);
|
2010-03-06 16:08:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-02-23 21:45:00 +00:00
|
|
|
void
|
|
|
|
BrowserStreamParent::NPP_DestroyStream(NPReason reason)
|
2010-02-23 21:45:00 +00:00
|
|
|
{
|
2014-12-29 23:14:11 +00:00
|
|
|
NS_ASSERTION(ALIVE == mState || INITIALIZING == mState,
|
|
|
|
"NPP_DestroyStream called twice?");
|
|
|
|
bool stillInitializing = INITIALIZING == mState;
|
|
|
|
if (stillInitializing) {
|
|
|
|
mState = DEFERRING_DESTROY;
|
|
|
|
mDeferredDestroyReason = reason;
|
|
|
|
} else {
|
|
|
|
mState = DYING;
|
2015-11-02 05:53:26 +00:00
|
|
|
Unused << SendNPP_DestroyStream(reason);
|
2014-12-29 23:14:11 +00:00
|
|
|
}
|
2010-02-23 21:45:00 +00:00
|
|
|
}
|
2010-02-25 11:00:56 +00:00
|
|
|
|
2010-02-23 21:45:00 +00:00
|
|
|
bool
|
|
|
|
BrowserStreamParent::RecvStreamDestroyed()
|
|
|
|
{
|
|
|
|
if (DYING != mState) {
|
|
|
|
NS_ERROR("Unexpected state");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-23 20:34:46 +00:00
|
|
|
mStreamPeer = nullptr;
|
2011-10-19 14:46:24 +00:00
|
|
|
|
2010-02-23 21:45:00 +00:00
|
|
|
mState = DELETING;
|
2010-04-27 05:43:03 +00:00
|
|
|
return Send__delete__(this);
|
2010-02-23 21:45:00 +00:00
|
|
|
}
|
2010-02-25 11:00:56 +00:00
|
|
|
|
2010-02-23 21:45:00 +00:00
|
|
|
int32_t
|
|
|
|
BrowserStreamParent::WriteReady()
|
|
|
|
{
|
2014-12-29 23:14:11 +00:00
|
|
|
if (mState == INITIALIZING) {
|
|
|
|
return 0;
|
|
|
|
}
|
2010-02-23 21:45:00 +00:00
|
|
|
return kSendDataChunk;
|
2009-08-18 20:20:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t
|
2009-09-09 14:04:09 +00:00
|
|
|
BrowserStreamParent::Write(int32_t offset,
|
|
|
|
int32_t len,
|
|
|
|
void* buffer)
|
2009-08-18 20:20:03 +00:00
|
|
|
{
|
2009-12-18 22:22:51 +00:00
|
|
|
PLUGIN_LOG_DEBUG_FUNCTION;
|
2009-09-22 21:06:00 +00:00
|
|
|
|
2010-02-23 21:45:00 +00:00
|
|
|
NS_ASSERTION(ALIVE == mState, "Sending data after NPP_DestroyStream?");
|
|
|
|
NS_ASSERTION(len > 0, "Non-positive length to NPP_Write");
|
2009-08-18 20:20:03 +00:00
|
|
|
|
2010-02-23 21:45:00 +00:00
|
|
|
if (len > kSendDataChunk)
|
|
|
|
len = kSendDataChunk;
|
|
|
|
|
2010-04-27 05:43:03 +00:00
|
|
|
return SendWrite(offset,
|
2016-04-09 13:50:59 +00:00
|
|
|
mStream->end,
|
|
|
|
nsCString(static_cast<char*>(buffer), len)) ?
|
2010-04-27 05:43:03 +00:00
|
|
|
len : -1;
|
2009-08-18 20:20:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-09-09 14:04:09 +00:00
|
|
|
BrowserStreamParent::StreamAsFile(const char* fname)
|
2009-08-18 20:20:03 +00:00
|
|
|
{
|
2009-12-18 22:22:51 +00:00
|
|
|
PLUGIN_LOG_DEBUG_FUNCTION;
|
2009-09-22 21:06:00 +00:00
|
|
|
|
2010-02-23 21:45:00 +00:00
|
|
|
NS_ASSERTION(ALIVE == mState,
|
|
|
|
"Calling streamasfile after NPP_DestroyStream?");
|
2010-02-25 11:00:56 +00:00
|
|
|
|
2011-10-19 14:46:24 +00:00
|
|
|
// Make sure our stream survives until the plugin process tells us we've
|
|
|
|
// been destroyed (until RecvStreamDestroyed() is called). Since we retain
|
|
|
|
// mStreamPeer at most once, we won't get in trouble if StreamAsFile() is
|
|
|
|
// called more than once.
|
|
|
|
if (!mStreamPeer) {
|
|
|
|
nsNPAPIPlugin::RetainStream(mStream, getter_AddRefs(mStreamPeer));
|
|
|
|
}
|
|
|
|
|
2015-11-02 05:53:26 +00:00
|
|
|
Unused << SendNPP_StreamAsFile(nsCString(fname));
|
2010-02-23 21:45:00 +00:00
|
|
|
return;
|
2009-08-18 20:20:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace plugins
|
|
|
|
} // namespace mozilla
|