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