Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Functions | Variables
TestStreamChannel.cpp File Reference
#include "TestCommon.h"
#include "nsIComponentRegistrar.h"
#include "nsIStreamTransportService.h"
#include "nsIAsyncInputStream.h"
#include "nsIProgressEventSink.h"
#include "nsIInterfaceRequestor.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIProxyObjectManager.h"
#include "nsIRequest.h"
#include "nsIServiceManager.h"
#include "nsIComponentManager.h"
#include "nsCOMPtr.h"
#include "nsMemory.h"
#include "nsString.h"
#include "nsIFileStreams.h"
#include "nsIStreamListener.h"
#include "nsIEventQueueService.h"
#include "nsIEventQueue.h"
#include "nsILocalFile.h"
#include "nsNetUtil.h"
#include "nsAutoLock.h"
#include "prlog.h"

Go to the source code of this file.

Classes

class  MyListener

Defines

#define LOG(args)

Functions

static NS_DEFINE_CID (kSimpleURICID, NS_SIMPLEURI_CID)
static NS_DEFINE_CID (kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID)
static void *PR_CALLBACK DoneEvent_Handler (PLEvent *ev)
static void PR_CALLBACK DoneEvent_Cleanup (PLEvent *ev)
static void PostDoneEvent ()
 NS_IMPL_ISUPPORTS2 (MyListener, nsIRequestObserver, nsIStreamListener) class MyCallbacks
 NS_IMPL_ISUPPORTS2 (MyCallbacks, nsIInterfaceRequestor, nsIProgressEventSink) static nsresult RunTest(nsIFile *file)
 asynchronously copy file.
int main (int argc, char *argv[])

Variables

PRBool gDone = PR_FALSE
nsIEventQueuegEventQ = nsnull

Define Documentation

#define LOG (   args)

Definition at line 70 of file TestStreamChannel.cpp.


Function Documentation

static void PR_CALLBACK DoneEvent_Cleanup ( PLEvent ev) [static]

Definition at line 91 of file TestStreamChannel.cpp.

{
    delete ev;
}

Here is the caller graph for this function:

static void* PR_CALLBACK DoneEvent_Handler ( PLEvent ev) [static]

Definition at line 84 of file TestStreamChannel.cpp.

{
    gDone = PR_TRUE;
    return nsnull;
}

Here is the caller graph for this function:

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

Definition at line 243 of file TestStreamChannel.cpp.

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

    nsresult rv;

    if (argc < 2) {
        printf("usage: %s <file-to-read>\n", argv[0]);
        return -1;
    }
    char* fileName = argv[1];
    {
        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);

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

        nsCOMPtr<nsIEventQueueService> eventQService =
                 do_GetService(kEventQueueServiceCID, &rv);
        if (NS_FAILED(rv)) return rv;

        rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
        if (NS_FAILED(rv)) return rv;

        nsCOMPtr<nsILocalFile> file;
        rv = NS_NewNativeLocalFile(nsDependentCString(fileName), PR_FALSE, getter_AddRefs(file));
        if (NS_FAILED(rv)) return rv;

        rv = RunTest(file);
        NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");

        NS_RELEASE(gEventQ);

        // give background threads a chance to finish whatever work they may
        // be doing.
        PR_Sleep(PR_SecondsToInterval(1));
    } // 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 NS_OK;
}

Here is the call graph for this function:

static NS_DEFINE_CID ( kSimpleURICID  ,
NS_SIMPLEURI_CID   
) [static]

Definition at line 159 of file TestStreamChannel.cpp.

                  : public nsIInterfaceRequestor
                  , public nsIProgressEventSink
{
public:
    NS_DECL_ISUPPORTS

    MyCallbacks() {}
    virtual ~MyCallbacks() {}

    NS_IMETHOD GetInterface(const nsID &iid, void **result)
    {
        return QueryInterface(iid, result);
    }

    NS_IMETHOD OnStatus(nsIRequest *req, nsISupports *ctx, nsresult status,
                        const PRUnichar *statusArg)
    {
        LOG(("MyCallbacks::OnStatus [status=%x]\n", status));
        return NS_OK;
    }

    NS_IMETHOD OnProgress(nsIRequest *req, nsISupports *ctx,
                          PRUint64 progress, PRUint64 progressMax)
    {
        LOG(("MyCallbacks::OnProgress [progress=%llu/%llu]\n", progress, progressMax));
        return NS_OK;
    }
};

asynchronously copy file.

Definition at line 194 of file TestStreamChannel.cpp.

{
    nsresult rv;

    LOG(("RunTest\n"));

    nsCOMPtr<nsIInputStream> stream;
    rv = NS_NewLocalFileInputStream(getter_AddRefs(stream), file);
    if (NS_FAILED(rv)) return rv;

    nsCOMPtr<nsIURI> uri = do_CreateInstance(kSimpleURICID);
    if (uri)
        uri->SetSpec(NS_LITERAL_CSTRING("foo://bar"));

    nsCOMPtr<nsIChannel> chan;
    rv = NS_NewInputStreamChannel(getter_AddRefs(chan), uri, stream);
    if (NS_FAILED(rv)) return rv;

    rv = chan->SetNotificationCallbacks(new MyCallbacks());
    if (NS_FAILED(rv)) return rv;

    rv = chan->AsyncOpen(new MyListener(), nsnull);
    if (NS_FAILED(rv)) return rv;

    gDone = PR_FALSE;
    while (!gDone) {
        PLEvent *event;
        rv = gEventQ->WaitForEvent(&event);
        if (NS_FAILED(rv)) return rv;
        rv = gEventQ->HandleEvent(event);
        if (NS_FAILED(rv)) return rv;
    }

    return NS_OK;
}

Here is the call graph for this function:

static void PostDoneEvent ( ) [static]

Definition at line 97 of file TestStreamChannel.cpp.

{
    LOG(("PostDoneEvent\n"));

    PLEvent *ev = new PLEvent();

    PL_InitEvent(ev, nsnull, 
            DoneEvent_Handler,
            DoneEvent_Cleanup);

    gEventQ->PostEvent(ev);
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 78 of file TestStreamChannel.cpp.

Definition at line 79 of file TestStreamChannel.cpp.