2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-19 02:01:16 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1999-08-25 02:04:05 +04:00
|
|
|
*
|
2004-04-19 02:01:16 +04:00
|
|
|
* The contents of this file are subject to the Mozilla 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/MPL/
|
1999-08-25 02:04:05 +04:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* 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.
|
1999-08-25 02:04:05 +04:00
|
|
|
*
|
1999-11-06 06:40:37 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-19 02:01:16 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-29 00:14:13 +04:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 06:40:37 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-19 02:01:16 +04:00
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
2001-09-29 00:14:13 +04:00
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-19 02:01:16 +04:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-29 00:14:13 +04:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-19 02:01:16 +04:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-08-25 02:04:05 +04:00
|
|
|
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIComponentManager.h"
|
2002-01-30 00:22:13 +03:00
|
|
|
#include "nsIComponentRegistrar.h"
|
1999-08-25 02:04:05 +04:00
|
|
|
#include "nsIStreamConverterService.h"
|
|
|
|
#include "nsIStreamConverter.h"
|
2001-03-08 03:41:53 +03:00
|
|
|
#include "nsICategoryManager.h"
|
1999-08-25 02:04:05 +04:00
|
|
|
#include "nsIFactory.h"
|
|
|
|
#include "nsIStringStream.h"
|
1999-10-30 00:50:06 +04:00
|
|
|
#include "nsCOMPtr.h"
|
1999-11-30 07:50:42 +03:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
|
1999-09-02 06:18:28 +04:00
|
|
|
#include "nspr.h"
|
|
|
|
|
1999-08-25 02:04:05 +04:00
|
|
|
#define ASYNC_TEST // undefine this if you want to test sycnronous conversion.
|
|
|
|
|
|
|
|
/////////////////////////////////
|
|
|
|
// Event pump setup
|
|
|
|
/////////////////////////////////
|
|
|
|
#include "nsIEventQueueService.h"
|
2001-04-19 23:43:43 +04:00
|
|
|
#ifdef XP_WIN
|
1999-08-25 02:04:05 +04:00
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2000-02-29 18:40:42 +03:00
|
|
|
#ifdef XP_OS2
|
|
|
|
#include <os2.h>
|
|
|
|
#endif
|
|
|
|
|
1999-08-25 02:04:05 +04:00
|
|
|
static int gKeepRunning = 0;
|
|
|
|
static nsIEventQueue* gEventQ = nsnull;
|
|
|
|
/////////////////////////////////
|
|
|
|
// Event pump END
|
|
|
|
/////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////
|
|
|
|
// Test converters include
|
|
|
|
/////////////////////////////////
|
|
|
|
#include "Converters.h"
|
|
|
|
|
|
|
|
// CID setup
|
|
|
|
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
|
|
|
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.
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
class EndListener : public nsIStreamListener {
|
|
|
|
public:
|
|
|
|
// nsISupports declaration
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2003-01-09 01:35:09 +03:00
|
|
|
EndListener() {};
|
1999-08-25 02:04:05 +04:00
|
|
|
|
|
|
|
// nsIStreamListener method
|
2001-02-21 23:38:08 +03:00
|
|
|
NS_IMETHOD OnDataAvailable(nsIRequest* request, nsISupports *ctxt, nsIInputStream *inStr,
|
1999-08-25 02:04:05 +04:00
|
|
|
PRUint32 sourceOffset, PRUint32 count)
|
|
|
|
{
|
1999-08-29 18:11:41 +04:00
|
|
|
nsresult rv;
|
|
|
|
PRUint32 read, len;
|
1999-09-10 02:05:05 +04:00
|
|
|
rv = inStr->Available(&len);
|
1999-08-29 18:11:41 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2000-06-03 13:46:12 +04:00
|
|
|
char *buffer = (char*)nsMemory::Alloc(len + 1);
|
1999-08-29 18:11:41 +04:00
|
|
|
if (!buffer) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
rv = inStr->Read(buffer, len, &read);
|
|
|
|
buffer[len] = '\0';
|
1999-09-10 02:05:05 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
1999-10-07 08:10:12 +04:00
|
|
|
printf("CONTEXT %p: Received %u bytes and the following data: \n %s\n\n", ctxt, read, buffer);
|
1999-09-10 02:05:05 +04:00
|
|
|
}
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(buffer);
|
1999-08-29 18:11:41 +04:00
|
|
|
|
1999-08-25 02:04:05 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-04-10 10:01:08 +04:00
|
|
|
// nsIRequestObserver methods
|
2001-03-08 03:41:53 +03:00
|
|
|
NS_IMETHOD OnStartRequest(nsIRequest* request, nsISupports *ctxt) { return NS_OK; }
|
1999-08-25 02:04:05 +04:00
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
NS_IMETHOD OnStopRequest(nsIRequest* request, nsISupports *ctxt,
|
2001-04-10 10:01:08 +04:00
|
|
|
nsresult aStatus) { return NS_OK; }
|
1999-08-25 02:04:05 +04:00
|
|
|
};
|
|
|
|
|
2003-09-08 03:12:15 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(EndListener, nsIStreamListener)
|
1999-08-25 02:04:05 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// EndListener END
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
nsresult SendData(const char * aData, nsIStreamListener* aListener, nsIRequest* request) {
|
2005-11-08 22:23:00 +03: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 03:41:53 +03:00
|
|
|
|
2004-11-01 21:50:36 +03:00
|
|
|
PRUint32 avail;
|
|
|
|
dataStream->Available(&avail);
|
|
|
|
|
|
|
|
return aListener->OnDataAvailable(request, nsnull, dataStream, 0, avail);
|
2000-03-02 23:48:40 +03:00
|
|
|
}
|
2001-03-08 03:41:53 +03:00
|
|
|
#define SEND_DATA(x) SendData(x, converterListener, request)
|
2000-03-02 23:48:40 +03:00
|
|
|
|
1999-08-25 02:04:05 +04:00
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2002-07-04 18:29:25 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIServiceManager> servMan;
|
|
|
|
NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
|
|
|
|
nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
|
|
|
|
NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
|
|
|
|
if (registrar)
|
|
|
|
registrar->AutoRegister(nsnull);
|
2002-01-30 00:22:13 +03:00
|
|
|
|
2002-07-04 18:29:25 +04:00
|
|
|
// Create the Event Queue for this thread...
|
|
|
|
nsCOMPtr<nsIEventQueueService> eventQService =
|
|
|
|
do_GetService(kEventQueueServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-08-25 02:04:05 +04:00
|
|
|
|
2002-07-04 18:29:25 +04:00
|
|
|
eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
|
1999-08-25 02:04:05 +04:00
|
|
|
|
2002-07-04 18:29:25 +04:00
|
|
|
nsCOMPtr<nsICategoryManager> catman =
|
|
|
|
do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
|
2001-03-08 03:41:53 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2005-11-08 22:23:00 +03:00
|
|
|
nsCString previous;
|
2002-07-04 18:29:25 +04:00
|
|
|
|
|
|
|
///////////////////////////////////////////
|
|
|
|
// BEGIN - Stream converter registration
|
|
|
|
// All stream converters must register with the ComponentManager
|
|
|
|
///////////////////////////////////////////
|
|
|
|
|
|
|
|
// these stream converters are just for testing. running this harness
|
|
|
|
// from the dist/bin dir will also pickup converters registered
|
|
|
|
// in other modules (necko converters for example).
|
|
|
|
|
|
|
|
PRUint32 converterListSize = 7;
|
|
|
|
const char *const converterList[] = {
|
|
|
|
"?from=a/foo&to=b/foo",
|
|
|
|
"?from=b/foo&to=c/foo",
|
|
|
|
"?from=b/foo&to=d/foo",
|
|
|
|
"?from=c/foo&to=d/foo",
|
|
|
|
"?from=d/foo&to=e/foo",
|
|
|
|
"?from=d/foo&to=f/foo",
|
|
|
|
"?from=t/foo&to=k/foo",
|
|
|
|
};
|
|
|
|
|
|
|
|
TestConverterFactory *convFactory = new TestConverterFactory(kTestConverterCID, "TestConverter", NS_ISTREAMCONVERTER_KEY);
|
|
|
|
nsCOMPtr<nsIFactory> convFactSup(do_QueryInterface(convFactory, &rv));
|
2001-03-08 03:41:53 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-11-11 03:39:21 +03:00
|
|
|
|
2002-07-04 18:29:25 +04:00
|
|
|
for (PRUint32 count = 0; count < converterListSize; ++count) {
|
|
|
|
// register the TestConverter with the component manager. One contractid registration
|
|
|
|
// per conversion pair (from - to pair).
|
|
|
|
nsCString contractID(NS_ISTREAMCONVERTER_KEY);
|
|
|
|
contractID.Append(converterList[count]);
|
2004-11-01 21:50:36 +03:00
|
|
|
rv = registrar->RegisterFactory(kTestConverterCID,
|
|
|
|
"TestConverter",
|
|
|
|
contractID.get(),
|
|
|
|
convFactSup);
|
2002-07-04 18:29:25 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
rv = catman->AddCategoryEntry(NS_ISTREAMCONVERTER_KEY, converterList[count], "x",
|
|
|
|
PR_TRUE, PR_TRUE, getter_Copies(previous));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
1999-08-25 02:04:05 +04:00
|
|
|
|
2002-07-04 18:29:25 +04:00
|
|
|
nsCOMPtr<nsIStreamConverterService> StreamConvService =
|
|
|
|
do_GetService(kStreamConverterServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// Define the *from* content type and *to* content-type for conversion.
|
2004-06-29 20:45:07 +04:00
|
|
|
static const char fromStr[] = "a/foo";
|
|
|
|
static const char toStr[] = "c/foo";
|
2000-04-25 09:40:45 +04:00
|
|
|
|
1999-08-25 02:04:05 +04:00
|
|
|
#ifdef ASYNC_TEST
|
2004-06-29 20:45:07 +04:00
|
|
|
// ASYNCHRONOUS conversion
|
2002-07-04 18:29:25 +04: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");
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-03-08 03:41:53 +03:00
|
|
|
|
2002-07-04 18:29:25 +04:00
|
|
|
rv = NS_NewInputStreamChannel(getter_AddRefs(channel),
|
|
|
|
dummyURI,
|
|
|
|
nsnull, // inStr
|
|
|
|
"text/plain", // content-type
|
|
|
|
-1); // XXX fix contentLength
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-08-29 18:11:41 +04:00
|
|
|
|
2002-07-04 18:29:25 +04:00
|
|
|
nsCOMPtr<nsIRequest> request(do_QueryInterface(channel));
|
|
|
|
#endif
|
1999-08-25 02:04:05 +04:00
|
|
|
|
2002-07-04 18:29:25 +04:00
|
|
|
nsCOMPtr<nsIRequest> request;
|
1999-08-25 02:04:05 +04:00
|
|
|
|
2002-07-04 18:29:25 +04: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 18:11:41 +04:00
|
|
|
|
2002-07-04 18:29:25 +04: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.
|
|
|
|
nsIStreamListener *converterListener = nsnull;
|
2004-06-29 20:45:07 +04:00
|
|
|
rv = StreamConvService->AsyncConvertData(fromStr, toStr,
|
2002-07-04 18:29:25 +04:00
|
|
|
dataReceiver, nsnull, &converterListener);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
NS_RELEASE(dataReceiver);
|
2001-02-21 23:38:08 +03:00
|
|
|
|
2002-07-04 18:29:25 +04: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.
|
|
|
|
rv = converterListener->OnStartRequest(request, nsnull);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2000-03-02 23:48:40 +03:00
|
|
|
|
2002-07-04 18:29:25 +04:00
|
|
|
rv = SEND_DATA("aaa");
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-08-29 18:11:41 +04:00
|
|
|
|
2002-07-04 18:29:25 +04:00
|
|
|
rv = SEND_DATA("aaa");
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-03-08 03:41:53 +03:00
|
|
|
|
2002-07-04 18:29:25 +04:00
|
|
|
// Finish the request.
|
|
|
|
rv = converterListener->OnStopRequest(request, nsnull, rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-08-29 18:11:41 +04:00
|
|
|
|
2002-07-04 18:29:25 +04:00
|
|
|
NS_RELEASE(converterListener);
|
1999-08-25 02:04:05 +04:00
|
|
|
#else
|
2004-06-29 20:45:07 +04:00
|
|
|
// SYNCHRONOUS conversion
|
2002-07-04 18:29:25 +04:00
|
|
|
nsCOMPtr<nsIInputStream> convertedData;
|
2004-06-29 20:45:07 +04:00
|
|
|
rv = StreamConvService->Convert(inputData, fromStr, toStr,
|
2002-07-04 18:29:25 +04:00
|
|
|
nsnull, getter_AddRefs(convertedData));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-03-08 03:41:53 +03:00
|
|
|
#endif
|
1999-08-25 02:04:05 +04:00
|
|
|
|
2002-07-04 18:29:25 +04:00
|
|
|
// Enter the message pump to allow the URL load to proceed.
|
|
|
|
while ( gKeepRunning ) {
|
|
|
|
PLEvent *gEvent;
|
2003-04-09 02:01:43 +04:00
|
|
|
gEventQ->WaitForEvent(&gEvent);
|
|
|
|
gEventQ->HandleEvent(gEvent);
|
2002-07-04 18:29:25 +04:00
|
|
|
/* gKeepRunning = PR_FALSE; */
|
|
|
|
}
|
|
|
|
} // this scopes the nsCOMPtrs
|
|
|
|
// no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
|
|
|
|
NS_ShutdownXPCOM(nsnull);
|
2001-04-19 23:43:43 +04:00
|
|
|
return rv;
|
1999-08-25 02:04:05 +04:00
|
|
|
}
|