home *** CD-ROM | disk | FTP | other *** search
/ Computer Active 2010 July / CA07.iso / Multimedija / QuickTimeInstaller.exe / AppleApplicationSupport.msi / WebKit.resources_inspector_Resource.js < prev    next >
Encoding:
C/C++ Source or Header  |  2010-03-15  |  15.9 KB  |  626 lines

  1. /*
  2.  * Copyright (C) 2007, 2008 Apple Inc.  All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  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. 
  16.  *
  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.
  27.  */
  28.  
  29. WebInspector.Resource = function(requestHeaders, url, domain, path, lastPathComponent, identifier, mainResource, cached)
  30. {
  31.     this.identifier = identifier;
  32.  
  33.     this.startTime = -1;
  34.     this.endTime = -1;
  35.     this.mainResource = mainResource;
  36.     this.requestHeaders = requestHeaders;
  37.     this.url = url;
  38.     this.domain = domain;
  39.     this.path = path;
  40.     this.lastPathComponent = lastPathComponent;
  41.     this.cached = cached;
  42.  
  43.     this.category = WebInspector.resourceCategories.other;
  44. }
  45.  
  46. // Keep these in sync with WebCore::InspectorResource::Type
  47. WebInspector.Resource.Type = {
  48.     Document:   0,
  49.     Stylesheet: 1,
  50.     Image:      2,
  51.     Font:       3,
  52.     Script:     4,
  53.     XHR:        5,
  54.     Other:      6,
  55.  
  56.     isTextType: function(type)
  57.     {
  58.         return (type === this.Document) || (type === this.Stylesheet) || (type === this.Script) || (type === this.XHR);
  59.     },
  60.  
  61.     toString: function(type)
  62.     {
  63.         switch (type) {
  64.             case this.Document:
  65.                 return WebInspector.UIString("document");
  66.             case this.Stylesheet:
  67.                 return WebInspector.UIString("stylesheet");
  68.             case this.Image:
  69.                 return WebInspector.UIString("image");
  70.             case this.Font:
  71.                 return WebInspector.UIString("font");
  72.             case this.Script:
  73.                 return WebInspector.UIString("script");
  74.             case this.XHR:
  75.                 return WebInspector.UIString("XHR");
  76.             case this.Other:
  77.             default:
  78.                 return WebInspector.UIString("other");
  79.         }
  80.     }
  81. }
  82.  
  83. WebInspector.Resource.prototype = {
  84.     get url()
  85.     {
  86.         return this._url;
  87.     },
  88.  
  89.     set url(x)
  90.     {
  91.         if (this._url === x)
  92.             return;
  93.  
  94.         var oldURL = this._url;
  95.         this._url = x;
  96.  
  97.         // FIXME: We should make the WebInspector object listen for the "url changed" event.
  98.         // Then resourceURLChanged can be removed.
  99.         WebInspector.resourceURLChanged(this, oldURL);
  100.  
  101.         this.dispatchEventToListeners("url changed");
  102.     },
  103.  
  104.     get domain()
  105.     {
  106.         return this._domain;
  107.     },
  108.  
  109.     set domain(x)
  110.     {
  111.         if (this._domain === x)
  112.             return;
  113.         this._domain = x;
  114.     },
  115.  
  116.     get lastPathComponent()
  117.     {
  118.         return this._lastPathComponent;
  119.     },
  120.  
  121.     set lastPathComponent(x)
  122.     {
  123.         if (this._lastPathComponent === x)
  124.             return;
  125.         this._lastPathComponent = x;
  126.         this._lastPathComponentLowerCase = x ? x.toLowerCase() : null;
  127.     },
  128.  
  129.     get displayName()
  130.     {
  131.         var title = this.lastPathComponent;
  132.         if (!title)
  133.             title = this.displayDomain;
  134.         if (!title && this.url)
  135.             title = this.url.trimURL(WebInspector.mainResource ? WebInspector.mainResource.domain : "");
  136.         if (title === "/")
  137.             title = this.url;
  138.         return title;
  139.     },
  140.  
  141.     get displayDomain()
  142.     {
  143.         // WebInspector.Database calls this, so don't access more than this.domain.
  144.         if (this.domain && (!WebInspector.mainResource || (WebInspector.mainResource && this.domain !== WebInspector.mainResource.domain)))
  145.             return this.domain;
  146.         return "";
  147.     },
  148.  
  149.     get startTime()
  150.     {
  151.         return this._startTime || -1;
  152.     },
  153.  
  154.     set startTime(x)
  155.     {
  156.         if (this._startTime === x)
  157.             return;
  158.  
  159.         this._startTime = x;
  160.  
  161.         if (WebInspector.panels.resources)
  162.             WebInspector.panels.resources.refreshResource(this);
  163.     },
  164.  
  165.     get responseReceivedTime()
  166.     {
  167.         return this._responseReceivedTime || -1;
  168.     },
  169.  
  170.     set responseReceivedTime(x)
  171.     {
  172.         if (this._responseReceivedTime === x)
  173.             return;
  174.  
  175.         this._responseReceivedTime = x;
  176.  
  177.         if (WebInspector.panels.resources)
  178.             WebInspector.panels.resources.refreshResource(this);
  179.     },
  180.  
  181.     get endTime()
  182.     {
  183.         return this._endTime || -1;
  184.     },
  185.  
  186.     set endTime(x)
  187.     {
  188.         if (this._endTime === x)
  189.             return;
  190.  
  191.         this._endTime = x;
  192.  
  193.         if (WebInspector.panels.resources)
  194.             WebInspector.panels.resources.refreshResource(this);
  195.     },
  196.  
  197.     get duration()
  198.     {
  199.         if (this._endTime === -1 || this._startTime === -1)
  200.             return -1;
  201.         return this._endTime - this._startTime;
  202.     },
  203.  
  204.     get latency()
  205.     {
  206.         if (this._responseReceivedTime === -1 || this._startTime === -1)
  207.             return -1;
  208.         return this._responseReceivedTime - this._startTime;
  209.     },
  210.  
  211.     get contentLength()
  212.     {
  213.         return this._contentLength || 0;
  214.     },
  215.  
  216.     set contentLength(x)
  217.     {
  218.         if (this._contentLength === x)
  219.             return;
  220.  
  221.         this._contentLength = x;
  222.  
  223.         if (WebInspector.panels.resources)
  224.             WebInspector.panels.resources.refreshResource(this);
  225.     },
  226.  
  227.     get expectedContentLength()
  228.     {
  229.         return this._expectedContentLength || 0;
  230.     },
  231.  
  232.     set expectedContentLength(x)
  233.     {
  234.         if (this._expectedContentLength === x)
  235.             return;
  236.         this._expectedContentLength = x;
  237.     },
  238.  
  239.     get finished()
  240.     {
  241.         return this._finished;
  242.     },
  243.  
  244.     set finished(x)
  245.     {
  246.         if (this._finished === x)
  247.             return;
  248.  
  249.         this._finished = x;
  250.  
  251.         if (x) {
  252.             this._checkTips();
  253.             this._checkWarnings();
  254.             this.dispatchEventToListeners("finished");
  255.         }
  256.     },
  257.  
  258.     get failed()
  259.     {
  260.         return this._failed;
  261.     },
  262.  
  263.     set failed(x)
  264.     {
  265.         this._failed = x;
  266.     },
  267.  
  268.     get category()
  269.     {
  270.         return this._category;
  271.     },
  272.  
  273.     set category(x)
  274.     {
  275.         if (this._category === x)
  276.             return;
  277.  
  278.         var oldCategory = this._category;
  279.         if (oldCategory)
  280.             oldCategory.removeResource(this);
  281.  
  282.         this._category = x;
  283.  
  284.         if (this._category)
  285.             this._category.addResource(this);
  286.  
  287.         if (WebInspector.panels.resources) {
  288.             WebInspector.panels.resources.refreshResource(this);
  289.             WebInspector.panels.resources.recreateViewForResourceIfNeeded(this);
  290.         }
  291.     },
  292.  
  293.     get mimeType()
  294.     {
  295.         return this._mimeType;
  296.     },
  297.  
  298.     set mimeType(x)
  299.     {
  300.         if (this._mimeType === x)
  301.             return;
  302.  
  303.         this._mimeType = x;
  304.     },
  305.  
  306.     get type()
  307.     {
  308.         return this._type;
  309.     },
  310.  
  311.     set type(x)
  312.     {
  313.         if (this._type === x)
  314.             return;
  315.  
  316.         this._type = x;
  317.  
  318.         switch (x) {
  319.             case WebInspector.Resource.Type.Document:
  320.                 this.category = WebInspector.resourceCategories.documents;
  321.                 break;
  322.             case WebInspector.Resource.Type.Stylesheet:
  323.                 this.category = WebInspector.resourceCategories.stylesheets;
  324.                 break;
  325.             case WebInspector.Resource.Type.Script:
  326.                 this.category = WebInspector.resourceCategories.scripts;
  327.                 break;
  328.             case WebInspector.Resource.Type.Image:
  329.                 this.category = WebInspector.resourceCategories.images;
  330.                 break;
  331.             case WebInspector.Resource.Type.Font:
  332.                 this.category = WebInspector.resourceCategories.fonts;
  333.                 break;
  334.             case WebInspector.Resource.Type.XHR:
  335.                 this.category = WebInspector.resourceCategories.xhr;
  336.                 break;
  337.             case WebInspector.Resource.Type.Other:
  338.             default:
  339.                 this.category = WebInspector.resourceCategories.other;
  340.                 break;
  341.         }
  342.     },
  343.  
  344.     get documentNode() {
  345.         if ("identifier" in this)
  346.             return InspectorController.getResourceDocumentNode(this.identifier);
  347.         return null;
  348.     },
  349.  
  350.     get requestHeaders()
  351.     {
  352.         if (this._requestHeaders === undefined)
  353.             this._requestHeaders = {};
  354.         return this._requestHeaders;
  355.     },
  356.  
  357.     set requestHeaders(x)
  358.     {
  359.         if (this._requestHeaders === x)
  360.             return;
  361.  
  362.         this._requestHeaders = x;
  363.         delete this._sortedRequestHeaders;
  364.  
  365.         this.dispatchEventToListeners("requestHeaders changed");
  366.     },
  367.  
  368.     get sortedRequestHeaders()
  369.     {
  370.         if (this._sortedRequestHeaders !== undefined)
  371.             return this._sortedRequestHeaders;
  372.  
  373.         this._sortedRequestHeaders = [];
  374.         for (var key in this.requestHeaders)
  375.             this._sortedRequestHeaders.push({header: key, value: this.requestHeaders[key]});
  376.         this._sortedRequestHeaders.sort(function(a,b) { return a.header.localeCompare(b.header) });
  377.  
  378.         return this._sortedRequestHeaders;
  379.     },
  380.  
  381.     get responseHeaders()
  382.     {
  383.         if (this._responseHeaders === undefined)
  384.             this._responseHeaders = {};
  385.         return this._responseHeaders;
  386.     },
  387.  
  388.     set responseHeaders(x)
  389.     {
  390.         if (this._responseHeaders === x)
  391.             return;
  392.  
  393.         this._responseHeaders = x;
  394.         delete this._sortedResponseHeaders;
  395.  
  396.         this.dispatchEventToListeners("responseHeaders changed");
  397.     },
  398.  
  399.     get sortedResponseHeaders()
  400.     {
  401.         if (this._sortedResponseHeaders !== undefined)
  402.             return this._sortedResponseHeaders;
  403.  
  404.         this._sortedResponseHeaders = [];
  405.         for (var key in this.responseHeaders)
  406.             this._sortedResponseHeaders.push({header: key, value: this.responseHeaders[key]});
  407.         this._sortedResponseHeaders.sort(function(a,b) { return a.header.localeCompare(b.header) });
  408.  
  409.         return this._sortedResponseHeaders;
  410.     },
  411.  
  412.     get scripts()
  413.     {
  414.         if (!("_scripts" in this))
  415.             this._scripts = [];
  416.         return this._scripts;
  417.     },
  418.  
  419.     addScript: function(script)
  420.     {
  421.         if (!script)
  422.             return;
  423.         this.scripts.unshift(script);
  424.         script.resource = this;
  425.     },
  426.  
  427.     removeAllScripts: function()
  428.     {
  429.         if (!this._scripts)
  430.             return;
  431.  
  432.         for (var i = 0; i < this._scripts.length; ++i) {
  433.             if (this._scripts[i].resource === this)
  434.                 delete this._scripts[i].resource;
  435.         }
  436.  
  437.         delete this._scripts;
  438.     },
  439.  
  440.     removeScript: function(script)
  441.     {
  442.         if (!script)
  443.             return;
  444.  
  445.         if (script.resource === this)
  446.             delete script.resource;
  447.  
  448.         if (!this._scripts)
  449.             return;
  450.  
  451.         this._scripts.remove(script);
  452.     },
  453.  
  454.     get errors()
  455.     {
  456.         return this._errors || 0;
  457.     },
  458.  
  459.     set errors(x)
  460.     {
  461.         this._errors = x;
  462.     },
  463.  
  464.     get warnings()
  465.     {
  466.         return this._warnings || 0;
  467.     },
  468.  
  469.     set warnings(x)
  470.     {
  471.         this._warnings = x;
  472.     },
  473.  
  474.     get tips()
  475.     {
  476.         if (!("_tips" in this))
  477.             this._tips = {};
  478.         return this._tips;
  479.     },
  480.  
  481.     _addTip: function(tip)
  482.     {
  483.         if (tip.id in this.tips)
  484.             return;
  485.  
  486.         this.tips[tip.id] = tip;
  487.  
  488.         // FIXME: Re-enable this code once we have a scope bar in the Console.
  489.         // Otherwise, we flood the Console with too many tips.
  490.         /*
  491.         var msg = new WebInspector.ConsoleMessage(WebInspector.ConsoleMessage.MessageSource.Other,
  492.             WebInspector.ConsoleMessage.MessageLevel.Tip, -1, this.url, null, 1, tip.message);
  493.         WebInspector.console.addMessage(msg);
  494.         */
  495.     },
  496.  
  497.     _checkTips: function()
  498.     {
  499.         for (var tip in WebInspector.Tips)
  500.             this._checkTip(WebInspector.Tips[tip]);
  501.     },
  502.  
  503.     _checkTip: function(tip)
  504.     {
  505.         var addTip = false;
  506.         switch (tip.id) {
  507.             case WebInspector.Tips.ResourceNotCompressed.id:
  508.                 addTip = this._shouldCompress();
  509.                 break;
  510.         }
  511.  
  512.         if (addTip)
  513.             this._addTip(tip);
  514.     },
  515.  
  516.     _shouldCompress: function()
  517.     {
  518.         return WebInspector.Resource.Type.isTextType(this.type)
  519.             && this.domain
  520.             && !("Content-Encoding" in this.responseHeaders)
  521.             && this.contentLength !== undefined
  522.             && this.contentLength >= 512;
  523.     },
  524.  
  525.     _mimeTypeIsConsistentWithType: function()
  526.     {
  527.         if (typeof this.type === "undefined"
  528.          || this.type === WebInspector.Resource.Type.Other
  529.          || this.type === WebInspector.Resource.Type.XHR)
  530.             return true;
  531.  
  532.         if (this.mimeType in WebInspector.MIMETypes)
  533.             return this.type in WebInspector.MIMETypes[this.mimeType];
  534.  
  535.         return true;
  536.     },
  537.  
  538.     _checkWarnings: function()
  539.     {
  540.         for (var warning in WebInspector.Warnings)
  541.             this._checkWarning(WebInspector.Warnings[warning]);
  542.     },
  543.  
  544.     _checkWarning: function(warning)
  545.     {
  546.         var addWarning = false;
  547.         var msg;
  548.         switch (warning.id) {
  549.             case WebInspector.Warnings.IncorrectMIMEType.id:
  550.                 if (!this._mimeTypeIsConsistentWithType())
  551.                     msg = new WebInspector.ConsoleMessage(WebInspector.ConsoleMessage.MessageSource.Other,
  552.                         WebInspector.ConsoleMessage.MessageLevel.Warning, -1, this.url, null, 1,
  553.                         String.sprintf(WebInspector.Warnings.IncorrectMIMEType.message,
  554.                         WebInspector.Resource.Type.toString(this.type), this.mimeType));
  555.                 break;
  556.         }
  557.  
  558.         if (msg)
  559.             WebInspector.console.addMessage(msg);
  560.     }
  561. }
  562.  
  563. WebInspector.Resource.prototype.__proto__ = WebInspector.Object.prototype;
  564.  
  565. WebInspector.Resource.CompareByStartTime = function(a, b)
  566. {
  567.     if (a.startTime < b.startTime)
  568.         return -1;
  569.     if (a.startTime > b.startTime)
  570.         return 1;
  571.     return 0;
  572. }
  573.  
  574. WebInspector.Resource.CompareByResponseReceivedTime = function(a, b)
  575. {
  576.     if (a.responseReceivedTime === -1 && b.responseReceivedTime !== -1)
  577.         return 1;
  578.     if (a.responseReceivedTime !== -1 && b.responseReceivedTime === -1)
  579.         return -1;
  580.     if (a.responseReceivedTime < b.responseReceivedTime)
  581.         return -1;
  582.     if (a.responseReceivedTime > b.responseReceivedTime)
  583.         return 1;
  584.     return 0;
  585. }
  586.  
  587. WebInspector.Resource.CompareByEndTime = function(a, b)
  588. {
  589.     if (a.endTime === -1 && b.endTime !== -1)
  590.         return 1;
  591.     if (a.endTime !== -1 && b.endTime === -1)
  592.         return -1;
  593.     if (a.endTime < b.endTime)
  594.         return -1;
  595.     if (a.endTime > b.endTime)
  596.         return 1;
  597.     return 0;
  598. }
  599.  
  600. WebInspector.Resource.CompareByDuration = function(a, b)
  601. {
  602.     if (a.duration < b.duration)
  603.         return -1;
  604.     if (a.duration > b.duration)
  605.         return 1;
  606.     return 0;
  607. }
  608.  
  609. WebInspector.Resource.CompareByLatency = function(a, b)
  610. {
  611.     if (a.latency < b.latency)
  612.         return -1;
  613.     if (a.latency > b.latency)
  614.         return 1;
  615.     return 0;
  616. }
  617.  
  618. WebInspector.Resource.CompareBySize = function(a, b)
  619. {
  620.     if (a.contentLength < b.contentLength)
  621.         return -1;
  622.     if (a.contentLength > b.contentLength)
  623.         return 1;
  624.     return 0;
  625. }
  626.