services/sync/tps/extensions/mozmill/resource/driver/mozmill.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/sync/tps/extensions/mozmill/resource/driver/mozmill.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,285 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, you can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +var EXPORTED_SYMBOLS = ["controller", "utils", "elementslib", "os",
     1.9 +                        "getBrowserController", "newBrowserController",
    1.10 +                        "getAddonsController", "getPreferencesController",
    1.11 +                        "newMail3PaneController", "getMail3PaneController",
    1.12 +                        "wm", "platform", "getAddrbkController",
    1.13 +                        "getMsgComposeController", "getDownloadsController",
    1.14 +                        "Application", "findElement",
    1.15 +                        "getPlacesController", 'isMac', 'isLinux', 'isWindows',
    1.16 +                        "firePythonCallback", "getAddons"
    1.17 +                       ];
    1.18 +
    1.19 +const Cc = Components.classes;
    1.20 +const Ci = Components.interfaces;
    1.21 +const Cu = Components.utils;
    1.22 +
    1.23 +
    1.24 +Cu.import("resource://gre/modules/AddonManager.jsm");
    1.25 +Cu.import("resource://gre/modules/Services.jsm");
    1.26 +
    1.27 +// imports
    1.28 +var assertions = {};  Cu.import('resource://mozmill/modules/assertions.js', assertions);
    1.29 +var broker = {};      Cu.import('resource://mozmill/driver/msgbroker.js', broker);
    1.30 +var controller = {};  Cu.import('resource://mozmill/driver/controller.js', controller);
    1.31 +var elementslib = {}; Cu.import('resource://mozmill/driver/elementslib.js', elementslib);
    1.32 +var findElement = {}; Cu.import('resource://mozmill/driver/mozelement.js', findElement);
    1.33 +var os = {};          Cu.import('resource://mozmill/stdlib/os.js', os);
    1.34 +var utils = {};       Cu.import('resource://mozmill/stdlib/utils.js', utils);
    1.35 +var windows = {};     Cu.import('resource://mozmill/modules/windows.js', windows);
    1.36 +
    1.37 +
    1.38 +const DEBUG = false;
    1.39 +
    1.40 +// This is a useful "check" timer. See utils.js, good for debugging
    1.41 +if (DEBUG) {
    1.42 +  utils.startTimer();
    1.43 +}
    1.44 +
    1.45 +var assert = new assertions.Assert();
    1.46 +
    1.47 +// platform information
    1.48 +var platform = os.getPlatform();
    1.49 +var isMac = false;
    1.50 +var isWindows = false;
    1.51 +var isLinux = false;
    1.52 +
    1.53 +if (platform == "darwin"){
    1.54 +  isMac = true;
    1.55 +}
    1.56 +
    1.57 +if (platform == "winnt"){
    1.58 +  isWindows = true;
    1.59 +}
    1.60 +
    1.61 +if (platform == "linux"){
    1.62 +  isLinux = true;
    1.63 +}
    1.64 +
    1.65 +var wm = Services.wm;
    1.66 +
    1.67 +var appInfo = Services.appinfo;
    1.68 +var Application = utils.applicationName;
    1.69 +
    1.70 +
    1.71 +/**
    1.72 + * Retrieves the list with information about installed add-ons.
    1.73 + *
    1.74 + * @returns {String} JSON data of installed add-ons
    1.75 + */
    1.76 +function getAddons() {
    1.77 +  var addons = null;
    1.78 +
    1.79 +  AddonManager.getAllAddons(function (addonList) {
    1.80 +    var tmp_list = [ ];
    1.81 +
    1.82 +    addonList.forEach(function (addon) {
    1.83 +      var tmp = { };
    1.84 +
    1.85 +      // We have to filter out properties of type 'function' of the addon
    1.86 +      // object, which will break JSON.stringify() and result in incomplete
    1.87 +      // addon information.
    1.88 +      for (var key in addon) {
    1.89 +        if (typeof(addon[key]) !== "function") {
    1.90 +          tmp[key] = addon[key];
    1.91 +        }
    1.92 +      }
    1.93 +
    1.94 +      tmp_list.push(tmp);
    1.95 +    });
    1.96 +
    1.97 +    addons = tmp_list;
    1.98 +  });
    1.99 +
   1.100 +  try {
   1.101 +    // Sychronize with getAllAddons so we do not return too early
   1.102 +    assert.waitFor(function () {
   1.103 +      return !!addons;
   1.104 +    })
   1.105 +
   1.106 +    return addons;
   1.107 +  } catch (e) {
   1.108 +    return null;
   1.109 +  }
   1.110 +}
   1.111 +
   1.112 +/**
   1.113 + * Retrieves application details for the Mozmill report
   1.114 + *
   1.115 + * @return {String} JSON data of application details
   1.116 + */
   1.117 +function getApplicationDetails() {
   1.118 +  var locale = Cc["@mozilla.org/chrome/chrome-registry;1"]
   1.119 +               .getService(Ci.nsIXULChromeRegistry)
   1.120 +               .getSelectedLocale("global");
   1.121 +
   1.122 +  // Put all our necessary information into JSON and return it:
   1.123 +  // appinfo, startupinfo, and addons
   1.124 +  var details = {
   1.125 +    application_id: appInfo.ID,
   1.126 +    application_name: Application,
   1.127 +    application_version: appInfo.version,
   1.128 +    application_locale: locale,
   1.129 +    platform_buildid: appInfo.platformBuildID,
   1.130 +    platform_version: appInfo.platformVersion,
   1.131 +    addons: getAddons(),
   1.132 +    startupinfo: getStartupInfo(),
   1.133 +    paths: {
   1.134 +      appdata: Services.dirsvc.get('UAppData', Ci.nsIFile).path,
   1.135 +      profile: Services.dirsvc.get('ProfD', Ci.nsIFile).path
   1.136 +    }
   1.137 +  };
   1.138 +
   1.139 +  return JSON.stringify(details);
   1.140 +}
   1.141 +
   1.142 +// get startup time if available
   1.143 +// see http://blog.mozilla.com/tglek/2011/04/26/measuring-startup-speed-correctly/
   1.144 +function getStartupInfo() {
   1.145 +  var startupInfo = {};
   1.146 +
   1.147 +  try {
   1.148 +    var _startupInfo = Services.startup.getStartupInfo();
   1.149 +    for (var time in _startupInfo) {
   1.150 +      // convert from Date object to ms since epoch
   1.151 +      startupInfo[time] = _startupInfo[time].getTime();
   1.152 +    }
   1.153 +  } catch (e) {
   1.154 +    startupInfo = null;
   1.155 +  }
   1.156 +
   1.157 +  return startupInfo;
   1.158 +}
   1.159 +
   1.160 +
   1.161 +
   1.162 +function newBrowserController () {
   1.163 +  return new controller.MozMillController(utils.getMethodInWindows('OpenBrowserWindow')());
   1.164 +}
   1.165 +
   1.166 +function getBrowserController () {
   1.167 +  var browserWindow = wm.getMostRecentWindow("navigator:browser");
   1.168 +
   1.169 +  if (browserWindow == null) {
   1.170 +    return newBrowserController();
   1.171 +  } else {
   1.172 +    return new controller.MozMillController(browserWindow);
   1.173 +  }
   1.174 +}
   1.175 +
   1.176 +function getPlacesController () {
   1.177 +  utils.getMethodInWindows('PlacesCommandHook').showPlacesOrganizer('AllBookmarks');
   1.178 +
   1.179 +  return new controller.MozMillController(wm.getMostRecentWindow(''));
   1.180 +}
   1.181 +
   1.182 +function getAddonsController () {
   1.183 +  if (Application == 'SeaMonkey') {
   1.184 +    utils.getMethodInWindows('toEM')();
   1.185 +  }
   1.186 +  else if (Application == 'Thunderbird') {
   1.187 +    utils.getMethodInWindows('openAddonsMgr')();
   1.188 +  }
   1.189 +  else if (Application == 'Sunbird') {
   1.190 +    utils.getMethodInWindows('goOpenAddons')();
   1.191 +  } else {
   1.192 +    utils.getMethodInWindows('BrowserOpenAddonsMgr')();
   1.193 +  }
   1.194 +
   1.195 +  return new controller.MozMillController(wm.getMostRecentWindow(''));
   1.196 +}
   1.197 +
   1.198 +function getDownloadsController() {
   1.199 +  utils.getMethodInWindows('BrowserDownloadsUI')();
   1.200 +
   1.201 +  return new controller.MozMillController(wm.getMostRecentWindow(''));
   1.202 +}
   1.203 +
   1.204 +function getPreferencesController() {
   1.205 +  if (Application == 'Thunderbird') {
   1.206 +    utils.getMethodInWindows('openOptionsDialog')();
   1.207 +  } else {
   1.208 +    utils.getMethodInWindows('openPreferences')();
   1.209 +  }
   1.210 +
   1.211 +  return new controller.MozMillController(wm.getMostRecentWindow(''));
   1.212 +}
   1.213 +
   1.214 +// Thunderbird functions
   1.215 +function newMail3PaneController () {
   1.216 +  return new controller.MozMillController(utils.getMethodInWindows('toMessengerWindow')());
   1.217 +}
   1.218 +
   1.219 +function getMail3PaneController () {
   1.220 +  var mail3PaneWindow = wm.getMostRecentWindow("mail:3pane");
   1.221 +
   1.222 +  if (mail3PaneWindow == null) {
   1.223 +    return newMail3PaneController();
   1.224 +  } else {
   1.225 +    return new controller.MozMillController(mail3PaneWindow);
   1.226 +  }
   1.227 +}
   1.228 +
   1.229 +// Thunderbird - Address book window
   1.230 +function newAddrbkController () {
   1.231 +  utils.getMethodInWindows("toAddressBook")();
   1.232 +  utils.sleep(2000);
   1.233 +  var addyWin = wm.getMostRecentWindow("mail:addressbook");
   1.234 +
   1.235 +  return new controller.MozMillController(addyWin);
   1.236 +}
   1.237 +
   1.238 +function getAddrbkController () {
   1.239 +  var addrbkWindow = wm.getMostRecentWindow("mail:addressbook");
   1.240 +  if (addrbkWindow == null) {
   1.241 +    return newAddrbkController();
   1.242 +  } else {
   1.243 +    return new controller.MozMillController(addrbkWindow);
   1.244 +  }
   1.245 +}
   1.246 +
   1.247 +function firePythonCallback (filename, method, args, kwargs) {
   1.248 +  obj = {'filename': filename, 'method': method};
   1.249 +  obj['args'] = args || [];
   1.250 +  obj['kwargs'] = kwargs || {};
   1.251 +
   1.252 +  broker.sendMessage("firePythonCallback", obj);
   1.253 +}
   1.254 +
   1.255 +function timer (name) {
   1.256 +  this.name = name;
   1.257 +  this.timers = {};
   1.258 +  this.actions = [];
   1.259 +
   1.260 +  frame.timers.push(this);
   1.261 +}
   1.262 +
   1.263 +timer.prototype.start = function (name) {
   1.264 +  this.timers[name].startTime = (new Date).getTime();
   1.265 +}
   1.266 +
   1.267 +timer.prototype.stop = function (name) {
   1.268 +  var t = this.timers[name];
   1.269 +
   1.270 +  t.endTime = (new Date).getTime();
   1.271 +  t.totalTime = (t.endTime - t.startTime);
   1.272 +}
   1.273 +
   1.274 +timer.prototype.end = function () {
   1.275 +  frame.events.fireEvent("timer", this);
   1.276 +  frame.timers.remove(this);
   1.277 +}
   1.278 +
   1.279 +// Initialization
   1.280 +
   1.281 +/**
   1.282 + * Initialize Mozmill
   1.283 + */
   1.284 +function initialize() {
   1.285 +  windows.init();
   1.286 +}
   1.287 +
   1.288 +initialize();

mercurial