2001-09-28 20:14:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* 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/. */
|
2001-06-27 02:33:44 +00:00
|
|
|
|
2004-10-21 20:57:17 +00:00
|
|
|
#include "TestCommon.h"
|
2001-06-27 02:33:44 +00:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
2001-09-05 21:28:38 +00:00
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
2001-06-27 02:33:44 +00:00
|
|
|
#include "nsIProgressEventSink.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "prprf.h"
|
2002-09-03 23:36:13 +00:00
|
|
|
#include "nsXPCOM.h"
|
2001-06-27 02:33:44 +00:00
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "prlong.h"
|
|
|
|
#include "plstr.h"
|
2009-04-14 08:02:58 +00:00
|
|
|
#include "nsCOMArray.h"
|
2002-01-29 21:22:13 +00:00
|
|
|
#include "nsIComponentRegistrar.h"
|
2005-11-08 19:23:00 +00:00
|
|
|
|
2008-07-28 01:09:10 +00:00
|
|
|
namespace TestPageLoad {
|
|
|
|
|
2001-06-27 02:33:44 +00:00
|
|
|
int getStrLine(const char *src, char *str, int ind, int max);
|
|
|
|
nsresult auxLoad(char *uriBuf);
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2012-08-07 17:17:27 +00:00
|
|
|
#define RETURN_IF_FAILED(rv, ret, step) \
|
2001-06-27 02:33:44 +00:00
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
if (NS_FAILED(rv)) { \
|
|
|
|
printf(">>> %s failed: rv=%x\n", step, rv); \
|
2012-08-07 17:17:27 +00:00
|
|
|
return ret;\
|
2001-06-27 02:33:44 +00:00
|
|
|
} \
|
|
|
|
PR_END_MACRO
|
|
|
|
|
2004-06-27 13:50:47 +00:00
|
|
|
static nsCString globalStream;
|
2001-06-27 02:33:44 +00:00
|
|
|
//static char urlBuf[256];
|
|
|
|
static nsCOMPtr<nsIURI> baseURI;
|
2009-04-14 08:02:58 +00:00
|
|
|
static nsCOMArray<nsIURI> uriList;
|
2001-06-27 02:33:44 +00:00
|
|
|
|
|
|
|
//Temp, should remove:
|
|
|
|
static int numStart=0;
|
|
|
|
static int numFound=0;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
static int32_t gKeepRunning = 0;
|
2006-05-10 17:30:15 +00:00
|
|
|
|
2001-06-27 02:33:44 +00:00
|
|
|
|
|
|
|
//--------writer fun----------------------
|
|
|
|
|
|
|
|
static NS_METHOD streamParse (nsIInputStream* in,
|
|
|
|
void* closure,
|
|
|
|
const char* fromRawSegment,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t toOffset,
|
|
|
|
uint32_t count,
|
|
|
|
uint32_t *writeCount) {
|
2001-06-27 02:33:44 +00:00
|
|
|
|
|
|
|
char parseBuf[2048], loc[2048], lineBuf[2048];
|
|
|
|
char *loc_t, *loc_t2;
|
|
|
|
int i = 0;
|
2010-06-30 23:39:35 +00:00
|
|
|
const char *tmp;
|
2001-06-27 02:33:44 +00:00
|
|
|
|
2003-05-23 21:34:47 +00:00
|
|
|
if(!globalStream.IsEmpty()) {
|
2004-06-27 13:50:47 +00:00
|
|
|
globalStream.Append(fromRawSegment);
|
2010-06-30 23:39:35 +00:00
|
|
|
tmp = globalStream.get();
|
2001-06-27 02:33:44 +00:00
|
|
|
//printf("\n>>NOW:\n^^^^^\n%s\n^^^^^^^^^^^^^^", tmp);
|
|
|
|
} else {
|
2010-06-30 23:39:35 +00:00
|
|
|
tmp = fromRawSegment;
|
2001-06-27 02:33:44 +00:00
|
|
|
}
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2001-06-27 02:33:44 +00:00
|
|
|
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"))!= NULL
|
|
|
|
|| (loc_t=PL_strcasestr(lineBuf, "script"))!=NULL) {
|
|
|
|
loc_t2=PL_strcasestr(loc_t, "src");
|
|
|
|
if(loc_t2!=NULL) {
|
|
|
|
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"))!= NULL) {
|
|
|
|
loc_t2=PL_strcasestr(loc_t, "href");
|
|
|
|
if(loc_t2!=NULL) {
|
|
|
|
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"))!=NULL) {
|
|
|
|
loc_t+=10;
|
|
|
|
strcpy(loc, loc_t);
|
|
|
|
sscanf(loc, "=\"%[^\"]", parseBuf);
|
|
|
|
if(parseBuf[0]!='\0') {
|
|
|
|
numFound++;
|
|
|
|
auxLoad(parseBuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i++;
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2001-06-27 02:33:44 +00:00
|
|
|
}
|
|
|
|
*writeCount = count;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsIStreamListener implementation
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class MyListener : public nsIStreamListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
|
|
|
|
2003-01-08 22:35:09 +00:00
|
|
|
MyListener() { }
|
2001-06-27 02:33:44 +00:00
|
|
|
virtual ~MyListener() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS2(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);
|
2006-05-10 17:30:15 +00:00
|
|
|
if (--gKeepRunning == 0)
|
|
|
|
QuitPumpingEvents();
|
2001-06-27 02:33:44 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MyListener::OnDataAvailable(nsIRequest *req, nsISupports *ctxt,
|
|
|
|
nsIInputStream *stream,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t offset, uint32_t count)
|
2001-06-27 02:33:44 +00:00
|
|
|
{
|
|
|
|
//printf(">>> OnDataAvailable [count=%u]\n", count);
|
2002-02-18 15:22:56 +00:00
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t bytesRead=0;
|
2001-06-27 02:33:44 +00:00
|
|
|
char buf[1024];
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
if(ctxt == nullptr) {
|
2001-06-27 02:33:44 +00:00
|
|
|
bytesRead=0;
|
|
|
|
rv = stream->ReadSegments(streamParse, &offset, count, &bytesRead);
|
|
|
|
} else {
|
|
|
|
while (count) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t amount = NS_MIN<uint32_t>(count, sizeof(buf));
|
2001-06-27 02:33:44 +00:00
|
|
|
rv = stream->Read(buf, amount, &bytesRead);
|
|
|
|
count -= bytesRead;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
printf(">>> stream->Read failed with rv=%x\n", rv);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// NotificationCallbacks implementation
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class MyNotifications : public nsIInterfaceRequestor
|
|
|
|
, public nsIProgressEventSink
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
|
|
|
NS_DECL_NSIPROGRESSEVENTSINK
|
|
|
|
|
2003-01-08 22:35:09 +00:00
|
|
|
MyNotifications() { }
|
2001-06-27 02:33:44 +00:00
|
|
|
virtual ~MyNotifications() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS2(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 PRUnichar *statusText)
|
|
|
|
{
|
|
|
|
//printf("status: %x\n", status);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MyNotifications::OnProgress(nsIRequest *req, nsISupports *ctx,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint64_t progress, uint64_t progressMax)
|
2001-06-27 02:33:44 +00:00
|
|
|
{
|
2005-01-09 20:43:35 +00:00
|
|
|
// char buf[100];
|
|
|
|
// PR_snprintf(buf, sizeof(buf), "%llu/%llu\n", progress, progressMax);
|
|
|
|
// printf("%s", buf);
|
2001-06-27 02:33:44 +00:00
|
|
|
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;
|
2005-11-08 19:23:00 +00:00
|
|
|
globalStream.Assign('\0');
|
2001-06-27 02:33:44 +00:00
|
|
|
while(c!='\n' && c!='\0' && i<max) {
|
|
|
|
str[i] = src[ind];
|
|
|
|
i++; ind++;
|
|
|
|
c = src[ind];
|
|
|
|
}
|
|
|
|
str[i]='\0';
|
|
|
|
if(i==max || c=='\0') {
|
2004-06-27 13:50:47 +00:00
|
|
|
globalStream.Assign(str);
|
2001-06-27 02:33:44 +00:00
|
|
|
//printf("\nCarryover (%d|%d):\n------------\n%s\n-------\n",i,max,str);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return ind;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------AUX LOAD-----------
|
|
|
|
nsresult auxLoad(char *uriBuf)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2001-06-27 02:33:44 +00:00
|
|
|
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);
|
|
|
|
}
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2001-06-27 02:33:44 +00:00
|
|
|
//Compare to see if exists
|
2011-09-29 06:19:26 +00:00
|
|
|
bool equal;
|
2012-08-22 15:56:38 +00:00
|
|
|
for(int32_t i = 0; i < uriList.Count(); i++) {
|
2009-04-14 08:02:58 +00:00
|
|
|
uri->Equals(uriList[i], &equal);
|
2001-06-27 02:33:44 +00:00
|
|
|
if(equal) {
|
2002-08-22 22:01:50 +00:00
|
|
|
printf("(duplicate, canceling) %s\n",uriBuf);
|
2001-06-27 02:33:44 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
2009-04-14 08:02:58 +00:00
|
|
|
uriList.AppendObject(uri);
|
2012-07-30 14:20:58 +00:00
|
|
|
rv = NS_NewChannel(getter_AddRefs(chan), uri, nullptr, nullptr, callbacks);
|
2012-08-07 17:17:27 +00:00
|
|
|
RETURN_IF_FAILED(rv, rv, "NS_NewChannel");
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2001-06-27 02:33:44 +00:00
|
|
|
gKeepRunning++;
|
|
|
|
rv = chan->AsyncOpen(listener, myBool);
|
2012-08-07 17:17:27 +00:00
|
|
|
RETURN_IF_FAILED(rv, rv, "AsyncOpen");
|
2001-06-27 02:33:44 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2001-06-27 02:33:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------Buffer writer fun---------
|
|
|
|
|
2008-07-28 01:09:10 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
using namespace TestPageLoad;
|
2001-06-27 02:33:44 +00:00
|
|
|
|
|
|
|
//---------MAIN-----------
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
2004-10-21 20:57:17 +00:00
|
|
|
{
|
|
|
|
if (test_common_init(&argc, &argv) != 0)
|
|
|
|
return -1;
|
|
|
|
|
2001-06-27 02:33:44 +00:00
|
|
|
nsresult rv;
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2001-06-27 02:33:44 +00:00
|
|
|
if (argc == 1) {
|
|
|
|
printf("usage: TestPageLoad <url>\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2002-07-04 14:29:25 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIServiceManager> servMan;
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
|
2002-07-04 14:29:25 +00:00
|
|
|
|
|
|
|
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]);
|
2012-08-07 17:17:27 +00:00
|
|
|
RETURN_IF_FAILED(rv, -1, "NS_NewURI");
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
rv = NS_NewChannel(getter_AddRefs(chan), baseURI, nullptr, nullptr, callbacks);
|
2012-08-07 17:17:27 +00:00
|
|
|
RETURN_IF_FAILED(rv, -1, "NS_OpenURI");
|
2002-07-04 14:29:25 +00:00
|
|
|
gKeepRunning++;
|
|
|
|
|
|
|
|
//TIMER STARTED-----------------------
|
|
|
|
printf("Starting clock ... \n");
|
|
|
|
start = PR_Now();
|
2012-07-30 14:20:58 +00:00
|
|
|
rv = chan->AsyncOpen(listener, nullptr);
|
2012-08-07 17:17:27 +00:00
|
|
|
RETURN_IF_FAILED(rv, -1, "AsyncOpen");
|
2002-07-04 14:29:25 +00:00
|
|
|
|
2006-05-10 17:30:15 +00:00
|
|
|
PumpEvents();
|
2002-07-04 14:29:25 +00:00
|
|
|
|
|
|
|
finish = PR_Now();
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t totalTime32;
|
|
|
|
uint64_t totalTime64;
|
2002-07-04 14:29:25 +00:00
|
|
|
LL_SUB(totalTime64, finish, start);
|
|
|
|
LL_L2UI(totalTime32, totalTime64);
|
|
|
|
|
|
|
|
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
|
2012-07-30 14:20:58 +00:00
|
|
|
rv = NS_ShutdownXPCOM(nullptr);
|
2002-07-04 14:29:25 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
|
2001-06-27 02:33:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|