1 // Copyright (c) 2008-2013 Kris Maglione <maglione.k at Gmail>
3 // This work is licensed for reuse under an MIT license. Details are
4 // given in the LICENSE.txt file included with this file.
7 defineModule("storage", {
8 exports: ["File", "Storage", "storage"],
9 require: ["services", "util"]
12 lazyRequire("config", ["config"]);
13 lazyRequire("io", ["IO"]);
14 lazyRequire("overlay", ["overlay"]);
16 var win32 = /^win(32|nt)$/i.test(services.runtime.OS);
17 var myObject = JSON.parse("{}").constructor;
19 var StoreBase = Class("StoreBase", {
20 OPTIONS: ["privateData", "replacer"],
22 fireEvent: function (event, arg) { storage.fireEvent(this.name, event, arg); },
24 get serial() JSON.stringify(this._object, this.replacer),
26 init: function (name, store, load, options) {
28 this._options = options;
30 this.__defineGetter__("store", () => store);
31 this.__defineGetter__("name", () => name);
32 for (let [k, v] in Iterator(options))
33 if (this.OPTIONS.indexOf(k) >= 0)
38 clone: function (storage) {
39 let store = storage.privateMode ? false : this.store;
40 let res = this.constructor(this.name, store, this._load, this._options);
41 res.storage = storage;
45 changed: function () { this.timer && this.timer.tell(); },
47 reload: function reload() {
48 this._object = this._load() || this._constructor();
49 this.fireEvent("change", null);
52 delete: function delete_() {
53 delete storage.keys[this.name];
54 delete storage[this.name];
55 storage.infoPath.child(this.name).remove(false);
58 save: function () { (self.storage || storage)._saveData(this); },
60 __iterator__: function () Iterator(this._object)
63 var ArrayStore = Class("ArrayStore", StoreBase, {
66 get length() this._object.length,
68 set: function set(index, value, quiet) {
69 var orig = this._object[index];
70 this._object[index] = value;
72 this.fireEvent("change", index);
77 push: function push(value) {
78 this._object.push(value);
79 this.fireEvent("push", this._object.length);
82 pop: function pop(value, ord) {
84 var res = this._object.pop();
86 res = this._object.splice(ord, 1)[0];
88 this.fireEvent("pop", this._object.length, ord);
92 shift: function shift(value) {
93 var res = this._object.shift();
94 this.fireEvent("shift", this._object.length);
98 insert: function insert(value, ord) {
100 this._object.unshift(value);
102 this._object = this._object.slice(0, ord)
104 .concat(this._object.slice(ord));
105 this.fireEvent("insert", this._object.length, ord);
108 truncate: function truncate(length, fromEnd) {
109 var res = this._object.length;
110 if (this._object.length > length) {
112 this._object.splice(0, this._object.length - length);
113 this._object.length = length;
114 this.fireEvent("truncate", length);
120 mutate: function mutate(funcName) {
121 var _funcName = funcName;
122 arguments[0] = this._object;
123 this._object = Array[_funcName].apply(Array, arguments);
124 this.fireEvent("change", null);
127 get: function get(index) index >= 0 ? this._object[index] : this._object[this._object.length + index]
130 var ObjectStore = Class("ObjectStore", StoreBase, {
131 _constructor: myObject,
135 this.fireEvent("clear");
138 get: function get(key, default_) {
139 return key in this._object ? this._object[key] :
140 arguments.length > 1 ? this.set(key, default_) :
144 keys: function keys() Object.keys(this._object),
146 remove: function remove(key) {
147 var res = this._object[key];
148 delete this._object[key];
149 this.fireEvent("remove", key);
153 set: function set(key, val) {
154 var defined = key in this._object;
155 var orig = this._object[key];
156 this._object[key] = val;
158 this.fireEvent("add", key);
159 else if (orig != val)
160 this.fireEvent("change", key);
165 var sessionGlobal = Cu.import("resource://gre/modules/Services.jsm", {});
167 var Storage = Module("Storage", {
168 Local: function Local(dactyl, modules, window) ({
169 init: function init() {
170 this.privateMode = PrivateBrowsingUtils.isWindowPrivate(window);
176 init: function init() {
179 let { Services } = Cu.import("resource://gre/modules/Services.jsm", {});
180 if (!Services.dactylSession)
181 Services.dactylSession = Cu.createObjectIn(sessionGlobal);
182 this.session = Services.dactylSession;
185 cleanup: function () {
188 for (let key in keys(this.keys)) {
190 this[key].timer.flush();
198 _loadData: function loadData(name, store, type) {
200 let file = storage.infoPath.child(name);
202 let data = file.read();
203 let result = JSON.parse(data);
204 if (result instanceof type)
213 _saveData: function saveData(obj) {
214 if (obj.privateData && storage.privateMode)
216 if (obj.store && storage.infoPath)
217 storage.infoPath.child(obj.name).write(obj.serial);
220 storeForSession: function storeForSession(key, val) {
222 this.session[key] = sessionGlobal.JSON.parse(JSON.stringify(val));
224 delete this.dactylSession[key];
227 infoPath: Class.Memoize(() =>
228 File(IO.runtimePath.replace(/,.*/, ""))
229 .child("info").child(config.profileName)),
231 exists: function exists(key) this.infoPath.child(key).exists(),
233 remove: function remove(key) {
234 if (this.exists(key)) {
235 if (this[key] && this[key].timer)
236 this[key].timer.flush();
238 delete this.keys[key];
239 this.infoPath.child(key).remove(false);
243 newObject: function newObject(key, constructor, params) {
244 if (params == null || !isObject(params))
245 throw Error("Invalid argument type");
247 if (this.isLocalModule) {
248 this.globalInstance.newObject.apply(this.globalInstance, arguments);
250 if (!(key in this.keys) && this.privateMode && key in this.globalInstance.keys) {
251 let obj = this.globalInstance.keys[key];
252 this.keys[key] = this._privatize(obj);
255 return this.keys[key];
258 let reload = params.reload || this.alwaysReload[key];
259 if (!(key in this.keys) || reload) {
260 if (key in this && !reload)
261 throw Error("Cannot add storage key with that name.");
263 let load = () => this._loadData(key, params.store, params.type || myObject);
265 this.keys[key] = new constructor(key, params.store, load, params);
266 this.keys[key].timer = new Timer(1000, 10000, () => this.save(key));
267 this.__defineGetter__(key, function () this.keys[key]);
269 return this.keys[key];
272 newMap: function newMap(key, options) {
273 return this.newObject(key, ObjectStore, options);
276 newArray: function newArray(key, options) {
277 return this.newObject(key, ArrayStore, update({ type: Array }, options));
280 addObserver: function addObserver(key, callback, ref) {
282 let refs = overlay.getData(ref, "storage-refs");
284 var callbackRef = util.weakReference(callback);
287 callbackRef = { get: function () callback };
290 this.removeDeadObservers();
292 if (!(key in this.observers))
293 this.observers[key] = [];
295 if (!this.observers[key].some(o => o.callback.get() == callback))
296 this.observers[key].push({ ref: ref && Cu.getWeakReference(ref),
297 callback: callbackRef });
300 removeObserver: function (key, callback) {
301 this.removeDeadObservers();
303 if (!(key in this.observers))
306 this.observers[key] = this.observers[key].filter(elem => elem.callback.get() != callback);
307 if (this.observers[key].length == 0)
308 delete obsevers[key];
311 removeDeadObservers: function () {
313 if (!o.callback.get())
316 let ref = o.ref && o.ref.get();
317 return ref && !ref.closed && overlay.getData(ref, "storage-refs", null);
320 for (let [key, ary] in Iterator(this.observers)) {
321 this.observers[key] = ary = ary.filter(filter);
323 delete this.observers[key];
327 fireEvent: function fireEvent(key, event, arg) {
328 this.removeDeadObservers();
330 if (key in this.observers)
331 // Safe, since we have our own Array object here.
332 for each (let observer in this.observers[key])
333 observer.callback.get()(key, event, arg);
335 if (key in this.keys && this.keys[key].timer)
336 this[key].timer.tell();
339 load: function load(key) {
340 if (this[key].store && this[key].reload)
344 save: function save(key) {
346 this._saveData(this.keys[key]);
349 saveAll: function storeAll() {
350 for each (let obj in this.keys)
355 get privateMode() this._privateMode,
356 set privateMode(enabled) {
357 this._privateMode = Boolean(enabled);
359 if (this.isLocalModule) {
367 for (let [k, v] in Iterator(keys))
368 this.keys[k] = this._privatize(v);
371 return this._privateMode;
374 _privatize: function privatize(obj) {
375 if (obj.privateData && obj.clone)
376 return obj.clone(this);
381 skipXpcom: function skipXpcom(key, val) val instanceof Ci.nsISupports ? null : val
384 cleanup: function (dactyl, modules, window) {
385 overlay.setData(window, "storage-refs", null);
386 this.removeDeadObservers();
391 * @class File A class to wrap nsIFile objects and simplify operations
394 * @param {nsIFile|string} path Expanded according to {@link IO#expandPath}
395 * @param {boolean} checkPWD Whether to allow expansion relative to the
396 * current directory. @default true
397 * @param {string} charset The charset of the file. @default File.defaultEncoding
399 var File = Class("File", {
400 init: function (path, checkPWD, charset) {
401 let file = services.File();
404 this.charset = charset;
406 if (path instanceof Ci.nsIFileURL)
409 if (path instanceof Ci.nsIFile || path instanceof File)
411 else if (/file:\/\//.test(path))
412 file = services["file:"].getFileFromURLSpec(path);
415 let expandedPath = File.expandPath(path);
417 if (!File.isAbsolutePath(expandedPath) && checkPWD)
418 file = checkPWD.child(expandedPath);
420 file.initWithPath(expandedPath);
424 return File.DoesNotExist(path, e);
427 this.file = file.QueryInterface(Ci.nsILocalFile);
431 charset: Class.Memoize(() => File.defaultEncoding),
434 * @property {nsIFileURL} Returns the nsIFileURL object for this file.
436 URI: Class.Memoize(function () {
437 let uri = services.io.newFileURI(this.file)
438 .QueryInterface(Ci.nsIFileURL);
439 uri.QueryInterface(Ci.nsIMutable).mutable = false;
444 * Iterates over the objects in this directory.
446 iterDirectory: function iterDirectory() {
448 throw Error(_("io.noSuchFile"));
449 if (!this.isDirectory())
450 throw Error(_("io.eNotDir"));
451 for (let file in iter(this.directoryEntries))
456 * Returns a new file for the given child of this directory entry.
458 child: function child() {
459 let f = this.constructor(this);
460 for (let [, name] in Iterator(arguments))
461 for each (let elem in name.split(File.pathSplit))
467 * Returns an iterator for all lines in a file.
469 get lines() File.readLines(services.FileInStream(this.file, -1, 0, 0),
473 * Reads this file's entire contents in "text" mode and returns the
474 * content as a string.
476 * @param {string} encoding The encoding from which to decode the file.
480 read: function read(encoding) {
481 let ifstream = services.FileInStream(this.file, -1, 0, 0);
483 return File.readStream(ifstream, encoding || this.charset);
487 * Returns the list of files in this directory.
489 * @param {boolean} sort Whether to sort the returned directory
491 * @returns {[nsIFile]}
493 readDirectory: function readDirectory(sort) {
494 if (!this.isDirectory())
495 throw Error(_("io.eNotDir"));
497 let array = [e for (e in this.iterDirectory())];
499 array.sort((a, b) => (b.isDirectory() - a.isDirectory() ||
500 String.localeCompare(a.path, b.path)));
505 * Returns a new nsIFileURL object for this file.
507 * @returns {nsIFileURL}
509 toURI: function toURI() services.io.newFileURI(this.file),
512 * Writes the string *buf* to this file.
514 * @param {string} buf The file content.
515 * @param {string|number} mode The file access mode, a bitwise OR of
516 * the following flags:
517 * {@link #MODE_RDONLY}: 0x01
518 * {@link #MODE_WRONLY}: 0x02
519 * {@link #MODE_RDWR}: 0x04
520 * {@link #MODE_CREATE}: 0x08
521 * {@link #MODE_APPEND}: 0x10
522 * {@link #MODE_TRUNCATE}: 0x20
523 * {@link #MODE_SYNC}: 0x40
524 * Alternatively, the following abbreviations may be used:
525 * ">" is equivalent to {@link #MODE_WRONLY} | {@link #MODE_CREATE} | {@link #MODE_TRUNCATE}
526 * ">>" is equivalent to {@link #MODE_WRONLY} | {@link #MODE_CREATE} | {@link #MODE_APPEND}
528 * @param {number} perms The file mode bits of the created file. This
529 * is only used when creating a new file and does not change
530 * permissions if the file exists.
532 * @param {string} encoding The encoding to used to write the file.
535 write: function write(buf, mode, perms, encoding) {
536 function getStream(defaultChar) {
537 return services.ConvOutStream(ofstream, encoding, 0, defaultChar);
539 if (buf instanceof File)
543 encoding = this.charset;
546 mode = File.MODE_WRONLY | File.MODE_CREATE | File.MODE_APPEND;
547 else if (!mode || mode == ">")
548 mode = File.MODE_WRONLY | File.MODE_CREATE | File.MODE_TRUNCATE;
552 if (!this.exists()) // OCREAT won't create the directory
553 this.create(this.NORMAL_FILE_TYPE, perms);
555 let ofstream = services.FileOutStream(this.file, mode, perms, 0);
557 var ocstream = getStream(0);
558 ocstream.writeString(buf);
560 catch (e if e.result == Cr.NS_ERROR_LOSS_OF_SIGNIFICANT_DATA) {
562 ocstream = getStream("?".charCodeAt(0));
563 ocstream.writeString(buf);
576 // Wrapped native methods:
577 copyTo: function copyTo(dir, name)
578 this.file.copyTo(this.constructor(dir).file,
581 copyToFollowingLinks: function copyToFollowingLinks(dir, name)
582 this.file.copyToFollowingLinks(this.constructor(dir).file,
585 moveTo: function moveTo(dir, name)
586 this.file.moveTo(this.constructor(dir).file,
589 equals: function equals(file)
590 this.file.equals(this.constructor(file).file),
592 contains: function contains(dir, recur)
593 this.file.contains(this.constructor(dir).file,
596 getRelativeDescriptor: function getRelativeDescriptor(file)
597 this.file.getRelativeDescriptor(this.constructor(file).file),
599 setRelativeDescriptor: function setRelativeDescriptor(file, path)
600 this.file.setRelativeDescriptor(this.constructor(file).file,
604 * @property {number} Open for reading only.
610 * @property {number} Open for writing only.
616 * @property {number} Open for reading and writing.
622 * @property {number} If the file does not exist, the file is created.
623 * If the file exists, this flag has no effect.
629 * @property {number} The file pointer is set to the end of the file
630 * prior to each write.
636 * @property {number} If the file exists, its length is truncated to 0.
642 * @property {number} If set, each write will wait for both the file
643 * data and file status to be physically updated.
649 * @property {number} With MODE_CREATE, if the file does not exist, the
650 * file is created. If the file already exists, no action and NULL
657 * @property {string} The current platform's path separator.
659 PATH_SEP: Class.Memoize(function () {
660 let f = services.directory.get("CurProcD", Ci.nsIFile);
662 return f.path.substr(f.parent.path.length, 1);
665 pathSplit: Class.Memoize(function () util.regexp("(?:/|" + util.regexp.escape(this.PATH_SEP) + ")", "g")),
667 DoesNotExist: function DoesNotExist(path, error) ({
669 exists: function () false,
670 __noSuchMethod__: function () { throw error || Error("Does not exist"); }
673 defaultEncoding: "UTF-8",
676 * Expands "~" and environment variables in *path*.
678 * "~" is expanded to to the value of $HOME. On Windows if this is not
679 * set then the following are tried in order:
681 * ${HOMDRIVE}$HOMEPATH
683 * The variable notation is $VAR (terminated by a non-word character)
684 * or ${VAR}. %VAR% is also supported on Windows.
686 * @param {string} path The unexpanded path string.
687 * @param {boolean} relative Whether the path is relative or absolute.
690 expandPath: function expandPath(path, relative) {
691 function getenv(name) services.environment.get(name);
693 // expand any $ENV vars - this is naive but so is Vim and we like to be compatible
694 // TODO: Vim does not expand variables set to an empty string (and documents it).
695 // Kris reckons we shouldn't replicate this 'bug'. --djk
696 // TODO: should we be doing this for all paths?
697 function expand(path) path.replace(
698 win32 ? /\$(\w+)\b|\${(\w+)}|%(\w+)%/g
699 : /\$(\w+)\b|\${(\w+)}/g,
700 (m, n1, n2, n3) => (getenv(n1 || n2 || n3) || m));
705 if (!relative && RegExp("~(?:$|[/" + util.regexp.escape(File.PATH_SEP) + "])").test(path)) {
706 // Try $HOME first, on all systems
707 let home = getenv("HOME");
709 // Windows has its own idiosyncratic $HOME variables.
710 if (win32 && (!home || !File(home).exists()))
711 home = getenv("USERPROFILE") ||
712 getenv("HOMEDRIVE") + getenv("HOMEPATH");
714 path = home + path.substr(1);
717 // TODO: Vim expands paths twice, once before checking for ~, once
718 // after, but doesn't document it. Is this just a bug? --Kris
720 return path.replace("/", File.PATH_SEP, "g");
723 expandPathList: function (list) list.map(this.expandPath),
725 readURL: function readURL(url, encoding) {
726 let channel = services.io.newChannel(url, null, null);
727 channel.contentType = "text/plain";
728 return this.readStream(channel.open(), encoding);
731 readStream: function readStream(ifstream, encoding) {
733 var icstream = services.CharsetStream(
734 ifstream, encoding || File.defaultEncoding, 4096, // buffer size
735 services.CharsetStream.DEFAULT_REPLACEMENT_CHARACTER);
739 while (icstream.readString(4096, str) != 0)
740 buffer.push(str.value);
741 return buffer.join("");
749 readLines: function readLines(ifstream, encoding) {
751 var icstream = services.CharsetStream(
752 ifstream, encoding || File.defaultEncoding, 4096, // buffer size
753 services.CharsetStream.DEFAULT_REPLACEMENT_CHARACTER);
756 while (icstream.readLine(value))
765 isAbsolutePath: function isAbsolutePath(path) {
767 services.File().initWithPath(path);
775 joinPaths: function joinPaths(head, tail, cwd) {
776 let path = this(head, cwd);
778 // FIXME: should only expand environment vars and normalize path separators
779 path.appendRelativePath(this.expandPath(tail, true));
782 return File.DoesNotExist(e);
787 replacePathSep: function (path) path.replace("/", File.PATH_SEP, "g")
790 let (file = services.directory.get("ProfD", Ci.nsIFile)) {
791 Object.keys(file).forEach(function (prop) {
792 if (!(prop in File.prototype)) {
795 isFunction = callable(file[prop]);
800 File.prototype[prop] = util.wrapCallback(function wrapper() this.file[prop].apply(this.file, arguments));
802 Object.defineProperty(File.prototype, prop, {
804 get: function wrap_get() this.file[prop],
805 set: function wrap_set(val) { this.file[prop] = val; }
814 // catch(e){ dump(e + "\n" + (e.stack || Error().stack)); Components.utils.reportError(e) }
816 // vim: set fdm=marker sw=4 sts=4 ts=8 et ft=javascript: