storage.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. const protocol = require("devtools/shared/protocol");
  6. const { Arg, RetVal, types } = protocol;
  7. let childSpecs = {};
  8. function createStorageSpec(options) {
  9. // common methods for all storage types
  10. let methods = {
  11. getStoreObjects: {
  12. request: {
  13. host: Arg(0),
  14. names: Arg(1, "nullable:array:string"),
  15. options: Arg(2, "nullable:json")
  16. },
  17. response: RetVal(options.storeObjectType)
  18. },
  19. getFields: {
  20. request: {
  21. subType: Arg(0, "nullable:string")
  22. },
  23. response: {
  24. value: RetVal("json")
  25. }
  26. }
  27. };
  28. // extra methods specific for storage type
  29. Object.assign(methods, options.methods);
  30. childSpecs[options.typeName] = protocol.generateActorSpec({
  31. typeName: options.typeName,
  32. methods
  33. });
  34. }
  35. // Cookies store object
  36. types.addDictType("cookieobject", {
  37. uniqueKey: "string",
  38. name: "string",
  39. value: "longstring",
  40. path: "nullable:string",
  41. host: "string",
  42. isDomain: "boolean",
  43. isSecure: "boolean",
  44. isHttpOnly: "boolean",
  45. creationTime: "number",
  46. lastAccessed: "number",
  47. expires: "number"
  48. });
  49. // Array of cookie store objects
  50. types.addDictType("cookiestoreobject", {
  51. total: "number",
  52. offset: "number",
  53. data: "array:nullable:cookieobject"
  54. });
  55. // Common methods for edit/remove
  56. const editRemoveMethods = {
  57. getFields: {
  58. request: {},
  59. response: {
  60. value: RetVal("json")
  61. }
  62. },
  63. editItem: {
  64. request: {
  65. data: Arg(0, "json"),
  66. },
  67. response: {}
  68. },
  69. removeItem: {
  70. request: {
  71. host: Arg(0, "string"),
  72. name: Arg(1, "string"),
  73. },
  74. response: {}
  75. },
  76. };
  77. // Cookies actor spec
  78. createStorageSpec({
  79. typeName: "cookies",
  80. storeObjectType: "cookiestoreobject",
  81. methods: Object.assign({},
  82. editRemoveMethods,
  83. {
  84. addItem: {
  85. request: {
  86. guid: Arg(0, "string"),
  87. },
  88. response: {}
  89. }
  90. }, {
  91. removeAll: {
  92. request: {
  93. host: Arg(0, "string"),
  94. domain: Arg(1, "nullable:string")
  95. },
  96. response: {}
  97. }
  98. }, {
  99. removeAllSessionCookies: {
  100. request: {
  101. host: Arg(0, "string"),
  102. domain: Arg(1, "nullable:string")
  103. },
  104. response: {}
  105. }
  106. }
  107. )
  108. });
  109. // Local Storage / Session Storage store object
  110. types.addDictType("storageobject", {
  111. name: "string",
  112. value: "longstring"
  113. });
  114. // Common methods for local/session storage
  115. const storageMethods = Object.assign({},
  116. editRemoveMethods,
  117. {
  118. addItem: {
  119. request: {
  120. guid: Arg(0, "string"),
  121. host: Arg(1, "nullable:string")
  122. },
  123. response: {}
  124. }
  125. }, {
  126. removeAll: {
  127. request: {
  128. host: Arg(0, "string")
  129. },
  130. response: {}
  131. }
  132. }
  133. );
  134. // Array of Local Storage / Session Storage store objects
  135. types.addDictType("storagestoreobject", {
  136. total: "number",
  137. offset: "number",
  138. data: "array:nullable:storageobject"
  139. });
  140. createStorageSpec({
  141. typeName: "localStorage",
  142. storeObjectType: "storagestoreobject",
  143. methods: storageMethods
  144. });
  145. createStorageSpec({
  146. typeName: "sessionStorage",
  147. storeObjectType: "storagestoreobject",
  148. methods: storageMethods
  149. });
  150. types.addDictType("cacheobject", {
  151. "url": "string",
  152. "status": "string"
  153. });
  154. // Array of Cache store objects
  155. types.addDictType("cachestoreobject", {
  156. total: "number",
  157. offset: "number",
  158. data: "array:nullable:cacheobject"
  159. });
  160. // Cache storage spec
  161. createStorageSpec({
  162. typeName: "Cache",
  163. storeObjectType: "cachestoreobject",
  164. methods: {
  165. removeAll: {
  166. request: {
  167. host: Arg(0, "string"),
  168. name: Arg(1, "string"),
  169. },
  170. response: {}
  171. },
  172. removeItem: {
  173. request: {
  174. host: Arg(0, "string"),
  175. name: Arg(1, "string"),
  176. },
  177. response: {}
  178. },
  179. }
  180. });
  181. // Indexed DB store object
  182. // This is a union on idb object, db metadata object and object store metadata
  183. // object
  184. types.addDictType("idbobject", {
  185. uniqueKey: "string",
  186. name: "nullable:string",
  187. db: "nullable:string",
  188. objectStore: "nullable:string",
  189. origin: "nullable:string",
  190. version: "nullable:number",
  191. storage: "nullable:string",
  192. objectStores: "nullable:number",
  193. keyPath: "nullable:string",
  194. autoIncrement: "nullable:boolean",
  195. indexes: "nullable:string",
  196. value: "nullable:longstring"
  197. });
  198. // Array of Indexed DB store objects
  199. types.addDictType("idbstoreobject", {
  200. total: "number",
  201. offset: "number",
  202. data: "array:nullable:idbobject"
  203. });
  204. // Result of Indexed DB delete operation: can block or throw error
  205. types.addDictType("idbdeleteresult", {
  206. blocked: "nullable:boolean",
  207. error: "nullable:string"
  208. });
  209. createStorageSpec({
  210. typeName: "indexedDB",
  211. storeObjectType: "idbstoreobject",
  212. methods: {
  213. removeDatabase: {
  214. request: {
  215. host: Arg(0, "string"),
  216. name: Arg(1, "string"),
  217. },
  218. response: RetVal("idbdeleteresult")
  219. },
  220. removeAll: {
  221. request: {
  222. host: Arg(0, "string"),
  223. name: Arg(1, "string"),
  224. },
  225. response: {}
  226. },
  227. removeItem: {
  228. request: {
  229. host: Arg(0, "string"),
  230. name: Arg(1, "string"),
  231. },
  232. response: {}
  233. },
  234. }
  235. });
  236. // Update notification object
  237. types.addDictType("storeUpdateObject", {
  238. changed: "nullable:json",
  239. deleted: "nullable:json",
  240. added: "nullable:json"
  241. });
  242. // Generate a type definition for an object with actors for all storage types.
  243. types.addDictType("storelist", Object.keys(childSpecs).reduce((obj, type) => {
  244. obj[type] = type;
  245. return obj;
  246. }, {}));
  247. exports.childSpecs = childSpecs;
  248. exports.storageSpec = protocol.generateActorSpec({
  249. typeName: "storage",
  250. /**
  251. * List of event notifications that the server can send to the client.
  252. *
  253. * - stores-update : When any store object in any storage type changes.
  254. * - stores-cleared : When all the store objects are removed.
  255. * - stores-reloaded : When all stores are reloaded. This generally mean that
  256. * we should refetch everything again.
  257. */
  258. events: {
  259. "stores-update": {
  260. type: "storesUpdate",
  261. data: Arg(0, "storeUpdateObject")
  262. },
  263. "stores-cleared": {
  264. type: "storesCleared",
  265. data: Arg(0, "json")
  266. },
  267. "stores-reloaded": {
  268. type: "storesReloaded",
  269. data: Arg(0, "json")
  270. }
  271. },
  272. methods: {
  273. listStores: {
  274. request: {},
  275. response: RetVal("storelist")
  276. },
  277. }
  278. });