2 const ID_SUFFIX = "@slipperymonkey.fractalbrew.com";
4 var TAGMATCH = /^.*@(\S+)\s*(.*)/
10 var dbfile = Services.dirsvc.get("ProfD", Ci.nsIFile);
11 dbfile.append("slippery.sqlite");
12 this.db = Services.storage.openUnsharedDatabase(dbfile);
13 if (this.db.schemaVersion < DB_SCHEMA)
17 initDatabase: function() {
18 this.db.createTable("script",
19 "id INTEGER PRIMARY KEY AUTOINCREMENT," +
27 this.db.createTable("include",
28 "script_id INTEGER," +
31 this.db.executeSimpleSQL("CREATE TRIGGER delete_script AFTER DELETE ON script BEGIN " +
32 "DELETE FROM include WHERE script_id=old.id; " +
34 this.db.schemaVersion = DB_SCHEMA;
37 getScript: function(aId) {
38 if (aId.substring(aId.length - ID_SUFFIX.length) != ID_SUFFIX)
41 var stmt = this.db.createStatement("SELECT id, name, version, author, description, uri, enabled, script FROM script WHERE id=:id");
42 stmt.params.id = aId.substring(0, aId.length - ID_SUFFIX.length);
43 if (stmt.executeStep())
44 return new Script(stmt.row);
48 getAllScripts: function() {
51 var stmt = this.db.createStatement("SELECT id, name, version, author, description, uri, enabled, script FROM script");
52 while (stmt.executeStep())
53 scripts.push(new Script(stmt.row));
57 getScriptsForURI: function(aURI) {
60 var stmt = this.db.createStatement("SELECT DISTINCT id, name, version, author, description, uri, enabled, script FROM script JOIN include ON script.id=include.script_id WHERE :uri GLOB address AND enabled=1");
61 stmt.params.uri = aURI.spec;
62 while (stmt.executeStep())
63 scripts.push(new Script(stmt.row));
67 hasScript: function(aURI) {
68 var stmt = this.db.createStatement("SELECT COUNT() AS count FROM script WHERE uri=:uri");
69 stmt.params.uri = aURI.spec;
71 if (stmt.executeStep())
72 return stmt.row.count > 0;
76 installScript: function(aScript, aURI) {
87 var stmt = this.db.createStatement("INSERT INTO script VALUES (NULL, :name, :version, :author, :description, :uri, :enabled, :script)");
88 stmt.params.uri = aURI.spec;
89 stmt.params.enabled = 1;
90 stmt.params.script = aScript;
92 var start = aScript.indexOf("==UserScript==");
93 var end = aScript.indexOf("==/UserScript==");
94 if (start < 0 || end < 0)
97 LOG("Looking for tags in " + aScript.substring(start, end));
98 var lines = aScript.substring(start, end).split("\n");
100 while (pos < lines.length) {
101 var matches = TAGMATCH.exec(lines[pos]);
103 LOG("Tag: " + matches[1] + " = " + matches[2]);
104 switch (matches[1]) {
109 stmt.params[matches[1]] = matches[2];
110 data[matches[1]] = matches[2];
113 includes.push(matches[2]);
121 var id = this.db.lastInsertRowID;
123 stmt = this.db.createStatement("INSERT INTO include VALUES (:id, :address)");
124 includes.forEach(function(aInclude) {
126 stmt.params.address = aInclude;
130 AddonManagerPrivate.callInstallListeners("onNewInstall", null,
131 new Script(data), null, false);
134 LOGE("Exception installing script", e);
138 uninstallScript: function(aScript) {
139 AddonManagerPrivate.callAddonListeners("onUninstalling", aScript, false);
140 var stmt = this.db.createStatement("DELETE FROM script WHERE id=:id");
141 stmt.params.id = aScript._id;
143 AddonManagerPrivate.callAddonListeners("onUninstalled", aScript);
146 updateDisabledState: function(aScript) {
147 var stmt = this.db.createStatement("UPDATE script SET enabled=:enabled WHERE id=:id");
148 stmt.params.id = aScript._id;
149 stmt.params.enabled = aScript.enabled;
153 shutdown: function() {
154 this.db.asyncClose();
158 function Script(aData) {
159 this.id = aData.id + ID_SUFFIX;
161 this.name = aData.name;
162 this.version = aData.version;
163 this.creator = aData.author;
164 this.description = aData.description;
165 this.homepageURL = aData.uri;
166 this.enabled = aData.enabled;
167 this.script = aData.script;
176 providesUpdatesSecurely: true,
179 scope: AddonManager.SCOPE_PROFILE,
181 pendingOperations: 0,
192 return this.enabled != 1;
195 set userDisabled(val) {
196 if (val == this.userDisabled)
199 AddonManagerPrivate.callAddonListeners(val ? "onEnabling" : "onDisabling",
201 this.enabled = val ? 0 : 1;
202 ScriptDatabase.updateDisabledState(this);
203 AddonManagerPrivate.callAddonListeners(val ? "onEnabled" : "onDisabled",
208 var perms = AddonManager.PERM_CAN_UNINSTALL;
209 perms |= this.userDisabled ? AddonManager.PERM_CAN_ENABLE : AddonManager.PERM_CAN_DISABLE;
213 isCompatibleWith: function() {
217 findUpdates: function(aListener) {
218 if ("onNoCompatibilityUpdateAvailable" in aListener)
219 aListener.onNoCompatibilityUpdateAvailable(this);
220 if ("onNoUpdateAvailable" in aListener)
221 aListener.onNoUpdateAvailable(this);
222 if ("onUpdateFinished" in aListener)
223 aListener.onUpdateFinished(this);
226 uninstall: function() {
227 ScriptDatabase.uninstallScript(this);
231 ScriptDatabase.startup();