Bug 1306892 - Fetch stylesheets from network monitor. r=tromey

If the toolbox is open when a stylesheet is loaded, the network monitor should
have recorded the response content.  When a tool asks for stylesheet text, try
asking the network monitor first before falling back to an extra fetch as a last
resort.

MozReview-Commit-ID: E2pQ04ARfQo

--HG--
extra : rebase_source : b10fb44e313ece5757961ca81a3bc0f76753ed8e
This commit is contained in:
J. Ryan Stinnett 2018-01-05 18:44:25 -06:00
Родитель 803b720962
Коммит 22d7f8de39
3 изменённых файлов: 75 добавлений и 7 удалений

Просмотреть файл

@ -1,6 +1,6 @@
/* vim: set ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
// A test to ensure Style Editor doesn't bybass cache when loading style sheet
@ -42,8 +42,6 @@ add_task(function* () {
}
}
is(items.length, 2,
"Got two requests for doc_uncached.css after Style Editor was loaded.");
ok(items[1].fromCache,
"Second request was loaded from browser cache");
is(items.length, 1,
"Got one request for doc_uncached.css after Style Editor was loaded.");
});

Просмотреть файл

@ -423,6 +423,12 @@ var StyleSheetActor = protocol.ActorClassWithSpec(styleSheetSpec, {
* If an error occurs, the promise is rejected with that error.
*/
fetchStylesheet: Task.async(function* (href) {
// Check if network monitor observed this load, and if so, use that.
let result = this.fetchStylesheetFromNetworkMonitor(href);
if (result) {
return result;
}
let options = {
loadFromCache: true,
policy: Ci.nsIContentPolicy.TYPE_INTERNAL_STYLESHEET,
@ -442,7 +448,6 @@ var StyleSheetActor = protocol.ActorClassWithSpec(styleSheetSpec, {
options.principal = this.ownerDocument.nodePrincipal;
}
let result;
try {
result = yield fetch(this.href, options);
} catch (e) {
@ -458,6 +463,43 @@ var StyleSheetActor = protocol.ActorClassWithSpec(styleSheetSpec, {
return result;
}),
/**
* Try to locate the console actor if it exists via our parent actor (the tab).
*/
get _consoleActor() {
if (this.parentActor.exited) {
return null;
}
let form = this.parentActor.form();
return this.conn._getOrCreateActor(form.consoleActor);
},
/**
* Try to fetch the stylesheet text from the network monitor. If it was enabled during
* the load, it should have a copy of the text saved.
*
* @param string href
* The URL of the sheet to fetch.
*/
fetchStylesheetFromNetworkMonitor(href) {
let consoleActor = this._consoleActor;
if (!consoleActor) {
return null;
}
let request = consoleActor.getNetworkEventActorForURL(href);
if (!request) {
return null;
}
let content = request._response.content;
if (request._discardResponseBody || !content) {
return null;
}
return {
content: content.text,
contentType: content.mimeType,
};
},
/**
* Protocol method to get the media rules for the stylesheet.
*/

Просмотреть файл

@ -68,6 +68,7 @@ function WebConsoleActor(connection, parentActor) {
this.dbg = this.parentActor.makeDebugger();
this._netEvents = new Map();
this._networkEventActorsByURL = new Map();
this._gripDepth = 0;
this._listeners = new Set();
this._lastConsoleInputEvaluation = undefined;
@ -124,13 +125,23 @@ WebConsoleActor.prototype =
/**
* Holds a map between nsIChannel objects and NetworkEventActors for requests
* created with sendHTTPRequest.
* created with sendHTTPRequest or found via the network listener.
*
* @private
* @type Map
*/
_netEvents: null,
/**
* Holds a map from URL to NetworkEventActors for requests noticed by the network
* listener. Requests are added when they start, so the actor might not yet have all
* data for the request until it has completed.
*
* @private
* @type Map
*/
_networkEventActorsByURL: null,
/**
* Holds a set of all currently registered listeners.
*
@ -1632,6 +1643,8 @@ WebConsoleActor.prototype =
let actor = this.getNetworkEventActor(event.channelId);
actor.init(event);
this._networkEventActorsByURL.set(actor._request.url, actor);
let packet = {
from: this.actorID,
type: "networkEvent",
@ -1665,6 +1678,18 @@ WebConsoleActor.prototype =
return actor;
},
/**
* Get the NetworkEventActor for a given URL that may have been noticed by the network
* listener. Requests are added when they start, so the actor might not yet have all
* data for the request until it has completed.
*
* @param string url
* The URL of the request to search for.
*/
getNetworkEventActorForURL(url) {
return this._networkEventActorsByURL.get(url);
},
/**
* Send a new HTTP request from the target's window.
*
@ -1992,6 +2017,9 @@ NetworkEventActor.prototype =
}
this._longStringActors = new Set();
if (this._request.url) {
this.parent._networkEventActorsByURL.delete(this._request.url);
}
if (this.channel) {
this.parent._netEvents.delete(this.channel);
}