NetworkLog.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (C) 2011 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. */
  33. WebInspector.NetworkLog = function()
  34. {
  35. this._requests = [];
  36. this._requestForId = {};
  37. WebInspector.networkManager.addEventListener(WebInspector.NetworkManager.EventTypes.RequestStarted, this._onRequestStarted, this);
  38. WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.MainFrameNavigated, this._onMainFrameNavigated, this);
  39. WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.OnLoad, this._onLoad, this);
  40. WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.DOMContentLoaded, this._onDOMContentLoaded, this);
  41. }
  42. WebInspector.NetworkLog.prototype = {
  43. /**
  44. * @return {Array.<WebInspector.NetworkRequest>}
  45. */
  46. get requests()
  47. {
  48. return this._requests;
  49. },
  50. /**
  51. * @param {string} url
  52. * @return {WebInspector.NetworkRequest}
  53. */
  54. requestForURL: function(url)
  55. {
  56. for (var i = 0; i < this._requests.length; ++i) {
  57. if (this._requests[i].url === url)
  58. return this._requests[i];
  59. }
  60. return null;
  61. },
  62. /**
  63. * @param {WebInspector.NetworkRequest} request
  64. * @return {WebInspector.PageLoad}
  65. */
  66. pageLoadForRequest: function(request)
  67. {
  68. return request.__page;
  69. },
  70. /**
  71. * @param {WebInspector.Event} event
  72. */
  73. _onMainFrameNavigated: function(event)
  74. {
  75. var mainFrame = /** type {WebInspector.ResourceTreeFrame} */ event.data;
  76. // Preserve requests from the new session.
  77. this._currentPageLoad = null;
  78. var oldRequests = this._requests.splice(0, this._requests.length);
  79. for (var i = 0; i < oldRequests.length; ++i) {
  80. var request = oldRequests[i];
  81. if (request.loaderId === mainFrame.loaderId) {
  82. if (!this._currentPageLoad)
  83. this._currentPageLoad = new WebInspector.PageLoad(request);
  84. this._requests.push(request);
  85. request.__page = this._currentPageLoad;
  86. }
  87. }
  88. },
  89. /**
  90. * @param {WebInspector.Event} event
  91. */
  92. _onRequestStarted: function(event)
  93. {
  94. var request = /** @type {WebInspector.NetworkRequest} */ (event.data);
  95. this._requests.push(request);
  96. this._requestForId[request.requestId] = request;
  97. request.__page = this._currentPageLoad;
  98. },
  99. /**
  100. * @param {WebInspector.Event} event
  101. */
  102. _onDOMContentLoaded: function(event)
  103. {
  104. if (this._currentPageLoad)
  105. this._currentPageLoad.contentLoadTime = event.data;
  106. },
  107. /**
  108. * @param {WebInspector.Event} event
  109. */
  110. _onLoad: function(event)
  111. {
  112. if (this._currentPageLoad)
  113. this._currentPageLoad.loadTime = event.data;
  114. },
  115. /**
  116. * @param {NetworkAgent.RequestId} requestId
  117. * @return {?WebInspector.NetworkRequest}
  118. */
  119. requestForId: function(requestId)
  120. {
  121. return this._requestForId[requestId];
  122. }
  123. }
  124. /**
  125. * @type {WebInspector.NetworkLog}
  126. */
  127. WebInspector.networkLog = null;
  128. /**
  129. * @constructor
  130. * @param {WebInspector.NetworkRequest} mainRequest
  131. */
  132. WebInspector.PageLoad = function(mainRequest)
  133. {
  134. this.id = ++WebInspector.PageLoad._lastIdentifier;
  135. this.url = mainRequest.url;
  136. this.startTime = mainRequest.startTime;
  137. }
  138. WebInspector.PageLoad._lastIdentifier = 0;