instrumentation: add next-share/
[cs-p2p-next.git] / instrumentation / next-share / BaseLib / Transport / components / TribeProtocolHandler.js
1 // -*- coding: utf-8 -*-
2 // vi:si:et:sw=2:sts=2:ts=2
3 /*
4   TribeProtocolHandler - Torrent video for <video>
5   
6   Written by Jan Gerber, Riccardo Petrocco
7   see LICENSE.txt for license information
8  */
9
10 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
11
12 const Cc = Components.classes;
13 const Ci = Components.interfaces;
14
15 var tribeLoggingEnabled = false;
16
17 function LOG(aMsg) {
18   if (tribeLoggingEnabled)
19   {
20     aMsg = ("*** Tribe : " + aMsg);
21     Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).logStringMessage(aMsg);
22     dump(aMsg);
23   }
24 }
25
26
27 function TribeProtocol() {
28   this.prefService = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch).QueryInterface(Ci.nsIPrefService);
29   try {
30     tribeLoggingEnabled = this.prefService.getBoolPref("tribe.logging.enabled");
31   } catch (e) {}
32
33 }
34
35 TribeProtocol.prototype =
36 {
37   classDescription: "Tribe protocol",
38   classID: Components.ID("bcb8d306-66cf-4358-8473-807981ffe365"),
39   contractID: "@mozilla.org/network/protocol;1?name=tribe",
40   QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler,
41                                          Ci.nsISupports]),
42   _xpcom_factory : TribeProtocolFactory,
43   scheme: "tribe",
44   defaultPort: -1,
45   protocolFlags: Ci.nsIProtocolHandler.URI_NORELATIVE |
46              Ci.nsIProtocolHandler.URI_NOAUTH |
47              Ci.nsIProtocolHandler.URI_LOADABLE_BY_ANYONE,
48
49   allowPort: function(port, scheme)
50   {
51     return false;
52   },
53
54   newURI: function(spec, charset, baseURI)
55   {
56     var uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);
57     uri.spec = spec;
58     return uri;
59   },
60
61   newChannel: function(input_uri)
62   {
63     // aURI is a nsIUri, so get a string from it using .spec
64     var key = input_uri.spec;
65
66     // strip away the kSCHEME: part
67     var torrent_url = key.substring(key.indexOf("://") + 3, key.length);    
68     // the URL will not be encoded since we will not be able to decode it afterwards
69     //torrent_url = encodeURI(torrent_url);
70     LOG('\nopening: '+torrent_url+'\n');
71
72     var channel = Cc["@p2pnext.org/tribe/channel;1"].createInstance(Ci.tribeIChannel);
73     channel.setTorrentUrl(torrent_url);
74     return channel;
75   },
76
77
78
79 var TribeProtocolFactory =
80 {
81   createInstance: function (outer, iid)
82   {
83     if (outer != null)
84       throw Components.results.NS_ERROR_NO_AGGREGATION;
85
86     if (!iid.equals(Ci.nsIProtocolHandler) &&
87         !iid.equals(Ci.nsISupports) )
88       throw Components.results.NS_ERROR_NO_INTERFACE;
89
90     return (new TribeProtocol()).QueryInterface(iid);
91   }
92 };
93
94 function NSGetModule(compMgr, fileSpec) {
95   return XPCOMUtils.generateModule([TribeProtocol]);
96 }
97