DOMStorage.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (C) 2008 Nokia Inc. All rights reserved.
  3. * Copyright (C) 2013 Samsung Electronics. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  15. * its contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /**
  30. * @constructor
  31. * @param {string} securityOrigin
  32. * @param {boolean} isLocalStorage
  33. */
  34. WebInspector.DOMStorage = function(securityOrigin, isLocalStorage)
  35. {
  36. this._securityOrigin = securityOrigin;
  37. this._isLocalStorage = isLocalStorage;
  38. }
  39. /**
  40. * @param {string} securityOrigin
  41. * @param {boolean} isLocalStorage
  42. * @return {DOMStorageAgent.StorageId}
  43. */
  44. WebInspector.DOMStorage.storageId = function(securityOrigin, isLocalStorage)
  45. {
  46. return { securityOrigin: securityOrigin, isLocalStorage: isLocalStorage };
  47. }
  48. WebInspector.DOMStorage.prototype = {
  49. /** @return {DOMStorageAgent.StorageId} */
  50. get id()
  51. {
  52. return WebInspector.DOMStorage.storageId(this._securityOrigin, this._isLocalStorage);
  53. },
  54. /** @return {string} */
  55. get securityOrigin()
  56. {
  57. return this._securityOrigin;
  58. },
  59. /** @return {boolean} */
  60. get isLocalStorage()
  61. {
  62. return this._isLocalStorage;
  63. },
  64. /**
  65. * @param {function(?Protocol.Error, Array.<DOMStorageAgent.Item>):void=} callback
  66. */
  67. getItems: function(callback)
  68. {
  69. DOMStorageAgent.getDOMStorageItems(this.id, callback);
  70. },
  71. /**
  72. * @param {string} key
  73. * @param {string} value
  74. * @param {function(?Protocol.Error):void=} callback
  75. */
  76. setItem: function(key, value, callback)
  77. {
  78. DOMStorageAgent.setDOMStorageItem(this.id, key, value, callback);
  79. },
  80. /**
  81. * @param {string} key
  82. * @param {function(?Protocol.Error):void=} callback
  83. */
  84. removeItem: function(key, callback)
  85. {
  86. DOMStorageAgent.removeDOMStorageItem(this.id, key, callback);
  87. }
  88. }
  89. /**
  90. * @constructor
  91. * @extends {WebInspector.Object}
  92. */
  93. WebInspector.DOMStorageModel = function()
  94. {
  95. this._storages = {};
  96. InspectorBackend.registerDOMStorageDispatcher(new WebInspector.DOMStorageDispatcher(this));
  97. DOMStorageAgent.enable();
  98. WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.SecurityOriginAdded, this._securityOriginAdded, this);
  99. WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.SecurityOriginRemoved, this._securityOriginRemoved, this);
  100. }
  101. WebInspector.DOMStorageModel.Events = {
  102. DOMStorageAdded: "DOMStorageAdded",
  103. DOMStorageRemoved: "DOMStorageRemoved",
  104. DOMStorageItemsCleared: "DOMStorageItemsCleared",
  105. DOMStorageItemRemoved: "DOMStorageItemRemoved",
  106. DOMStorageItemAdded: "DOMStorageItemAdded",
  107. DOMStorageItemUpdated: "DOMStorageItemUpdated"
  108. }
  109. WebInspector.DOMStorageModel.prototype = {
  110. /**
  111. * @param {WebInspector.Event} event
  112. */
  113. _securityOriginAdded: function(event)
  114. {
  115. var securityOrigin = /** @type {string} */ (event.data);
  116. var localStorageKey = this._storageKey(securityOrigin, true);
  117. console.assert(!this._storages[localStorageKey]);
  118. var localStorage = new WebInspector.DOMStorage(securityOrigin, true);
  119. this._storages[localStorageKey] = localStorage;
  120. this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageAdded, localStorage);
  121. var sessionStorageKey = this._storageKey(securityOrigin, false);
  122. console.assert(!this._storages[sessionStorageKey]);
  123. var sessionStorage = new WebInspector.DOMStorage(securityOrigin, false);
  124. this._storages[sessionStorageKey] = sessionStorage;
  125. this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageAdded, sessionStorage);
  126. },
  127. /**
  128. * @param {WebInspector.Event} event
  129. */
  130. _securityOriginRemoved: function(event)
  131. {
  132. var securityOrigin = /** @type {string} */ (event.data);
  133. var localStorageKey = this._storageKey(securityOrigin, true);
  134. var localStorage = this._storages[localStorageKey];
  135. console.assert(localStorage);
  136. delete this._storages[localStorageKey];
  137. this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageRemoved, localStorage);
  138. var sessionStorageKey = this._storageKey(securityOrigin, false);
  139. var sessionStorage = this._storages[sessionStorageKey];
  140. console.assert(sessionStorage);
  141. delete this._storages[sessionStorageKey];
  142. this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageRemoved, sessionStorage);
  143. },
  144. /**
  145. * @param {string} securityOrigin
  146. * @param {boolean} isLocalStorage
  147. * @return {string}
  148. */
  149. _storageKey: function(securityOrigin, isLocalStorage)
  150. {
  151. return JSON.stringify(WebInspector.DOMStorage.storageId(securityOrigin, isLocalStorage));
  152. },
  153. /**
  154. * @param {DOMStorageAgent.StorageId} storageId
  155. */
  156. _domStorageItemsCleared: function(storageId)
  157. {
  158. var domStorage = this.storageForId(storageId);
  159. var storageData = {
  160. storage: domStorage
  161. };
  162. this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageItemsCleared, storageData);
  163. },
  164. /**
  165. * @param {DOMStorageAgent.StorageId} storageId
  166. * @param {string} key
  167. */
  168. _domStorageItemRemoved: function(storageId, key)
  169. {
  170. var domStorage = this.storageForId(storageId);
  171. var storageData = {
  172. storage: domStorage,
  173. key: key
  174. };
  175. this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageItemRemoved, storageData);
  176. },
  177. /**
  178. * @param {DOMStorageAgent.StorageId} storageId
  179. * @param {string} key
  180. * @param {string} newValue
  181. */
  182. _domStorageItemAdded: function(storageId, key, newValue)
  183. {
  184. var domStorage = this.storageForId(storageId);
  185. var storageData = {
  186. storage: domStorage,
  187. key: key,
  188. newValue: newValue
  189. };
  190. this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageItemAdded, storageData);
  191. },
  192. /**
  193. * @param {DOMStorageAgent.StorageId} storageId
  194. * @param {string} key
  195. * @param {string} oldValue
  196. * @param {string} newValue
  197. */
  198. _domStorageItemUpdated: function(storageId, key, oldValue, newValue)
  199. {
  200. var domStorage = this._storages[storageId];
  201. var storageData = {
  202. storage: domStorage,
  203. key: key,
  204. oldValue: oldValue,
  205. newValue: newValue
  206. };
  207. this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageItemUpdated, storageData);
  208. },
  209. /**
  210. * @param {DOMStorageAgent.StorageId} storageId
  211. * @return {WebInspector.DOMStorage}
  212. */
  213. storageForId: function(storageId)
  214. {
  215. return this._storages[JSON.stringify(storageId)];
  216. },
  217. /**
  218. * @return {Array.<WebInspector.DOMStorage>}
  219. */
  220. storages: function()
  221. {
  222. var result = [];
  223. for (var id in this._storages)
  224. result.push(this._storages[id]);
  225. return result;
  226. },
  227. __proto__: WebInspector.Object.prototype
  228. }
  229. /**
  230. * @constructor
  231. * @implements {DOMStorageAgent.Dispatcher}
  232. * @param {WebInspector.DOMStorageModel} model
  233. */
  234. WebInspector.DOMStorageDispatcher = function(model)
  235. {
  236. this._model = model;
  237. }
  238. WebInspector.DOMStorageDispatcher.prototype = {
  239. /**
  240. * @param {DOMStorageAgent.StorageId} storageId
  241. */
  242. domStorageItemsCleared: function(storageId)
  243. {
  244. this._model._domStorageItemsCleared(storageId);
  245. },
  246. /**
  247. * @param {DOMStorageAgent.StorageId} storageId
  248. * @param {string} key
  249. */
  250. domStorageItemRemoved: function(storageId, key)
  251. {
  252. this._model._domStorageItemRemoved(storageId, key);
  253. },
  254. /**
  255. * @param {DOMStorageAgent.StorageId} storageId
  256. * @param {string} key
  257. * @param {string} newValue
  258. */
  259. domStorageItemAdded: function(storageId, key, newValue)
  260. {
  261. this._model._domStorageItemAdded(storageId, key, newValue);
  262. },
  263. /**
  264. * @param {DOMStorageAgent.StorageId} storageId
  265. * @param {string} key
  266. * @param {string} oldValue
  267. * @param {string} newValue
  268. */
  269. domStorageItemUpdated: function(storageId, key, oldValue, newValue)
  270. {
  271. this._model._domStorageItemUpdated(storageId, key, oldValue, newValue);
  272. },
  273. }
  274. /**
  275. * @type {WebInspector.DOMStorageModel}
  276. */
  277. WebInspector.domStorageModel = null;