1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
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 MPL, 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 MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "TestCommon.h"
39 #include "nsNetUtil.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"
50 #include "nsSupportsArray.h"
51 #include "nsIComponentRegistrar.h"
53 int getStrLine(const char *src
, char *str
, int ind
, int max
);
54 nsresult
auxLoad(char *uriBuf
);
55 //----------------------------------------------------------------------
58 #define RETURN_IF_FAILED(rv, step) \
60 if (NS_FAILED(rv)) { \
61 printf(">>> %s failed: rv=%x\n", step, rv); \
66 static nsCString globalStream
;
67 //static char urlBuf[256];
68 static nsCOMPtr
<nsIURI
> baseURI
;
69 static nsCOMPtr
<nsISupportsArray
> uriList
;
71 //Temp, should remove:
72 static int numStart
=0;
73 static int numFound
=0;
75 static PRInt32 gKeepRunning
= 0;
78 //--------writer fun----------------------
80 static NS_METHOD
streamParse (nsIInputStream
* in
,
82 const char* fromRawSegment
,
85 PRUint32
*writeCount
) {
87 char parseBuf
[2048], loc
[2048], lineBuf
[2048];
92 if(!globalStream
.IsEmpty()) {
93 globalStream
.Append(fromRawSegment
);
94 tmp
= ToNewCString(globalStream
);
95 //printf("\n>>NOW:\n^^^^^\n%s\n^^^^^^^^^^^^^^", tmp);
97 tmp
= (char *)fromRawSegment
;
100 while(i
< (int)count
) {
101 i
= getStrLine(tmp
, lineBuf
, i
, count
);
107 if((loc_t
=PL_strcasestr(lineBuf
, "img"))!= NULL
108 || (loc_t
=PL_strcasestr(lineBuf
, "script"))!=NULL
) {
109 loc_t2
=PL_strcasestr(loc_t
, "src");
113 sscanf(loc
, "=\"%[^\"]", parseBuf
);
114 if(parseBuf
[0]=='\0')
115 sscanf(loc
, "=%s", parseBuf
);
116 if(parseBuf
[0]!='\0'){
123 /***NEED BETTER CHECK FOR STYLESHEETS
124 if((loc_t=PL_strcasestr(lineBuf, "link"))!= NULL) {
125 loc_t2=PL_strcasestr(loc_t, "href");
129 //printf("%s\n", loc);
130 sscanf(loc, "=\"%[^\"]", parseBuf);
131 if(parseBuf[0]!='\0'){
132 //printf("%s\n", parseBuf);
139 if((loc_t
=PL_strcasestr(lineBuf
, "background"))!=NULL
) {
142 sscanf(loc
, "=\"%[^\"]", parseBuf
);
143 if(parseBuf
[0]!='\0') {
155 //-----------------------------------------------------------------------------
156 // nsIStreamListener implementation
157 //-----------------------------------------------------------------------------
159 class MyListener
: public nsIStreamListener
163 NS_DECL_NSIREQUESTOBSERVER
164 NS_DECL_NSISTREAMLISTENER
167 virtual ~MyListener() {}
170 NS_IMPL_ISUPPORTS2(MyListener
,
175 MyListener::OnStartRequest(nsIRequest
*req
, nsISupports
*ctxt
)
177 //printf(">>> OnStartRequest\n");
183 MyListener::OnStopRequest(nsIRequest
*req
, nsISupports
*ctxt
, nsresult status
)
185 //printf(">>> OnStopRequest status=%x\n", status);
186 if (--gKeepRunning
== 0)
192 MyListener::OnDataAvailable(nsIRequest
*req
, nsISupports
*ctxt
,
193 nsIInputStream
*stream
,
194 PRUint32 offset
, PRUint32 count
)
196 //printf(">>> OnDataAvailable [count=%u]\n", count);
197 nsresult rv
= NS_ERROR_FAILURE
;
198 PRUint32 bytesRead
=0;
203 rv
= stream
->ReadSegments(streamParse
, &offset
, count
, &bytesRead
);
206 PRUint32 amount
= PR_MIN(count
, sizeof(buf
));
207 rv
= stream
->Read(buf
, amount
, &bytesRead
);
213 printf(">>> stream->Read failed with rv=%x\n", rv
);
220 //-----------------------------------------------------------------------------
221 // NotificationCallbacks implementation
222 //-----------------------------------------------------------------------------
224 class MyNotifications
: public nsIInterfaceRequestor
225 , public nsIProgressEventSink
229 NS_DECL_NSIINTERFACEREQUESTOR
230 NS_DECL_NSIPROGRESSEVENTSINK
232 MyNotifications() { }
233 virtual ~MyNotifications() {}
236 NS_IMPL_THREADSAFE_ISUPPORTS2(MyNotifications
,
237 nsIInterfaceRequestor
,
238 nsIProgressEventSink
)
241 MyNotifications::GetInterface(const nsIID
&iid
, void **result
)
243 return QueryInterface(iid
, result
);
247 MyNotifications::OnStatus(nsIRequest
*req
, nsISupports
*ctx
,
248 nsresult status
, const PRUnichar
*statusText
)
250 //printf("status: %x\n", status);
255 MyNotifications::OnProgress(nsIRequest
*req
, nsISupports
*ctx
,
256 PRUint64 progress
, PRUint64 progressMax
)
259 // PR_snprintf(buf, sizeof(buf), "%llu/%llu\n", progress, progressMax);
260 // printf("%s", buf);
264 //-----------------------------------------------------------------------------
266 //-----------------------------------------------------------------------------
268 //---------getStrLine Helper function---------------
269 //Finds a newline in src starting at ind. Puts the
270 //line in str (must be big enough). Returns the index
271 //of the newline, or -1 if at end of string. If reaches
272 //end of string ('\0'), then will copy contents to
274 int getStrLine(const char *src
, char *str
, int ind
, int max
) {
277 globalStream
.Assign('\0');
278 while(c
!='\n' && c
!='\0' && i
<max
) {
284 if(i
==max
|| c
=='\0') {
285 globalStream
.Assign(str
);
286 //printf("\nCarryover (%d|%d):\n------------\n%s\n-------\n",i,max,str);
292 //----------AUX LOAD-----------
293 nsresult
auxLoad(char *uriBuf
)
297 nsCOMPtr
<nsISupportsPRBool
> myBool
= do_CreateInstance(NS_SUPPORTS_PRBOOL_CONTRACTID
);
299 nsCOMPtr
<nsIURI
> uri
;
300 nsCOMPtr
<nsIChannel
> chan
;
301 nsCOMPtr
<nsIStreamListener
> listener
= new MyListener();
302 nsCOMPtr
<nsIInterfaceRequestor
> callbacks
= new MyNotifications();
304 printf("Getting: %s", uriBuf
);
307 if(strncmp(uriBuf
, "http:", 5)) {
309 rv
= NS_NewURI(getter_AddRefs(uri
), uriBuf
, baseURI
);
310 if (NS_FAILED(rv
)) return(rv
);
312 //Absolute link, no base needed
313 rv
= NS_NewURI(getter_AddRefs(uri
), uriBuf
);
314 if (NS_FAILED(rv
)) return(rv
);
317 //Compare to see if exists
319 uriList
->Count(&num
);
321 nsCOMPtr
<nsIURI
> uriTmp
;
322 for(PRUint32 i
= 0; i
< num
; i
++) {
323 uriList
->GetElementAt(i
, getter_AddRefs(uriTmp
));
324 uri
->Equals(uriTmp
, &equal
);
326 printf("(duplicate, canceling) %s\n",uriBuf
);
331 uriList
->AppendElement(uri
);
332 rv
= NS_NewChannel(getter_AddRefs(chan
), uri
, nsnull
, nsnull
, callbacks
);
333 RETURN_IF_FAILED(rv
, "NS_NewChannel");
336 rv
= chan
->AsyncOpen(listener
, myBool
);
337 RETURN_IF_FAILED(rv
, "AsyncOpen");
343 //---------Buffer writer fun---------
346 //---------MAIN-----------
348 int main(int argc
, char **argv
)
350 if (test_common_init(&argc
, &argv
) != 0)
356 printf("usage: TestPageLoad <url>\n");
360 nsCOMPtr
<nsIServiceManager
> servMan
;
361 NS_InitXPCOM2(getter_AddRefs(servMan
), nsnull
, nsnull
);
362 nsCOMPtr
<nsIComponentRegistrar
> registrar
= do_QueryInterface(servMan
);
363 NS_ASSERTION(registrar
, "Null nsIComponentRegistrar");
365 registrar
->AutoRegister(nsnull
);
367 PRTime start
, finish
;
369 uriList
= do_CreateInstance(NS_SUPPORTSARRAY_CONTRACTID
, &rv
);
370 RETURN_IF_FAILED(rv
, "do_CreateInstance(NS_SUPPORTSARRAY_CONTRACTID)");
372 printf("Loading necko ... \n");
373 nsCOMPtr
<nsIChannel
> chan
;
374 nsCOMPtr
<nsIStreamListener
> listener
= new MyListener();
375 nsCOMPtr
<nsIInterfaceRequestor
> callbacks
= new MyNotifications();
377 rv
= NS_NewURI(getter_AddRefs(baseURI
), argv
[1]);
378 RETURN_IF_FAILED(rv
, "NS_NewURI");
380 rv
= NS_NewChannel(getter_AddRefs(chan
), baseURI
, nsnull
, nsnull
, callbacks
);
381 RETURN_IF_FAILED(rv
, "NS_OpenURI");
384 //TIMER STARTED-----------------------
385 printf("Starting clock ... \n");
387 rv
= chan
->AsyncOpen(listener
, nsnull
);
388 RETURN_IF_FAILED(rv
, "AsyncOpen");
393 PRUint32 totalTime32
;
394 PRUint64 totalTime64
;
395 LL_SUB(totalTime64
, finish
, start
);
396 LL_L2UI(totalTime32
, totalTime64
);
398 printf("\n\n--------------------\nAll done:\nnum found:%d\nnum start:%d\n", numFound
, numStart
);
400 printf("\n\n>>PageLoadTime>>%u>>\n\n", totalTime32
);
401 } // this scopes the nsCOMPtrs
402 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
403 rv
= NS_ShutdownXPCOM(nsnull
);
404 NS_ASSERTION(NS_SUCCEEDED(rv
), "NS_ShutdownXPCOM failed");