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 namespace TestPageLoad
{
55 int getStrLine(const char *src
, char *str
, int ind
, int max
);
56 nsresult
auxLoad(char *uriBuf
);
57 //----------------------------------------------------------------------
60 #define RETURN_IF_FAILED(rv, step) \
62 if (NS_FAILED(rv)) { \
63 printf(">>> %s failed: rv=%x\n", step, rv); \
68 static nsCString globalStream
;
69 //static char urlBuf[256];
70 static nsCOMPtr
<nsIURI
> baseURI
;
71 static nsCOMPtr
<nsISupportsArray
> uriList
;
73 //Temp, should remove:
74 static int numStart
=0;
75 static int numFound
=0;
77 static PRInt32 gKeepRunning
= 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
.Append(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);
188 if (--gKeepRunning
== 0)
194 MyListener::OnDataAvailable(nsIRequest
*req
, nsISupports
*ctxt
,
195 nsIInputStream
*stream
,
196 PRUint32 offset
, PRUint32 count
)
198 //printf(">>> OnDataAvailable [count=%u]\n", count);
199 nsresult rv
= NS_ERROR_FAILURE
;
200 PRUint32 bytesRead
=0;
205 rv
= stream
->ReadSegments(streamParse
, &offset
, count
, &bytesRead
);
208 PRUint32 amount
= PR_MIN(count
, sizeof(buf
));
209 rv
= stream
->Read(buf
, amount
, &bytesRead
);
215 printf(">>> stream->Read failed with rv=%x\n", rv
);
222 //-----------------------------------------------------------------------------
223 // NotificationCallbacks implementation
224 //-----------------------------------------------------------------------------
226 class MyNotifications
: public nsIInterfaceRequestor
227 , public nsIProgressEventSink
231 NS_DECL_NSIINTERFACEREQUESTOR
232 NS_DECL_NSIPROGRESSEVENTSINK
234 MyNotifications() { }
235 virtual ~MyNotifications() {}
238 NS_IMPL_THREADSAFE_ISUPPORTS2(MyNotifications
,
239 nsIInterfaceRequestor
,
240 nsIProgressEventSink
)
243 MyNotifications::GetInterface(const nsIID
&iid
, void **result
)
245 return QueryInterface(iid
, result
);
249 MyNotifications::OnStatus(nsIRequest
*req
, nsISupports
*ctx
,
250 nsresult status
, const PRUnichar
*statusText
)
252 //printf("status: %x\n", status);
257 MyNotifications::OnProgress(nsIRequest
*req
, nsISupports
*ctx
,
258 PRUint64 progress
, PRUint64 progressMax
)
261 // PR_snprintf(buf, sizeof(buf), "%llu/%llu\n", progress, progressMax);
262 // printf("%s", buf);
266 //-----------------------------------------------------------------------------
268 //-----------------------------------------------------------------------------
270 //---------getStrLine Helper function---------------
271 //Finds a newline in src starting at ind. Puts the
272 //line in str (must be big enough). Returns the index
273 //of the newline, or -1 if at end of string. If reaches
274 //end of string ('\0'), then will copy contents to
276 int getStrLine(const char *src
, char *str
, int ind
, int max
) {
279 globalStream
.Assign('\0');
280 while(c
!='\n' && c
!='\0' && i
<max
) {
286 if(i
==max
|| c
=='\0') {
287 globalStream
.Assign(str
);
288 //printf("\nCarryover (%d|%d):\n------------\n%s\n-------\n",i,max,str);
294 //----------AUX LOAD-----------
295 nsresult
auxLoad(char *uriBuf
)
299 nsCOMPtr
<nsISupportsPRBool
> myBool
= do_CreateInstance(NS_SUPPORTS_PRBOOL_CONTRACTID
);
301 nsCOMPtr
<nsIURI
> uri
;
302 nsCOMPtr
<nsIChannel
> chan
;
303 nsCOMPtr
<nsIStreamListener
> listener
= new MyListener();
304 nsCOMPtr
<nsIInterfaceRequestor
> callbacks
= new MyNotifications();
306 printf("Getting: %s", uriBuf
);
309 if(strncmp(uriBuf
, "http:", 5)) {
311 rv
= NS_NewURI(getter_AddRefs(uri
), uriBuf
, baseURI
);
312 if (NS_FAILED(rv
)) return(rv
);
314 //Absolute link, no base needed
315 rv
= NS_NewURI(getter_AddRefs(uri
), uriBuf
);
316 if (NS_FAILED(rv
)) return(rv
);
319 //Compare to see if exists
321 uriList
->Count(&num
);
323 nsCOMPtr
<nsIURI
> uriTmp
;
324 for(PRUint32 i
= 0; i
< num
; i
++) {
325 uriList
->GetElementAt(i
, getter_AddRefs(uriTmp
));
326 uri
->Equals(uriTmp
, &equal
);
328 printf("(duplicate, canceling) %s\n",uriBuf
);
333 uriList
->AppendElement(uri
);
334 rv
= NS_NewChannel(getter_AddRefs(chan
), uri
, nsnull
, nsnull
, callbacks
);
335 RETURN_IF_FAILED(rv
, "NS_NewChannel");
338 rv
= chan
->AsyncOpen(listener
, myBool
);
339 RETURN_IF_FAILED(rv
, "AsyncOpen");
345 //---------Buffer writer fun---------
349 using namespace TestPageLoad
;
351 //---------MAIN-----------
353 int main(int argc
, char **argv
)
355 if (test_common_init(&argc
, &argv
) != 0)
361 printf("usage: TestPageLoad <url>\n");
365 nsCOMPtr
<nsIServiceManager
> servMan
;
366 NS_InitXPCOM2(getter_AddRefs(servMan
), nsnull
, nsnull
);
367 nsCOMPtr
<nsIComponentRegistrar
> registrar
= do_QueryInterface(servMan
);
368 NS_ASSERTION(registrar
, "Null nsIComponentRegistrar");
370 registrar
->AutoRegister(nsnull
);
372 PRTime start
, finish
;
374 uriList
= do_CreateInstance(NS_SUPPORTSARRAY_CONTRACTID
, &rv
);
375 RETURN_IF_FAILED(rv
, "do_CreateInstance(NS_SUPPORTSARRAY_CONTRACTID)");
377 printf("Loading necko ... \n");
378 nsCOMPtr
<nsIChannel
> chan
;
379 nsCOMPtr
<nsIStreamListener
> listener
= new MyListener();
380 nsCOMPtr
<nsIInterfaceRequestor
> callbacks
= new MyNotifications();
382 rv
= NS_NewURI(getter_AddRefs(baseURI
), argv
[1]);
383 RETURN_IF_FAILED(rv
, "NS_NewURI");
385 rv
= NS_NewChannel(getter_AddRefs(chan
), baseURI
, nsnull
, nsnull
, callbacks
);
386 RETURN_IF_FAILED(rv
, "NS_OpenURI");
389 //TIMER STARTED-----------------------
390 printf("Starting clock ... \n");
392 rv
= chan
->AsyncOpen(listener
, nsnull
);
393 RETURN_IF_FAILED(rv
, "AsyncOpen");
398 PRUint32 totalTime32
;
399 PRUint64 totalTime64
;
400 LL_SUB(totalTime64
, finish
, start
);
401 LL_L2UI(totalTime32
, totalTime64
);
403 printf("\n\n--------------------\nAll done:\nnum found:%d\nnum start:%d\n", numFound
, numStart
);
405 printf("\n\n>>PageLoadTime>>%u>>\n\n", totalTime32
);
406 } // this scopes the nsCOMPtrs
407 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
408 rv
= NS_ShutdownXPCOM(nsnull
);
409 NS_ASSERTION(NS_SUCCEEDED(rv
), "NS_ShutdownXPCOM failed");