tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

storage.js (6853B)


      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 
      6 const protocol = require("resource://devtools/shared/protocol.js");
      7 const { Arg, RetVal, types } = protocol;
      8 
      9 const childSpecs = {};
     10 
     11 function createStorageSpec(options) {
     12  // common methods for all storage types
     13  const methods = {
     14    getStoreObjects: {
     15      request: {
     16        host: Arg(0),
     17        names: Arg(1, "nullable:array:string"),
     18        options: Arg(2, "nullable:json"),
     19      },
     20      response: RetVal(options.storeObjectType),
     21    },
     22    getFields: {
     23      request: {
     24        subType: Arg(0, "nullable:string"),
     25      },
     26      response: {
     27        value: RetVal("json"),
     28      },
     29    },
     30  };
     31 
     32  // extra methods specific for storage type
     33  Object.assign(methods, options.methods);
     34 
     35  childSpecs[options.typeName] = protocol.generateActorSpec({
     36    typeName: options.typeName,
     37    methods,
     38    events: {
     39      "single-store-update": {
     40        type: "storesUpdate",
     41        data: Arg(0, "storeUpdateObject"),
     42      },
     43      "single-store-cleared": {
     44        type: "storesCleared",
     45        data: Arg(0, "json"),
     46      },
     47    },
     48  });
     49 }
     50 
     51 // Cookies store object
     52 types.addDictType("cookieobject", {
     53  uniqueKey: "string",
     54  name: "string",
     55  value: "longstring",
     56  path: "nullable:string",
     57  host: "string",
     58  hostOnly: "boolean",
     59  isSecure: "boolean",
     60  isHttpOnly: "boolean",
     61  creationTime: "number",
     62  updateTime: "number",
     63  lastAccessed: "number",
     64  expires: "number",
     65 });
     66 
     67 // Array of cookie store objects
     68 types.addDictType("cookiestoreobject", {
     69  total: "number",
     70  offset: "number",
     71  data: "array:nullable:cookieobject",
     72 });
     73 
     74 // Result of add/edit cookie operation: can throw error
     75 types.addDictType("cookieoperationresult", {
     76  errorString: "nullable:string",
     77 });
     78 
     79 // Common methods for edit/remove
     80 const editRemoveMethods = {
     81  getFields: {
     82    request: {},
     83    response: {
     84      value: RetVal("json"),
     85    },
     86  },
     87  editItem: {
     88    request: {
     89      data: Arg(0, "json"),
     90    },
     91    response: {},
     92  },
     93  removeItem: {
     94    request: {
     95      host: Arg(0, "string"),
     96      name: Arg(1, "string"),
     97    },
     98    response: {},
     99  },
    100 };
    101 
    102 // Cookies actor spec
    103 createStorageSpec({
    104  typeName: "cookies",
    105  storeObjectType: "cookiestoreobject",
    106  methods: Object.assign(
    107    {},
    108    editRemoveMethods,
    109    {
    110      addItem: {
    111        request: {
    112          guid: Arg(0, "string"),
    113          host: Arg(1, "nullable:string"),
    114        },
    115        response: RetVal("cookieoperationresult"),
    116      },
    117    },
    118    {
    119      editItem: {
    120        request: editRemoveMethods.editItem.request,
    121        response: RetVal("cookieoperationresult"),
    122      },
    123    },
    124    {
    125      removeAll: {
    126        request: {
    127          host: Arg(0, "string"),
    128          domain: Arg(1, "nullable:string"),
    129        },
    130        response: {},
    131      },
    132    },
    133    {
    134      removeAllSessionCookies: {
    135        request: {
    136          host: Arg(0, "string"),
    137          domain: Arg(1, "nullable:string"),
    138        },
    139        response: {},
    140      },
    141    }
    142  ),
    143 });
    144 
    145 // Local Storage / Session Storage store object
    146 types.addDictType("storageobject", {
    147  name: "string",
    148  value: "longstring",
    149 });
    150 
    151 // Common methods for local/session storage
    152 const storageMethods = Object.assign(
    153  {},
    154  editRemoveMethods,
    155  {
    156    addItem: {
    157      request: {
    158        guid: Arg(0, "string"),
    159        host: Arg(1, "nullable:string"),
    160      },
    161      response: {},
    162    },
    163  },
    164  {
    165    removeAll: {
    166      request: {
    167        host: Arg(0, "string"),
    168      },
    169      response: {},
    170    },
    171  }
    172 );
    173 
    174 // Array of Local Storage / Session Storage store objects
    175 types.addDictType("storagestoreobject", {
    176  total: "number",
    177  offset: "number",
    178  data: "array:nullable:storageobject",
    179 });
    180 
    181 createStorageSpec({
    182  typeName: "localStorage",
    183  storeObjectType: "storagestoreobject",
    184  methods: storageMethods,
    185 });
    186 
    187 createStorageSpec({
    188  typeName: "sessionStorage",
    189  storeObjectType: "storagestoreobject",
    190  methods: storageMethods,
    191 });
    192 
    193 types.addDictType("extensionobject", {
    194  name: "nullable:string",
    195  value: "nullable:longstring",
    196  area: "string",
    197  isValueEditable: "boolean",
    198 });
    199 
    200 types.addDictType("extensionstoreobject", {
    201  total: "number",
    202  offset: "number",
    203  data: "array:nullable:extensionobject",
    204 });
    205 
    206 createStorageSpec({
    207  typeName: "extensionStorage",
    208  storeObjectType: "extensionstoreobject",
    209  // Same as storageMethods except for addItem
    210  methods: Object.assign({}, editRemoveMethods, {
    211    removeAll: {
    212      request: {
    213        host: Arg(0, "string"),
    214      },
    215      response: {},
    216    },
    217  }),
    218 });
    219 
    220 types.addDictType("cacheobject", {
    221  url: "string",
    222  status: "string",
    223 });
    224 
    225 // Array of Cache store objects
    226 types.addDictType("cachestoreobject", {
    227  total: "number",
    228  offset: "number",
    229  data: "array:nullable:cacheobject",
    230 });
    231 
    232 // Cache storage spec
    233 createStorageSpec({
    234  typeName: "Cache",
    235  storeObjectType: "cachestoreobject",
    236  methods: {
    237    removeAll: {
    238      request: {
    239        host: Arg(0, "string"),
    240        name: Arg(1, "string"),
    241      },
    242      response: {},
    243    },
    244    removeItem: {
    245      request: {
    246        host: Arg(0, "string"),
    247        name: Arg(1, "string"),
    248      },
    249      response: {},
    250    },
    251  },
    252 });
    253 
    254 // Indexed DB store object
    255 // This is a union on idb object, db metadata object and object store metadata
    256 // object
    257 types.addDictType("idbobject", {
    258  uniqueKey: "string",
    259  name: "nullable:string",
    260  db: "nullable:string",
    261  objectStore: "nullable:string",
    262  origin: "nullable:string",
    263  version: "nullable:number",
    264  storage: "nullable:string",
    265  objectStores: "nullable:number",
    266  keyPath: "nullable:string",
    267  autoIncrement: "nullable:boolean",
    268  indexes: "nullable:string",
    269  value: "nullable:longstring",
    270 });
    271 
    272 // Array of Indexed DB store objects
    273 types.addDictType("idbstoreobject", {
    274  total: "number",
    275  offset: "number",
    276  data: "array:nullable:idbobject",
    277 });
    278 
    279 // Result of Indexed DB delete operation: can block or throw error
    280 types.addDictType("idbdeleteresult", {
    281  blocked: "nullable:boolean",
    282  error: "nullable:string",
    283 });
    284 
    285 createStorageSpec({
    286  typeName: "indexedDB",
    287  storeObjectType: "idbstoreobject",
    288  methods: {
    289    removeDatabase: {
    290      request: {
    291        host: Arg(0, "string"),
    292        name: Arg(1, "string"),
    293      },
    294      response: RetVal("idbdeleteresult"),
    295    },
    296    removeAll: {
    297      request: {
    298        host: Arg(0, "string"),
    299        name: Arg(1, "string"),
    300      },
    301      response: {},
    302    },
    303    removeItem: {
    304      request: {
    305        host: Arg(0, "string"),
    306        name: Arg(1, "string"),
    307      },
    308      response: {},
    309    },
    310  },
    311 });
    312 
    313 // Update notification object
    314 types.addDictType("storeUpdateObject", {
    315  changed: "nullable:json",
    316  deleted: "nullable:json",
    317  added: "nullable:json",
    318 });
    319 
    320 exports.childSpecs = childSpecs;