MOD Nettoyage du fichier js de l'extension firefox.
[pompage.git] / xpi / components / handleProtocol.js
1 // Le composant définit pour cette extension
2 const DIVXPROT_HANDLER_CONTRACTID = "@mozilla.org/network/protocol;1?name=divx";
3 const DIVXPROT_HANDLER_CID = Components.ID("{65aa548e-1dda-11dc-8314-0800200c9a66}");
4
5 // Les composants utilisés
6 const URI_CONTRACTID = "@mozilla.org/network/simple-uri;1";
7 const kIOSERVICE_CONTRACTID = "@mozilla.org/network/io-service;1";
8
9 // Les interfaces utilisées
10 const nsIProtocolHandler = Components.interfaces.nsIProtocolHandler;
11 const nsIURI = Components.interfaces.nsIURI;
12 const nsISupports = Components.interfaces.nsISupports;
13 const nsIIOService = Components.interfaces.nsIIOService;
14
15
16 /**
17 * Classe ProtocolHandler.
18 */
19 function ProtocolHandler(scheme)
20 {
21 this.scheme = scheme;
22 }
23
24 // attributs par défaut
25 DivxProtocolHandler.prototype.defaultPort = -1;
26 DivxProtocolHandler.prototype.protocolFlags = nsIProtocolHandler.URI_NORELATIVE;
27
28 /**
29 * est-ce qu'un port est autorisé.
30 */
31 DivxProtocolHandler.prototype.allowPort = function(aPort, aScheme)
32 {
33 return false;
34 }
35
36 /**
37 * Renvoie le nouvel URI.
38 */
39 DivxProtocolHandler.prototype.newURI = function(aSpec, aCharset, aBaseURI)
40 {
41 var uri = Components.classes[URI_CONTRACTID].createInstance(nsIURI);
42 uri.spec = aSpec;
43 return uri;
44 }
45
46 /**
47 * Renvoie le nouveau channel.
48 */
49 DivxProtocolHandler.prototype.newChannel = function(aURI)
50 {
51 var chemin = aURI.spec;
52
53 var root = "F:/Films/";
54 chemin = chemin.replace("divx://", root)
55
56 //dump(chemin);
57
58 // crée un nouveau fichier
59 var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
60 file.initWithPath("C:\\Program Files\\VideoLAN\\VLC\\vlc.exe");
61
62 // crée un nouveau processus
63 var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
64 process.init(file);
65
66 // lance le processus. false pour ne pas être blockant
67 var args = [chemin, "-f"];
68 process.run(false, args, args.length);
69
70 // trouver une méthode plus simple pour qu'il ne fasse rien..
71 var ios = Components.classes[kIOSERVICE_CONTRACTID].getService(nsIIOService);
72 return ios.newChannel("javascript:document.location='" + finalURL + "'", null, null);
73 }
74
75 /**
76 * Classe DivxProtocolHandlerFactory.
77 */
78 function DivxProtocolHandlerFactory(scheme)
79 {
80 this.scheme = scheme;
81 }
82
83 /**
84 * Méthode permettant la création d'objet de type 'DivxProtocolHandler'.
85 */
86 DivxProtocolHandlerFactory.prototype.createInstance = function(outer, iid)
87 {
88 if(outer != null) throw Components.results.NS_ERROR_NO_AGGREGATION;
89
90 if(!iid.equals(nsIProtocolHandler) && !iid.equals(nsISupports))
91 throw Components.results.NS_ERROR_INVALID_ARG;
92
93 return new DivxProtocolHandler(this.scheme);
94 }
95
96 // la factory
97 var factory_divx = new DivxProtocolHandlerFactory("divx");
98
99 /**
100 * Notre module.
101 */
102 var DIVXModule = new Object();
103
104 /**
105 * Appelé lors de l'enregistrement du module.
106 */
107 DIVXModule.registerSelf = function(compMgr, fileSpec, location, type)
108 {
109 compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
110
111 // on enregistre le protocole handler
112 compMgr.registerFactoryLocation(DIVXPROT_HANDLER_CID,
113 "divx",
114 DIVXPROT_HANDLER_CONTRACTID,
115 fileSpec, location, type);
116 }
117
118 /**
119 * Appelé lors du déchargement du module.
120 */
121 DIVXModule.unregisterSelf = function(compMgr, fileSpec, location)
122 {
123 compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
124
125 // on dés-enregistre notre protocole handler
126 compMgr.unregisterFactoryLocation(DIVXPROT_HANDLER_CID, fileSpec);
127 }
128
129 /**
130 * Demande la factory pour construire le protocole handler.
131 */
132 DIVXModule.getClassObject = function(compMgr, cid, iid)
133 {
134 if(!iid.equals(Components.interfaces.nsIFactory))
135 throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
136
137 if(cid.equals(DIVXPROT_HANDLER_CID)) return factory_divx;
138
139 throw Components.results.NS_ERROR_NO_INTERFACE;
140 }
141
142 /**
143 * Est-ce que le module peut être déchargé ?
144 */
145 DIVXModule.canUnload = function(compMgr)
146 {
147 return true;
148 }
149
150 /**
151 * Le point d'entré, retourne le module.
152 */
153 function NSGetModule(compMgr, fileSpec)
154 {
155 return DIVXModule;
156 }