forked from stylish-userstyles/stylish
/
stylishDataSource.js
134 lines (121 loc) · 5.47 KB
/
stylishDataSource.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"use strict";
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
function StylishDataSource() {}
StylishDataSource.prototype = {
/*
nsISupports
*/
QueryInterface: XPCOMUtils.generateQI([Components.interfaces.stylishDataSource, Components.interfaces.nsIClassInfo, Components.interfaces.nsISupports]),
/*
nsIClassInfo
*/
getInterfaces: function getInterfaces(aCount) {
var interfaces = [Components.interfaces.stylishDataSource, Components.interfaces.nsIClassInfo, Components.interfaces.nsISupports];
aCount.value = interfaces.length;
return interfaces;
},
getHelperForLanguage: function getHelperForLanguage(aLanguage) {
return null;
},
classDescription: "Stylem Data Source",
classID: Components.ID("{034c0c63-b55e-4022-a291-a542db4ea930}"),
contractID: "@stylem.ext/stylem-data-source;1",
implementationLanguage: Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT,
flags: 0,
alreadyComplained: false,
/*
stylishDataSource
*/
getConnection: function() {
var storageService = Components.classes["@mozilla.org/storage/service;1"].getService(Components.interfaces.mozIStorageService);
try {
//xxx what about uris?
var connection = storageService.openDatabase(this.getFile());
} catch (ex) {
if (!this.alreadyComplained) {
this.alreadyComplained = true;
var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService)
promptService.alert(null, 'Problem with Stylem', 'Stylem is having problems opening its database. It will be non-functional until this problem is fixed. See http://userstyles.org/help/db for help.');
}
throw ex;
}
this.migrate(connection);
return connection;
},
getFile: function() {
if (!this._file) {
var path = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch).getCharPref("extensions.stylem.dbFile");
if (path) {
this._file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsIFile);
this._file.initWithPath(path);
} else {
this._file = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile);
this._file.append("stylish.sqlite");
}
}
return this._file;
},
/*
private
*/
_file: null,
migrate: function(connection) {
var expectedDataVersion = 7;
var currentDataVersion = connection.schemaVersion;
if (currentDataVersion >= expectedDataVersion)
return;
connection.beginTransaction();
switch (currentDataVersion) {
case 0:
connection.executeSimpleSQL("DROP TABLE IF EXISTS styles; CREATE TABLE styles (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, url TEXT, updateUrl TEXT, md5Url TEXT, name TEXT NOT NULL, code TEXT NOT NULL, enabled INTEGER NOT NULL);");
connection.executeSimpleSQL("DROP TABLE IF EXISTS style_meta; CREATE TABLE style_meta (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, style_id INTEGER NOT NULL, name TEXT NOT NULL, value TEXT NOT NULL);");
connection.executeSimpleSQL("DROP INDEX IF EXISTS style_meta_style_id; CREATE INDEX style_meta_style_id ON style_meta (style_id);");
case 1:
try {
connection.executeSimpleSQL("ALTER TABLE styles ADD COLUMN originalCode TEXT NULL;");
} catch (ex) {
// this can happen if the user downgrades to a version with schema 1 then upgrades. they will then already have the column.
Components.utils.reportError("Error on migrate version 1 - " + ex);
}
case 2:
try {
connection.executeSimpleSQL("ALTER TABLE styles ADD COLUMN idUrl TEXT NULL; UPDATE styles SET idUrl = url;");
} catch (ex) {
Components.utils.reportError("Error on migrate version 2 - " + ex);
}
case 3:
try {
connection.executeSimpleSQL("UPDATE styles SET md5Url = REPLACE(md5Url, 'http://userstyles.org/styles/', 'http://update.userstyles.org/') WHERE md5Url LIKE 'http://userstyles.org/styles/%.md5';");
} catch (ex) {
Components.utils.reportError("Error on migrate version 3 - " + ex);
}
case 4:
try {
connection.executeSimpleSQL("ALTER TABLE styles ADD COLUMN applyBackgroundUpdates INTEGER NOT NULL DEFAULT 1;"); // 1 = AddonManager.AUTOUPDATE_DEFAULT
} catch (ex) {
Components.utils.reportError("Error on migrate version 4 - " + ex);
}
case 5:
try {
connection.executeSimpleSQL("ALTER TABLE styles ADD COLUMN originalMd5 TEXT NULL;");
} catch (ex) {
Components.utils.reportError("Error on migrate version 5 - " + ex);
}
case 6:
try {
connection.executeSimpleSQL("UPDATE styles SET url = REPLACE(url, 'http://userstyles.org/', 'https://userstyles.org/') WHERE url LIKE 'http://userstyles.org/%';");
connection.executeSimpleSQL("UPDATE styles SET updateUrl = REPLACE(updateUrl, 'http://userstyles.org/', 'https://userstyles.org/') WHERE updateUrl LIKE 'http://userstyles.org/%';");
connection.executeSimpleSQL("UPDATE styles SET md5Url = REPLACE(md5Url, 'http://update.userstyles.org/', 'https://update.userstyles.org/') WHERE md5Url LIKE 'http://update.userstyles.org/%';");
} catch (ex) {
Components.utils.reportError("Error on migrate version 6 - " + ex);
}
}
connection.schemaVersion = expectedDataVersion;
connection.commitTransaction();
}
};
var components = [StylishDataSource];
if (XPCOMUtils.generateNSGetFactory)
var NSGetFactory = XPCOMUtils.generateNSGetFactory(components);
else
var NSGetModule = XPCOMUtils.generateNSGetModule(components);