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