A mozilla/npAPInsIInputStreamShim.cpp

A mozilla/npAPInsIInputStreamShim.h

- Shim to allow the np4xplugin API to call pass stream data to pluglet

M mozilla/Makefile.in

- Add the shim to the source files

M mozilla/nppluglet.cpp

- implement the layer that calls the shim

M test/test.html

- Pass the plugin a src of its .java file.

M test/test.java

- additional debug printout inf

mozilla/npAPInsIInputStreamShim.cpp mozilla/npAPInsIInputStreamShim.h mozilla/Makefile.in mozilla/nppluglet.cpp test/test.html test/test.java


git-svn-id: svn://10.0.0.236/trunk@214151 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
edburns%acm.org
2006-10-26 04:19:00 +00:00
parent 67a8ba89cc
commit cd61788d87
6 changed files with 610 additions and 14 deletions

View File

@@ -55,6 +55,7 @@ NO_DIST_INSTALL = 1
CPPSRCS = nsScriptablePeer.cpp \
nppluglet.cpp \
npAPInsIInputStreamShim.cpp \
$(NULL)
XPIDLSRCS = nsISimplePlugin.idl
@@ -89,4 +90,5 @@ endif
LOCAL_INCLUDES = -I./$(XPIDL_GEN_DIR) \
-I$(PLUGIN_SDK)/samples/include \
-I$(PLUGIN_SDK)/../../base/src \
$(NULL)

View File

@@ -0,0 +1,424 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998-1999 Netscape Communications Corporation. All
* Rights Reserved.
*
*/
#include "npAPInsIInputStreamShim.h"
#include "nsCRT.h"
#include "prlog.h"
#include "prlock.h"
#include "nsStreamUtils.h"
const PRUint32 npAPInsIInputStreamShim::INITIAL_BUFFER_LENGTH = 16532;
const PRInt32 npAPInsIInputStreamShim::buffer_increment = 5120;
const PRInt32 npAPInsIInputStreamShim::do_close_code = -524;
npAPInsIInputStreamShim::npAPInsIInputStreamShim(nsIPluginStreamListener *plugletListener, nsIPluginStreamInfo* streamInfo)
: mPlugletListener(plugletListener),
mStreamInfo(streamInfo),
mBuffer(nsnull), mBufferLength(0), mCountFromPluginHost(0),
mCountFromPluglet(0), mAvailable(0), mAvailableForPluglet(0),
mNumWrittenFromPluginHost(0),
mDoClose(PR_FALSE), mDidClose(PR_FALSE), mLock(nsnull),
mCloseStatus(NS_OK), mCallback(nsnull), mCallbackFlags(0)
{
NS_INIT_ISUPPORTS();
mLock = PR_NewLock();
}
npAPInsIInputStreamShim::~npAPInsIInputStreamShim()
{
mPlugletListener = nsnull;
mStreamInfo = nsnull;
PR_Lock(mLock);
delete [] mBuffer;
mBuffer = nsnull;
mBufferLength = 0;
PR_Unlock(mLock);
mAvailable = 0;
mAvailableForPluglet = 0;
mNumWrittenFromPluginHost = 0;
mDoClose = PR_TRUE;
mDidClose = PR_TRUE;
PR_DestroyLock(mLock);
mLock = nsnull;
}
//NS_IMPL_ISUPPORTS(npAPInsIInputStreamShim, NS_GET_IID(nsIInputStream))
NS_IMETHODIMP_(nsrefcnt) npAPInsIInputStreamShim::AddRef(void)
{
NS_PRECONDITION(PRInt32(mRefCnt) >= 0, "illegal refcnt");
// Note that we intentionally don't check for owning thread safety.
++mRefCnt;
NS_LOG_ADDREF(this, mRefCnt, "npAPInsIInputStreamShim", sizeof(*this));
return mRefCnt;
}
NS_IMETHODIMP_(nsrefcnt) npAPInsIInputStreamShim::Release(void)
{
NS_PRECONDITION(0 != mRefCnt, "dup release");
// Note that we intentionally don't check for owning thread safety.
--mRefCnt;
NS_LOG_RELEASE(this, mRefCnt, "npAPInsIInputStreamShim");
if (mRefCnt == 0) {
mRefCnt = 1; /* stabilize */
NS_DELETEXPCOM(this);
return 0;
}
return mRefCnt;
}
NS_IMPL_QUERY_INTERFACE2(npAPInsIInputStreamShim,
nsIInputStream,
nsIAsyncInputStream)
NS_IMETHODIMP
npAPInsIInputStreamShim::AllowStreamToReadFromBuffer(int32 len, void* buf,
int32* outWritten)
{
nsresult rv = NS_ERROR_NULL_POINTER;
if (!mPlugletListener) {
return rv;
}
PR_Lock(mLock);
rv = this->CopyFromPluginHostToBuffer(len, buf, outWritten);
PR_Unlock(mLock);
if (NS_SUCCEEDED(rv)) {
rv = mPlugletListener->OnDataAvailable(mStreamInfo, this,
(PRUint32) outWritten);
}
return rv;
}
NS_IMETHODIMP
npAPInsIInputStreamShim::CopyFromPluginHostToBuffer(int32 len, void* buf,
int32 * outWritten)
{
nsresult rv = NS_ERROR_NULL_POINTER;
if (nsnull == outWritten) {
return rv;
}
if (mDoClose) {
DoClose();
return NS_ERROR_NOT_AVAILABLE;
}
// if we don't have a buffer, create one
if (!mBuffer) {
if (0 < INITIAL_BUFFER_LENGTH) {
mBuffer = new char[INITIAL_BUFFER_LENGTH];
mBufferLength = INITIAL_BUFFER_LENGTH;
}
else {
// make sure we allocate enough buffer to store what is
// currently available.
if (mAvailable < buffer_increment) {
mBufferLength = buffer_increment;
}
else {
PRInt32 bufLengthCalc = mAvailable / buffer_increment;
mBufferLength = buffer_increment +
(bufLengthCalc * buffer_increment);
}
mBuffer = new char[mBufferLength];
}
if (!mBuffer) {
mBufferLength = 0;
return NS_ERROR_FAILURE;
}
}
else {
// See if we need to grow our buffer. If what we have plus what
// we're about to get is greater than the current buffer size...
if (mBufferLength < (mCountFromPluginHost + mAvailable)) {
// create the new buffer
char *tBuffer = new char[mBufferLength + buffer_increment];
if (!tBuffer) {
return NS_ERROR_FAILURE;
}
// copy the old buffer into the new buffer
memcpy(tBuffer, mBuffer, mBufferLength);
// delete the old buffer
delete [] mBuffer;
// update mBuffer;
mBuffer = tBuffer;
// update our bufferLength
mBufferLength += buffer_increment;
}
}
mNumWrittenFromPluginHost = len;
*outWritten = mNumWrittenFromPluginHost;
if (0 < mNumWrittenFromPluginHost) {
// copy the bytes the from the plugin host into our buffer
memcpy(mBuffer + mCountFromPluginHost, buf, mNumWrittenFromPluginHost);
mCountFromPluginHost += mNumWrittenFromPluginHost;
mAvailableForPluglet = mCountFromPluginHost - mCountFromPluglet;
}
// if we have bytes available for the pluglet, and they it have
// requested a callback when bytes are available.
if (mCallback && 0 < mAvailableForPluglet
&& !(mCallbackFlags & WAIT_CLOSURE_ONLY)) {
rv = mCallback->OnInputStreamReady(this);
mCallback = nsnull;
mCallbackFlags = nsnull;
}
rv = NS_OK;
return rv;
}
NS_IMETHODIMP
npAPInsIInputStreamShim::DoClose(void)
{
nsresult rv = NS_ERROR_FAILURE;
PR_ASSERT(mDoClose);
if (mDidClose) {
return NS_OK;
}
if (mCallback && mAvailableForPluglet &&
!(mCallbackFlags & WAIT_CLOSURE_ONLY)) {
rv = mCallback->OnInputStreamReady(this);
mCallback = nsnull;
mCallbackFlags = nsnull;
}
mCloseStatus = NS_BASE_STREAM_CLOSED;
mDidClose = PR_TRUE;
if (mPlugletListener && mStreamInfo) {
mPlugletListener->OnStopBinding(mStreamInfo, mCloseStatus);
}
return rv;
}
//
// nsIInputStream methods
//
NS_IMETHODIMP
npAPInsIInputStreamShim::Available(PRUint32* aResult)
{
nsresult rv = NS_ERROR_FAILURE;
if (!aResult) {
return NS_ERROR_NULL_POINTER;
}
if (mDoClose) {
if (mAvailableForPluglet) {
*aResult = mAvailableForPluglet;
return NS_OK;
}
return mCloseStatus;
}
PR_ASSERT(mLock);
PR_Lock(mLock);
*aResult = mAvailableForPluglet;
// *aResult = PR_UINT32_MAX;
rv = NS_OK;
PR_Unlock(mLock);
return rv;
}
NS_IMETHODIMP
npAPInsIInputStreamShim::Close()
{
nsresult rv = NS_ERROR_FAILURE;
PR_ASSERT(mLock);
PR_Lock(mLock);
mDoClose = PR_TRUE;
mCloseStatus = NS_BASE_STREAM_CLOSED;
rv = NS_OK;
PR_Unlock(mLock);
return rv;
}
NS_IMETHODIMP
npAPInsIInputStreamShim::Read(char* aBuffer, PRUint32 aCount, PRUint32 *aNumRead)
{
nsresult rv = NS_ERROR_FAILURE;
if (!aBuffer || !aNumRead) {
return NS_ERROR_NULL_POINTER;
}
if (mDoClose) {
return mCloseStatus;
}
*aNumRead = 0;
PR_ASSERT(mLock);
PR_ASSERT(mCountFromPluglet <= mCountFromPluginHost);
PR_Lock(mLock);
if (mAvailableForPluglet) {
if (aCount <= (mCountFromPluginHost - mCountFromPluglet)) {
// what she's asking for is less than or equal to what we have
memcpy(aBuffer, (mBuffer + mCountFromPluglet), aCount);
mCountFromPluglet += aCount;
*aNumRead = aCount;
}
else {
// what she's asking for is more than what we have
memcpy(aBuffer, (mBuffer + mCountFromPluglet),
(mCountFromPluginHost - mCountFromPluglet));
*aNumRead = (mCountFromPluginHost - mCountFromPluglet);
mCountFromPluglet += (mCountFromPluginHost - mCountFromPluglet);
}
mAvailableForPluglet -= *aNumRead;
rv = NS_OK;
}
else {
rv = NS_BASE_STREAM_WOULD_BLOCK;
}
PR_Unlock(mLock);
return rv;
}
NS_IMETHODIMP
npAPInsIInputStreamShim::ReadSegments(nsWriteSegmentFun writer, void * aClosure,
PRUint32 aCount, PRUint32 *aNumRead)
{
nsresult rv = NS_ERROR_FAILURE;
if (!writer || !aNumRead) {
return NS_ERROR_NULL_POINTER;
}
if (mDoClose && 0 == mAvailableForPluglet) {
return mCloseStatus;
}
*aNumRead = 0;
PR_ASSERT(mLock);
PR_ASSERT(mCountFromPluglet <= mCountFromPluginHost);
PR_Lock(mLock);
PRInt32 bytesToWrite = mAvailableForPluglet;
PRUint32 bytesWritten;
PRUint32 totalBytesWritten = 0;
if (bytesToWrite > aCount) {
bytesToWrite = aCount;
}
if (0 == mAvailableForPluglet) {
rv = NS_BASE_STREAM_WOULD_BLOCK;
}
while (bytesToWrite) {
// what she's asking for is less than or equal to what we have
rv = writer(this, aClosure,
(mBuffer + mCountFromPluglet),
totalBytesWritten, bytesToWrite, &bytesWritten);
if (NS_FAILED(rv)) {
break;
}
bytesToWrite -= bytesWritten;
totalBytesWritten += bytesWritten;
mCountFromPluglet += bytesWritten;
}
*aNumRead = totalBytesWritten;
mAvailableForPluglet -= totalBytesWritten;
PR_Unlock(mLock);
}
NS_IMETHODIMP
npAPInsIInputStreamShim::IsNonBlocking(PRBool *_retval)
{
*_retval = PR_TRUE;
return NS_OK;
}
//
// nsIAsyncInputStream methods
//
NS_IMETHODIMP
npAPInsIInputStreamShim::CloseWithStatus(nsresult astatus)
{
this->Close();
mCloseStatus = astatus;
return NS_OK;
}
NS_IMETHODIMP
npAPInsIInputStreamShim::AsyncWait(nsIInputStreamCallback *aCallback,
PRUint32 aFlags, PRUint32 aRequestedCount,
nsIEventTarget *aEventTarget)
{
PR_Lock(mLock);
mCallback = nsnull;
mCallbackFlags = nsnull;
nsCOMPtr<nsIInputStreamCallback> proxy;
if (aEventTarget) {
nsresult rv = NS_NewInputStreamReadyEvent(getter_AddRefs(proxy),
aCallback, aEventTarget);
if (NS_FAILED(rv)) return rv;
aCallback = proxy;
}
if (NS_FAILED(mCloseStatus) ||
(mAvailableForPluglet && !(aFlags & WAIT_CLOSURE_ONLY))) {
// stream is already closed or readable; post event.
aCallback->OnInputStreamReady(this);
}
else {
// queue up callback object to be notified when data becomes available
mCallback = aCallback;
mCallbackFlags = aFlags;
}
PR_Unlock(mLock);
return NS_OK;
}

View File

@@ -0,0 +1,154 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998-1999 Netscape Communications Corporation. All
* Rights Reserved.
*
*/
#ifndef npAPInsIInputStreamShim_h
#define npAPInsIInputStreamShim_h
#include "nsIPluginStreamListener.h"
#include "nsIPluginStreamInfo.h"
#include "nsIInputStream.h"
#include "nsIAsyncInputStream.h"
#include "nsCOMPtr.h"
struct PRLock;
class npAPInsIInputStreamShim : public nsIAsyncInputStream
{
public:
npAPInsIInputStreamShim(nsIPluginStreamListener *plugletListener,
nsIPluginStreamInfo *streamInfo);
virtual ~npAPInsIInputStreamShim();
// nsISupports methods
NS_DECL_ISUPPORTS
// nsIInputStream methods
NS_DECL_NSIINPUTSTREAM
// nsIAsyncInputStream
NS_DECL_NSIASYNCINPUTSTREAM
NS_IMETHOD AllowStreamToReadFromBuffer(int32 len, void* buf, int32* outWritten);
private:
NS_IMETHOD DoClose(void);
NS_IMETHOD CopyFromPluginHostToBuffer(int32 len, void* buf,
int32* outWritten);
nsCOMPtr<nsIPluginStreamListener> mPlugletListener;
nsCOMPtr<nsIPluginStreamInfo> mStreamInfo;
/**
* a dynamically allocated buffer, written by nppluglet.cpp's
* nsPluginInstance::Write(), read by
* PlugletStreamListener::OnDataAvailable calling our Read().
* deleting this buffer signifies the nppluglet's InputStream should
* be closed.
* MUST be deallocated in the destructor.
*/
char *mBuffer;
/**
* the length of the buffer
*/
PRUint32 mBufferLength;
/**
* Running sum of the total number of bytes read so far from the
* npapi plugin.
*/
PRUint32 mCountFromPluginHost;
/**
* Running sum of the total number of bytes read so far by pluglet
*/
PRUint32 mCountFromPluglet;
/**
* The number of bytes available on the plugin's InputStream
*/
PRInt32 mAvailable;
/**
* The number of bytes available in the buffer that haven't yet been
* read by the pluglet.
*/
PRInt32 mAvailableForPluglet;
/**
* The number of bytes written to our internal buffer on the last
* call to AllowStreamToReadFromBuffer.
*/
PRInt32 mNumWrittenFromPluginHost;
PRBool mDoClose;
PRBool mDidClose;
/**
* Provides mutex
*/
PRLock *mLock;
nsresult mCloseStatus;
nsCOMPtr<nsIInputStreamCallback> mCallback;
PRUint32 mCallbackFlags;
static const PRUint32 INITIAL_BUFFER_LENGTH;
static const PRInt32 buffer_increment;
static const PRInt32 do_close_code;
};
#endif // npAPInsIInputStreamShim_h

View File

@@ -51,6 +51,8 @@
#include "plstr.h"
#include "npAPInsIInputStreamShim.h"
// service manager which will give the access to all public browser services
// we will use memory service as an illustration
nsIServiceManager * gServiceManager = NULL;
@@ -283,11 +285,10 @@ NPError nsPluginInstance::NewStream(NPMIMEType type, NPStream* stream,
if (hostStreamInfo) {
rv = listener->OnStartBinding(hostStreamInfo);
if (NS_SUCCEEDED(rv)) {
stream->pdata = (void *) listener.get();
nsCOMPtr<nsISupports> toCast =
do_QueryInterface(listener);
nsISupports *toAddRef = (nsISupports *) toCast.get();
NS_ADDREF(toAddRef);
npAPInsIInputStreamShim *shim =
new npAPInsIInputStreamShim(listener,
hostStreamInfo);
stream->pdata = (void *) shim;
}
}
}
@@ -304,12 +305,9 @@ NPError nsPluginInstance::DestroyStream(NPStream *stream, NPError reason)
return rv;
}
nsCOMPtr<nsIPluginStreamListener> listener =
(nsIPluginStreamListener *) stream->pdata;
nsCOMPtr<nsISupports> toCast = do_QueryInterface(listener);
nsISupports *toRelease = (nsISupports *) toCast.get();
NS_RELEASE(toRelease);
listener = nsnull;
npAPInsIInputStreamShim *shim =
(npAPInsIInputStreamShim *) stream->pdata;
delete shim;
stream->pdata = nsnull;
return NS_OK;
@@ -324,8 +322,15 @@ int32 nsPluginInstance::Write(NPStream *stream, int32 offset,
int32 len, void *buffer)
{
int32 result = len;
nsCOMPtr<nsIPluginStreamListener> listener =
(nsIPluginStreamListener *) stream->pdata;
npAPInsIInputStreamShim *shim =
(npAPInsIInputStreamShim *) stream->pdata;
nsCOMPtr<nsIPluginStreamListener> listener = nsnull;
nsresult rv = NS_ERROR_NULL_POINTER;
rv = shim->AllowStreamToReadFromBuffer(len, buffer, &result);
if (NS_SUCCEEDED(rv)) {
printf("debug: edburns: passed %d bytes to pluglet\n", result);
}
return result;
}

View File

@@ -1,5 +1,7 @@
<html>
<head><title>TestPluglet that loads its source file</title><head>
<body>
<EMBED type="application/x-simple-pluglet" name="test" width=400 height=200>
<EMBED type="application/x-simple-pluglet" name="test"
src="test.java" width=400 height=200>
</body>
</html>

View File

@@ -185,6 +185,9 @@ class TestStreamListener implements PlugletStreamListener {
org.mozilla.util.DebugPluglet.print("--TestStreamListener.onStartBinding ");
org.mozilla.util.DebugPluglet.print("length "+streamInfo.getLength());
org.mozilla.util.DebugPluglet.print(" contenet type "+ streamInfo.getContentType());
org.mozilla.util.DebugPluglet.print(" url "+ streamInfo.getURL());
org.mozilla.util.DebugPluglet.print(" seekable "+ streamInfo.isSeekable());
}
/**
* Notify the client that data is available in the input stream. This
@@ -199,12 +202,18 @@ class TestStreamListener implements PlugletStreamListener {
try{
org.mozilla.util.DebugPluglet.print("--TestStreamListener.onDataAvailable ");
org.mozilla.util.DebugPluglet.print("--length "+input.available()+"\n");
String cur = null;
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(input));
while (null != (cur = bufferedReader.readLine())) {
org.mozilla.util.DebugPluglet.print(cur);
}
} catch(Exception e) {
;
}
}
public void onFileAvailable(PlugletStreamInfo plugletInfo, String fileName) {
org.mozilla.util.DebugPluglet.print("--TestStreamListener.onFileAvailable\n");
org.mozilla.util.DebugPluglet.print(" fileName" + fileName);
}
/**
* Notify the observer that the URL has finished loading.