1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const { classes: Cc, interfaces: Ci, results: Cr, utils: Cu } = Components;
Cu.import("resource://gre/modules/ExtensionUtils.jsm");
Cu.import('resource://gre/modules/Services.jsm');
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
let { PlatformInfo } = ExtensionUtils;
if (PlatformInfo.os == "android" && !Services.prefs.getBoolPref("network.mdns.use_js_fallback")) {
Cu.import("resource://gre/modules/MulticastDNSAndroid.jsm");
} else {
Cu.import("resource://gre/modules/MulticastDNS.jsm");
}
const DNSSERVICEDISCOVERY_CID = Components.ID("{f9346d98-f27a-4e89-b744-493843416480}");
const DNSSERVICEDISCOVERY_CONTRACT_ID = "@mozilla.org/toolkit/components/mdnsresponder/dns-sd;1";
const DNSSERVICEINFO_CONTRACT_ID = "@mozilla.org/toolkit/components/mdnsresponder/dns-info;1";
function log(aMsg) {
dump("-*- nsDNSServiceDiscovery.js : " + aMsg + "\n");
}
function generateUuid() {
var uuidGenerator = Components.classes["@mozilla.org/uuid-generator;1"].
getService(Ci.nsIUUIDGenerator);
return uuidGenerator.generateUUID().toString();
}
// Helper class to transform return objects to correct type.
function ListenerWrapper(aListener, aMdns) {
this.listener = aListener;
this.mdns = aMdns;
this.discoveryStarting = false;
this.stopDiscovery = false;
this.registrationStarting = false;
this.stopRegistration = false;
this.uuid = generateUuid();
}
ListenerWrapper.prototype = {
// Helper function for transforming an Object into nsIDNSServiceInfo.
makeServiceInfo: function (aServiceInfo) {
let serviceInfo = Cc[DNSSERVICEINFO_CONTRACT_ID].createInstance(Ci.nsIDNSServiceInfo);
for (let name of ['host', 'address', 'port', 'serviceName', 'serviceType']) {
try {
serviceInfo[name] = aServiceInfo[name];
} catch (e) {
// ignore exceptions
}
}
let attributes;
try {
attributes = _toPropertyBag2(aServiceInfo.attributes);
} catch (err) {
// Ignore unset attributes in object.
log("Caught unset attributes error: " + err + " - " + err.stack);
attributes = Cc['@mozilla.org/hash-property-bag;1']
.createInstance(Ci.nsIWritablePropertyBag2);
}
serviceInfo.attributes = attributes;
return serviceInfo;
},
/* transparent types */
onDiscoveryStarted: function(aServiceType) {
this.discoveryStarting = false;
this.listener.onDiscoveryStarted(aServiceType);
if (this.stopDiscovery) {
this.mdns.stopDiscovery(aServiceType, this);
}
},
onDiscoveryStopped: function(aServiceType) {
this.listener.onDiscoveryStopped(aServiceType);
},
onStartDiscoveryFailed: function(aServiceType, aErrorCode) {
log('onStartDiscoveryFailed: ' + aServiceType + ' (' + aErrorCode + ')');
this.discoveryStarting = false;
this.stopDiscovery = true;
this.listener.onStartDiscoveryFailed(aServiceType, aErrorCode);
},
onStopDiscoveryFailed: function(aServiceType, aErrorCode) {
log('onStopDiscoveryFailed: ' + aServiceType + ' (' + aErrorCode + ')');
this.listener.onStopDiscoveryFailed(aServiceType, aErrorCode);
},
/* transform types */
onServiceFound: function(aServiceInfo) {
this.listener.onServiceFound(this.makeServiceInfo(aServiceInfo));
},
onServiceLost: function(aServiceInfo) {
this.listener.onServiceLost(this.makeServiceInfo(aServiceInfo));
},
onServiceRegistered: function(aServiceInfo) {
this.registrationStarting = false;
this.listener.onServiceRegistered(this.makeServiceInfo(aServiceInfo));
if (this.stopRegistration) {
this.mdns.unregisterService(aServiceInfo, this);
}
},
onServiceUnregistered: function(aServiceInfo) {
this.listener.onServiceUnregistered(this.makeServiceInfo(aServiceInfo));
},
onServiceResolved: function(aServiceInfo) {
this.listener.onServiceResolved(this.makeServiceInfo(aServiceInfo));
},
onRegistrationFailed: function(aServiceInfo, aErrorCode) {
log('onRegistrationFailed: (' + aErrorCode + ')');
this.registrationStarting = false;
this.stopRegistration = true;
this.listener.onRegistrationFailed(this.makeServiceInfo(aServiceInfo), aErrorCode);
},
onUnregistrationFailed: function(aServiceInfo, aErrorCode) {
log('onUnregistrationFailed: (' + aErrorCode + ')');
this.listener.onUnregistrationFailed(this.makeServiceInfo(aServiceInfo), aErrorCode);
},
onResolveFailed: function(aServiceInfo, aErrorCode) {
log('onResolveFailed: (' + aErrorCode + ')');
this.listener.onResolveFailed(this.makeServiceInfo(aServiceInfo), aErrorCode);
}
};
function nsDNSServiceDiscovery() {
log("nsDNSServiceDiscovery");
this.mdns = new MulticastDNS();
}
nsDNSServiceDiscovery.prototype = {
classID: DNSSERVICEDISCOVERY_CID,
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference, Ci.nsIDNSServiceDiscovery]),
startDiscovery: function(aServiceType, aListener) {
log("startDiscovery");
let listener = new ListenerWrapper(aListener, this.mdns);
listener.discoveryStarting = true;
this.mdns.startDiscovery(aServiceType, listener);
return {
QueryInterface: XPCOMUtils.generateQI([Ci.nsICancelable]),
cancel: (function() {
if (this.discoveryStarting || this.stopDiscovery) {
this.stopDiscovery = true;
return;
}
this.mdns.stopDiscovery(aServiceType, listener);
}).bind(listener)
};
},
registerService: function(aServiceInfo, aListener) {
log("registerService");
let listener = new ListenerWrapper(aListener, this.mdns);
listener.registrationStarting = true;
this.mdns.registerService(aServiceInfo, listener);
return {
QueryInterface: XPCOMUtils.generateQI([Ci.nsICancelable]),
cancel: (function() {
if (this.registrationStarting || this.stopRegistration) {
this.stopRegistration = true;
return;
}
this.mdns.unregisterService(aServiceInfo, listener);
}).bind(listener)
};
},
resolveService: function(aServiceInfo, aListener) {
log("resolveService");
this.mdns.resolveService(aServiceInfo, new ListenerWrapper(aListener));
}
};
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([nsDNSServiceDiscovery]);
function _toPropertyBag2(obj)
{
if (obj.QueryInterface) {
return obj.QueryInterface(Ci.nsIPropertyBag2);
}
let result = Cc['@mozilla.org/hash-property-bag;1']
.createInstance(Ci.nsIWritablePropertyBag2);
for (let name in obj) {
result.setPropertyAsAString(name, obj[name]);
}
return result;
}
|