2 * Copyright 2004-2007 Aaron Boodman
3 * Portions copyright 2015 Ketmar Dark <ketmar@ketmar.no-ip.org>
4 * Contributors: See contributors list in install.rdf and CREDITS
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * Note that this license applies only to the Greasemonkey extension source
14 * files, not to the user scripts which it runs. User scripts are licensed
15 * separately by their authors.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * The above copyright notice and this permission notice shall be included in all
26 * copies or substantial portions of the Software.
28 ////////////////////////////////////////////////////////////////////////////////
29 function ScriptStorage (nfo
) {
30 this.dbBaseName
= nfo
.name
;
36 ScriptStorage
.prototype.__defineGetter__("dbFile", function () {
37 let file
= getUserDBDir();
38 file
.append(this.dbBaseName
+".db");
43 ScriptStorage
.prototype.__defineGetter__("db", function () {
44 if (this.dbObj
== null) {
45 this.dbObj
= Services
.storage
.openDatabase(this.dbFile
);
46 // the auto_vacuum pragma has to be set before the table is created
47 //this.dbObj.executeSimpleSQL("PRAGMA auto_vacuum = INCREMENTAL;");
48 //this.dbObj.executeSimpleSQL("PRAGMA incremental_vacuum(10);");
49 this.dbObj
.executeSimpleSQL("PRAGMA page_size = 512;");
50 this.dbObj
.executeSimpleSQL("PRAGMA auto_vacuum = NONE;");
51 //this.dbObj.executeSimpleSQL("PRAGMA journal_mode = WAL;");
52 //this.dbObj.executeSimpleSQL("PRAGMA wal_autocheckpoint = 10;");
53 this.dbObj
.executeSimpleSQL("PRAGMA journal_mode = DELETE;"); // will slow down bulk inserts, but IDC
54 this.dbObj
.executeSimpleSQL(
55 "CREATE TABLE IF NOT EXISTS scriptvals ("+
56 " name TEXT PRIMARY KEY NOT NULL"+
60 // run vacuum once manually to switch to the correct auto_vacuum mode for
61 // databases that were created with incorrect auto_vacuum
62 //this.dbObj.executeSimpleSQL("VACUUM;");
68 ScriptStorage
.prototype.__defineGetter__("opened", function () {
69 return (this.dbObj
!= null);
73 ScriptStorage
.prototype.close = function () {
81 ScriptStorage
.prototype.setValue = function (name
, val
) {
82 if (arguments
.length
!= 2) {
83 logError("invalid number of arguments to `GM_setValue()`");
84 throw new Error("invalid number of arguments to `GM_setValue()`");
87 this.deleteValue(name
);
90 //if (name === undefined) return;
91 //if (typeof(name) === "Object") return;
92 let stmt
= this.db
.createStatement("INSERT OR REPLACE INTO scriptvals (name, value) VALUES (:name, :value)");
94 stmt
.params
.name
= name
;
95 stmt
.params
.value
= JSON
.stringify(val
);
103 ScriptStorage
.prototype.getValue = function (name
, defval
) {
104 if (arguments
.length
< 1 || arguments
.length
> 2) {
105 logError("invalid number of arguments to `GM_getValue()`");
106 throw new Error("invalid number of arguments to `GM_getValue()`");
108 if (typeof(defval
) === "undefined") defval
= null;
110 let stmt
= this.db
.createStatement("SELECT value FROM scriptvals WHERE name = :name");
112 stmt
.params
.name
= name
;
113 while (stmt
.step()) value
= stmt
.row
.value
;
115 logException("GM_getValue", e
);
119 if (typeof(value
) !== "string") return defval
;
122 let exposedProps
= "__exposedProps__";
123 value
= JSON
.parse(value
);
124 if (typeof(value
) == "object") {
125 if (!(exposedProps
in value
)) value
[exposedProps
] = {};
126 for (let prop
in value
) {
127 if (prop
!== exposedProps
&& Object
.prototype.hasOwnProperty
.call(value
, prop
)) {
128 value
[exposedProps
][prop
] = "rw"; // there is nothing wrong in making this r/w
134 //logError("JSON parse error: "+e.name+": "+e.message);
140 ScriptStorage
.prototype.deleteValue = function (name
) {
141 if (arguments
.length
!= 1) {
142 logError("invalid number of arguments to `GM_deleteValue()`");
143 throw new Error("invalid number of arguments to `GM_deleteValue()`");
145 let stmt
= this.db
.createStatement("DELETE FROM scriptvals WHERE name = :name");
147 stmt
.params
.name
= name
;
155 ScriptStorage
.prototype.listValues = function () {
156 if (arguments
.length
!= 0) {
157 logError("invalid number of arguments to `GM_listValues()`");
158 throw new Error("invalid number of arguments to `GM_listValues()`");
161 //let eprp = {length: "r"};
164 let stmt
= this.db
.createStatement("SELECT name FROM scriptvals");
166 while (stmt
.executeStep()) {
167 valueNames
.push(stmt
.row
.name
);
168 //vals[count++] = stmt.row.name;
173 //let vals = Array.prototype.slice.call(valueNames);
174 //vals.__exposedProps__ = {length: "r"};
175 //!vals.length = count;
176 //!vals.__exposedProps__ = eprp;
177 let vals
= valueNames
;
182 ////////////////////////////////////////////////////////////////////////////////
183 exports
.ScriptStorage
= ScriptStorage
;