ResourceTreeElement.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (C) 2013 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. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. WebInspector.ResourceTreeElement = function(resource, representedObject)
  26. {
  27. console.assert(resource instanceof WebInspector.Resource);
  28. WebInspector.SourceCodeTreeElement.call(this, resource, [WebInspector.ResourceTreeElement.StyleClassName, WebInspector.ResourceTreeElement.ResourceIconStyleClassName, resource.type], "", "", representedObject || resource, false);
  29. this._updateResource(resource);
  30. };
  31. WebInspector.ResourceTreeElement.StyleClassName = "resource";
  32. WebInspector.ResourceTreeElement.ResourceIconStyleClassName = "resource-icon";
  33. WebInspector.ResourceTreeElement.FailedStyleClassName = "failed";
  34. WebInspector.ResourceTreeElement.compareResourceTreeElements = function(a, b)
  35. {
  36. // Compare by type first to keep resources grouped by type when not sorted into folders.
  37. var comparisonResult = a.resource.type.localeCompare(b.resource.type);
  38. if (comparisonResult !== 0)
  39. return comparisonResult;
  40. // Compare async resource types by their first timestamp so they are in chronological order.
  41. if (a.resource.type === WebInspector.Resource.Type.XHR || a.resource.type === WebInspector.Resource.Type.WebSocket)
  42. return a.resource.firstTimestamp - b.resource.firstTimestamp || 0;
  43. // Compare by subtitle when the types are the same. The subtitle is used to show the
  44. // domain of the resource. This causes resources to group by domain. If the resource
  45. // is on the same domain as the frame it will have an empty subtitle. This is good
  46. // because empty string sorts first, so those will appear before external resources.
  47. comparisonResult = a.subtitle.localeCompare(b.subtitle);
  48. if (comparisonResult !== 0)
  49. return comparisonResult;
  50. // Compare by title when the subtitles are the same.
  51. return a.mainTitle.localeCompare(b.mainTitle);
  52. }
  53. WebInspector.ResourceTreeElement.compareFolderAndResourceTreeElements = function(a, b)
  54. {
  55. var aIsFolder = a instanceof WebInspector.FolderTreeElement;
  56. var bIsFolder = b instanceof WebInspector.FolderTreeElement;
  57. if (aIsFolder && !bIsFolder)
  58. return -1;
  59. if (!aIsFolder && bIsFolder)
  60. return 1;
  61. if (aIsFolder && bIsFolder)
  62. return a.mainTitle.localeCompare(b.mainTitle);
  63. return WebInspector.ResourceTreeElement.compareResourceTreeElements(a, b);
  64. }
  65. WebInspector.ResourceTreeElement.prototype = {
  66. constructor: WebInspector.ResourceTreeElement,
  67. // Public
  68. get resource()
  69. {
  70. return this._resource;
  71. },
  72. get filterableData()
  73. {
  74. return {text: this._resource.url};
  75. },
  76. get reloadButton()
  77. {
  78. if (!this._reloadButton) {
  79. this._reloadButton = document.createElement("img");
  80. this._reloadButton.className = "reload-button";
  81. this._reloadButton.title = WebInspector.UIString("Reload page (%s)\nReload ignoring cache (%s)").format(WebInspector._reloadPageKeyboardShortcut.displayName, WebInspector._reloadPageIgnoringCacheKeyboardShortcut.displayName);
  82. this._reloadButton.addEventListener("click", this._reloadPageClicked.bind(this));
  83. }
  84. return this._reloadButton;
  85. },
  86. ondblclick: function()
  87. {
  88. InspectorFrontendHost.openInNewTab(this._resource.url);
  89. },
  90. // Protected (Used by FrameTreeElement)
  91. _updateResource: function(resource)
  92. {
  93. console.assert(resource instanceof WebInspector.Resource);
  94. // This method is for subclasses like FrameTreeElement who don't use a resource as the representedObject.
  95. // This method should only be called once if the representedObject is a resource, since changing the resource
  96. // without changing the representedObject is bad. If you need to change the resource, make a new ResourceTreeElement.
  97. console.assert(!this._resource || !(this.representedObject instanceof WebInspector.Resource));
  98. if (this._resource) {
  99. this._resource.removeEventListener(WebInspector.Resource.Event.URLDidChange, this._urlDidChange, this);
  100. this._resource.removeEventListener(WebInspector.Resource.Event.TypeDidChange, this._typeDidChange, this);
  101. this._resource.removeEventListener(WebInspector.Resource.Event.LoadingDidFinish, this._updateStatus, this);
  102. this._resource.removeEventListener(WebInspector.Resource.Event.LoadingDidFail, this._updateStatus, this);
  103. }
  104. this._updateSourceCode(resource);
  105. this._resource = resource;
  106. resource.addEventListener(WebInspector.Resource.Event.URLDidChange, this._urlDidChange, this);
  107. resource.addEventListener(WebInspector.Resource.Event.TypeDidChange, this._typeDidChange, this);
  108. resource.addEventListener(WebInspector.Resource.Event.LoadingDidFinish, this._updateStatus, this);
  109. resource.addEventListener(WebInspector.Resource.Event.LoadingDidFail, this._updateStatus, this);
  110. this._updateTitles();
  111. this._updateStatus();
  112. this._updateToolTip();
  113. },
  114. // Protected
  115. _updateTitles: function()
  116. {
  117. var frame = this._resource.parentFrame;
  118. var isMainResource = this._resource.isMainResource();
  119. if (isMainResource && frame) {
  120. // When the resource is a main resource, get the host from the current frame's parent frame instead of the current frame.
  121. var parentResourceHost = frame.parentFrame ? frame.parentFrame.mainResource.urlComponents.host : null;
  122. } else if (frame) {
  123. // When the resource is a normal sub-resource, get the host from the current frame's main resource.
  124. var parentResourceHost = frame.mainResource.urlComponents.host;
  125. }
  126. var urlComponents = this._resource.urlComponents;
  127. var oldMainTitle = this.mainTitle;
  128. this.mainTitle = WebInspector.displayNameForURL(this._resource.url, urlComponents);
  129. // Show the host as the subtitle if it is different from the main resource or if this is the main frame's main resource.
  130. var subtitle = parentResourceHost !== urlComponents.host || frame.isMainFrame() && isMainResource ? WebInspector.displayNameForHost(urlComponents.host) : null;
  131. this.subtitle = this.mainTitle !== subtitle ? subtitle : null;
  132. if (oldMainTitle !== this.mainTitle)
  133. this.callFirstAncestorFunction("descendantResourceTreeElementMainTitleDidChange", [this, oldMainTitle]);
  134. },
  135. // Private
  136. _updateStatus: function()
  137. {
  138. if (this._resource.failed)
  139. this.addClassName(WebInspector.ResourceTreeElement.FailedStyleClassName);
  140. else
  141. this.removeClassName(WebInspector.ResourceTreeElement.FailedStyleClassName);
  142. if (this._resource.finished || this._resource.failed) {
  143. // Remove the spinner and replace with a reload button in case it's the main frame's main resource.
  144. var frame = this._resource.parentFrame;
  145. this.status = this._resource.isMainResource() && frame && frame.isMainFrame() ? this.reloadButton : null;
  146. } else {
  147. var spinner = new WebInspector.IndeterminateProgressSpinner;
  148. this.status = spinner.element;
  149. }
  150. },
  151. _updateToolTip: function()
  152. {
  153. this.tooltip = this._resource.url;
  154. },
  155. _reloadPageClicked: function(event)
  156. {
  157. event.stopPropagation();
  158. // Ignore cache when the shift key is pressed.
  159. PageAgent.reload(event.shiftKey);
  160. },
  161. _urlDidChange: function(event)
  162. {
  163. this._updateTitles();
  164. this._updateToolTip();
  165. },
  166. _typeDidChange: function(event)
  167. {
  168. this.removeClassName(event.data.oldType);
  169. this.addClassName(this._resource.type);
  170. this.callFirstAncestorFunction("descendantResourceTreeElementTypeDidChange", [this, event.data.oldType]);
  171. }
  172. };
  173. WebInspector.ResourceTreeElement.prototype.__proto__ = WebInspector.SourceCodeTreeElement.prototype;