Bug 1119006 - Remove files where code is still calling removed NS_OpenURI API (r=mcmanus)

This commit is contained in:
Christoph Kerschbaumer 2015-01-14 11:52:57 -08:00
parent 1d8b9c525f
commit 943d34a148
9 changed files with 2 additions and 1197 deletions

View File

@ -39,32 +39,6 @@
q: cite
*/
/* Here is how to open a channel for testing
(from embed/qa/testembed/Tests.cpp):
nsCOMPtr<nsIChannel> theChannel;
nsCString uri;
nsCOMPtr<nsIURI> theURI;
rv = NS_NewURI(getter_AddRefs(theURI), theSpec);
if (!theURI)
error;
rv = NS_OpenURI(getter_AddRefs(theChannel), theURI, nullptr, theLoadGroup);
if (!theChannel)
error;
nsCOMPtr<nsILoadGroup> theLoadGroup(do_CreateInstance(NS_LOADGROUP_CONTRACTID));
if (!theLoadGroup)
error;
nsCOMPtr<nsIStreamListener> listener(static_cast<nsIStreamListener*>(qaBrowserImpl));
//nsCOMPtr<nsIWeakReference> thisListener(do_GetWeakReference(listener));
//qaWebBrowser->AddWebBrowserListener(thisListener, NS_GET_IID(nsIStreamListener));
// this calls nsIStreamListener::OnDataAvailable()
rv = theChannel->AsyncOpen(listener, nullptr);
nsCOMPtr<nsIRequest> theRequest = do_QueryInterface(theChannel);
// Now we can do things on nsIRequest (like what?)
*/
#include "nsHTMLURIRefObject.h"
#include "mozilla/mozalloc.h"

View File

@ -171,7 +171,7 @@ int main(int argc, char **argv)
nsILoadInfo::SEC_NORMAL,
nsIContentPolicy::TYPE_OTHER);
RETURN_IF_FAILED(rv, "NS_OpenURI");
RETURN_IF_FAILED(rv, "NS_NewChannel");
rv = chan->AsyncOpen(listener, nullptr);
RETURN_IF_FAILED(rv, "AsyncOpen");

View File

@ -1,398 +0,0 @@
/* -*- Mode: C++; tab-width: 4; 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 "nsNetUtil.h"
#include "nsIServiceManager.h"
#include "nsIInterfaceRequestor.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIProgressEventSink.h"
#include "nsIComponentManager.h"
#include "prprf.h"
#include "nsXPCOM.h"
#include "nsISupportsPrimitives.h"
#include "plstr.h"
#include "nsCOMArray.h"
#include "nsIComponentRegistrar.h"
#include <algorithm>
#include "nsIScriptSecurityManager.h"
namespace TestPageLoad {
int getStrLine(const char *src, char *str, int ind, int max);
nsresult auxLoad(char *uriBuf);
//----------------------------------------------------------------------
#define RETURN_IF_FAILED(rv, ret, step) \
PR_BEGIN_MACRO \
if (NS_FAILED(rv)) { \
printf(">>> %s failed: rv=%x\n", step, static_cast<uint32_t>(rv)); \
return ret;\
} \
PR_END_MACRO
static nsCString globalStream;
//static char urlBuf[256];
static nsCOMPtr<nsIURI> baseURI;
static nsCOMArray<nsIURI> uriList;
//Temp, should remove:
static int numStart=0;
static int numFound=0;
static int32_t gKeepRunning = 0;
//--------writer fun----------------------
static NS_METHOD streamParse (nsIInputStream* in,
void* closure,
const char* fromRawSegment,
uint32_t toOffset,
uint32_t count,
uint32_t *writeCount) {
char parseBuf[2048], loc[2048], lineBuf[2048];
char *loc_t, *loc_t2;
int i = 0;
const char *tmp;
if(!globalStream.IsEmpty()) {
globalStream.Append(fromRawSegment);
tmp = globalStream.get();
//printf("\n>>NOW:\n^^^^^\n%s\n^^^^^^^^^^^^^^", tmp);
} else {
tmp = fromRawSegment;
}
while(i < (int)count) {
i = getStrLine(tmp, lineBuf, i, count);
if(i < 0) {
*writeCount = count;
return NS_OK;
}
parseBuf[0]='\0';
if((loc_t=PL_strcasestr(lineBuf, "img"))!= nullptr
|| (loc_t=PL_strcasestr(lineBuf, "script"))!=nullptr) {
loc_t2=PL_strcasestr(loc_t, "src");
if(loc_t2!=nullptr) {
loc_t2+=3;
strcpy(loc, loc_t2);
sscanf(loc, "=\"%[^\"]", parseBuf);
if(parseBuf[0]=='\0')
sscanf(loc, "=%s", parseBuf);
if(parseBuf[0]!='\0'){
numFound++;
auxLoad(parseBuf);
}
}
}
/***NEED BETTER CHECK FOR STYLESHEETS
if((loc_t=PL_strcasestr(lineBuf, "link"))!= nullptr) {
loc_t2=PL_strcasestr(loc_t, "href");
if(loc_t2!=nullptr) {
loc_t2+=4;
strcpy(loc, loc_t2);
//printf("%s\n", loc);
sscanf(loc, "=\"%[^\"]", parseBuf);
if(parseBuf[0]!='\0'){
//printf("%s\n", parseBuf);
numFound++;
auxLoad(parseBuf);
}
}
}
*/
if((loc_t=PL_strcasestr(lineBuf, "background"))!=nullptr) {
loc_t+=10;
strcpy(loc, loc_t);
sscanf(loc, "=\"%[^\"]", parseBuf);
if(parseBuf[0]!='\0') {
numFound++;
auxLoad(parseBuf);
}
}
i++;
}
*writeCount = count;
return NS_OK;
}
//-----------------------------------------------------------------------------
// nsIStreamListener implementation
//-----------------------------------------------------------------------------
class MyListener : public nsIStreamListener
{
virtual ~MyListener() {}
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIREQUESTOBSERVER
NS_DECL_NSISTREAMLISTENER
MyListener() { }
};
NS_IMPL_ISUPPORTS(MyListener,
nsIRequestObserver,
nsIStreamListener)
NS_IMETHODIMP
MyListener::OnStartRequest(nsIRequest *req, nsISupports *ctxt)
{
//printf(">>> OnStartRequest\n");
numStart++;
return NS_OK;
}
NS_IMETHODIMP
MyListener::OnStopRequest(nsIRequest *req, nsISupports *ctxt, nsresult status)
{
//printf(">>> OnStopRequest status=%x\n", status);
if (--gKeepRunning == 0)
QuitPumpingEvents();
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);
nsresult rv = NS_ERROR_FAILURE;
uint32_t bytesRead=0;
char buf[1024];
if(ctxt == nullptr) {
bytesRead=0;
rv = stream->ReadSegments(streamParse, nullptr, count, &bytesRead);
} else {
while (count) {
uint32_t amount = std::min<uint32_t>(count, sizeof(buf));
rv = stream->Read(buf, amount, &bytesRead);
count -= bytesRead;
}
}
if (NS_FAILED(rv)) {
printf(">>> stream->Read failed with rv=%x\n",
static_cast<uint32_t>(rv));
return rv;
}
return NS_OK;
}
//-----------------------------------------------------------------------------
// NotificationCallbacks implementation
//-----------------------------------------------------------------------------
class MyNotifications : public nsIInterfaceRequestor
, public nsIProgressEventSink
{
virtual ~MyNotifications() {}
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIINTERFACEREQUESTOR
NS_DECL_NSIPROGRESSEVENTSINK
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)
{
// char buf[100];
// PR_snprintf(buf, sizeof(buf), "%llu/%llu\n", progress, progressMax);
// printf("%s", buf);
return NS_OK;
}
//-----------------------------------------------------------------------------
// main, etc..
//-----------------------------------------------------------------------------
//---------getStrLine Helper function---------------
//Finds a newline in src starting at ind. Puts the
//line in str (must be big enough). Returns the index
//of the newline, or -1 if at end of string. If reaches
//end of string ('\0'), then will copy contents to
//globalStream.
int getStrLine(const char *src, char *str, int ind, int max) {
char c = src[ind];
int i=0;
globalStream.Assign('\0');
while(c!='\n' && c!='\0' && i<max) {
str[i] = src[ind];
i++; ind++;
c = src[ind];
}
str[i]='\0';
if(i==max || c=='\0') {
globalStream.Assign(str);
//printf("\nCarryover (%d|%d):\n------------\n%s\n-------\n",i,max,str);
return -1;
}
return ind;
}
//----------AUX LOAD-----------
nsresult auxLoad(char *uriBuf)
{
nsresult rv;
nsCOMPtr<nsISupportsPRBool> myBool = do_CreateInstance(NS_SUPPORTS_PRBOOL_CONTRACTID);
nsCOMPtr<nsIURI> uri;
nsCOMPtr<nsIChannel> chan;
nsCOMPtr<nsIStreamListener> listener = new MyListener();
nsCOMPtr<nsIInterfaceRequestor> callbacks = new MyNotifications();
printf("Getting: %s", uriBuf);
//If relative link
if(strncmp(uriBuf, "http:", 5)) {
//Relative link
rv = NS_NewURI(getter_AddRefs(uri), uriBuf, baseURI);
if (NS_FAILED(rv)) return(rv);
} else {
//Absolute link, no base needed
rv = NS_NewURI(getter_AddRefs(uri), uriBuf);
if (NS_FAILED(rv)) return(rv);
}
//Compare to see if exists
bool equal;
for(int32_t i = 0; i < uriList.Count(); i++) {
uri->Equals(uriList[i], &equal);
if(equal) {
printf("(duplicate, canceling) %s\n",uriBuf);
return NS_OK;
}
}
printf("\n");
uriList.AppendObject(uri);
nsCOMPtr<nsIScriptSecurityManager> secman =
do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
RETURN_IF_FAILED(rv, rv, "Couldn't get script security manager!");
nsCOMPtr<nsIPrincipal> systemPrincipal;
rv = secman->GetSystemPrincipal(getter_AddRefs(systemPrincipal));
RETURN_IF_FAILED(rv, rv, "Couldn't get system principal!");
rv = NS_NewChannel(getter_AddRefs(chan),
uri,
systemPrincipal,
nsILoadInfo::SEC_NORMAL,
nsIContentPolicy::TYPE_OTHER,
nullptr, // loadGroup
callbacks);
RETURN_IF_FAILED(rv, rv, "NS_NewChannel");
gKeepRunning++;
rv = chan->AsyncOpen(listener, myBool);
RETURN_IF_FAILED(rv, rv, "AsyncOpen");
return NS_OK;
}
//---------Buffer writer fun---------
} // namespace
using namespace TestPageLoad;
//---------MAIN-----------
int main(int argc, char **argv)
{
if (test_common_init(&argc, &argv) != 0)
return -1;
nsresult rv;
if (argc == 1) {
printf("usage: TestPageLoad <url>\n");
return -1;
}
{
nsCOMPtr<nsIServiceManager> servMan;
NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
PRTime start, finish;
printf("Loading necko ... \n");
nsCOMPtr<nsIChannel> chan;
nsCOMPtr<nsIStreamListener> listener = new MyListener();
nsCOMPtr<nsIInterfaceRequestor> callbacks = new MyNotifications();
rv = NS_NewURI(getter_AddRefs(baseURI), argv[1]);
RETURN_IF_FAILED(rv, -1, "NS_NewURI");
nsCOMPtr<nsIScriptSecurityManager> secman =
do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
RETURN_IF_FAILED(rv, -1, "Couldn't get script security manager!");
nsCOMPtr<nsIPrincipal> systemPrincipal;
rv = secman->GetSystemPrincipal(getter_AddRefs(systemPrincipal));
RETURN_IF_FAILED(rv, -1, "Couldn't get system principal!");
rv = NS_NewChannel(getter_AddRefs(chan),
baseURI,
systemPrincipal,
nsILoadInfo::SEC_NORMAL,
nsIContentPolicy::TYPE_OTHER,
nullptr, // loadGroup
callbacks);
RETURN_IF_FAILED(rv, -1, "NS_OpenURI");
gKeepRunning++;
//TIMER STARTED-----------------------
printf("Starting clock ... \n");
start = PR_Now();
rv = chan->AsyncOpen(listener, nullptr);
RETURN_IF_FAILED(rv, -1, "AsyncOpen");
PumpEvents();
finish = PR_Now();
uint32_t totalTime32 = uint32_t(finish - start);
printf("\n\n--------------------\nAll done:\nnum found:%d\nnum start:%d\n", numFound, numStart);
printf("\n\n>>PageLoadTime>>%u>>\n\n", totalTime32);
} // 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,273 +0,0 @@
#include "TestCommon.h"
#include <stdio.h>
#include "nsCRT.h" /* should be "plstr.h"? */
#include "nsNetUtil.h"
#include "nsIServiceManager.h"
#include "nsIComponentRegistrar.h"
#include "nsISupportsArray.h"
#include "nsContentUtils.h"
#include <algorithm>
namespace TestPerf {
static nsIIOService *gIOService = nullptr;
//-----------------------------------------------------------------------------
static bool
load_sync_1(nsISupports *element, void *data)
{
nsCOMPtr<nsIInputStream> stream;
nsCOMPtr<nsIURI> uri( do_QueryInterface(element) );
nsAutoCString spec;
nsresult rv;
rv = NS_OpenURI(getter_AddRefs(stream),
uri,
nsContentUtils::GetSystemPrincipal(),
nsILoadInfo::SEC_NORMAL,
nsIContentPolicy::TYPE_OTHER,
nullptr, // aLoadGroup
nullptr, // aCallbacks
LOAD_NORMAL,
gIOService);
if (NS_FAILED(rv)) {
uri->GetAsciiSpec(spec);
fprintf(stderr, "*** failed opening %s [rv=%x]\n", spec.get(), rv);
return true;
}
char buf[4096];
uint32_t bytesRead;
while (1) {
rv = stream->Read(buf, sizeof(buf), &bytesRead);
if (NS_FAILED(rv) || bytesRead == 0) {
if (NS_FAILED(rv)) {
uri->GetAsciiSpec(spec);
fprintf(stderr, "*** failed reading %s [rv=%x]\n", spec.get(), rv);
}
break;
}
}
return true;
}
static nsresult
load_sync(nsISupportsArray *urls)
{
urls->EnumerateForwards(load_sync_1, nullptr);
return NS_OK;
}
//-----------------------------------------------------------------------------
static int gRequestCount = 0;
class MyListener : public nsIStreamListener
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIREQUESTOBSERVER
NS_DECL_NSISTREAMLISTENER
MyListener() { }
virtual ~MyListener() {}
};
NS_IMPL_ISUPPORTS(MyListener, nsIStreamListener, nsIRequestObserver)
NS_IMETHODIMP
MyListener::OnStartRequest(nsIRequest *req, nsISupports *ctx)
{
return NS_OK;
}
NS_IMETHODIMP
MyListener::OnDataAvailable(nsIRequest *req, nsISupports *ctx,
nsIInputStream *stream,
uint64_t offset, uint32_t count)
{
nsresult rv;
char buf[4096];
uint32_t n, bytesRead;
while (count) {
n = std::min<uint32_t>(count, sizeof(buf));
rv = stream->Read(buf, n, &bytesRead);
if (NS_FAILED(rv))
break;
count -= bytesRead;
if (bytesRead == 0)
break;
}
return NS_OK;
}
NS_IMETHODIMP
MyListener::OnStopRequest(nsIRequest *req, nsISupports *ctx, nsresult status)
{
if (NS_FAILED(status)) {
nsAutoCString spec;
req->GetName(spec);
fprintf(stderr, "*** failed loading %s [reason=%x]\n", spec.get(), status);
}
if (--gRequestCount == 0) {
// post shutdown event
QuitPumpingEvents();
}
return NS_OK;
}
static bool
load_async_1(nsISupports *element, void *data)
{
nsCOMPtr<nsIURI> uri( do_QueryInterface(element) );
if (!uri)
return true;
MyListener *listener = new MyListener();
if (!listener)
return true;
NS_ADDREF(listener);
nsresult rv = NS_OpenURI(listener,
nullptr, // aContext
uri,
nsContentUtils::GetSystemPrincipal(),
nsILoadInfo::SEC_NORMAL,
nsIContentPolicy::TYPE_OTHER,
nullptr, // aLoadGroup
nullptr, // aCallbacks
gIOService);
NS_RELEASE(listener);
if (NS_SUCCEEDED(rv))
gRequestCount++;
else
printf(">> NS_OpenURI failed [rv=%x]\n", rv);
return true;
}
static nsresult
load_async(nsISupportsArray *urls)
{
urls->EnumerateForwards(load_async_1, nullptr);
PumpEvents();
return NS_OK;
}
//-----------------------------------------------------------------------------
static nsresult
read_file(const char *fname, nsISupportsArray *urls)
{
FILE *fp = fopen(fname, "r");
if (!fp) {
printf("failed opening file: %s\n", fname);
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsIURI> uri;
nsresult rv;
char buf[512];
while (fgets(buf, sizeof(buf), fp)) {
// remove trailing newline
buf[strlen(buf) - 1] = 0;
rv = NS_NewURI(getter_AddRefs(uri), buf, nullptr, gIOService);
if (NS_FAILED(rv))
printf("*** ignoring malformed uri: %s\n", buf);
else {
//nsXPIDLCString spec;
//uri->GetSpec(getter_Copies(spec));
//printf("read url: %s\n", spec.get());
urls->AppendElement(uri);
}
}
fclose(fp);
return NS_OK;
}
//-----------------------------------------------------------------------------
static void
print_usage()
{
printf("usage: TestPerf [-sync|-async] <file-of-urls>\n");
}
} // namespace
using namespace TestPerf;
int
main(int argc, char **argv)
{
if (test_common_init(&argc, &argv) != 0)
return -1;
nsresult rv;
bool sync;
if (argc < 3) {
print_usage();
return -1;
}
if (PL_strcasecmp(argv[1], "-sync") == 0)
sync = true;
else if (PL_strcasecmp(argv[1], "-async") == 0)
sync = false;
else {
print_usage();
return -1;
}
nsCOMPtr<nsIServiceManager> servMan;
NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
registrar->AutoRegister(nullptr);
// cache the io service
{
nsCOMPtr<nsIIOService> ioserv( do_GetIOService() );
NS_ADDREF(gIOService = ioserv);
}
nsCOMPtr<nsISupportsArray> urls;
rv = NS_NewISupportsArray(getter_AddRefs(urls));
if (NS_FAILED(rv)) return -1;
rv = read_file(argv[2], urls);
if (NS_FAILED(rv)) {
printf("failed reading file-of-urls\n");
return -1;
}
uint32_t urlCount;
urls->Count(&urlCount);
PRIntervalTime start = PR_IntervalNow();
if (sync)
rv = load_sync(urls);
else
rv = load_async(urls);
if (NS_FAILED(rv)) {
printf("load failed\n");
return -1;
}
PRIntervalTime end = PR_IntervalNow();
fprintf(stderr, "read: %u urls; total time: %u milliseconds\n",
urlCount,
PR_IntervalToMilliseconds(end - start));
NS_RELEASE(gIOService);
return 0;
}

View File

@ -649,7 +649,7 @@ nsresult StartLoadingURL(const char* aUrlString)
NS_RELEASE(callbacks);
if (NS_FAILED(rv)) {
LOG(("ERROR: NS_OpenURI failed for %s [rv=%x]\n", aUrlString, rv));
LOG(("ERROR: NS_NewChannel failed for %s [rv=%x]\n", aUrlString, rv));
return rv;
}

View File

@ -1,111 +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 <nsCOMPtr.h>
#include <nsString.h>
#include <nsIURI.h>
#include <nsIChannel.h>
#include <nsIHTTPChannel.h>
#include <nsIInputStream.h>
#include "nsContentUtils.h"
#include <nsNetUtil.h>
/*
* Test synchronous HTTP.
*/
#define RETURN_IF_FAILED(rv, what) \
PR_BEGIN_MACRO \
if (NS_FAILED(rv)) { \
printf(what ": failed - %08x\n", rv); \
return -1; \
} \
PR_END_MACRO
struct TestContext {
nsCOMPtr<nsIURI> uri;
nsCOMPtr<nsIChannel> channel;
nsCOMPtr<nsIInputStream> inputStream;
PRTime t1, t2;
uint32_t bytesRead, totalRead;
TestContext()
: t1(0), t2(0), bytesRead(0), totalRead(0)
{ printf("TestContext [this=%p]\n", (void*)this); }
~TestContext()
{ printf("~TestContext [this=%p]\n", (void*)this); }
};
int
main(int argc, char **argv)
{
nsresult rv;
TestContext *c;
int i, nc=0, npending=0;
char buf[256];
if (argc < 2) {
printf("Usage: TestSyncHTTP <url-list>\n");
return -1;
}
c = new TestContext[argc-1];
for (i=0; i<(argc-1); ++i, ++nc) {
rv = NS_NewURI(getter_AddRefs(c[i].uri), argv[i+1]);
RETURN_IF_FAILED(rv, "NS_NewURI");
rv = NS_OpenURI(getter_AddRefs(c[i].channel,
c[i].uri,
nsContentUtils::GetSystemPrincipal(),
nsILoadInfo::SEC_NORMAL,
nsIContentPolicy::TYPE_OTHER);
RETURN_IF_FAILED(rv, "NS_OpenURI");
nsCOMPtr<nsIHTTPChannel> httpChannel = do_QueryInterface(c[i].channel);
if (httpChannel)
httpChannel->SetOpenHasEventQueue(false);
// initialize these fields for reading
c[i].bytesRead = 1;
c[i].totalRead = 0;
}
for (i=0; i<nc; ++i) {
c[i].t1 = PR_Now();
rv = c[i].channel->Open(getter_AddRefs(c[i].inputStream));
RETURN_IF_FAILED(rv, "nsIChannel::OpenInputStream");
}
npending = nc;
while (npending) {
for (i=0; i<nc; ++i) {
//
// read the response content...
//
if (c[i].bytesRead > 0) {
rv = c[i].inputStream->Read(buf, sizeof buf, &c[i].bytesRead);
RETURN_IF_FAILED(rv, "nsIInputStream::Read");
c[i].totalRead += c[i].bytesRead;
if (c[i].bytesRead == 0) {
c[i].t2 = PR_Now();
printf("finished GET of: %s\n", argv[i+1]);
printf("total read: %u bytes\n", c[i].totalRead);
printf("total read time: %0.3f\n",
((double) (c[i].t2 - c[i].t1))/1000000.0);
npending--;
}
}
}
}
delete[] c;
NS_ShutdownXPCOM(nullptr);
return 0;
}

View File

@ -1,290 +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 <stdio.h>
#include "nsCOMPtr.h"
#include "nsIEventQueueService.h"
#include "nsIServiceManager.h"
#include "nsIStreamListener.h"
#include "nsIURI.h"
#include "nsNetUtil.h"
#include "nsContentUtils.h"
#include <algorithm>
//#include "prthread.h"
// This test attempts to load a URL on a separate thread. It is currently
// designed simply to expose the problems inherent in such an ambitous task
// (i.e., it don't work).
// Utility functions...
// Create event queue for current thread.
static nsCOMPtr<nsIEventQueue>
createEventQueue() {
nsCOMPtr<nsIEventQueue> result;
// Get event queue service.
nsresult rv = NS_OK;
nsCOMPtr<nsIEventQueueService> eqs =
do_GetService(NS_EVENTQUEUESERVICE_CONTRACTID, &rv);
if ( NS_SUCCEEDED( rv ) ) {
eqs->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(result));
} else {
printf( "%s %d: NS_WITH_SERVICE(nsIEventQueueService) failed, rv=0x%08X\n",
(char*)__FILE__, (int)__LINE__, (int)rv );
}
return result;
}
// Create channel for requested URL.
static nsCOMPtr<nsIChannel>
createChannel( const char *url ) {
nsCOMPtr<nsIInputStream> result;
nsCOMPtr<nsIURI> uri;
printf( "Calling NS_NewURI for %s...\n", url );
nsresult rv = NS_NewURI( getter_AddRefs( uri ), url );
if ( NS_SUCCEEDED( rv ) ) {
printf( "...NS_NewURI completed OK\n" );
// Allocate a new input channel on this thread.
printf( "Calling NS_OpenURI...\n" );
nsresult rv = NS_OpenURI(getter_AddRefs(result),
uri,
nsContentUtils::GetSystemPrincipal(),
nsILoadInfo::SEC_NORMAL,
nsIContentPolicy::TYPE_OTHER);
if ( NS_SUCCEEDED( rv ) ) {
printf( "...NS_OpenURI completed OK\n" );
} else {
printf( "%s %d: NS_OpenURI failed, rv=0x%08X\n",
(char*)__FILE__, (int)__LINE__, (int)rv );
}
} else {
printf( "%s %d: NS_NewURI failed, rv=0x%08X\n",
(char*)__FILE__, (int)__LINE__, (int)rv );
}
return result;
}
// Test listener. It basically dumps incoming data to console.
class TestListener : public nsIStreamListener {
public:
NS_DECL_ISUPPORTS
NS_DECL_NSISTREAMLISTENER
NS_DECL_NSISTREAMOBSERVER
TestListener();
~TestListener();
static void IOThread( void *p );
private:
bool mDone;
int mThreadNo;
FILE *mFile;
static int threadCount;
}; // class TestListener
int TestListener::threadCount = 0;
TestListener::TestListener()
: mDone( false ), mThreadNo( ++threadCount ) {
printf( "TestListener ctor called on thread %d\n", mThreadNo );
}
TestListener::~TestListener() {
printf( "TestListener dtor called on thread %d\n", mThreadNo );
}
NS_IMPL_ISUPPORTS( TestListener, nsIStreamListener, nsIRequestObserver )
NS_IMETHODIMP
TestListener::OnStartRequest( nsIChannel *aChannel, nsISupports *aContext ) {
nsresult rv = NS_OK;
printf( "TestListener::OnStartRequest called on thread %d\n", mThreadNo );
// Open output file.
char fileName[32];
sprintf( fileName, "%s%d", "thread", mThreadNo );
mFile = fopen( fileName, "wb" );
setbuf( mFile, 0 );
return rv;
}
NS_IMETHODIMP
TestListener::OnStopRequest( nsIChannel *aChannel,
nsISupports *aContext,
nsresult aStatus,
const char16_t *aMsg ) {
nsresult rv = NS_OK;
printf( "TestListener::OnStopRequest called on thread %d\n", mThreadNo );
fclose( mFile );
mDone = true;
return rv;
}
NS_IMETHODIMP
TestListener::OnDataAvailable( nsIChannel *aChannel,
nsISupports *aContext,
nsIInputStream *aStream,
uint64_t offset,
uint32_t aLength ) {
nsresult rv = NS_OK;
printf( "TestListener::OnDataAvailable called on thread %d\n", mThreadNo );
// Write the data to the console.
// Read a buffer full till aLength bytes have been processed.
char buffer[ 8192 ];
unsigned long bytesRemaining = aLength;
while ( bytesRemaining ) {
unsigned int bytesRead;
// Read a buffer full or the number remaining (whichever is smaller).
rv = aStream->Read( buffer,
std::min( sizeof( buffer ), bytesRemaining ),
&bytesRead );
if ( NS_SUCCEEDED( rv ) ) {
// Write the bytes just read to the output file.
fwrite( buffer, 1, bytesRead, mFile );
bytesRemaining -= bytesRead;
} else {
printf( "%s %d: Read error, rv=0x%08X\n",
(char*)__FILE__, (int)__LINE__, (int)rv );
break;
}
}
printf( "\n" );
return rv;
}
// IOThread: this function creates a new TestListener object (on the new
// thread), opens a channel, and does AsyncRead to it.
void
TestListener::IOThread( void *p ) {
printf( "I/O thread (0x%08X) started...\n", (int)(void*)PR_GetCurrentThread() );
// Argument is pointer to the nsIEventQueue for the main thread.
nsIEventQueue *mainThreadQ = static_cast<nsIEventQueue*>(p);
// Create channel for random web page.
nsCOMPtr<nsIChannel> channel = createChannel( (const char*)p );
if ( channel ) {
// Create event queue.
nsCOMPtr<nsIEventQueue> ioEventQ = createEventQueue();
if ( ioEventQ ) {
// Create test listener.
TestListener *testListener = new TestListener();
testListener->AddRef();
// Read the channel.
printf( "Doing AsyncRead...\n" );
nsresult rv = channel->AsyncRead( testListener, 0 );
if ( NS_SUCCEEDED( rv ) ) {
printf( "...AsyncRead completed OK\n" );
// Process events till testListener says stop.
printf( "Start event loop on io thread %d...\n", testListener->mThreadNo );
while ( !testListener->mDone ) {
PLEvent *event;
ioEventQ->GetEvent( &event );
ioEventQ->HandleEvent( event );
}
printf( "...io thread %d event loop exiting\n", testListener->mThreadNo );
} else {
printf( "%s %d: AsyncRead failed on thread %d, rv=0x%08X\n",
(char*)__FILE__, (int)__LINE__, testListener->mThreadNo, (int)rv );
}
// Release the test listener.
testListener->Release();
}
}
printf( "...I/O thread terminating\n" );
}
static const int maxThreads = 5;
int
main( int argc, char* argv[] ) {
setbuf( stdout, 0 );
if ( argc < 2 || argc > maxThreads + 1 ) {
printf( "usage: testThreadedIO url1 <url2>...\n"
"where <url#> is a location to be loaded on a separate thread\n"
"limit is %d urls/threads", maxThreads );
return -1;
}
nsresult rv= (nsresult)-1;
printf( "Test starting...\n" );
// Initialize XPCOM.
printf( "Initializing XPCOM...\n" );
rv = NS_InitXPCOM2(nullptr, nullptr, nullptr);
if ( NS_FAILED( rv ) ) {
printf( "%s %d: NS_InitXPCOM failed, rv=0x%08X\n",
(char*)__FILE__, (int)__LINE__, (int)rv );
return rv;
}
printf( "...XPCOM initialized OK\n" );
// Create the Event Queue for this thread...
printf( "Creating event queue for main thread (0x%08X)...\n",
(int)(void*)PR_GetCurrentThread() );
{
nsCOMPtr<nsIEventQueue> mainThreadQ = createEventQueue();
if ( mainThreadQ ) {
printf( "...main thread's event queue created OK\n" );
// Spawn threads to do I/O.
int goodThreads = 0;
PRThread *thread[ maxThreads ];
for ( int threadNo = 1; threadNo < argc; threadNo++ ) {
printf( "Creating I/O thread %d to load %s...\n", threadNo, argv[threadNo] );
PRThread *ioThread = PR_CreateThread( PR_USER_THREAD,
TestListener::IOThread,
argv[threadNo],
PR_PRIORITY_NORMAL,
PR_GLOBAL_THREAD,
PR_JOINABLE_THREAD,
0 );
if ( ioThread ) {
thread[ goodThreads++ ] = ioThread;
printf( "...I/O thread %d (0x%08X) created OK\n",
threadNo, (int)(void*)ioThread );
} else {
printf( "%s %d: PR_CreateThread for thread %d failed\n",
(char*)__FILE__, (int)__LINE__, threadNo );
}
}
// Wait for all the threads to terminate.
for ( int joinThread = 0; joinThread < goodThreads; joinThread++ ) {
printf( "Waiting for thread %d to terminate...\n", joinThread+1 );
PR_JoinThread( thread[ joinThread ] );
}
}
} // this scopes the nsCOMPtrs
// no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
// Shut down XPCOM.
printf( "Shutting down XPCOM...\n" );
NS_ShutdownXPCOM( 0 );
printf( "...XPCOM shutdown complete\n" );
// Exit.
printf( "...test complete, rv=0x%08X\n", (int)rv );
return rv;
}

View File

@ -24,7 +24,6 @@ GeckoSimplePrograms([
'TestDNS',
'TestIncrementalDownload',
'TestOpen',
'TestPageLoad',
'TestProtocols',
'TestServ',
'TestStandardURL',
@ -38,7 +37,6 @@ GeckoSimplePrograms([
#SIMPLE_PROGRAMS += [
# TestIDN',
# TestIOThreads',
# TestPerf',
# TestSocketTransport',
# TestStreamChannel',
# TestStreamPump',

View File

@ -1,95 +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 <stdio.h>
#include "nscore.h"
#include "nsIConverterInputStream.h"
#include "nsIURL.h"
#include "nsNetUtil.h"
#include "nsCRT.h"
#include "nsString.h"
#include "prprf.h"
#include "prtime.h"
static nsString* ConvertCharacterSetName(const char* aName)
{
return new nsString(NS_ConvertASCIItoUTF16(aName));
}
int main(int argc, char** argv)
{
if (3 != argc) {
printf("usage: CvtURL url utf8\n");
return -1;
}
char* characterSetName = argv[2];
nsString* cset = ConvertCharacterSetName(characterSetName);
if (NS_PTR_TO_INT32(cset) < 0) {
printf("illegal character set name: '%s'\n", characterSetName);
return -1;
}
// Create url object
char* urlName = argv[1];
nsIURI* url;
nsresult rv;
rv = NS_NewURI(&url, urlName);
if (NS_OK != rv) {
printf("invalid URL: '%s'\n", urlName);
return -1;
}
// Get an input stream from the url
nsIInputStream* in;
nsresult ec = NS_OpenURI(&in,
url,
nsContentUtils::GetSystemPrincipal(),
nsILoadInfo::SEC_NORMAL,
nsIContentPolicy::TYPE_OTHER);
if (nullptr == in) {
printf("open of url('%s') failed: error=%x\n", urlName, ec);
return -1;
}
// Translate the input using the argument character set id into
// unicode
nsCOMPtr<nsIConverterInputStream> uin =
do_CreateInstance("@mozilla.org/intl/converter-input-stream;1", &rv);
if (NS_SUCCEEDED(rv))
rv = uin->Init(in, cset->get(), 4096);
if (NS_FAILED(rv)) {
printf("can't create converter input stream: %d\n", rv);
return -1;
}
// Read the input and write some output
PRTime start = PR_Now();
int32_t count = 0;
for (;;) {
char16_t buf[1000];
uint32_t nb;
ec = uin->Read(buf, 0, 1000, &nb);
if (NS_FAILED(ec)) {
printf("i/o error: %d\n", ec);
break;
}
if (nb == 0) break; // EOF
count += nb;
}
PRTime end = PR_Now();
PRTime conversion = (end - start) / 1000;
char buf[500];
PR_snprintf(buf, sizeof(buf),
"converting and discarding %d bytes took %lldms",
count, conversion);
puts(buf);
// Release the objects
in->Release();
url->Release();
return 0;
}