Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
1 // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 "use strict";
8 this.EXPORTED_SYMBOLS = ["SimpleServiceDiscovery"];
10 const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
12 Cu.import("resource://gre/modules/Services.jsm");
13 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
15 function log(msg) {
16 Services.console.logStringMessage("[SSDP] " + msg);
17 }
19 XPCOMUtils.defineLazyGetter(this, "converter", function () {
20 let conv = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter);
21 conv.charset = "utf8";
22 return conv;
23 });
25 // Spec information:
26 // https://tools.ietf.org/html/draft-cai-ssdp-v1-03
27 // http://www.dial-multiscreen.org/dial-protocol-specification
28 const SSDP_PORT = 1900;
29 const SSDP_ADDRESS = "239.255.255.250";
31 const SSDP_DISCOVER_PACKET =
32 "M-SEARCH * HTTP/1.1\r\n" +
33 "HOST: " + SSDP_ADDRESS + ":" + SSDP_PORT + "\r\n" +
34 "MAN: \"ssdp:discover\"\r\n" +
35 "MX: 2\r\n" +
36 "ST: %SEARCH_TARGET%\r\n\r\n";
38 const SSDP_DISCOVER_TIMEOUT = 10000;
40 /*
41 * SimpleServiceDiscovery manages any discovered SSDP services. It uses a UDP
42 * broadcast to locate available services on the local network.
43 */
44 var SimpleServiceDiscovery = {
45 _targets: new Map(),
46 _services: new Map(),
47 _searchSocket: null,
48 _searchInterval: 0,
49 _searchTimestamp: 0,
50 _searchTimeout: Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer),
51 _searchRepeat: Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer),
53 _forceTrailingSlash: function(aURL) {
54 // Some devices add the trailing '/' and some don't. Let's make sure
55 // it's there for consistency.
56 if (!aURL.endsWith("/")) {
57 aURL += "/";
58 }
59 return aURL;
60 },
62 // nsIUDPSocketListener implementation
63 onPacketReceived: function(aSocket, aMessage) {
64 // Listen for responses from specific targets. There could be more than one
65 // available.
66 let response = aMessage.data.split("\n");
67 let location;
68 let target;
69 let valid = false;
70 response.some(function(row) {
71 let header = row.toUpperCase();
72 if (header.startsWith("LOCATION")) {
73 location = row.substr(10).trim();
74 } else if (header.startsWith("ST")) {
75 target = row.substr(4).trim();
76 if (this._targets.has(target)) {
77 valid = true;
78 }
79 }
81 if (location && valid) {
82 location = this._forceTrailingSlash(location);
84 // When we find a valid response, package up the service information
85 // and pass it on.
86 let service = {
87 location: location,
88 target: target
89 };
91 try {
92 this._processService(service);
93 } catch (e) {}
95 return true;
96 }
97 return false;
98 }.bind(this));
99 },
101 onStopListening: function(aSocket, aStatus) {
102 // This is fired when the socket is closed expectedly or unexpectedly.
103 // nsITimer.cancel() is a no-op if the timer is not active.
104 this._searchTimeout.cancel();
105 this._searchSocket = null;
106 },
108 // Start a search. Make it continuous by passing an interval (in milliseconds).
109 // This will stop a current search loop because the timer resets itself.
110 search: function search(aInterval) {
111 if (aInterval > 0) {
112 this._searchInterval = aInterval || 0;
113 this._searchRepeat.initWithCallback(this._search.bind(this), this._searchInterval, Ci.nsITimer.TYPE_REPEATING_SLACK);
114 }
115 this._search();
116 },
118 // Stop the current continuous search
119 stopSearch: function stopSearch() {
120 this._searchRepeat.cancel();
121 },
123 _usingLAN: function() {
124 let network = Cc["@mozilla.org/network/network-link-service;1"].getService(Ci.nsINetworkLinkService);
125 return (network.linkType == Ci.nsINetworkLinkService.LINK_TYPE_WIFI || network.linkType == Ci.nsINetworkLinkService.LINK_TYPE_ETHERNET);
126 },
128 _search: function _search() {
129 // If a search is already active, shut it down.
130 this._searchShutdown();
132 // We only search if on local network
133 if (!this._usingLAN()) {
134 return;
135 }
137 // Update the timestamp so we can use it to clean out stale services the
138 // next time we search.
139 this._searchTimestamp = Date.now();
141 // Look for any fixed IP targets. Some routers might be configured to block
142 // UDP broadcasts, so this is a way to skip discovery.
143 this._searchFixedTargets();
145 // Perform a UDP broadcast to search for SSDP devices
146 let socket = Cc["@mozilla.org/network/udp-socket;1"].createInstance(Ci.nsIUDPSocket);
147 try {
148 socket.init(SSDP_PORT, false);
149 socket.asyncListen(this);
150 } catch (e) {
151 // We were unable to create the broadcast socket. Just return, but don't
152 // kill the interval timer. This might work next time.
153 log("failed to start socket: " + e);
154 return;
155 }
157 this._searchSocket = socket;
158 this._searchTimeout.initWithCallback(this._searchShutdown.bind(this), SSDP_DISCOVER_TIMEOUT, Ci.nsITimer.TYPE_ONE_SHOT);
160 let data = SSDP_DISCOVER_PACKET;
161 for (let [key, target] of this._targets) {
162 let msgData = data.replace("%SEARCH_TARGET%", target.target);
163 try {
164 let msgRaw = converter.convertToByteArray(msgData);
165 socket.send(SSDP_ADDRESS, SSDP_PORT, msgRaw, msgRaw.length);
166 } catch (e) {
167 log("failed to convert to byte array: " + e);
168 }
169 }
170 },
172 _searchFixedTargets: function _searchFixedTargets() {
173 let fixedTargets = null;
174 try {
175 fixedTargets = Services.prefs.getCharPref("browser.casting.fixedTargets");
176 } catch (e) {}
178 if (!fixedTargets) {
179 return;
180 }
182 fixedTargets = JSON.parse(fixedTargets);
183 for (let fixedTarget of fixedTargets) {
184 // Verify we have the right data
185 if (!"location" in fixedTarget || !"target" in fixedTarget) {
186 continue;
187 }
189 fixedTarget.location = this._forceTrailingSlash(fixedTarget.location);
191 let service = {
192 location: fixedTarget.location,
193 target: fixedTarget.target
194 };
196 // We don't assume the fixed target is ready. We still need to ping it.
197 try {
198 this._processService(service);
199 } catch (e) {}
200 }
201 },
203 // Called when the search timeout is hit. We use it to cleanup the socket and
204 // perform some post-processing on the services list.
205 _searchShutdown: function _searchShutdown() {
206 if (this._searchSocket) {
207 // This will call onStopListening.
208 this._searchSocket.close();
210 // Clean out any stale services
211 for (let [key, service] of this._services) {
212 if (service.lastPing != this._searchTimestamp) {
213 Services.obs.notifyObservers(null, "ssdp-service-lost", service.location);
214 this._services.delete(service.location);
215 }
216 }
217 }
218 },
220 registerTarget: function registerTarget(aTarget, aAppFactory) {
221 // Only add if we don't already know about this target
222 if (!this._targets.has(aTarget)) {
223 this._targets.set(aTarget, { target: aTarget, factory: aAppFactory });
224 }
225 },
227 findAppForService: function findAppForService(aService, aApp) {
228 if (!aService || !aService.target) {
229 return null;
230 }
232 // Find the registration for the target
233 if (this._targets.has(aService.target)) {
234 return this._targets.get(aService.target).factory(aService, aApp);
235 }
236 return null;
237 },
239 findServiceForLocation: function findServiceForLocation(aLocation) {
240 if (this._services.has(aLocation)) {
241 return this._services.get(aLocation);
242 }
243 return null;
244 },
246 // Returns an array copy of the active services
247 get services() {
248 let array = [];
249 for (let [key, service] of this._services) {
250 array.push(service);
251 }
252 return array;
253 },
255 _processService: function _processService(aService) {
256 // Use the REST api to request more information about this service
257 let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
258 xhr.open("GET", aService.location, true);
259 xhr.channel.loadFlags |= Ci.nsIRequest.INHIBIT_CACHING;
260 xhr.overrideMimeType("text/xml");
262 xhr.addEventListener("load", (function() {
263 if (xhr.status == 200) {
264 let doc = xhr.responseXML;
265 aService.appsURL = xhr.getResponseHeader("Application-URL");
266 if (aService.appsURL && !aService.appsURL.endsWith("/"))
267 aService.appsURL += "/";
268 aService.friendlyName = doc.querySelector("friendlyName").textContent;
269 aService.uuid = doc.querySelector("UDN").textContent;
270 aService.manufacturer = doc.querySelector("manufacturer").textContent;
271 aService.modelName = doc.querySelector("modelName").textContent;
273 // Only add and notify if we don't already know about this service
274 if (!this._services.has(aService.location)) {
275 this._services.set(aService.location, aService);
276 Services.obs.notifyObservers(null, "ssdp-service-found", aService.location);
277 }
279 // Make sure we remember this service is not stale
280 this._services.get(aService.location).lastPing = this._searchTimestamp;
281 }
282 }).bind(this), false);
284 xhr.send(null);
285 }
286 }