WebInspector.Resource = function(request, url, documentURL, frameId, loaderId, type, mimeType)
{
this._request = request;
if (this._request)
this._request.setResource(this);
this.url = url;
this._documentURL = documentURL;
this._frameId = frameId;
this._loaderId = loaderId;
this._type = type || WebInspector.resourceTypes.Other;
this._mimeType = mimeType;
this.history = [];
this._content;
this._contentEncoded;
this._pendingContentCallbacks = [];
}
WebInspector.Resource._domainModelBindings = [];
WebInspector.Resource.registerDomainModelBinding = function(type, binding)
{
WebInspector.Resource._domainModelBindings[type.name()] = binding;
}
WebInspector.Resource._resourceRevisionRegistry = function()
{
if (!WebInspector.Resource._resourceRevisionRegistryObject) {
if (window.localStorage) {
var resourceHistory = window.localStorage["resource-history"];
try {
WebInspector.Resource._resourceRevisionRegistryObject = resourceHistory ? JSON.parse(resourceHistory) : {};
} catch (e) {
WebInspector.Resource._resourceRevisionRegistryObject = {};
}
} else
WebInspector.Resource._resourceRevisionRegistryObject = {};
}
return WebInspector.Resource._resourceRevisionRegistryObject;
}
WebInspector.Resource.restoreRevisions = function()
{
var registry = WebInspector.Resource._resourceRevisionRegistry();
var filteredRegistry = {};
for (var url in registry) {
var historyItems = registry[url];
var resource = WebInspector.resourceForURL(url);
var filteredHistoryItems = [];
for (var i = 0; historyItems && i < historyItems.length; ++i) {
var historyItem = historyItems[i];
if (resource && historyItem.loaderId === resource.loaderId) {
resource.addRevision(window.localStorage[historyItem.key], new Date(historyItem.timestamp), true);
filteredHistoryItems.push(historyItem);
filteredRegistry[url] = filteredHistoryItems;
} else
delete window.localStorage[historyItem.key];
}
}
WebInspector.Resource._resourceRevisionRegistryObject = filteredRegistry;
function persist()
{
window.localStorage["resource-history"] = JSON.stringify(filteredRegistry);
}
setTimeout(persist, 0);
}
WebInspector.Resource.persistRevision = function(resource)
{
if (!window.localStorage)
return;
var url = resource.url;
var loaderId = resource.loaderId;
var timestamp = resource._contentTimestamp.getTime();
var key = "resource-history|" + url + "|" + loaderId + "|" + timestamp;
var content = resource._content;
var registry = WebInspector.Resource._resourceRevisionRegistry();
var historyItems = registry[resource.url];
if (!historyItems) {
historyItems = [];
registry[resource.url] = historyItems;
}
historyItems.push({url: url, loaderId: loaderId, timestamp: timestamp, key: key});
function persist()
{
window.localStorage[key] = content;
window.localStorage["resource-history"] = JSON.stringify(registry);
}
setTimeout(persist, 0);
}
WebInspector.Resource.Events = {
RevisionAdded: "revision-added",
MessageAdded: "message-added",
MessagesCleared: "messages-cleared",
}
WebInspector.Resource.prototype = {
get request()
{
return this._request;
},
get url()
{
return this._url;
},
set url(x)
{
this._url = x;
this._parsedURL = new WebInspector.ParsedURL(x);
},
get parsedURL()
{
return this._parsedURL;
},
get documentURL()
{
return this._documentURL;
},
get frameId()
{
return this._frameId;
},
get loaderId()
{
return this._loaderId;
},
get displayName()
{
return this._parsedURL.displayName;
},
get type()
{
return this._request ? this._request.type : this._type;
},
get mimeType()
{
return this._request ? this._request.mimeType : this._mimeType;
},
get messages()
{
return this._messages || [];
},
addMessage: function(msg)
{
if (!msg.isErrorOrWarning() || !msg.message)
return;
if (!this._messages)
this._messages = [];
this._messages.push(msg);
this.dispatchEventToListeners(WebInspector.Resource.Events.MessageAdded, msg);
},
get errors()
{
return this._errors || 0;
},
set errors(x)
{
this._errors = x;
},
get warnings()
{
return this._warnings || 0;
},
set warnings(x)
{
this._warnings = x;
},
clearErrorsAndWarnings: function()
{
this._messages = [];
this._warnings = 0;
this._errors = 0;
this.dispatchEventToListeners(WebInspector.Resource.Events.MessagesCleared);
},
get content()
{
return this._content;
},
get contentEncoded()
{
return this._contentEncoded;
},
get contentTimestamp()
{
return this._contentTimestamp;
},
isEditable: function()
{
if (this._actualResource)
return false;
var binding = WebInspector.Resource._domainModelBindings[this.type.name()];
return binding && binding.canSetContent(this);
},
setContent: function(newContent, majorChange, callback)
{
if (!this.isEditable()) {
if (callback)
callback("Resource is not editable");
return;
}
var binding = WebInspector.Resource._domainModelBindings[this.type.name()];
binding.setContent(this, newContent, majorChange, callback);
},
addRevision: function(newContent, timestamp, restoringHistory)
{
var revision = new WebInspector.ResourceRevision(this, this._content, this._contentTimestamp);
this.history.push(revision);
this._content = newContent;
this._contentTimestamp = timestamp || new Date();
this.dispatchEventToListeners(WebInspector.Resource.Events.RevisionAdded, revision);
if (!restoringHistory)
this._persistRevision();
WebInspector.resourceTreeModel.dispatchEventToListeners(WebInspector.ResourceTreeModel.EventTypes.ResourceContentCommitted, { resource: this, content: newContent });
},
_persistRevision: function()
{
WebInspector.Resource.persistRevision(this);
},
contentURL: function()
{
return this._url;
},
requestContent: function(callback)
{
if (typeof this._content !== "undefined") {
callback(this._content, !!this._contentEncoded, this.canonicalMimeType());
return;
}
this._pendingContentCallbacks.push(callback);
this._innerRequestContent();
},
canonicalMimeType: function()
{
if (this.type === WebInspector.resourceTypes.Document)
return "text/html";
if (this.type === WebInspector.resourceTypes.Script)
return "text/javascript";
if (this.type === WebInspector.resourceTypes.Stylesheet)
return "text/css";
return this.mimeType;
},
searchInContent: function(query, caseSensitive, isRegex, callback)
{
function callbackWrapper(error, searchMatches)
{
callback(searchMatches || []);
}
if (this.frameId)
PageAgent.searchInResource(this.frameId, this.url, query, caseSensitive, isRegex, callbackWrapper);
else
callback([]);
},
populateImageSource: function(image)
{
function onResourceContent()
{
image.src = this._contentURL();
}
this.requestContent(onResourceContent.bind(this));
},
_contentURL: function()
{
const maxDataUrlSize = 1024 * 1024;
if (this._content == null || this._content.length > maxDataUrlSize)
return this.url;
return "data:" + this.mimeType + (this._contentEncoded ? ";base64," : ",") + this._content;
},
_innerRequestContent: function()
{
if (this._contentRequested)
return;
this._contentRequested = true;
function callback(error, content, contentEncoded)
{
this._content = error ? null : content;
this._contentEncoded = contentEncoded;
this._originalContent = content;
var callbacks = this._pendingContentCallbacks.slice();
for (var i = 0; i < callbacks.length; ++i)
callbacks[i](this._content, this._contentEncoded, this.canonicalMimeType());
this._pendingContentCallbacks.length = 0;
delete this._contentRequested;
}
PageAgent.getResourceContent(this.frameId, this.url, callback.bind(this));
}
}
WebInspector.Resource.prototype.__proto__ = WebInspector.Object.prototype;
WebInspector.ResourceRevision = function(resource, content, timestamp)
{
this._resource = resource;
this._content = content;
this._timestamp = timestamp;
}
WebInspector.ResourceRevision.prototype = {
get resource()
{
return this._resource;
},
get timestamp()
{
return this._timestamp;
},
get content()
{
return this._content || null;
},
revertToThis: function()
{
function revert(content)
{
this._resource.setContent(content, true);
}
this.requestContent(revert.bind(this));
},
contentURL: function()
{
return this._resource.url;
},
requestContent: function(callback)
{
if (typeof this._content === "string") {
callback(this._content, false, this.resource.mimeType);
return;
}
if (typeof this.resource._originalContent === "string") {
this._content = this._resource._originalContent;
callback(this._content, false, this.resource.mimeType);
return;
}
function callbackWrapper(error, content, contentEncoded)
{
callback(error ? null : content, contentEncoded, this.resource.mimeType);
}
PageAgent.getResourceContent(this._resource.frameId, this._resource.url, callbackWrapper.bind(this));
},
searchInContent: function(query, caseSensitive, isRegex, callback)
{
callback([]);
}
}
WebInspector.ResourceDomainModelBinding = function() { }
WebInspector.ResourceDomainModelBinding.prototype = {
canSetContent: function(resource) { return true; },
setContent: function(resource, content, majorChange, callback) { }
}