ApplicationCacheModel.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. AND ITS CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC.
  20. * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /**
  29. * @constructor
  30. * @extends {WebInspector.Object}
  31. */
  32. WebInspector.ApplicationCacheModel = function()
  33. {
  34. ApplicationCacheAgent.enable();
  35. InspectorBackend.registerApplicationCacheDispatcher(new WebInspector.ApplicationCacheDispatcher(this));
  36. WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.FrameNavigated, this._frameNavigated, this);
  37. WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.FrameDetached, this._frameDetached, this);
  38. this._statuses = {};
  39. this._manifestURLsByFrame = {};
  40. this._mainFrameNavigated();
  41. this._onLine = true;
  42. }
  43. WebInspector.ApplicationCacheModel.EventTypes = {
  44. FrameManifestStatusUpdated: "FrameManifestStatusUpdated",
  45. FrameManifestAdded: "FrameManifestAdded",
  46. FrameManifestRemoved: "FrameManifestRemoved",
  47. NetworkStateChanged: "NetworkStateChanged"
  48. }
  49. WebInspector.ApplicationCacheModel.prototype = {
  50. _frameNavigated: function(event)
  51. {
  52. var frame = /** @type {WebInspector.ResourceTreeFrame} */ (event.data);
  53. if (frame.isMainFrame()) {
  54. this._mainFrameNavigated();
  55. return;
  56. }
  57. ApplicationCacheAgent.getManifestForFrame(frame.id, this._manifestForFrameLoaded.bind(this, frame.id));
  58. },
  59. /**
  60. * @param {WebInspector.Event} event
  61. */
  62. _frameDetached: function(event)
  63. {
  64. var frame = /** @type {WebInspector.ResourceTreeFrame} */ (event.data);
  65. this._frameManifestRemoved(frame.id);
  66. },
  67. _mainFrameNavigated: function()
  68. {
  69. ApplicationCacheAgent.getFramesWithManifests(this._framesWithManifestsLoaded.bind(this));
  70. },
  71. /**
  72. * @param {string} frameId
  73. * @param {?Protocol.Error} error
  74. * @param {string} manifestURL
  75. */
  76. _manifestForFrameLoaded: function(frameId, error, manifestURL)
  77. {
  78. if (error) {
  79. console.error(error);
  80. return;
  81. }
  82. if (!manifestURL)
  83. this._frameManifestRemoved(frameId);
  84. },
  85. /**
  86. * @param {?Protocol.Error} error
  87. * @param {Array.<ApplicationCacheAgent.FrameWithManifest>} framesWithManifests
  88. */
  89. _framesWithManifestsLoaded: function(error, framesWithManifests)
  90. {
  91. if (error) {
  92. console.error(error);
  93. return;
  94. }
  95. for (var i = 0; i < framesWithManifests.length; ++i)
  96. this._frameManifestUpdated(framesWithManifests[i].frameId, framesWithManifests[i].manifestURL, framesWithManifests[i].status);
  97. },
  98. /**
  99. * @param {string} frameId
  100. * @param {string} manifestURL
  101. * @param {number} status
  102. */
  103. _frameManifestUpdated: function(frameId, manifestURL, status)
  104. {
  105. if (status === applicationCache.UNCACHED) {
  106. this._frameManifestRemoved(frameId);
  107. return;
  108. }
  109. if (!manifestURL)
  110. return;
  111. if (this._manifestURLsByFrame[frameId] && manifestURL !== this._manifestURLsByFrame[frameId])
  112. this._frameManifestRemoved(frameId);
  113. var statusChanged = this._statuses[frameId] !== status;
  114. this._statuses[frameId] = status;
  115. if (!this._manifestURLsByFrame[frameId]) {
  116. this._manifestURLsByFrame[frameId] = manifestURL;
  117. this.dispatchEventToListeners(WebInspector.ApplicationCacheModel.EventTypes.FrameManifestAdded, frameId);
  118. }
  119. if (statusChanged)
  120. this.dispatchEventToListeners(WebInspector.ApplicationCacheModel.EventTypes.FrameManifestStatusUpdated, frameId);
  121. },
  122. /**
  123. * @param {string} frameId
  124. */
  125. _frameManifestRemoved: function(frameId)
  126. {
  127. if (!this._manifestURLsByFrame[frameId])
  128. return;
  129. var manifestURL = this._manifestURLsByFrame[frameId];
  130. delete this._manifestURLsByFrame[frameId];
  131. delete this._statuses[frameId];
  132. this.dispatchEventToListeners(WebInspector.ApplicationCacheModel.EventTypes.FrameManifestRemoved, frameId);
  133. },
  134. /**
  135. * @param {string} frameId
  136. * @return {string}
  137. */
  138. frameManifestURL: function(frameId)
  139. {
  140. return this._manifestURLsByFrame[frameId] || "";
  141. },
  142. /**
  143. * @param {string} frameId
  144. * @return {number}
  145. */
  146. frameManifestStatus: function(frameId)
  147. {
  148. return this._statuses[frameId] || applicationCache.UNCACHED;
  149. },
  150. /**
  151. * @return {boolean}
  152. */
  153. get onLine()
  154. {
  155. return this._onLine;
  156. },
  157. /**
  158. * @param {string} frameId
  159. * @param {string} manifestURL
  160. * @param {number} status
  161. */
  162. _statusUpdated: function(frameId, manifestURL, status)
  163. {
  164. this._frameManifestUpdated(frameId, manifestURL, status);
  165. },
  166. /**
  167. * @param {string} frameId
  168. * @param {function(Object)} callback
  169. */
  170. requestApplicationCache: function(frameId, callback)
  171. {
  172. function callbackWrapper(error, applicationCache)
  173. {
  174. if (error) {
  175. console.error(error);
  176. callback(null);
  177. return;
  178. }
  179. callback(applicationCache);
  180. }
  181. ApplicationCacheAgent.getApplicationCacheForFrame(frameId, callbackWrapper.bind(this));
  182. },
  183. /**
  184. * @param {boolean} isNowOnline
  185. */
  186. _networkStateUpdated: function(isNowOnline)
  187. {
  188. this._onLine = isNowOnline;
  189. this.dispatchEventToListeners(WebInspector.ApplicationCacheModel.EventTypes.NetworkStateChanged, isNowOnline);
  190. },
  191. __proto__: WebInspector.Object.prototype
  192. }
  193. /**
  194. * @constructor
  195. * @implements {ApplicationCacheAgent.Dispatcher}
  196. */
  197. WebInspector.ApplicationCacheDispatcher = function(applicationCacheModel)
  198. {
  199. this._applicationCacheModel = applicationCacheModel;
  200. }
  201. WebInspector.ApplicationCacheDispatcher.prototype = {
  202. /**
  203. * @param {string} frameId
  204. * @param {string} manifestURL
  205. * @param {number} status
  206. */
  207. applicationCacheStatusUpdated: function(frameId, manifestURL, status)
  208. {
  209. this._applicationCacheModel._statusUpdated(frameId, manifestURL, status);
  210. },
  211. /**
  212. * @param {boolean} isNowOnline
  213. */
  214. networkStateUpdated: function(isNowOnline)
  215. {
  216. this._applicationCacheModel._networkStateUpdated(isNowOnline);
  217. }
  218. }