Bug 1188644 - Use channel->ascynOpen2 in netwerk/test (r=mcmanus)

This commit is contained in:
Christoph Kerschbaumer 2015-08-10 10:20:40 -07:00
parent 6005a1a10c
commit d0e2fd6fe9
7 changed files with 4 additions and 941 deletions

View File

@ -1,283 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
#include "TestCommon.h"
#include <stdio.h>
#ifdef WIN32
#include <windows.h>
#endif
#include "nspr.h"
#include "nscore.h"
#include "nsCOMPtr.h"
#include "nsIServiceManager.h"
#include "nsIStreamListener.h"
#include "nsIInputStream.h"
#include "nsIChannel.h"
#include "nsIURL.h"
#include "nsIInterfaceRequestor.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIDNSService.h"
#include "mozilla/Attributes.h"
#include "nsISimpleEnumerator.h"
#include "nsNetUtil.h"
#include "nsIScriptSecurityManager.h"
#include "nsServiceManagerUtils.h"
#include "nsStringAPI.h"
static bool gError = false;
static int32_t gKeepRunning = 0;
#define NS_IEQUALS_IID \
{ 0x11c5c8ee, 0x1dd2, 0x11b2, \
{ 0xa8, 0x93, 0xbb, 0x23, 0xa1, 0xb6, 0x27, 0x76 }}
class nsIEquals : public nsISupports {
public:
NS_DECLARE_STATIC_IID_ACCESSOR(NS_IEQUALS_IID)
NS_IMETHOD Equals(void *aPtr, bool *_retval) = 0;
};
NS_DEFINE_STATIC_IID_ACCESSOR(nsIEquals, NS_IEQUALS_IID)
class ConsumerContext final : public nsIEquals {
~ConsumerContext() {}
public:
NS_DECL_THREADSAFE_ISUPPORTS
ConsumerContext() { }
NS_IMETHOD Equals(void *aPtr, bool *_retval) override {
*_retval = true;
if (aPtr != this) *_retval = false;
return NS_OK;
}
};
NS_IMPL_ISUPPORTS(ConsumerContext, nsIEquals)
class Consumer : public nsIStreamListener {
virtual ~Consumer();
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIREQUESTOBSERVER
NS_DECL_NSISTREAMLISTENER
Consumer();
nsresult Init(nsIURI *aURI, nsIChannel *aChannel, nsISupports *aContext);
nsresult Validate(nsIRequest *request, nsISupports *aContext);
// member data
bool mOnStart; // have we received an OnStart?
bool mOnStop; // have we received an onStop?
int32_t mOnDataCount; // number of times OnData was called.
nsCOMPtr<nsIURI> mURI;
nsCOMPtr<nsIChannel> mChannel;
nsCOMPtr<nsIEquals> mContext;
};
// nsISupports implementation
NS_IMPL_ISUPPORTS(Consumer, nsIStreamListener, nsIRequestObserver)
// nsIRequestObserver implementation
NS_IMETHODIMP
Consumer::OnStartRequest(nsIRequest *request, nsISupports* aContext) {
fprintf(stderr, "Consumer::OnStart() -> in\n\n");
if (mOnStart) {
fprintf(stderr, "INFO: multiple OnStarts received\n");
}
mOnStart = true;
nsresult rv = Validate(request, aContext);
if (NS_FAILED(rv)) return rv;
fprintf(stderr, "Consumer::OnStart() -> out\n\n");
return rv;
}
NS_IMETHODIMP
Consumer::OnStopRequest(nsIRequest *request, nsISupports *aContext,
nsresult aStatus) {
fprintf(stderr, "Consumer::OnStop() -> in\n\n");
if (!mOnStart) {
gError = true;
fprintf(stderr, "ERROR: No OnStart received\n");
}
if (mOnStop) {
fprintf(stderr, "INFO: multiple OnStops received\n");
}
fprintf(stderr, "INFO: received %d OnData()s\n", mOnDataCount);
mOnStop = true;
nsresult rv = Validate(request, aContext);
if (NS_FAILED(rv)) return rv;
fprintf(stderr, "Consumer::OnStop() -> out\n\n");
return rv;
}
// nsIStreamListener implementation
NS_IMETHODIMP
Consumer::OnDataAvailable(nsIRequest *request, nsISupports *aContext,
nsIInputStream *aIStream,
uint64_t aOffset, uint32_t aLength) {
fprintf(stderr, "Consumer::OnData() -> in\n\n");
if (!mOnStart) {
gError = true;
fprintf(stderr, "ERROR: No OnStart received\n");
}
mOnDataCount += 1;
nsresult rv = Validate(request, aContext);
if (NS_FAILED(rv)) return rv;
fprintf(stderr, "Consumer::OnData() -> out\n\n");
return rv;
}
// Consumer implementation
Consumer::Consumer() {
mOnStart = mOnStop = false;
mOnDataCount = 0;
gKeepRunning++;
}
Consumer::~Consumer() {
fprintf(stderr, "Consumer::~Consumer -> in\n\n");
if (!mOnStart) {
gError = true;
fprintf(stderr, "ERROR: Never got an OnStart\n");
}
if (!mOnStop) {
gError = true;
fprintf(stderr, "ERROR: Never got an OnStop \n");
}
fprintf(stderr, "Consumer::~Consumer -> out\n\n");
if (--gKeepRunning == 0)
QuitPumpingEvents();
}
nsresult
Consumer::Init(nsIURI *aURI, nsIChannel* aChannel, nsISupports *aContext) {
mURI = aURI;
mChannel = aChannel;
mContext = do_QueryInterface(aContext);
return NS_OK;
}
nsresult
Consumer::Validate(nsIRequest* request, nsISupports *aContext) {
nsresult rv = NS_OK;
nsCOMPtr<nsIURI> uri;
nsCOMPtr<nsIChannel> aChannel = do_QueryInterface(request);
rv = aChannel->GetURI(getter_AddRefs(uri));
if (NS_FAILED(rv)) return rv;
bool same = false;
rv = mURI->Equals(uri, &same);
if (NS_FAILED(rv)) return rv;
if (!same)
fprintf(stderr, "INFO: URIs do not match\n");
rv = mContext->Equals((void*)aContext, &same);
if (NS_FAILED(rv)) return rv;
if (!same) {
gError = true;
fprintf(stderr, "ERROR: Contexts do not match\n");
}
return rv;
}
nsresult StartLoad(const char *);
int main(int argc, char *argv[]) {
if (test_common_init(&argc, &argv) != 0)
return -1;
nsresult rv = NS_OK;
bool cmdLineURL = false;
if (argc > 1) {
// run in signle url mode
cmdLineURL = true;
}
rv = NS_InitXPCOM2(nullptr, nullptr, nullptr);
if (NS_FAILED(rv)) return -1;
if (cmdLineURL) {
rv = StartLoad(argv[1]);
} else {
rv = StartLoad("http://badhostnamexyz/test.txt");
}
if (NS_FAILED(rv)) return -1;
// Enter the message pump to allow the URL load to proceed.
PumpEvents();
NS_ShutdownXPCOM(nullptr);
if (gError) {
fprintf(stderr, "\n\n-------ERROR-------\n\n");
}
return 0;
}
nsresult StartLoad(const char *aURISpec) {
nsresult rv = NS_OK;
// create a context
ConsumerContext *context = new ConsumerContext;
nsCOMPtr<nsISupports> contextSup = do_QueryInterface(context, &rv);
if (NS_FAILED(rv)) return rv;
// create a uri
nsCOMPtr<nsIURI> uri;
rv = NS_NewURI(getter_AddRefs(uri), aURISpec);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIScriptSecurityManager> secman =
do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIPrincipal> systemPrincipal;
rv = secman->GetSystemPrincipal(getter_AddRefs(systemPrincipal));
if (NS_FAILED(rv)) return rv;
// create a channel
nsCOMPtr<nsIChannel> channel;
rv = NS_NewChannel(getter_AddRefs(channel),
uri,
systemPrincipal,
nsILoadInfo::SEC_NORMAL,
nsIContentPolicy::TYPE_OTHER);
if (NS_FAILED(rv)) return rv;
Consumer *consumer = new Consumer;
rv = consumer->Init(uri, channel, contextSup);
if (NS_FAILED(rv)) return rv;
// kick off the load
nsCOMPtr<nsIRequest> request;
return channel->AsyncOpen(static_cast<nsIStreamListener*>(consumer), contextSup);
}

View File

@ -1,188 +0,0 @@
#include "nsNetUtil.h"
#include "nsIEventQueueService.h"
#include "nsIServiceManager.h"
#include "nsIComponentRegistrar.h"
#include "nsIInterfaceRequestor.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIProgressEventSink.h"
#include <algorithm>
#include "nsIContentPolicy.h"
#include "mozilla/LoadInfo.h"
#include "nsContentUtils.h"
#define RETURN_IF_FAILED(rv, step) \
PR_BEGIN_MACRO \
if (NS_FAILED(rv)) { \
printf(">>> %s failed: rv=%x\n", step, rv); \
return rv;\
} \
PR_END_MACRO
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
static nsIEventQueue* gEventQ = nullptr;
static bool gKeepRunning = true;
//-----------------------------------------------------------------------------
// nsIStreamListener implementation
//-----------------------------------------------------------------------------
class MyListener : public nsIStreamListener
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIREQUESTOBSERVER
NS_DECL_NSISTREAMLISTENER
MyListener() { }
virtual ~MyListener() {}
};
NS_IMPL_ISUPPORTS(MyListener,
nsIRequestObserver,
nsIStreamListener)
NS_IMETHODIMP
MyListener::OnStartRequest(nsIRequest *req, nsISupports *ctxt)
{
printf(">>> OnStartRequest\n");
return NS_OK;
}
NS_IMETHODIMP
MyListener::OnStopRequest(nsIRequest *req, nsISupports *ctxt, nsresult status)
{
printf(">>> OnStopRequest status=%x\n", status);
gKeepRunning = false;
return NS_OK;
}
NS_IMETHODIMP
MyListener::OnDataAvailable(nsIRequest *req, nsISupports *ctxt,
nsIInputStream *stream,
uint64_t offset, uint32_t count)
{
printf(">>> OnDataAvailable [count=%u]\n", count);
char buf[256];
nsresult rv;
uint32_t bytesRead=0;
while (count) {
uint32_t amount = std::min<uint32_t>(count, sizeof(buf));
rv = stream->Read(buf, amount, &bytesRead);
if (NS_FAILED(rv)) {
printf(">>> stream->Read failed with rv=%x\n", rv);
return rv;
}
fwrite(buf, 1, bytesRead, stdout);
count -= bytesRead;
}
return NS_OK;
}
//-----------------------------------------------------------------------------
// NotificationCallbacks implementation
//-----------------------------------------------------------------------------
class MyNotifications : public nsIInterfaceRequestor
, public nsIProgressEventSink
{
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIINTERFACEREQUESTOR
NS_DECL_NSIPROGRESSEVENTSINK
MyNotifications() { }
virtual ~MyNotifications() {}
};
NS_IMPL_ISUPPORTS(MyNotifications,
nsIInterfaceRequestor,
nsIProgressEventSink)
NS_IMETHODIMP
MyNotifications::GetInterface(const nsIID &iid, void **result)
{
return QueryInterface(iid, result);
}
NS_IMETHODIMP
MyNotifications::OnStatus(nsIRequest *req, nsISupports *ctx,
nsresult status, const char16_t *statusText)
{
printf("status: %x\n", status);
return NS_OK;
}
NS_IMETHODIMP
MyNotifications::OnProgress(nsIRequest *req, nsISupports *ctx,
uint64_t progress, uint64_t progressMax)
{
printf("progress: %llu/%llu\n", progress, progressMax);
return NS_OK;
}
//-----------------------------------------------------------------------------
// main, etc..
//-----------------------------------------------------------------------------
int main(int argc, char **argv)
{
nsresult rv;
if (argc == 1) {
printf("usage: TestHttp <url>\n");
return -1;
}
{
nsCOMPtr<nsIServiceManager> servMan;
NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
if (registrar)
registrar->AutoRegister(nullptr);
// Create the Event Queue for this thread...
nsCOMPtr<nsIEventQueueService> eqs =
do_GetService(kEventQueueServiceCID, &rv);
RETURN_IF_FAILED(rv, "do_GetService(EventQueueService)");
rv = eqs->CreateMonitoredThreadEventQueue();
RETURN_IF_FAILED(rv, "CreateMonitoredThreadEventQueue");
rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
RETURN_IF_FAILED(rv, "GetThreadEventQueue");
nsCOMPtr<nsIURI> uri;
nsCOMPtr<nsIChannel> chan;
nsCOMPtr<nsIStreamListener> listener = new MyListener();
nsCOMPtr<nsIInterfaceRequestor> callbacks = new MyNotifications();
rv = NS_NewURI(getter_AddRefs(uri), argv[1]);
RETURN_IF_FAILED(rv, "NS_NewURI");
rv = NS_NewChannel(getter_AddRefs(chan),
uri,
nsContentUtils::GetSystemPrincipal(),
nsILoadInfo::SEC_NORMAL,
nsIContentPolicy::TYPE_OTHER);
RETURN_IF_FAILED(rv, "NS_NewChannel");
rv = chan->AsyncOpen(listener, nullptr);
RETURN_IF_FAILED(rv, "AsyncOpen");
while (gKeepRunning)
gEventQ->ProcessPendingEvents();
printf(">>> done\n");
} // this scopes the nsCOMPtrs
// no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
rv = NS_ShutdownXPCOM(nullptr);
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
return 0;
}

View File

@ -1,264 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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/. */
#include "nsIResProtocolHandler.h"
#include "nsIServiceManager.h"
#include "nsIInputStream.h"
#include "nsIComponentManager.h"
#include "nsIComponentRegistrar.h"
#include "nsIStreamListener.h"
#include "nsIEventQueueService.h"
#include "nsIURI.h"
#include "nsCRT.h"
#include "nsNetCID.h"
#include "nsIScriptSecurityManager.h"
#include "nsILoadInfo.h"
#include "nsNetUtil.h"
static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
////////////////////////////////////////////////////////////////////////////////
nsresult
SetupMapping()
{
nsresult rv;
nsCOMPtr<nsIIOService> serv(do_GetService(kIOServiceCID, &rv));
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIProtocolHandler> ph;
rv = serv->GetProtocolHandler("res", getter_AddRefs(ph));
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIResProtocolHandler> resPH = do_QueryInterface(ph, &rv);
if (NS_FAILED(rv)) return rv;
rv = resPH->AppendSubstitution("foo", "file://y|/");
if (NS_FAILED(rv)) return rv;
rv = resPH->AppendSubstitution("foo", "file://y|/mozilla/dist/win32_D.OBJ/bin/");
if (NS_FAILED(rv)) return rv;
return rv;
}
////////////////////////////////////////////////////////////////////////////////
nsresult
TestOpenInputStream(const char* url)
{
nsresult rv;
nsCOMPtr<nsIScriptSecurityManager> secman =
do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIPrincipal> systemPrincipal;
rv = secman->GetSystemPrincipal(getter_AddRefs(systemPrincipal));
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIURI> uri;
rv = NS_NewURI(getter_AddRefs(uri), url);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIChannel> channel;
rv = NS_NewChannel(getter_AddRefs(channel),
uri,
systemPrincipal,
nsILoadInfo::SEC_NORMAL,
nsIContentPolicy::TYPE_OTHER);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIInputStream> in;
rv = channel->Open(getter_AddRefs(in));
if (NS_FAILED(rv)) {
fprintf(stdout, "failed to OpenInputStream for %s\n", url);
return NS_OK;
}
char buf[1024];
while (1) {
uint32_t amt;
rv = in->Read(buf, sizeof(buf), &amt);
if (NS_FAILED(rv)) return rv;
if (amt == 0) break; // eof
char* str = buf;
while (amt-- > 0) {
fputc(*str++, stdout);
}
}
nsCOMPtr<nsIURI> uri;
char* str;
rv = channel->GetOriginalURI(getter_AddRefs(uri));
if (NS_FAILED(rv)) return rv;
rv = uri->GetSpec(&str);
if (NS_FAILED(rv)) return rv;
fprintf(stdout, "%s resolved to ", str);
free(str);
rv = channel->GetURI(getter_AddRefs(uri));
if (NS_FAILED(rv)) return rv;
rv = uri->GetSpec(&str);
if (NS_FAILED(rv)) return rv;
fprintf(stdout, "%s\n", str);
free(str);
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
bool gDone = false;
nsIEventQueue* gEventQ = nullptr;
class Listener : public nsIStreamListener
{
public:
NS_DECL_ISUPPORTS
Listener() {}
virtual ~Listener() {}
NS_IMETHOD OnStartRequest(nsIRequest *request, nsISupports *ctxt) {
nsresult rv;
nsCOMPtr<nsIURI> uri;
nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
rv = channel->GetURI(getter_AddRefs(uri));
if (NS_SUCCEEDED(rv)) {
char* str;
rv = uri->GetSpec(&str);
if (NS_SUCCEEDED(rv)) {
fprintf(stdout, "Starting to load %s\n", str);
free(str);
}
}
return NS_OK;
}
NS_IMETHOD OnStopRequest(nsIRequest *request, nsISupports *ctxt,
nsresult aStatus) {
nsresult rv;
nsCOMPtr<nsIURI> uri;
nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
rv = channel->GetURI(getter_AddRefs(uri));
if (NS_SUCCEEDED(rv)) {
char* str;
rv = uri->GetSpec(&str);
if (NS_SUCCEEDED(rv)) {
fprintf(stdout, "Ending load %s, status=%x\n", str, aStatus);
free(str);
}
}
gDone = true;
return NS_OK;
}
NS_IMETHOD OnDataAvailable(nsIRequest *request, nsISupports *ctxt,
nsIInputStream *inStr,
uint64_t sourceOffset, uint32_t count) {
nsresult rv;
char buf[1024];
while (count > 0) {
uint32_t amt;
rv = inStr->Read(buf, sizeof(buf), &amt);
count -= amt;
char* c = buf;
while (amt-- > 0) {
fputc(*c++, stdout);
}
}
return NS_OK;
}
};
NS_IMPL_ISUPPORTS(Listener, nsIStreamListener, nsIRequestObserver)
nsresult
TestAsyncRead(const char* url)
{
nsresult rv;
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<nsIScriptSecurityManager> secman =
do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIPrincipal> systemPrincipal;
rv = secman->GetSystemPrincipal(getter_AddRefs(systemPrincipal));
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIURI> uri;
rv = NS_NewURI(getter_AddRefs(uri), url);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIChannel> channel;
rv = NS_NewChannel(getter_AddRefs(channel),
uri,
systemPrincipal,
nsILoadInfo::SEC_NORMAL,
nsIContentPolicy::TYPE_OTHER);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIStreamListener> listener = new Listener();
if (listener == nullptr)
return NS_ERROR_OUT_OF_MEMORY;
rv = channel->AsyncOpen(nullptr, listener);
if (NS_FAILED(rv)) return rv;
while (!gDone) {
PLEvent* event;
rv = gEventQ->GetEvent(&event);
if (NS_FAILED(rv)) return rv;
rv = gEventQ->HandleEvent(event);
if (NS_FAILED(rv)) return rv;
}
return rv;
}
int
main(int argc, char* argv[])
{
nsresult rv;
{
nsCOMPtr<nsIServiceManager> servMan;
NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
if (registrar)
registrar->AutoRegister(nullptr);
NS_ASSERTION(NS_SUCCEEDED(rv), "AutoregisterComponents failed");
if (argc < 2) {
printf("usage: %s resource://foo/<path-to-resolve>\n", argv[0]);
return -1;
}
rv = SetupMapping();
NS_ASSERTION(NS_SUCCEEDED(rv), "SetupMapping failed");
if (NS_FAILED(rv)) return rv;
rv = TestOpenInputStream(argv[1]);
NS_ASSERTION(NS_SUCCEEDED(rv), "TestOpenInputStream failed");
rv = TestAsyncRead(argv[1]);
NS_ASSERTION(NS_SUCCEEDED(rv), "TestAsyncRead failed");
} // this scopes the nsCOMPtrs
// no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
rv = NS_ShutdownXPCOM(nullptr);
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
return rv;
}
////////////////////////////////////////////////////////////////////////////////

View File

@ -1,200 +0,0 @@
/* 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/. */
#include "TestCommon.h"
#include "nsIComponentRegistrar.h"
#include "nsIStreamTransportService.h"
#include "nsIAsyncInputStream.h"
#include "nsIProgressEventSink.h"
#include "nsIInterfaceRequestor.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIRequest.h"
#include "nsIServiceManager.h"
#include "nsIComponentManager.h"
#include "nsCOMPtr.h"
#include "nsMemory.h"
#include "nsStringAPI.h"
#include "nsIFileStreams.h"
#include "nsIStreamListener.h"
#include "nsIFile.h"
#include "nsNetUtil.h"
#include "nsAutoLock.h"
#include "mozilla/Logging.h"
#include <algorithm>
////////////////////////////////////////////////////////////////////////////////
//
// set NSPR_LOG_MODULES=Test:5
//
static PRLogModuleInfo *gTestLog = nullptr;
#define LOG(args) MOZ_LOG(gTestLog, mozilla::LogLevel::Debug, args)
////////////////////////////////////////////////////////////////////////////////
static NS_DEFINE_CID(kSimpleURICID, NS_SIMPLEURI_CID);
////////////////////////////////////////////////////////////////////////////////
class MyListener : public nsIStreamListener
{
public:
NS_DECL_ISUPPORTS
MyListener() {}
virtual ~MyListener() {}
NS_IMETHOD OnStartRequest(nsIRequest *req, nsISupports *ctx)
{
LOG(("MyListener::OnStartRequest\n"));
return NS_OK;
}
NS_IMETHOD OnDataAvailable(nsIRequest *req, nsISupports *ctx,
nsIInputStream *stream,
uint32_t offset, uint32_t count)
{
LOG(("MyListener::OnDataAvailable [offset=%u count=%u]\n", offset, count));
char buf[500];
nsresult rv;
while (count) {
uint32_t n, amt = std::min<uint32_t>(count, sizeof(buf));
rv = stream->Read(buf, amt, &n);
if (NS_FAILED(rv)) {
LOG((" read returned 0x%08x\n", rv));
return rv;
}
LOG((" read %u bytes\n", n));
count -= n;
}
return NS_OK;
}
NS_IMETHOD OnStopRequest(nsIRequest *req, nsISupports *ctx, nsresult status)
{
LOG(("MyListener::OnStopRequest [status=%x]\n", status));
QuitPumpingEvents();
return NS_OK;
}
};
NS_IMPL_ISUPPORTS(MyListener,
nsIRequestObserver,
nsIStreamListener)
////////////////////////////////////////////////////////////////////////////////
class MyCallbacks : 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 char16_t *statusArg)
{
LOG(("MyCallbacks::OnStatus [status=%x]\n", status));
return NS_OK;
}
NS_IMETHOD OnProgress(nsIRequest *req, nsISupports *ctx,
uint64_t progress, uint64_t progressMax)
{
LOG(("MyCallbacks::OnProgress [progress=%llu/%llu]\n", progress, progressMax));
return NS_OK;
}
};
NS_IMPL_ISUPPORTS(MyCallbacks,
nsIInterfaceRequestor,
nsIProgressEventSink)
////////////////////////////////////////////////////////////////////////////////
/**
* asynchronously copy file.
*/
static nsresult
RunTest(nsIFile *file)
{
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(), nullptr);
if (NS_FAILED(rv)) return rv;
PumpEvents();
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
int
main(int argc, char* argv[])
{
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), nullptr, nullptr);
nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
if (registrar)
registrar->AutoRegister(nullptr);
gTestLog = PR_NewLogModule("Test");
nsCOMPtr<nsIFile> file;
rv = NS_NewNativeLocalFile(nsDependentCString(fileName), false, getter_AddRefs(file));
if (NS_FAILED(rv)) return rv;
rv = RunTest(file);
NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");
// 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(nullptr);
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
return NS_OK;
}

View File

@ -74,7 +74,7 @@ int main(int argc, char **argv)
rv = NS_NewChannel(getter_AddRefs(chan),
uri,
systemPrincipal,
nsILoadInfo::SEC_NORMAL,
nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_INHERITS,
nsIContentPolicy::TYPE_OTHER);
if (NS_FAILED(rv))
@ -89,7 +89,7 @@ int main(int argc, char **argv)
if (NS_FAILED(rv))
return -1;
rv = chan->AsyncOpen(loader, nullptr);
rv = chan->AsyncOpen2(loader);
if (NS_FAILED(rv))
return -1;

View File

@ -140,7 +140,7 @@ main(int argc, char* argv[])
rv = NS_NewChannel(getter_AddRefs(channel),
uri,
systemPrincipal,
nsILoadInfo::SEC_NORMAL,
nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_INHERITS,
nsIContentPolicy::TYPE_OTHER);
if (NS_FAILED(rv)) return -1;
@ -158,7 +158,7 @@ main(int argc, char* argv[])
}
NS_ADDREF(listener);
channel->AsyncOpen(listener, nullptr);
channel->AsyncOpen2(listener);
PumpEvents();
} // this scopes the nsCOMPtrs

View File

@ -21,7 +21,6 @@ GeckoSimplePrograms([
'PropertiesTest',
'ReadNTLM',
'TestBlockingSocket',
'TestCallbacks',
'TestDNS',
'TestIncrementalDownload',
'TestOpen',
@ -39,7 +38,6 @@ GeckoSimplePrograms([
# TestIDN',
# TestIOThreads',
# TestSocketTransport',
# TestStreamChannel',
# TestStreamPump',
# TestStreamTransport',
# TestUDPSocketProvider',