1999-08-25 02:04:05 +04:00
|
|
|
#include "Converters.h"
|
|
|
|
#include "nsIStringStream.h"
|
2001-03-08 03:41:53 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2005-11-08 22:23:00 +03:00
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
1999-08-25 02:04:05 +04:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////
|
|
|
|
// TestConverter
|
|
|
|
//////////////////////////////////////////////////
|
|
|
|
|
2008-04-06 16:28:34 +04:00
|
|
|
NS_IMPL_ISUPPORTS3(TestConverter,
|
|
|
|
nsIStreamConverter,
|
|
|
|
nsIStreamListener,
|
|
|
|
nsIRequestObserver)
|
1999-08-25 02:04:05 +04:00
|
|
|
|
|
|
|
TestConverter::TestConverter() {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert aFromStream (of type aFromType), to _retval (nsIInputStream of type aToType).
|
|
|
|
// This Convert method simply converts the stream byte-by-byte, to the first character
|
|
|
|
// in the aToType "string".
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TestConverter::Convert(nsIInputStream *aFromStream,
|
2004-06-29 20:45:07 +04:00
|
|
|
const char *aFromType,
|
|
|
|
const char *aToType,
|
1999-08-25 02:04:05 +04:00
|
|
|
nsISupports *ctxt,
|
|
|
|
nsIInputStream **_retval) {
|
2001-03-08 03:41:53 +03:00
|
|
|
char buf[1024+1];
|
1999-08-25 02:04:05 +04:00
|
|
|
PRUint32 read;
|
|
|
|
nsresult rv = aFromStream->Read(buf, 1024, &read);
|
1999-09-10 02:05:05 +04:00
|
|
|
if (NS_FAILED(rv) || read == 0) return rv;
|
1999-08-25 02:04:05 +04:00
|
|
|
|
2001-03-10 21:18:36 +03:00
|
|
|
// verify that the data we're converting matches the from type
|
|
|
|
// if it doesn't then we're being handed the wrong data.
|
2004-06-29 20:45:07 +04:00
|
|
|
char fromChar = *aFromType;
|
2001-03-10 21:18:36 +03:00
|
|
|
|
|
|
|
if (fromChar != buf[0]) {
|
|
|
|
printf("We're receiving %c, but are supposed to have %c.\n", buf[0], fromChar);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-25 02:04:05 +04:00
|
|
|
// Get the first character
|
2004-06-29 20:45:07 +04:00
|
|
|
char toChar = *aToType;
|
1999-08-25 02:04:05 +04:00
|
|
|
|
|
|
|
for (PRUint32 i = 0; i < read; i++)
|
|
|
|
buf[i] = toChar;
|
|
|
|
|
2001-03-08 03:41:53 +03:00
|
|
|
buf[read] = '\0';
|
|
|
|
|
2005-11-08 22:23:00 +03:00
|
|
|
nsCOMPtr<nsIStringInputStream> str
|
|
|
|
(do_CreateInstance("@mozilla.org/io/string-input-stream;1", &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = str->SetData(buf, read);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_ADDREF(*_retval = str);
|
|
|
|
return NS_OK;
|
1999-08-25 02:04:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This method initializes any internal state before the stream converter
|
2004-06-29 20:45:07 +04:00
|
|
|
* begins asynchronous conversion */
|
1999-08-25 02:04:05 +04:00
|
|
|
NS_IMETHODIMP
|
2004-06-29 20:45:07 +04:00
|
|
|
TestConverter::AsyncConvertData(const char *aFromType,
|
|
|
|
const char *aToType,
|
1999-08-25 02:04:05 +04:00
|
|
|
nsIStreamListener *aListener,
|
|
|
|
nsISupports *ctxt) {
|
|
|
|
NS_ASSERTION(aListener, "null listener");
|
|
|
|
|
|
|
|
mListener = aListener;
|
|
|
|
|
|
|
|
// based on these types, setup internal state to handle the appropriate conversion.
|
|
|
|
fromType = aFromType;
|
|
|
|
toType = aToType;
|
|
|
|
|
|
|
|
return NS_OK;
|
2003-09-08 03:12:15 +04:00
|
|
|
}
|
1999-08-25 02:04:05 +04:00
|
|
|
|
|
|
|
// nsIStreamListener method
|
|
|
|
/* This method handles asyncronous conversion of data. */
|
|
|
|
NS_IMETHODIMP
|
2001-02-21 23:38:08 +03:00
|
|
|
TestConverter::OnDataAvailable(nsIRequest* request,
|
1999-08-25 02:04:05 +04:00
|
|
|
nsISupports *ctxt,
|
|
|
|
nsIInputStream *inStr,
|
|
|
|
PRUint32 sourceOffset,
|
|
|
|
PRUint32 count) {
|
|
|
|
nsresult rv;
|
2001-03-08 03:41:53 +03:00
|
|
|
nsCOMPtr<nsIInputStream> convertedStream;
|
1999-08-25 02:04:05 +04:00
|
|
|
// just make a syncronous call to the Convert() method.
|
|
|
|
// Anything can happen here, I just happen to be using the sync call to
|
|
|
|
// do the actual conversion.
|
2001-06-30 15:02:25 +04:00
|
|
|
rv = Convert(inStr, fromType.get(), toType.get(), ctxt, getter_AddRefs(convertedStream));
|
1999-08-25 02:04:05 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
PRUint32 len;
|
1999-09-10 02:05:05 +04:00
|
|
|
convertedStream->Available(&len);
|
2001-02-21 23:38:08 +03:00
|
|
|
return mListener->OnDataAvailable(request, ctxt, convertedStream, sourceOffset, len);
|
2003-09-08 03:12:15 +04:00
|
|
|
}
|
1999-08-25 02:04:05 +04:00
|
|
|
|
2001-04-10 10:01:08 +04:00
|
|
|
// nsIRequestObserver methods
|
1999-08-25 02:04:05 +04:00
|
|
|
/* These methods just pass through directly to the mListener */
|
|
|
|
NS_IMETHODIMP
|
2001-02-21 23:38:08 +03:00
|
|
|
TestConverter::OnStartRequest(nsIRequest* request, nsISupports *ctxt) {
|
|
|
|
return mListener->OnStartRequest(request, ctxt);
|
2003-09-08 03:12:15 +04:00
|
|
|
}
|
1999-08-25 02:04:05 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-02-21 23:38:08 +03:00
|
|
|
TestConverter::OnStopRequest(nsIRequest* request, nsISupports *ctxt,
|
2001-04-10 10:01:08 +04:00
|
|
|
nsresult aStatus) {
|
|
|
|
return mListener->OnStopRequest(request, ctxt, aStatus);
|
2003-09-08 03:12:15 +04:00
|
|
|
}
|
1999-08-25 02:04:05 +04:00
|
|
|
|
2010-06-10 22:11:40 +04:00
|
|
|
nsresult
|
|
|
|
CreateTestConverter(nsISupports* aOuter, REFNSIID aIID, void** aResult)
|
1999-08-25 02:04:05 +04:00
|
|
|
{
|
2010-06-10 22:11:40 +04:00
|
|
|
nsCOMPtr<nsISupports> conv = new TestConverter();
|
|
|
|
return conv->QueryInterface(aIID, aResult);
|
1999-08-25 02:04:05 +04:00
|
|
|
}
|