2012-12-10 18:13:55 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
2013-08-14 10:56:21 +04:00
|
|
|
#include "mozilla/dom/NetDashboardBinding.h"
|
2014-06-11 23:38:55 +04:00
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
2014-10-22 10:55:00 +04:00
|
|
|
#include "mozilla/ErrorNames.h"
|
2013-09-06 00:12:38 +04:00
|
|
|
#include "mozilla/net/Dashboard.h"
|
2013-08-14 10:56:21 +04:00
|
|
|
#include "mozilla/net/HttpInfo.h"
|
2013-09-06 00:12:38 +04:00
|
|
|
#include "nsHttp.h"
|
2013-09-22 07:04:57 +04:00
|
|
|
#include "nsICancelable.h"
|
2013-09-06 00:12:38 +04:00
|
|
|
#include "nsIDNSService.h"
|
2013-09-22 07:04:57 +04:00
|
|
|
#include "nsIDNSRecord.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsISocketTransport.h"
|
2013-09-06 00:12:38 +04:00
|
|
|
#include "nsIThread.h"
|
2014-04-24 00:45:10 +04:00
|
|
|
#include "nsProxyRelease.h"
|
2013-09-22 07:04:57 +04:00
|
|
|
#include "nsSocketTransportService2.h"
|
2013-09-19 17:54:39 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2013-09-23 07:35:52 +04:00
|
|
|
#include "nsURLHelper.h"
|
2012-12-10 18:13:55 +04:00
|
|
|
|
2013-05-22 20:05:28 +04:00
|
|
|
using mozilla::AutoSafeJSContext;
|
2014-03-11 20:59:50 +04:00
|
|
|
using mozilla::dom::Sequence;
|
2014-06-11 23:38:55 +04:00
|
|
|
using mozilla::dom::ToJSValue;
|
2014-03-11 20:59:50 +04:00
|
|
|
|
2012-12-10 18:13:55 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
class SocketData
|
|
|
|
: public nsISupports
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
|
|
|
SocketData()
|
|
|
|
{
|
|
|
|
mTotalSent = 0;
|
|
|
|
mTotalRecv = 0;
|
|
|
|
mThread = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t mTotalSent;
|
|
|
|
uint64_t mTotalRecv;
|
|
|
|
nsTArray<SocketInfo> mData;
|
2014-04-24 00:45:10 +04:00
|
|
|
nsMainThreadPtrHandle<NetDashboardCallback> mCallback;
|
2014-03-11 20:59:50 +04:00
|
|
|
nsIThread *mThread;
|
2014-06-24 20:36:44 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~SocketData()
|
|
|
|
{
|
|
|
|
}
|
2014-03-11 20:59:50 +04:00
|
|
|
};
|
|
|
|
|
2014-10-22 10:55:00 +04:00
|
|
|
static void GetErrorString(nsresult rv, nsAString& errorString);
|
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
NS_IMPL_ISUPPORTS0(SocketData)
|
|
|
|
|
|
|
|
|
|
|
|
class HttpData
|
|
|
|
: public nsISupports
|
2013-09-06 00:12:38 +04:00
|
|
|
{
|
2014-06-24 20:36:44 +04:00
|
|
|
virtual ~HttpData()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
|
|
|
HttpData()
|
|
|
|
{
|
|
|
|
mThread = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<HttpRetParams> mData;
|
2014-04-24 00:45:10 +04:00
|
|
|
nsMainThreadPtrHandle<NetDashboardCallback> mCallback;
|
2014-03-11 20:59:50 +04:00
|
|
|
nsIThread *mThread;
|
2013-09-06 00:12:38 +04:00
|
|
|
};
|
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
NS_IMPL_ISUPPORTS0(HttpData)
|
|
|
|
|
|
|
|
|
|
|
|
class WebSocketRequest
|
|
|
|
: public nsISupports
|
2012-12-10 18:13:55 +04:00
|
|
|
{
|
2014-06-24 20:36:44 +04:00
|
|
|
virtual ~WebSocketRequest()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
|
|
|
WebSocketRequest()
|
|
|
|
{
|
|
|
|
mThread = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-04-24 00:45:10 +04:00
|
|
|
nsMainThreadPtrHandle<NetDashboardCallback> mCallback;
|
2014-03-11 20:59:50 +04:00
|
|
|
nsIThread *mThread;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS0(WebSocketRequest)
|
|
|
|
|
|
|
|
|
|
|
|
class DnsData
|
|
|
|
: public nsISupports
|
|
|
|
{
|
2014-06-24 20:36:44 +04:00
|
|
|
virtual ~DnsData()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
|
|
|
DnsData()
|
|
|
|
{
|
|
|
|
mThread = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<DNSCacheEntries> mData;
|
2014-04-24 00:45:10 +04:00
|
|
|
nsMainThreadPtrHandle<NetDashboardCallback> mCallback;
|
2014-03-11 20:59:50 +04:00
|
|
|
nsIThread *mThread;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS0(DnsData)
|
|
|
|
|
|
|
|
|
|
|
|
class ConnectionData
|
|
|
|
: public nsITransportEventSink
|
|
|
|
, public nsITimerCallback
|
|
|
|
{
|
2014-06-24 20:36:44 +04:00
|
|
|
virtual ~ConnectionData()
|
|
|
|
{
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSITRANSPORTEVENTSINK
|
|
|
|
NS_DECL_NSITIMERCALLBACK
|
|
|
|
|
|
|
|
void StartTimer(uint32_t aTimeout);
|
|
|
|
void StopTimer();
|
|
|
|
|
2014-08-05 17:20:50 +04:00
|
|
|
explicit ConnectionData(Dashboard *target)
|
2014-03-11 20:59:50 +04:00
|
|
|
{
|
|
|
|
mThread = nullptr;
|
|
|
|
mDashboard = target;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISocketTransport> mSocket;
|
|
|
|
nsCOMPtr<nsIInputStream> mStreamIn;
|
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
2014-04-24 00:45:10 +04:00
|
|
|
nsMainThreadPtrHandle<NetDashboardCallback> mCallback;
|
2014-03-11 20:59:50 +04:00
|
|
|
nsIThread *mThread;
|
|
|
|
Dashboard *mDashboard;
|
|
|
|
|
|
|
|
nsCString mHost;
|
|
|
|
uint32_t mPort;
|
|
|
|
const char *mProtocol;
|
|
|
|
uint32_t mTimeout;
|
|
|
|
|
|
|
|
nsString mStatus;
|
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(ConnectionData, nsITransportEventSink, nsITimerCallback)
|
2014-03-11 20:59:50 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ConnectionData::OnTransportStatus(nsITransport *aTransport, nsresult aStatus,
|
2015-01-08 22:48:52 +03:00
|
|
|
int64_t aProgress, int64_t aProgressMax)
|
2014-03-11 20:59:50 +04:00
|
|
|
{
|
|
|
|
if (aStatus == NS_NET_STATUS_CONNECTED_TO) {
|
|
|
|
StopTimer();
|
|
|
|
}
|
|
|
|
|
2014-10-22 10:55:00 +04:00
|
|
|
GetErrorString(aStatus, mStatus);
|
2014-03-11 20:59:50 +04:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethodWithArg<nsRefPtr<ConnectionData> >
|
|
|
|
(mDashboard, &Dashboard::GetConnectionStatus, this);
|
|
|
|
mThread->Dispatch(event, NS_DISPATCH_NORMAL);
|
|
|
|
|
|
|
|
return NS_OK;
|
2012-12-10 18:13:55 +04:00
|
|
|
}
|
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
ConnectionData::Notify(nsITimer *aTimer)
|
2012-12-10 18:13:55 +04:00
|
|
|
{
|
2014-03-11 20:59:50 +04:00
|
|
|
MOZ_ASSERT(aTimer == mTimer);
|
|
|
|
|
|
|
|
if (mSocket) {
|
|
|
|
mSocket->Close(NS_ERROR_ABORT);
|
|
|
|
mSocket = nullptr;
|
|
|
|
mStreamIn = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTimer = nullptr;
|
|
|
|
|
2014-05-26 22:54:10 +04:00
|
|
|
mStatus.AssignLiteral(MOZ_UTF16("NS_ERROR_NET_TIMEOUT"));
|
2014-03-11 20:59:50 +04:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethodWithArg<nsRefPtr<ConnectionData> >
|
|
|
|
(mDashboard, &Dashboard::GetConnectionStatus, this);
|
|
|
|
mThread->Dispatch(event, NS_DISPATCH_NORMAL);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ConnectionData::StartTimer(uint32_t aTimeout)
|
|
|
|
{
|
|
|
|
if (!mTimer) {
|
|
|
|
mTimer = do_CreateInstance("@mozilla.org/timer;1");
|
|
|
|
}
|
|
|
|
|
|
|
|
mTimer->InitWithCallback(this, aTimeout * 1000,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
2012-12-10 18:13:55 +04:00
|
|
|
}
|
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
void
|
|
|
|
ConnectionData::StopTimer()
|
|
|
|
{
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
mTimer = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class LookupHelper;
|
|
|
|
|
|
|
|
class LookupArgument
|
|
|
|
: public nsISupports
|
|
|
|
{
|
2014-06-24 20:36:44 +04:00
|
|
|
virtual ~LookupArgument()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
|
|
|
|
LookupArgument(nsIDNSRecord *aRecord, LookupHelper *aHelper)
|
|
|
|
{
|
|
|
|
mRecord = aRecord;
|
|
|
|
mHelper = aHelper;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDNSRecord> mRecord;
|
|
|
|
nsRefPtr<LookupHelper> mHelper;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS0(LookupArgument)
|
|
|
|
|
|
|
|
|
|
|
|
class LookupHelper
|
|
|
|
: public nsIDNSListener
|
|
|
|
{
|
|
|
|
virtual ~LookupHelper()
|
|
|
|
{
|
|
|
|
if (mCancel) {
|
|
|
|
mCancel->Cancel(NS_ERROR_ABORT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-24 20:36:44 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIDNSLISTENER
|
|
|
|
|
|
|
|
LookupHelper() {
|
|
|
|
}
|
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
nsresult ConstructAnswer(LookupArgument *aArgument);
|
|
|
|
public:
|
|
|
|
nsCOMPtr<nsICancelable> mCancel;
|
2014-04-24 00:45:10 +04:00
|
|
|
nsMainThreadPtrHandle<NetDashboardCallback> mCallback;
|
2014-03-11 20:59:50 +04:00
|
|
|
nsIThread *mThread;
|
|
|
|
nsresult mStatus;
|
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(LookupHelper, nsIDNSListener)
|
2014-03-11 20:59:50 +04:00
|
|
|
|
2012-12-10 18:13:55 +04:00
|
|
|
NS_IMETHODIMP
|
2014-03-11 20:59:50 +04:00
|
|
|
LookupHelper::OnLookupComplete(nsICancelable *aRequest,
|
|
|
|
nsIDNSRecord *aRecord, nsresult aStatus)
|
2012-12-10 18:13:55 +04:00
|
|
|
{
|
2014-03-11 20:59:50 +04:00
|
|
|
MOZ_ASSERT(aRequest == mCancel);
|
|
|
|
mCancel = nullptr;
|
|
|
|
mStatus = aStatus;
|
|
|
|
|
|
|
|
nsRefPtr<LookupArgument> arg = new LookupArgument(aRecord, this);
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethodWithArg<nsRefPtr<LookupArgument> >(
|
|
|
|
this, &LookupHelper::ConstructAnswer, arg);
|
|
|
|
mThread->Dispatch(event, NS_DISPATCH_NORMAL);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
LookupHelper::ConstructAnswer(LookupArgument *aArgument)
|
|
|
|
{
|
|
|
|
nsIDNSRecord *aRecord = aArgument->mRecord;
|
|
|
|
AutoSafeJSContext cx;
|
|
|
|
|
|
|
|
mozilla::dom::DNSLookupDict dict;
|
|
|
|
dict.mAddress.Construct();
|
|
|
|
|
|
|
|
Sequence<nsString> &addresses = dict.mAddress.Value();
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(mStatus)) {
|
|
|
|
dict.mAnswer = true;
|
|
|
|
bool hasMore;
|
|
|
|
aRecord->HasMore(&hasMore);
|
|
|
|
while (hasMore) {
|
2015-05-28 21:07:44 +03:00
|
|
|
nsString* nextAddress = addresses.AppendElement(fallible);
|
2015-05-28 03:46:19 +03:00
|
|
|
if (!nextAddress) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString nextAddressASCII;
|
|
|
|
aRecord->GetNextAddrAsString(nextAddressASCII);
|
|
|
|
CopyASCIItoUTF16(nextAddressASCII, *nextAddress);
|
|
|
|
aRecord->HasMore(&hasMore);
|
2014-03-11 20:59:50 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dict.mAnswer = false;
|
2014-10-22 10:55:00 +04:00
|
|
|
GetErrorString(mStatus, dict.mError);
|
2014-03-11 20:59:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JS::RootedValue val(cx);
|
2014-06-11 23:38:55 +04:00
|
|
|
if (!ToJSValue(cx, dict, &val)) {
|
2012-12-10 18:13:55 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-03-11 20:59:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
this->mCallback->OnDashboardDataAvailable(val);
|
2012-12-10 18:13:55 +04:00
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(Dashboard, nsIDashboard, nsIDashboardEventNotifier)
|
2014-03-11 20:59:50 +04:00
|
|
|
|
|
|
|
Dashboard::Dashboard()
|
|
|
|
{
|
|
|
|
mEnableLogging = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Dashboard::~Dashboard()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Dashboard::RequestSockets(NetDashboardCallback *aCallback)
|
|
|
|
{
|
|
|
|
nsRefPtr<SocketData> socketData = new SocketData();
|
2014-04-24 00:45:10 +04:00
|
|
|
socketData->mCallback =
|
|
|
|
new nsMainThreadPtrHolder<NetDashboardCallback>(aCallback, true);
|
2014-03-11 20:59:50 +04:00
|
|
|
socketData->mThread = NS_GetCurrentThread();
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethodWithArg<nsRefPtr<SocketData> >
|
|
|
|
(this, &Dashboard::GetSocketsDispatch, socketData);
|
2012-12-10 18:13:55 +04:00
|
|
|
gSocketTransportService->Dispatch(event, NS_DISPATCH_NORMAL);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
nsresult
|
|
|
|
Dashboard::GetSocketsDispatch(SocketData *aSocketData)
|
2012-12-10 18:13:55 +04:00
|
|
|
{
|
2014-03-11 20:59:50 +04:00
|
|
|
nsRefPtr<SocketData> socketData = aSocketData;
|
2013-07-29 20:08:03 +04:00
|
|
|
if (gSocketTransportService) {
|
2014-03-11 20:59:50 +04:00
|
|
|
gSocketTransportService->GetSocketConnections(&socketData->mData);
|
|
|
|
socketData->mTotalSent = gSocketTransportService->GetSentBytes();
|
|
|
|
socketData->mTotalRecv = gSocketTransportService->GetReceivedBytes();
|
2013-07-29 20:08:03 +04:00
|
|
|
}
|
2014-03-11 20:59:50 +04:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethodWithArg<nsRefPtr<SocketData> >
|
|
|
|
(this, &Dashboard::GetSockets, socketData);
|
|
|
|
socketData->mThread->Dispatch(event, NS_DISPATCH_NORMAL);
|
|
|
|
return NS_OK;
|
2012-12-10 18:13:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-03-11 20:59:50 +04:00
|
|
|
Dashboard::GetSockets(SocketData *aSocketData)
|
2012-12-10 18:13:55 +04:00
|
|
|
{
|
2014-03-11 20:59:50 +04:00
|
|
|
nsRefPtr<SocketData> socketData = aSocketData;
|
2013-05-22 20:05:28 +04:00
|
|
|
AutoSafeJSContext cx;
|
2012-12-10 18:13:55 +04:00
|
|
|
|
2013-04-15 05:32:06 +04:00
|
|
|
mozilla::dom::SocketsDict dict;
|
2013-11-20 18:29:01 +04:00
|
|
|
dict.mSockets.Construct();
|
2013-04-15 05:32:06 +04:00
|
|
|
dict.mSent = 0;
|
|
|
|
dict.mReceived = 0;
|
|
|
|
|
2013-11-20 18:29:01 +04:00
|
|
|
Sequence<mozilla::dom::SocketElement> &sockets = dict.mSockets.Value();
|
2013-04-15 05:32:06 +04:00
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
uint32_t length = socketData->mData.Length();
|
2015-05-18 23:50:34 +03:00
|
|
|
if (!sockets.SetCapacity(length, fallible)) {
|
2013-04-15 05:32:06 +04:00
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2012-12-10 18:13:55 +04:00
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
for (uint32_t i = 0; i < socketData->mData.Length(); i++) {
|
2015-05-28 21:07:44 +03:00
|
|
|
dom::SocketElement &mSocket = *sockets.AppendElement(fallible);
|
2014-03-11 20:59:50 +04:00
|
|
|
CopyASCIItoUTF16(socketData->mData[i].host, mSocket.mHost);
|
|
|
|
mSocket.mPort = socketData->mData[i].port;
|
|
|
|
mSocket.mActive = socketData->mData[i].active;
|
|
|
|
mSocket.mTcp = socketData->mData[i].tcp;
|
|
|
|
mSocket.mSent = (double) socketData->mData[i].sent;
|
|
|
|
mSocket.mReceived = (double) socketData->mData[i].received;
|
|
|
|
dict.mSent += socketData->mData[i].sent;
|
|
|
|
dict.mReceived += socketData->mData[i].received;
|
2012-12-10 18:13:55 +04:00
|
|
|
}
|
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
dict.mSent += socketData->mTotalSent;
|
|
|
|
dict.mReceived += socketData->mTotalRecv;
|
2013-05-12 09:09:22 +04:00
|
|
|
JS::RootedValue val(cx);
|
2014-06-11 23:38:55 +04:00
|
|
|
if (!ToJSValue(cx, dict, &val))
|
2013-04-15 05:32:06 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-03-11 20:59:50 +04:00
|
|
|
socketData->mCallback->OnDashboardDataAvailable(val);
|
2012-12-10 18:13:55 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-03-11 20:59:50 +04:00
|
|
|
Dashboard::RequestHttpConnections(NetDashboardCallback *aCallback)
|
2012-12-10 18:13:55 +04:00
|
|
|
{
|
2014-03-11 20:59:50 +04:00
|
|
|
nsRefPtr<HttpData> httpData = new HttpData();
|
2014-04-24 00:45:10 +04:00
|
|
|
httpData->mCallback =
|
|
|
|
new nsMainThreadPtrHolder<NetDashboardCallback>(aCallback, true);
|
2014-03-11 20:59:50 +04:00
|
|
|
httpData->mThread = NS_GetCurrentThread();
|
2012-12-10 18:13:55 +04:00
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethodWithArg<nsRefPtr<HttpData> >
|
|
|
|
(this, &Dashboard::GetHttpDispatch, httpData);
|
2012-12-10 18:13:55 +04:00
|
|
|
gSocketTransportService->Dispatch(event, NS_DISPATCH_NORMAL);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
nsresult
|
|
|
|
Dashboard::GetHttpDispatch(HttpData *aHttpData)
|
|
|
|
{
|
|
|
|
nsRefPtr<HttpData> httpData = aHttpData;
|
|
|
|
HttpInfo::GetHttpConnectionData(&httpData->mData);
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethodWithArg<nsRefPtr<HttpData> >
|
|
|
|
(this, &Dashboard::GetHttpConnections, httpData);
|
|
|
|
httpData->mThread->Dispatch(event, NS_DISPATCH_NORMAL);
|
|
|
|
return NS_OK;
|
2012-12-10 18:13:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2014-03-11 20:59:50 +04:00
|
|
|
Dashboard::GetHttpConnections(HttpData *aHttpData)
|
2012-12-10 18:13:55 +04:00
|
|
|
{
|
2014-03-11 20:59:50 +04:00
|
|
|
nsRefPtr<HttpData> httpData = aHttpData;
|
2013-05-22 20:05:28 +04:00
|
|
|
AutoSafeJSContext cx;
|
2012-12-10 18:13:55 +04:00
|
|
|
|
2013-04-15 05:32:06 +04:00
|
|
|
mozilla::dom::HttpConnDict dict;
|
2013-11-20 18:29:01 +04:00
|
|
|
dict.mConnections.Construct();
|
|
|
|
|
2013-07-23 18:52:01 +04:00
|
|
|
using mozilla::dom::HalfOpenInfoDict;
|
2013-11-20 18:29:01 +04:00
|
|
|
using mozilla::dom::HttpConnectionElement;
|
|
|
|
using mozilla::dom::HttpConnInfo;
|
|
|
|
Sequence<HttpConnectionElement> &connections = dict.mConnections.Value();
|
2013-04-15 05:32:06 +04:00
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
uint32_t length = httpData->mData.Length();
|
2015-05-18 23:50:34 +03:00
|
|
|
if (!connections.SetCapacity(length, fallible)) {
|
2013-04-15 05:32:06 +04:00
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2012-12-10 18:13:55 +04:00
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
for (uint32_t i = 0; i < httpData->mData.Length(); i++) {
|
2015-05-28 21:07:44 +03:00
|
|
|
HttpConnectionElement &connection = *connections.AppendElement(fallible);
|
2013-11-20 18:29:01 +04:00
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
CopyASCIItoUTF16(httpData->mData[i].host, connection.mHost);
|
|
|
|
connection.mPort = httpData->mData[i].port;
|
|
|
|
connection.mSpdy = httpData->mData[i].spdy;
|
|
|
|
connection.mSsl = httpData->mData[i].ssl;
|
2013-11-20 18:29:01 +04:00
|
|
|
|
|
|
|
connection.mActive.Construct();
|
|
|
|
connection.mIdle.Construct();
|
|
|
|
connection.mHalfOpens.Construct();
|
|
|
|
|
|
|
|
Sequence<HttpConnInfo> &active = connection.mActive.Value();
|
|
|
|
Sequence<HttpConnInfo> &idle = connection.mIdle.Value();
|
|
|
|
Sequence<HalfOpenInfoDict> &halfOpens = connection.mHalfOpens.Value();
|
|
|
|
|
2015-05-18 23:50:34 +03:00
|
|
|
if (!active.SetCapacity(httpData->mData[i].active.Length(), fallible) ||
|
|
|
|
!idle.SetCapacity(httpData->mData[i].idle.Length(), fallible) ||
|
|
|
|
!halfOpens.SetCapacity(httpData->mData[i].halfOpens.Length(),
|
|
|
|
fallible)) {
|
2013-04-15 05:32:06 +04:00
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2013-11-20 18:29:01 +04:00
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
for (uint32_t j = 0; j < httpData->mData[i].active.Length(); j++) {
|
2015-05-28 21:07:44 +03:00
|
|
|
HttpConnInfo &info = *active.AppendElement(fallible);
|
2014-03-11 20:59:50 +04:00
|
|
|
info.mRtt = httpData->mData[i].active[j].rtt;
|
|
|
|
info.mTtl = httpData->mData[i].active[j].ttl;
|
|
|
|
info.mProtocolVersion =
|
|
|
|
httpData->mData[i].active[j].protocolVersion;
|
2012-12-10 18:13:55 +04:00
|
|
|
}
|
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
for (uint32_t j = 0; j < httpData->mData[i].idle.Length(); j++) {
|
2015-05-28 21:07:44 +03:00
|
|
|
HttpConnInfo &info = *idle.AppendElement(fallible);
|
2014-03-11 20:59:50 +04:00
|
|
|
info.mRtt = httpData->mData[i].idle[j].rtt;
|
|
|
|
info.mTtl = httpData->mData[i].idle[j].ttl;
|
|
|
|
info.mProtocolVersion = httpData->mData[i].idle[j].protocolVersion;
|
2012-12-10 18:13:55 +04:00
|
|
|
}
|
2013-07-23 18:52:01 +04:00
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
for (uint32_t j = 0; j < httpData->mData[i].halfOpens.Length(); j++) {
|
2015-05-28 21:07:44 +03:00
|
|
|
HalfOpenInfoDict &info = *halfOpens.AppendElement(fallible);
|
2014-03-11 20:59:50 +04:00
|
|
|
info.mSpeculative = httpData->mData[i].halfOpens[j].speculative;
|
2013-07-23 18:52:01 +04:00
|
|
|
}
|
2012-12-10 18:13:55 +04:00
|
|
|
}
|
|
|
|
|
2013-05-12 09:09:22 +04:00
|
|
|
JS::RootedValue val(cx);
|
2014-06-11 23:38:55 +04:00
|
|
|
if (!ToJSValue(cx, dict, &val)) {
|
2013-04-15 05:32:06 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-03-11 20:59:50 +04:00
|
|
|
|
|
|
|
httpData->mCallback->OnDashboardDataAvailable(val);
|
2012-12-10 18:13:55 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-03-11 20:59:50 +04:00
|
|
|
Dashboard::GetEnableLogging(bool *value)
|
2012-12-10 18:13:55 +04:00
|
|
|
{
|
|
|
|
*value = mEnableLogging;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Dashboard::SetEnableLogging(const bool value)
|
|
|
|
{
|
|
|
|
mEnableLogging = value;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Dashboard::AddHost(const nsACString& aHost, uint32_t aSerial, bool aEncrypted)
|
|
|
|
{
|
|
|
|
if (mEnableLogging) {
|
|
|
|
mozilla::MutexAutoLock lock(mWs.lock);
|
2014-03-11 20:59:50 +04:00
|
|
|
LogData mData(nsCString(aHost), aSerial, aEncrypted);
|
|
|
|
if (mWs.data.Contains(mData)) {
|
2012-12-10 18:13:55 +04:00
|
|
|
return NS_OK;
|
2014-03-11 20:59:50 +04:00
|
|
|
}
|
|
|
|
if (!mWs.data.AppendElement(mData)) {
|
2012-12-10 18:13:55 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-03-11 20:59:50 +04:00
|
|
|
}
|
2012-12-10 18:13:55 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Dashboard::RemoveHost(const nsACString& aHost, uint32_t aSerial)
|
|
|
|
{
|
|
|
|
if (mEnableLogging) {
|
|
|
|
mozilla::MutexAutoLock lock(mWs.lock);
|
|
|
|
int32_t index = mWs.IndexOf(nsCString(aHost), aSerial);
|
|
|
|
if (index == -1)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
mWs.data.RemoveElementAt(index);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Dashboard::NewMsgSent(const nsACString& aHost, uint32_t aSerial, uint32_t aLength)
|
|
|
|
{
|
|
|
|
if (mEnableLogging) {
|
|
|
|
mozilla::MutexAutoLock lock(mWs.lock);
|
|
|
|
int32_t index = mWs.IndexOf(nsCString(aHost), aSerial);
|
|
|
|
if (index == -1)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
mWs.data[index].mMsgSent++;
|
|
|
|
mWs.data[index].mSizeSent += aLength;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Dashboard::NewMsgReceived(const nsACString& aHost, uint32_t aSerial, uint32_t aLength)
|
|
|
|
{
|
|
|
|
if (mEnableLogging) {
|
|
|
|
mozilla::MutexAutoLock lock(mWs.lock);
|
|
|
|
int32_t index = mWs.IndexOf(nsCString(aHost), aSerial);
|
|
|
|
if (index == -1)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
mWs.data[index].mMsgReceived++;
|
|
|
|
mWs.data[index].mSizeReceived += aLength;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-03-11 20:59:50 +04:00
|
|
|
Dashboard::RequestWebsocketConnections(NetDashboardCallback *aCallback)
|
2012-12-10 18:13:55 +04:00
|
|
|
{
|
2014-03-11 20:59:50 +04:00
|
|
|
nsRefPtr<WebSocketRequest> wsRequest = new WebSocketRequest();
|
2014-04-24 00:45:10 +04:00
|
|
|
wsRequest->mCallback =
|
|
|
|
new nsMainThreadPtrHolder<NetDashboardCallback>(aCallback, true);
|
2014-03-11 20:59:50 +04:00
|
|
|
wsRequest->mThread = NS_GetCurrentThread();
|
2012-12-10 18:13:55 +04:00
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethodWithArg<nsRefPtr<WebSocketRequest> >
|
|
|
|
(this, &Dashboard::GetWebSocketConnections, wsRequest);
|
|
|
|
wsRequest->mThread->Dispatch(event, NS_DISPATCH_NORMAL);
|
2012-12-10 18:13:55 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-03-11 20:59:50 +04:00
|
|
|
Dashboard::GetWebSocketConnections(WebSocketRequest *aWsRequest)
|
2012-12-10 18:13:55 +04:00
|
|
|
{
|
2014-03-11 20:59:50 +04:00
|
|
|
nsRefPtr<WebSocketRequest> wsRequest = aWsRequest;
|
2013-05-22 20:05:28 +04:00
|
|
|
AutoSafeJSContext cx;
|
2012-12-10 18:13:55 +04:00
|
|
|
|
2013-04-15 05:32:06 +04:00
|
|
|
mozilla::dom::WebSocketDict dict;
|
2013-11-20 18:29:01 +04:00
|
|
|
dict.mWebsockets.Construct();
|
2014-03-11 20:59:50 +04:00
|
|
|
Sequence<mozilla::dom::WebSocketElement> &websockets =
|
|
|
|
dict.mWebsockets.Value();
|
2013-04-15 05:32:06 +04:00
|
|
|
|
2013-11-20 18:29:01 +04:00
|
|
|
mozilla::MutexAutoLock lock(mWs.lock);
|
2013-04-15 05:32:06 +04:00
|
|
|
uint32_t length = mWs.data.Length();
|
2015-05-18 23:50:34 +03:00
|
|
|
if (!websockets.SetCapacity(length, fallible)) {
|
2013-11-20 18:29:01 +04:00
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2013-04-15 05:32:06 +04:00
|
|
|
}
|
2012-12-10 18:13:55 +04:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mWs.data.Length(); i++) {
|
2015-05-28 21:07:44 +03:00
|
|
|
dom::WebSocketElement &websocket = *websockets.AppendElement(fallible);
|
2013-11-20 18:29:01 +04:00
|
|
|
CopyASCIItoUTF16(mWs.data[i].mHost, websocket.mHostport);
|
|
|
|
websocket.mMsgsent = mWs.data[i].mMsgSent;
|
|
|
|
websocket.mMsgreceived = mWs.data[i].mMsgReceived;
|
|
|
|
websocket.mSentsize = mWs.data[i].mSizeSent;
|
|
|
|
websocket.mReceivedsize = mWs.data[i].mSizeReceived;
|
|
|
|
websocket.mEncrypted = mWs.data[i].mEncrypted;
|
2012-12-10 18:13:55 +04:00
|
|
|
}
|
|
|
|
|
2013-05-12 09:09:22 +04:00
|
|
|
JS::RootedValue val(cx);
|
2014-06-11 23:38:55 +04:00
|
|
|
if (!ToJSValue(cx, dict, &val)) {
|
2013-04-15 05:32:06 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-03-11 20:59:50 +04:00
|
|
|
wsRequest->mCallback->OnDashboardDataAvailable(val);
|
2012-12-10 18:13:55 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-03-11 20:59:50 +04:00
|
|
|
Dashboard::RequestDNSInfo(NetDashboardCallback *aCallback)
|
2012-12-10 18:13:55 +04:00
|
|
|
{
|
2014-03-11 20:59:50 +04:00
|
|
|
nsRefPtr<DnsData> dnsData = new DnsData();
|
2014-04-24 00:45:10 +04:00
|
|
|
dnsData->mCallback =
|
|
|
|
new nsMainThreadPtrHolder<NetDashboardCallback>(aCallback, true);
|
2014-03-11 20:59:50 +04:00
|
|
|
|
2012-12-10 18:13:55 +04:00
|
|
|
nsresult rv;
|
2014-03-11 20:59:50 +04:00
|
|
|
dnsData->mData.Clear();
|
|
|
|
dnsData->mThread = NS_GetCurrentThread();
|
2012-12-10 18:13:55 +04:00
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
if (!mDnsService) {
|
|
|
|
mDnsService = do_GetService("@mozilla.org/network/dns-service;1", &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-12-10 18:13:55 +04:00
|
|
|
return rv;
|
2014-03-11 20:59:50 +04:00
|
|
|
}
|
2012-12-10 18:13:55 +04:00
|
|
|
}
|
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethodWithArg<nsRefPtr<DnsData> >
|
|
|
|
(this, &Dashboard::GetDnsInfoDispatch, dnsData);
|
2012-12-10 18:13:55 +04:00
|
|
|
gSocketTransportService->Dispatch(event, NS_DISPATCH_NORMAL);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
nsresult
|
|
|
|
Dashboard::GetDnsInfoDispatch(DnsData *aDnsData)
|
2012-12-10 18:13:55 +04:00
|
|
|
{
|
2014-03-11 20:59:50 +04:00
|
|
|
nsRefPtr<DnsData> dnsData = aDnsData;
|
|
|
|
if (mDnsService) {
|
|
|
|
mDnsService->GetDNSCacheEntries(&dnsData->mData);
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethodWithArg<nsRefPtr<DnsData> >
|
|
|
|
(this, &Dashboard::GetDNSCacheEntries, dnsData);
|
|
|
|
dnsData->mThread->Dispatch(event, NS_DISPATCH_NORMAL);
|
|
|
|
return NS_OK;
|
2012-12-10 18:13:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-03-11 20:59:50 +04:00
|
|
|
Dashboard::GetDNSCacheEntries(DnsData *dnsData)
|
2012-12-10 18:13:55 +04:00
|
|
|
{
|
2013-05-22 20:05:28 +04:00
|
|
|
AutoSafeJSContext cx;
|
2012-12-10 18:13:55 +04:00
|
|
|
|
2013-04-15 05:32:06 +04:00
|
|
|
mozilla::dom::DNSCacheDict dict;
|
2013-11-20 18:29:01 +04:00
|
|
|
dict.mEntries.Construct();
|
|
|
|
Sequence<mozilla::dom::DnsCacheEntry> &entries = dict.mEntries.Value();
|
2013-04-15 05:32:06 +04:00
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
uint32_t length = dnsData->mData.Length();
|
2015-05-18 23:50:34 +03:00
|
|
|
if (!entries.SetCapacity(length, fallible)) {
|
2013-11-20 18:29:01 +04:00
|
|
|
JS_ReportOutOfMemory(cx);
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2013-04-15 05:32:06 +04:00
|
|
|
}
|
2012-12-10 18:13:55 +04:00
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
for (uint32_t i = 0; i < dnsData->mData.Length(); i++) {
|
2015-05-28 21:07:44 +03:00
|
|
|
dom::DnsCacheEntry &entry = *entries.AppendElement(fallible);
|
2013-11-20 18:29:01 +04:00
|
|
|
entry.mHostaddr.Construct();
|
2013-04-15 05:32:06 +04:00
|
|
|
|
2013-11-20 18:29:01 +04:00
|
|
|
Sequence<nsString> &addrs = entry.mHostaddr.Value();
|
2015-05-18 23:50:34 +03:00
|
|
|
if (!addrs.SetCapacity(dnsData->mData[i].hostaddr.Length(), fallible)) {
|
2013-04-15 05:32:06 +04:00
|
|
|
JS_ReportOutOfMemory(cx);
|
2012-12-10 18:13:55 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2013-04-15 05:32:06 +04:00
|
|
|
}
|
2013-11-20 18:29:01 +04:00
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
CopyASCIItoUTF16(dnsData->mData[i].hostname, entry.mHostname);
|
|
|
|
entry.mExpiration = dnsData->mData[i].expiration;
|
2013-11-20 18:29:01 +04:00
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
for (uint32_t j = 0; j < dnsData->mData[i].hostaddr.Length(); j++) {
|
|
|
|
CopyASCIItoUTF16(dnsData->mData[i].hostaddr[j],
|
2015-05-28 21:07:44 +03:00
|
|
|
*addrs.AppendElement(fallible));
|
2012-12-10 18:13:55 +04:00
|
|
|
}
|
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
if (dnsData->mData[i].family == PR_AF_INET6) {
|
2013-11-20 18:29:01 +04:00
|
|
|
CopyASCIItoUTF16("ipv6", entry.mFamily);
|
2014-03-11 20:59:50 +04:00
|
|
|
} else {
|
2013-11-20 18:29:01 +04:00
|
|
|
CopyASCIItoUTF16("ipv4", entry.mFamily);
|
2014-03-11 20:59:50 +04:00
|
|
|
}
|
2012-12-10 18:13:55 +04:00
|
|
|
}
|
|
|
|
|
2013-05-12 09:09:22 +04:00
|
|
|
JS::RootedValue val(cx);
|
2014-06-11 23:38:55 +04:00
|
|
|
if (!ToJSValue(cx, dict, &val)) {
|
2013-04-15 05:32:06 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2014-03-11 20:59:50 +04:00
|
|
|
dnsData->mCallback->OnDashboardDataAvailable(val);
|
2012-12-10 18:13:55 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-04 20:50:11 +04:00
|
|
|
NS_IMETHODIMP
|
2014-03-11 20:59:50 +04:00
|
|
|
Dashboard::RequestDNSLookup(const nsACString &aHost,
|
2014-04-24 00:45:10 +04:00
|
|
|
NetDashboardCallback *aCallback)
|
2013-09-04 20:50:11 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
if (!mDnsService) {
|
|
|
|
mDnsService = do_GetService("@mozilla.org/network/dns-service;1", &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
2013-09-04 20:50:11 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
nsRefPtr<LookupHelper> helper = new LookupHelper();
|
2014-04-24 00:45:10 +04:00
|
|
|
helper->mCallback =
|
|
|
|
new nsMainThreadPtrHolder<NetDashboardCallback>(aCallback, true);
|
2014-03-11 20:59:50 +04:00
|
|
|
helper->mThread = NS_GetCurrentThread();
|
|
|
|
rv = mDnsService->AsyncResolve(aHost, 0, helper.get(),
|
|
|
|
NS_GetCurrentThread(),
|
|
|
|
getter_AddRefs(helper->mCancel));
|
|
|
|
return rv;
|
2013-09-04 20:50:11 +04:00
|
|
|
}
|
|
|
|
|
2013-07-09 00:35:08 +04:00
|
|
|
void
|
|
|
|
HttpConnInfo::SetHTTP1ProtocolVersion(uint8_t pv)
|
|
|
|
{
|
|
|
|
switch (pv) {
|
|
|
|
case NS_HTTP_VERSION_0_9:
|
2014-05-26 22:54:10 +04:00
|
|
|
protocolVersion.AssignLiteral(MOZ_UTF16("http/0.9"));
|
2013-07-09 00:35:08 +04:00
|
|
|
break;
|
|
|
|
case NS_HTTP_VERSION_1_0:
|
2014-05-26 22:54:10 +04:00
|
|
|
protocolVersion.AssignLiteral(MOZ_UTF16("http/1.0"));
|
2013-07-09 00:35:08 +04:00
|
|
|
break;
|
|
|
|
case NS_HTTP_VERSION_1_1:
|
2014-05-26 22:54:10 +04:00
|
|
|
protocolVersion.AssignLiteral(MOZ_UTF16("http/1.1"));
|
2013-07-09 00:35:08 +04:00
|
|
|
break;
|
2014-01-30 01:42:19 +04:00
|
|
|
case NS_HTTP_VERSION_2_0:
|
2014-05-26 22:54:10 +04:00
|
|
|
protocolVersion.AssignLiteral(MOZ_UTF16("http/2.0"));
|
2014-01-30 01:42:19 +04:00
|
|
|
break;
|
2013-07-09 00:35:08 +04:00
|
|
|
default:
|
2014-05-26 22:54:10 +04:00
|
|
|
protocolVersion.AssignLiteral(MOZ_UTF16("unknown protocol version"));
|
2013-07-09 00:35:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HttpConnInfo::SetHTTP2ProtocolVersion(uint8_t pv)
|
|
|
|
{
|
2014-11-17 20:35:06 +03:00
|
|
|
if (pv == SPDY_VERSION_31) {
|
2014-05-26 22:54:10 +04:00
|
|
|
protocolVersion.AssignLiteral(MOZ_UTF16("spdy/3.1"));
|
2014-09-23 21:49:13 +04:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT (pv == HTTP_VERSION_2);
|
|
|
|
protocolVersion.Assign(MOZ_UTF16("h2"));
|
2013-07-27 01:22:46 +04:00
|
|
|
}
|
2013-07-09 00:35:08 +04:00
|
|
|
}
|
|
|
|
|
2013-07-31 17:16:44 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Dashboard::RequestConnection(const nsACString& aHost, uint32_t aPort,
|
|
|
|
const char *aProtocol, uint32_t aTimeout,
|
2014-03-11 20:59:50 +04:00
|
|
|
NetDashboardCallback *aCallback)
|
2013-07-31 17:16:44 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2014-03-11 20:59:50 +04:00
|
|
|
nsRefPtr<ConnectionData> connectionData = new ConnectionData(this);
|
|
|
|
connectionData->mHost = aHost;
|
|
|
|
connectionData->mPort = aPort;
|
|
|
|
connectionData->mProtocol = aProtocol;
|
|
|
|
connectionData->mTimeout = aTimeout;
|
|
|
|
|
2014-04-24 00:45:10 +04:00
|
|
|
connectionData->mCallback =
|
|
|
|
new nsMainThreadPtrHolder<NetDashboardCallback>(aCallback, true);
|
2014-03-11 20:59:50 +04:00
|
|
|
connectionData->mThread = NS_GetCurrentThread();
|
2013-07-31 17:16:44 +04:00
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
rv = TestNewConnection(connectionData);
|
2013-07-31 17:16:44 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-10-22 10:55:00 +04:00
|
|
|
mozilla::net::GetErrorString(rv, connectionData->mStatus);
|
2013-09-07 05:29:24 +04:00
|
|
|
nsCOMPtr<nsIRunnable> event =
|
2014-03-11 20:59:50 +04:00
|
|
|
NS_NewRunnableMethodWithArg<nsRefPtr<ConnectionData> >
|
|
|
|
(this, &Dashboard::GetConnectionStatus, connectionData);
|
|
|
|
connectionData->mThread->Dispatch(event, NS_DISPATCH_NORMAL);
|
2013-07-31 17:16:44 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-03-11 20:59:50 +04:00
|
|
|
Dashboard::GetConnectionStatus(ConnectionData *aConnectionData)
|
2013-07-31 17:16:44 +04:00
|
|
|
{
|
2014-03-11 20:59:50 +04:00
|
|
|
nsRefPtr<ConnectionData> connectionData = aConnectionData;
|
2013-07-31 17:16:44 +04:00
|
|
|
AutoSafeJSContext cx;
|
|
|
|
|
|
|
|
mozilla::dom::ConnStatusDict dict;
|
2014-03-11 20:59:50 +04:00
|
|
|
dict.mStatus = connectionData->mStatus;
|
2013-07-31 17:16:44 +04:00
|
|
|
|
|
|
|
JS::RootedValue val(cx);
|
2014-06-11 23:38:55 +04:00
|
|
|
if (!ToJSValue(cx, dict, &val))
|
2013-07-31 17:16:44 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-03-11 20:59:50 +04:00
|
|
|
|
|
|
|
connectionData->mCallback->OnDashboardDataAvailable(val);
|
2013-07-31 17:16:44 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-03-11 20:59:50 +04:00
|
|
|
Dashboard::TestNewConnection(ConnectionData *aConnectionData)
|
2013-07-31 17:16:44 +04:00
|
|
|
{
|
2014-03-11 20:59:50 +04:00
|
|
|
nsRefPtr<ConnectionData> connectionData = aConnectionData;
|
|
|
|
|
2013-07-31 17:16:44 +04:00
|
|
|
nsresult rv;
|
2014-03-11 20:59:50 +04:00
|
|
|
if (!connectionData->mHost.Length() ||
|
|
|
|
!net_IsValidHostName(connectionData->mHost)) {
|
2013-07-31 17:16:44 +04:00
|
|
|
return NS_ERROR_UNKNOWN_HOST;
|
2014-03-11 20:59:50 +04:00
|
|
|
}
|
2013-07-31 17:16:44 +04:00
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
if (connectionData->mProtocol &&
|
|
|
|
NS_LITERAL_STRING("ssl").EqualsASCII(connectionData->mProtocol)) {
|
|
|
|
rv = gSocketTransportService->CreateTransport(
|
|
|
|
&connectionData->mProtocol, 1, connectionData->mHost,
|
|
|
|
connectionData->mPort, nullptr,
|
|
|
|
getter_AddRefs(connectionData->mSocket));
|
|
|
|
} else {
|
|
|
|
rv = gSocketTransportService->CreateTransport(
|
|
|
|
nullptr, 0, connectionData->mHost,
|
|
|
|
connectionData->mPort, nullptr,
|
|
|
|
getter_AddRefs(connectionData->mSocket));
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
2013-07-31 17:16:44 +04:00
|
|
|
return rv;
|
2014-03-11 20:59:50 +04:00
|
|
|
}
|
2013-07-31 17:16:44 +04:00
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
rv = connectionData->mSocket->SetEventSink(connectionData,
|
|
|
|
NS_GetCurrentThread());
|
|
|
|
if (NS_FAILED(rv)) {
|
2013-07-31 17:16:44 +04:00
|
|
|
return rv;
|
2014-03-11 20:59:50 +04:00
|
|
|
}
|
2013-07-31 17:16:44 +04:00
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
rv = connectionData->mSocket->OpenInputStream(
|
|
|
|
nsITransport::OPEN_BLOCKING, 0, 0,
|
|
|
|
getter_AddRefs(connectionData->mStreamIn));
|
|
|
|
if (NS_FAILED(rv)) {
|
2013-07-31 17:16:44 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
connectionData->StartTimer(connectionData->mTimeout);
|
2013-07-31 17:16:44 +04:00
|
|
|
|
2014-03-11 20:59:50 +04:00
|
|
|
return rv;
|
2013-07-31 17:16:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
nsresult key;
|
|
|
|
const char *error;
|
|
|
|
} ErrorEntry;
|
|
|
|
|
|
|
|
#undef ERROR
|
|
|
|
#define ERROR(key, val) {key, #key}
|
|
|
|
|
|
|
|
ErrorEntry socketTransportStatuses[] = {
|
|
|
|
ERROR(NS_NET_STATUS_RESOLVING_HOST, FAILURE(3)),
|
|
|
|
ERROR(NS_NET_STATUS_RESOLVED_HOST, FAILURE(11)),
|
|
|
|
ERROR(NS_NET_STATUS_CONNECTING_TO, FAILURE(7)),
|
|
|
|
ERROR(NS_NET_STATUS_CONNECTED_TO, FAILURE(4)),
|
|
|
|
ERROR(NS_NET_STATUS_SENDING_TO, FAILURE(5)),
|
|
|
|
ERROR(NS_NET_STATUS_WAITING_FOR, FAILURE(10)),
|
|
|
|
ERROR(NS_NET_STATUS_RECEIVING_FROM, FAILURE(6)),
|
|
|
|
};
|
|
|
|
#undef ERROR
|
|
|
|
|
2014-10-22 10:55:00 +04:00
|
|
|
|
|
|
|
static void
|
|
|
|
GetErrorString(nsresult rv, nsAString& errorString)
|
2013-07-31 17:16:44 +04:00
|
|
|
{
|
2014-10-22 10:55:00 +04:00
|
|
|
for (size_t i = 0; i < ArrayLength(socketTransportStatuses); ++i) {
|
2014-03-11 20:59:50 +04:00
|
|
|
if (socketTransportStatuses[i].key == rv) {
|
2014-10-22 10:55:00 +04:00
|
|
|
errorString.AssignASCII(socketTransportStatuses[i].error);
|
|
|
|
return;
|
2014-03-11 20:59:50 +04:00
|
|
|
}
|
2014-10-22 10:55:00 +04:00
|
|
|
}
|
|
|
|
nsAutoCString errorCString;
|
|
|
|
mozilla::GetErrorName(rv, errorCString);
|
|
|
|
CopyUTF8toUTF16(errorCString, errorString);
|
2013-07-31 17:16:44 +04:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|