123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426 |
- /*
- * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
- * its contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- /**
- * @constructor
- * @extends {WebInspector.Object}
- * @implements {WebInspector.ContentProvider}
- * @param {?WebInspector.NetworkRequest} request
- * @param {string} url
- * @param {string} documentURL
- * @param {PageAgent.FrameId} frameId
- * @param {NetworkAgent.LoaderId} loaderId
- * @param {WebInspector.ResourceType} type
- * @param {string} mimeType
- * @param {boolean=} isHidden
- */
- 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;
- /** @type {?string} */ this._content;
- /** @type {boolean} */ 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 = {
- /**
- * @return {?WebInspector.NetworkRequest}
- */
- get request()
- {
- return this._request;
- },
- /**
- * @return {string}
- */
- get url()
- {
- return this._url;
- },
- set url(x)
- {
- this._url = x;
- this._parsedURL = new WebInspector.ParsedURL(x);
- },
- get parsedURL()
- {
- return this._parsedURL;
- },
- /**
- * @return {string}
- */
- get documentURL()
- {
- return this._documentURL;
- },
- /**
- * @return {PageAgent.FrameId}
- */
- get frameId()
- {
- return this._frameId;
- },
- /**
- * @return {NetworkAgent.LoaderId}
- */
- get loaderId()
- {
- return this._loaderId;
- },
- /**
- * @return {string}
- */
- get displayName()
- {
- return this._parsedURL.displayName;
- },
- /**
- * @return {WebInspector.ResourceType}
- */
- get type()
- {
- return this._request ? this._request.type : this._type;
- },
- /**
- * @return {string}
- */
- get mimeType()
- {
- return this._request ? this._request.mimeType : this._mimeType;
- },
- /**
- * @return {Array.<WebInspector.ConsoleMessage>}
- */
- get messages()
- {
- return this._messages || [];
- },
- /**
- * @param {WebInspector.ConsoleMessage} msg
- */
- 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);
- },
- /**
- * @return {number}
- */
- get errors()
- {
- return this._errors || 0;
- },
- set errors(x)
- {
- this._errors = x;
- },
- /**
- * @return {number}
- */
- 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);
- },
- /**
- * @return {?string}
- */
- get content()
- {
- return this._content;
- },
- /**
- * @return {boolean}
- */
- get contentEncoded()
- {
- return this._contentEncoded;
- },
- /**
- * @return {string}
- */
- contentURL: function()
- {
- return this._url;
- },
- /**
- * @return {WebInspector.ResourceType}
- */
- contentType: function()
- {
- return this.type;
- },
- /**
- * @param {function(?string, boolean, string)} callback
- */
- 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;
- },
- /**
- * @param {string} query
- * @param {boolean} caseSensitive
- * @param {boolean} isRegex
- * @param {function(Array.<WebInspector.ContentProvider.SearchMatch>)} callback
- */
- searchInContent: function(query, caseSensitive, isRegex, callback)
- {
- /**
- * @param {?Protocol.Error} error
- * @param {Array.<PageAgent.SearchMatch>} searchMatches
- */
- function callbackWrapper(error, searchMatches)
- {
- callback(searchMatches || []);
- }
- if (this.type === WebInspector.resourceTypes.Document) {
- /**
- * @param {?string} content
- * @param {boolean} contentEncoded
- * @param {string} mimeType
- */
- function documentContentLoaded(content, contentEncoded, mimeType)
- {
- if (content === null) {
- callback([]);
- return;
- }
- var result = WebInspector.ContentProvider.performSearchInContent(content, query, caseSensitive, isRegex);
- callback(result);
- }
- this.requestContent(documentContentLoaded);
- return;
- }
- if (this.frameId)
- PageAgent.searchInResource(this.frameId, this.url, query, caseSensitive, isRegex, callbackWrapper);
- else
- callback([]);
- },
- /**
- * @param {Element} image
- */
- 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;
- /**
- * @param {?Protocol.Error} error
- * @param {?string} content
- * @param {boolean} contentEncoded
- */
- function contentLoaded(error, content, contentEncoded)
- {
- if (error || content === null) {
- loadFallbackContent.call(this, error);
- return;
- }
- replyWithContent.call(this, content, contentEncoded);
- }
- /**
- * @param {?string} content
- * @param {boolean} contentEncoded
- */
- function replyWithContent(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;
- }
- /**
- * @param {?Protocol.Error} error
- * @param {string} content
- * @param {boolean} contentEncoded
- */
- function resourceContentLoaded(error, content, contentEncoded)
- {
- contentLoaded.call(this, error, content, contentEncoded);
- }
-
- /**
- * @param {?Protocol.Error} error
- */
- function loadFallbackContent(error)
- {
- var scripts = WebInspector.debuggerModel.scriptsForSourceURL(this.url);
- if (!scripts.length) {
- console.error("Resource content request failed: " + error);
- replyWithContent.call(this, null, false);
- return;
- }
- var contentProvider;
- if (this.type === WebInspector.resourceTypes.Document)
- contentProvider = new WebInspector.ConcatenatedScriptsContentProvider(scripts);
- else if (this.type === WebInspector.resourceTypes.Script)
- contentProvider = scripts[0];
- if (!contentProvider) {
- console.error("Resource content request failed: " + error);
- replyWithContent.call(this, null, false);
- return;
- }
- contentProvider.requestContent(fallbackContentLoaded.bind(this));
- }
- /**
- * @param {?string} content
- * @param {boolean} contentEncoded
- * @param {string} mimeType
- */
- function fallbackContentLoaded(content, contentEncoded, mimeType)
- {
- replyWithContent.call(this, content, contentEncoded);
- }
- if (this.request) {
- /**
- * @param {?string} content
- * @param {boolean} contentEncoded
- * @param {string} mimeType
- */
- function requestContentLoaded(content, contentEncoded, mimeType)
- {
- contentLoaded.call(this, null, content, contentEncoded);
- }
-
- this.request.requestContent(requestContentLoaded.bind(this));
- return;
- }
- PageAgent.getResourceContent(this.frameId, this.url, resourceContentLoaded.bind(this));
- },
- /**
- * @return {boolean}
- */
- isHidden: function()
- {
- return !!this._isHidden;
- },
- __proto__: WebInspector.Object.prototype
- }
|