mozilla
Your Search Results

    Using IndexedDB in chrome

    The nsIIndexedDatabaseManager was removed in Firefox 33. (Please refer to bug 1019194.)

    IndexedDB is typically used to store data in the user's browser from content JavaScript.  (See Using IndexedDB for an overview.)  But the APIs can be also be accessed from chrome via the initWindowless method of the IndexedDB manager service.

    You need simply provide an object for the service to bind to.  For example:

    let DB_NAME = "my-database";
    let STORE_NAME = "my-store";
    let dbContext = this;
    
    function MyService() {
      this._db = null;
    }
    
    MyService.prototype = {
      open: function MyService_open(successCB, errorCB) {
    
        let idbManager = Components.classes["@mozilla.org/dom/indexeddb/manager;1"]
            .getService(Components.interfaces.nsIIndexedDatabaseManager);
    
        // Bind the service
        idbManager.initWindowless(dbContext);
    
        // From here on, it's like using IndexedDB from JavaScript content
        let req = dbContext.indexedDB.open(DB_NAME);
    
        req.onupgradeneeded = function(event) {
          let db = event.target.result;
          db.createObjectStore(STORE_NAME, { /* declare schema on first run */ });
        };
    
        req.onerror = function() {
          errorCB(req.errorCode);
        };
    
        req.onsuccess = function(event) {
          this._db = req.result;
          successCB("ready");
        }.bind(this);
      },
    
      // etc ...
    
    };

    Resources

    Document Tags and Contributors

    Contributors to this page: evold, jedp, chrisdavidmills
    Last updated by: evold,