Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Functions | Variables
TestUpload.cpp File Reference
#include "TestCommon.h"
#include "nsIComponentRegistrar.h"
#include "nsIEventQueueService.h"
#include "nsIIOService.h"
#include "nsIServiceManager.h"
#include "nsNetUtil.h"
#include "nsIUploadChannel.h"
#include "prlog.h"

Go to the source code of this file.

Classes

class  InputTestConsumer

Defines

#define LOG(args)   PR_LOG(gTestLog, PR_LOG_DEBUG, args)

Functions

static NS_DEFINE_CID (kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID)
static NS_DEFINE_CID (kIOServiceCID, NS_IOSERVICE_CID)
int main (int argc, char *argv[])

Variables

static int gKeepRunning = 1
static nsIEventQueuegEventQ = nsnull

Define Documentation

#define LOG (   args)    PR_LOG(gTestLog, PR_LOG_DEBUG, args)

Definition at line 61 of file TestUpload.cpp.


Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 135 of file TestUpload.cpp.

{
    if (test_common_init(&argc, &argv) != 0)
        return -1;

    nsresult rv;

    if (argc < 2) {
        printf("usage: %s <url> <file-to-upload>\n", argv[0]);
        return -1;
    }
    char* uriSpec  = argv[1];
    char* fileName = argv[2];

#if defined(PR_LOGGING) 
    gTestLog = PR_NewLogModule("Test");
#endif

    {
        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);

        // Create the Event Queue for this thread...
        nsCOMPtr<nsIEventQueueService> eventQService =
                 do_GetService(kEventQueueServiceCID, &rv);
        if (NS_FAILED(rv)) return rv;

        eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);

        nsCOMPtr<nsIIOService> ioService(do_GetService(kIOServiceCID, &rv));
        // first thing to do is create ourselves a stream that
        // is to be uploaded.
        nsCOMPtr<nsIInputStream> uploadStream;
        rv = NS_NewPostDataStream(getter_AddRefs(uploadStream),
                                  PR_TRUE,
                                  nsDependentCString(fileName), // XXX UTF-8
                                  0, ioService);
        if (NS_FAILED(rv)) return rv;

        // create our url.
        nsCOMPtr<nsIURI> uri;
        rv = NS_NewURI(getter_AddRefs(uri), uriSpec);
        if (NS_FAILED(rv)) return rv;

        nsCOMPtr<nsIChannel> channel;
        rv = ioService->NewChannelFromURI(uri, getter_AddRefs(channel));
        if (NS_FAILED(rv)) return rv;
       
        // QI and set the upload stream
        nsCOMPtr<nsIUploadChannel> uploadChannel(do_QueryInterface(channel));
        uploadChannel->SetUploadStream(uploadStream, EmptyCString(), -1);

        // create a dummy listener
        InputTestConsumer* listener;

        listener = new InputTestConsumer;
        if (!listener) {
            NS_ERROR("Failed to create a new stream listener!");
            return -1;
        }
        NS_ADDREF(listener);

        channel->AsyncOpen(listener, nsnull);

        while ( gKeepRunning ) {
            PLEvent *gEvent;
            gEventQ->WaitForEvent(&gEvent);
            gEventQ->HandleEvent(gEvent);
        }
    } // this scopes the nsCOMPtrs
    // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
    rv = NS_ShutdownXPCOM(nsnull);
    NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");

    return 0;
}

Here is the call graph for this function:

static NS_DEFINE_CID ( kIOServiceCID  ,
NS_IOSERVICE_CID   
) [static]

Variable Documentation

Definition at line 64 of file TestUpload.cpp.

int gKeepRunning = 1 [static]

Definition at line 63 of file TestUpload.cpp.