2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * @extends {WebInspector.Object}
32 * @implements {WebInspector.ContentProvider}
33 * @param {?WebInspector.NetworkRequest} request
35 * @param {string} documentURL
36 * @param {!PageAgent.FrameId} frameId
37 * @param {!NetworkAgent.LoaderId} loaderId
38 * @param {!WebInspector.ResourceType} type
39 * @param {string} mimeType
40 * @param {boolean=} isHidden
42 WebInspector.Resource = function(request, url, documentURL, frameId, loaderId, type, mimeType, isHidden)
44 this._request = request;
46 this._documentURL = documentURL;
47 this._frameId = frameId;
48 this._loaderId = loaderId;
49 this._type = type || WebInspector.resourceTypes.Other;
50 this._mimeType = mimeType;
51 this._isHidden = isHidden;
53 /** @type {?string} */ this._content;
54 /** @type {boolean} */ this._contentEncoded;
55 this._pendingContentCallbacks = [];
56 if (this._request && !this._request.finished)
57 this._request.addEventListener(WebInspector.NetworkRequest.Events.FinishedLoading, this._requestFinished, this);
60 WebInspector.Resource.Events = {
61 MessageAdded: "message-added",
62 MessagesCleared: "messages-cleared",
65 WebInspector.Resource.prototype = {
67 * @return {?WebInspector.NetworkRequest}
85 this._parsedURL = new WebInspector.ParsedURL(x);
90 return this._parsedURL;
98 return this._documentURL;
102 * @return {!PageAgent.FrameId}
106 return this._frameId;
110 * @return {!NetworkAgent.LoaderId}
114 return this._loaderId;
122 return this._parsedURL.displayName;
126 * @return {!WebInspector.ResourceType}
130 return this._request ? this._request.type : this._type;
138 return this._request ? this._request.mimeType : this._mimeType;
142 * @return {!Array.<!WebInspector.ConsoleMessage>}
146 return this._messages || [];
150 * @param {!WebInspector.ConsoleMessage} msg
152 addMessage: function(msg)
154 if (!msg.isErrorOrWarning() || !msg.messageText)
159 this._messages.push(msg);
160 this.dispatchEventToListeners(WebInspector.Resource.Events.MessageAdded, msg);
168 return this._errors || 0;
181 return this._warnings || 0;
189 clearErrorsAndWarnings: function()
194 this.dispatchEventToListeners(WebInspector.Resource.Events.MessagesCleared);
202 return this._content;
210 return this._contentEncoded;
216 contentURL: function()
222 * @return {!WebInspector.ResourceType}
224 contentType: function()
230 * @param {function(?string)} callback
232 requestContent: function(callback)
234 if (typeof this._content !== "undefined") {
235 callback(this._content);
239 this._pendingContentCallbacks.push(callback);
240 if (!this._request || this._request.finished)
241 this._innerRequestContent();
247 canonicalMimeType: function()
249 return this.type.canonicalMimeType() || this.mimeType;
253 * @param {string} query
254 * @param {boolean} caseSensitive
255 * @param {boolean} isRegex
256 * @param {function(!Array.<!WebInspector.ContentProvider.SearchMatch>)} callback
258 searchInContent: function(query, caseSensitive, isRegex, callback)
261 * @param {?Protocol.Error} error
262 * @param {!Array.<!PageAgent.SearchMatch>} searchMatches
264 function callbackWrapper(error, searchMatches)
266 callback(searchMatches || []);
269 if (this.type === WebInspector.resourceTypes.Document) {
275 PageAgent.searchInResource(this.frameId, this.url, query, caseSensitive, isRegex, callbackWrapper);
281 * @param {!Element} image
283 populateImageSource: function(image)
286 * @param {?string} content
287 * @this {WebInspector.Resource}
289 function onResourceContent(content)
291 var imageSrc = WebInspector.contentAsDataURL(this._content, this.mimeType, this._contentEncoded);
292 if (imageSrc === null)
294 image.src = imageSrc;
297 this.requestContent(onResourceContent.bind(this));
300 _requestFinished: function()
302 this._request.removeEventListener(WebInspector.NetworkRequest.Events.FinishedLoading, this._requestFinished, this);
303 if (this._pendingContentCallbacks.length)
304 this._innerRequestContent();
308 _innerRequestContent: function()
310 if (this._contentRequested)
312 this._contentRequested = true;
315 * @param {?Protocol.Error} error
316 * @param {?string} content
317 * @param {boolean} contentEncoded
318 * @this {WebInspector.Resource}
320 function contentLoaded(error, content, contentEncoded)
322 if (error || content === null) {
323 replyWithContent.call(this, null, false);
326 replyWithContent.call(this, content, contentEncoded);
330 * @param {?string} content
331 * @param {boolean} contentEncoded
332 * @this {WebInspector.Resource}
334 function replyWithContent(content, contentEncoded)
336 this._content = content;
337 this._contentEncoded = contentEncoded;
338 var callbacks = this._pendingContentCallbacks.slice();
339 for (var i = 0; i < callbacks.length; ++i)
340 callbacks[i](this._content);
341 this._pendingContentCallbacks.length = 0;
342 delete this._contentRequested;
346 * @param {?Protocol.Error} error
347 * @param {string} content
348 * @param {boolean} contentEncoded
349 * @this {WebInspector.Resource}
351 function resourceContentLoaded(error, content, contentEncoded)
353 contentLoaded.call(this, error, content, contentEncoded);
357 this.request.requestContent(requestContentLoaded.bind(this));
362 * @param {?string} content
363 * @this {WebInspector.Resource}
365 function requestContentLoaded(content)
367 contentLoaded.call(this, null, content, this.request.contentEncoded);
370 PageAgent.getResourceContent(this.frameId, this.url, resourceContentLoaded.bind(this));
378 return !!this._isHidden;
381 __proto__: WebInspector.Object.prototype