diff options
Diffstat (limited to '')
-rw-r--r-- | dom/presentation/provider/ControllerStateMachine.jsm | 236 | ||||
-rw-r--r-- | dom/presentation/provider/DeviceProviderHelpers.cpp | 53 | ||||
-rw-r--r-- | dom/presentation/provider/DeviceProviderHelpers.h | 29 | ||||
-rw-r--r-- | dom/presentation/provider/MulticastDNSDeviceProvider.cpp | 1153 | ||||
-rw-r--r-- | dom/presentation/provider/MulticastDNSDeviceProvider.h | 185 | ||||
-rw-r--r-- | dom/presentation/provider/PresentationControlService.jsm | 1054 | ||||
-rw-r--r-- | dom/presentation/provider/ReceiverStateMachine.jsm | 232 | ||||
-rw-r--r-- | dom/presentation/provider/StateMachineHelper.jsm | 38 | ||||
-rw-r--r-- | dom/presentation/provider/components.conf | 26 | ||||
-rw-r--r-- | dom/presentation/provider/moz.build | 29 | ||||
-rw-r--r-- | dom/presentation/provider/nsTCPDeviceInfo.h | 67 |
11 files changed, 3102 insertions, 0 deletions
diff --git a/dom/presentation/provider/ControllerStateMachine.jsm b/dom/presentation/provider/ControllerStateMachine.jsm new file mode 100644 index 0000000000..03f6b35bd5 --- /dev/null +++ b/dom/presentation/provider/ControllerStateMachine.jsm @@ -0,0 +1,236 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* 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"; + +var EXPORTED_SYMBOLS = ["ControllerStateMachine"]; + +const { CommandType, State } = ChromeUtils.import( + "resource://gre/modules/presentation/StateMachineHelper.jsm" +); + +const DEBUG = false; +function debug(str) { + dump("-*- ControllerStateMachine: " + str + "\n"); +} + +var handlers = [ + function _initHandler(stateMachine, command) { + // shouldn't receive any command at init state. + DEBUG && debug("unexpected command: " + JSON.stringify(command)); + }, + function _connectingHandler(stateMachine, command) { + switch (command.type) { + case CommandType.CONNECT_ACK: + stateMachine.state = State.CONNECTED; + stateMachine._notifyDeviceConnected(); + break; + case CommandType.DISCONNECT: + stateMachine.state = State.CLOSED; + stateMachine._notifyDisconnected(command.reason); + break; + default: + debug("unexpected command: " + JSON.stringify(command)); + // ignore unexpected command. + break; + } + }, + function _connectedHandler(stateMachine, command) { + switch (command.type) { + case CommandType.DISCONNECT: + stateMachine.state = State.CLOSED; + stateMachine._notifyDisconnected(command.reason); + break; + case CommandType.LAUNCH_ACK: + stateMachine._notifyLaunch(command.presentationId); + break; + case CommandType.TERMINATE: + stateMachine._notifyTerminate(command.presentationId); + break; + case CommandType.TERMINATE_ACK: + stateMachine._notifyTerminate(command.presentationId); + break; + case CommandType.ANSWER: + case CommandType.ICE_CANDIDATE: + stateMachine._notifyChannelDescriptor(command); + break; + case CommandType.RECONNECT_ACK: + stateMachine._notifyReconnect(command.presentationId); + break; + default: + debug("unexpected command: " + JSON.stringify(command)); + // ignore unexpected command. + break; + } + }, + function _closingHandler(stateMachine, command) { + switch (command.type) { + case CommandType.DISCONNECT: + stateMachine.state = State.CLOSED; + stateMachine._notifyDisconnected(command.reason); + break; + default: + debug("unexpected command: " + JSON.stringify(command)); + // ignore unexpected command. + break; + } + }, + function _closedHandler(stateMachine, command) { + // ignore every command in closed state. + DEBUG && debug("unexpected command: " + JSON.stringify(command)); + }, +]; + +function ControllerStateMachine(channel, deviceId) { + this.state = State.INIT; + this._channel = channel; + this._deviceId = deviceId; +} + +ControllerStateMachine.prototype = { + launch: function _launch(presentationId, url) { + if (this.state === State.CONNECTED) { + this._sendCommand({ + type: CommandType.LAUNCH, + presentationId, + url, + }); + } + }, + + terminate: function _terminate(presentationId) { + if (this.state === State.CONNECTED) { + this._sendCommand({ + type: CommandType.TERMINATE, + presentationId, + }); + } + }, + + terminateAck: function _terminateAck(presentationId) { + if (this.state === State.CONNECTED) { + this._sendCommand({ + type: CommandType.TERMINATE_ACK, + presentationId, + }); + } + }, + + reconnect: function _reconnect(presentationId, url) { + if (this.state === State.CONNECTED) { + this._sendCommand({ + type: CommandType.RECONNECT, + presentationId, + url, + }); + } + }, + + sendOffer: function _sendOffer(offer) { + if (this.state === State.CONNECTED) { + this._sendCommand({ + type: CommandType.OFFER, + offer, + }); + } + }, + + sendAnswer: function _sendAnswer() { + // answer can only be sent by presenting UA. + debug("controller shouldn't generate answer"); + }, + + updateIceCandidate: function _updateIceCandidate(candidate) { + if (this.state === State.CONNECTED) { + this._sendCommand({ + type: CommandType.ICE_CANDIDATE, + candidate, + }); + } + }, + + onCommand: function _onCommand(command) { + handlers[this.state](this, command); + }, + + onChannelReady: function _onChannelReady() { + if (this.state === State.INIT) { + this._sendCommand({ + type: CommandType.CONNECT, + deviceId: this._deviceId, + }); + this.state = State.CONNECTING; + } + }, + + onChannelClosed: function _onChannelClose(reason, isByRemote) { + switch (this.state) { + case State.CONNECTED: + if (isByRemote) { + this.state = State.CLOSED; + this._notifyDisconnected(reason); + } else { + this._sendCommand({ + type: CommandType.DISCONNECT, + reason, + }); + this.state = State.CLOSING; + this._closeReason = reason; + } + break; + case State.CLOSING: + if (isByRemote) { + this.state = State.CLOSED; + if (this._closeReason) { + reason = this._closeReason; + delete this._closeReason; + } + this._notifyDisconnected(reason); + } + break; + default: + DEBUG && + debug("unexpected channel close: " + reason + ", " + isByRemote); + break; + } + }, + + _sendCommand: function _sendCommand(command) { + this._channel.sendCommand(command); + }, + + _notifyDeviceConnected: function _notifyDeviceConnected() { + // XXX trigger following command + this._channel.notifyDeviceConnected(); + }, + + _notifyDisconnected: function _notifyDisconnected(reason) { + this._channel.notifyDisconnected(reason); + }, + + _notifyLaunch: function _notifyLaunch(presentationId) { + this._channel.notifyLaunch(presentationId); + }, + + _notifyTerminate: function _notifyTerminate(presentationId) { + this._channel.notifyTerminate(presentationId); + }, + + _notifyReconnect: function _notifyReconnect(presentationId) { + this._channel.notifyReconnect(presentationId); + }, + + _notifyChannelDescriptor: function _notifyChannelDescriptor(command) { + switch (command.type) { + case CommandType.ANSWER: + this._channel.notifyAnswer(command.answer); + break; + case CommandType.ICE_CANDIDATE: + this._channel.notifyIceCandidate(command.candidate); + break; + } + }, +}; diff --git a/dom/presentation/provider/DeviceProviderHelpers.cpp b/dom/presentation/provider/DeviceProviderHelpers.cpp new file mode 100644 index 0000000000..011afc7040 --- /dev/null +++ b/dom/presentation/provider/DeviceProviderHelpers.cpp @@ -0,0 +1,53 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* 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/. */ + +#include "DeviceProviderHelpers.h" + +#include "nsCOMPtr.h" +#include "nsIURI.h" +#include "nsNetUtil.h" + +namespace mozilla { +namespace dom { +namespace presentation { + +static const char* const kFxTVPresentationAppUrls[] = { + "app://fling-player.gaiamobile.org/index.html", + "app://notification-receiver.gaiamobile.org/index.html", nullptr}; + +/* static */ +bool DeviceProviderHelpers::IsCommonlySupportedScheme(const nsAString& aUrl) { + nsCOMPtr<nsIURI> uri; + nsresult rv = NS_NewURI(getter_AddRefs(uri), aUrl); + if (NS_FAILED(rv) || !uri) { + return false; + } + + nsAutoCString scheme; + uri->GetScheme(scheme); + if (scheme.LowerCaseEqualsLiteral("http") || + scheme.LowerCaseEqualsLiteral("https")) { + return true; + } + + return false; +} + +/* static */ +bool DeviceProviderHelpers::IsFxTVSupportedAppUrl(const nsAString& aUrl) { + // Check if matched with any presentation Apps on TV. + for (uint32_t i = 0; kFxTVPresentationAppUrls[i]; i++) { + if (aUrl.EqualsASCII(kFxTVPresentationAppUrls[i])) { + return true; + } + } + + return false; +} + +} // namespace presentation +} // namespace dom +} // namespace mozilla diff --git a/dom/presentation/provider/DeviceProviderHelpers.h b/dom/presentation/provider/DeviceProviderHelpers.h new file mode 100644 index 0000000000..4bf6f9ae5b --- /dev/null +++ b/dom/presentation/provider/DeviceProviderHelpers.h @@ -0,0 +1,29 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* 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/. */ + +#ifndef mozilla_dom_presentation_DeviceProviderHelpers_h +#define mozilla_dom_presentation_DeviceProviderHelpers_h + +#include "nsString.h" + +namespace mozilla { +namespace dom { +namespace presentation { + +class DeviceProviderHelpers final { + public: + static bool IsCommonlySupportedScheme(const nsAString& aUrl); + static bool IsFxTVSupportedAppUrl(const nsAString& aUrl); + + private: + DeviceProviderHelpers() = delete; +}; + +} // namespace presentation +} // namespace dom +} // namespace mozilla + +#endif // mozilla_dom_presentation_DeviceProviderHelpers_h diff --git a/dom/presentation/provider/MulticastDNSDeviceProvider.cpp b/dom/presentation/provider/MulticastDNSDeviceProvider.cpp new file mode 100644 index 0000000000..d5f443dfd2 --- /dev/null +++ b/dom/presentation/provider/MulticastDNSDeviceProvider.cpp @@ -0,0 +1,1153 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* 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/. */ + +#include "MulticastDNSDeviceProvider.h" + +#include "DeviceProviderHelpers.h" +#include "MainThreadUtils.h" +#include "mozilla/IntegerPrintfMacros.h" +#include "mozilla/Logging.h" +#include "mozilla/Preferences.h" +#include "mozilla/Unused.h" +#include "nsComponentManagerUtils.h" +#include "nsIWritablePropertyBag2.h" +#include "nsServiceManagerUtils.h" +#include "nsTCPDeviceInfo.h" +#include "nsThreadUtils.h" + +#ifdef MOZ_WIDGET_ANDROID +# include "nsIPropertyBag2.h" +#endif // MOZ_WIDGET_ANDROID + +#define PREF_PRESENTATION_DISCOVERY "dom.presentation.discovery.enabled" +#define PREF_PRESENTATION_DISCOVERY_TIMEOUT_MS \ + "dom.presentation.discovery.timeout_ms" +#define PREF_PRESENTATION_DISCOVERABLE "dom.presentation.discoverable" +#define PREF_PRESENTATION_DISCOVERABLE_ENCRYPTED \ + "dom.presentation.discoverable.encrypted" +#define PREF_PRESENTATION_DISCOVERABLE_RETRY_MS \ + "dom.presentation.discoverable.retry_ms" +#define PREF_PRESENTATION_DEVICE_NAME "dom.presentation.device.name" + +#define SERVICE_TYPE "_presentation-ctrl._tcp" +#define PROTOCOL_VERSION_TAG u"version" +#define CERT_FINGERPRINT_TAG u"certFingerprint" + +static mozilla::LazyLogModule sMulticastDNSProviderLogModule( + "MulticastDNSDeviceProvider"); + +#undef LOG_I +#define LOG_I(...) \ + MOZ_LOG(sMulticastDNSProviderLogModule, mozilla::LogLevel::Debug, \ + (__VA_ARGS__)) +#undef LOG_E +#define LOG_E(...) \ + MOZ_LOG(sMulticastDNSProviderLogModule, mozilla::LogLevel::Error, \ + (__VA_ARGS__)) + +namespace mozilla { +namespace dom { +namespace presentation { + +static const char* kObservedPrefs[] = { + PREF_PRESENTATION_DISCOVERY, PREF_PRESENTATION_DISCOVERY_TIMEOUT_MS, + PREF_PRESENTATION_DISCOVERABLE, PREF_PRESENTATION_DEVICE_NAME, nullptr}; + +namespace { + +#ifdef MOZ_WIDGET_ANDROID +static void GetAndroidDeviceName(nsACString& aRetVal) { + nsCOMPtr<nsIPropertyBag2> infoService = + do_GetService("@mozilla.org/system-info;1"); + MOZ_ASSERT(infoService, "Could not find a system info service"); + + Unused << NS_WARN_IF( + NS_FAILED(infoService->GetPropertyAsACString(u"device"_ns, aRetVal))); +} +#endif // MOZ_WIDGET_ANDROID + +} // anonymous namespace + +/** + * This wrapper is used to break circular-reference problem. + */ +class DNSServiceWrappedListener final + : public nsIDNSServiceDiscoveryListener, + public nsIDNSRegistrationListener, + public nsIDNSServiceResolveListener, + public nsIPresentationControlServerListener { + public: + NS_DECL_ISUPPORTS + NS_FORWARD_SAFE_NSIDNSSERVICEDISCOVERYLISTENER(mListener) + NS_FORWARD_SAFE_NSIDNSREGISTRATIONLISTENER(mListener) + NS_FORWARD_SAFE_NSIDNSSERVICERESOLVELISTENER(mListener) + NS_FORWARD_SAFE_NSIPRESENTATIONCONTROLSERVERLISTENER(mListener) + + explicit DNSServiceWrappedListener() = default; + + nsresult SetListener(MulticastDNSDeviceProvider* aListener) { + mListener = aListener; + return NS_OK; + } + + private: + virtual ~DNSServiceWrappedListener() = default; + + MulticastDNSDeviceProvider* mListener = nullptr; +}; + +NS_IMPL_ISUPPORTS(DNSServiceWrappedListener, nsIDNSServiceDiscoveryListener, + nsIDNSRegistrationListener, nsIDNSServiceResolveListener, + nsIPresentationControlServerListener) + +NS_IMPL_ISUPPORTS(MulticastDNSDeviceProvider, nsIPresentationDeviceProvider, + nsIDNSServiceDiscoveryListener, nsIDNSRegistrationListener, + nsIDNSServiceResolveListener, + nsIPresentationControlServerListener, nsIObserver) + +MulticastDNSDeviceProvider::MulticastDNSDeviceProvider() = default; +MulticastDNSDeviceProvider::~MulticastDNSDeviceProvider() { Uninit(); } + +nsresult MulticastDNSDeviceProvider::Init() { + MOZ_ASSERT(NS_IsMainThread()); + + if (mInitialized) { + return NS_OK; + } + + nsresult rv; + + mMulticastDNS = do_GetService(DNSSERVICEDISCOVERY_CONTRACT_ID, &rv); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + + mWrappedListener = new DNSServiceWrappedListener(); + if (NS_WARN_IF(NS_FAILED(rv = mWrappedListener->SetListener(this)))) { + return rv; + } + + mPresentationService = + do_CreateInstance(PRESENTATION_CONTROL_SERVICE_CONTACT_ID, &rv); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + + mDiscoveryTimer = NS_NewTimer(); + if (NS_WARN_IF(!mDiscoveryTimer)) { + return NS_ERROR_OUT_OF_MEMORY; + } + + mServerRetryTimer = NS_NewTimer(); + if (NS_WARN_IF(!mServerRetryTimer)) { + return NS_ERROR_OUT_OF_MEMORY; + } + Preferences::AddStrongObservers(this, kObservedPrefs); + + mDiscoveryEnabled = Preferences::GetBool(PREF_PRESENTATION_DISCOVERY); + mDiscoveryTimeoutMs = + Preferences::GetUint(PREF_PRESENTATION_DISCOVERY_TIMEOUT_MS); + mDiscoverable = Preferences::GetBool(PREF_PRESENTATION_DISCOVERABLE); + mDiscoverableEncrypted = + Preferences::GetBool(PREF_PRESENTATION_DISCOVERABLE_ENCRYPTED); + mServerRetryMs = + Preferences::GetUint(PREF_PRESENTATION_DISCOVERABLE_RETRY_MS); + mServiceName.Truncate(); + Preferences::GetCString(PREF_PRESENTATION_DEVICE_NAME, mServiceName); + +#ifdef MOZ_WIDGET_ANDROID + // FIXME: Bug 1185806 - Provide a common device name setting. + if (mServiceName.IsEmpty()) { + GetAndroidDeviceName(mServiceName); + Unused << Preferences::SetCString(PREF_PRESENTATION_DEVICE_NAME, + mServiceName); + } +#endif // MOZ_WIDGET_ANDROID + + Unused << mPresentationService->SetId(mServiceName); + + if (mDiscoveryEnabled && NS_WARN_IF(NS_FAILED(rv = ForceDiscovery()))) { + return rv; + } + + if (mDiscoverable && NS_WARN_IF(NS_FAILED(rv = StartServer()))) { + return rv; + } + + mInitialized = true; + return NS_OK; +} + +void MulticastDNSDeviceProvider::Uninit() { + MOZ_ASSERT(NS_IsMainThread()); + + if (!mInitialized) { + return; + } + + ClearDevices(); + + Preferences::RemoveObservers(this, kObservedPrefs); + + StopDiscovery(NS_OK); + StopServer(); + + mMulticastDNS = nullptr; + + if (mWrappedListener) { + mWrappedListener->SetListener(nullptr); + mWrappedListener = nullptr; + } + + mInitialized = false; +} + +nsresult MulticastDNSDeviceProvider::StartServer() { + LOG_I("StartServer: %s (%d)", mServiceName.get(), mDiscoverable); + MOZ_ASSERT(NS_IsMainThread()); + + if (!mDiscoverable) { + return NS_OK; + } + + nsresult rv; + + uint16_t servicePort; + if (NS_WARN_IF(NS_FAILED(rv = mPresentationService->GetPort(&servicePort)))) { + return rv; + } + + /** + * If |servicePort| is non-zero, it means PresentationControlService is + * running. Otherwise, we should make it start serving. + */ + if (servicePort) { + return RegisterMDNSService(); + } + + if (NS_WARN_IF(NS_FAILED( + rv = mPresentationService->SetListener(mWrappedListener)))) { + return rv; + } + + AbortServerRetry(); + + if (NS_WARN_IF(NS_FAILED( + rv = mPresentationService->StartServer(mDiscoverableEncrypted, 0)))) { + return rv; + } + + return NS_OK; +} + +void MulticastDNSDeviceProvider::StopServer() { + LOG_I("StopServer: %s", mServiceName.get()); + MOZ_ASSERT(NS_IsMainThread()); + + UnregisterMDNSService(NS_OK); + + AbortServerRetry(); + + if (mPresentationService) { + mPresentationService->SetListener(nullptr); + mPresentationService->Close(); + } +} + +void MulticastDNSDeviceProvider::AbortServerRetry() { + if (mIsServerRetrying) { + mIsServerRetrying = false; + mServerRetryTimer->Cancel(); + } +} + +nsresult MulticastDNSDeviceProvider::RegisterMDNSService() { + LOG_I("RegisterMDNSService: %s", mServiceName.get()); + + if (!mDiscoverable) { + return NS_OK; + } + + // Cancel on going service registration. + UnregisterMDNSService(NS_OK); + + nsresult rv; + + uint16_t servicePort; + if (NS_FAILED(rv = mPresentationService->GetPort(&servicePort)) || + !servicePort) { + // Abort service registration if server port is not available. + return rv; + } + + /** + * Register the presentation control channel server as an mDNS service. + */ + nsCOMPtr<nsIDNSServiceInfo> serviceInfo = + do_CreateInstance(DNSSERVICEINFO_CONTRACT_ID, &rv); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + if (NS_WARN_IF(NS_FAILED( + rv = serviceInfo->SetServiceType(nsLiteralCString(SERVICE_TYPE))))) { + return rv; + } + if (NS_WARN_IF(NS_FAILED(rv = serviceInfo->SetServiceName(mServiceName)))) { + return rv; + } + if (NS_WARN_IF(NS_FAILED(rv = serviceInfo->SetPort(servicePort)))) { + return rv; + } + + nsCOMPtr<nsIWritablePropertyBag2> propBag = + do_CreateInstance("@mozilla.org/hash-property-bag;1"); + MOZ_ASSERT(propBag); + + uint32_t version; + rv = mPresentationService->GetVersion(&version); + MOZ_ASSERT(NS_SUCCEEDED(rv)); + + rv = propBag->SetPropertyAsUint32(nsLiteralString(PROTOCOL_VERSION_TAG), + version); + MOZ_ASSERT(NS_SUCCEEDED(rv)); + + if (mDiscoverableEncrypted) { + nsAutoCString certFingerprint; + rv = mPresentationService->GetCertFingerprint(certFingerprint); + MOZ_ASSERT(NS_SUCCEEDED(rv)); + + rv = propBag->SetPropertyAsACString(nsLiteralString(CERT_FINGERPRINT_TAG), + certFingerprint); + MOZ_ASSERT(NS_SUCCEEDED(rv)); + } + + if (NS_WARN_IF(NS_FAILED(rv = serviceInfo->SetAttributes(propBag)))) { + return rv; + } + + return mMulticastDNS->RegisterService(serviceInfo, mWrappedListener, + getter_AddRefs(mRegisterRequest)); +} + +void MulticastDNSDeviceProvider::UnregisterMDNSService(nsresult aReason) { + LOG_I("UnregisterMDNSService: %s (0x%08" PRIx32 ")", mServiceName.get(), + static_cast<uint32_t>(aReason)); + MOZ_ASSERT(NS_IsMainThread()); + + if (mRegisterRequest) { + mRegisterRequest->Cancel(aReason); + mRegisterRequest = nullptr; + } +} + +nsresult MulticastDNSDeviceProvider::StopDiscovery(nsresult aReason) { + LOG_I("StopDiscovery (0x%08" PRIx32 ")", static_cast<uint32_t>(aReason)); + + MOZ_ASSERT(NS_IsMainThread()); + MOZ_ASSERT(mDiscoveryTimer); + + Unused << mDiscoveryTimer->Cancel(); + + if (mDiscoveryRequest) { + mDiscoveryRequest->Cancel(aReason); + mDiscoveryRequest = nullptr; + } + + return NS_OK; +} + +nsresult MulticastDNSDeviceProvider::Connect( + Device* aDevice, nsIPresentationControlChannel** aRetVal) { + MOZ_ASSERT(aDevice); + MOZ_ASSERT(mPresentationService); + + RefPtr<TCPDeviceInfo> deviceInfo = + new TCPDeviceInfo(aDevice->Id(), aDevice->Address(), aDevice->Port(), + aDevice->CertFingerprint()); + + return mPresentationService->Connect(deviceInfo, aRetVal); +} + +bool MulticastDNSDeviceProvider::IsCompatibleServer( + nsIDNSServiceInfo* aServiceInfo) { + MOZ_ASSERT(aServiceInfo); + + nsCOMPtr<nsIPropertyBag2> propBag; + if (NS_WARN_IF( + NS_FAILED(aServiceInfo->GetAttributes(getter_AddRefs(propBag)))) || + !propBag) { + return false; + } + + uint32_t remoteVersion; + if (NS_WARN_IF(NS_FAILED(propBag->GetPropertyAsUint32( + nsLiteralString(PROTOCOL_VERSION_TAG), &remoteVersion)))) { + return false; + } + + bool isCompatible = false; + Unused << NS_WARN_IF(NS_FAILED( + mPresentationService->IsCompatibleServer(remoteVersion, &isCompatible))); + + return isCompatible; +} + +nsresult MulticastDNSDeviceProvider::AddDevice( + const nsACString& aId, const nsACString& aServiceName, + const nsACString& aServiceType, const nsACString& aAddress, + const uint16_t aPort, const nsACString& aCertFingerprint) { + MOZ_ASSERT(NS_IsMainThread()); + MOZ_ASSERT(mPresentationService); + + RefPtr<Device> device = + new Device(aId, /* ID */ + aServiceName, aServiceType, aAddress, aPort, aCertFingerprint, + DeviceState::eActive, this); + + nsCOMPtr<nsIPresentationDeviceListener> listener; + if (NS_SUCCEEDED(GetListener(getter_AddRefs(listener))) && listener) { + Unused << listener->AddDevice(device); + } + + mDevices.AppendElement(device); + + return NS_OK; +} + +nsresult MulticastDNSDeviceProvider::UpdateDevice( + const uint32_t aIndex, const nsACString& aServiceName, + const nsACString& aServiceType, const nsACString& aAddress, + const uint16_t aPort, const nsACString& aCertFingerprint) { + MOZ_ASSERT(NS_IsMainThread()); + MOZ_ASSERT(mPresentationService); + + if (NS_WARN_IF(aIndex >= mDevices.Length())) { + return NS_ERROR_INVALID_ARG; + } + + RefPtr<Device> device = mDevices[aIndex]; + device->Update(aServiceName, aServiceType, aAddress, aPort, aCertFingerprint); + device->ChangeState(DeviceState::eActive); + + nsCOMPtr<nsIPresentationDeviceListener> listener; + if (NS_SUCCEEDED(GetListener(getter_AddRefs(listener))) && listener) { + Unused << listener->UpdateDevice(device); + } + + return NS_OK; +} + +nsresult MulticastDNSDeviceProvider::RemoveDevice(const uint32_t aIndex) { + MOZ_ASSERT(NS_IsMainThread()); + MOZ_ASSERT(mPresentationService); + + if (NS_WARN_IF(aIndex >= mDevices.Length())) { + return NS_ERROR_INVALID_ARG; + } + + RefPtr<Device> device = mDevices[aIndex]; + + LOG_I("RemoveDevice: %s", device->Id().get()); + mDevices.RemoveElementAt(aIndex); + + nsCOMPtr<nsIPresentationDeviceListener> listener; + if (NS_SUCCEEDED(GetListener(getter_AddRefs(listener))) && listener) { + Unused << listener->RemoveDevice(device); + } + + return NS_OK; +} + +bool MulticastDNSDeviceProvider::FindDeviceById(const nsACString& aId, + uint32_t& aIndex) { + MOZ_ASSERT(NS_IsMainThread()); + + RefPtr<Device> device = new Device(aId, + /* aName = */ ""_ns, + /* aType = */ ""_ns, + /* aHost = */ ""_ns, + /* aPort = */ 0, + /* aCertFingerprint */ ""_ns, + /* aState = */ DeviceState::eUnknown, + /* aProvider = */ nullptr); + size_t index = mDevices.IndexOf(device, 0, DeviceIdComparator()); + + if (index == mDevices.NoIndex) { + return false; + } + + aIndex = index; + return true; +} + +bool MulticastDNSDeviceProvider::FindDeviceByAddress(const nsACString& aAddress, + uint32_t& aIndex) { + MOZ_ASSERT(NS_IsMainThread()); + + RefPtr<Device> device = new Device(/* aId = */ ""_ns, + /* aName = */ ""_ns, + /* aType = */ ""_ns, aAddress, + /* aPort = */ 0, + /* aCertFingerprint */ ""_ns, + /* aState = */ DeviceState::eUnknown, + /* aProvider = */ nullptr); + size_t index = mDevices.IndexOf(device, 0, DeviceAddressComparator()); + + if (index == mDevices.NoIndex) { + return false; + } + + aIndex = index; + return true; +} + +void MulticastDNSDeviceProvider::MarkAllDevicesUnknown() { + MOZ_ASSERT(NS_IsMainThread()); + + for (auto& device : mDevices) { + device->ChangeState(DeviceState::eUnknown); + } +} + +void MulticastDNSDeviceProvider::ClearUnknownDevices() { + MOZ_ASSERT(NS_IsMainThread()); + + size_t i = mDevices.Length(); + while (i > 0) { + --i; + if (mDevices[i]->State() == DeviceState::eUnknown) { + Unused << NS_WARN_IF(NS_FAILED(RemoveDevice(i))); + } + } +} + +void MulticastDNSDeviceProvider::ClearDevices() { + MOZ_ASSERT(NS_IsMainThread()); + + size_t i = mDevices.Length(); + while (i > 0) { + --i; + Unused << NS_WARN_IF(NS_FAILED(RemoveDevice(i))); + } +} + +// nsIPresentationDeviceProvider +NS_IMETHODIMP +MulticastDNSDeviceProvider::GetListener( + nsIPresentationDeviceListener** aListener) { + MOZ_ASSERT(NS_IsMainThread()); + + if (NS_WARN_IF(!aListener)) { + return NS_ERROR_INVALID_POINTER; + } + + nsresult rv; + nsCOMPtr<nsIPresentationDeviceListener> listener = + do_QueryReferent(mDeviceListener, &rv); + if (NS_WARN_IF(NS_FAILED(rv))) { + return rv; + } + + listener.forget(aListener); + + return NS_OK; +} + +NS_IMETHODIMP +MulticastDNSDeviceProvider::SetListener( + nsIPresentationDeviceListener* aListener) { + MOZ_ASSERT(NS_IsMainThread()); + + mDeviceListener = do_GetWeakReference(aListener); + + nsresult rv; + if (mDeviceListener) { + if (NS_WARN_IF(NS_FAILED(rv = Init()))) { + return rv; + } + } else { + Uninit(); + } + + return NS_OK; +} + +NS_IMETHODIMP +MulticastDNSDeviceProvider::ForceDiscovery() { + LOG_I("ForceDiscovery (%d)", mDiscoveryEnabled); + MOZ_ASSERT(NS_IsMainThread()); + + if (!mDiscoveryEnabled) { + return NS_OK; + } + + MOZ_ASSERT(mDiscoveryTimer); + MOZ_ASSERT(mMulticastDNS); + + // if it's already discovering, extend existing discovery timeout. + nsresult rv; + if (mIsDiscovering) { + Unused << mDiscoveryTimer->Cancel(); + + if (NS_WARN_IF( + NS_FAILED(rv = mDiscoveryTimer->Init(this, mDiscoveryTimeoutMs, + nsITimer::TYPE_ONE_SHOT)))) { + return rv; + } + return NS_OK; + } + + StopDiscovery(NS_OK); + + if (NS_WARN_IF(NS_FAILED(rv = mMulticastDNS->StartDiscovery( + nsLiteralCString(SERVICE_TYPE), mWrappedListener, + getter_AddRefs(mDiscoveryRequest))))) { + return rv; + } + + return NS_OK; +} + +// nsIDNSServiceDiscoveryListener +NS_IMETHODIMP +MulticastDNSDeviceProvider::OnDiscoveryStarted(const nsACString& aServiceType) { + LOG_I("OnDiscoveryStarted"); + MOZ_ASSERT(NS_IsMainThread()); + MOZ_ASSERT(mDiscoveryTimer); + + MarkAllDevicesUnknown(); + + nsresult rv; + if (NS_WARN_IF( + NS_FAILED(rv = mDiscoveryTimer->Init(this, mDiscoveryTimeoutMs, + nsITimer::TYPE_ONE_SHOT)))) { + return rv; + } + + mIsDiscovering = true; + + return NS_OK; +} + +NS_IMETHODIMP +MulticastDNSDeviceProvider::OnDiscoveryStopped(const nsACString& aServiceType) { + LOG_I("OnDiscoveryStopped"); + MOZ_ASSERT(NS_IsMainThread()); + + ClearUnknownDevices(); + + mIsDiscovering = false; + + return NS_OK; +} + +NS_IMETHODIMP +MulticastDNSDeviceProvider::OnServiceFound(nsIDNSServiceInfo* aServiceInfo) { + MOZ_ASSERT(NS_IsMainThread()); + + if (NS_WARN_IF(!aServiceInfo)) { + return NS_ERROR_INVALID_ARG; + } + + nsresult rv; + + nsAutoCString serviceName; + if (NS_WARN_IF(NS_FAILED(rv = aServiceInfo->GetServiceName(serviceName)))) { + return rv; + } + + LOG_I("OnServiceFound: %s", serviceName.get()); + + if (mMulticastDNS) { + if (NS_WARN_IF(NS_FAILED(rv = mMulticastDNS->ResolveService( + aServiceInfo, mWrappedListener)))) { + return rv; + } + } + + return NS_OK; +} + +NS_IMETHODIMP +MulticastDNSDeviceProvider::OnServiceLost(nsIDNSServiceInfo* aServiceInfo) { + MOZ_ASSERT(NS_IsMainThread()); + + if (NS_WARN_IF(!aServiceInfo)) { + return NS_ERROR_INVALID_ARG; + } + + nsresult rv; + + nsAutoCString serviceName; + if (NS_WARN_IF(NS_FAILED(rv = aServiceInfo->GetServiceName(serviceName)))) { + return rv; + } + + LOG_I("OnServiceLost: %s", serviceName.get()); + + nsAutoCString host; + if (NS_WARN_IF(NS_FAILED(rv = aServiceInfo->GetHost(host)))) { + return rv; + } + + uint32_t index; + if (!FindDeviceById(host, index)) { + // given device was not found + return NS_OK; + } + + if (NS_WARN_IF(NS_FAILED(rv = RemoveDevice(index)))) { + return rv; + } + + return NS_OK; +} + +NS_IMETHODIMP +MulticastDNSDeviceProvider::OnStartDiscoveryFailed( + const nsACString& aServiceType, int32_t aErrorCode) { + LOG_E("OnStartDiscoveryFailed: %d", aErrorCode); + MOZ_ASSERT(NS_IsMainThread()); + + return NS_OK; +} + +NS_IMETHODIMP +MulticastDNSDeviceProvider::OnStopDiscoveryFailed( + const nsACString& aServiceType, int32_t aErrorCode) { + LOG_E("OnStopDiscoveryFailed: %d", aErrorCode); + MOZ_ASSERT(NS_IsMainThread()); + + return NS_OK; +} + +// nsIDNSRegistrationListener +NS_IMETHODIMP +MulticastDNSDeviceProvider::OnServiceRegistered( + nsIDNSServiceInfo* aServiceInfo) { + MOZ_ASSERT(NS_IsMainThread()); + + if (NS_WARN_IF(!aServiceInfo)) { + return NS_ERROR_INVALID_ARG; + } + nsresult rv; + + nsAutoCString name; + if (NS_WARN_IF(NS_FAILED(rv = aServiceInfo->GetServiceName(name)))) { + return rv; + } + + LOG_I("OnServiceRegistered (%s)", name.get()); + mRegisteredName = name; + + if (mMulticastDNS) { + if (NS_WARN_IF(NS_FAILED(rv = mMulticastDNS->ResolveService( + aServiceInfo, mWrappedListener)))) { + return rv; + } + } + + return NS_OK; +} + +NS_IMETHODIMP +MulticastDNSDeviceProvider::OnServiceUnregistered( + nsIDNSServiceInfo* aServiceInfo) { + LOG_I("OnServiceUnregistered"); + MOZ_ASSERT(NS_IsMainThread()); + + return NS_OK; +} + +NS_IMETHODIMP +MulticastDNSDeviceProvider::OnRegistrationFailed( + nsIDNSServiceInfo* aServiceInfo, int32_t aErrorCode) { + LOG_E("OnRegistrationFailed: %d", aErrorCode); + MOZ_ASSERT(NS_IsMainThread()); + + mRegisterRequest = nullptr; + + if (aErrorCode == nsIDNSRegistrationListener::ERROR_SERVICE_NOT_RUNNING) { + return NS_DispatchToMainThread(NewRunnableMethod( + "dom::presentation::MulticastDNSDeviceProvider::RegisterMDNSService", + this, &MulticastDNSDeviceProvider::RegisterMDNSService)); + } + + return NS_OK; +} + +NS_IMETHODIMP +MulticastDNSDeviceProvider::OnUnregistrationFailed( + nsIDNSServiceInfo* aServiceInfo, int32_t aErrorCode) { + LOG_E("OnUnregistrationFailed: %d", aErrorCode); + MOZ_ASSERT(NS_IsMainThread()); + + return NS_OK; +} + +// nsIDNSServiceResolveListener +NS_IMETHODIMP +MulticastDNSDeviceProvider::OnServiceResolved(nsIDNSServiceInfo* aServiceInfo) { + MOZ_ASSERT(NS_IsMainThread()); + + if (NS_WARN_IF(!aServiceInfo)) { + return NS_ERROR_INVALID_ARG; + } + + nsresult rv; + + nsAutoCString serviceName; + if (NS_WARN_IF(NS_FAILED(rv = aServiceInfo->GetServiceName(serviceName)))) { + return rv; + } + + LOG_I("OnServiceResolved: %s", serviceName.get()); + + nsAutoCString host; + if (NS_WARN_IF(NS_FAILED(rv = aServiceInfo->GetHost(host)))) { + return rv; + } + + if (mRegisteredName == serviceName) { + LOG_I("ignore self"); + + if (NS_WARN_IF(NS_FAILED(rv = mPresentationService->SetId(host)))) { + return rv; + } + + return NS_OK; + } + + if (!IsCompatibleServer(aServiceInfo)) { + LOG_I("ignore incompatible service: %s", serviceName.get()); + return NS_OK; + } + + nsAutoCString address; + if (NS_WARN_IF(NS_FAILED(rv = aServiceInfo->GetAddress(address)))) { + return rv; + } + + uint16_t port; + if (NS_WARN_IF(NS_FAILED(rv = aServiceInfo->GetPort(&port)))) { + return rv; + } + + nsAutoCString serviceType; + if (NS_WARN_IF(NS_FAILED(rv = aServiceInfo->GetServiceType(serviceType)))) { + return rv; + } + + nsCOMPtr<nsIPropertyBag2> propBag; + if (NS_WARN_IF( + NS_FAILED(aServiceInfo->GetAttributes(getter_AddRefs(propBag)))) || + !propBag) { + return rv; + } + + nsAutoCString certFingerprint; + Unused << propBag->GetPropertyAsACString( + nsLiteralString(CERT_FINGERPRINT_TAG), certFingerprint); + + uint32_t index; + if (FindDeviceById(host, index)) { + return UpdateDevice(index, serviceName, serviceType, address, port, + certFingerprint); + } + return AddDevice(host, serviceName, serviceType, address, port, + certFingerprint); +} + +NS_IMETHODIMP +MulticastDNSDeviceProvider::OnResolveFailed(nsIDNSServiceInfo* aServiceInfo, + int32_t aErrorCode) { + LOG_E("OnResolveFailed: %d", aErrorCode); + MOZ_ASSERT(NS_IsMainThread()); + + return NS_OK; +} + +// nsIPresentationControlServerListener +NS_IMETHODIMP +MulticastDNSDeviceProvider::OnServerReady(uint16_t aPort, + const nsACString& aCertFingerprint) { + LOG_I("OnServerReady: %d, %s", aPort, + PromiseFlatCString(aCertFingerprint).get()); + MOZ_ASSERT(NS_IsMainThread()); + + if (mDiscoverable) { + RegisterMDNSService(); + } + + return NS_OK; +} + +NS_IMETHODIMP +MulticastDNSDeviceProvider::OnServerStopped(nsresult aResult) { + LOG_I("OnServerStopped: (0x%08" PRIx32 ")", static_cast<uint32_t>(aResult)); + + UnregisterMDNSService(aResult); + + // Try restart server if it is stopped abnormally. + if (NS_FAILED(aResult) && mDiscoverable) { + mIsServerRetrying = true; + mServerRetryTimer->Init(this, mServerRetryMs, nsITimer::TYPE_ONE_SHOT); + } + + return NS_OK; +} + +// Create a new device if we were unable to find one with the address. +already_AddRefed<MulticastDNSDeviceProvider::Device> +MulticastDNSDeviceProvider::GetOrCreateDevice(nsITCPDeviceInfo* aDeviceInfo) { + nsAutoCString address; + Unused << aDeviceInfo->GetAddress(address); + + RefPtr<Device> device; + uint32_t index; + if (FindDeviceByAddress(address, index)) { + device = mDevices[index]; + } else { + // Create a one-time device object for non-discoverable controller. + // This device will not be in the list of available devices and cannot + // be used for requesting session. + nsAutoCString id; + Unused << aDeviceInfo->GetId(id); + uint16_t port; + Unused << aDeviceInfo->GetPort(&port); + + device = new Device(id, + /* aName = */ id, + /* aType = */ ""_ns, address, port, + /* aCertFingerprint */ ""_ns, DeviceState::eActive, + /* aProvider = */ nullptr); + } + + return device.forget(); +} + +NS_IMETHODIMP +MulticastDNSDeviceProvider::OnSessionRequest( + nsITCPDeviceInfo* aDeviceInfo, const nsAString& aUrl, + const nsAString& aPresentationId, + nsIPresentationControlChannel* aControlChannel) { + MOZ_ASSERT(NS_IsMainThread()); + + nsAutoCString address; + Unused << aDeviceInfo->GetAddress(address); + + LOG_I("OnSessionRequest: %s", address.get()); + + RefPtr<Device> device = GetOrCreateDevice(aDeviceInfo); + nsCOMPtr<nsIPresentationDeviceListener> listener; + if (NS_SUCCEEDED(GetListener(getter_AddRefs(listener))) && listener) { + Unused << listener->OnSessionRequest(device, aUrl, aPresentationId, + aControlChannel); + } + + return NS_OK; +} + +NS_IMETHODIMP +MulticastDNSDeviceProvider::OnTerminateRequest( + nsITCPDeviceInfo* aDeviceInfo, const nsAString& aPresentationId, + nsIPresentationControlChannel* aControlChannel, bool aIsFromReceiver) { + MOZ_ASSERT(NS_IsMainThread()); + + nsAutoCString address; + Unused << aDeviceInfo->GetAddress(address); + + LOG_I("OnTerminateRequest: %s", address.get()); + + RefPtr<Device> device = GetOrCreateDevice(aDeviceInfo); + nsCOMPtr<nsIPresentationDeviceListener> listener; + if (NS_SUCCEEDED(GetListener(getter_AddRefs(listener))) && listener) { + Unused << listener->OnTerminateRequest(device, aPresentationId, + aControlChannel, aIsFromReceiver); + } + + return NS_OK; +} + +NS_IMETHODIMP +MulticastDNSDeviceProvider::OnReconnectRequest( + nsITCPDeviceInfo* aDeviceInfo, const nsAString& aUrl, + const nsAString& aPresentationId, + nsIPresentationControlChannel* aControlChannel) { + MOZ_ASSERT(NS_IsMainThread()); + + nsAutoCString address; + Unused << aDeviceInfo->GetAddress(address); + + LOG_I("OnReconnectRequest: %s", address.get()); + + RefPtr<Device> device = GetOrCreateDevice(aDeviceInfo); + nsCOMPtr<nsIPresentationDeviceListener> listener; + if (NS_SUCCEEDED(GetListener(getter_AddRefs(listener))) && listener) { + Unused << listener->OnReconnectRequest(device, aUrl, aPresentationId, + aControlChannel); + } + + return NS_OK; +} + +// nsIObserver +NS_IMETHODIMP +MulticastDNSDeviceProvider::Observe(nsISupports* aSubject, const char* aTopic, + const char16_t* aData) { + MOZ_ASSERT(NS_IsMainThread()); + + NS_ConvertUTF16toUTF8 data(aData); + LOG_I("Observe: topic = %s, data = %s", aTopic, data.get()); + + if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) { + if (data.EqualsLiteral(PREF_PRESENTATION_DISCOVERY)) { + OnDiscoveryChanged(Preferences::GetBool(PREF_PRESENTATION_DISCOVERY)); + } else if (data.EqualsLiteral(PREF_PRESENTATION_DISCOVERY_TIMEOUT_MS)) { + OnDiscoveryTimeoutChanged( + Preferences::GetUint(PREF_PRESENTATION_DISCOVERY_TIMEOUT_MS)); + } else if (data.EqualsLiteral(PREF_PRESENTATION_DISCOVERABLE)) { + OnDiscoverableChanged( + Preferences::GetBool(PREF_PRESENTATION_DISCOVERABLE)); + } else if (data.EqualsLiteral(PREF_PRESENTATION_DEVICE_NAME)) { + nsAutoCString newServiceName; + Preferences::GetCString(PREF_PRESENTATION_DEVICE_NAME, newServiceName); + if (!mServiceName.Equals(newServiceName)) { + OnServiceNameChanged(newServiceName); + } + } + } else if (!strcmp(aTopic, NS_TIMER_CALLBACK_TOPIC)) { + nsCOMPtr<nsITimer> timer = do_QueryInterface(aSubject); + if (!timer) { + return NS_ERROR_UNEXPECTED; + } + + if (timer == mDiscoveryTimer) { + StopDiscovery(NS_OK); + } else if (timer == mServerRetryTimer) { + mIsServerRetrying = false; + StartServer(); + } + } + + return NS_OK; +} + +nsresult MulticastDNSDeviceProvider::OnDiscoveryChanged(bool aEnabled) { + LOG_I("DiscoveryEnabled = %d\n", aEnabled); + MOZ_ASSERT(NS_IsMainThread()); + + mDiscoveryEnabled = aEnabled; + + if (mDiscoveryEnabled) { + return ForceDiscovery(); + } + + return StopDiscovery(NS_OK); +} + +nsresult MulticastDNSDeviceProvider::OnDiscoveryTimeoutChanged( + uint32_t aTimeoutMs) { + LOG_I("OnDiscoveryTimeoutChanged = %d\n", aTimeoutMs); + MOZ_ASSERT(NS_IsMainThread()); + + mDiscoveryTimeoutMs = aTimeoutMs; + + return NS_OK; +} + +nsresult MulticastDNSDeviceProvider::OnDiscoverableChanged(bool aEnabled) { + LOG_I("Discoverable = %d\n", aEnabled); + MOZ_ASSERT(NS_IsMainThread()); + + mDiscoverable = aEnabled; + + if (mDiscoverable) { + return StartServer(); + } + + StopServer(); + return NS_OK; +} + +nsresult MulticastDNSDeviceProvider::OnServiceNameChanged( + const nsACString& aServiceName) { + LOG_I("serviceName = %s\n", PromiseFlatCString(aServiceName).get()); + MOZ_ASSERT(NS_IsMainThread()); + + mServiceName = aServiceName; + + UnregisterMDNSService(NS_OK); + + if (mDiscoverable) { + return RegisterMDNSService(); + } + + return NS_OK; +} + +// MulticastDNSDeviceProvider::Device +NS_IMPL_ISUPPORTS(MulticastDNSDeviceProvider::Device, nsIPresentationDevice) + +// nsIPresentationDevice +NS_IMETHODIMP +MulticastDNSDeviceProvider::Device::GetId(nsACString& aId) { + aId = mId; + + return NS_OK; +} + +NS_IMETHODIMP +MulticastDNSDeviceProvider::Device::GetName(nsACString& aName) { + aName = mName; + + return NS_OK; +} + +NS_IMETHODIMP +MulticastDNSDeviceProvider::Device::GetType(nsACString& aType) { + aType = mType; + + return NS_OK; +} + +NS_IMETHODIMP +MulticastDNSDeviceProvider::Device::EstablishControlChannel( + nsIPresentationControlChannel** aRetVal) { + if (!mProvider) { + return NS_ERROR_FAILURE; + } + + return mProvider->Connect(this, aRetVal); +} + +NS_IMETHODIMP +MulticastDNSDeviceProvider::Device::Disconnect() { + // No need to do anything when disconnect. + return NS_OK; +} + +NS_IMETHODIMP +MulticastDNSDeviceProvider::Device::IsRequestedUrlSupported( + const nsAString& aRequestedUrl, bool* aRetVal) { + MOZ_ASSERT(NS_IsMainThread()); + + if (!aRetVal) { + return NS_ERROR_INVALID_POINTER; + } + + // TV 2.6 also supports presentation Apps and HTTP/HTTPS hosted receiver page. + if (DeviceProviderHelpers::IsFxTVSupportedAppUrl(aRequestedUrl) || + DeviceProviderHelpers::IsCommonlySupportedScheme(aRequestedUrl)) { + *aRetVal = true; + } + + return NS_OK; +} + +} // namespace presentation +} // namespace dom +} // namespace mozilla diff --git a/dom/presentation/provider/MulticastDNSDeviceProvider.h b/dom/presentation/provider/MulticastDNSDeviceProvider.h new file mode 100644 index 0000000000..ff2866d799 --- /dev/null +++ b/dom/presentation/provider/MulticastDNSDeviceProvider.h @@ -0,0 +1,185 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* 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/. */ + +#ifndef mozilla_dom_presentation_provider_MulticastDNSDeviceProvider_h +#define mozilla_dom_presentation_provider_MulticastDNSDeviceProvider_h + +#include "mozilla/RefPtr.h" +#include "nsCOMPtr.h" +#include "nsICancelable.h" +#include "nsIDNSServiceDiscovery.h" +#include "nsIObserver.h" +#include "nsIPresentationDevice.h" +#include "nsIPresentationDeviceProvider.h" +#include "nsIPresentationControlService.h" +#include "nsITimer.h" +#include "nsIWeakReferenceUtils.h" +#include "nsString.h" +#include "nsTArray.h" + +class nsITCPDeviceInfo; + +namespace mozilla { +namespace dom { +namespace presentation { + +class DNSServiceWrappedListener; +class MulticastDNSService; + +class MulticastDNSDeviceProvider final + : public nsIPresentationDeviceProvider, + public nsIDNSServiceDiscoveryListener, + public nsIDNSRegistrationListener, + public nsIDNSServiceResolveListener, + public nsIPresentationControlServerListener, + public nsIObserver { + public: + NS_DECL_ISUPPORTS + NS_DECL_NSIPRESENTATIONDEVICEPROVIDER + NS_DECL_NSIDNSSERVICEDISCOVERYLISTENER + NS_DECL_NSIDNSREGISTRATIONLISTENER + NS_DECL_NSIDNSSERVICERESOLVELISTENER + NS_DECL_NSIPRESENTATIONCONTROLSERVERLISTENER + NS_DECL_NSIOBSERVER + + explicit MulticastDNSDeviceProvider(); + nsresult Init(); + void Uninit(); + + private: + enum class DeviceState : uint32_t { eUnknown, eActive }; + + class Device final : public nsIPresentationDevice { + public: + NS_DECL_ISUPPORTS + NS_DECL_NSIPRESENTATIONDEVICE + + explicit Device(const nsACString& aId, const nsACString& aName, + const nsACString& aType, const nsACString& aAddress, + const uint16_t aPort, const nsACString& aCertFingerprint, + DeviceState aState, MulticastDNSDeviceProvider* aProvider) + : mId(aId), + mName(aName), + mType(aType), + mAddress(aAddress), + mPort(aPort), + mCertFingerprint(aCertFingerprint), + mState(aState), + mProvider(aProvider) {} + + const nsCString& Id() const { return mId; } + + const nsCString& Address() const { return mAddress; } + + uint16_t Port() const { return mPort; } + + const nsCString& CertFingerprint() const { return mCertFingerprint; } + + DeviceState State() const { return mState; } + + void ChangeState(DeviceState aState) { mState = aState; } + + void Update(const nsACString& aName, const nsACString& aType, + const nsACString& aAddress, const uint16_t aPort, + const nsACString& aCertFingerprint) { + mName = aName; + mType = aType; + mAddress = aAddress; + mPort = aPort; + mCertFingerprint = aCertFingerprint; + } + + private: + virtual ~Device() = default; + + nsCString mId; + nsCString mName; + nsCString mType; + nsCString mAddress; + uint16_t mPort; + nsCString mCertFingerprint; + DeviceState mState; + MulticastDNSDeviceProvider* mProvider; + }; + + struct DeviceIdComparator { + bool Equals(const RefPtr<Device>& aA, const RefPtr<Device>& aB) const { + return aA->Id() == aB->Id(); + } + }; + + struct DeviceAddressComparator { + bool Equals(const RefPtr<Device>& aA, const RefPtr<Device>& aB) const { + return aA->Address() == aB->Address(); + } + }; + + virtual ~MulticastDNSDeviceProvider(); + nsresult StartServer(); + void StopServer(); + void AbortServerRetry(); + nsresult RegisterMDNSService(); + void UnregisterMDNSService(nsresult aReason); + nsresult StopDiscovery(nsresult aReason); + nsresult Connect(Device* aDevice, nsIPresentationControlChannel** aRetVal); + bool IsCompatibleServer(nsIDNSServiceInfo* aServiceInfo); + + // device manipulation + nsresult AddDevice(const nsACString& aId, const nsACString& aServiceName, + const nsACString& aServiceType, const nsACString& aAddress, + const uint16_t aPort, const nsACString& aCertFingerprint); + nsresult UpdateDevice(const uint32_t aIndex, const nsACString& aServiceName, + const nsACString& aServiceType, + const nsACString& aAddress, const uint16_t aPort, + const nsACString& aCertFingerprint); + nsresult RemoveDevice(const uint32_t aIndex); + bool FindDeviceById(const nsACString& aId, uint32_t& aIndex); + + bool FindDeviceByAddress(const nsACString& aAddress, uint32_t& aIndex); + + already_AddRefed<Device> GetOrCreateDevice(nsITCPDeviceInfo* aDeviceInfo); + + void MarkAllDevicesUnknown(); + void ClearUnknownDevices(); + void ClearDevices(); + + // preferences + nsresult OnDiscoveryChanged(bool aEnabled); + nsresult OnDiscoveryTimeoutChanged(uint32_t aTimeoutMs); + nsresult OnDiscoverableChanged(bool aEnabled); + nsresult OnServiceNameChanged(const nsACString& aServiceName); + + bool mInitialized = false; + nsWeakPtr mDeviceListener; + nsCOMPtr<nsIPresentationControlService> mPresentationService; + nsCOMPtr<nsIDNSServiceDiscovery> mMulticastDNS; + RefPtr<DNSServiceWrappedListener> mWrappedListener; + + nsCOMPtr<nsICancelable> mDiscoveryRequest; + nsCOMPtr<nsICancelable> mRegisterRequest; + + nsTArray<RefPtr<Device>> mDevices; + + bool mDiscoveryEnabled = false; + bool mIsDiscovering = false; + uint32_t mDiscoveryTimeoutMs; + nsCOMPtr<nsITimer> mDiscoveryTimer; + + bool mDiscoverable = false; + bool mDiscoverableEncrypted = false; + bool mIsServerRetrying = false; + uint32_t mServerRetryMs; + nsCOMPtr<nsITimer> mServerRetryTimer; + + nsCString mServiceName; + nsCString mRegisteredName; +}; + +} // namespace presentation +} // namespace dom +} // namespace mozilla + +#endif // mozilla_dom_presentation_provider_MulticastDNSDeviceProvider_h diff --git a/dom/presentation/provider/PresentationControlService.jsm b/dom/presentation/provider/PresentationControlService.jsm new file mode 100644 index 0000000000..07e46d1f1c --- /dev/null +++ b/dom/presentation/provider/PresentationControlService.jsm @@ -0,0 +1,1054 @@ +/* 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 { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); +const { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm"); +const { clearTimeout, setTimeout } = ChromeUtils.import( + "resource://gre/modules/Timer.jsm" +); + +ChromeUtils.defineModuleGetter( + this, + "ControllerStateMachine", + "resource://gre/modules/presentation/ControllerStateMachine.jsm" +); +ChromeUtils.defineModuleGetter( + this, + "ReceiverStateMachine", + "resource://gre/modules/presentation/ReceiverStateMachine.jsm" +); + +const kProtocolVersion = 1; // need to review isCompatibleServer while fiddling the version number. +const kLocalCertName = "presentation"; + +const DEBUG = Services.prefs.getBoolPref("dom.presentation.tcp_server.debug"); +function log(aMsg) { + dump("-*- PresentationControlService.js: " + aMsg + "\n"); +} + +function TCPDeviceInfo(aAddress, aPort, aId, aCertFingerprint) { + this.address = aAddress; + this.port = aPort; + this.id = aId; + this.certFingerprint = aCertFingerprint || ""; +} + +function PresentationControlService() { + this._id = null; + this._port = 0; + this._serverSocket = null; +} + +PresentationControlService.prototype = { + /** + * If a user agent connects to this server, we create a control channel but + * hand it to |TCPDevice.listener| when the initial information exchange + * finishes. Therefore, we hold the control channels in this period. + */ + _controlChannels: [], + + startServer(aEncrypted, aPort) { + if (this._isServiceInit()) { + DEBUG && + log("PresentationControlService - server socket has been initialized"); + throw Components.Exception("", Cr.NS_ERROR_FAILURE); + } + + /** + * 0 or undefined indicates opt-out parameter, and a port will be selected + * automatically. + */ + let serverSocketPort = + typeof aPort !== "undefined" && aPort !== 0 ? aPort : -1; + + if (aEncrypted) { + let self = this; + let localCertService = Cc[ + "@mozilla.org/security/local-cert-service;1" + ].getService(Ci.nsILocalCertService); + localCertService.getOrCreateCert(kLocalCertName, { + handleCert(aCert, aRv) { + DEBUG && log("PresentationControlService - handleCert"); + if (aRv) { + self._notifyServerStopped(aRv); + } else { + self._serverSocket = Cc[ + "@mozilla.org/network/tls-server-socket;1" + ].createInstance(Ci.nsITLSServerSocket); + + self._serverSocketInit(serverSocketPort, aCert); + } + }, + }); + } else { + this._serverSocket = Cc[ + "@mozilla.org/network/server-socket;1" + ].createInstance(Ci.nsIServerSocket); + + this._serverSocketInit(serverSocketPort, null); + } + }, + + _serverSocketInit(aPort, aCert) { + if (!this._serverSocket) { + DEBUG && log("PresentationControlService - create server socket fail."); + throw Components.Exception("", Cr.NS_ERROR_FAILURE); + } + + try { + this._serverSocket.init(aPort, false, -1); + + if (aCert) { + this._serverSocket.serverCert = aCert; + this._serverSocket.setSessionTickets(false); + let requestCert = Ci.nsITLSServerSocket.REQUEST_NEVER; + this._serverSocket.setRequestClientCertificate(requestCert); + } + + this._serverSocket.asyncListen(this); + } catch (e) { + // NS_ERROR_SOCKET_ADDRESS_IN_USE + DEBUG && + log("PresentationControlService - init server socket fail: " + e); + throw Components.Exception("", Cr.NS_ERROR_FAILURE); + } + + this._port = this._serverSocket.port; + + DEBUG && + log("PresentationControlService - service start on port: " + this._port); + + // Monitor network interface change to restart server socket. + Services.obs.addObserver(this, "network:offline-status-changed"); + + this._notifyServerReady(); + }, + + _notifyServerReady() { + Services.tm.dispatchToMainThread(() => { + if (this._listener) { + this._listener.onServerReady(this._port, this.certFingerprint); + } + }); + }, + + _notifyServerStopped(aRv) { + Services.tm.dispatchToMainThread(() => { + if (this._listener) { + this._listener.onServerStopped(aRv); + } + }); + }, + + isCompatibleServer(aVersion) { + // No compatibility issue for the first version of control protocol + return this.version === aVersion; + }, + + get id() { + return this._id; + }, + + set id(aId) { + this._id = aId; + }, + + get port() { + return this._port; + }, + + get version() { + return kProtocolVersion; + }, + + get certFingerprint() { + if (!this._serverSocket.serverCert) { + return null; + } + + return this._serverSocket.serverCert.sha256Fingerprint; + }, + + set listener(aListener) { + this._listener = aListener; + }, + + get listener() { + return this._listener; + }, + + _isServiceInit() { + return this._serverSocket !== null; + }, + + connect(aDeviceInfo) { + if (!this.id) { + DEBUG && + log( + "PresentationControlService - Id has not initialized; connect fails" + ); + return null; + } + DEBUG && log("PresentationControlService - connect to " + aDeviceInfo.id); + + let socketTransport = this._attemptConnect(aDeviceInfo); + return new TCPControlChannel(this, socketTransport, aDeviceInfo, "sender"); + }, + + _attemptConnect(aDeviceInfo) { + let sts = Cc["@mozilla.org/network/socket-transport-service;1"].getService( + Ci.nsISocketTransportService + ); + + let socketTransport; + try { + if (aDeviceInfo.certFingerprint) { + let overrideService = Cc[ + "@mozilla.org/security/certoverride;1" + ].getService(Ci.nsICertOverrideService); + overrideService.rememberTemporaryValidityOverrideUsingFingerprint( + aDeviceInfo.address, + aDeviceInfo.port, + aDeviceInfo.certFingerprint, + Ci.nsICertOverrideService.ERROR_UNTRUSTED | + Ci.nsICertOverrideService.ERROR_MISMATCH + ); + + socketTransport = sts.createTransport( + ["ssl"], + aDeviceInfo.address, + aDeviceInfo.port, + null + ); + } else { + socketTransport = sts.createTransport( + [], + aDeviceInfo.address, + aDeviceInfo.port, + null + ); + } + // Shorten the connection failure procedure. + socketTransport.setTimeout(Ci.nsISocketTransport.TIMEOUT_CONNECT, 2); + } catch (e) { + DEBUG && log("PresentationControlService - createTransport throws: " + e); + // Pop the exception to |TCPDevice.establishControlChannel| + throw Components.Exception("", Cr.NS_ERROR_FAILURE); + } + return socketTransport; + }, + + responseSession(aDeviceInfo, aSocketTransport) { + if (!this._isServiceInit()) { + DEBUG && + log( + "PresentationControlService - should never receive remote " + + "session request before server socket initialization" + ); + return null; + } + DEBUG && + log( + "PresentationControlService - responseSession to " + + JSON.stringify(aDeviceInfo) + ); + return new TCPControlChannel( + this, + aSocketTransport, + aDeviceInfo, + "receiver" + ); + }, + + // Triggered by TCPControlChannel + onSessionRequest(aDeviceInfo, aUrl, aPresentationId, aControlChannel) { + DEBUG && + log( + "PresentationControlService - onSessionRequest: " + + aDeviceInfo.address + + ":" + + aDeviceInfo.port + ); + if (!this.listener) { + this.releaseControlChannel(aControlChannel); + return; + } + + this.listener.onSessionRequest( + aDeviceInfo, + aUrl, + aPresentationId, + aControlChannel + ); + this.releaseControlChannel(aControlChannel); + }, + + onSessionTerminate( + aDeviceInfo, + aPresentationId, + aControlChannel, + aIsFromReceiver + ) { + DEBUG && + log( + "TCPPresentationServer - onSessionTerminate: " + + aDeviceInfo.address + + ":" + + aDeviceInfo.port + ); + if (!this.listener) { + this.releaseControlChannel(aControlChannel); + return; + } + + this.listener.onTerminateRequest( + aDeviceInfo, + aPresentationId, + aControlChannel, + aIsFromReceiver + ); + this.releaseControlChannel(aControlChannel); + }, + + onSessionReconnect(aDeviceInfo, aUrl, aPresentationId, aControlChannel) { + DEBUG && + log( + "TCPPresentationServer - onSessionReconnect: " + + aDeviceInfo.address + + ":" + + aDeviceInfo.port + ); + if (!this.listener) { + this.releaseControlChannel(aControlChannel); + return; + } + + this.listener.onReconnectRequest( + aDeviceInfo, + aUrl, + aPresentationId, + aControlChannel + ); + this.releaseControlChannel(aControlChannel); + }, + + // nsIServerSocketListener (Triggered by nsIServerSocket.init) + onSocketAccepted(aServerSocket, aClientSocket) { + DEBUG && + log( + "PresentationControlService - onSocketAccepted: " + + aClientSocket.host + + ":" + + aClientSocket.port + ); + let deviceInfo = new TCPDeviceInfo(aClientSocket.host, aClientSocket.port); + this.holdControlChannel(this.responseSession(deviceInfo, aClientSocket)); + }, + + holdControlChannel(aControlChannel) { + this._controlChannels.push(aControlChannel); + }, + + releaseControlChannel(aControlChannel) { + let index = this._controlChannels.indexOf(aControlChannel); + if (index !== -1) { + delete this._controlChannels[index]; + } + }, + + // nsIServerSocketListener (Triggered by nsIServerSocket.init) + onStopListening(aServerSocket, aStatus) { + DEBUG && log("PresentationControlService - onStopListening: " + aStatus); + }, + + close() { + DEBUG && log("PresentationControlService - close"); + if (this._isServiceInit()) { + DEBUG && log("PresentationControlService - close server socket"); + this._serverSocket.close(); + this._serverSocket = null; + + Services.obs.removeObserver(this, "network:offline-status-changed"); + + this._notifyServerStopped(Cr.NS_OK); + } + this._port = 0; + }, + + // nsIObserver + observe(aSubject, aTopic, aData) { + DEBUG && log("PresentationControlService - observe: " + aTopic); + switch (aTopic) { + case "network:offline-status-changed": { + if (aData == "offline") { + DEBUG && log("network offline"); + return; + } + this._restartServer(); + break; + } + } + }, + + _restartServer() { + DEBUG && log("PresentationControlService - restart service"); + + // restart server socket + if (this._isServiceInit()) { + this.close(); + + try { + this.startServer(); + } catch (e) { + DEBUG && log("PresentationControlService - restart service fail: " + e); + } + } + }, + + classID: Components.ID("{f4079b8b-ede5-4b90-a112-5b415a931deb}"), + QueryInterface: ChromeUtils.generateQI([ + "nsIServerSocketListener", + "nsIPresentationControlService", + "nsIObserver", + ]), +}; + +function ChannelDescription(aInit) { + this._type = aInit.type; + switch (this._type) { + case Ci.nsIPresentationChannelDescription.TYPE_TCP: + this._tcpAddresses = Cc["@mozilla.org/array;1"].createInstance( + Ci.nsIMutableArray + ); + for (let address of aInit.tcpAddress) { + let wrapper = Cc["@mozilla.org/supports-cstring;1"].createInstance( + Ci.nsISupportsCString + ); + wrapper.data = address; + this._tcpAddresses.appendElement(wrapper); + } + + this._tcpPort = aInit.tcpPort; + break; + case Ci.nsIPresentationChannelDescription.TYPE_DATACHANNEL: + this._dataChannelSDP = aInit.dataChannelSDP; + break; + } +} + +ChannelDescription.prototype = { + _type: 0, + _tcpAddresses: null, + _tcpPort: 0, + _dataChannelSDP: "", + + get type() { + return this._type; + }, + + get tcpAddress() { + return this._tcpAddresses; + }, + + get tcpPort() { + return this._tcpPort; + }, + + get dataChannelSDP() { + return this._dataChannelSDP; + }, + + classID: Components.ID("{82507aea-78a2-487e-904a-858a6c5bf4e1}"), + QueryInterface: ChromeUtils.generateQI(["nsIPresentationChannelDescription"]), +}; + +// Helper function: transfer nsIPresentationChannelDescription to json +function discriptionAsJson(aDescription) { + let json = {}; + json.type = aDescription.type; + switch (aDescription.type) { + case Ci.nsIPresentationChannelDescription.TYPE_TCP: + let addresses = aDescription.tcpAddress.QueryInterface(Ci.nsIArray); + json.tcpAddress = []; + for (let idx = 0; idx < addresses.length; idx++) { + let address = addresses.queryElementAt(idx, Ci.nsISupportsCString); + json.tcpAddress.push(address.data); + } + json.tcpPort = aDescription.tcpPort; + break; + case Ci.nsIPresentationChannelDescription.TYPE_DATACHANNEL: + json.dataChannelSDP = aDescription.dataChannelSDP; + break; + } + return json; +} + +const kDisconnectTimeout = 5000; +const kTerminateTimeout = 5000; + +function TCPControlChannel( + presentationService, + transport, + deviceInfo, + direction +) { + DEBUG && log("create TCPControlChannel for : " + direction); + this._deviceInfo = deviceInfo; + this._direction = direction; + this._transport = transport; + + this._presentationService = presentationService; + + if (direction === "receiver") { + // Need to set security observer before I/O stream operation. + this._setSecurityObserver(this); + } + + let currentThread = Services.tm.currentThread; + transport.setEventSink(this, currentThread); + + this._input = this._transport + .openInputStream(0, 0, 0) + .QueryInterface(Ci.nsIAsyncInputStream); + this._input.asyncWait( + this.QueryInterface(Ci.nsIStreamListener), + Ci.nsIAsyncInputStream.WAIT_CLOSURE_ONLY, + 0, + currentThread + ); + + this._output = this._transport + .openOutputStream(Ci.nsITransport.OPEN_UNBUFFERED, 0, 0) + .QueryInterface(Ci.nsIAsyncOutputStream); + + this._outgoingMsgs = []; + + this._stateMachine = + direction === "sender" + ? new ControllerStateMachine(this, presentationService.id) + : new ReceiverStateMachine(this); + + if (direction === "receiver" && !transport.securityInfo) { + // Since the transport created by server socket is already CONNECTED_TO. + this._outgoingEnabled = true; + this._createInputStreamPump(); + } +} + +TCPControlChannel.prototype = { + _outgoingEnabled: false, + _incomingEnabled: false, + _pendingOpen: false, + _pendingOffer: null, + _pendingAnswer: null, + _pendingClose: null, + _pendingCloseReason: null, + _pendingReconnect: false, + + sendOffer(aOffer) { + this._stateMachine.sendOffer(discriptionAsJson(aOffer)); + }, + + sendAnswer(aAnswer) { + this._stateMachine.sendAnswer(discriptionAsJson(aAnswer)); + }, + + sendIceCandidate(aCandidate) { + this._stateMachine.updateIceCandidate(aCandidate); + }, + + launch(aPresentationId, aUrl) { + this._stateMachine.launch(aPresentationId, aUrl); + }, + + terminate(aPresentationId) { + if (!this._terminatingId) { + this._terminatingId = aPresentationId; + this._stateMachine.terminate(aPresentationId); + + // Start a guard timer to ensure terminateAck is processed. + this._terminateTimer = setTimeout(() => { + DEBUG && + log("TCPControlChannel - terminate timeout: " + aPresentationId); + delete this._terminateTimer; + if (this._pendingDisconnect) { + this._pendingDisconnect(); + } else { + this.disconnect(Cr.NS_OK); + } + }, kTerminateTimeout); + } else { + this._stateMachine.terminateAck(aPresentationId); + delete this._terminatingId; + } + }, + + _flushOutgoing() { + if (!this._outgoingEnabled || this._outgoingMsgs.length === 0) { + return; + } + + this._output.asyncWait(this, 0, 0, Services.tm.currentThread); + }, + + // may throw an exception + _send(aMsg) { + DEBUG && log("TCPControlChannel - Send: " + JSON.stringify(aMsg, null, 2)); + + /** + * XXX In TCP streaming, it is possible that more than one message in one + * TCP packet. We use line delimited JSON to identify where one JSON encoded + * object ends and the next begins. Therefore, we do not allow newline + * characters whithin the whole message, and add a newline at the end. + * Please see the parser code in |onDataAvailable|. + */ + let message = JSON.stringify(aMsg).replace(["\n"], "") + "\n"; + try { + this._output.write(message, message.length); + } catch (e) { + DEBUG && log("TCPControlChannel - Failed to send message: " + e.name); + throw e; + } + }, + + _setSecurityObserver(observer) { + if (this._transport && this._transport.securityInfo) { + DEBUG && log("TCPControlChannel - setSecurityObserver: " + observer); + let connectionInfo = this._transport.securityInfo.QueryInterface( + Ci.nsITLSServerConnectionInfo + ); + connectionInfo.setSecurityObserver(observer); + } + }, + + // nsITLSServerSecurityObserver + onHandshakeDone(socket, clientStatus) { + log( + "TCPControlChannel - onHandshakeDone: TLS version: " + + clientStatus.tlsVersionUsed.toString(16) + ); + this._setSecurityObserver(null); + + // Process input/output after TLS handshake is complete. + this._outgoingEnabled = true; + this._createInputStreamPump(); + }, + + // nsIAsyncOutputStream + onOutputStreamReady() { + DEBUG && log("TCPControlChannel - onOutputStreamReady"); + if (this._outgoingMsgs.length === 0) { + return; + } + + try { + this._send(this._outgoingMsgs[0]); + } catch (e) { + if (e.result === Cr.NS_BASE_STREAM_WOULD_BLOCK) { + this._output.asyncWait(this, 0, 0, Services.tm.currentThread); + return; + } + + this._closeTransport(); + return; + } + this._outgoingMsgs.shift(); + this._flushOutgoing(); + }, + + // nsIAsyncInputStream (Triggered by nsIInputStream.asyncWait) + // Only used for detecting connection refused + onInputStreamReady(aStream) { + DEBUG && log("TCPControlChannel - onInputStreamReady"); + try { + aStream.available(); + } catch (e) { + DEBUG && log("TCPControlChannel - onInputStreamReady error: " + e.name); + // NS_ERROR_CONNECTION_REFUSED + this._notifyDisconnected(e.result); + } + }, + + // nsITransportEventSink (Triggered by nsISocketTransport.setEventSink) + onTransportStatus(aTransport, aStatus) { + DEBUG && + log( + "TCPControlChannel - onTransportStatus: " + + aStatus.toString(16) + + " with role: " + + this._direction + ); + if (aStatus === Ci.nsISocketTransport.STATUS_CONNECTED_TO) { + this._outgoingEnabled = true; + this._createInputStreamPump(); + } + }, + + // nsIRequestObserver (Triggered by nsIInputStreamPump.asyncRead) + onStartRequest() { + DEBUG && + log("TCPControlChannel - onStartRequest with role: " + this._direction); + this._incomingEnabled = true; + }, + + // nsIRequestObserver (Triggered by nsIInputStreamPump.asyncRead) + onStopRequest(aRequest, aContext, aStatus) { + DEBUG && + log( + "TCPControlChannel - onStopRequest: " + + aStatus + + " with role: " + + this._direction + ); + this._stateMachine.onChannelClosed(aStatus, true); + }, + + // nsIStreamListener (Triggered by nsIInputStreamPump.asyncRead) + onDataAvailable(aRequest, aInputStream) { + let data = NetUtil.readInputStreamToString( + aInputStream, + aInputStream.available() + ); + DEBUG && log("TCPControlChannel - onDataAvailable: " + data); + + // Parser of line delimited JSON. Please see |_send| for more informaiton. + let jsonArray = data.split("\n"); + jsonArray.pop(); + for (let json of jsonArray) { + let msg; + try { + msg = JSON.parse(json); + } catch (e) { + DEBUG && log("TCPSignalingChannel - error in parsing json: " + e); + } + + this._handleMessage(msg); + } + }, + + _createInputStreamPump() { + if (this._pump) { + return; + } + + DEBUG && + log("TCPControlChannel - create pump with role: " + this._direction); + this._pump = Cc["@mozilla.org/network/input-stream-pump;1"].createInstance( + Ci.nsIInputStreamPump + ); + this._pump.init(this._input, 0, 0, false); + this._pump.asyncRead(this, null); + this._stateMachine.onChannelReady(); + }, + + // Handle command from remote side + _handleMessage(aMsg) { + DEBUG && + log( + "TCPControlChannel - handleMessage from " + + JSON.stringify(this._deviceInfo) + + ": " + + JSON.stringify(aMsg) + ); + this._stateMachine.onCommand(aMsg); + }, + + get listener() { + return this._listener; + }, + + set listener(aListener) { + DEBUG && log("TCPControlChannel - set listener: " + aListener); + if (!aListener) { + this._listener = null; + return; + } + + this._listener = aListener; + if (this._pendingOpen) { + this._pendingOpen = false; + DEBUG && log("TCPControlChannel - notify pending opened"); + this._listener.notifyConnected(); + } + + if (this._pendingOffer) { + let offer = this._pendingOffer; + DEBUG && + log( + "TCPControlChannel - notify pending offer: " + JSON.stringify(offer) + ); + this._listener.onOffer(new ChannelDescription(offer)); + this._pendingOffer = null; + } + + if (this._pendingAnswer) { + let answer = this._pendingAnswer; + DEBUG && + log( + "TCPControlChannel - notify pending answer: " + JSON.stringify(answer) + ); + this._listener.onAnswer(new ChannelDescription(answer)); + this._pendingAnswer = null; + } + + if (this._pendingClose) { + DEBUG && log("TCPControlChannel - notify pending closed"); + this._notifyDisconnected(this._pendingCloseReason); + this._pendingClose = null; + } + + if (this._pendingReconnect) { + DEBUG && log("TCPControlChannel - notify pending reconnected"); + this._notifyReconnected(); + this._pendingReconnect = false; + } + }, + + /** + * These functions are designed to handle the interaction with listener + * appropriately. |_FUNC| is to handle |this._listener.FUNC|. + */ + _onOffer(aOffer) { + if (!this._incomingEnabled) { + return; + } + if (!this._listener) { + this._pendingOffer = aOffer; + return; + } + DEBUG && log("TCPControlChannel - notify offer: " + JSON.stringify(aOffer)); + this._listener.onOffer(new ChannelDescription(aOffer)); + }, + + _onAnswer(aAnswer) { + if (!this._incomingEnabled) { + return; + } + if (!this._listener) { + this._pendingAnswer = aAnswer; + return; + } + DEBUG && + log("TCPControlChannel - notify answer: " + JSON.stringify(aAnswer)); + this._listener.onAnswer(new ChannelDescription(aAnswer)); + }, + + _notifyConnected() { + this._pendingClose = false; + this._pendingCloseReason = Cr.NS_OK; + + if (!this._listener) { + this._pendingOpen = true; + return; + } + + DEBUG && + log("TCPControlChannel - notify opened with role: " + this._direction); + this._listener.notifyConnected(); + }, + + _notifyDisconnected(aReason) { + this._pendingOpen = false; + this._pendingOffer = null; + this._pendingAnswer = null; + + // Remote endpoint closes the control channel with abnormal reason. + if (aReason == Cr.NS_OK && this._pendingCloseReason != Cr.NS_OK) { + aReason = this._pendingCloseReason; + } + + if (!this._listener) { + this._pendingClose = true; + this._pendingCloseReason = aReason; + return; + } + + DEBUG && + log("TCPControlChannel - notify closed with role: " + this._direction); + this._listener.notifyDisconnected(aReason); + }, + + _notifyReconnected() { + if (!this._listener) { + this._pendingReconnect = true; + return; + } + + DEBUG && + log( + "TCPControlChannel - notify reconnected with role: " + this._direction + ); + this._listener.notifyReconnected(); + }, + + _closeOutgoing() { + if (this._outgoingEnabled) { + this._output.close(); + this._outgoingEnabled = false; + } + }, + _closeIncoming() { + if (this._incomingEnabled) { + this._pump = null; + this._input.close(); + this._incomingEnabled = false; + } + }, + _closeTransport() { + if (this._disconnectTimer) { + clearTimeout(this._disconnectTimer); + delete this._disconnectTimer; + } + + if (this._terminateTimer) { + clearTimeout(this._terminateTimer); + delete this._terminateTimer; + } + + delete this._pendingDisconnect; + + this._transport.setEventSink(null, null); + + this._closeIncoming(); + this._closeOutgoing(); + this._presentationService.releaseControlChannel(this); + }, + + disconnect(aReason) { + DEBUG && log("TCPControlChannel - disconnect with reason: " + aReason); + + // Pending disconnect during termination procedure. + if (this._terminateTimer) { + // Store only the first disconnect action. + if (!this._pendingDisconnect) { + this._pendingDisconnect = this.disconnect.bind(this, aReason); + } + return; + } + + if (this._outgoingEnabled && !this._disconnectTimer) { + // default reason is NS_OK + aReason = !aReason ? Cr.NS_OK : aReason; + + this._stateMachine.onChannelClosed(aReason, false); + + // Start a guard timer to ensure the transport will be closed. + this._disconnectTimer = setTimeout(() => { + DEBUG && log("TCPControlChannel - disconnect timeout"); + this._closeTransport(); + }, kDisconnectTimeout); + } + }, + + reconnect(aPresentationId, aUrl) { + DEBUG && log("TCPControlChannel - reconnect with role: " + this._direction); + if (this._direction != "sender") { + throw Components.Exception("", Cr.NS_ERROR_FAILURE); + } + + this._stateMachine.reconnect(aPresentationId, aUrl); + }, + + // callback from state machine + sendCommand(command) { + this._outgoingMsgs.push(command); + this._flushOutgoing(); + }, + + notifyDeviceConnected(deviceId) { + switch (this._direction) { + case "receiver": + this._deviceInfo.id = deviceId; + break; + } + this._notifyConnected(); + }, + + notifyDisconnected(reason) { + this._closeTransport(); + this._notifyDisconnected(reason); + }, + + notifyLaunch(presentationId, url) { + switch (this._direction) { + case "receiver": + this._presentationService.onSessionRequest( + this._deviceInfo, + url, + presentationId, + this + ); + break; + } + }, + + notifyTerminate(presentationId) { + if (!this._terminatingId) { + this._terminatingId = presentationId; + this._presentationService.onSessionTerminate( + this._deviceInfo, + presentationId, + this, + this._direction === "sender" + ); + return; + } + + // Cancel terminate guard timer after receiving terminate-ack. + if (this._terminateTimer) { + clearTimeout(this._terminateTimer); + delete this._terminateTimer; + } + + if (this._terminatingId !== presentationId) { + // Requested presentation Id doesn't matched with the one in ACK. + // Disconnect the control channel with error. + DEBUG && + log("TCPControlChannel - unmatched terminatingId: " + presentationId); + this.disconnect(Cr.NS_ERROR_FAILURE); + } + + delete this._terminatingId; + if (this._pendingDisconnect) { + this._pendingDisconnect(); + } + }, + + notifyReconnect(presentationId, url) { + switch (this._direction) { + case "receiver": + this._presentationService.onSessionReconnect( + this._deviceInfo, + url, + presentationId, + this + ); + break; + case "sender": + this._notifyReconnected(); + break; + } + }, + + notifyOffer(offer) { + this._onOffer(offer); + }, + + notifyAnswer(answer) { + this._onAnswer(answer); + }, + + notifyIceCandidate(candidate) { + this._listener.onIceCandidate(candidate); + }, + + classID: Components.ID("{fefb8286-0bdc-488b-98bf-0c11b485c955}"), + QueryInterface: ChromeUtils.generateQI([ + "nsIPresentationControlChannel", + "nsIStreamListener", + ]), +}; + +var EXPORTED_SYMBOLS = ["PresentationControlService"]; diff --git a/dom/presentation/provider/ReceiverStateMachine.jsm b/dom/presentation/provider/ReceiverStateMachine.jsm new file mode 100644 index 0000000000..aaad83a20e --- /dev/null +++ b/dom/presentation/provider/ReceiverStateMachine.jsm @@ -0,0 +1,232 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* 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"; + +var EXPORTED_SYMBOLS = ["ReceiverStateMachine"]; + +const { CommandType, State } = ChromeUtils.import( + "resource://gre/modules/presentation/StateMachineHelper.jsm" +); + +const DEBUG = false; +function debug(str) { + dump("-*- ReceiverStateMachine: " + str + "\n"); +} + +var handlers = [ + function _initHandler(stateMachine, command) { + // shouldn't receive any command at init state + DEBUG && debug("unexpected command: " + JSON.stringify(command)); + }, + function _connectingHandler(stateMachine, command) { + switch (command.type) { + case CommandType.CONNECT: + stateMachine._sendCommand({ + type: CommandType.CONNECT_ACK, + }); + stateMachine.state = State.CONNECTED; + stateMachine._notifyDeviceConnected(command.deviceId); + break; + case CommandType.DISCONNECT: + stateMachine.state = State.CLOSED; + stateMachine._notifyDisconnected(command.reason); + break; + default: + debug("unexpected command: " + JSON.stringify(command)); + // ignore unexpected command + break; + } + }, + function _connectedHandler(stateMachine, command) { + switch (command.type) { + case CommandType.DISCONNECT: + stateMachine.state = State.CLOSED; + stateMachine._notifyDisconnected(command.reason); + break; + case CommandType.LAUNCH: + stateMachine._notifyLaunch(command.presentationId, command.url); + stateMachine._sendCommand({ + type: CommandType.LAUNCH_ACK, + presentationId: command.presentationId, + }); + break; + case CommandType.TERMINATE: + stateMachine._notifyTerminate(command.presentationId); + break; + case CommandType.TERMINATE_ACK: + stateMachine._notifyTerminate(command.presentationId); + break; + case CommandType.OFFER: + case CommandType.ICE_CANDIDATE: + stateMachine._notifyChannelDescriptor(command); + break; + case CommandType.RECONNECT: + stateMachine._notifyReconnect(command.presentationId, command.url); + stateMachine._sendCommand({ + type: CommandType.RECONNECT_ACK, + presentationId: command.presentationId, + }); + break; + default: + debug("unexpected command: " + JSON.stringify(command)); + // ignore unexpected command + break; + } + }, + function _closingHandler(stateMachine, command) { + switch (command.type) { + case CommandType.DISCONNECT: + stateMachine.state = State.CLOSED; + stateMachine._notifyDisconnected(command.reason); + break; + default: + debug("unexpected command: " + JSON.stringify(command)); + // ignore unexpected command + break; + } + }, + function _closedHandler(stateMachine, command) { + // ignore every command in closed state. + DEBUG && debug("unexpected command: " + JSON.stringify(command)); + }, +]; + +function ReceiverStateMachine(channel) { + this.state = State.INIT; + this._channel = channel; +} + +ReceiverStateMachine.prototype = { + launch: function _launch() { + // presentation session can only be launched by controlling UA. + debug("receiver shouldn't trigger launch"); + }, + + terminate: function _terminate(presentationId) { + if (this.state === State.CONNECTED) { + this._sendCommand({ + type: CommandType.TERMINATE, + presentationId, + }); + } + }, + + terminateAck: function _terminateAck(presentationId) { + if (this.state === State.CONNECTED) { + this._sendCommand({ + type: CommandType.TERMINATE_ACK, + presentationId, + }); + } + }, + + reconnect: function _reconnect() { + debug("receiver shouldn't trigger reconnect"); + }, + + sendOffer: function _sendOffer() { + // offer can only be sent by controlling UA. + debug("receiver shouldn't generate offer"); + }, + + sendAnswer: function _sendAnswer(answer) { + if (this.state === State.CONNECTED) { + this._sendCommand({ + type: CommandType.ANSWER, + answer, + }); + } + }, + + updateIceCandidate: function _updateIceCandidate(candidate) { + if (this.state === State.CONNECTED) { + this._sendCommand({ + type: CommandType.ICE_CANDIDATE, + candidate, + }); + } + }, + + onCommand: function _onCommand(command) { + handlers[this.state](this, command); + }, + + onChannelReady: function _onChannelReady() { + if (this.state === State.INIT) { + this.state = State.CONNECTING; + } + }, + + onChannelClosed: function _onChannelClose(reason, isByRemote) { + switch (this.state) { + case State.CONNECTED: + if (isByRemote) { + this.state = State.CLOSED; + this._notifyDisconnected(reason); + } else { + this._sendCommand({ + type: CommandType.DISCONNECT, + reason, + }); + this.state = State.CLOSING; + this._closeReason = reason; + } + break; + case State.CLOSING: + if (isByRemote) { + this.state = State.CLOSED; + if (this._closeReason) { + reason = this._closeReason; + delete this._closeReason; + } + this._notifyDisconnected(reason); + } else { + // do nothing and wait for remote channel closed. + } + break; + default: + DEBUG && + debug("unexpected channel close: " + reason + ", " + isByRemote); + break; + } + }, + + _sendCommand: function _sendCommand(command) { + this._channel.sendCommand(command); + }, + + _notifyDeviceConnected: function _notifyDeviceConnected(deviceName) { + this._channel.notifyDeviceConnected(deviceName); + }, + + _notifyDisconnected: function _notifyDisconnected(reason) { + this._channel.notifyDisconnected(reason); + }, + + _notifyLaunch: function _notifyLaunch(presentationId, url) { + this._channel.notifyLaunch(presentationId, url); + }, + + _notifyTerminate: function _notifyTerminate(presentationId) { + this._channel.notifyTerminate(presentationId); + }, + + _notifyReconnect: function _notifyReconnect(presentationId, url) { + this._channel.notifyReconnect(presentationId, url); + }, + + _notifyChannelDescriptor: function _notifyChannelDescriptor(command) { + switch (command.type) { + case CommandType.OFFER: + this._channel.notifyOffer(command.offer); + break; + case CommandType.ICE_CANDIDATE: + this._channel.notifyIceCandidate(command.candidate); + break; + } + }, +}; diff --git a/dom/presentation/provider/StateMachineHelper.jsm b/dom/presentation/provider/StateMachineHelper.jsm new file mode 100644 index 0000000000..cc2dc02895 --- /dev/null +++ b/dom/presentation/provider/StateMachineHelper.jsm @@ -0,0 +1,38 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* 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"; + +var EXPORTED_SYMBOLS = ["State", "CommandType"]; + +const State = Object.freeze({ + INIT: 0, + CONNECTING: 1, + CONNECTED: 2, + CLOSING: 3, + CLOSED: 4, +}); + +const CommandType = Object.freeze({ + // control channel life cycle + CONNECT: "connect", // { deviceId: <string> } + CONNECT_ACK: "connect-ack", // { presentationId: <string> } + DISCONNECT: "disconnect", // { reason: <int> } + // presentation session life cycle + LAUNCH: "launch", // { presentationId: <string>, url: <string> } + LAUNCH_ACK: "launch-ack", // { presentationId: <string> } + TERMINATE: "terminate", // { presentationId: <string> } + TERMINATE_ACK: "terminate-ack", // { presentationId: <string> } + RECONNECT: "reconnect", // { presentationId: <string> } + RECONNECT_ACK: "reconnect-ack", // { presentationId: <string> } + // session transport establishment + OFFER: "offer", // { offer: <json> } + ANSWER: "answer", // { answer: <json> } + ICE_CANDIDATE: "ice-candidate", // { candidate: <string> } +}); + +this.State = State; +this.CommandType = CommandType; diff --git a/dom/presentation/provider/components.conf b/dom/presentation/provider/components.conf new file mode 100644 index 0000000000..04cb28ec75 --- /dev/null +++ b/dom/presentation/provider/components.conf @@ -0,0 +1,26 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +categories = {} + +if buildconfig.substs['MOZ_WIDGET_TOOLKIT'] in ('cocoa', 'android'): + categories["presentation-device-provider"] = "MulticastDNSDeviceProvider" + +Classes = [ + { + 'cid': '{f4079b8b-ede5-4b90-a112-5b415a931deb}', + 'contract_ids': ['@mozilla.org/presentation/control-service;1'], + 'jsm': 'resource://gre/modules/PresentationControlService.jsm', + 'constructor': 'PresentationControlService', + }, + { + 'cid': '{814f947a-52f7-41c9-94a1-3684797284ac}', + 'contract_ids': ['@mozilla.org/presentation-device/multicastdns-provider;1'], + 'type': 'mozilla::dom::presentation::MulticastDNSDeviceProvider', + 'headers': ['/dom/presentation/provider/MulticastDNSDeviceProvider.h'], + 'categories': categories, + }, +] diff --git a/dom/presentation/provider/moz.build b/dom/presentation/provider/moz.build new file mode 100644 index 0000000000..f6c4527d2c --- /dev/null +++ b/dom/presentation/provider/moz.build @@ -0,0 +1,29 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +EXTRA_JS_MODULES += ["PresentationControlService.jsm"] + +UNIFIED_SOURCES += [ + "DeviceProviderHelpers.cpp", + "MulticastDNSDeviceProvider.cpp", +] + +XPCOM_MANIFESTS += [ + "components.conf", +] + +XPCOM_MANIFESTS += [ + "components.conf", +] + +EXTRA_JS_MODULES.presentation += [ + "ControllerStateMachine.jsm", + "ReceiverStateMachine.jsm", + "StateMachineHelper.jsm", +] + +include("/ipc/chromium/chromium-config.mozbuild") +FINAL_LIBRARY = "xul" diff --git a/dom/presentation/provider/nsTCPDeviceInfo.h b/dom/presentation/provider/nsTCPDeviceInfo.h new file mode 100644 index 0000000000..ef646f0bf7 --- /dev/null +++ b/dom/presentation/provider/nsTCPDeviceInfo.h @@ -0,0 +1,67 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* 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/. */ + +#ifndef __TCPDeviceInfo_h__ +#define __TCPDeviceInfo_h__ + +namespace mozilla { +namespace dom { +namespace presentation { + +class TCPDeviceInfo final : public nsITCPDeviceInfo { + public: + NS_DECL_ISUPPORTS + NS_DECL_NSITCPDEVICEINFO + + explicit TCPDeviceInfo(const nsACString& aId, const nsACString& aAddress, + const uint16_t aPort, + const nsACString& aCertFingerprint) + : mId(aId), + mAddress(aAddress), + mPort(aPort), + mCertFingerprint(aCertFingerprint) {} + + private: + virtual ~TCPDeviceInfo() = default; + + nsCString mId; + nsCString mAddress; + uint16_t mPort; + nsCString mCertFingerprint; +}; + +NS_IMPL_ISUPPORTS(TCPDeviceInfo, nsITCPDeviceInfo) + +// nsITCPDeviceInfo +NS_IMETHODIMP +TCPDeviceInfo::GetId(nsACString& aId) { + aId = mId; + return NS_OK; +} + +NS_IMETHODIMP +TCPDeviceInfo::GetAddress(nsACString& aAddress) { + aAddress = mAddress; + return NS_OK; +} + +NS_IMETHODIMP +TCPDeviceInfo::GetPort(uint16_t* aPort) { + *aPort = mPort; + return NS_OK; +} + +NS_IMETHODIMP +TCPDeviceInfo::GetCertFingerprint(nsACString& aCertFingerprint) { + aCertFingerprint = mCertFingerprint; + return NS_OK; +} + +} // namespace presentation +} // namespace dom +} // namespace mozilla + +#endif /* !__TCPDeviceInfo_h__ */ |