1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is Mozilla Communicator client code.
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1998
20
* the Initial Developer. All Rights Reserved.
24
* Alternatively, the contents of this file may be used under the terms of
25
* either the GNU General Public License Version 2 or later (the "GPL"), or
26
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27
* in which case the provisions of the GPL or the LGPL are applicable instead
28
* of those above. If you wish to allow use of your version of this file only
29
* under the terms of either the GPL or the LGPL, and not to allow others to
30
* use your version of this file under the terms of the NPL, indicate your
31
* decision by deleting the provisions above and replace them with the notice
32
* and other provisions required by the GPL or the LGPL. If you do not delete
33
* the provisions above, a recipient may use your version of this file under
34
* the terms of any one of the NPL, the GPL or the LGPL.
36
* ***** END LICENSE BLOCK ***** */
38
#include "nsNetUtil.h"
39
#include "nsIEventQueueService.h"
40
#include "nsIServiceManager.h"
41
#include "nsIInterfaceRequestor.h"
42
#include "nsIInterfaceRequestorUtils.h"
43
#include "nsIProgressEventSink.h"
44
#include "nsIComponentManager.h"
47
#include "nsISupportsPrimitives.h"
51
#include "nsSupportsArray.h"
52
#include "nsReadableUtils.h"
53
#include "nsIComponentRegistrar.h"
54
int getStrLine(const char *src, char *str, int ind, int max);
55
nsresult auxLoad(char *uriBuf);
56
//----------------------------------------------------------------------
59
#define RETURN_IF_FAILED(rv, step) \
61
if (NS_FAILED(rv)) { \
62
printf(">>> %s failed: rv=%x\n", step, rv); \
67
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
68
static nsIEventQueue* gEventQ = nsnull;
69
static PRBool gKeepRunning = PR_FALSE;
70
static nsString globalStream;
71
//static char urlBuf[256];
72
static nsCOMPtr<nsIURI> baseURI;
73
static nsCOMPtr<nsISupportsArray> uriList;
75
//Temp, should remove:
76
static int numStart=0;
77
static int numFound=0;
80
//--------writer fun----------------------
82
static NS_METHOD streamParse (nsIInputStream* in,
84
const char* fromRawSegment,
87
PRUint32 *writeCount) {
89
char parseBuf[2048], loc[2048], lineBuf[2048];
94
if(!globalStream.IsEmpty()) {
95
globalStream.AppendWithConversion(fromRawSegment);
96
tmp = ToNewCString(globalStream);
97
//printf("\n>>NOW:\n^^^^^\n%s\n^^^^^^^^^^^^^^", tmp);
99
tmp = (char *)fromRawSegment;
102
while(i < (int)count) {
103
i = getStrLine(tmp, lineBuf, i, count);
109
if((loc_t=PL_strcasestr(lineBuf, "img"))!= NULL
110
|| (loc_t=PL_strcasestr(lineBuf, "script"))!=NULL) {
111
loc_t2=PL_strcasestr(loc_t, "src");
115
sscanf(loc, "=\"%[^\"]", parseBuf);
116
if(parseBuf[0]=='\0')
117
sscanf(loc, "=%s", parseBuf);
118
if(parseBuf[0]!='\0'){
125
/***NEED BETTER CHECK FOR STYLESHEETS
126
if((loc_t=PL_strcasestr(lineBuf, "link"))!= NULL) {
127
loc_t2=PL_strcasestr(loc_t, "href");
131
//printf("%s\n", loc);
132
sscanf(loc, "=\"%[^\"]", parseBuf);
133
if(parseBuf[0]!='\0'){
134
//printf("%s\n", parseBuf);
141
if((loc_t=PL_strcasestr(lineBuf, "background"))!=NULL) {
144
sscanf(loc, "=\"%[^\"]", parseBuf);
145
if(parseBuf[0]!='\0') {
157
//-----------------------------------------------------------------------------
158
// nsIStreamListener implementation
159
//-----------------------------------------------------------------------------
161
class MyListener : public nsIStreamListener
165
NS_DECL_NSIREQUESTOBSERVER
166
NS_DECL_NSISTREAMLISTENER
169
virtual ~MyListener() {}
172
NS_IMPL_ISUPPORTS2(MyListener,
177
MyListener::OnStartRequest(nsIRequest *req, nsISupports *ctxt)
179
//printf(">>> OnStartRequest\n");
185
MyListener::OnStopRequest(nsIRequest *req, nsISupports *ctxt, nsresult status)
187
//printf(">>> OnStopRequest status=%x\n", status);
193
MyListener::OnDataAvailable(nsIRequest *req, nsISupports *ctxt,
194
nsIInputStream *stream,
195
PRUint32 offset, PRUint32 count)
197
//printf(">>> OnDataAvailable [count=%u]\n", count);
198
nsresult rv = NS_ERROR_FAILURE;
199
PRUint32 bytesRead=0;
204
rv = stream->ReadSegments(streamParse, &offset, count, &bytesRead);
207
PRUint32 amount = PR_MIN(count, sizeof(buf));
208
rv = stream->Read(buf, amount, &bytesRead);
214
printf(">>> stream->Read failed with rv=%x\n", rv);
221
//-----------------------------------------------------------------------------
222
// NotificationCallbacks implementation
223
//-----------------------------------------------------------------------------
225
class MyNotifications : public nsIInterfaceRequestor
226
, public nsIProgressEventSink
230
NS_DECL_NSIINTERFACEREQUESTOR
231
NS_DECL_NSIPROGRESSEVENTSINK
233
MyNotifications() { }
234
virtual ~MyNotifications() {}
237
NS_IMPL_THREADSAFE_ISUPPORTS2(MyNotifications,
238
nsIInterfaceRequestor,
239
nsIProgressEventSink)
242
MyNotifications::GetInterface(const nsIID &iid, void **result)
244
return QueryInterface(iid, result);
248
MyNotifications::OnStatus(nsIRequest *req, nsISupports *ctx,
249
nsresult status, const PRUnichar *statusText)
251
//printf("status: %x\n", status);
256
MyNotifications::OnProgress(nsIRequest *req, nsISupports *ctx,
257
PRUint32 progress, PRUint32 progressMax)
259
//printf("progress: %u/%u\n", progress, progressMax);
263
//-----------------------------------------------------------------------------
265
//-----------------------------------------------------------------------------
267
//---------getStrLine Helper function---------------
268
//Finds a newline in src starting at ind. Puts the
269
//line in str (must be big enough). Returns the index
270
//of the newline, or -1 if at end of string. If reaches
271
//end of string ('\0'), then will copy contents to
273
int getStrLine(const char *src, char *str, int ind, int max) {
276
globalStream.Assign(NS_LITERAL_STRING("\0"));
277
while(c!='\n' && c!='\0' && i<max) {
283
if(i==max || c=='\0') {
284
globalStream.AssignWithConversion(str);
285
//printf("\nCarryover (%d|%d):\n------------\n%s\n-------\n",i,max,str);
291
//----------AUX LOAD-----------
292
nsresult auxLoad(char *uriBuf)
296
nsCOMPtr<nsISupportsPRBool> myBool = do_CreateInstance(NS_SUPPORTS_PRBOOL_CONTRACTID);
298
nsCOMPtr<nsIURI> uri;
299
nsCOMPtr<nsIChannel> chan;
300
nsCOMPtr<nsIStreamListener> listener = new MyListener();
301
nsCOMPtr<nsIInterfaceRequestor> callbacks = new MyNotifications();
303
printf("Getting: %s", uriBuf);
306
if(strncmp(uriBuf, "http:", 5)) {
308
rv = NS_NewURI(getter_AddRefs(uri), uriBuf, baseURI);
309
if (NS_FAILED(rv)) return(rv);
311
//Absolute link, no base needed
312
rv = NS_NewURI(getter_AddRefs(uri), uriBuf);
313
if (NS_FAILED(rv)) return(rv);
316
//Compare to see if exists
318
uriList->Count(&num);
320
nsCOMPtr<nsIURI> uriTmp;
321
for(PRUint32 i = 0; i < num; i++) {
322
uriList->GetElementAt(i, getter_AddRefs(uriTmp));
323
uri->Equals(uriTmp, &equal);
325
printf("(duplicate, canceling) %s\n",uriBuf);
330
uriList->AppendElement(uri);
331
rv = NS_NewChannel(getter_AddRefs(chan), uri, nsnull, nsnull, callbacks);
332
RETURN_IF_FAILED(rv, "NS_NewChannel");
335
rv = chan->AsyncOpen(listener, myBool);
336
RETURN_IF_FAILED(rv, "AsyncOpen");
342
//---------Buffer writer fun---------
345
//---------MAIN-----------
347
int main(int argc, char **argv)
352
printf("usage: TestPageLoad <url>\n");
356
nsCOMPtr<nsIServiceManager> servMan;
357
NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
358
nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
359
NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
361
registrar->AutoRegister(nsnull);
363
PRTime start, finish;
365
rv = NS_NewISupportsArray(getter_AddRefs(uriList));
366
RETURN_IF_FAILED(rv, "NS_NewISupportsArray");
368
// Create the Event Queue for this thread...
369
nsCOMPtr<nsIEventQueueService> eqs =
370
do_GetService(kEventQueueServiceCID, &rv);
371
RETURN_IF_FAILED(rv, "do_GetService(EventQueueService)");
373
rv = eqs->CreateMonitoredThreadEventQueue();
374
RETURN_IF_FAILED(rv, "CreateMonitoredThreadEventQueue");
376
rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
377
RETURN_IF_FAILED(rv, "GetThreadEventQueue");
379
printf("Loading necko ... \n");
380
nsCOMPtr<nsIChannel> chan;
381
nsCOMPtr<nsIStreamListener> listener = new MyListener();
382
nsCOMPtr<nsIInterfaceRequestor> callbacks = new MyNotifications();
384
rv = NS_NewURI(getter_AddRefs(baseURI), argv[1]);
385
RETURN_IF_FAILED(rv, "NS_NewURI");
387
rv = NS_NewChannel(getter_AddRefs(chan), baseURI, nsnull, nsnull, callbacks);
388
RETURN_IF_FAILED(rv, "NS_OpenURI");
391
//TIMER STARTED-----------------------
392
printf("Starting clock ... \n");
394
rv = chan->AsyncOpen(listener, nsnull);
395
RETURN_IF_FAILED(rv, "AsyncOpen");
397
while (gKeepRunning) {
398
gEventQ->ProcessPendingEvents();
402
PRUint32 totalTime32;
403
PRUint64 totalTime64;
404
LL_SUB(totalTime64, finish, start);
405
LL_L2UI(totalTime32, totalTime64);
407
printf("\n\n--------------------\nAll done:\nnum found:%d\nnum start:%d\n", numFound, numStart);
409
printf("\n\n>>PageLoadTime>>%u>>\n\n", totalTime32);
410
} // this scopes the nsCOMPtrs
411
// no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
412
rv = NS_ShutdownXPCOM(nsnull);
413
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");