NetworkUISourceCodeProvider.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (C) 2012 Google 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 are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * @constructor
  32. * @param {WebInspector.SimpleWorkspaceProvider} networkWorkspaceProvider
  33. * @param {WebInspector.Workspace} workspace
  34. */
  35. WebInspector.NetworkUISourceCodeProvider = function(networkWorkspaceProvider, workspace)
  36. {
  37. this._networkWorkspaceProvider = networkWorkspaceProvider;
  38. this._workspace = workspace;
  39. WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.ResourceAdded, this._resourceAdded, this);
  40. WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.MainFrameNavigated, this._mainFrameNavigated, this);
  41. WebInspector.debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.ParsedScriptSource, this._parsedScriptSource, this);
  42. WebInspector.cssModel.addEventListener(WebInspector.CSSStyleModel.Events.StyleSheetAdded, this._styleSheetAdded, this);
  43. this._processedURLs = {};
  44. }
  45. WebInspector.NetworkUISourceCodeProvider.prototype = {
  46. _populate: function()
  47. {
  48. function populateFrame(frame)
  49. {
  50. for (var i = 0; i < frame.childFrames.length; ++i)
  51. populateFrame.call(this, frame.childFrames[i]);
  52. var resources = frame.resources();
  53. for (var i = 0; i < resources.length; ++i)
  54. this._resourceAdded({data:resources[i]});
  55. }
  56. populateFrame.call(this, WebInspector.resourceTreeModel.mainFrame);
  57. },
  58. /**
  59. * @param {WebInspector.Event} event
  60. */
  61. _parsedScriptSource: function(event)
  62. {
  63. var script = /** @type {WebInspector.Script} */ (event.data);
  64. if (!script.sourceURL || script.isInlineScript() || script.isSnippet())
  65. return;
  66. // Filter out embedder injected content scripts.
  67. if (script.isContentScript && !script.hasSourceURL) {
  68. var parsedURL = new WebInspector.ParsedURL(script.sourceURL);
  69. if (!parsedURL.host)
  70. return;
  71. }
  72. this._addFile(script.sourceURL, script, script.isContentScript);
  73. },
  74. /**
  75. * @param {WebInspector.Event} event
  76. */
  77. _styleSheetAdded: function(event)
  78. {
  79. var header = /** @type {WebInspector.CSSStyleSheetHeader} */ (event.data);
  80. if ((!header.hasSourceURL || header.isInline) && header.origin !== "inspector")
  81. return;
  82. this._addFile(header.resourceURL(), header, false);
  83. },
  84. /**
  85. * @param {WebInspector.Event} event
  86. */
  87. _resourceAdded: function(event)
  88. {
  89. var resource = /** @type {WebInspector.Resource} */ (event.data);
  90. this._addFile(resource.url, resource);
  91. },
  92. /**
  93. * @param {WebInspector.Event} event
  94. */
  95. _mainFrameNavigated: function(event)
  96. {
  97. this._reset();
  98. },
  99. /**
  100. * @param {string} url
  101. * @param {WebInspector.ContentProvider} contentProvider
  102. * @param {boolean=} isContentScript
  103. */
  104. _addFile: function(url, contentProvider, isContentScript)
  105. {
  106. if (this._workspace.hasMappingForURL(url))
  107. return;
  108. var type = contentProvider.contentType();
  109. if (type !== WebInspector.resourceTypes.Stylesheet && type !== WebInspector.resourceTypes.Document && type !== WebInspector.resourceTypes.Script)
  110. return;
  111. if (this._processedURLs[url])
  112. return;
  113. var mimeType = type.canonicalMimeType();
  114. var overridingContentProvider = new WebInspector.ContentProviderOverridingMimeType(contentProvider, mimeType);
  115. this._processedURLs[url] = true;
  116. var isEditable = type !== WebInspector.resourceTypes.Document;
  117. this._networkWorkspaceProvider.addFileForURL(url, overridingContentProvider, isEditable, isContentScript);
  118. },
  119. _reset: function()
  120. {
  121. this._processedURLs = {};
  122. this._networkWorkspaceProvider.reset();
  123. this._populate();
  124. }
  125. }
  126. /**
  127. * @type {?WebInspector.SimpleWorkspaceProvider}
  128. */
  129. WebInspector.networkWorkspaceProvider = null;