michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, you can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: var EXPORTED_SYMBOLS = ["controller", "utils", "elementslib", "os", michael@0: "getBrowserController", "newBrowserController", michael@0: "getAddonsController", "getPreferencesController", michael@0: "newMail3PaneController", "getMail3PaneController", michael@0: "wm", "platform", "getAddrbkController", michael@0: "getMsgComposeController", "getDownloadsController", michael@0: "Application", "findElement", michael@0: "getPlacesController", 'isMac', 'isLinux', 'isWindows', michael@0: "firePythonCallback", "getAddons" michael@0: ]; michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: michael@0: michael@0: Cu.import("resource://gre/modules/AddonManager.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: // imports michael@0: var assertions = {}; Cu.import('resource://mozmill/modules/assertions.js', assertions); michael@0: var broker = {}; Cu.import('resource://mozmill/driver/msgbroker.js', broker); michael@0: var controller = {}; Cu.import('resource://mozmill/driver/controller.js', controller); michael@0: var elementslib = {}; Cu.import('resource://mozmill/driver/elementslib.js', elementslib); michael@0: var findElement = {}; Cu.import('resource://mozmill/driver/mozelement.js', findElement); michael@0: var os = {}; Cu.import('resource://mozmill/stdlib/os.js', os); michael@0: var utils = {}; Cu.import('resource://mozmill/stdlib/utils.js', utils); michael@0: var windows = {}; Cu.import('resource://mozmill/modules/windows.js', windows); michael@0: michael@0: michael@0: const DEBUG = false; michael@0: michael@0: // This is a useful "check" timer. See utils.js, good for debugging michael@0: if (DEBUG) { michael@0: utils.startTimer(); michael@0: } michael@0: michael@0: var assert = new assertions.Assert(); michael@0: michael@0: // platform information michael@0: var platform = os.getPlatform(); michael@0: var isMac = false; michael@0: var isWindows = false; michael@0: var isLinux = false; michael@0: michael@0: if (platform == "darwin"){ michael@0: isMac = true; michael@0: } michael@0: michael@0: if (platform == "winnt"){ michael@0: isWindows = true; michael@0: } michael@0: michael@0: if (platform == "linux"){ michael@0: isLinux = true; michael@0: } michael@0: michael@0: var wm = Services.wm; michael@0: michael@0: var appInfo = Services.appinfo; michael@0: var Application = utils.applicationName; michael@0: michael@0: michael@0: /** michael@0: * Retrieves the list with information about installed add-ons. michael@0: * michael@0: * @returns {String} JSON data of installed add-ons michael@0: */ michael@0: function getAddons() { michael@0: var addons = null; michael@0: michael@0: AddonManager.getAllAddons(function (addonList) { michael@0: var tmp_list = [ ]; michael@0: michael@0: addonList.forEach(function (addon) { michael@0: var tmp = { }; michael@0: michael@0: // We have to filter out properties of type 'function' of the addon michael@0: // object, which will break JSON.stringify() and result in incomplete michael@0: // addon information. michael@0: for (var key in addon) { michael@0: if (typeof(addon[key]) !== "function") { michael@0: tmp[key] = addon[key]; michael@0: } michael@0: } michael@0: michael@0: tmp_list.push(tmp); michael@0: }); michael@0: michael@0: addons = tmp_list; michael@0: }); michael@0: michael@0: try { michael@0: // Sychronize with getAllAddons so we do not return too early michael@0: assert.waitFor(function () { michael@0: return !!addons; michael@0: }) michael@0: michael@0: return addons; michael@0: } catch (e) { michael@0: return null; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Retrieves application details for the Mozmill report michael@0: * michael@0: * @return {String} JSON data of application details michael@0: */ michael@0: function getApplicationDetails() { michael@0: var locale = Cc["@mozilla.org/chrome/chrome-registry;1"] michael@0: .getService(Ci.nsIXULChromeRegistry) michael@0: .getSelectedLocale("global"); michael@0: michael@0: // Put all our necessary information into JSON and return it: michael@0: // appinfo, startupinfo, and addons michael@0: var details = { michael@0: application_id: appInfo.ID, michael@0: application_name: Application, michael@0: application_version: appInfo.version, michael@0: application_locale: locale, michael@0: platform_buildid: appInfo.platformBuildID, michael@0: platform_version: appInfo.platformVersion, michael@0: addons: getAddons(), michael@0: startupinfo: getStartupInfo(), michael@0: paths: { michael@0: appdata: Services.dirsvc.get('UAppData', Ci.nsIFile).path, michael@0: profile: Services.dirsvc.get('ProfD', Ci.nsIFile).path michael@0: } michael@0: }; michael@0: michael@0: return JSON.stringify(details); michael@0: } michael@0: michael@0: // get startup time if available michael@0: // see http://blog.mozilla.com/tglek/2011/04/26/measuring-startup-speed-correctly/ michael@0: function getStartupInfo() { michael@0: var startupInfo = {}; michael@0: michael@0: try { michael@0: var _startupInfo = Services.startup.getStartupInfo(); michael@0: for (var time in _startupInfo) { michael@0: // convert from Date object to ms since epoch michael@0: startupInfo[time] = _startupInfo[time].getTime(); michael@0: } michael@0: } catch (e) { michael@0: startupInfo = null; michael@0: } michael@0: michael@0: return startupInfo; michael@0: } michael@0: michael@0: michael@0: michael@0: function newBrowserController () { michael@0: return new controller.MozMillController(utils.getMethodInWindows('OpenBrowserWindow')()); michael@0: } michael@0: michael@0: function getBrowserController () { michael@0: var browserWindow = wm.getMostRecentWindow("navigator:browser"); michael@0: michael@0: if (browserWindow == null) { michael@0: return newBrowserController(); michael@0: } else { michael@0: return new controller.MozMillController(browserWindow); michael@0: } michael@0: } michael@0: michael@0: function getPlacesController () { michael@0: utils.getMethodInWindows('PlacesCommandHook').showPlacesOrganizer('AllBookmarks'); michael@0: michael@0: return new controller.MozMillController(wm.getMostRecentWindow('')); michael@0: } michael@0: michael@0: function getAddonsController () { michael@0: if (Application == 'SeaMonkey') { michael@0: utils.getMethodInWindows('toEM')(); michael@0: } michael@0: else if (Application == 'Thunderbird') { michael@0: utils.getMethodInWindows('openAddonsMgr')(); michael@0: } michael@0: else if (Application == 'Sunbird') { michael@0: utils.getMethodInWindows('goOpenAddons')(); michael@0: } else { michael@0: utils.getMethodInWindows('BrowserOpenAddonsMgr')(); michael@0: } michael@0: michael@0: return new controller.MozMillController(wm.getMostRecentWindow('')); michael@0: } michael@0: michael@0: function getDownloadsController() { michael@0: utils.getMethodInWindows('BrowserDownloadsUI')(); michael@0: michael@0: return new controller.MozMillController(wm.getMostRecentWindow('')); michael@0: } michael@0: michael@0: function getPreferencesController() { michael@0: if (Application == 'Thunderbird') { michael@0: utils.getMethodInWindows('openOptionsDialog')(); michael@0: } else { michael@0: utils.getMethodInWindows('openPreferences')(); michael@0: } michael@0: michael@0: return new controller.MozMillController(wm.getMostRecentWindow('')); michael@0: } michael@0: michael@0: // Thunderbird functions michael@0: function newMail3PaneController () { michael@0: return new controller.MozMillController(utils.getMethodInWindows('toMessengerWindow')()); michael@0: } michael@0: michael@0: function getMail3PaneController () { michael@0: var mail3PaneWindow = wm.getMostRecentWindow("mail:3pane"); michael@0: michael@0: if (mail3PaneWindow == null) { michael@0: return newMail3PaneController(); michael@0: } else { michael@0: return new controller.MozMillController(mail3PaneWindow); michael@0: } michael@0: } michael@0: michael@0: // Thunderbird - Address book window michael@0: function newAddrbkController () { michael@0: utils.getMethodInWindows("toAddressBook")(); michael@0: utils.sleep(2000); michael@0: var addyWin = wm.getMostRecentWindow("mail:addressbook"); michael@0: michael@0: return new controller.MozMillController(addyWin); michael@0: } michael@0: michael@0: function getAddrbkController () { michael@0: var addrbkWindow = wm.getMostRecentWindow("mail:addressbook"); michael@0: if (addrbkWindow == null) { michael@0: return newAddrbkController(); michael@0: } else { michael@0: return new controller.MozMillController(addrbkWindow); michael@0: } michael@0: } michael@0: michael@0: function firePythonCallback (filename, method, args, kwargs) { michael@0: obj = {'filename': filename, 'method': method}; michael@0: obj['args'] = args || []; michael@0: obj['kwargs'] = kwargs || {}; michael@0: michael@0: broker.sendMessage("firePythonCallback", obj); michael@0: } michael@0: michael@0: function timer (name) { michael@0: this.name = name; michael@0: this.timers = {}; michael@0: this.actions = []; michael@0: michael@0: frame.timers.push(this); michael@0: } michael@0: michael@0: timer.prototype.start = function (name) { michael@0: this.timers[name].startTime = (new Date).getTime(); michael@0: } michael@0: michael@0: timer.prototype.stop = function (name) { michael@0: var t = this.timers[name]; michael@0: michael@0: t.endTime = (new Date).getTime(); michael@0: t.totalTime = (t.endTime - t.startTime); michael@0: } michael@0: michael@0: timer.prototype.end = function () { michael@0: frame.events.fireEvent("timer", this); michael@0: frame.timers.remove(this); michael@0: } michael@0: michael@0: // Initialization michael@0: michael@0: /** michael@0: * Initialize Mozmill michael@0: */ michael@0: function initialize() { michael@0: windows.init(); michael@0: } michael@0: michael@0: initialize();