WebInspector.Resource = function(request, url, documentURL, frameId, loaderId, type, mimeType, isHidden)
{
this._request = request;
this.url = url;
this._documentURL = documentURL;
this._frameId = frameId;
this._loaderId = loaderId;
this._type = type || WebInspector.resourceTypes.Other;
this._mimeType = mimeType;
this._isHidden = isHidden;
this._content;
this._contentEncoded;
this._pendingContentCallbacks = [];
if (this._request && !this._request.finished)
this._request.addEventListener(WebInspector.NetworkRequest.Events.FinishedLoading, this._requestFinished, this);
}
WebInspector.Resource.Events = {
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;
},
contentURL: function()
{
return this._url;
},
contentType: function()
{
return this.type;
},
requestContent: function(callback)
{
if (typeof this._content !== "undefined") {
callback(this._content, !!this._contentEncoded, this.canonicalMimeType());
return;
}
this._pendingContentCallbacks.push(callback);
if (!this._request || this._request.finished)
this._innerRequestContent();
},
canonicalMimeType: function()
{
return this.type.canonicalMimeType() || 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()
{
var imageSrc = WebInspector.contentAsDataURL(this._content, this.mimeType, this._contentEncoded);
if (imageSrc === null)
imageSrc = this.url;
image.src = imageSrc;
}
this.requestContent(onResourceContent.bind(this));
},
_requestFinished: function()
{
this._request.removeEventListener(WebInspector.NetworkRequest.Events.FinishedLoading, this._requestFinished, this);
if (this._pendingContentCallbacks.length)
this._innerRequestContent();
},
_innerRequestContent: function()
{
if (this._contentRequested)
return;
this._contentRequested = true;
function contentLoaded(content, contentEncoded)
{
this._content = content;
this._contentEncoded = contentEncoded;
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;
}
function resourceContentLoaded(error, content, contentEncoded)
{
if (error)
console.error("Resource content request failed: " + error);
contentLoaded.call(this, error ? null : content, contentEncoded);
}
if (this.request) {
function requestContentLoaded(content, contentEncoded, mimeType)
{
contentLoaded.call(this, content, contentEncoded);
}
this.request.requestContent(requestContentLoaded.bind(this));
return;
}
PageAgent.getResourceContent(this.frameId, this.url, resourceContentLoaded.bind(this));
},
isHidden: function()
{
return !!this._isHidden;
},
__proto__: WebInspector.Object.prototype
}