2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2008-03-25 03:38:03 +03:00
|
|
|
|
2010-02-01 06:12:57 +03:00
|
|
|
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
|
2010-04-21 02:14:15 +04:00
|
|
|
Components.utils.import("resource://gre/modules/AddonManager.jsm");
|
2010-02-01 06:12:57 +03:00
|
|
|
|
2008-03-25 03:38:03 +03:00
|
|
|
//=================================================
|
|
|
|
// Console constructor
|
|
|
|
function Console() {
|
|
|
|
this._console = Components.classes["@mozilla.org/consoleservice;1"]
|
2012-06-06 04:05:50 +04:00
|
|
|
.getService(Ci.nsIConsoleService);
|
2008-03-25 03:38:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//=================================================
|
|
|
|
// Console implementation
|
|
|
|
Console.prototype = {
|
2012-06-06 04:05:50 +04:00
|
|
|
log: function cs_log(aMsg) {
|
2008-03-25 03:38:03 +03:00
|
|
|
this._console.logStringMessage(aMsg);
|
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
open: function cs_open() {
|
2008-03-25 03:38:03 +03:00
|
|
|
var wMediator = Components.classes["@mozilla.org/appshell/window-mediator;1"]
|
|
|
|
.getService(Ci.nsIWindowMediator);
|
|
|
|
var console = wMediator.getMostRecentWindow("global:console");
|
|
|
|
if (!console) {
|
|
|
|
var wWatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
|
|
|
|
.getService(Ci.nsIWindowWatcher);
|
|
|
|
wWatch.openWindow(null, "chrome://global/content/console.xul", "_blank",
|
|
|
|
"chrome,dialog=no,all", null);
|
|
|
|
} else {
|
|
|
|
// console was already open
|
|
|
|
console.focus();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.extIConsole])
|
2008-03-25 03:38:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//=================================================
|
|
|
|
// EventItem constructor
|
|
|
|
function EventItem(aType, aData) {
|
|
|
|
this._type = aType;
|
|
|
|
this._data = aData;
|
|
|
|
}
|
|
|
|
|
|
|
|
//=================================================
|
|
|
|
// EventItem implementation
|
|
|
|
EventItem.prototype = {
|
2012-06-06 04:05:50 +04:00
|
|
|
_cancel: false,
|
2008-03-25 03:38:03 +03:00
|
|
|
|
|
|
|
get type() {
|
|
|
|
return this._type;
|
|
|
|
},
|
|
|
|
|
|
|
|
get data() {
|
|
|
|
return this._data;
|
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
preventDefault: function ei_pd() {
|
2008-03-25 03:38:03 +03:00
|
|
|
this._cancel = true;
|
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.extIEventItem])
|
2008-03-25 03:38:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//=================================================
|
|
|
|
// Events constructor
|
2010-02-01 06:12:57 +03:00
|
|
|
function Events(notifier) {
|
2008-03-25 03:38:03 +03:00
|
|
|
this._listeners = [];
|
2010-02-01 06:12:57 +03:00
|
|
|
this._notifier = notifier;
|
2008-03-25 03:38:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//=================================================
|
|
|
|
// Events implementation
|
|
|
|
Events.prototype = {
|
2012-06-06 04:05:50 +04:00
|
|
|
addListener: function evts_al(aEvent, aListener) {
|
2010-02-01 06:12:57 +03:00
|
|
|
function hasFilter(element) {
|
|
|
|
return element.event == aEvent && element.listener == aListener;
|
|
|
|
}
|
|
|
|
|
2008-03-25 03:38:03 +03:00
|
|
|
if (this._listeners.some(hasFilter))
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._listeners.push({
|
|
|
|
event: aEvent,
|
|
|
|
listener: aListener
|
|
|
|
});
|
|
|
|
|
2010-02-01 06:12:57 +03:00
|
|
|
if (this._notifier) {
|
|
|
|
this._notifier(aEvent, aListener);
|
2008-03-25 03:38:03 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
removeListener: function evts_rl(aEvent, aListener) {
|
2008-03-25 03:38:03 +03:00
|
|
|
function hasFilter(element) {
|
2009-01-15 21:47:55 +03:00
|
|
|
return (element.event != aEvent) || (element.listener != aListener);
|
2008-03-25 03:38:03 +03:00
|
|
|
}
|
2010-02-01 06:12:57 +03:00
|
|
|
|
|
|
|
this._listeners = this._listeners.filter(hasFilter);
|
2008-03-25 03:38:03 +03:00
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
dispatch: function evts_dispatch(aEvent, aEventItem) {
|
2010-02-03 15:40:55 +03:00
|
|
|
var eventItem = new EventItem(aEvent, aEventItem);
|
2008-03-25 03:38:03 +03:00
|
|
|
|
|
|
|
this._listeners.forEach(function(key){
|
|
|
|
if (key.event == aEvent) {
|
|
|
|
key.listener.handleEvent ?
|
|
|
|
key.listener.handleEvent(eventItem) :
|
|
|
|
key.listener(eventItem);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return !eventItem._cancel;
|
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.extIEvents])
|
2008-03-25 03:38:03 +03:00
|
|
|
};
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
//=================================================
|
|
|
|
// PreferenceObserver (internal class)
|
|
|
|
//
|
|
|
|
// PreferenceObserver is a global singleton which watches the browser's
|
|
|
|
// preferences and sends you events when things change.
|
|
|
|
|
|
|
|
function PreferenceObserver() {
|
|
|
|
this._observersDict = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
PreferenceObserver.prototype = {
|
|
|
|
/**
|
|
|
|
* Add a preference observer.
|
|
|
|
*
|
|
|
|
* @param aPrefs the nsIPrefBranch onto which we'll install our listener.
|
|
|
|
* @param aDomain the domain our listener will watch (a string).
|
|
|
|
* @param aEvent the event to listen to (you probably want "change").
|
|
|
|
* @param aListener the function to call back when the event fires. This
|
|
|
|
* function will receive an EventData argument.
|
|
|
|
*/
|
2012-06-06 04:05:50 +04:00
|
|
|
addListener: function po_al(aPrefs, aDomain, aEvent, aListener) {
|
2012-06-06 04:05:50 +04:00
|
|
|
var root = aPrefs.root;
|
|
|
|
if (!this._observersDict[root]) {
|
|
|
|
this._observersDict[root] = {};
|
|
|
|
}
|
|
|
|
var observer = this._observersDict[root][aDomain];
|
|
|
|
|
|
|
|
if (!observer) {
|
|
|
|
observer = {
|
|
|
|
events: new Events(),
|
2012-06-06 04:05:50 +04:00
|
|
|
observe: function po_observer_obs(aSubject, aTopic, aData) {
|
2012-06-06 04:05:50 +04:00
|
|
|
this.events.dispatch("change", aData);
|
|
|
|
},
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
|
|
|
|
Ci.nsISupportsWeakReference])
|
|
|
|
};
|
|
|
|
observer.prefBranch = aPrefs;
|
|
|
|
observer.prefBranch.addObserver(aDomain, observer, /* ownsWeak = */ true);
|
|
|
|
|
|
|
|
// Notice that the prefBranch keeps a weak reference to the observer;
|
|
|
|
// it's this._observersDict which keeps the observer alive.
|
|
|
|
this._observersDict[root][aDomain] = observer;
|
|
|
|
}
|
|
|
|
observer.events.addListener(aEvent, aListener);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a preference observer.
|
|
|
|
*
|
|
|
|
* This function's parameters are identical to addListener's.
|
|
|
|
*/
|
2012-06-06 04:05:50 +04:00
|
|
|
removeListener: function po_rl(aPrefs, aDomain, aEvent, aListener) {
|
2012-06-06 04:05:50 +04:00
|
|
|
var root = aPrefs.root;
|
|
|
|
if (!this._observersDict[root] ||
|
|
|
|
!this._observersDict[root][aDomain]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var observer = this._observersDict[root][aDomain];
|
|
|
|
observer.events.removeListener(aEvent, aListener);
|
|
|
|
|
|
|
|
if (observer.events._listeners.length == 0) {
|
|
|
|
// nsIPrefBranch objects are not singletons -- we can have two
|
|
|
|
// nsIPrefBranch'es for the same branch. There's no guarantee that
|
|
|
|
// aPrefs is the same object as observer.prefBranch, so we have to call
|
|
|
|
// removeObserver on observer.prefBranch.
|
|
|
|
observer.prefBranch.removeObserver(aDomain, observer);
|
|
|
|
delete this._observersDict[root][aDomain];
|
|
|
|
if (Object.keys(this._observersDict[root]).length == 0) {
|
|
|
|
delete this._observersDict[root];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2008-03-25 03:38:03 +03:00
|
|
|
|
|
|
|
//=================================================
|
|
|
|
// PreferenceBranch constructor
|
|
|
|
function PreferenceBranch(aBranch) {
|
|
|
|
if (!aBranch)
|
|
|
|
aBranch = "";
|
|
|
|
|
|
|
|
this._root = aBranch;
|
|
|
|
this._prefs = Components.classes["@mozilla.org/preferences-service;1"]
|
2012-06-06 04:05:50 +04:00
|
|
|
.getService(Ci.nsIPrefService)
|
|
|
|
.QueryInterface(Ci.nsIPrefBranch);
|
2008-03-25 03:38:03 +03:00
|
|
|
|
|
|
|
if (aBranch)
|
|
|
|
this._prefs = this._prefs.getBranch(aBranch);
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
let prefs = this._prefs;
|
|
|
|
this._events = {
|
2012-06-06 04:05:50 +04:00
|
|
|
addListener: function pb_al(aEvent, aListener) {
|
2012-06-06 04:05:50 +04:00
|
|
|
gPreferenceObserver.addListener(prefs, "", aEvent, aListener);
|
|
|
|
},
|
2012-06-06 04:05:50 +04:00
|
|
|
removeListener: function pb_rl(aEvent, aListener) {
|
2012-06-06 04:05:50 +04:00
|
|
|
gPreferenceObserver.removeListener(prefs, "", aEvent, aListener);
|
|
|
|
},
|
2012-06-06 04:05:50 +04:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.extIEvents])
|
2012-06-06 04:05:50 +04:00
|
|
|
};
|
2008-03-25 03:38:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//=================================================
|
|
|
|
// PreferenceBranch implementation
|
|
|
|
PreferenceBranch.prototype = {
|
|
|
|
get root() {
|
|
|
|
return this._root;
|
|
|
|
},
|
|
|
|
|
|
|
|
get all() {
|
|
|
|
return this.find({});
|
|
|
|
},
|
|
|
|
|
|
|
|
get events() {
|
|
|
|
return this._events;
|
|
|
|
},
|
|
|
|
|
|
|
|
// XXX: Disabled until we can figure out the wrapped object issues
|
|
|
|
// name: "name" or /name/
|
|
|
|
// path: "foo.bar." or "" or /fo+\.bar/
|
|
|
|
// type: Boolean, Number, String (getPrefType)
|
|
|
|
// locked: true, false (prefIsLocked)
|
|
|
|
// modified: true, false (prefHasUserValue)
|
2012-06-06 04:05:50 +04:00
|
|
|
find: function prefs_find(aOptions) {
|
2008-03-25 03:38:03 +03:00
|
|
|
var retVal = [];
|
2009-10-26 23:02:06 +03:00
|
|
|
var items = this._prefs.getChildList("");
|
2008-03-25 03:38:03 +03:00
|
|
|
|
|
|
|
for (var i = 0; i < items.length; i++) {
|
|
|
|
retVal.push(new Preference(items[i], this));
|
|
|
|
}
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
has: function prefs_has(aName) {
|
2008-03-25 03:38:03 +03:00
|
|
|
return (this._prefs.getPrefType(aName) != Ci.nsIPrefBranch.PREF_INVALID);
|
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
get: function prefs_get(aName) {
|
2008-03-25 03:38:03 +03:00
|
|
|
return this.has(aName) ? new Preference(aName, this) : null;
|
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
getValue: function prefs_gv(aName, aValue) {
|
2008-03-25 03:38:03 +03:00
|
|
|
var type = this._prefs.getPrefType(aName);
|
|
|
|
|
|
|
|
switch (type) {
|
2012-01-17 05:18:29 +04:00
|
|
|
case Ci.nsIPrefBranch.PREF_STRING:
|
2008-03-25 03:38:03 +03:00
|
|
|
aValue = this._prefs.getComplexValue(aName, Ci.nsISupportsString).data;
|
|
|
|
break;
|
2012-01-17 05:18:29 +04:00
|
|
|
case Ci.nsIPrefBranch.PREF_BOOL:
|
2008-03-25 03:38:03 +03:00
|
|
|
aValue = this._prefs.getBoolPref(aName);
|
|
|
|
break;
|
2012-01-17 05:18:29 +04:00
|
|
|
case Ci.nsIPrefBranch.PREF_INT:
|
2008-03-25 03:38:03 +03:00
|
|
|
aValue = this._prefs.getIntPref(aName);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return aValue;
|
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
setValue: function prefs_sv(aName, aValue) {
|
2008-03-25 03:38:03 +03:00
|
|
|
var type = aValue != null ? aValue.constructor.name : "";
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case "String":
|
|
|
|
var str = Components.classes["@mozilla.org/supports-string;1"]
|
|
|
|
.createInstance(Ci.nsISupportsString);
|
|
|
|
str.data = aValue;
|
|
|
|
this._prefs.setComplexValue(aName, Ci.nsISupportsString, str);
|
|
|
|
break;
|
|
|
|
case "Boolean":
|
|
|
|
this._prefs.setBoolPref(aName, aValue);
|
|
|
|
break;
|
|
|
|
case "Number":
|
|
|
|
this._prefs.setIntPref(aName, aValue);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw("Unknown preference value specified.");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
reset: function prefs_reset() {
|
2008-03-25 03:38:03 +03:00
|
|
|
this._prefs.resetBranch("");
|
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.extIPreferenceBranch])
|
2008-03-25 03:38:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//=================================================
|
|
|
|
// Preference constructor
|
|
|
|
function Preference(aName, aBranch) {
|
|
|
|
this._name = aName;
|
|
|
|
this._branch = aBranch;
|
|
|
|
|
|
|
|
var self = this;
|
2012-06-06 04:05:50 +04:00
|
|
|
this._events = {
|
2012-06-06 04:05:50 +04:00
|
|
|
addListener: function pref_al(aEvent, aListener) {
|
2012-06-06 04:05:50 +04:00
|
|
|
gPreferenceObserver.addListener(self._branch._prefs, self._name, aEvent, aListener);
|
|
|
|
},
|
2012-06-06 04:05:50 +04:00
|
|
|
removeListener: function pref_rl(aEvent, aListener) {
|
2012-06-06 04:05:50 +04:00
|
|
|
gPreferenceObserver.removeListener(self._branch._prefs, self._name, aEvent, aListener);
|
|
|
|
},
|
2012-06-06 04:05:50 +04:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.extIEvents])
|
2012-06-06 04:05:50 +04:00
|
|
|
};
|
2008-03-25 03:38:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//=================================================
|
|
|
|
// Preference implementation
|
|
|
|
Preference.prototype = {
|
|
|
|
get name() {
|
|
|
|
return this._name;
|
|
|
|
},
|
|
|
|
|
|
|
|
get type() {
|
|
|
|
var value = "";
|
|
|
|
var type = this.branch._prefs.getPrefType(this._name);
|
|
|
|
|
|
|
|
switch (type) {
|
2012-01-17 05:18:29 +04:00
|
|
|
case Ci.nsIPrefBranch.PREF_STRING:
|
2008-03-25 03:38:03 +03:00
|
|
|
value = "String";
|
|
|
|
break;
|
2012-01-17 05:18:29 +04:00
|
|
|
case Ci.nsIPrefBranch.PREF_BOOL:
|
2008-03-25 03:38:03 +03:00
|
|
|
value = "Boolean";
|
|
|
|
break;
|
2012-01-17 05:18:29 +04:00
|
|
|
case Ci.nsIPrefBranch.PREF_INT:
|
2008-03-25 03:38:03 +03:00
|
|
|
value = "Number";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
|
|
|
|
get value() {
|
|
|
|
return this.branch.getValue(this._name, null);
|
|
|
|
},
|
|
|
|
|
|
|
|
set value(aValue) {
|
|
|
|
return this.branch.setValue(this._name, aValue);
|
|
|
|
},
|
|
|
|
|
|
|
|
get locked() {
|
|
|
|
return this.branch._prefs.prefIsLocked(this.name);
|
|
|
|
},
|
|
|
|
|
|
|
|
set locked(aValue) {
|
|
|
|
this.branch._prefs[ aValue ? "lockPref" : "unlockPref" ](this.name);
|
|
|
|
},
|
|
|
|
|
|
|
|
get modified() {
|
|
|
|
return this.branch._prefs.prefHasUserValue(this.name);
|
|
|
|
},
|
|
|
|
|
|
|
|
get branch() {
|
|
|
|
return this._branch;
|
|
|
|
},
|
|
|
|
|
|
|
|
get events() {
|
|
|
|
return this._events;
|
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
reset: function pref_reset() {
|
2008-03-25 03:38:03 +03:00
|
|
|
this.branch._prefs.clearUserPref(this.name);
|
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.extIPreference])
|
2008-03-25 03:38:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//=================================================
|
|
|
|
// SessionStorage constructor
|
|
|
|
function SessionStorage() {
|
|
|
|
this._storage = {};
|
|
|
|
this._events = new Events();
|
|
|
|
}
|
|
|
|
|
|
|
|
//=================================================
|
|
|
|
// SessionStorage implementation
|
|
|
|
SessionStorage.prototype = {
|
|
|
|
get events() {
|
|
|
|
return this._events;
|
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
has: function ss_has(aName) {
|
2008-03-25 03:38:03 +03:00
|
|
|
return this._storage.hasOwnProperty(aName);
|
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
set: function ss_set(aName, aValue) {
|
2008-03-25 03:38:03 +03:00
|
|
|
this._storage[aName] = aValue;
|
|
|
|
this._events.dispatch("change", aName);
|
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
get: function ss_get(aName, aDefaultValue) {
|
2008-03-25 03:38:03 +03:00
|
|
|
return this.has(aName) ? this._storage[aName] : aDefaultValue;
|
|
|
|
},
|
|
|
|
|
|
|
|
QueryInterface : XPCOMUtils.generateQI([Ci.extISessionStorage])
|
|
|
|
};
|
|
|
|
|
|
|
|
//=================================================
|
2012-06-06 04:05:50 +04:00
|
|
|
// ExtensionObserver constructor (internal class)
|
|
|
|
//
|
|
|
|
// ExtensionObserver is a global singleton which watches the browser's
|
|
|
|
// extensions and sends you events when things change.
|
2008-03-25 03:38:03 +03:00
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
function ExtensionObserver() {
|
|
|
|
this._eventsDict = {};
|
2008-03-25 03:38:03 +03:00
|
|
|
|
2010-04-21 02:14:15 +04:00
|
|
|
AddonManager.addAddonListener(this);
|
|
|
|
AddonManager.addInstallListener(this);
|
2008-03-25 03:38:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//=================================================
|
2012-06-06 04:05:50 +04:00
|
|
|
// ExtensionObserver implementation (internal class)
|
|
|
|
ExtensionObserver.prototype = {
|
2012-06-06 04:05:50 +04:00
|
|
|
onDisabling: function eo_onDisabling(addon, needsRestart) {
|
2012-06-06 04:05:50 +04:00
|
|
|
this._dispatchEvent(addon.id, "disable");
|
2010-04-21 02:14:15 +04:00
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
onEnabling: function eo_onEnabling(addon, needsRestart) {
|
2012-06-06 04:05:50 +04:00
|
|
|
this._dispatchEvent(addon.id, "enable");
|
2010-04-21 02:14:15 +04:00
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
onUninstalling: function eo_onUninstalling(addon, needsRestart) {
|
2012-06-06 04:05:50 +04:00
|
|
|
this._dispatchEvent(addon.id, "uninstall");
|
2010-04-21 02:14:15 +04:00
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
onOperationCancelled: function eo_onOperationCancelled(addon) {
|
2012-06-06 04:05:50 +04:00
|
|
|
this._dispatchEvent(addon.id, "cancel");
|
2010-04-21 02:14:15 +04:00
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
onInstallEnded: function eo_onInstallEnded(install, addon) {
|
2012-06-06 04:05:50 +04:00
|
|
|
this._dispatchEvent(addon.id, "upgrade");
|
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
addListener: function eo_al(aId, aEvent, aListener) {
|
2012-06-06 04:05:50 +04:00
|
|
|
var events = this._eventsDict[aId];
|
|
|
|
if (!events) {
|
|
|
|
events = new Events();
|
|
|
|
this._eventsDict[aId] = events;
|
|
|
|
}
|
|
|
|
events.addListener(aEvent, aListener);
|
2008-03-25 03:38:03 +03:00
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
removeListener: function eo_rl(aId, aEvent, aListener) {
|
2012-06-06 04:05:50 +04:00
|
|
|
var events = this._eventsDict[aId];
|
|
|
|
if (!events) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
events.removeListener(aEvent, aListener);
|
|
|
|
if (events._listeners.length == 0) {
|
|
|
|
delete this._eventsDict[aId];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
_dispatchEvent: function eo_dispatchEvent(aId, aEvent) {
|
2012-06-06 04:05:50 +04:00
|
|
|
var events = this._eventsDict[aId];
|
|
|
|
if (events) {
|
|
|
|
events.dispatch(aEvent, aId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//=================================================
|
|
|
|
// Extension constructor
|
|
|
|
function Extension(aItem) {
|
|
|
|
this._item = aItem;
|
|
|
|
this._firstRun = false;
|
|
|
|
this._prefs = new PreferenceBranch("extensions." + this.id + ".");
|
|
|
|
this._storage = new SessionStorage();
|
|
|
|
|
|
|
|
let id = this.id;
|
|
|
|
this._events = {
|
2012-06-06 04:05:50 +04:00
|
|
|
addListener: function ext_events_al(aEvent, aListener) {
|
2012-06-06 04:05:50 +04:00
|
|
|
gExtensionObserver.addListener(id, aEvent, aListener);
|
|
|
|
},
|
2012-06-06 04:05:50 +04:00
|
|
|
removeListener: function ext_events_rl(aEvent, aListener) {
|
2012-06-06 04:05:50 +04:00
|
|
|
gExtensionObserver.addListener(id, aEvent, aListener);
|
|
|
|
},
|
2012-06-06 04:05:50 +04:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.extIEvents])
|
2012-06-06 04:05:50 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
var installPref = "install-event-fired";
|
|
|
|
if (!this._prefs.has(installPref)) {
|
|
|
|
this._prefs.setValue(installPref, true);
|
|
|
|
this._firstRun = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//=================================================
|
|
|
|
// Extension implementation
|
|
|
|
Extension.prototype = {
|
2008-03-25 03:38:03 +03:00
|
|
|
get id() {
|
|
|
|
return this._item.id;
|
|
|
|
},
|
|
|
|
|
|
|
|
get name() {
|
|
|
|
return this._item.name;
|
|
|
|
},
|
|
|
|
|
|
|
|
get enabled() {
|
2010-04-21 02:14:15 +04:00
|
|
|
return this._item.isActive;
|
2008-03-25 03:38:03 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
get version() {
|
|
|
|
return this._item.version;
|
|
|
|
},
|
|
|
|
|
|
|
|
get firstRun() {
|
|
|
|
return this._firstRun;
|
|
|
|
},
|
|
|
|
|
|
|
|
get storage() {
|
|
|
|
return this._storage;
|
|
|
|
},
|
|
|
|
|
|
|
|
get prefs() {
|
|
|
|
return this._prefs;
|
|
|
|
},
|
|
|
|
|
|
|
|
get events() {
|
|
|
|
return this._events;
|
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.extIExtension])
|
2008-03-25 03:38:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//=================================================
|
|
|
|
// Extensions constructor
|
2010-04-21 02:14:15 +04:00
|
|
|
function Extensions(addons) {
|
2010-05-01 22:04:44 +04:00
|
|
|
this._cache = {};
|
2010-04-21 02:14:15 +04:00
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
addons.forEach(function (addon) {
|
2010-04-21 02:14:15 +04:00
|
|
|
this._cache[addon.id] = new Extension(addon);
|
|
|
|
}, this);
|
2008-03-25 03:38:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//=================================================
|
|
|
|
// Extensions implementation
|
|
|
|
Extensions.prototype = {
|
|
|
|
get all() {
|
|
|
|
return this.find({});
|
|
|
|
},
|
|
|
|
|
|
|
|
// XXX: Disabled until we can figure out the wrapped object issues
|
|
|
|
// id: "some@id" or /id/
|
|
|
|
// name: "name" or /name/
|
|
|
|
// version: "1.0.1"
|
|
|
|
// minVersion: "1.0"
|
|
|
|
// maxVersion: "2.0"
|
2012-06-06 04:05:50 +04:00
|
|
|
find: function exts_find(aOptions) {
|
2010-04-21 02:14:15 +04:00
|
|
|
return [e for each (e in this._cache)];
|
2008-03-25 03:38:03 +03:00
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
has: function exts_has(aId) {
|
2010-04-21 02:14:15 +04:00
|
|
|
return aId in this._cache;
|
2008-03-25 03:38:03 +03:00
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
get: function exts_get(aId) {
|
2010-04-21 02:14:15 +04:00
|
|
|
return this.has(aId) ? this._cache[aId] : null;
|
2008-03-25 03:38:03 +03:00
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.extIExtensions])
|
2008-03-25 03:38:03 +03:00
|
|
|
};
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
//=================================================
|
|
|
|
// Application globals
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
gExtensionObserver = new ExtensionObserver();
|
2012-06-06 04:05:50 +04:00
|
|
|
gPreferenceObserver = new PreferenceObserver();
|
|
|
|
|
2008-03-25 03:38:03 +03:00
|
|
|
//=================================================
|
|
|
|
// extApplication constructor
|
|
|
|
function extApplication() {
|
|
|
|
}
|
|
|
|
|
|
|
|
//=================================================
|
|
|
|
// extApplication implementation
|
|
|
|
extApplication.prototype = {
|
|
|
|
initToolkitHelpers: function extApp_initToolkitHelpers() {
|
2010-02-01 06:12:57 +03:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "_info",
|
|
|
|
"@mozilla.org/xre/app-info;1",
|
|
|
|
"nsIXULAppInfo");
|
2008-03-25 03:38:03 +03:00
|
|
|
|
2010-02-01 06:12:57 +03:00
|
|
|
this._obs = Cc["@mozilla.org/observer-service;1"].
|
|
|
|
getService(Ci.nsIObserverService);
|
2012-06-06 04:05:50 +04:00
|
|
|
this._obs.addObserver(this, "xpcom-shutdown", /* ownsWeak = */ true);
|
2010-02-01 06:12:57 +03:00
|
|
|
this._registered = {"unload": true};
|
2008-03-25 03:38:03 +03:00
|
|
|
},
|
|
|
|
|
2011-05-25 12:46:51 +04:00
|
|
|
classInfo: XPCOMUtils.generateCI({interfaces: [Ci.extIApplication,
|
|
|
|
Ci.nsIObserver],
|
|
|
|
flags: Ci.nsIClassInfo.SINGLETON}),
|
2008-03-25 03:38:03 +03:00
|
|
|
|
|
|
|
// extIApplication
|
|
|
|
get id() {
|
|
|
|
return this._info.ID;
|
|
|
|
},
|
|
|
|
|
|
|
|
get name() {
|
|
|
|
return this._info.name;
|
|
|
|
},
|
|
|
|
|
|
|
|
get version() {
|
|
|
|
return this._info.version;
|
|
|
|
},
|
|
|
|
|
|
|
|
// for nsIObserver
|
|
|
|
observe: function app_observe(aSubject, aTopic, aData) {
|
|
|
|
if (aTopic == "app-startup") {
|
|
|
|
this.events.dispatch("load", "application");
|
|
|
|
}
|
|
|
|
else if (aTopic == "final-ui-startup") {
|
|
|
|
this.events.dispatch("ready", "application");
|
|
|
|
}
|
|
|
|
else if (aTopic == "quit-application-requested") {
|
|
|
|
// we can stop the quit by checking the return value
|
|
|
|
if (this.events.dispatch("quit", "application") == false)
|
|
|
|
aSubject.data = true;
|
|
|
|
}
|
|
|
|
else if (aTopic == "xpcom-shutdown") {
|
|
|
|
this.events.dispatch("unload", "application");
|
2012-06-06 04:05:50 +04:00
|
|
|
gExtensionObserver = null;
|
2012-06-06 04:05:50 +04:00
|
|
|
gPreferenceObserver = null;
|
2008-03-25 03:38:03 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
get console() {
|
2010-02-01 06:12:57 +03:00
|
|
|
let console = new Console();
|
2012-06-06 04:05:50 +04:00
|
|
|
this.__defineGetter__("console", function () console);
|
2010-02-01 06:12:57 +03:00
|
|
|
return this.console;
|
2008-03-25 03:38:03 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
get storage() {
|
2010-02-01 06:12:57 +03:00
|
|
|
let storage = new SessionStorage();
|
2012-06-06 04:05:50 +04:00
|
|
|
this.__defineGetter__("storage", function () storage);
|
2010-02-01 06:12:57 +03:00
|
|
|
return this.storage;
|
2008-03-25 03:38:03 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
get prefs() {
|
2010-02-01 06:12:57 +03:00
|
|
|
let prefs = new PreferenceBranch("");
|
2012-06-06 04:05:50 +04:00
|
|
|
this.__defineGetter__("prefs", function () prefs);
|
2010-02-01 06:12:57 +03:00
|
|
|
return this.prefs;
|
2008-03-25 03:38:03 +03:00
|
|
|
},
|
|
|
|
|
2010-04-21 02:14:15 +04:00
|
|
|
getExtensions: function(callback) {
|
2012-06-06 04:05:50 +04:00
|
|
|
AddonManager.getAddonsByTypes(["extension"], function (addons) {
|
2010-04-21 02:14:15 +04:00
|
|
|
callback.callback(new Extensions(addons));
|
|
|
|
});
|
2008-03-25 03:38:03 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
get events() {
|
|
|
|
|
2010-02-01 06:12:57 +03:00
|
|
|
// This ensures that FUEL only registers for notifications as needed
|
|
|
|
// by callers. Note that the unload (xpcom-shutdown) event is listened
|
|
|
|
// for by default, as it's needed for cleanup purposes.
|
|
|
|
var self = this;
|
|
|
|
function registerCheck(aEvent) {
|
|
|
|
var rmap = { "load": "app-startup",
|
|
|
|
"ready": "final-ui-startup",
|
|
|
|
"quit": "quit-application-requested"};
|
|
|
|
if (!(aEvent in rmap) || aEvent in self._registered)
|
|
|
|
return;
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
self._obs.addObserver(self, rmap[aEvent], /* ownsWeak = */ true);
|
2010-02-01 06:12:57 +03:00
|
|
|
self._registered[aEvent] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
let events = new Events(registerCheck);
|
2012-06-06 04:05:50 +04:00
|
|
|
this.__defineGetter__("events", function () events);
|
2010-02-01 06:12:57 +03:00
|
|
|
return this.events;
|
2008-03-25 03:38:03 +03:00
|
|
|
},
|
|
|
|
|
2008-09-07 08:30:13 +04:00
|
|
|
// helper method for correct quitting/restarting
|
|
|
|
_quitWithFlags: function app__quitWithFlags(aFlags) {
|
|
|
|
let cancelQuit = Components.classes["@mozilla.org/supports-PRBool;1"]
|
|
|
|
.createInstance(Components.interfaces.nsISupportsPRBool);
|
2010-05-26 16:50:29 +04:00
|
|
|
let quitType = aFlags & Components.interfaces.nsIAppStartup.eRestart ? "restart" : null;
|
|
|
|
this._obs.notifyObservers(cancelQuit, "quit-application-requested", quitType);
|
2008-09-07 08:30:13 +04:00
|
|
|
if (cancelQuit.data)
|
|
|
|
return false; // somebody canceled our quit request
|
2009-01-15 21:47:55 +03:00
|
|
|
|
2008-09-07 08:30:13 +04:00
|
|
|
let appStartup = Components.classes['@mozilla.org/toolkit/app-startup;1']
|
|
|
|
.getService(Components.interfaces.nsIAppStartup);
|
|
|
|
appStartup.quit(aFlags);
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
quit: function app_quit() {
|
|
|
|
return this._quitWithFlags(Components.interfaces.nsIAppStartup.eAttemptQuit);
|
|
|
|
},
|
|
|
|
|
|
|
|
restart: function app_restart() {
|
|
|
|
return this._quitWithFlags(Components.interfaces.nsIAppStartup.eAttemptQuit |
|
|
|
|
Components.interfaces.nsIAppStartup.eRestart);
|
|
|
|
},
|
|
|
|
|
2012-06-06 04:05:50 +04:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.extIApplication, Ci.nsISupportsWeakReference])
|
2008-03-25 03:38:03 +03:00
|
|
|
};
|