DOMStorage.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. * @extends {WebInspector.Object}
  32. * @param {string} securityOrigin
  33. * @param {boolean} isLocalStorage
  34. */
  35. WebInspector.DOMStorage = function(securityOrigin, isLocalStorage)
  36. {
  37. this._securityOrigin = securityOrigin;
  38. this._isLocalStorage = isLocalStorage;
  39. }
  40. /**
  41. * @param {string} securityOrigin
  42. * @param {boolean} isLocalStorage
  43. * @return {DOMStorageAgent.StorageId}
  44. */
  45. WebInspector.DOMStorage.storageId = function(securityOrigin, isLocalStorage)
  46. {
  47. return { securityOrigin: securityOrigin, isLocalStorage: isLocalStorage };
  48. }
  49. WebInspector.DOMStorage.Events = {
  50. DOMStorageItemsCleared: "DOMStorageItemsCleared",
  51. DOMStorageItemRemoved: "DOMStorageItemRemoved",
  52. DOMStorageItemAdded: "DOMStorageItemAdded",
  53. DOMStorageItemUpdated: "DOMStorageItemUpdated"
  54. }
  55. WebInspector.DOMStorage.prototype = {
  56. /** @return {DOMStorageAgent.StorageId} */
  57. get id()
  58. {
  59. return WebInspector.DOMStorage.storageId(this._securityOrigin, this._isLocalStorage);
  60. },
  61. /** @return {string} */
  62. get securityOrigin()
  63. {
  64. return this._securityOrigin;
  65. },
  66. /** @return {boolean} */
  67. get isLocalStorage()
  68. {
  69. return this._isLocalStorage;
  70. },
  71. /**
  72. * @param {function(?Protocol.Error, Array.<DOMStorageAgent.Item>):void=} callback
  73. */
  74. getItems: function(callback)
  75. {
  76. DOMStorageAgent.getDOMStorageItems(this.id, callback);
  77. },
  78. /**
  79. * @param {string} key
  80. * @param {string} value
  81. */
  82. setItem: function(key, value)
  83. {
  84. DOMStorageAgent.setDOMStorageItem(this.id, key, value);
  85. },
  86. /**
  87. * @param {string} key
  88. */
  89. removeItem: function(key)
  90. {
  91. DOMStorageAgent.removeDOMStorageItem(this.id, key);
  92. },
  93. __proto__: WebInspector.Object.prototype
  94. }
  95. /**
  96. * @constructor
  97. * @extends {WebInspector.Object}
  98. */
  99. WebInspector.DOMStorageModel = function()
  100. {
  101. /** @type {!Object.<string, !WebInspector.DOMStorage>} */
  102. this._storages = {};
  103. InspectorBackend.registerDOMStorageDispatcher(new WebInspector.DOMStorageDispatcher(this));
  104. DOMStorageAgent.enable();
  105. WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.SecurityOriginAdded, this._securityOriginAdded, this);
  106. WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.SecurityOriginRemoved, this._securityOriginRemoved, this);
  107. }
  108. WebInspector.DOMStorageModel.Events = {
  109. DOMStorageAdded: "DOMStorageAdded",
  110. DOMStorageRemoved: "DOMStorageRemoved"
  111. }
  112. WebInspector.DOMStorageModel.prototype = {
  113. /**
  114. * @param {WebInspector.Event} event
  115. */
  116. _securityOriginAdded: function(event)
  117. {
  118. var securityOrigin = /** @type {string} */ (event.data);
  119. var localStorageKey = this._storageKey(securityOrigin, true);
  120. console.assert(!this._storages[localStorageKey]);
  121. var localStorage = new WebInspector.DOMStorage(securityOrigin, true);
  122. this._storages[localStorageKey] = localStorage;
  123. this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageAdded, localStorage);
  124. var sessionStorageKey = this._storageKey(securityOrigin, false);
  125. console.assert(!this._storages[sessionStorageKey]);
  126. var sessionStorage = new WebInspector.DOMStorage(securityOrigin, false);
  127. this._storages[sessionStorageKey] = sessionStorage;
  128. this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageAdded, sessionStorage);
  129. },
  130. /**
  131. * @param {WebInspector.Event} event
  132. */
  133. _securityOriginRemoved: function(event)
  134. {
  135. var securityOrigin = /** @type {string} */ (event.data);
  136. var localStorageKey = this._storageKey(securityOrigin, true);
  137. var localStorage = this._storages[localStorageKey];
  138. console.assert(localStorage);
  139. delete this._storages[localStorageKey];
  140. this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageRemoved, localStorage);
  141. var sessionStorageKey = this._storageKey(securityOrigin, false);
  142. var sessionStorage = this._storages[sessionStorageKey];
  143. console.assert(sessionStorage);
  144. delete this._storages[sessionStorageKey];
  145. this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageRemoved, sessionStorage);
  146. },
  147. /**
  148. * @param {string} securityOrigin
  149. * @param {boolean} isLocalStorage
  150. * @return {string}
  151. */
  152. _storageKey: function(securityOrigin, isLocalStorage)
  153. {
  154. return JSON.stringify(WebInspector.DOMStorage.storageId(securityOrigin, isLocalStorage));
  155. },
  156. /**
  157. * @param {DOMStorageAgent.StorageId} storageId
  158. */
  159. _domStorageItemsCleared: function(storageId)
  160. {
  161. var domStorage = this.storageForId(storageId);
  162. if (!domStorage)
  163. return;
  164. var eventData = {};
  165. domStorage.dispatchEventToListeners(WebInspector.DOMStorage.Events.DOMStorageItemsCleared, eventData);
  166. },
  167. /**
  168. * @param {DOMStorageAgent.StorageId} storageId
  169. * @param {string} key
  170. */
  171. _domStorageItemRemoved: function(storageId, key)
  172. {
  173. var domStorage = this.storageForId(storageId);
  174. if (!domStorage)
  175. return;
  176. var eventData = { key: key };
  177. domStorage.dispatchEventToListeners(WebInspector.DOMStorage.Events.DOMStorageItemRemoved, eventData);
  178. },
  179. /**
  180. * @param {DOMStorageAgent.StorageId} storageId
  181. * @param {string} key
  182. * @param {string} value
  183. */
  184. _domStorageItemAdded: function(storageId, key, value)
  185. {
  186. var domStorage = this.storageForId(storageId);
  187. if (!domStorage)
  188. return;
  189. var eventData = { key: key, value: value };
  190. domStorage.dispatchEventToListeners(WebInspector.DOMStorage.Events.DOMStorageItemAdded, eventData);
  191. },
  192. /**
  193. * @param {DOMStorageAgent.StorageId} storageId
  194. * @param {string} key
  195. * @param {string} oldValue
  196. * @param {string} value
  197. */
  198. _domStorageItemUpdated: function(storageId, key, oldValue, value)
  199. {
  200. var domStorage = this.storageForId(storageId);
  201. if (!domStorage)
  202. return;
  203. var eventData = { key: key, oldValue: oldValue, value: value };
  204. domStorage.dispatchEventToListeners(WebInspector.DOMStorage.Events.DOMStorageItemUpdated, eventData);
  205. },
  206. /**
  207. * @param {DOMStorageAgent.StorageId} storageId
  208. * @return {WebInspector.DOMStorage}
  209. */
  210. storageForId: function(storageId)
  211. {
  212. return this._storages[JSON.stringify(storageId)];
  213. },
  214. /**
  215. * @return {Array.<WebInspector.DOMStorage>}
  216. */
  217. storages: function()
  218. {
  219. var result = [];
  220. for (var id in this._storages)
  221. result.push(this._storages[id]);
  222. return result;
  223. },
  224. __proto__: WebInspector.Object.prototype
  225. }
  226. /**
  227. * @constructor
  228. * @implements {DOMStorageAgent.Dispatcher}
  229. * @param {WebInspector.DOMStorageModel} model
  230. */
  231. WebInspector.DOMStorageDispatcher = function(model)
  232. {
  233. this._model = model;
  234. }
  235. WebInspector.DOMStorageDispatcher.prototype = {
  236. /**
  237. * @param {DOMStorageAgent.StorageId} storageId
  238. */
  239. domStorageItemsCleared: function(storageId)
  240. {
  241. this._model._domStorageItemsCleared(storageId);
  242. },
  243. /**
  244. * @param {DOMStorageAgent.StorageId} storageId
  245. * @param {string} key
  246. */
  247. domStorageItemRemoved: function(storageId, key)
  248. {
  249. this._model._domStorageItemRemoved(storageId, key);
  250. },
  251. /**
  252. * @param {DOMStorageAgent.StorageId} storageId
  253. * @param {string} key
  254. * @param {string} value
  255. */
  256. domStorageItemAdded: function(storageId, key, value)
  257. {
  258. this._model._domStorageItemAdded(storageId, key, value);
  259. },
  260. /**
  261. * @param {DOMStorageAgent.StorageId} storageId
  262. * @param {string} key
  263. * @param {string} oldValue
  264. * @param {string} value
  265. */
  266. domStorageItemUpdated: function(storageId, key, oldValue, value)
  267. {
  268. this._model._domStorageItemUpdated(storageId, key, oldValue, value);
  269. },
  270. }
  271. /**
  272. * @type {WebInspector.DOMStorageModel}
  273. */
  274. WebInspector.domStorageModel = null;