My favorites | Sign in
Project Home Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/* See license.txt for terms of usage */

FBL.ns(function() { with (FBL) {

// ************************************************************************************************
// Constants

const Cc = Components.classes;
const Ci = Components.interfaces;

const ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
const prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch2);
const versionChecker = Cc["@mozilla.org/xpcom/version-comparator;1"].getService(Ci.nsIVersionComparator);
const appInfo = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULAppInfo);

// Maximum cached size of a signle response (bytes)
var responseSizeLimit = 1024 * 1024 * 5;

// List of text content types. These content-types are cached.
var contentTypes =
{
"text/plain": 1,
"text/html": 1,
"text/xml": 1,
"text/xsl": 1,
"text/xul": 1,
"text/css": 1,
"text/sgml": 1,
"text/rtf": 1,
"text/x-setext": 1,
"text/richtext": 1,
"text/javascript": 1,
"text/jscript": 1,
"text/tab-separated-values": 1,
"text/rdf": 1,
"text/xif": 1,
"text/ecmascript": 1,
"text/vnd.curl": 1,
"text/x-json": 1,
"text/x-js": 1,
"text/js": 1,
"text/vbscript": 1,
"view-source": 1,
"view-fragment": 1,
"application/xml": 1,
"application/xhtml+xml": 1,
"application/vnd.mozilla.xul+xml": 1,
"application/javascript": 1,
"application/x-javascript": 1,
"application/x-httpd-php": 1,
"application/rdf+xml": 1,
"application/ecmascript": 1,
"application/http-index-format": 1,
"application/json": 1,
"application/x-js": 1,
"multipart/mixed" : 1,
"multipart/x-mixed-replace" : 1,
"image/svg+xml" : 1
};

// ************************************************************************************************
// Model implementation

/**
* Implementation of cache model. The only purpose of this object is to register an HTTP
* observer so, HTTP communication can be interecepted and all incoming data stored within
* a cache.
*/
Firebug.TabCacheModel = extend(Firebug.Module,
{
dispatchName: "tabCache",
contentTypes: contentTypes,
fbListeners: [],

initializeUI: function(owner)
{
if (FBTrace.DBG_CACHE)
FBTrace.sysout("tabCache.initializeUI; Cache model initialized.");

// Read maximum size limit for cached response from preferences.
responseSizeLimit = Firebug.getPref(Firebug.prefDomain, "cache.responseLimit");

// Read additional text mime-types from preferences.
var mimeTypes = Firebug.getPref(Firebug.prefDomain, "cache.mimeTypes");
if (mimeTypes)
{
var list = mimeTypes.split(" ");
for (var i=0; i<list.length; i++)
contentTypes[list[i]] = 1;
}

// Register for HTTP events.
if (Ci.nsITraceableChannel)
httpObserver.addObserver(this, "firebug-http-event", false);
},

shutdown: function()
{
if (FBTrace.DBG_CACHE)
FBTrace.sysout("tabCache.shutdown; Cache model destroyed.");

if (Ci.nsITraceableChannel)
httpObserver.removeObserver(this, "firebug-http-event");
},

initContext: function(context)
{
if (FBTrace.DBG_CACHE)
FBTrace.sysout("tabCache.initContext for: " + context.getName());
},

/* nsIObserver */
observe: function(subject, topic, data)
{
try
{
if (!(subject instanceof Ci.nsIHttpChannel))
return;
// XXXjjb this same code is in net.js, better to have it only once
var win = getWindowForRequest(subject);
if (win)
var tabId = Firebug.getTabIdForWindow(win);
if (!tabId)
return;

if (topic == "http-on-modify-request")
this.onModifyRequest(subject, win, tabId);
else if (topic == "http-on-examine-response")
this.onExamineResponse(subject, win, tabId);
else if (topic == "http-on-examine-cached-response")
this.onCachedResponse(subject, win, tabId);
}
catch (err)
{
if (FBTrace.DBG_ERRORS)
FBTrace.sysout("tabCache.observe EXCEPTION", err);
}
},

onModifyRequest: function(request, win, tabId)
{
},

onExamineResponse: function(request, win, tabId)
{
this.registerStreamListener(request, win);
},

onCachedResponse: function(request, win, tabId)
{
this.registerStreamListener(request, win);
},

registerStreamListener: function(request, win)
{
try
{
// Due to #489317, the content type must be checked in onStartRequest
//if (!this.shouldCacheRequest(request))
// return;

request.QueryInterface(Ci.nsITraceableChannel);

// Create Firebug's stream listener that is tracing HTTP responses.
var newListener = CCIN("@joehewitt.com/firebug-channel-listener;1", "nsIStreamListener");
newListener.wrappedJSObject.window = win;

// Set proxy listener for back notifiction from XPCOM to chrome (using real interface
// so nsIRequest object is properly passed from XPCOM scope).
newListener.QueryInterface(Ci.nsITraceableChannel);
newListener.setNewListener(new ChannelListenerProxy(win));

// Add tee listener into the chain of request stream listeners so, the chain
// doesn't include a JS code. This way all exceptions are propertly distributed
// (#515051).
// The nsIStreamListenerTee differes in following branches:
// 1.9.1: not possible to registere nsIRequestObserver with tee
// 1.9.2: implements nsIStreamListenerTee_1_9_2 with initWithObserver methods
// 1.9.3: adds third parameter to the existing init method.
if (versionChecker.compare(appInfo.version, "3.6*") >= 0)
{
var tee = CCIN("@mozilla.org/network/stream-listener-tee;1", "nsIStreamListenerTee");
tee = tee.QueryInterface(Ci.nsIStreamListenerTee);

if (Ci.nsIStreamListenerTee_1_9_2)
tee = tee.QueryInterface(Ci.nsIStreamListenerTee_1_9_2);

// The response will be written into the outputStream of this pipe.
// Both ends of the pipe must be blocking.
var sink = CCIN("@mozilla.org/pipe;1", "nsIPipe");
sink.init(false, false, 0x20000, 0x4000, null);

// Remember the input stream, so it isn't released by GC.
// See issue 2788 for more details.
newListener.wrappedJSObject.inputStream = sink.inputStream;

var originalListener = request.setNewListener(tee);
newListener.wrappedJSObject.sink = sink;

if (tee.initWithObserver)
tee.initWithObserver(originalListener, sink.outputStream, newListener);
else
tee.init(originalListener, sink.outputStream, newListener);
}
else
{
newListener.wrappedJSObject.listener = request.setNewListener(newListener);
}

// xxxHonza: this is a workaround for #489317. Just passing
// shouldCacheRequest method to the component so, onStartRequest
// can decide whether to cache or not.
newListener.wrappedJSObject.shouldCacheRequest = function(request)
{
try {
return Firebug.TabCacheModel.shouldCacheRequest(request)
} catch (err) {}
return false;
}

// xxxHonza: this is a workaround for the tracing-listener to get the
// right context. Notice that if the window (parent browser) is closed
// during the response download the TabWatcher (used within this method)
// is undefined. But in such a case no cache is needed anyway.
// Another thing is that the context isn't available now, but will be
// as soon as this method is used from the stream listener.
newListener.wrappedJSObject.getContext = function(win)
{
try {
return TabWatcher.getContextByWindow(win);
} catch (err){}
return null;
}
}
catch (err)
{
if (FBTrace.DBG_ERRORS)
FBTrace.sysout("tabCache: Register Traceable Listener EXCEPTION", err);
}
},

shouldCacheRequest: function(request)
{
request.QueryInterface(Ci.nsIHttpChannel);

// Allow to customize caching rules.
if (dispatch2(this.fbListeners, "shouldCacheRequest", [request]))
return true;

// Cache only text responses for now.
var contentType = request.contentType;
if (contentType)
contentType = contentType.split(";")[0];

contentType = trim(contentType);
if (contentTypes[contentType])
return true;

// Hack to work around application/octet-stream for js files (2063).
// Let's cache all files with js extensions.
var extension = getFileExtension(safeGetName(request));
if (extension == "js")
return true;

if (FBTrace.DBG_CACHE)
FBTrace.sysout("tabCache.shouldCacheRequest; Request not cached: " +
request.contentType + ", " + safeGetName(request));

return false;
},
});

// ************************************************************************************************

/**
* This cache object is intended to cache all responses made by a specific tab.
* The implementation is based on nsITraceableChannel interface introduced in
* Firefox 3.0.4. This interface allows to intercept all incoming HTTP data.
*
* This object replaces the SourceCache, which still exist only for backward
* compatibility.
*
* The object is derived from SourceCache so, the same interface and most of the
* implementation is used.
*/
Firebug.TabCache = function(context)
{
if (FBTrace.DBG_CACHE)
FBTrace.sysout("tabCache.TabCache Created for: " + context.getName());

Firebug.SourceCache.call(this, context);
};

Firebug.TabCache.prototype = extend(Firebug.SourceCache.prototype,
{
responses: [], // responses in progress.

storePartialResponse: function(request, responseText, win)
{
if (FBTrace.DBG_CACHE)
FBTrace.sysout("tabCache.storePartialResponse " + safeGetName(request),
request.contentCharset);

try
{
responseText = FBL.convertToUnicode(responseText, win.document.characterSet);
}
catch (err)
{
if (FBTrace.DBG_ERRORS || FBTrace.DBG_CACHE)
FBTrace.sysout("tabCache.storePartialResponse EXCEPTION " +
safeGetName(request), err);

// Even responses that are not converted are stored into the cache.
// return false;
}

var url = safeGetName(request);
var response = this.getResponse(request);

// Size of each response is limited.
var limitNotReached = true;
if (response.size + responseText.length >= responseSizeLimit)
{
limitNotReached = false;
responseText = responseText.substr(0, responseSizeLimit - response.size);
FBTrace.sysout("tabCache.storePartialResponse Max size limit reached for: " + url);
}

response.size += responseText.length;

// Store partial content into the cache.
this.store(url, responseText);

// Return false if furhter parts of this response should be ignored.
return limitNotReached;
},

getResponse: function(request)
{
var url = safeGetName(request);
var response = this.responses[url];
if (!response)
{
this.invalidate(url);
this.responses[url] = response = {
request: request,
size: 0
};
}

return response;
},

storeSplitLines: function(url, lines)
{
if (FBTrace.DBG_CACHE)
FBTrace.sysout("tabCache.storeSplitLines: " + url, lines);

var currLines = this.cache[url];
if (!currLines)
currLines = this.cache[url] = [];

// Join the last line with the new first one so, the source code
// lines are properly formatted...
if (currLines.length)
{
// ... but only if the last line isn't already completed.
var lastLine = currLines[currLines.length-1];
if (lastLine && lastLine.search(/\r|\n/) == -1)
currLines[currLines.length-1] += lines.shift();
}

// Append new lines (if any) into the array for specified url.
if (lines.length)
this.cache[url] = currLines.concat(lines);

return this.cache[url];
},

loadFromCache: function(url, method, file)
{
// The ancestor implementation (SourceCache) uses ioService.newChannel, which
// can result in additional request to the server (in case the response can't
// be loaded from the Firefox cache) - known as double-load problem.
// This new implementation (TabCache) uses nsITraceableChannel so, all responses
// should be already cached.

// xxxHonza: TODO entire implementation of this method should be removed in Firebug 1.5
// xxxHonza: let's try to get the response from the cache till #449198 is fixed.
var stream;
var responseText;
try
{
if (!url)
return responseText;

var channel = ioService.newChannel(url, null, null);

// These flag combination doesn't repost the request.
channel.loadFlags = Ci.nsIRequest.LOAD_FROM_CACHE |
Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE |
Ci.nsICachingChannel.LOAD_BYPASS_LOCAL_CACHE_IF_BUSY;

var charset = "UTF-8";
var doc = this.context.window.document;
if (doc)
charset = doc.characterSet;

stream = channel.open();

// The response doesn't have to be in the browser cache.
if (!stream.available())
{
if (FBTrace.DBG_CACHE)
FBTrace.sysout("tabCache.loadFromCache; Failed to load source for: " + url);

stream.close();
return [$STR("message.Failed to load source for") + ": " + url];
}

// Don't load responses that shouldn't be cached.
if (!Firebug.TabCacheModel.shouldCacheRequest(channel))
{
if (FBTrace.DBG_CACHE)
FBTrace.sysout("tabCache.loadFromCache; The resource from this URL is not text: " + url);

stream.close();
return [$STR("message.The resource from this URL is not text") + ": " + url];
}

responseText = readFromStream(stream, charset);

if (FBTrace.DBG_CACHE)
FBTrace.sysout("tabCache.loadFromCache (response coming from FF Cache) " +
url, responseText);

responseText = this.store(url, responseText);
}
catch (err)
{
if (FBTrace.DBG_ERRORS || FBTrace.DBG_CACHE)
FBTrace.sysout("tabCache.loadFromCache EXCEPTION on url \'" + url +"\'", err);
}
finally
{
if (stream)
stream.close();
}

return responseText;
},

// nsIStreamListener - callbacks from channel stream listener component.
onStartRequest: function(request, requestContext)
{
if (FBTrace.DBG_CACHE)
FBTrace.sysout("tabCache.channel.startRequest: " + safeGetName(request));

// Make sure the response-entry (used to count total response size) is properly
// initialized (cleared) now. If no data is received, the response entry remains empty.
var response = this.getResponse(request);

dispatch(Firebug.TabCacheModel.fbListeners, "onStartRequest", [this.context, request]);
dispatch(this.fbListeners, "onStartRequest", [this.context, request]);
},

onDataAvailable: function(request, requestContext, inputStream, offset, count)
{
if (FBTrace.DBG_CACHE)
FBTrace.sysout("tabCache.channel.onDataAvailable: " + safeGetName(request));

// If the stream is read a new one must be provided (the stream doesn't implement
// nsISeekableStream).
var stream = {
value: inputStream
};

dispatch(Firebug.TabCacheModel.fbListeners, "onDataAvailable",
[this.context, request, requestContext, stream, offset, count]);
dispatch(this.fbListeners, "onDataAvailable", [this.context,
request, requestContext, stream, offset, count]);

return stream.value;
},

onStopRequest: function(request, requestContext, statusCode)
{
// The response is finally received so, remove the request from the list of
// current responses.
var url = safeGetName(request);
delete this.responses[url];

var lines = this.cache[this.removeAnchor(url)];
var responseText = lines ? lines.join("") : "";

if (FBTrace.DBG_CACHE)
FBTrace.sysout("tabCache.channel.stopRequest: " + safeGetRequestName(request), responseText);

dispatch(Firebug.TabCacheModel.fbListeners, "onStopRequest", [this.context, request, responseText]);
dispatch(this.fbListeners, "onStopRequest", [this.context, request, responseText]);
}
});

// ************************************************************************************************
// Proxy Listener

function ChannelListenerProxy(win)
{
this.wrappedJSObject = this;
this.window = win;
}

ChannelListenerProxy.prototype =
{
/* nsIStreamListener */
onStartRequest: function(request, requestContext)
{
var context = this.getContext();
if (context)
context.sourceCache.onStartRequest(request, requestContext);
},

onDataAvailable: function(request, requestContext, inputStream, offset, count)
{
var context = this.getContext();
if (!context)
return null;

return context.sourceCache.onDataAvailable(request, requestContext, inputStream, offset, count);
},

onStopRequest: function(request, requestContext, statusCode)
{
var context = this.getContext();
if (context)
context.sourceCache.onStopRequest(request, requestContext, statusCode);
},

/* nsISupports */
QueryInterface: function(iid)
{
if (iid.equals(Ci.nsIStreamListener) ||
iid.equals(Ci.nsISupportsWeakReference) ||
iid.equals(Ci.nsISupports))
{
return this;
}

throw Components.results.NS_NOINTERFACE;
},

getContext: function()
{
try {
return TabWatcher.getContextByWindow(this.window);
} catch (e) {}
return null;
}
}

// ************************************************************************************************
// Helpers

function safeGetName(request)
{
try {
return request.name;
}
catch (exc) {
}

return null;
}

// ************************************************************************************************
// Registration

Firebug.registerModule(Firebug.TabCacheModel);

// ************************************************************************************************

}});

Change log

r6551 by odvarko on Apr 14, 2010   Diff
Backport 1.6 -> 1.5: 6323, 6369, 6493,
6494
Go to: 
Project members, sign in to write a code review

Older revisions

r5850 by odvarko on Jan 26, 2010   Diff
Merge 1.6 -> 1.5: 5737, 5739, 5746,
5774, 5776, 5780
r5698 by odvarko on Jan 15, 2010   Diff
Merge 1.6 -> 1.5: 5678, 5686, 5687,
5695, 5697
r5431 by odvarko on Dec 22, 2009   Diff
Tracing updated for cache.
All revisions of this file

File info

Size: 20039 bytes, 583 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting