2016-06-02 09:47:00 +03:00
|
|
|
/* -*- 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 "mozilla/dom/FlyWebPublishedServer.h"
|
|
|
|
#include "mozilla/dom/FlyWebPublishBinding.h"
|
|
|
|
#include "mozilla/dom/FlyWebService.h"
|
|
|
|
#include "mozilla/dom/Request.h"
|
|
|
|
#include "mozilla/dom/FlyWebServerEvents.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "nsGlobalWindow.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
static LazyLogModule gFlyWebPublishedServerLog("FlyWebPublishedServer");
|
|
|
|
#undef LOG_I
|
|
|
|
#define LOG_I(...) MOZ_LOG(mozilla::dom::gFlyWebPublishedServerLog, mozilla::LogLevel::Debug, (__VA_ARGS__))
|
|
|
|
#undef LOG_E
|
|
|
|
#define LOG_E(...) MOZ_LOG(mozilla::dom::gFlyWebPublishedServerLog, mozilla::LogLevel::Error, (__VA_ARGS__))
|
|
|
|
|
2016-06-07 13:10:29 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(FlyWebPublishedServer, mozilla::DOMEventTargetHelper)
|
2016-06-02 09:47:00 +03:00
|
|
|
|
|
|
|
FlyWebPublishedServer::FlyWebPublishedServer(nsPIDOMWindowInner* aOwner,
|
|
|
|
const nsAString& aName,
|
2016-06-07 13:10:29 +03:00
|
|
|
const FlyWebPublishOptions& aOptions,
|
|
|
|
Promise* aPublishPromise)
|
2016-06-02 09:47:00 +03:00
|
|
|
: mozilla::DOMEventTargetHelper(aOwner)
|
|
|
|
, mOwnerWindowID(aOwner ? aOwner->WindowID() : 0)
|
2016-06-07 13:10:29 +03:00
|
|
|
, mPublishPromise(aPublishPromise)
|
2016-06-02 09:47:00 +03:00
|
|
|
, mName(aName)
|
|
|
|
, mUiUrl(aOptions.mUiUrl)
|
|
|
|
, mIsRegistered(true) // Registered by the FlyWebService
|
|
|
|
{
|
2016-06-07 13:10:29 +03:00
|
|
|
mHttpServer = new HttpServer();
|
|
|
|
mHttpServer->Init(-1, Preferences::GetBool("flyweb.use-tls", false), this);
|
2016-06-02 09:47:00 +03:00
|
|
|
}
|
|
|
|
|
2016-06-07 13:10:29 +03:00
|
|
|
FlyWebPublishedServer::~FlyWebPublishedServer()
|
2016-06-02 09:47:00 +03:00
|
|
|
{
|
2016-06-07 13:10:29 +03:00
|
|
|
// Make sure to unregister to avoid dangling pointers
|
2016-06-02 09:47:00 +03:00
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
FlyWebPublishedServer::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
|
|
|
{
|
|
|
|
return FlyWebPublishedServerBinding::Wrap(aCx, this, aGivenProto);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FlyWebPublishedServer::Close()
|
|
|
|
{
|
|
|
|
// Unregister from server.
|
|
|
|
if (mIsRegistered) {
|
2016-06-07 13:10:29 +03:00
|
|
|
FlyWebService::GetOrCreate()->UnregisterServer(this);
|
2016-06-02 09:47:00 +03:00
|
|
|
mIsRegistered = false;
|
2016-06-07 13:10:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mMDNSCancelRegister) {
|
|
|
|
mMDNSCancelRegister->Cancel(NS_BINDING_ABORTED);
|
|
|
|
mMDNSCancelRegister = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mHttpServer) {
|
|
|
|
RefPtr<HttpServer> server = mHttpServer.forget();
|
|
|
|
server->Close();
|
|
|
|
}
|
|
|
|
}
|
2016-06-02 09:47:00 +03:00
|
|
|
|
2016-06-07 13:10:29 +03:00
|
|
|
void
|
|
|
|
FlyWebPublishedServer::OnServerStarted(nsresult aStatus)
|
|
|
|
{
|
|
|
|
if (NS_SUCCEEDED(aStatus)) {
|
|
|
|
FlyWebService::GetOrCreate()->StartDiscoveryOf(this);
|
|
|
|
} else {
|
|
|
|
DiscoveryStarted(aStatus);
|
2016-06-02 09:47:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-06-07 13:10:29 +03:00
|
|
|
FlyWebPublishedServer::OnServerClose()
|
|
|
|
{
|
|
|
|
mHttpServer = nullptr;
|
|
|
|
Close();
|
|
|
|
|
|
|
|
DispatchTrustedEvent(NS_LITERAL_STRING("close"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FlyWebPublishedServer::OnRequest(InternalRequest* aRequest)
|
2016-06-02 09:47:00 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetOwner());
|
|
|
|
RefPtr<FlyWebFetchEvent> e = new FlyWebFetchEvent(this,
|
|
|
|
new Request(global, aRequest),
|
|
|
|
aRequest);
|
|
|
|
e->Init(this);
|
|
|
|
e->InitEvent(NS_LITERAL_STRING("fetch"), false, false);
|
|
|
|
|
|
|
|
DispatchTrustedEvent(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-06-07 13:10:29 +03:00
|
|
|
FlyWebPublishedServer::OnWebSocket(InternalRequest* aConnectRequest)
|
2016-06-02 09:47:00 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetOwner());
|
|
|
|
RefPtr<FlyWebFetchEvent> e = new FlyWebWebSocketEvent(this,
|
|
|
|
new Request(global, aConnectRequest),
|
|
|
|
aConnectRequest);
|
|
|
|
e->Init(this);
|
|
|
|
e->InitEvent(NS_LITERAL_STRING("websocket"), false, false);
|
|
|
|
|
|
|
|
DispatchTrustedEvent(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-06-07 13:10:29 +03:00
|
|
|
FlyWebPublishedServer::OnFetchResponse(InternalRequest* aRequest,
|
|
|
|
InternalResponse* aResponse)
|
2016-06-02 09:47:00 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aRequest);
|
|
|
|
MOZ_ASSERT(aResponse);
|
|
|
|
|
2016-06-07 13:10:29 +03:00
|
|
|
LOG_I("FlyWebPublishedMDNSServer::OnFetchResponse(%p)", this);
|
2016-06-02 09:47:00 +03:00
|
|
|
|
|
|
|
if (mHttpServer) {
|
|
|
|
mHttpServer->SendResponse(aRequest, aResponse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-07 13:10:25 +03:00
|
|
|
already_AddRefed<WebSocket>
|
2016-06-07 13:10:29 +03:00
|
|
|
FlyWebPublishedServer::OnWebSocketAccept(InternalRequest* aConnectRequest,
|
|
|
|
const Optional<nsAString>& aProtocol,
|
|
|
|
ErrorResult& aRv)
|
2016-06-07 13:10:25 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aConnectRequest);
|
|
|
|
|
|
|
|
LOG_I("FlyWebPublishedMDNSServer::OnWebSocketAccept(%p)", this);
|
|
|
|
|
|
|
|
if (!mHttpServer) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString negotiatedExtensions;
|
|
|
|
nsCOMPtr<nsITransportProvider> provider =
|
|
|
|
mHttpServer->AcceptWebSocket(aConnectRequest,
|
|
|
|
aProtocol,
|
|
|
|
negotiatedExtensions,
|
|
|
|
aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(provider);
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(GetOwner());
|
|
|
|
AutoJSContext cx;
|
|
|
|
GlobalObject global(cx, nsGlobalWindow::Cast(window)->FastGetGlobalJSObject());
|
|
|
|
|
|
|
|
nsCString url;
|
|
|
|
aConnectRequest->GetURL(url);
|
|
|
|
Sequence<nsString> protocols;
|
|
|
|
if (aProtocol.WasPassed() &&
|
|
|
|
!protocols.AppendElement(aProtocol.Value(), fallible)) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return WebSocket::ConstructorCommon(global,
|
|
|
|
NS_ConvertUTF8toUTF16(url),
|
|
|
|
protocols,
|
|
|
|
provider,
|
|
|
|
negotiatedExtensions,
|
|
|
|
aRv);
|
|
|
|
}
|
|
|
|
|
2016-06-02 09:47:00 +03:00
|
|
|
void
|
2016-06-07 13:10:29 +03:00
|
|
|
FlyWebPublishedServer::OnWebSocketResponse(InternalRequest* aConnectRequest,
|
|
|
|
InternalResponse* aResponse)
|
2016-06-02 09:47:00 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aConnectRequest);
|
|
|
|
MOZ_ASSERT(aResponse);
|
|
|
|
|
|
|
|
LOG_I("FlyWebPublishedMDNSServer::OnWebSocketResponse(%p)", this);
|
|
|
|
|
|
|
|
if (mHttpServer) {
|
|
|
|
mHttpServer->SendWebSocketResponse(aConnectRequest, aResponse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-07 13:10:29 +03:00
|
|
|
void FlyWebPublishedServer::DiscoveryStarted(nsresult aStatus)
|
2016-06-07 12:46:03 +03:00
|
|
|
{
|
2016-06-07 13:10:29 +03:00
|
|
|
if (NS_SUCCEEDED(aStatus)) {
|
|
|
|
mPublishPromise->MaybeResolve(this);
|
|
|
|
} else {
|
|
|
|
Close();
|
|
|
|
mPublishPromise->MaybeReject(aStatus);
|
2016-06-02 09:47:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
|