~jetsaredim/firefox-extensions/firebug.upstream

« back to all changes in this revision

Viewing changes to content/firebug/tabWatcher.js

  • Committer: Jared Greenwald
  • Date: 2008-04-22 18:20:42 UTC
  • Revision ID: jgreenwa@4lom-20080422182042-90kbumm805h2hjt6
 * new upstream source (v1.2~b21+svn572)

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
// ************************************************************************************************
6
6
// Constants
7
7
 
8
 
const nsIWebNavigation = CI("nsIWebNavigation");
9
 
const nsIWebProgressListener = CI("nsIWebProgressListener");
10
 
const nsIWebProgress = CI("nsIWebProgress");
11
 
const nsISupportsWeakReference = CI("nsISupportsWeakReference");
12
 
const nsISupports = CI("nsISupports");
13
 
const nsIURI = CI("nsIURI");
 
8
const Cc = Components.classes;
 
9
const Ci = Components.interfaces;
 
10
const nsIWebNavigation = Ci.nsIWebNavigation;
 
11
const nsIWebProgressListener = Ci.nsIWebProgressListener;
 
12
const nsIWebProgress = Ci.nsIWebProgress;
 
13
const nsISupportsWeakReference = Ci.nsISupportsWeakReference;
 
14
const nsISupports = Ci.nsISupports;
 
15
const nsIURI = Ci.nsIURI;
14
16
 
15
17
const NOTIFY_STATE_DOCUMENT = nsIWebProgress.NOTIFY_STATE_DOCUMENT;
16
18
 
105
107
     */
106
108
    watchTopWindow: function(win, uri)
107
109
    {
 
110
        if (FBTrace.DBG_WINDOWS)                                                                     /*@explore*/
 
111
            FBTrace.sysout("tabWatcher.watchTopWindow uri="+(typeof(uri) == nsIURI?uri.spec:uri)+"\n");                          /*@explore*/
108
112
        if (tabBrowser.selectedBrowser.cancelNextLoad)
109
113
        {
110
114
            // We need to cancel this load and try again after a delay... this is used
141
145
            context = this.owner.createTabContext(win, browser, browser.chrome, persistedState);
142
146
            contexts.push(context);
143
147
 
 
148
            if (FBTrace.DBG_WINDOWS) {                                                                                 /*@explore*/
 
149
                context.uid = FBL.getUniqueId();                                                                       /*@explore*/
 
150
                FBTrace.sysout("tabWatcher created context ++++++++++++++ with id="+context.uid+" for uri="+uri+" and win.location.href="+win.location.href+"\n"); /*@explore*/
 
151
            }                                                                                                          /*@explore*/
 
152
                                                                                                                       /*@explore*/
144
153
            this.dispatch("initContext", [context]);
145
154
 
146
 
            win.addEventListener("pagehide", onUnloadTopWindow, true);
 
155
            win.addEventListener("pagehide", onPageHideTopWindow, true);
147
156
            win.addEventListener("pageshow", onLoadWindowContent, true);
148
157
            win.addEventListener("DOMContentLoaded", onLoadWindowContent, true);
 
158
            if (FBTrace.DBG_INITIALIZE)                                                                                /*@explore*/
 
159
                FBTrace.sysout("tabWatcher.watchTopWindow pagehide, pageshow, DomContentLoaded addEventListener\n");   /*@explore*/
149
160
        }
150
161
        // XXXjjb at this point we either have context or we just pushed null into contexts and sent it to init...
151
162
 
153
164
        if (context)
154
165
            context.loaded = !context.browser.webProgress.isLoadingDocument;
155
166
 
 
167
        if (FBTrace.DBG_WINDOWS && context.loaded)                                                                     /*@explore*/
 
168
            FBTrace.sysout("***************> Context loaded in tabWatcher.watchTopWindow\n");                          /*@explore*/
156
169
        this.watchContext(win, context);
157
170
    },
158
171
 
166
179
        var context = this.getContextByWindow(win);
167
180
        if ((context && !context.window) || (isSystem && !Firebug.allowSystemPages))
168
181
        {
 
182
            if (FBTrace.DBG_WINDOWS)                                                                                   /*@explore*/
 
183
                FBTrace.sysout("tabWatcher.watchLoadedTopWindow bailing, context.window:"+context.window+" isSystem:"+isSystem+"\n"); /*@explore*/
169
184
            this.unwatchTopWindow(win);
170
185
            this.watchContext(win, null, isSystem);
171
186
            return;
172
187
        }
173
188
 
 
189
        if (FBTrace.DBG_WINDOWS)                                                                                       /*@explore*/
 
190
            FBTrace.sysout("watchLoadedTopWindow context="+(context?(context.uid+" loaded="+context.loaded):'undefined')+"\n"); /*@explore*/
 
191
                                                                                                                       /*@explore*/
174
192
        if (context && !context.loaded)
175
193
        {
176
194
            context.loaded = true;
 
195
            if (FBTrace.DBG_WINDOWS)                                                                                   /*@explore*/
 
196
                FBTrace.sysout("***************> Context loaded in tabWatcher.watchLoadedTopWindow\n");                /*@explore*/
177
197
            this.dispatch("loadedContext", [context]);
178
198
        }
179
199
    },
190
210
        // are called several times, so we have to avoid dispatching watchWindow
191
211
        // more than once
192
212
        var href = win.location.href;
 
213
                                                                                                                       /*@explore*/
 
214
        if (FBTrace.DBG_WINDOWS) {                                                                                     /*@explore*/
 
215
            FBTrace.sysout("watchWindow for href="+href+" context="+context+"\n");                                     /*@explore*/
 
216
            if (context)                                                                                               /*@explore*/
 
217
                for (var i = 0; i < context.windows.length; i++)                                                       /*@explore*/
 
218
                    FBTrace.sysout("watchWindow context("+context.uid+").windows["+i+"]= ("                            /*@explore*/
 
219
                             +context.windows[i].__firebug__uid+") "+context.windows[i].location.href+"\n");           /*@explore*/
 
220
        }                                                                                                              /*@explore*/
 
221
                                                                                                                       /*@explore*/
193
222
        if (context && context.windows.indexOf(win) == -1 && href != aboutBlank)
194
223
        {
195
224
            context.windows.push(win);
196
225
 
 
226
            if (FBTrace.DBG_WINDOWS)                                                                                   /*@explore*/
 
227
                FBTrace.sysout("watchWindow sets context for href="+href+"\n");                                        /*@explore*/
 
228
                                                                                                                       /*@explore*/
197
229
            var eventType = (win.parent == win) ? "pagehide" : "unload";
198
230
            win.addEventListener(eventType, onUnloadWindow, false);
 
231
            if (FBTrace.DBG_INITIALIZE) FBTrace.sysout("tabWatcher.watchWindow "+eventType+" addEventListener\n");     /*@explore*/
199
232
            this.dispatch("watchWindow", [context, win]);
200
233
        }
201
234
    },
206
239
    unwatchTopWindow: function(win)
207
240
    {
208
241
        var context = this.getContextByWindow(win);
 
242
        if (FBTrace.DBG_WINDOWS) FBTrace.dumpStack("tabWatcher.unwatchTopWindow context="+context+"\n");               /*@explore*/
209
243
        this.unwatchContext(win, context);
210
244
    },
211
245
 
217
251
        var context = this.getContextByWindow(win);
218
252
 
219
253
        var index = context ? context.windows.indexOf(win) : -1;
 
254
        if (FBTrace.DBG_WINDOWS)                                                                                       /*@explore*/
 
255
            FBTrace.sysout("tabWatcher.unwatchWindow context="+context+" index of win="+index+"\n");                   /*@explore*/
220
256
        if (index != -1)
221
257
        {
222
258
            context.windows.splice(index, 1);
223
 
            this.dispatch("unwatchWindow", [context, win]);  // XXXjjb Joe check
 
259
            this.dispatch("unwatchWindow", [context, win]);
224
260
        }
225
261
    },
226
262
 
229
265
     */
230
266
    watchBrowser: function(browser)
231
267
    {
 
268
        if (FBTrace.DBG_WINDOWS) FBTrace.sysout("tabWatcher.watchBrowser for URI:"+safeGetURI(browser)+"\n");  /*@explore*/
232
269
        this.watchTopWindow(browser.contentWindow, safeGetURI(browser));
233
270
    },
234
271
 
251
288
        if (!context)
252
289
        {
253
290
            var browser = this.getBrowserByWindow(win);
254
 
            this.owner.destroyTabContext(browser, null);
 
291
            if (this.owner)
 
292
                this.owner.destroyTabContext(browser, null);
 
293
            // else we are probably exiting anyway.
255
294
            return;
256
295
        }
257
296
 
258
297
        var persistedState = {location: context.window.location.href};
259
 
        context.browser.persistedState = persistedState;
 
298
        context.browser.persistedState = persistedState;  // store our state on FF browser elt
260
299
 
261
300
        iterateWindows(context.window, function(win)
262
301
        {
264
303
        });
265
304
 
266
305
        this.dispatch("destroyContext", [context, persistedState]);
 
306
        if (FBTrace.DBG_WINDOWS) FBTrace.sysout("tabWatcher.unwatchContext  ------------------- delete context "+(context.window?context.window.location:"no window")+"\n");
267
307
 
268
308
        if (this.cancelNextLoad)
269
309
        {
271
311
            context.browser.cancelNextLoad = true;
272
312
        }
273
313
 
274
 
        try
275
 
        {
276
 
            context.window.removeEventListener("pagehide", onUnloadTopWindow, true);
277
 
        }
278
 
        catch (exc)
279
 
        {
280
 
        }
281
 
 
282
314
        fbs.countContext(false);
283
315
 
284
316
        this.owner.destroyTabContext(context.browser, context);
291
323
 
292
324
    // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
293
325
 
294
 
    getContextByWindow: function(win)
295
 
    {
296
 
        while (win && win.parent != win)
297
 
            win = win.parent;
298
 
 
299
 
        if (!win) // eg search bar, and sometimes win.parent is null??
300
 
            return;
301
 
 
 
326
    getContextByWindow: function(winIn)
 
327
    {
 
328
        var rootWindow = getRootWindow(winIn);
 
329
 
 
330
        if (rootWindow)
 
331
        {
 
332
            for (var i = 0; i < contexts.length; ++i)
 
333
            {
 
334
                var context = contexts[i];
 
335
                if (context.window == rootWindow)
 
336
                    return context;
 
337
            }
 
338
        }
 
339
 
 
340
        // eg search bar, maybe a global sandbox or other non-window global
 
341
        if (FBTrace.DBG_WINDOWS) FBTrace.sysout("TabWatcher.getContextByWindow rootWindow:"+rootWindow," trying sandboxes\n"); /*@explore*/
 
342
 
 
343
        return this.getContextBySandbox(winIn);
 
344
    },
 
345
 
 
346
    getContextBySandbox: function(sandbox)
 
347
    {
302
348
        for (var i = 0; i < contexts.length; ++i)
303
349
        {
304
350
            var context = contexts[i];
305
 
            if (context.window == win)
306
 
                return context;
 
351
            if (context.sandboxes)
 
352
            {
 
353
                for (var iframe = 0; iframe < context.sandboxes.length; iframe++)
 
354
                {
 
355
                    if (context.sandboxes[iframe] == sandbox)
 
356
                        return context;
 
357
                }
 
358
            }
307
359
        }
 
360
        return null;
308
361
    },
309
362
 
 
363
 
310
364
    getBrowserByWindow: function(win)
311
365
    {
312
366
        for (var i = 0; i < tabBrowser.browsers.length; ++i)
346
400
 
347
401
    dispatch: function(name, args)
348
402
    {
 
403
        if (FBTrace.DBG_WINDOWS)                                                                                       /*@explore*/
 
404
            FBTrace.sysout("TabWatcher.dispatch "+name+" to "+listeners.length+" listeners\n");                        /*@explore*/
 
405
                                                                                                                       /*@explore*/
349
406
        for (var i = 0; i < listeners.length; ++i)
350
407
        {
351
408
            var listener = listeners[i];
352
 
            if (name in listener)
 
409
            if ( listener.hasOwnProperty(name) )
353
410
            {
354
411
                try
355
412
                {
358
415
                catch (exc)
359
416
                {
360
417
                    ERROR(exc);
 
418
                    FBTrace.dumpProperties(" Exception in TabWatcher.dispatch "+ name, exc);                           /*@explore*/
 
419
                    FBTrace.dumpProperties(" Exception in TabWatcher.dispatch for listener[name]:", listener[name]);   /*@explore*/
361
420
                }
362
421
            }
363
422
        }
398
457
        // Only watch windows that are their own parent - e.g. not frames
399
458
        if (progress.DOMWindow.parent == progress.DOMWindow)
400
459
        {
 
460
            if (FBTrace.DBG_WINDOWS)                                                                                   /*@explore*/
 
461
                FBTrace.sysout("TabProgressListener.onLocationChange to uri=\'"                                        /*@explore*/
 
462
                                          +(uri?uri.spec:"null location")+"\'\n");                                     /*@explore*/
 
463
                                                                                                                       /*@explore*/
401
464
            TabWatcher.watchTopWindow(progress.DOMWindow, uri);
402
465
        }
403
466
    },
419
482
{
420
483
    onStateChange: function(progress, request, flag, status)
421
484
    {
 
485
        if (FBTrace.DBG_WINDOWS)                                                                                       /*@explore*/
 
486
                FBTrace.sysout("FrameProgressListener "+getStateDescription(flag)+" uid="                              /*@explore*/
 
487
                             +progress.DOMWindow.__firebug__uid+" request.name="+safeGetName(request)+"\n");                    /*@explore*/
422
488
        if (flag & STATE_IS_REQUEST && flag & STATE_START)
423
489
        {
424
490
            // We need to get the hook in as soon as the new DOMWindow is created, but before
440
506
                    TabWatcher.watchTopWindow(win, win.location.href);
441
507
                    return;  // new one under our thumb
442
508
                }
 
509
                else
 
510
                    TabWatcher.watchWindow(win);
443
511
            }
444
512
        }
445
513
 
458
526
// Local Helpers
459
527
 
460
528
 
 
529
function onPageHideTopWindow(event)
 
530
{
 
531
    var win = event.currentTarget;
 
532
    win.removeEventListener("pagehide", onPageHideTopWindow, true);
 
533
    // http://developer.mozilla.org/en/docs/Using_Firefox_1.5_caching#pagehide_event
 
534
    if (event.persisted) // then the page is cached and there cannot be an unload handler
 
535
    {
 
536
        TabWatcher.unwatchTopWindow(win);
 
537
    }
 
538
    else
 
539
    {
 
540
        // Page is not cached, there may be an unload
 
541
        win.addEventListener("unload", onUnloadTopWindow, true);
 
542
        if (FBTrace.DBG_WINDOWS) /*@explore*/
 
543
            FBTrace.sysout("tabWatcher onPageHideTopWindow set unload handler "+win.location+"\n"); /*@explore*/
 
544
    }
 
545
}
 
546
 
461
547
function onUnloadTopWindow(event)
462
548
{
463
 
    TabWatcher.unwatchTopWindow(event.currentTarget);
 
549
    var win = event.currentTarget;
 
550
    win.removeEventListener("unload", onUnloadTopWindow, true);
 
551
    if (FBTrace.DBG_WINDOWS) /*@explore*/
 
552
        FBTrace.sysout("tabWatcher onUnloadTopWindow "+win.location+"\n"); /*@explore*/
 
553
    TabWatcher.unwatchTopWindow(win);
464
554
}
465
555
 
466
556
function onLoadWindowContent(event)
467
557
{
 
558
    if (FBTrace.DBG_WINDOWS)                                                                                           /*@explore*/
 
559
        FBTrace.sysout("tabWatcher.onLoadWindowContent event.type="+event.type+"\n");                                  /*@explore*/
 
560
                                                                                                                       /*@explore*/
468
561
    var win = event.currentTarget;
469
562
    try
470
563
    {
471
564
        win.removeEventListener("pageshow", onLoadWindowContent, true);
 
565
        if (FBTrace.DBG_INITIALIZE) FBTrace.sysout("tabWatcher.onLoadWindowContent  pageshow removeEventListener\n");  /*@explore*/
472
566
    }
473
567
    catch (exc) {}
474
568
 
475
569
    try
476
570
    {
477
571
        win.removeEventListener("DOMContentLoaded", onLoadWindowContent, true);
 
572
         if (FBTrace.DBG_INITIALIZE) FBTrace.sysout("tabWatcher.onLoadWindowContent  DOMContentLoaded removeEventListener\n"); /*@explore*/
478
573
    }
479
574
    catch (exc) {}
480
575
 
504
599
    var win = event.currentTarget;
505
600
    var eventType = (win.parent == win) ? "pagehide" : "unload";
506
601
    win.removeEventListener(eventType, onUnloadWindow, false);
 
602
    if (FBTrace.DBG_INITIALIZE)                                                                                        /*@explore*/
 
603
        FBTrace.sysout("tabWatcher.onUnloadWindow "+win.location.href +" removeEventListener: "+ eventType+"\n");      /*@explore*/
507
604
    TabWatcher.unwatchWindow(win);
508
605
}
509
606
 
536
633
    }
537
634
}
538
635
 
539
 
function getStateDescription(flag) {
540
 
    var state = "";
541
 
    if (flag & nsIWebProgressListener.STATE_START) state += "STATE_START ";
542
 
    else if (flag & nsIWebProgressListener.STATE_REDIRECTING) state += "STATE_REDIRECTING ";
543
 
    else if (flag & nsIWebProgressListener.STATE_TRANSFERRING) state += "STATE_TRANSFERRING ";
544
 
    else if (flag & nsIWebProgressListener.STATE_NEGOTIATING) state += "STATE_NEGOTIATING ";
545
 
    else if (flag & nsIWebProgressListener.STATE_STOP) state += "STATE_STOP ";
546
 
 
547
 
    if (flag & nsIWebProgressListener.STATE_IS_REQUEST) state += "STATE_IS_REQUEST ";
548
 
    if (flag & nsIWebProgressListener.STATE_IS_DOCUMENT) state += "STATE_IS_DOCUMENT ";
549
 
    if (flag & nsIWebProgressListener.STATE_IS_NETWORK) state += "STATE_IS_NETWORK ";
550
 
    if (flag & nsIWebProgressListener.STATE_IS_WINDOW) state += "STATE_IS_WINDOW ";
551
 
    if (flag & nsIWebProgressListener.STATE_RESTORING) state += "STATE_RESTORING ";
552
 
    if (flag & nsIWebProgressListener.STATE_IS_INSECURE) state += "STATE_IS_INSECURE ";
553
 
    if (flag & nsIWebProgressListener.STATE_IS_BROKEN) state += "STATE_IS_BROKEN ";
554
 
    if (flag & nsIWebProgressListener.STATE_IS_SECURE) state += "STATE_IS_SECURE ";
555
 
    if (flag & nsIWebProgressListener.STATE_SECURE_HIGH) state += "STATE_SECURE_HIGH ";
556
 
    if (flag & nsIWebProgressListener.STATE_SECURE_MED) state += "STATE_SECURE_MED ";
557
 
    if (flag & nsIWebProgressListener.STATE_SECURE_LOW) state += "STATE_SECURE_LOW ";
558
 
 
559
 
    return state;
560
 
}
561
 
 
562
636
// ************************************************************************************************
563
637
 
564
638
}});