michael@0: // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: this.EXPORTED_SYMBOLS = ["SimpleServiceDiscovery"]; michael@0: michael@0: const { classes: Cc, interfaces: Ci, utils: Cu } = Components; michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: michael@0: function log(msg) { michael@0: Services.console.logStringMessage("[SSDP] " + msg); michael@0: } michael@0: michael@0: XPCOMUtils.defineLazyGetter(this, "converter", function () { michael@0: let conv = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter); michael@0: conv.charset = "utf8"; michael@0: return conv; michael@0: }); michael@0: michael@0: // Spec information: michael@0: // https://tools.ietf.org/html/draft-cai-ssdp-v1-03 michael@0: // http://www.dial-multiscreen.org/dial-protocol-specification michael@0: const SSDP_PORT = 1900; michael@0: const SSDP_ADDRESS = "239.255.255.250"; michael@0: michael@0: const SSDP_DISCOVER_PACKET = michael@0: "M-SEARCH * HTTP/1.1\r\n" + michael@0: "HOST: " + SSDP_ADDRESS + ":" + SSDP_PORT + "\r\n" + michael@0: "MAN: \"ssdp:discover\"\r\n" + michael@0: "MX: 2\r\n" + michael@0: "ST: %SEARCH_TARGET%\r\n\r\n"; michael@0: michael@0: const SSDP_DISCOVER_TIMEOUT = 10000; michael@0: michael@0: /* michael@0: * SimpleServiceDiscovery manages any discovered SSDP services. It uses a UDP michael@0: * broadcast to locate available services on the local network. michael@0: */ michael@0: var SimpleServiceDiscovery = { michael@0: _targets: new Map(), michael@0: _services: new Map(), michael@0: _searchSocket: null, michael@0: _searchInterval: 0, michael@0: _searchTimestamp: 0, michael@0: _searchTimeout: Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer), michael@0: _searchRepeat: Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer), michael@0: michael@0: _forceTrailingSlash: function(aURL) { michael@0: // Some devices add the trailing '/' and some don't. Let's make sure michael@0: // it's there for consistency. michael@0: if (!aURL.endsWith("/")) { michael@0: aURL += "/"; michael@0: } michael@0: return aURL; michael@0: }, michael@0: michael@0: // nsIUDPSocketListener implementation michael@0: onPacketReceived: function(aSocket, aMessage) { michael@0: // Listen for responses from specific targets. There could be more than one michael@0: // available. michael@0: let response = aMessage.data.split("\n"); michael@0: let location; michael@0: let target; michael@0: let valid = false; michael@0: response.some(function(row) { michael@0: let header = row.toUpperCase(); michael@0: if (header.startsWith("LOCATION")) { michael@0: location = row.substr(10).trim(); michael@0: } else if (header.startsWith("ST")) { michael@0: target = row.substr(4).trim(); michael@0: if (this._targets.has(target)) { michael@0: valid = true; michael@0: } michael@0: } michael@0: michael@0: if (location && valid) { michael@0: location = this._forceTrailingSlash(location); michael@0: michael@0: // When we find a valid response, package up the service information michael@0: // and pass it on. michael@0: let service = { michael@0: location: location, michael@0: target: target michael@0: }; michael@0: michael@0: try { michael@0: this._processService(service); michael@0: } catch (e) {} michael@0: michael@0: return true; michael@0: } michael@0: return false; michael@0: }.bind(this)); michael@0: }, michael@0: michael@0: onStopListening: function(aSocket, aStatus) { michael@0: // This is fired when the socket is closed expectedly or unexpectedly. michael@0: // nsITimer.cancel() is a no-op if the timer is not active. michael@0: this._searchTimeout.cancel(); michael@0: this._searchSocket = null; michael@0: }, michael@0: michael@0: // Start a search. Make it continuous by passing an interval (in milliseconds). michael@0: // This will stop a current search loop because the timer resets itself. michael@0: search: function search(aInterval) { michael@0: if (aInterval > 0) { michael@0: this._searchInterval = aInterval || 0; michael@0: this._searchRepeat.initWithCallback(this._search.bind(this), this._searchInterval, Ci.nsITimer.TYPE_REPEATING_SLACK); michael@0: } michael@0: this._search(); michael@0: }, michael@0: michael@0: // Stop the current continuous search michael@0: stopSearch: function stopSearch() { michael@0: this._searchRepeat.cancel(); michael@0: }, michael@0: michael@0: _usingLAN: function() { michael@0: let network = Cc["@mozilla.org/network/network-link-service;1"].getService(Ci.nsINetworkLinkService); michael@0: return (network.linkType == Ci.nsINetworkLinkService.LINK_TYPE_WIFI || network.linkType == Ci.nsINetworkLinkService.LINK_TYPE_ETHERNET); michael@0: }, michael@0: michael@0: _search: function _search() { michael@0: // If a search is already active, shut it down. michael@0: this._searchShutdown(); michael@0: michael@0: // We only search if on local network michael@0: if (!this._usingLAN()) { michael@0: return; michael@0: } michael@0: michael@0: // Update the timestamp so we can use it to clean out stale services the michael@0: // next time we search. michael@0: this._searchTimestamp = Date.now(); michael@0: michael@0: // Look for any fixed IP targets. Some routers might be configured to block michael@0: // UDP broadcasts, so this is a way to skip discovery. michael@0: this._searchFixedTargets(); michael@0: michael@0: // Perform a UDP broadcast to search for SSDP devices michael@0: let socket = Cc["@mozilla.org/network/udp-socket;1"].createInstance(Ci.nsIUDPSocket); michael@0: try { michael@0: socket.init(SSDP_PORT, false); michael@0: socket.asyncListen(this); michael@0: } catch (e) { michael@0: // We were unable to create the broadcast socket. Just return, but don't michael@0: // kill the interval timer. This might work next time. michael@0: log("failed to start socket: " + e); michael@0: return; michael@0: } michael@0: michael@0: this._searchSocket = socket; michael@0: this._searchTimeout.initWithCallback(this._searchShutdown.bind(this), SSDP_DISCOVER_TIMEOUT, Ci.nsITimer.TYPE_ONE_SHOT); michael@0: michael@0: let data = SSDP_DISCOVER_PACKET; michael@0: for (let [key, target] of this._targets) { michael@0: let msgData = data.replace("%SEARCH_TARGET%", target.target); michael@0: try { michael@0: let msgRaw = converter.convertToByteArray(msgData); michael@0: socket.send(SSDP_ADDRESS, SSDP_PORT, msgRaw, msgRaw.length); michael@0: } catch (e) { michael@0: log("failed to convert to byte array: " + e); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: _searchFixedTargets: function _searchFixedTargets() { michael@0: let fixedTargets = null; michael@0: try { michael@0: fixedTargets = Services.prefs.getCharPref("browser.casting.fixedTargets"); michael@0: } catch (e) {} michael@0: michael@0: if (!fixedTargets) { michael@0: return; michael@0: } michael@0: michael@0: fixedTargets = JSON.parse(fixedTargets); michael@0: for (let fixedTarget of fixedTargets) { michael@0: // Verify we have the right data michael@0: if (!"location" in fixedTarget || !"target" in fixedTarget) { michael@0: continue; michael@0: } michael@0: michael@0: fixedTarget.location = this._forceTrailingSlash(fixedTarget.location); michael@0: michael@0: let service = { michael@0: location: fixedTarget.location, michael@0: target: fixedTarget.target michael@0: }; michael@0: michael@0: // We don't assume the fixed target is ready. We still need to ping it. michael@0: try { michael@0: this._processService(service); michael@0: } catch (e) {} michael@0: } michael@0: }, michael@0: michael@0: // Called when the search timeout is hit. We use it to cleanup the socket and michael@0: // perform some post-processing on the services list. michael@0: _searchShutdown: function _searchShutdown() { michael@0: if (this._searchSocket) { michael@0: // This will call onStopListening. michael@0: this._searchSocket.close(); michael@0: michael@0: // Clean out any stale services michael@0: for (let [key, service] of this._services) { michael@0: if (service.lastPing != this._searchTimestamp) { michael@0: Services.obs.notifyObservers(null, "ssdp-service-lost", service.location); michael@0: this._services.delete(service.location); michael@0: } michael@0: } michael@0: } michael@0: }, michael@0: michael@0: registerTarget: function registerTarget(aTarget, aAppFactory) { michael@0: // Only add if we don't already know about this target michael@0: if (!this._targets.has(aTarget)) { michael@0: this._targets.set(aTarget, { target: aTarget, factory: aAppFactory }); michael@0: } michael@0: }, michael@0: michael@0: findAppForService: function findAppForService(aService, aApp) { michael@0: if (!aService || !aService.target) { michael@0: return null; michael@0: } michael@0: michael@0: // Find the registration for the target michael@0: if (this._targets.has(aService.target)) { michael@0: return this._targets.get(aService.target).factory(aService, aApp); michael@0: } michael@0: return null; michael@0: }, michael@0: michael@0: findServiceForLocation: function findServiceForLocation(aLocation) { michael@0: if (this._services.has(aLocation)) { michael@0: return this._services.get(aLocation); michael@0: } michael@0: return null; michael@0: }, michael@0: michael@0: // Returns an array copy of the active services michael@0: get services() { michael@0: let array = []; michael@0: for (let [key, service] of this._services) { michael@0: array.push(service); michael@0: } michael@0: return array; michael@0: }, michael@0: michael@0: _processService: function _processService(aService) { michael@0: // Use the REST api to request more information about this service michael@0: let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest); michael@0: xhr.open("GET", aService.location, true); michael@0: xhr.channel.loadFlags |= Ci.nsIRequest.INHIBIT_CACHING; michael@0: xhr.overrideMimeType("text/xml"); michael@0: michael@0: xhr.addEventListener("load", (function() { michael@0: if (xhr.status == 200) { michael@0: let doc = xhr.responseXML; michael@0: aService.appsURL = xhr.getResponseHeader("Application-URL"); michael@0: if (aService.appsURL && !aService.appsURL.endsWith("/")) michael@0: aService.appsURL += "/"; michael@0: aService.friendlyName = doc.querySelector("friendlyName").textContent; michael@0: aService.uuid = doc.querySelector("UDN").textContent; michael@0: aService.manufacturer = doc.querySelector("manufacturer").textContent; michael@0: aService.modelName = doc.querySelector("modelName").textContent; michael@0: michael@0: // Only add and notify if we don't already know about this service michael@0: if (!this._services.has(aService.location)) { michael@0: this._services.set(aService.location, aService); michael@0: Services.obs.notifyObservers(null, "ssdp-service-found", aService.location); michael@0: } michael@0: michael@0: // Make sure we remember this service is not stale michael@0: this._services.get(aService.location).lastPing = this._searchTimestamp; michael@0: } michael@0: }).bind(this), false); michael@0: michael@0: xhr.send(null); michael@0: } michael@0: }