Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, you can obtain one at http://mozilla.org/MPL/2.0/. */
5 var EXPORTED_SYMBOLS = ["controller", "utils", "elementslib", "os",
6 "getBrowserController", "newBrowserController",
7 "getAddonsController", "getPreferencesController",
8 "newMail3PaneController", "getMail3PaneController",
9 "wm", "platform", "getAddrbkController",
10 "getMsgComposeController", "getDownloadsController",
11 "Application", "findElement",
12 "getPlacesController", 'isMac', 'isLinux', 'isWindows',
13 "firePythonCallback", "getAddons"
14 ];
16 const Cc = Components.classes;
17 const Ci = Components.interfaces;
18 const Cu = Components.utils;
21 Cu.import("resource://gre/modules/AddonManager.jsm");
22 Cu.import("resource://gre/modules/Services.jsm");
24 // imports
25 var assertions = {}; Cu.import('resource://mozmill/modules/assertions.js', assertions);
26 var broker = {}; Cu.import('resource://mozmill/driver/msgbroker.js', broker);
27 var controller = {}; Cu.import('resource://mozmill/driver/controller.js', controller);
28 var elementslib = {}; Cu.import('resource://mozmill/driver/elementslib.js', elementslib);
29 var findElement = {}; Cu.import('resource://mozmill/driver/mozelement.js', findElement);
30 var os = {}; Cu.import('resource://mozmill/stdlib/os.js', os);
31 var utils = {}; Cu.import('resource://mozmill/stdlib/utils.js', utils);
32 var windows = {}; Cu.import('resource://mozmill/modules/windows.js', windows);
35 const DEBUG = false;
37 // This is a useful "check" timer. See utils.js, good for debugging
38 if (DEBUG) {
39 utils.startTimer();
40 }
42 var assert = new assertions.Assert();
44 // platform information
45 var platform = os.getPlatform();
46 var isMac = false;
47 var isWindows = false;
48 var isLinux = false;
50 if (platform == "darwin"){
51 isMac = true;
52 }
54 if (platform == "winnt"){
55 isWindows = true;
56 }
58 if (platform == "linux"){
59 isLinux = true;
60 }
62 var wm = Services.wm;
64 var appInfo = Services.appinfo;
65 var Application = utils.applicationName;
68 /**
69 * Retrieves the list with information about installed add-ons.
70 *
71 * @returns {String} JSON data of installed add-ons
72 */
73 function getAddons() {
74 var addons = null;
76 AddonManager.getAllAddons(function (addonList) {
77 var tmp_list = [ ];
79 addonList.forEach(function (addon) {
80 var tmp = { };
82 // We have to filter out properties of type 'function' of the addon
83 // object, which will break JSON.stringify() and result in incomplete
84 // addon information.
85 for (var key in addon) {
86 if (typeof(addon[key]) !== "function") {
87 tmp[key] = addon[key];
88 }
89 }
91 tmp_list.push(tmp);
92 });
94 addons = tmp_list;
95 });
97 try {
98 // Sychronize with getAllAddons so we do not return too early
99 assert.waitFor(function () {
100 return !!addons;
101 })
103 return addons;
104 } catch (e) {
105 return null;
106 }
107 }
109 /**
110 * Retrieves application details for the Mozmill report
111 *
112 * @return {String} JSON data of application details
113 */
114 function getApplicationDetails() {
115 var locale = Cc["@mozilla.org/chrome/chrome-registry;1"]
116 .getService(Ci.nsIXULChromeRegistry)
117 .getSelectedLocale("global");
119 // Put all our necessary information into JSON and return it:
120 // appinfo, startupinfo, and addons
121 var details = {
122 application_id: appInfo.ID,
123 application_name: Application,
124 application_version: appInfo.version,
125 application_locale: locale,
126 platform_buildid: appInfo.platformBuildID,
127 platform_version: appInfo.platformVersion,
128 addons: getAddons(),
129 startupinfo: getStartupInfo(),
130 paths: {
131 appdata: Services.dirsvc.get('UAppData', Ci.nsIFile).path,
132 profile: Services.dirsvc.get('ProfD', Ci.nsIFile).path
133 }
134 };
136 return JSON.stringify(details);
137 }
139 // get startup time if available
140 // see http://blog.mozilla.com/tglek/2011/04/26/measuring-startup-speed-correctly/
141 function getStartupInfo() {
142 var startupInfo = {};
144 try {
145 var _startupInfo = Services.startup.getStartupInfo();
146 for (var time in _startupInfo) {
147 // convert from Date object to ms since epoch
148 startupInfo[time] = _startupInfo[time].getTime();
149 }
150 } catch (e) {
151 startupInfo = null;
152 }
154 return startupInfo;
155 }
159 function newBrowserController () {
160 return new controller.MozMillController(utils.getMethodInWindows('OpenBrowserWindow')());
161 }
163 function getBrowserController () {
164 var browserWindow = wm.getMostRecentWindow("navigator:browser");
166 if (browserWindow == null) {
167 return newBrowserController();
168 } else {
169 return new controller.MozMillController(browserWindow);
170 }
171 }
173 function getPlacesController () {
174 utils.getMethodInWindows('PlacesCommandHook').showPlacesOrganizer('AllBookmarks');
176 return new controller.MozMillController(wm.getMostRecentWindow(''));
177 }
179 function getAddonsController () {
180 if (Application == 'SeaMonkey') {
181 utils.getMethodInWindows('toEM')();
182 }
183 else if (Application == 'Thunderbird') {
184 utils.getMethodInWindows('openAddonsMgr')();
185 }
186 else if (Application == 'Sunbird') {
187 utils.getMethodInWindows('goOpenAddons')();
188 } else {
189 utils.getMethodInWindows('BrowserOpenAddonsMgr')();
190 }
192 return new controller.MozMillController(wm.getMostRecentWindow(''));
193 }
195 function getDownloadsController() {
196 utils.getMethodInWindows('BrowserDownloadsUI')();
198 return new controller.MozMillController(wm.getMostRecentWindow(''));
199 }
201 function getPreferencesController() {
202 if (Application == 'Thunderbird') {
203 utils.getMethodInWindows('openOptionsDialog')();
204 } else {
205 utils.getMethodInWindows('openPreferences')();
206 }
208 return new controller.MozMillController(wm.getMostRecentWindow(''));
209 }
211 // Thunderbird functions
212 function newMail3PaneController () {
213 return new controller.MozMillController(utils.getMethodInWindows('toMessengerWindow')());
214 }
216 function getMail3PaneController () {
217 var mail3PaneWindow = wm.getMostRecentWindow("mail:3pane");
219 if (mail3PaneWindow == null) {
220 return newMail3PaneController();
221 } else {
222 return new controller.MozMillController(mail3PaneWindow);
223 }
224 }
226 // Thunderbird - Address book window
227 function newAddrbkController () {
228 utils.getMethodInWindows("toAddressBook")();
229 utils.sleep(2000);
230 var addyWin = wm.getMostRecentWindow("mail:addressbook");
232 return new controller.MozMillController(addyWin);
233 }
235 function getAddrbkController () {
236 var addrbkWindow = wm.getMostRecentWindow("mail:addressbook");
237 if (addrbkWindow == null) {
238 return newAddrbkController();
239 } else {
240 return new controller.MozMillController(addrbkWindow);
241 }
242 }
244 function firePythonCallback (filename, method, args, kwargs) {
245 obj = {'filename': filename, 'method': method};
246 obj['args'] = args || [];
247 obj['kwargs'] = kwargs || {};
249 broker.sendMessage("firePythonCallback", obj);
250 }
252 function timer (name) {
253 this.name = name;
254 this.timers = {};
255 this.actions = [];
257 frame.timers.push(this);
258 }
260 timer.prototype.start = function (name) {
261 this.timers[name].startTime = (new Date).getTime();
262 }
264 timer.prototype.stop = function (name) {
265 var t = this.timers[name];
267 t.endTime = (new Date).getTime();
268 t.totalTime = (t.endTime - t.startTime);
269 }
271 timer.prototype.end = function () {
272 frame.events.fireEvent("timer", this);
273 frame.timers.remove(this);
274 }
276 // Initialization
278 /**
279 * Initialize Mozmill
280 */
281 function initialize() {
282 windows.init();
283 }
285 initialize();