2001-09-28 20:14:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
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/. */
|
1999-08-24 22:04:05 +00:00
|
|
|
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIStreamConverterService.h"
|
|
|
|
#include "nsIStreamConverter.h"
|
2001-03-08 00:41:53 +00:00
|
|
|
#include "nsICategoryManager.h"
|
2010-06-10 18:11:40 +00:00
|
|
|
#include "mozilla/Module.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
1999-08-24 22:04:05 +00:00
|
|
|
#include "nsIStringStream.h"
|
1999-10-29 20:50:06 +00:00
|
|
|
#include "nsCOMPtr.h"
|
2006-05-10 17:30:15 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2012-06-06 03:18:25 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-09-23 03:34:09 +00:00
|
|
|
#include "nsMemory.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsIRequest.h"
|
|
|
|
#include "nsNetCID.h"
|
1999-11-30 04:50:42 +00:00
|
|
|
|
1999-08-24 22:04:05 +00:00
|
|
|
#define ASYNC_TEST // undefine this if you want to test sycnronous conversion.
|
|
|
|
|
|
|
|
/////////////////////////////////
|
|
|
|
// Event pump setup
|
|
|
|
/////////////////////////////////
|
2001-04-19 19:43:43 +00:00
|
|
|
#ifdef XP_WIN
|
1999-08-24 22:04:05 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2000-02-29 15:40:42 +00:00
|
|
|
|
1999-08-24 22:04:05 +00:00
|
|
|
static int gKeepRunning = 0;
|
|
|
|
/////////////////////////////////
|
|
|
|
// Event pump END
|
|
|
|
/////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////
|
|
|
|
// Test converters include
|
|
|
|
/////////////////////////////////
|
|
|
|
#include "Converters.h"
|
|
|
|
|
|
|
|
// CID setup
|
|
|
|
static NS_DEFINE_CID(kStreamConverterServiceCID, NS_STREAMCONVERTERSERVICE_CID);
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// EndListener - This listener is the final one in the chain. It
|
|
|
|
// receives the fully converted data, although it doesn't do anything with
|
|
|
|
// the data.
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
2012-06-06 03:18:25 +00:00
|
|
|
class EndListener MOZ_FINAL : public nsIStreamListener {
|
1999-08-24 22:04:05 +00:00
|
|
|
public:
|
|
|
|
// nsISupports declaration
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2012-12-09 17:23:19 +00:00
|
|
|
EndListener() {}
|
1999-08-24 22:04:05 +00:00
|
|
|
|
|
|
|
// nsIStreamListener method
|
2001-02-21 20:38:08 +00:00
|
|
|
NS_IMETHOD OnDataAvailable(nsIRequest* request, nsISupports *ctxt, nsIInputStream *inStr,
|
2012-09-06 02:41:02 +00:00
|
|
|
uint64_t sourceOffset, uint32_t count)
|
1999-08-24 22:04:05 +00:00
|
|
|
{
|
1999-08-29 14:11:41 +00:00
|
|
|
nsresult rv;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t read;
|
|
|
|
uint64_t len64;
|
2012-08-11 02:44:11 +00:00
|
|
|
rv = inStr->Available(&len64);
|
1999-08-29 14:11:41 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2013-01-15 12:22:03 +00:00
|
|
|
uint32_t len = (uint32_t)std::min(len64, (uint64_t)(UINT32_MAX - 1));
|
1999-08-29 14:11:41 +00:00
|
|
|
|
2000-06-03 09:46:12 +00:00
|
|
|
char *buffer = (char*)nsMemory::Alloc(len + 1);
|
1999-08-29 14:11:41 +00:00
|
|
|
if (!buffer) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
rv = inStr->Read(buffer, len, &read);
|
|
|
|
buffer[len] = '\0';
|
1999-09-09 22:05:05 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-04-07 21:25:00 +00:00
|
|
|
printf("CONTEXT %p: Received %u bytes and the following data: \n %s\n\n",
|
|
|
|
static_cast<void*>(ctxt), read, buffer);
|
1999-09-09 22:05:05 +00:00
|
|
|
}
|
2000-06-03 09:46:12 +00:00
|
|
|
nsMemory::Free(buffer);
|
1999-08-29 14:11:41 +00:00
|
|
|
|
1999-08-24 22:04:05 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-04-10 06:01:08 +00:00
|
|
|
// nsIRequestObserver methods
|
2001-03-08 00:41:53 +00:00
|
|
|
NS_IMETHOD OnStartRequest(nsIRequest* request, nsISupports *ctxt) { return NS_OK; }
|
1999-08-24 22:04:05 +00:00
|
|
|
|
2001-02-21 20:38:08 +00:00
|
|
|
NS_IMETHOD OnStopRequest(nsIRequest* request, nsISupports *ctxt,
|
2001-04-10 06:01:08 +00:00
|
|
|
nsresult aStatus) { return NS_OK; }
|
1999-08-24 22:04:05 +00:00
|
|
|
};
|
|
|
|
|
2008-04-06 12:28:34 +00:00
|
|
|
NS_IMPL_ISUPPORTS2(EndListener,
|
|
|
|
nsIStreamListener,
|
|
|
|
nsIRequestObserver)
|
|
|
|
|
1999-08-24 22:04:05 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// EndListener END
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2001-02-21 20:38:08 +00:00
|
|
|
nsresult SendData(const char * aData, nsIStreamListener* aListener, nsIRequest* request) {
|
2005-11-08 19:23:00 +00:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStringInputStream> dataStream
|
|
|
|
(do_CreateInstance("@mozilla.org/io/string-input-stream;1", &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = dataStream->SetData(aData, strlen(aData));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-03-08 00:41:53 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint64_t avail = 0;
|
2004-11-01 18:50:36 +00:00
|
|
|
dataStream->Available(&avail);
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint64_t offset = 0;
|
2012-08-11 02:44:11 +00:00
|
|
|
while (avail > 0) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t count = saturated(avail);
|
2012-08-11 02:44:11 +00:00
|
|
|
rv = aListener->OnDataAvailable(request, nullptr, dataStream,
|
2012-09-06 02:41:02 +00:00
|
|
|
offset, count);
|
2012-08-11 02:44:11 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
offset += count;
|
|
|
|
avail -= count;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2000-03-02 20:48:40 +00:00
|
|
|
}
|
2001-03-08 00:41:53 +00:00
|
|
|
#define SEND_DATA(x) SendData(x, converterListener, request)
|
2000-03-02 20:48:40 +00:00
|
|
|
|
2010-06-10 18:11:40 +00:00
|
|
|
static const mozilla::Module::CIDEntry kTestCIDs[] = {
|
2013-09-19 19:29:27 +00:00
|
|
|
{ &kTestConverterCID, false, nullptr, CreateTestConverter },
|
|
|
|
{ nullptr }
|
2010-06-10 18:11:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const mozilla::Module::ContractIDEntry kTestContracts[] = {
|
|
|
|
{ NS_ISTREAMCONVERTER_KEY "?from=a/foo&to=b/foo", &kTestConverterCID },
|
|
|
|
{ NS_ISTREAMCONVERTER_KEY "?from=b/foo&to=c/foo", &kTestConverterCID },
|
|
|
|
{ NS_ISTREAMCONVERTER_KEY "?from=b/foo&to=d/foo", &kTestConverterCID },
|
|
|
|
{ NS_ISTREAMCONVERTER_KEY "?from=c/foo&to=d/foo", &kTestConverterCID },
|
|
|
|
{ NS_ISTREAMCONVERTER_KEY "?from=d/foo&to=e/foo", &kTestConverterCID },
|
|
|
|
{ NS_ISTREAMCONVERTER_KEY "?from=d/foo&to=f/foo", &kTestConverterCID },
|
|
|
|
{ NS_ISTREAMCONVERTER_KEY "?from=t/foo&to=k/foo", &kTestConverterCID },
|
2013-09-19 19:29:27 +00:00
|
|
|
{ nullptr }
|
2010-06-10 18:11:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const mozilla::Module::CategoryEntry kTestCategories[] = {
|
|
|
|
{ NS_ISTREAMCONVERTER_KEY, "?from=a/foo&to=b/foo", "x" },
|
|
|
|
{ NS_ISTREAMCONVERTER_KEY, "?from=b/foo&to=c/foo", "x" },
|
|
|
|
{ NS_ISTREAMCONVERTER_KEY, "?from=b/foo&to=d/foo", "x" },
|
|
|
|
{ NS_ISTREAMCONVERTER_KEY, "?from=c/foo&to=d/foo", "x" },
|
|
|
|
{ NS_ISTREAMCONVERTER_KEY, "?from=d/foo&to=e/foo", "x" },
|
|
|
|
{ NS_ISTREAMCONVERTER_KEY, "?from=d/foo&to=f/foo", "x" },
|
|
|
|
{ NS_ISTREAMCONVERTER_KEY, "?from=t/foo&to=k/foo", "x" },
|
2013-09-19 19:29:27 +00:00
|
|
|
{ nullptr }
|
2010-06-10 18:11:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const mozilla::Module kTestModule = {
|
|
|
|
mozilla::Module::kVersion,
|
|
|
|
kTestCIDs,
|
|
|
|
kTestContracts,
|
|
|
|
kTestCategories
|
|
|
|
};
|
|
|
|
|
1999-08-24 22:04:05 +00:00
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2002-07-04 14:29:25 +00:00
|
|
|
{
|
2010-06-10 18:11:40 +00:00
|
|
|
XRE_AddStaticComponent(&kTestModule);
|
|
|
|
|
2002-07-04 14:29:25 +00:00
|
|
|
nsCOMPtr<nsIServiceManager> servMan;
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
|
2002-01-29 21:22:13 +00:00
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
nsCOMPtr<nsIThread> thread = do_GetCurrentThread();
|
1999-08-24 22:04:05 +00:00
|
|
|
|
2002-07-04 14:29:25 +00:00
|
|
|
nsCOMPtr<nsICategoryManager> catman =
|
|
|
|
do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
|
2012-08-07 17:17:27 +00:00
|
|
|
if (NS_FAILED(rv)) return -1;
|
2005-11-08 19:23:00 +00:00
|
|
|
nsCString previous;
|
2002-07-04 14:29:25 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIStreamConverterService> StreamConvService =
|
|
|
|
do_GetService(kStreamConverterServiceCID, &rv);
|
2012-08-07 17:17:27 +00:00
|
|
|
if (NS_FAILED(rv)) return -1;
|
2002-07-04 14:29:25 +00:00
|
|
|
|
|
|
|
// Define the *from* content type and *to* content-type for conversion.
|
2004-06-29 16:45:07 +00:00
|
|
|
static const char fromStr[] = "a/foo";
|
|
|
|
static const char toStr[] = "c/foo";
|
2000-04-25 05:40:45 +00:00
|
|
|
|
1999-08-24 22:04:05 +00:00
|
|
|
#ifdef ASYNC_TEST
|
2004-06-29 16:45:07 +00:00
|
|
|
// ASYNCHRONOUS conversion
|
2002-07-04 14:29:25 +00:00
|
|
|
|
|
|
|
// Build up a channel that represents the content we're
|
|
|
|
// starting the transaction with.
|
|
|
|
//
|
|
|
|
// sample multipart mixed content-type string:
|
|
|
|
// "multipart/x-mixed-replacE;boundary=thisrandomstring"
|
|
|
|
#if 0
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
nsCOMPtr<nsIURI> dummyURI;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(dummyURI), "http://meaningless");
|
2012-08-07 17:17:27 +00:00
|
|
|
if (NS_FAILED(rv)) return -1;
|
2001-03-08 00:41:53 +00:00
|
|
|
|
2002-07-04 14:29:25 +00:00
|
|
|
rv = NS_NewInputStreamChannel(getter_AddRefs(channel),
|
|
|
|
dummyURI,
|
2012-07-30 14:20:58 +00:00
|
|
|
nullptr, // inStr
|
2002-07-04 14:29:25 +00:00
|
|
|
"text/plain", // content-type
|
|
|
|
-1); // XXX fix contentLength
|
2012-08-07 17:17:27 +00:00
|
|
|
if (NS_FAILED(rv)) return -1;
|
1999-08-29 14:11:41 +00:00
|
|
|
|
2002-07-04 14:29:25 +00:00
|
|
|
nsCOMPtr<nsIRequest> request(do_QueryInterface(channel));
|
|
|
|
#endif
|
1999-08-24 22:04:05 +00:00
|
|
|
|
2002-07-04 14:29:25 +00:00
|
|
|
nsCOMPtr<nsIRequest> request;
|
1999-08-24 22:04:05 +00:00
|
|
|
|
2002-07-04 14:29:25 +00:00
|
|
|
// setup a listener to receive the converted data. This guy is the end
|
|
|
|
// listener in the chain, he wants the fully converted (toType) data.
|
|
|
|
// An example of this listener in mozilla would be the DocLoader.
|
|
|
|
nsIStreamListener *dataReceiver = new EndListener();
|
|
|
|
NS_ADDREF(dataReceiver);
|
1999-08-29 14:11:41 +00:00
|
|
|
|
2002-07-04 14:29:25 +00:00
|
|
|
// setup a listener to push the data into. This listener sits inbetween the
|
|
|
|
// unconverted data of fromType, and the final listener in the chain (in this case
|
|
|
|
// the dataReceiver.
|
2012-07-30 14:20:58 +00:00
|
|
|
nsIStreamListener *converterListener = nullptr;
|
2004-06-29 16:45:07 +00:00
|
|
|
rv = StreamConvService->AsyncConvertData(fromStr, toStr,
|
2012-07-30 14:20:58 +00:00
|
|
|
dataReceiver, nullptr, &converterListener);
|
2012-08-07 17:17:27 +00:00
|
|
|
if (NS_FAILED(rv)) return -1;
|
2002-07-04 14:29:25 +00:00
|
|
|
NS_RELEASE(dataReceiver);
|
2001-02-21 20:38:08 +00:00
|
|
|
|
2002-07-04 14:29:25 +00:00
|
|
|
// at this point we have a stream listener to push data to, and the one
|
|
|
|
// that will receive the converted data. Let's mimic On*() calls and get the conversion
|
|
|
|
// going. Typically these On*() calls would be made inside their respective wrappers On*()
|
|
|
|
// methods.
|
2012-07-30 14:20:58 +00:00
|
|
|
rv = converterListener->OnStartRequest(request, nullptr);
|
2012-08-07 17:17:27 +00:00
|
|
|
if (NS_FAILED(rv)) return -1;
|
2000-03-02 20:48:40 +00:00
|
|
|
|
2002-07-04 14:29:25 +00:00
|
|
|
rv = SEND_DATA("aaa");
|
2012-08-07 17:17:27 +00:00
|
|
|
if (NS_FAILED(rv)) return -1;
|
1999-08-29 14:11:41 +00:00
|
|
|
|
2002-07-04 14:29:25 +00:00
|
|
|
rv = SEND_DATA("aaa");
|
2012-08-07 17:17:27 +00:00
|
|
|
if (NS_FAILED(rv)) return -1;
|
2001-03-08 00:41:53 +00:00
|
|
|
|
2002-07-04 14:29:25 +00:00
|
|
|
// Finish the request.
|
2012-07-30 14:20:58 +00:00
|
|
|
rv = converterListener->OnStopRequest(request, nullptr, rv);
|
2012-08-07 17:17:27 +00:00
|
|
|
if (NS_FAILED(rv)) return -1;
|
1999-08-29 14:11:41 +00:00
|
|
|
|
2002-07-04 14:29:25 +00:00
|
|
|
NS_RELEASE(converterListener);
|
1999-08-24 22:04:05 +00:00
|
|
|
#else
|
2004-06-29 16:45:07 +00:00
|
|
|
// SYNCHRONOUS conversion
|
2002-07-04 14:29:25 +00:00
|
|
|
nsCOMPtr<nsIInputStream> convertedData;
|
2004-06-29 16:45:07 +00:00
|
|
|
rv = StreamConvService->Convert(inputData, fromStr, toStr,
|
2012-07-30 14:20:58 +00:00
|
|
|
nullptr, getter_AddRefs(convertedData));
|
2012-08-07 17:17:27 +00:00
|
|
|
if (NS_FAILED(rv)) return -1;
|
2001-03-08 00:41:53 +00:00
|
|
|
#endif
|
1999-08-24 22:04:05 +00:00
|
|
|
|
2002-07-04 14:29:25 +00:00
|
|
|
// Enter the message pump to allow the URL load to proceed.
|
|
|
|
while ( gKeepRunning ) {
|
2006-05-10 17:30:15 +00:00
|
|
|
if (!NS_ProcessNextEvent(thread))
|
|
|
|
break;
|
2002-07-04 14:29:25 +00:00
|
|
|
}
|
|
|
|
} // this scopes the nsCOMPtrs
|
|
|
|
// no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_ShutdownXPCOM(nullptr);
|
2012-08-07 17:17:27 +00:00
|
|
|
return 0;
|
1999-08-24 22:04:05 +00:00
|
|
|
}
|