RetroZilla/extensions/cck/browser/resources/content/cckwizard/srcfiles/cckService.js.in
2015-10-20 23:03:22 -04:00

663 lines
21 KiB
JavaScript

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Client Customization Kit (CCK).
*
* The Initial Developer of the Original Code is
* Giorgio Maone <g.maone@informaction.com>
* Portions created by the Initial Developer are Copyright (C) 2005
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* IBM Corp.
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
function CCKService() {
this.register();
}
CCKService.prototype={
get wrappedJSObject() {
return this;
}
,
QueryInterface: function(iid) {
this.queryInterfaceSupport(iid,SERVICE_IIDS);
return this;
}
,
// nsIObserver implementation
observe: function(subject, topic, data) {
//dump(SERVICE_NAME+" notified of "+subject+","+topic+","+data); //DDEBUG
switch(topic) {
case "xpcom-shutdown":
this.unregister();
break;
case "profile-after-change":
this.init();
break;
case "profile-before-change":
this.uninit();
break;
}
}
,
register: function() {
const osvr=Components.classes['@mozilla.org/observer-service;1'].getService(
Components.interfaces.nsIObserverService);
osvr.addObserver(this,"xpcom-shutdown",false);
osvr.addObserver(this,"profile-after-change",false);
osvr.addObserver(this,"profile-before-change",false);
}
,
unregister: function() {
const osvr=Components.classes['@mozilla.org/observer-service;1'].getService(
Components.interfaces.nsIObserverService);
osvr.removeObserver(this,"xpcom-shutdown");
osvr.removeObserver(this,"profile-after-change");
osvr.removeObserver(this,"profile-before-change");
}
,
_inited: false
,
init: function() {
if(this._inited) return;
this._inited=true;
var prefbranch;
try {
prefbranch=Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
} catch (ex) {
return;
}
var sbs;
var bundle;
var pm;
var ioService;
try {
sbs = Components.classes["@mozilla.org/intl/stringbundle;1"]
.getService(Components.interfaces.nsIStringBundleService );
bundle = sbs.createBundle("chrome://cck/content/cck.properties");
pm = Components.classes["@mozilla.org/permissionmanager;1"].getService(Components.interfaces.nsIPermissionManager);
ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
} catch (ex) {
return;
}
var cckinited;
try {
OrgName = bundle.GetStringFromName("OrganizationName");
cckinited = prefbranch.getBoolPref("cck." + OrgName + ".initialized");
} catch (ex) {
try {
cckinited = prefbranch.getBoolPref("cck.initialized");
OrgName = bundle.GetStringFromName("OrganizationName");
prefbranch.setBoolPref("cck." + OrgName + ".initialized", cckinited);
prefbranch.deleteBranch("cck.initialized");
} catch (ex) {
cckinited = false;
}
}
if ("@mozilla.org/windows-registry-key;1" in Components.classes) {
const nsIWindowsRegKey = Components.interfaces.nsIWindowsRegKey;
var i = 1;
do {
/* This could fail if you don't have the right authority on Windows */
try {
RegName = bundle.GetStringFromName("RegName" + i);
RootKey = bundle.GetStringFromName("RootKey" + i);
Key = bundle.GetStringFromName("Key" + i);
Name = bundle.GetStringFromName("Name" + i);
NameValue = bundle.GetStringFromName("NameValue" + i);
Type = bundle.GetStringFromName("Type" + i);
} catch (ex) {
RegName = "";
}
if (RegName && RegName.length) {
try {
var key = Components.classes["@mozilla.org/windows-registry-key;1"]
.createInstance(nsIWindowsRegKey);
var rootKey;
switch (RootKey) {
case "HKEY_CLASSES_ROOT":
rootKey = nsIWindowsRegKey.ROOT_KEY_CLASSES_ROOT;
break;
case "HKEY_CURRENT_USER":
rootKey = nsIWindowsRegKey.ROOT_KEY_CURRENT_USER;
break;
default:
rootKey = nsIWindowsRegKey.ROOT_KEY_LOCAL_MACHINE;
break;
}
key.create(rootKey, Key, nsIWindowsRegKey.ACCESS_WRITE);
switch (Type) {
case "REG_DWORD":
key.writeIntValue(Name, NameValue);
break;
case "REG_QWORD":
key.writeInt64Value(Name, NameValue);
break;
case "REG_BINARY":
key.writeBinaryValue(Name, NameValue);
break;
case "REG_SZ":
default:
key.writeStringValue(Name, NameValue);
break;
}
key.close();
} catch (ex) {}
}
i++;
} while (RegName && RegName.length)
}
const nsIX509CertDB2 = Components.interfaces.nsIX509CertDB2;
const nsX509CertDBContractID = "@mozilla.org/security/x509certdb;1";
var certDB = Components.classes[nsX509CertDBContractID].getService(nsIX509CertDB2);
var i = 1;
do {
try {
CertName = bundle.GetStringFromName("Cert" + i);
} catch (ex) {
CertName = "";
}
try {
CertTrust = bundle.GetStringFromName("CertTrust" + i);
} catch (ex) {
CertTrust = "C,C,C";
}
if (CertName && CertName.length) {
var ioService=Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
var scriptableStream=Components.classes["@mozilla.org/scriptableinputstream;1"]
.getService(Components.interfaces.nsIScriptableInputStream);
var channel=ioService.newChannel("chrome://cck/content/"+CertName,null,null);
var input=channel.open();
scriptableStream.init(input);
var certfile=scriptableStream.read(input.available());
scriptableStream.close();
input.close();
var beginCert = "-----BEGIN CERTIFICATE-----";
var endCert = "-----END CERTIFICATE-----";
certfile = certfile.replace(/[\r\n]/g, "");
begin = certfile.indexOf(beginCert);
end = certfile.indexOf(endCert);
cert = certfile.substring(begin + beginCert.length, end);
certDB.addCertFromBase64(cert, CertTrust, "");
}
i++;
} while (CertName && CertName.length)
var popupAllowedSites;
try {
popupAllowedSites = bundle.GetStringFromName("PopupAllowedSites");
} catch (ex) {}
if (popupAllowedSites) {
var popupAllowedSitesArray = popupAllowedSites.split(",");
for (var i=0; i < popupAllowedSitesArray.length; i++) {
try {
var uri = ioService.newURI("http://" + popupAllowedSitesArray[i], null, null);
pm.add(uri, "popup", 1);
} catch (ex) {}
}
}
var installAllowedSites;
try {
installAllowedSites = bundle.GetStringFromName("InstallAllowedSites");
} catch (ex) {}
if (popupAllowedSites) {
var installAllowedSitesArray = installAllowedSites.split(",");
for (var i=0; i < installAllowedSitesArray.length; i++) {
try {
var uri = ioService.newURI("http://" + installAllowedSitesArray[i], null, null);
pm.add(uri, "install", 1);
} catch (ex) {}
}
}
var i = 1;
do {
try {
prefName = bundle.GetStringFromName("LockPref" + i);
} catch (ex) {
prefName = "";
}
if (prefName && prefName.length) {
prefbranch.lockPref(prefName);
}
i++;
} while (prefName && prefName.length);
id = bundle.GetStringFromName("id");
var rdfs = Components.classes["@mozilla.org/rdf/rdf-service;1"]
.getService(Components.interfaces.nsIRDFService);
var extensionDS= Components.classes["@mozilla.org/extensions/manager;1"]
.getService(Components.interfaces.nsIExtensionManager).datasource;
var extension = rdfs.GetResource("urn:mozilla:item:" + id);
/* if hidden is set, set it */
try {
hidden = bundle.GetStringFromName("hidden");
} catch (ex) {
hidden = "";
}
if (hidden == "true") {
var predicate = rdfs.GetResource("http://www.mozilla.org/2004/em-rdf#hidden");
var target = rdfs.GetLiteral("true");
extensionDS.Assert(extension, predicate, target, true);
}
/* if locked is set, set it */
try {
locked = bundle.GetStringFromName("locked");
} catch (ex) {
locked = "";
}
if (locked == "true") {
var predicate = rdfs.GetResource("http://www.mozilla.org/2004/em-rdf#locked");
var target = rdfs.GetLiteral("true");
extensionDS.Assert(extension, predicate, target, true);
}
/* Only do bookmarks if we haven't been inited to avoid duplicated */
if (!cckinited)
{
var RDF;
var BMSVC;
var bmRoot;
try {
RDF = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService)
BMSVC = RDF.GetDataSource("rdf:bookmarks").QueryInterface(Components.interfaces.nsIBookmarksService);
BMSVC.readBookmarks();
bmRoot = RDF.GetResource("NC:BookmarksRoot");
} catch (ex) {
return false;
}
try {
BMSVC.QueryInterface(Components.interfaces.nsIBookmarksService_MOZILLA_1_8_BRANCH);
} catch (ex) {}
var ToolbarLocation;
var tbIndex = -1; // last
try {
ToolbarLocation = bundle.GetStringFromName("ToolbarLocation");
} catch (ex) {}
if ((ToolbarLocation) && (ToolbarLocation == "First")) {
tbIndex = 1;
addBookmarks(bundle, BMSVC, "Toolbar", BMSVC.getBookmarksToolbarFolder(), tbIndex);
addFolder(bundle, BMSVC, "Toolbar", BMSVC.getBookmarksToolbarFolder(), tbIndex);
} else {
addFolder(bundle, BMSVC, "Toolbar", BMSVC.getBookmarksToolbarFolder(), tbIndex);
addBookmarks(bundle, BMSVC, "Toolbar", BMSVC.getBookmarksToolbarFolder(), tbIndex);
}
var BookmarkLocation;
var bmIndex = -1; // last
try {
BookmarkLocation = bundle.GetStringFromName("BookmarkLocation");
} catch (ex) {}
if ((BookmarkLocation) && (BookmarkLocation == "First")) {
bmIndex = 1;
addBookmarks(bundle, BMSVC, "", bmRoot, bmIndex);
addFolder(bundle, BMSVC, "Bookmark", bmRoot, bmIndex);
} else {
addFolder(bundle, BMSVC, "Bookmark", bmRoot, bmIndex);
addBookmarks(bundle, BMSVC, "", bmRoot, bmIndex);
}
OrgName = bundle.GetStringFromName("OrganizationName");
prefbranch.setBoolPref("cck." + OrgName + ".initialized", true);
}
}
,
uninit: function() {
var prefbranch;
try {
prefbranch=Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
} catch (ex) {
return;
}
var sbs;
var bundle;
try {
sbs = Components.classes["@mozilla.org/intl/stringbundle;1"]
.getService(Components.interfaces.nsIStringBundleService );
bundle = sbs.createBundle("chrome://cck/content/cck.properties");
} catch (ex) {
return;
}
id = bundle.GetStringFromName("id");
var rdfs = Components.classes["@mozilla.org/rdf/rdf-service;1"]
.getService(Components.interfaces.nsIRDFService);
var extensionDS= Components.classes["@mozilla.org/extensions/manager;1"]
.getService(Components.interfaces.nsIExtensionManager).datasource;
var extension = rdfs.GetResource("urn:mozilla:item:" + id);
var opTypeArc = rdfs.GetResource("http://www.mozilla.org/2004/em-rdf#opType");
var opType = extensionDS.GetTarget(extension, opTypeArc, true);
opType = opType.QueryInterface(Components.interfaces.nsIRDFLiteral).Value;
if ((opType == "needs-uninstall") || (opType == "needs-disable") || (opType == "needs-upgrade")) {
var i = 1;
do {
try {
prefName = bundle.GetStringFromName("LockPref" + i);
} catch (ex) {
prefName = "";
}
if (prefName && prefName.length) {
prefbranch.unlockPref(prefName);
}
i++;
} while (prefName && prefName.length);
}
/* if hidden is set, unset it */
try {
hidden = bundle.GetStringFromName("hidden");
} catch (ex) {
hidden = "";
}
if (hidden == "true") {
var predicate = rdfs.GetResource("http://www.mozilla.org/2004/em-rdf#hidden");
var target = rdfs.GetLiteral("false");
extensionDS.Assert(extension, predicate, target, true);
}
/* if locked is set, unset it */
try {
locked = bundle.GetStringFromName("locked");
} catch (ex) {
locked = "";
}
if (locked == "true") {
var predicate = rdfs.GetResource("http://www.mozilla.org/2004/em-rdf#locked");
var target = rdfs.GetLiteral("false");
extensionDS.Assert(extension, predicate, target, true);
}
}
}
function addBookmarks(bundle, BMSVC, prefix, folder, location)
{
var BookmarkTitle;
var BookmarkURL;
// items are added in reverse order if we are set to "First"
var start, end, increment;
var i = 1;
var numBookmarks = 0;
while (1) {
try {
var title = bundle.GetStringFromName(prefix + "BookmarkTitle" + i);
} catch (ex) {
break;
}
i++;
numBookmarks++;
}
if (location == -1) {
start = 1;
end = numBookmarks+1;
increment = 1;
} else {
start = numBookmarks;
end = 0;
increment = -1;
}
for (var i=start; i!=end; i+=increment) {
try {
BookmarkTitle = bundle.GetStringFromName(prefix + "BookmarkTitle" + i);
} catch (ex) {
BookmarkTitle = "";
}
if ((BookmarkTitle) && (BookmarkTitle.length)) {
try {
BookmarkURL = bundle.GetStringFromName(prefix + "BookmarkURL" + i);
} catch (ex) {
BookmarkURL = "";
}
if ((BookmarkURL) && (BookmarkURL.length)) {
var bmtype;
try {
bmtype = bundle.GetStringFromName(prefix + "BookmarkType" + i);
} catch (ex) {
bmtype = "";
}
if (bmtype == "live") {
BMSVC.createLivemarkInContainer(BookmarkTitle, "", BookmarkURL, "", folder, location);
} else if (bmtype == "separator") {
try {
BMSVC.createSeparatorInContainer(folder, -1);
} catch (ex) {}
} else {
if (BMSVC.createBookmarkInContainer.length == 8)
BMSVC.createBookmarkInContainer(BookmarkTitle, BookmarkURL, "", "", "", "", folder, location);
else
BMSVC.createBookmarkInContainer(BookmarkTitle, BookmarkURL, "", "", "", folder, location);
}
}
}
}
}
function addFolder(bundle, BMSVC, prefix, folder, location)
{
var BookmarkFolder;
var BookmarkTitle;
var BookmarkURL;
// items are added in reverse order if we are set to "First"
var start, end, increment;
if (location == -1) {
start = 1;
end = 6;
increment = 1;
} else {
start = 5;
end = 0;
increment = -1;
}
// Bookmarks folder with bookmarks
for (var i=start; i!=end; i+=increment) {
try {
BookmarkFolder = bundle.GetStringFromName(prefix + "Folder" + i);
} catch (ex) {
BookmarkFolder = "";
}
if ((BookmarkFolder) && (BookmarkFolder.length)) {
var newfolder;
newfolder = BMSVC.createFolderInContainer(BookmarkFolder, folder, location);
var j = 1;
var numBookmarks = 0;
while (1) {
try {
var title = bundle.GetStringFromName(prefix + "Folder" + i + ".BookmarkTitle" + j);
} catch (ex) {
break;
}
j++;
numBookmarks++;
}
for (var j=1; j<=numBookmarks; j++) {
try {
BookmarkTitle = bundle.GetStringFromName(prefix + "Folder" + i + ".BookmarkTitle" + j);
} catch (ex) {
BookmarkTitle = "";
}
if ((BookmarkTitle) && (BookmarkTitle.length)) {
try {
BookmarkURL = bundle.GetStringFromName(prefix + "Folder" + i + ".BookmarkURL" + j);
} catch (ex) {
BookmarkURL = "";
}
if ((BookmarkURL) && (BookmarkURL.length)) {
var bmtype;
try {
bmtype = bundle.GetStringFromName(prefix + "Folder" + i + ".BookmarkType" + j);
} catch (ex) {
bmtype = "";
}
if (bmtype == "live") {
BMSVC.createLivemarkInContainer(BookmarkTitle, "", BookmarkURL, "", newfolder, -1);
} else if (bmtype == "separator") {
try {
BMSVC.createSeparatorInContainer(neefolder, -1);
} catch (ex) {}
} else {
if (BMSVC.createBookmarkInContainer.length == 8)
BMSVC.createBookmarkInContainer(BookmarkTitle, BookmarkURL, "", "", "", "", newfolder, -1);
else
BMSVC.createBookmarkInContainer(BookmarkTitle, BookmarkURL, "", "", "", newfolder, -1);
}
}
}
}
}
}
}
// XPCOM Scaffolding code
// component defined in this file
const SERVICE_NAME="CCK Service - %OrganizationName%";
const SERVICE_ID="%uuid%";
const SERVICE_CTRID = "@mozilla.org/cck-service-%OrganizationName%;1";
const SERVICE_CONSTRUCTOR=CCKService;
const SERVICE_CID = Components.ID(SERVICE_ID);
// interfaces implemented by this component
const SERVICE_IIDS =
[
Components.interfaces.nsIObserver,
Components.interfaces.nsISupports,
Components.interfaces.nsISupportsWeakReference
];
// Factory object
const SERVICE_FACTORY = {
_instance: new SERVICE_CONSTRUCTOR(),
createInstance: function (outer, iid) {
if (outer != null)
throw Components.results.NS_ERROR_NO_AGGREGATION;
xpcom_checkInterfaces(iid,SERVICE_IIDS,Components.results.NS_ERROR_INVALID_ARG);
// kept this for flexibility sake, but we're really adopting an
// early instantiation and late init singleton pattern
return this._instance==null?this._instance=new SERVICE_CONSTRUCTOR():this._instance;
}
};
function xpcom_checkInterfaces(iid,iids,ex) {
for(var j=iids.length; j-- >0;) {
if(iid.equals(iids[j])) return true;
}
throw ex;
}
// Module
var Module = new Object();
Module.firstTime=true;
Module.registerSelf = function (compMgr, fileSpec, location, type) {
if(this.firstTime) {
debug("*** Registering "+SERVICE_CTRID+".\n");
compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar
).registerFactoryLocation(SERVICE_CID,
SERVICE_NAME,
SERVICE_CTRID,
fileSpec,
location,
type);
Components.classes['@mozilla.org/categorymanager;1'].getService(
Components.interfaces.nsICategoryManager
).addCategoryEntry("app-startup",
SERVICE_NAME, "service," + SERVICE_CTRID, true, true, null);
this.firstTime=false;
}
}
Module.unregisterSelf = function(compMgr, fileSpec, location) {
compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar
).unregisterFactoryLocation(SERVICE_CID, fileSpec);
Components.classes['@mozilla.org/categorymanager;1'].getService(
Components.interfaces.nsICategoryManager
).deleteCategoryEntry("app-startup",SERVICE_NAME, true);
}
Module.getClassObject = function (compMgr, cid, iid) {
if(cid.equals(SERVICE_CID))
return SERVICE_FACTORY;
if (!iid.equals(Components.interfaces.nsIFactory))
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
throw Components.results.NS_ERROR_NO_INTERFACE;
}
Module.canUnload = function(compMgr) {
return true;
}
// entrypoint
function NSGetModule(compMgr, fileSpec) {
return Module;
}