/* ***** BEGIN LICENSE BLOCK ***** Copyright © 2009 Center for History and New Media George Mason University, Fairfax, Virginia, USA http://zotero.org This file is part of Zotero. Zotero is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Zotero is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with Zotero. If not, see . Based on nsChromeExtensionHandler example code by Ed Anuff at http://kb.mozillazine.org/Dev_:_Extending_the_Chrome_Protocol ***** END LICENSE BLOCK ***** */ const Cc = Components.classes; const Ci = Components.interfaces; const Cr = Components.results; const Cu = Components.utils; /** XPCOM files to be loaded for all modes **/ const xpcomFilesAll = [ 'zotero', 'intl', 'prefs', 'dataDirectory', 'debug', 'error', 'utilities/date', 'utilities/utilities', 'utilities/utilities_item', 'utilities/openurl', 'utilities/xregexp-all', 'utilities/xregexp-unicode-zotero', 'utilities_internal', 'translate/src/utilities_translate', 'file', 'http', 'mimeTypeHandler', 'pdfWorker/manager', 'ipc', 'prompt', 'profile', 'progressWindow', 'proxy', 'translate/src/translation/translate', 'translate/src/translator', 'translate/src/tlds', 'translation/translate_firefox', 'isbn', 'preferencePanes', 'uiProperties', ]; /** XPCOM files to be loaded only for local translation and DB access **/ const xpcomFilesLocal = [ 'collectionTreeRow', 'annotations', 'api', 'attachments', 'browserDownload', 'cite', 'citeprocRsBridge', 'cookieSandbox', 'data/library', 'data/libraries', 'data/dataObject', 'data/dataObjects', 'data/dataObjectUtilities', 'data/cachedTypes', 'data/notes', 'data/item', 'data/items', 'data/collection', 'data/collections', 'data/feedItem', 'data/feedItems', 'data/feed', 'data/feeds', 'data/creators', 'data/group', 'data/groups', 'data/itemFields', 'data/relations', 'data/search', 'data/searchConditions', 'data/searches', 'data/tags', 'db', 'dictionaries', 'duplicates', 'editorInstance', 'feedReader', 'fileDragDataProvider', 'fulltext', 'id', 'integration', 'locale', 'locateManager', 'mime', 'notifier', 'fileHandlers', 'plugins', 'reader', 'progressQueue', 'progressQueueDialog', 'quickCopy', 'recognizeDocument', 'report', 'retractions', 'router', 'schema', 'server', 'server_integration', 'session', 'streamer', 'style', 'sync', 'sync/syncAPIClient', 'sync/syncEngine', 'sync/syncExceptions', 'sync/syncEventListeners', 'sync/syncFullTextEngine', 'sync/syncLocal', 'sync/syncRunner', 'sync/syncUtilities', 'storage', 'storage/storageEngine', 'storage/storageLocal', 'storage/storageRequest', 'storage/storageResult', 'storage/storageUtilities', 'storage/streamListener', 'storage/zfs', 'storage/webdav', 'syncedSettings', 'timeline', 'uri', 'users', 'translation/translate_item', 'translation/translators', 'connector/httpIntegrationClient', 'connector/server_connector', 'connector/server_connectorIntegration', 'itemTreeManager', ]; Components.utils.import("resource://gre/modules/ComponentUtils.jsm"); const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); var instanceID = (new Date()).getTime(); var isFirstLoadThisSession = true; var zContext = null; var initCallbacks = []; var zInitOptions = {}; // Components.utils.import('resource://zotero/require.js'); // Not using Cu.import here since we don't want the require module to be cached // for includes within ZoteroPane or other code, where we want the window instance available to modules. Components.classes["@mozilla.org/moz/jssubscript-loader;1"] .getService(Components.interfaces.mozIJSSubScriptLoader) .loadSubScript('resource://zotero/require.js'); var ZoteroContext = function() {} ZoteroContext.prototype = { require, /** * Convenience method to replicate window.alert() **/ // TODO: is this still used? if so, move to zotero.js "alert":function alert(msg){ this.Zotero.debug("alert() is deprecated from Zotero XPCOM"); Cc["@mozilla.org/embedcomp/prompt-service;1"] .getService(Ci.nsIPromptService) .alert(null, "", msg); }, /** * Convenience method to replicate window.confirm() **/ // TODO: is this still used? if so, move to zotero.js "confirm":function confirm(msg){ this.Zotero.debug("confirm() is deprecated from Zotero XPCOM"); return Cc["@mozilla.org/embedcomp/prompt-service;1"] .getService(Ci.nsIPromptService) .confirm(null, "", msg); }, "Cc":Cc, "Ci":Ci, /** * Switches in or out of connector mode */ "switchConnectorMode":function(isConnector) { if(isConnector !== this.isConnector) { Services.obs.notifyObservers(zContext.Zotero, "zotero-before-reload", isConnector ? "connector" : "full"); zContext.Zotero.shutdown().then(function() { // create a new zContext makeZoteroContext(isConnector); return zContext.Zotero.init(zInitOptions); }).done(); } return zContext; }, /** * Shuts down Zotero, calls a callback (that may return a promise), * then reinitializes Zotero. Returns a promise that is resolved * when this process completes. */ "reinit":function(cb, isConnector, options = {}) { Services.obs.notifyObservers(zContext.Zotero, "zotero-before-reload", isConnector ? "connector" : "full"); return zContext.Zotero.shutdown().then(function() { return cb ? cb() : false; }).finally(function() { makeZoteroContext(isConnector); var o = {}; Object.assign(o, zInitOptions); Object.assign(o, options); return zContext.Zotero.init(o); }); } }; const { XPCOMUtils } = ChromeUtils.import( "resource://gre/modules/XPCOMUtils.jsm" ); XPCOMUtils.defineLazyModuleGetters(ZoteroContext.prototype, { setTimeout: "resource://gre/modules/Timer.jsm", clearTimeout: "resource://gre/modules/Timer.jsm", setInterval: "resource://gre/modules/Timer.jsm", clearInterval: "resource://gre/modules/Timer.jsm", requestIdleCallback: "resource://gre/modules/Timer.jsm", cancelIdleCallback: "resource://gre/modules/Timer.jsm", }); /** * The class from which the Zotero global XPCOM context is constructed * * @constructor * This runs when ZoteroService is first requested to load all applicable scripts and initialize * Zotero. Calls to other XPCOM components must be in here rather than in top-level code, as other * components may not have yet been initialized. */ function makeZoteroContext(isConnector) { if(zContext) { // Swap out old zContext var oldzContext = zContext; // Create new zContext zContext = new ZoteroContext(); // Swap in old Zotero object, so that references don't break, but empty it zContext.Zotero = oldzContext.Zotero; for(var key in zContext.Zotero) delete zContext.Zotero[key]; } else { zContext = new ZoteroContext(); zContext.Zotero = function() {}; } var subscriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"].getService(Ci.mozIJSSubScriptLoader); // Load zotero.js first subscriptLoader.loadSubScript("chrome://zotero/content/xpcom/" + xpcomFilesAll[0] + ".js", zContext, 'utf-8'); // Load CiteProc into Zotero.CiteProc namespace zContext.Zotero.CiteProc = {"Zotero":zContext.Zotero}; subscriptLoader.loadSubScript("chrome://zotero/content/xpcom/citeproc.js", zContext.Zotero.CiteProc, 'utf-8'); // Load XRegExp object into Zotero.XRegExp const xregexpFiles = [ /**Core functions**/ 'xregexp-all', 'xregexp-unicode-zotero' //adds support for some Unicode categories used in Zotero ]; for (var i=0; i