~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/netwerk/test/TestPageLoad.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
 *
 
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/
 
9
 *
 
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
 
13
 * License.
 
14
 *
 
15
 * The Original Code is Mozilla Communicator client code.
 
16
 *
 
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.
 
21
 *
 
22
 * Contributor(s):
 
23
 *
 
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.
 
35
 *
 
36
 * ***** END LICENSE BLOCK ***** */
 
37
 
 
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"
 
45
#include "prprf.h"
 
46
#include "nsXPCOM.h"
 
47
#include "nsISupportsPrimitives.h"
 
48
#include "nsTimer.h"
 
49
#include "prlong.h"
 
50
#include "plstr.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
//----------------------------------------------------------------------
 
57
 
 
58
 
 
59
#define RETURN_IF_FAILED(rv, step) \
 
60
    PR_BEGIN_MACRO \
 
61
    if (NS_FAILED(rv)) { \
 
62
        printf(">>> %s failed: rv=%x\n", step, rv); \
 
63
        return rv;\
 
64
    } \
 
65
    PR_END_MACRO
 
66
 
 
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;
 
74
 
 
75
//Temp, should remove:
 
76
static int numStart=0;
 
77
static int numFound=0;
 
78
 
 
79
 
 
80
//--------writer fun----------------------
 
81
 
 
82
static NS_METHOD streamParse (nsIInputStream* in,
 
83
                              void* closure,
 
84
                              const char* fromRawSegment,
 
85
                              PRUint32 toOffset,
 
86
                              PRUint32 count,
 
87
                              PRUint32 *writeCount) {
 
88
 
 
89
  char parseBuf[2048], loc[2048], lineBuf[2048];
 
90
  char *loc_t, *loc_t2;
 
91
  int i = 0;
 
92
  char *tmp;
 
93
 
 
94
  if(!globalStream.IsEmpty()) {
 
95
    globalStream.AppendWithConversion(fromRawSegment);
 
96
    tmp = ToNewCString(globalStream);
 
97
    //printf("\n>>NOW:\n^^^^^\n%s\n^^^^^^^^^^^^^^", tmp);
 
98
  } else {
 
99
    tmp = (char *)fromRawSegment;
 
100
  }
 
101
 
 
102
  while(i < (int)count) {
 
103
    i = getStrLine(tmp, lineBuf, i, count);
 
104
    if(i < 0) {
 
105
      *writeCount = count;
 
106
      return NS_OK;
 
107
    }
 
108
    parseBuf[0]='\0';
 
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");
 
112
      if(loc_t2!=NULL) {
 
113
        loc_t2+=3;
 
114
        strcpy(loc, loc_t2);
 
115
        sscanf(loc, "=\"%[^\"]", parseBuf);
 
116
        if(parseBuf[0]=='\0')
 
117
          sscanf(loc, "=%s", parseBuf);         
 
118
        if(parseBuf[0]!='\0'){
 
119
          numFound++;
 
120
          auxLoad(parseBuf);
 
121
        }     
 
122
      }
 
123
    }
 
124
 
 
125
    /***NEED BETTER CHECK FOR STYLESHEETS
 
126
    if((loc_t=PL_strcasestr(lineBuf, "link"))!= NULL) { 
 
127
       loc_t2=PL_strcasestr(loc_t, "href");
 
128
      if(loc_t2!=NULL) {
 
129
        loc_t2+=4;
 
130
        strcpy(loc, loc_t2);
 
131
        //printf("%s\n", loc);
 
132
        sscanf(loc, "=\"%[^\"]", parseBuf);
 
133
        if(parseBuf[0]!='\0'){
 
134
          //printf("%s\n", parseBuf);
 
135
          numFound++;
 
136
          auxLoad(parseBuf);
 
137
        }     
 
138
      }
 
139
    }
 
140
    */
 
141
    if((loc_t=PL_strcasestr(lineBuf, "background"))!=NULL) {
 
142
      loc_t+=10;
 
143
      strcpy(loc, loc_t);
 
144
      sscanf(loc, "=\"%[^\"]", parseBuf);
 
145
      if(parseBuf[0]!='\0') {
 
146
        numFound++;
 
147
        auxLoad(parseBuf);
 
148
      }
 
149
    }
 
150
    i++;
 
151
 
 
152
  }
 
153
  *writeCount = count;
 
154
  return NS_OK;
 
155
}
 
156
 
 
157
//-----------------------------------------------------------------------------
 
158
// nsIStreamListener implementation
 
159
//-----------------------------------------------------------------------------
 
160
 
 
161
class MyListener : public nsIStreamListener
 
162
{
 
163
public:
 
164
    NS_DECL_ISUPPORTS
 
165
    NS_DECL_NSIREQUESTOBSERVER
 
166
    NS_DECL_NSISTREAMLISTENER
 
167
 
 
168
    MyListener() { }
 
169
    virtual ~MyListener() {}
 
170
};
 
171
 
 
172
NS_IMPL_ISUPPORTS2(MyListener,
 
173
                   nsIRequestObserver,
 
174
                   nsIStreamListener)
 
175
 
 
176
NS_IMETHODIMP
 
177
MyListener::OnStartRequest(nsIRequest *req, nsISupports *ctxt)
 
178
{
 
179
  //printf(">>> OnStartRequest\n");
 
180
    numStart++;
 
181
    return NS_OK;
 
182
}
 
183
 
 
184
NS_IMETHODIMP
 
185
MyListener::OnStopRequest(nsIRequest *req, nsISupports *ctxt, nsresult status)
 
186
{
 
187
    //printf(">>> OnStopRequest status=%x\n", status);
 
188
    gKeepRunning--;
 
189
    return NS_OK;
 
190
}
 
191
 
 
192
NS_IMETHODIMP
 
193
MyListener::OnDataAvailable(nsIRequest *req, nsISupports *ctxt,
 
194
                            nsIInputStream *stream,
 
195
                            PRUint32 offset, PRUint32 count)
 
196
{
 
197
    //printf(">>> OnDataAvailable [count=%u]\n", count);
 
198
    nsresult rv = NS_ERROR_FAILURE;
 
199
    PRUint32 bytesRead=0;
 
200
    char buf[1024];
 
201
 
 
202
    if(ctxt == nsnull) {
 
203
      bytesRead=0;
 
204
      rv = stream->ReadSegments(streamParse, &offset, count, &bytesRead);
 
205
    } else {
 
206
      while (count) {
 
207
        PRUint32 amount = PR_MIN(count, sizeof(buf));
 
208
        rv = stream->Read(buf, amount, &bytesRead);  
 
209
        count -= bytesRead;
 
210
      }
 
211
    }
 
212
 
 
213
    if (NS_FAILED(rv)) {
 
214
      printf(">>> stream->Read failed with rv=%x\n", rv);
 
215
      return rv;
 
216
    }
 
217
 
 
218
    return NS_OK;
 
219
}
 
220
 
 
221
//-----------------------------------------------------------------------------
 
222
// NotificationCallbacks implementation
 
223
//-----------------------------------------------------------------------------
 
224
 
 
225
class MyNotifications : public nsIInterfaceRequestor
 
226
                      , public nsIProgressEventSink
 
227
{
 
228
public:
 
229
    NS_DECL_ISUPPORTS
 
230
    NS_DECL_NSIINTERFACEREQUESTOR
 
231
    NS_DECL_NSIPROGRESSEVENTSINK
 
232
 
 
233
    MyNotifications() { }
 
234
    virtual ~MyNotifications() {}
 
235
};
 
236
 
 
237
NS_IMPL_THREADSAFE_ISUPPORTS2(MyNotifications,
 
238
                              nsIInterfaceRequestor,
 
239
                              nsIProgressEventSink)
 
240
 
 
241
NS_IMETHODIMP
 
242
MyNotifications::GetInterface(const nsIID &iid, void **result)
 
243
{
 
244
    return QueryInterface(iid, result);
 
245
}
 
246
 
 
247
NS_IMETHODIMP
 
248
MyNotifications::OnStatus(nsIRequest *req, nsISupports *ctx,
 
249
                          nsresult status, const PRUnichar *statusText)
 
250
{
 
251
    //printf("status: %x\n", status);
 
252
    return NS_OK;
 
253
}
 
254
 
 
255
NS_IMETHODIMP
 
256
MyNotifications::OnProgress(nsIRequest *req, nsISupports *ctx,
 
257
                            PRUint32 progress, PRUint32 progressMax)
 
258
{
 
259
    //printf("progress: %u/%u\n", progress, progressMax);
 
260
    return NS_OK;
 
261
}
 
262
 
 
263
//-----------------------------------------------------------------------------
 
264
// main, etc..
 
265
//-----------------------------------------------------------------------------
 
266
 
 
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 
 
272
//globalStream. 
 
273
int getStrLine(const char *src, char *str, int ind, int max) {
 
274
  char c = src[ind];
 
275
  int i=0;
 
276
  globalStream.Assign(NS_LITERAL_STRING("\0"));
 
277
  while(c!='\n' && c!='\0' && i<max) {
 
278
    str[i] = src[ind];
 
279
    i++; ind++;
 
280
    c = src[ind];
 
281
  }
 
282
  str[i]='\0';
 
283
  if(i==max || c=='\0') {
 
284
    globalStream.AssignWithConversion(str);
 
285
    //printf("\nCarryover (%d|%d):\n------------\n%s\n-------\n",i,max,str);
 
286
    return -1;
 
287
  }
 
288
  return ind;
 
289
}
 
290
 
 
291
//----------AUX LOAD-----------
 
292
nsresult auxLoad(char *uriBuf)
 
293
{
 
294
    nsresult rv;
 
295
 
 
296
    nsCOMPtr<nsISupportsPRBool> myBool = do_CreateInstance(NS_SUPPORTS_PRBOOL_CONTRACTID);
 
297
 
 
298
    nsCOMPtr<nsIURI> uri;
 
299
    nsCOMPtr<nsIChannel> chan;
 
300
    nsCOMPtr<nsIStreamListener> listener = new MyListener();
 
301
    nsCOMPtr<nsIInterfaceRequestor> callbacks = new MyNotifications();
 
302
 
 
303
    printf("Getting: %s", uriBuf);
 
304
 
 
305
    //If relative link
 
306
    if(strncmp(uriBuf, "http:", 5)) {
 
307
      //Relative link
 
308
      rv = NS_NewURI(getter_AddRefs(uri), uriBuf, baseURI);
 
309
      if (NS_FAILED(rv)) return(rv);
 
310
    } else {
 
311
      //Absolute link, no base needed
 
312
      rv = NS_NewURI(getter_AddRefs(uri), uriBuf);
 
313
      if (NS_FAILED(rv)) return(rv);
 
314
    }
 
315
 
 
316
    //Compare to see if exists
 
317
    PRUint32 num;
 
318
    uriList->Count(&num);
 
319
    PRBool equal;
 
320
    nsCOMPtr<nsIURI> uriTmp;
 
321
    for(PRUint32 i = 0; i < num; i++) {
 
322
      uriList->GetElementAt(i, getter_AddRefs(uriTmp));
 
323
      uri->Equals(uriTmp, &equal);
 
324
      if(equal) {
 
325
        printf("(duplicate, canceling) %s\n",uriBuf); 
 
326
        return NS_OK;
 
327
      }
 
328
    }
 
329
    printf("\n");
 
330
    uriList->AppendElement(uri);
 
331
    rv = NS_NewChannel(getter_AddRefs(chan), uri, nsnull, nsnull, callbacks);
 
332
    RETURN_IF_FAILED(rv, "NS_NewChannel");
 
333
 
 
334
    gKeepRunning++;
 
335
    rv = chan->AsyncOpen(listener, myBool);
 
336
    RETURN_IF_FAILED(rv, "AsyncOpen");
 
337
 
 
338
    return NS_OK;
 
339
 
 
340
}
 
341
 
 
342
//---------Buffer writer fun---------
 
343
 
 
344
 
 
345
//---------MAIN-----------
 
346
 
 
347
int main(int argc, char **argv)
 
348
{
 
349
    nsresult rv;
 
350
 
 
351
    if (argc == 1) {
 
352
        printf("usage: TestPageLoad <url>\n");
 
353
        return -1;
 
354
    }
 
355
    {
 
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");
 
360
        if (registrar)
 
361
            registrar->AutoRegister(nsnull);
 
362
 
 
363
        PRTime start, finish;
 
364
 
 
365
        rv = NS_NewISupportsArray(getter_AddRefs(uriList));
 
366
        RETURN_IF_FAILED(rv, "NS_NewISupportsArray");
 
367
 
 
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)");
 
372
 
 
373
        rv = eqs->CreateMonitoredThreadEventQueue();
 
374
        RETURN_IF_FAILED(rv, "CreateMonitoredThreadEventQueue");
 
375
 
 
376
        rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
 
377
        RETURN_IF_FAILED(rv, "GetThreadEventQueue");
 
378
 
 
379
        printf("Loading necko ... \n");
 
380
        nsCOMPtr<nsIChannel> chan;
 
381
        nsCOMPtr<nsIStreamListener> listener = new MyListener();
 
382
        nsCOMPtr<nsIInterfaceRequestor> callbacks = new MyNotifications();
 
383
 
 
384
        rv = NS_NewURI(getter_AddRefs(baseURI), argv[1]);
 
385
        RETURN_IF_FAILED(rv, "NS_NewURI");
 
386
 
 
387
        rv = NS_NewChannel(getter_AddRefs(chan), baseURI, nsnull, nsnull, callbacks);
 
388
        RETURN_IF_FAILED(rv, "NS_OpenURI");
 
389
        gKeepRunning++;
 
390
 
 
391
        //TIMER STARTED-----------------------
 
392
        printf("Starting clock ... \n");
 
393
        start = PR_Now();
 
394
        rv = chan->AsyncOpen(listener, nsnull);
 
395
        RETURN_IF_FAILED(rv, "AsyncOpen");
 
396
 
 
397
        while (gKeepRunning) {
 
398
            gEventQ->ProcessPendingEvents();
 
399
        }
 
400
 
 
401
        finish = PR_Now();
 
402
        PRUint32 totalTime32;
 
403
        PRUint64 totalTime64;
 
404
        LL_SUB(totalTime64, finish, start);
 
405
        LL_L2UI(totalTime32, totalTime64);
 
406
 
 
407
        printf("\n\n--------------------\nAll done:\nnum found:%d\nnum start:%d\n", numFound, numStart);
 
408
 
 
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");
 
414
    return 0;
 
415
}