home *** CD-ROM | disk | FTP | other *** search
/ Computer Active Guide 2009 July / CAG7.ISO / Internetas / SafariSetup.exe / AppleApplicationSupport.msi / WebKit.resources_inspector_Resource.js < prev    next >
Encoding:
C/C++ Source or Header  |  2010-06-03  |  16.1 KB  |  631 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(identifier, url)
  30. {
  31.     this.identifier = identifier;
  32.     this._url = url;
  33.     this._startTime = -1;
  34.     this._endTime = -1;
  35.     this._requestMethod = "";
  36.     this._requestFormData = "";
  37.     this._category = WebInspector.resourceCategories.other;
  38. }
  39.  
  40. WebInspector.Resource.StatusText = {
  41.     100: "Continue",
  42.     101: "Switching Protocols",
  43.     102: "Processing (WebDav)",
  44.     200: "OK",
  45.     201: "Created",
  46.     202: "Accepted",
  47.     203: "Non-Authoritative Information",
  48.     204: "No Content",
  49.     205: "Reset Content",
  50.     206: "Partial Content",
  51.     207: "Multi-Status (WebDav)",
  52.     300: "Multiple Choices",
  53.     301: "Moved Permanently",
  54.     302: "Found",
  55.     303: "See Other",
  56.     304: "Not Modified",
  57.     305: "Use Proxy",
  58.     306: "Switch Proxy",
  59.     307: "Temporary",
  60.     400: "Bad Request",
  61.     401: "Unauthorized",
  62.     402: "Payment Required",
  63.     403: "Forbidden",
  64.     404: "Not Found",
  65.     405: "Method Not Allowed",
  66.     406: "Not Acceptable",
  67.     407: "Proxy Authentication Required",
  68.     408: "Request Timeout",
  69.     409: "Conflict",
  70.     410: "Gone",
  71.     411: "Length Required",
  72.     412: "Precondition Failed",
  73.     413: "Request Entity Too Large",
  74.     414: "Request-URI Too Long",
  75.     415: "Unsupported Media Type",
  76.     416: "Requested Range Not Satisfiable",
  77.     417: "Expectation Failed",
  78.     418: "I'm a teapot",
  79.     422: "Unprocessable Entity (WebDav)",
  80.     423: "Locked (WebDav)",
  81.     424: "Failed Dependency (WebDav)",
  82.     425: "Unordered Collection",
  83.     426: "Upgrade Required",
  84.     449: "Retry With",
  85.     500: "Internal Server Error",
  86.     501: "Not Implemented",
  87.     502: "Bad Gateway",
  88.     503: "Service Unavailable",
  89.     504: "Gateway Timeout",
  90.     505: "HTTP Version Not Supported",
  91.     506: "Variant Also Negotiates",
  92.     507: "Insufficient Storage (WebDav)",
  93.     509: "Bandwidth Limit Exceeded",
  94.     510: "Not Extended"
  95. };
  96.  
  97. // Keep these in sync with WebCore::InspectorResource::Type
  98. WebInspector.Resource.Type = {
  99.     Document:   0,
  100.     Stylesheet: 1,
  101.     Image:      2,
  102.     Font:       3,
  103.     Script:     4,
  104.     XHR:        5,
  105.     Media:      6,
  106.     Other:      7,
  107.  
  108.     isTextType: function(type)
  109.     {
  110.         return (type === this.Document) || (type === this.Stylesheet) || (type === this.Script) || (type === this.XHR);
  111.     },
  112.  
  113.     toString: function(type)
  114.     {
  115.         switch (type) {
  116.             case this.Document:
  117.                 return WebInspector.UIString("document");
  118.             case this.Stylesheet:
  119.                 return WebInspector.UIString("stylesheet");
  120.             case this.Image:
  121.                 return WebInspector.UIString("image");
  122.             case this.Font:
  123.                 return WebInspector.UIString("font");
  124.             case this.Script:
  125.                 return WebInspector.UIString("script");
  126.             case this.XHR:
  127.                 return WebInspector.UIString("XHR");
  128.             case this.Other:
  129.             default:
  130.                 return WebInspector.UIString("other");
  131.         }
  132.     }
  133. }
  134.  
  135. WebInspector.Resource.prototype = {
  136.     get url()
  137.     {
  138.         return this._url;
  139.     },
  140.  
  141.     set url(x)
  142.     {
  143.         if (this._url === x)
  144.             return;
  145.  
  146.         var oldURL = this._url;
  147.         this._url = x;
  148.  
  149.         // FIXME: We should make the WebInspector object listen for the "url changed" event.
  150.         // Then resourceURLChanged can be removed.
  151.         WebInspector.resourceURLChanged(this, oldURL);
  152.  
  153.         this.dispatchEventToListeners("url changed");
  154.     },
  155.  
  156.     get documentURL()
  157.     {
  158.         return this._documentURL;
  159.     },
  160.  
  161.     set documentURL(x)
  162.     {
  163.         if (this._documentURL === x)
  164.             return;
  165.         this._documentURL = x;
  166.     },
  167.  
  168.     get domain()
  169.     {
  170.         return this._domain;
  171.     },
  172.  
  173.     set domain(x)
  174.     {
  175.         if (this._domain === x)
  176.             return;
  177.         this._domain = x;
  178.     },
  179.  
  180.     get lastPathComponent()
  181.     {
  182.         return this._lastPathComponent;
  183.     },
  184.  
  185.     set lastPathComponent(x)
  186.     {
  187.         if (this._lastPathComponent === x)
  188.             return;
  189.         this._lastPathComponent = x;
  190.         this._lastPathComponentLowerCase = x ? x.toLowerCase() : null;
  191.     },
  192.  
  193.     get displayName()
  194.     {
  195.         var title = this.lastPathComponent;
  196.         if (!title)
  197.             title = this.displayDomain;
  198.         if (!title && this.url)
  199.             title = this.url.trimURL(WebInspector.mainResource ? WebInspector.mainResource.domain : "");
  200.         if (title === "/")
  201.             title = this.url;
  202.         return title;
  203.     },
  204.  
  205.     get displayDomain()
  206.     {
  207.         // WebInspector.Database calls this, so don't access more than this.domain.
  208.         if (this.domain && (!WebInspector.mainResource || (WebInspector.mainResource && this.domain !== WebInspector.mainResource.domain)))
  209.             return this.domain;
  210.         return "";
  211.     },
  212.  
  213.     get startTime()
  214.     {
  215.         return this._startTime || -1;
  216.     },
  217.  
  218.     set startTime(x)
  219.     {
  220.         if (this._startTime === x)
  221.             return;
  222.  
  223.         this._startTime = x;
  224.  
  225.         if (WebInspector.panels.resources)
  226.             WebInspector.panels.resources.refreshResource(this);
  227.     },
  228.  
  229.     get responseReceivedTime()
  230.     {
  231.         return this._responseReceivedTime || -1;
  232.     },
  233.  
  234.     set responseReceivedTime(x)
  235.     {
  236.         if (this._responseReceivedTime === x)
  237.             return;
  238.  
  239.         this._responseReceivedTime = x;
  240.  
  241.         if (WebInspector.panels.resources)
  242.             WebInspector.panels.resources.refreshResource(this);
  243.     },
  244.  
  245.     get endTime()
  246.     {
  247.         return this._endTime || -1;
  248.     },
  249.  
  250.     set endTime(x)
  251.     {
  252.         if (this._endTime === x)
  253.             return;
  254.  
  255.         this._endTime = x;
  256.  
  257.         if (WebInspector.panels.resources)
  258.             WebInspector.panels.resources.refreshResource(this);
  259.     },
  260.  
  261.     get duration()
  262.     {
  263.         if (this._endTime === -1 || this._startTime === -1)
  264.             return -1;
  265.         return this._endTime - this._startTime;
  266.     },
  267.  
  268.     get latency()
  269.     {
  270.         if (this._responseReceivedTime === -1 || this._startTime === -1)
  271.             return -1;
  272.         return this._responseReceivedTime - this._startTime;
  273.     },
  274.  
  275.     get resourceSize()
  276.     {
  277.         return this._resourceSize || 0;
  278.     },
  279.  
  280.     set resourceSize(x)
  281.     {
  282.         if (this._resourceSize === x)
  283.             return;
  284.  
  285.         this._resourceSize = x;
  286.  
  287.         if (WebInspector.panels.resources)
  288.             WebInspector.panels.resources.refreshResource(this);
  289.     },
  290.  
  291.     get transferSize()
  292.     {
  293.         // FIXME: this is wrong for chunked-encoding resources.
  294.         return this.cached ? 0 : Number(this.responseHeaders["Content-Length"] || this.resourceSize || 0);
  295.     },
  296.  
  297.     get expectedContentLength()
  298.     {
  299.         return this._expectedContentLength || 0;
  300.     },
  301.  
  302.     set expectedContentLength(x)
  303.     {
  304.         if (this._expectedContentLength === x)
  305.             return;
  306.         this._expectedContentLength = x;
  307.     },
  308.  
  309.     get finished()
  310.     {
  311.         return this._finished;
  312.     },
  313.  
  314.     set finished(x)
  315.     {
  316.         if (this._finished === x)
  317.             return;
  318.  
  319.         this._finished = x;
  320.  
  321.         if (x) {
  322.             this._checkWarnings();
  323.             this.dispatchEventToListeners("finished");
  324.         }
  325.     },
  326.  
  327.     get failed()
  328.     {
  329.         return this._failed;
  330.     },
  331.  
  332.     set failed(x)
  333.     {
  334.         this._failed = x;
  335.     },
  336.  
  337.     get category()
  338.     {
  339.         return this._category;
  340.     },
  341.  
  342.     set category(x)
  343.     {
  344.         if (this._category === x)
  345.             return;
  346.  
  347.         var oldCategory = this._category;
  348.         if (oldCategory)
  349.             oldCategory.removeResource(this);
  350.  
  351.         this._category = x;
  352.  
  353.         if (this._category)
  354.             this._category.addResource(this);
  355.  
  356.         if (WebInspector.panels.resources) {
  357.             WebInspector.panels.resources.refreshResource(this);
  358.             WebInspector.panels.resources.recreateViewForResourceIfNeeded(this);
  359.         }
  360.     },
  361.  
  362.     get mimeType()
  363.     {
  364.         return this._mimeType;
  365.     },
  366.  
  367.     set mimeType(x)
  368.     {
  369.         if (this._mimeType === x)
  370.             return;
  371.  
  372.         this._mimeType = x;
  373.     },
  374.  
  375.     get type()
  376.     {
  377.         return this._type;
  378.     },
  379.  
  380.     set type(x)
  381.     {
  382.         if (this._type === x)
  383.             return;
  384.  
  385.         this._type = x;
  386.  
  387.         switch (x) {
  388.             case WebInspector.Resource.Type.Document:
  389.                 this.category = WebInspector.resourceCategories.documents;
  390.                 break;
  391.             case WebInspector.Resource.Type.Stylesheet:
  392.                 this.category = WebInspector.resourceCategories.stylesheets;
  393.                 break;
  394.             case WebInspector.Resource.Type.Script:
  395.                 this.category = WebInspector.resourceCategories.scripts;
  396.                 break;
  397.             case WebInspector.Resource.Type.Image:
  398.                 this.category = WebInspector.resourceCategories.images;
  399.                 break;
  400.             case WebInspector.Resource.Type.Font:
  401.                 this.category = WebInspector.resourceCategories.fonts;
  402.                 break;
  403.             case WebInspector.Resource.Type.XHR:
  404.                 this.category = WebInspector.resourceCategories.xhr;
  405.                 break;
  406.             case WebInspector.Resource.Type.Other:
  407.             default:
  408.                 this.category = WebInspector.resourceCategories.other;
  409.                 break;
  410.         }
  411.     },
  412.  
  413.     get requestHeaders()
  414.     {
  415.         if (this._requestHeaders === undefined)
  416.             this._requestHeaders = {};
  417.         return this._requestHeaders;
  418.     },
  419.  
  420.     set requestHeaders(x)
  421.     {
  422.         if (this._requestHeaders === x)
  423.             return;
  424.  
  425.         this._requestHeaders = x;
  426.         delete this._sortedRequestHeaders;
  427.  
  428.         this.dispatchEventToListeners("requestHeaders changed");
  429.     },
  430.  
  431.     get sortedRequestHeaders()
  432.     {
  433.         if (this._sortedRequestHeaders !== undefined)
  434.             return this._sortedRequestHeaders;
  435.  
  436.         this._sortedRequestHeaders = [];
  437.         for (var key in this.requestHeaders)
  438.             this._sortedRequestHeaders.push({header: key, value: this.requestHeaders[key]});
  439.         this._sortedRequestHeaders.sort(function(a,b) { return a.header.localeCompare(b.header) });
  440.  
  441.         return this._sortedRequestHeaders;
  442.     },
  443.  
  444.     get responseHeaders()
  445.     {
  446.         if (this._responseHeaders === undefined)
  447.             this._responseHeaders = {};
  448.         return this._responseHeaders;
  449.     },
  450.  
  451.     set responseHeaders(x)
  452.     {
  453.         if (this._responseHeaders === x)
  454.             return;
  455.  
  456.         this._responseHeaders = x;
  457.         delete this._sortedResponseHeaders;
  458.  
  459.         this.dispatchEventToListeners("responseHeaders changed");
  460.     },
  461.  
  462.     get sortedResponseHeaders()
  463.     {
  464.         if (this._sortedResponseHeaders !== undefined)
  465.             return this._sortedResponseHeaders;
  466.  
  467.         this._sortedResponseHeaders = [];
  468.         for (var key in this.responseHeaders)
  469.             this._sortedResponseHeaders.push({header: key, value: this.responseHeaders[key]});
  470.         this._sortedResponseHeaders.sort(function(a,b) { return a.header.localeCompare(b.header) });
  471.  
  472.         return this._sortedResponseHeaders;
  473.     },
  474.  
  475.     get scripts()
  476.     {
  477.         if (!("_scripts" in this))
  478.             this._scripts = [];
  479.         return this._scripts;
  480.     },
  481.  
  482.     addScript: function(script)
  483.     {
  484.         if (!script)
  485.             return;
  486.         this.scripts.unshift(script);
  487.         script.resource = this;
  488.     },
  489.  
  490.     removeAllScripts: function()
  491.     {
  492.         if (!this._scripts)
  493.             return;
  494.  
  495.         for (var i = 0; i < this._scripts.length; ++i) {
  496.             if (this._scripts[i].resource === this)
  497.                 delete this._scripts[i].resource;
  498.         }
  499.  
  500.         delete this._scripts;
  501.     },
  502.  
  503.     removeScript: function(script)
  504.     {
  505.         if (!script)
  506.             return;
  507.  
  508.         if (script.resource === this)
  509.             delete script.resource;
  510.  
  511.         if (!this._scripts)
  512.             return;
  513.  
  514.         this._scripts.remove(script);
  515.     },
  516.  
  517.     get errors()
  518.     {
  519.         return this._errors || 0;
  520.     },
  521.  
  522.     set errors(x)
  523.     {
  524.         this._errors = x;
  525.     },
  526.  
  527.     get warnings()
  528.     {
  529.         return this._warnings || 0;
  530.     },
  531.  
  532.     set warnings(x)
  533.     {
  534.         this._warnings = x;
  535.     },
  536.  
  537.     _mimeTypeIsConsistentWithType: function()
  538.     {
  539.         // If status is an error, content is likely to be of an inconsistent type,
  540.         // as it's going to be an error message. We do not want to emit a warning
  541.         // for this, though, as this will already be reported as resource loading failure.
  542.         if (this.statusCode >= 400)
  543.             return true;
  544.  
  545.         if (typeof this.type === "undefined"
  546.          || this.type === WebInspector.Resource.Type.Other
  547.          || this.type === WebInspector.Resource.Type.XHR)
  548.             return true;
  549.  
  550.         if (this.mimeType in WebInspector.MIMETypes)
  551.             return this.type in WebInspector.MIMETypes[this.mimeType];
  552.  
  553.         return false;
  554.     },
  555.  
  556.     _checkWarnings: function()
  557.     {
  558.         for (var warning in WebInspector.Warnings)
  559.             this._checkWarning(WebInspector.Warnings[warning]);
  560.     },
  561.  
  562.     _checkWarning: function(warning)
  563.     {
  564.         var msg;
  565.         switch (warning.id) {
  566.             case WebInspector.Warnings.IncorrectMIMEType.id:
  567.                 if (!this._mimeTypeIsConsistentWithType())
  568.                     msg = new WebInspector.ConsoleMessage(WebInspector.ConsoleMessage.MessageSource.Other,
  569.                         WebInspector.ConsoleMessage.MessageType.Log, 
  570.                         WebInspector.ConsoleMessage.MessageLevel.Warning, -1, this.url, null, 1,
  571.                         String.sprintf(WebInspector.Warnings.IncorrectMIMEType.message,
  572.                         WebInspector.Resource.Type.toString(this.type), this.mimeType));
  573.                 break;
  574.         }
  575.  
  576.         if (msg)
  577.             WebInspector.console.addMessage(msg);
  578.     }
  579. }
  580.  
  581. WebInspector.Resource.prototype.__proto__ = WebInspector.Object.prototype;
  582.  
  583. WebInspector.Resource.CompareByStartTime = function(a, b)
  584. {
  585.     return a.startTime - b.startTime;
  586. }
  587.  
  588. WebInspector.Resource.CompareByResponseReceivedTime = function(a, b)
  589. {
  590.     var aVal = a.responseReceivedTime;
  591.     var bVal = b.responseReceivedTime;
  592.     if (aVal === -1 ^ bVal === -1)
  593.         return bVal - aVal;
  594.     return aVal - bVal;
  595. }
  596.  
  597. WebInspector.Resource.CompareByEndTime = function(a, b)
  598. {
  599.     var aVal = a.endTime;
  600.     var bVal = b.endTime;
  601.     if (aVal === -1 ^ bVal === -1)
  602.         return bVal - aVal;
  603.     return aVal - bVal;
  604. }
  605.  
  606. WebInspector.Resource.CompareByDuration = function(a, b)
  607. {
  608.     return a.duration - b.duration;
  609. }
  610.  
  611. WebInspector.Resource.CompareByLatency = function(a, b)
  612. {
  613.     return a.latency - b.latency;
  614. }
  615.  
  616. WebInspector.Resource.CompareBySize = function(a, b)
  617. {
  618.     return a.resourceSize - b.resourceSize;
  619. }
  620.  
  621. WebInspector.Resource.CompareByTransferSize = function(a, b)
  622. {
  623.     return a.transferSize - b.transferSize;
  624. }
  625.  
  626.  
  627. WebInspector.Resource.StatusTextForCode = function(code)
  628. {
  629.     return code ? code + " " + WebInspector.Resource.StatusText[code] : "";
  630. }
  631.