!import
1 /* -*- indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is the Mozilla Firefox browser.
16 *
17 * The Initial Developer of the Original Code is
18 * Benjamin Smedberg <benjamin@smedbergs.us>
19 *
20 * Portions created by the Initial Developer are Copyright (C) 2004
21 * the Initial Developer. All Rights Reserved.
22 *
23 * Contributor(s):
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39 const nsISupports = Components.interfaces.nsISupports;
40
41 const nsICommandLine = Components.interfaces.nsICommandLine;
42 const nsICommandLineHandler = Components.interfaces.nsICommandLineHandler;
43 const nsICommandLineValidator = Components.interfaces.nsICommandLineValidator;
44 const nsIDOMWindowInternal = Components.interfaces.nsIDOMWindowInternal;
45 const nsIFactory = Components.interfaces.nsIFactory;
46 const nsIFileURL = Components.interfaces.nsIFileURL;
47 const nsINetUtil = Components.interfaces.nsINetUtil;
48 const nsISupportsString = Components.interfaces.nsISupportsString;
49 const nsIURILoader = Components.interfaces.nsIURILoader;
50 const nsIWindowMediator = Components.interfaces.nsIWindowMediator;
51 const nsIWindowWatcher = Components.interfaces.nsIWindowWatcher;
52
53 const NS_ERROR_ABORT = Components.results.NS_ERROR_ABORT;
54
55 const URI_INHERITS_SECURITY_CONTEXT = Components.interfaces.nsIProtocolHandler
56 .URI_INHERITS_SECURITY_CONTEXT;
57
resolveURIInternal
58 function resolveURIInternal(aCmdLine, aArgument) {
59 var uri = aCmdLine.resolveURI(aArgument);
60
61 if (!(uri instanceof nsIFileURL)) {
62 return uri;
63 }
64
65 try {
66 if (uri.file.exists())
67 return uri;
68 }
69 catch (e) {
70 Components.utils.reportError(e);
71 }
72
73 // We have interpreted the argument as a relative file URI, but the file
74 // doesn't exist. Try URI fixup heuristics: see bug 290782.
75
76 try {
77 var urifixup = Components.classes["@mozilla.org/docshell/urifixup;1"]
78 .getService(nsIURIFixup);
79
80 uri = urifixup.createFixupURI(aArgument, 0);
81 }
82 catch (e) {
83 Components.utils.reportError(e);
84 }
85
86 return uri;
87 }
88
mayOpenURI
89 function mayOpenURI(uri)
90 {
91 var ext = Components.classes["@mozilla.org/uriloader/external-protocol-service;1"]
92 .getService(Components.interfaces.nsIExternalProtocolService);
93
94 return ext.isExposedProtocol(uri.scheme);
95 }
96
openURI
97 function openURI(uri)
98 {
99 if (!mayOpenURI(uri))
100 throw Components.results.NS_ERROR_FAILURE;
101
102 var io = Components.classes["@mozilla.org/network/io-service;1"]
103 .getService(Components.interfaces.nsIIOService);
104 var channel = io.newChannelFromURI(uri);
105 var loader = Components.classes["@mozilla.org/uriloader;1"]
106 .getService(Components.interfaces.nsIURILoader);
107
108 // We cannot load a URI on startup asynchronously without protecting
109 // the startup
110
111 var loadgroup = Components.classes["@mozilla.org/network/load-group;1"]
112 .createInstance(Components.interfaces.nsILoadGroup);
113
114 var appstartup = Components.classes["@mozilla.org/toolkit/app-startup;1"]
115 .getService(Components.interfaces.nsIAppStartup);
116
117 var loadlistener = {
ll_start
118 onStartRequest: function ll_start(aRequest, aContext) {
119 appstartup.enterLastWindowClosingSurvivalArea();
120 },
121
ll_stop
122 onStopRequest: function ll_stop(aRequest, aContext, aStatusCode) {
123 appstartup.exitLastWindowClosingSurvivalArea();
124 },
125
ll_QI
126 QueryInterface: function ll_QI(iid) {
127 if (iid.equals(nsISupports) ||
128 iid.equals(Components.interfaces.nsIRequestObserver) ||
129 iid.equals(Components.interfaces.nsISupportsWeakReference))
130 return this;
131
132 throw Components.results.NS_ERROR_NO_INTERFACE;
133 }
134 };
135
136 loadgroup.groupObserver = loadlistener;
137
138 var listener = {
onStartURIOpen
139 onStartURIOpen: function(uri) { return false; },
doContent
140 doContent: function(ctype, preferred, request, handler) { return false; },
isPreferred
141 isPreferred: function(ctype, desired) { return false; },
canHandleContent
142 canHandleContent: function(ctype, preferred, desired) { return false; },
143 loadCookie: null,
144 parentContentListener: null,
getInterface
145 getInterface: function(iid) {
146 if (iid.equals(Components.interfaces.nsIURIContentListener))
147 return this;
148
149 if (iid.equals(Components.interfaces.nsILoadGroup))
150 return loadgroup;
151
152 throw Components.results.NS_ERROR_NO_INTERFACE;
153 }
154 };
155 loader.openURI(channel, true, listener);
156 }
157
158 var nsMailDefaultHandler = {
159 /* nsISupports */
160
mdh_QI
Called: Object:equals (5 calls, 50 v-uS)
161 QueryInterface : function mdh_QI(iid) {
162 if (iid.equals(nsICommandLineHandler) ||
163 iid.equals(nsICommandLineValidator) ||
164 iid.equals(nsIFactory) ||
165 iid.equals(nsISupports))
166 return this;
167
168 throw Components.results.NS_ERROR_NO_INTERFACE;
169 },
170
171 /* nsICommandLineHandler */
172
mdh_handle
173 handle : function mdh_handle(cmdLine) {
174 var uri;
175
176 try {
177 var remoteCommand = cmdLine.handleFlagWithParam("remote", true);
178 }
179 catch (e) {
180 throw NS_ERROR_ABORT;
181 }
182
183 if (remoteCommand != null) {
184 try {
185 var a = /^\s*(\w+)\(([^\)]*)\)\s*$/.exec(remoteCommand);
186 var remoteVerb = a[1].toLowerCase();
187 var remoteParams = a[2].split(",");
188
189 switch (remoteVerb) {
190 case "openurl":
191 var xuri = cmdLine.resolveURI(remoteParams[0]);
192 openURI(xuri);
193 break;
194
195 case "mailto":
196 var xuri = cmdLine.resolveURI("mailto:" + remoteParams[0]);
197 openURI(xuri);
198 break;
199
200 case "xfedocommand":
201 // xfeDoCommand(openBrowser)
202 switch (remoteParams[0].toLowerCase()) {
203 case "openinbox":
204 var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
205 .getService(nsIWindowMediator);
206 var win = wm.getMostRecentWindow("mail:3pane");
207 if (win) {
208 win.focus();
209 }
210 else {
211 var wwatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
212 .getService(nsIWindowWatcher);
213
214 // Bug 277798 - we have to pass an argument to openWindow(), or
215 // else it won't honor the dialog=no instruction.
216 var argstring = Components.classes["@mozilla.org/supports-string;1"]
217 .createInstance(nsISupportsString);
218 wwatch.openWindow(null, "chrome://messenger/content/", "_blank",
219 "chrome,dialog=no,all", argstring);
220 }
221 break;
222
223 case "composemessage":
224 var wwatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
225 .getService(nsIWindowWatcher);
226 var argstring = Components.classes["@mozilla.org/supports-string;1"]
227 .createInstance(nsISupportsString);
228 remoteParams.shift();
229 argstring.data = remoteParams.join(",");
230 wwatch.openWindow(null, "chrome://messenger/content/messengercompose/messengercompose.xul", "_blank",
231 "chrome,dialog=no,all", argstring);
232 break;
233
234 default:
235 throw Components.results.NS_ERROR_ABORT;
236 }
237 break;
238
239 default:
240 // Somebody sent us a remote command we don't know how to process:
241 // just abort.
242 throw Components.results.NS_ERROR_ABORT;
243 }
244
245 cmdLine.preventDefault = true;
246 }
247 catch (e) {
248 // If we had a -remote flag but failed to process it, throw
249 // NS_ERROR_ABORT so that the xremote code knows to return a failure
250 // back to the handling code.
251 dump(e);
252 throw Components.results.NS_ERROR_ABORT;
253 }
254 }
255
256 var chromeParam = cmdLine.handleFlagWithParam("chrome", false);
257 if (chromeParam) {
258 try {
259 var features = "chrome,dialog=no,all";
260 var wwatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
261 .getService(nsIWindowWatcher);
262 var argstring = Components.classes["@mozilla.org/supports-string;1"]
263 .createInstance(nsISupportsString);
264 var uri = resolveURIInternal(cmdLine, chromeParam);
265 var netutil = Components.classes["@mozilla.org/network/util;1"]
266 .getService(nsINetUtil);
267 // only load URIs which do not inherit chrome privs
268 if (!netutil.URIChainHasFlags(uri, URI_INHERITS_SECURITY_CONTEXT)) {
269 wwatch.openWindow(null, uri.spec, "_blank",
270 "chrome,dialog=no,all", argstring);
271 cmdLine.preventDefault = true;
272 }
273 }
274 catch (e) {
275 dump(e);
276 }
277 }
278
279 var count = cmdLine.length;
280 if (count) {
281 var i = 0;
282 while (i < count) {
283 var curarg = cmdLine.getArgument(i);
284 if (!curarg.match(/^-/))
285 break;
286
287 dump ("Warning: unrecognized command line flag " + curarg + "\n");
288 // To emulate the pre-nsICommandLine behavior, we ignore the
289 // argument after an unrecognized flag.
290 i += 2;
291 // xxxbsmedberg: make me use the console service!
292 }
293
294 if (i < count) {
295 uri = cmdLine.getArgument(i);
296
297 // mailto: URIs are frequently passed with spaces in them. They should be
298 // escaped into %20, but we hack around bad clients, see bug 231032
299 if (uri.match(/^mailto:/)) {
300 while (++i < count) {
301 var testarg = cmdLine.getArgument(i);
302 if (testarg.match(/^-/))
303 break;
304
305 uri += " " + testarg;
306 }
307 }
308 }
309 }
310
311 if (!uri && cmdLine.preventDefault)
312 return;
313
314 if (!uri && cmdLine.state != nsICommandLine.STATE_INITIAL_LAUNCH) {
315 try {
316 var wmed = Components.classes["@mozilla.org/appshell/window-mediator;1"]
317 .getService(nsIWindowMediator);
318
319 var wlist = wmed.getEnumerator("mail:3pane");
320 if (wlist.hasMoreElements()) {
321 var window = wlist.getNext().QueryInterface(nsIDOMWindowInternal);
322 window.focus();
323 return;
324 }
325 }
326 catch (e) {
327 dump(e);
328 }
329 }
330
331 if (uri) {
332 openURI(cmdLine.resolveURI(uri));
333 // XXX: add error-handling here! (error dialog, if nothing else)
334 } else {
335 var wwatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
336 .getService(nsIWindowWatcher);
337
338 var argstring = Components.classes["@mozilla.org/supports-string;1"]
339 .createInstance(nsISupportsString);
340
341 wwatch.openWindow(null, "chrome://messenger/content/", "_blank",
342 "chrome,dialog=no,all", argstring);
343 }
344 },
345
346 /* nsICommandLineValidator */
mdh_validate
347 validate : function mdh_validate(cmdLine) {
348 // Other handlers may use osint so only handle the osint flag if the mail
349 // or compose flag is also present and the command line is valid.
350 var osintFlagIdx = cmdLine.findFlag("osint", false);
351 var mailFlagIdx = cmdLine.findFlag("mail", false);
352 var composeFlagIdx = cmdLine.findFlag("compose", false);
353
354 // If both flags are present use the first flag found so the command line
355 // length test will fail.
356 if (mailFlagIdx > -1 && composeFlagIdx > -1)
357 var actionFlagIdx = mailFlagIdx > composeFlagIdx ? composeFlagIdx : mailFlagIdx;
358 else
359 actionFlagIdx = mailFlagIdx > -1 ? mailFlagIdx : composeFlagIdx;
360
361 if (actionFlagIdx && (osintFlagIdx > -1)) {
362 var param = cmdLine.getArgument(actionFlagIdx + 1);
363 if (cmdLine.length != actionFlagIdx + 2 ||
364 /thunderbird.url.(mailto|news):/.test(param))
365 throw NS_ERROR_ABORT;
366 cmdLine.handleFlag("osint", false)
367 }
368 },
369
370 helpInfo : "",
371
372 /* nsIFactory */
373
mdh_CI
374 createInstance : function mdh_CI(outer, iid) {
375 if (outer != null)
376 throw Components.results.NS_ERROR_NO_AGGREGATION;
377
378 return this.QueryInterface(iid);
379 },
380
mdh_lock
381 lockFactory : function mdh_lock(lock) {
382 /* no-op */
383 }
384 };
385
386 const mdh_contractID = "@mozilla.org/mail/clh;1";
387 const mdh_CID = Components.ID("{44346520-c5d2-44e5-a1ec-034e04d7fac4}");
388
389 var Module = {
390 /* nsISupports */
391
QI
392 QueryInterface : function QI(iid) {
393 if (iid.equals(Components.interfaces.nsIModule) &&
394 iid.equals(Components.interfaces.nsISupports))
395 return this;
396
397 throw Components.results.NS_ERROR_NO_INTERFACE;
398 },
399
400 /* nsIModule */
getClassObject
401 getClassObject : function (compMgr, cid, iid) {
402 if (cid.equals(mdh_CID))
403 return nsMailDefaultHandler.QueryInterface(iid);
404
405 throw Components.results.NS_ERROR_FAILURE;
406 },
407
mod_regself
408 registerSelf: function mod_regself(compMgr, fileSpec, location, type) {
409 var compReg =
410 compMgr.QueryInterface( Components.interfaces.nsIComponentRegistrar );
411
412 compReg.registerFactoryLocation(mdh_CID,
413 "nsMailDefaultHandler",
414 mdh_contractID,
415 fileSpec,
416 location,
417 type );
418
419 var catMan = Components.classes["@mozilla.org/categorymanager;1"]
420 .getService(Components.interfaces.nsICategoryManager);
421
422 catMan.addCategoryEntry("command-line-handler",
423 "x-default",
424 mdh_contractID, true, true);
425 catMan.addCategoryEntry("command-line-validator",
426 "b-default",
427 mdh_contractID, true, true);
428 },
429
mod_unregself
430 unregisterSelf : function mod_unregself(compMgr, location, type) {
431 var compReg = compMgr.QueryInterface(nsIComponentRegistrar);
432 compReg.unregisterFactoryLocation(mdh_CID, location);
433
434 var catMan = Components.classes["@mozilla.org/categorymanager;1"]
435 .getService(Components.interfaces.nsICategoryManager);
436
437 catMan.deleteCategoryEntry("command-line-handler",
438 "x-default", true);
439 catMan.deleteCategoryEntry("command-line-validator",
440 "b-default", true);
441 },
442
canUnload
443 canUnload: function(compMgr) {
444 return true;
445 }
446 }
447
448 // NSGetModule: Return the nsIModule object.
NSGetModule
449 function NSGetModule(compMgr, fileSpec) {
450 return Module;
451 }