Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | const {Cc,Ci,Cu} = require("chrome"); |
michael@0 | 2 | const ObservableObject = require("devtools/shared/observable-object"); |
michael@0 | 3 | const promise = require("devtools/toolkit/deprecated-sync-thenables"); |
michael@0 | 4 | |
michael@0 | 5 | const {EventEmitter} = Cu.import("resource://gre/modules/devtools/event-emitter.js"); |
michael@0 | 6 | const {generateUUID} = Cc['@mozilla.org/uuid-generator;1'].getService(Ci.nsIUUIDGenerator); |
michael@0 | 7 | |
michael@0 | 8 | /** |
michael@0 | 9 | * IndexedDB wrapper that just save project objects |
michael@0 | 10 | * |
michael@0 | 11 | * The only constraint is that project objects have to have |
michael@0 | 12 | * a unique `location` object. |
michael@0 | 13 | */ |
michael@0 | 14 | |
michael@0 | 15 | const global = this; |
michael@0 | 16 | const IDB = { |
michael@0 | 17 | _db: null, |
michael@0 | 18 | |
michael@0 | 19 | open: function () { |
michael@0 | 20 | let deferred = promise.defer(); |
michael@0 | 21 | |
michael@0 | 22 | var idbManager = Cc["@mozilla.org/dom/indexeddb/manager;1"] |
michael@0 | 23 | .getService(Ci.nsIIndexedDatabaseManager); |
michael@0 | 24 | idbManager.initWindowless(global); |
michael@0 | 25 | |
michael@0 | 26 | let request = global.indexedDB.open("AppProjects", 5); |
michael@0 | 27 | request.onerror = function(event) { |
michael@0 | 28 | deferred.reject("Unable to open AppProjects indexedDB. " + |
michael@0 | 29 | "Error code: " + event.target.errorCode); |
michael@0 | 30 | }; |
michael@0 | 31 | request.onupgradeneeded = function(event) { |
michael@0 | 32 | let db = event.target.result; |
michael@0 | 33 | db.createObjectStore("projects", { keyPath: "location" }); |
michael@0 | 34 | }; |
michael@0 | 35 | |
michael@0 | 36 | request.onsuccess = function() { |
michael@0 | 37 | let db = IDB._db = request.result; |
michael@0 | 38 | let objectStore = db.transaction("projects").objectStore("projects"); |
michael@0 | 39 | let projects = [] |
michael@0 | 40 | objectStore.openCursor().onsuccess = function(event) { |
michael@0 | 41 | let cursor = event.target.result; |
michael@0 | 42 | if (cursor) { |
michael@0 | 43 | if (cursor.value.location) { |
michael@0 | 44 | // We need to make sure this object has a `.location` property. |
michael@0 | 45 | // The UI depends on this property. |
michael@0 | 46 | // This should not be needed as we make sure to register valid |
michael@0 | 47 | // projects, but in the past (before bug 924568), we might have |
michael@0 | 48 | // registered invalid objects. |
michael@0 | 49 | projects.push(cursor.value); |
michael@0 | 50 | } |
michael@0 | 51 | cursor.continue(); |
michael@0 | 52 | } else { |
michael@0 | 53 | deferred.resolve(projects); |
michael@0 | 54 | } |
michael@0 | 55 | }; |
michael@0 | 56 | }; |
michael@0 | 57 | |
michael@0 | 58 | return deferred.promise; |
michael@0 | 59 | }, |
michael@0 | 60 | |
michael@0 | 61 | add: function(project) { |
michael@0 | 62 | let deferred = promise.defer(); |
michael@0 | 63 | |
michael@0 | 64 | if (!project.location) { |
michael@0 | 65 | // We need to make sure this object has a `.location` property. |
michael@0 | 66 | deferred.reject("Missing location property on project object."); |
michael@0 | 67 | } else { |
michael@0 | 68 | let transaction = IDB._db.transaction(["projects"], "readwrite"); |
michael@0 | 69 | let objectStore = transaction.objectStore("projects"); |
michael@0 | 70 | let request = objectStore.add(project); |
michael@0 | 71 | request.onerror = function(event) { |
michael@0 | 72 | deferred.reject("Unable to add project to the AppProjects indexedDB: " + |
michael@0 | 73 | this.error.name + " - " + this.error.message ); |
michael@0 | 74 | }; |
michael@0 | 75 | request.onsuccess = function() { |
michael@0 | 76 | deferred.resolve(); |
michael@0 | 77 | }; |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | return deferred.promise; |
michael@0 | 81 | }, |
michael@0 | 82 | |
michael@0 | 83 | update: function(project) { |
michael@0 | 84 | let deferred = promise.defer(); |
michael@0 | 85 | |
michael@0 | 86 | var transaction = IDB._db.transaction(["projects"], "readwrite"); |
michael@0 | 87 | var objectStore = transaction.objectStore("projects"); |
michael@0 | 88 | var request = objectStore.put(project); |
michael@0 | 89 | request.onerror = function(event) { |
michael@0 | 90 | deferred.reject("Unable to update project to the AppProjects indexedDB: " + |
michael@0 | 91 | this.error.name + " - " + this.error.message ); |
michael@0 | 92 | }; |
michael@0 | 93 | request.onsuccess = function() { |
michael@0 | 94 | deferred.resolve(); |
michael@0 | 95 | }; |
michael@0 | 96 | |
michael@0 | 97 | return deferred.promise; |
michael@0 | 98 | }, |
michael@0 | 99 | |
michael@0 | 100 | remove: function(location) { |
michael@0 | 101 | let deferred = promise.defer(); |
michael@0 | 102 | |
michael@0 | 103 | let request = IDB._db.transaction(["projects"], "readwrite") |
michael@0 | 104 | .objectStore("projects") |
michael@0 | 105 | .delete(location); |
michael@0 | 106 | request.onsuccess = function(event) { |
michael@0 | 107 | deferred.resolve(); |
michael@0 | 108 | }; |
michael@0 | 109 | request.onerror = function() { |
michael@0 | 110 | deferred.reject("Unable to delete project to the AppProjects indexedDB: " + |
michael@0 | 111 | this.error.name + " - " + this.error.message ); |
michael@0 | 112 | }; |
michael@0 | 113 | |
michael@0 | 114 | return deferred.promise; |
michael@0 | 115 | } |
michael@0 | 116 | }; |
michael@0 | 117 | |
michael@0 | 118 | const store = new ObservableObject({ projects:[] }); |
michael@0 | 119 | |
michael@0 | 120 | let loadDeferred = promise.defer(); |
michael@0 | 121 | |
michael@0 | 122 | IDB.open().then(function (projects) { |
michael@0 | 123 | store.object.projects = projects; |
michael@0 | 124 | AppProjects.emit("ready", store.object.projects); |
michael@0 | 125 | loadDeferred.resolve(); |
michael@0 | 126 | }); |
michael@0 | 127 | |
michael@0 | 128 | const AppProjects = { |
michael@0 | 129 | load: function() { |
michael@0 | 130 | return loadDeferred.promise; |
michael@0 | 131 | }, |
michael@0 | 132 | |
michael@0 | 133 | addPackaged: function(folder) { |
michael@0 | 134 | let project = { |
michael@0 | 135 | type: "packaged", |
michael@0 | 136 | location: folder.path, |
michael@0 | 137 | // We need a unique id, that is the app origin, |
michael@0 | 138 | // in order to identify the app when being installed on the device. |
michael@0 | 139 | // The packaged app local path is a valid id, but only on the client. |
michael@0 | 140 | // This origin will be used to generate the true id of an app: |
michael@0 | 141 | // its manifest URL. |
michael@0 | 142 | // If the app ends up specifying an explicit origin in its manifest, |
michael@0 | 143 | // we will override this random UUID on app install. |
michael@0 | 144 | packagedAppOrigin: generateUUID().toString().slice(1, -1) |
michael@0 | 145 | }; |
michael@0 | 146 | return IDB.add(project).then(function () { |
michael@0 | 147 | store.object.projects.push(project); |
michael@0 | 148 | // return the added objects (proxified) |
michael@0 | 149 | return store.object.projects[store.object.projects.length - 1]; |
michael@0 | 150 | }); |
michael@0 | 151 | }, |
michael@0 | 152 | |
michael@0 | 153 | addHosted: function(manifestURL) { |
michael@0 | 154 | let project = { |
michael@0 | 155 | type: "hosted", |
michael@0 | 156 | location: manifestURL |
michael@0 | 157 | }; |
michael@0 | 158 | return IDB.add(project).then(function () { |
michael@0 | 159 | store.object.projects.push(project); |
michael@0 | 160 | // return the added objects (proxified) |
michael@0 | 161 | return store.object.projects[store.object.projects.length - 1]; |
michael@0 | 162 | }); |
michael@0 | 163 | }, |
michael@0 | 164 | |
michael@0 | 165 | update: function (project) { |
michael@0 | 166 | return IDB.update({ |
michael@0 | 167 | type: project.type, |
michael@0 | 168 | location: project.location, |
michael@0 | 169 | packagedAppOrigin: project.packagedAppOrigin |
michael@0 | 170 | }).then(() => project); |
michael@0 | 171 | }, |
michael@0 | 172 | |
michael@0 | 173 | remove: function(location) { |
michael@0 | 174 | return IDB.remove(location).then(function () { |
michael@0 | 175 | let projects = store.object.projects; |
michael@0 | 176 | for (let i = 0; i < projects.length; i++) { |
michael@0 | 177 | if (projects[i].location == location) { |
michael@0 | 178 | projects.splice(i, 1); |
michael@0 | 179 | return; |
michael@0 | 180 | } |
michael@0 | 181 | } |
michael@0 | 182 | throw new Error("Unable to find project in AppProjects store"); |
michael@0 | 183 | }); |
michael@0 | 184 | }, |
michael@0 | 185 | |
michael@0 | 186 | get: function(location) { |
michael@0 | 187 | let projects = store.object.projects; |
michael@0 | 188 | for (let i = 0; i < projects.length; i++) { |
michael@0 | 189 | if (projects[i].location == location) { |
michael@0 | 190 | return projects[i]; |
michael@0 | 191 | } |
michael@0 | 192 | } |
michael@0 | 193 | return null; |
michael@0 | 194 | }, |
michael@0 | 195 | |
michael@0 | 196 | store: store |
michael@0 | 197 | }; |
michael@0 | 198 | |
michael@0 | 199 | EventEmitter.decorate(AppProjects); |
michael@0 | 200 | |
michael@0 | 201 | exports.AppProjects = AppProjects; |
michael@0 | 202 |