|
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/. */ |
|
4 |
|
5 const Ci = Components.interfaces; |
|
6 const Cu = Components.utils; |
|
7 const Cc = Components.classes; |
|
8 |
|
9 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
10 Cu.import("resource://gre/modules/Services.jsm"); |
|
11 Cu.import("resource://gre/modules/Messaging.jsm"); |
|
12 |
|
13 function ContentDispatchChooser() {} |
|
14 |
|
15 ContentDispatchChooser.prototype = |
|
16 { |
|
17 classID: Components.ID("5a072a22-1e66-4100-afc1-07aed8b62fc5"), |
|
18 |
|
19 QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentDispatchChooser]), |
|
20 |
|
21 get protoSvc() { |
|
22 delete this.protoSvc; |
|
23 return this.protoSvc = Cc["@mozilla.org/uriloader/external-protocol-service;1"].getService(Ci.nsIExternalProtocolService); |
|
24 }, |
|
25 |
|
26 _getChromeWin: function getChromeWin() { |
|
27 try { |
|
28 return Services.wm.getMostRecentWindow("navigator:browser"); |
|
29 } catch (e) { |
|
30 throw Cr.NS_ERROR_FAILURE; |
|
31 } |
|
32 }, |
|
33 |
|
34 ask: function ask(aHandler, aWindowContext, aURI, aReason) { |
|
35 let window = null; |
|
36 try { |
|
37 if (aWindowContext) |
|
38 window = aWindowContext.getInterface(Ci.nsIDOMWindow); |
|
39 } catch (e) { /* it's OK to not have a window */ } |
|
40 |
|
41 // The current list is based purely on the scheme. Redo the query using the url to get more |
|
42 // specific results. |
|
43 aHandler = this.protoSvc.getProtocolHandlerInfoFromOS(aURI.spec, {}); |
|
44 |
|
45 // The first handler in the set is the Android Application Chooser (which will fall back to a default if one is set) |
|
46 // If we have more than one option, let the OS handle showing a list (if needed). |
|
47 if (aHandler.possibleApplicationHandlers.length > 1) { |
|
48 aHandler.launchWithURI(aURI, aWindowContext); |
|
49 } else { |
|
50 let win = this._getChromeWin(); |
|
51 if (win && win.NativeWindow) { |
|
52 let bundle = Services.strings.createBundle("chrome://browser/locale/handling.properties"); |
|
53 let failedText = bundle.GetStringFromName("protocol.failed"); |
|
54 let searchText = bundle.GetStringFromName("protocol.toast.search"); |
|
55 |
|
56 win.NativeWindow.toast.show(failedText, "long", { |
|
57 button: { |
|
58 label: searchText, |
|
59 callback: function() { |
|
60 let message = { |
|
61 type: "Intent:Open", |
|
62 url: "market://search?q=" + aURI.scheme, |
|
63 }; |
|
64 |
|
65 sendMessageToJava(message); |
|
66 } |
|
67 } |
|
68 }); |
|
69 } |
|
70 } |
|
71 }, |
|
72 }; |
|
73 |
|
74 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([ContentDispatchChooser]); |