Source code

Revision control

Other Tools

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http:mozilla.org/MPL/2.0/. */
4
5
#include "mozilla/dom/NetDashboardBinding.h"
6
#include "mozilla/dom/ToJSValue.h"
7
#include "mozilla/ErrorNames.h"
8
#include "mozilla/net/Dashboard.h"
9
#include "mozilla/net/HttpInfo.h"
10
#include "nsHttp.h"
11
#include "nsICancelable.h"
12
#include "nsIDNSService.h"
13
#include "nsIDNSRecord.h"
14
#include "nsIInputStream.h"
15
#include "nsINamed.h"
16
#include "nsISocketTransport.h"
17
#include "nsIThread.h"
18
#include "nsProxyRelease.h"
19
#include "nsSocketTransportService2.h"
20
#include "nsThreadUtils.h"
21
#include "nsURLHelper.h"
22
#include "mozilla/Logging.h"
23
#include "nsIOService.h"
24
#include "../cache2/CacheFileUtils.h"
25
26
using mozilla::AutoSafeJSContext;
27
using mozilla::dom::Sequence;
28
using mozilla::dom::ToJSValue;
29
30
namespace mozilla {
31
namespace net {
32
33
class SocketData : public nsISupports {
34
public:
35
NS_DECL_THREADSAFE_ISUPPORTS
36
37
SocketData() {
38
mTotalSent = 0;
39
mTotalRecv = 0;
40
mEventTarget = nullptr;
41
}
42
43
uint64_t mTotalSent;
44
uint64_t mTotalRecv;
45
nsTArray<SocketInfo> mData;
46
nsMainThreadPtrHandle<nsINetDashboardCallback> mCallback;
47
nsIEventTarget* mEventTarget;
48
49
private:
50
virtual ~SocketData() = default;
51
};
52
53
static void GetErrorString(nsresult rv, nsAString& errorString);
54
55
NS_IMPL_ISUPPORTS0(SocketData)
56
57
class HttpData : public nsISupports {
58
virtual ~HttpData() = default;
59
60
public:
61
NS_DECL_THREADSAFE_ISUPPORTS
62
63
HttpData() { mEventTarget = nullptr; }
64
65
nsTArray<HttpRetParams> mData;
66
nsMainThreadPtrHandle<nsINetDashboardCallback> mCallback;
67
nsIEventTarget* mEventTarget;
68
};
69
70
NS_IMPL_ISUPPORTS0(HttpData)
71
72
class WebSocketRequest : public nsISupports {
73
virtual ~WebSocketRequest() = default;
74
75
public:
76
NS_DECL_THREADSAFE_ISUPPORTS
77
78
WebSocketRequest() { mEventTarget = nullptr; }
79
80
nsMainThreadPtrHandle<nsINetDashboardCallback> mCallback;
81
nsIEventTarget* mEventTarget;
82
};
83
84
NS_IMPL_ISUPPORTS0(WebSocketRequest)
85
86
class DnsData : public nsISupports {
87
virtual ~DnsData() = default;
88
89
public:
90
NS_DECL_THREADSAFE_ISUPPORTS
91
92
DnsData() { mEventTarget = nullptr; }
93
94
nsTArray<DNSCacheEntries> mData;
95
nsMainThreadPtrHandle<nsINetDashboardCallback> mCallback;
96
nsIEventTarget* mEventTarget;
97
};
98
99
NS_IMPL_ISUPPORTS0(DnsData)
100
101
class ConnectionData : public nsITransportEventSink,
102
public nsITimerCallback,
103
public nsINamed {
104
virtual ~ConnectionData() {
105
if (mTimer) {
106
mTimer->Cancel();
107
}
108
}
109
110
public:
111
NS_DECL_THREADSAFE_ISUPPORTS
112
NS_DECL_NSITRANSPORTEVENTSINK
113
NS_DECL_NSITIMERCALLBACK
114
115
NS_IMETHOD GetName(nsACString& aName) override {
116
aName.AssignLiteral("net::ConnectionData");
117
return NS_OK;
118
}
119
120
void StartTimer(uint32_t aTimeout);
121
void StopTimer();
122
123
explicit ConnectionData(Dashboard* target) : mPort(0), mTimeout(0) {
124
mEventTarget = nullptr;
125
mDashboard = target;
126
}
127
128
nsCOMPtr<nsISocketTransport> mSocket;
129
nsCOMPtr<nsIInputStream> mStreamIn;
130
nsCOMPtr<nsITimer> mTimer;
131
nsMainThreadPtrHandle<nsINetDashboardCallback> mCallback;
132
nsIEventTarget* mEventTarget;
133
Dashboard* mDashboard;
134
135
nsCString mHost;
136
uint32_t mPort;
137
nsCString mProtocol;
138
uint32_t mTimeout;
139
140
nsString mStatus;
141
};
142
143
NS_IMPL_ISUPPORTS(ConnectionData, nsITransportEventSink, nsITimerCallback,
144
nsINamed)
145
146
class RcwnData : public nsISupports {
147
virtual ~RcwnData() = default;
148
149
public:
150
NS_DECL_THREADSAFE_ISUPPORTS
151
152
RcwnData() { mEventTarget = nullptr; }
153
154
nsMainThreadPtrHandle<nsINetDashboardCallback> mCallback;
155
nsIEventTarget* mEventTarget;
156
};
157
158
NS_IMPL_ISUPPORTS0(RcwnData)
159
160
NS_IMETHODIMP
161
ConnectionData::OnTransportStatus(nsITransport* aTransport, nsresult aStatus,
162
int64_t aProgress, int64_t aProgressMax) {
163
if (aStatus == NS_NET_STATUS_CONNECTED_TO) {
164
StopTimer();
165
}
166
167
GetErrorString(aStatus, mStatus);
168
mEventTarget->Dispatch(NewRunnableMethod<RefPtr<ConnectionData>>(
169
"net::Dashboard::GetConnectionStatus", mDashboard,
170
&Dashboard::GetConnectionStatus, this),
171
NS_DISPATCH_NORMAL);
172
173
return NS_OK;
174
}
175
176
NS_IMETHODIMP
177
ConnectionData::Notify(nsITimer* aTimer) {
178
MOZ_ASSERT(aTimer == mTimer);
179
180
if (mSocket) {
181
mSocket->Close(NS_ERROR_ABORT);
182
mSocket = nullptr;
183
mStreamIn = nullptr;
184
}
185
186
mTimer = nullptr;
187
188
mStatus.AssignLiteral(u"NS_ERROR_NET_TIMEOUT");
189
mEventTarget->Dispatch(NewRunnableMethod<RefPtr<ConnectionData>>(
190
"net::Dashboard::GetConnectionStatus", mDashboard,
191
&Dashboard::GetConnectionStatus, this),
192
NS_DISPATCH_NORMAL);
193
194
return NS_OK;
195
}
196
197
void ConnectionData::StartTimer(uint32_t aTimeout) {
198
if (!mTimer) {
199
mTimer = NS_NewTimer();
200
}
201
202
mTimer->InitWithCallback(this, aTimeout * 1000, nsITimer::TYPE_ONE_SHOT);
203
}
204
205
void ConnectionData::StopTimer() {
206
if (mTimer) {
207
mTimer->Cancel();
208
mTimer = nullptr;
209
}
210
}
211
212
class LookupHelper;
213
214
class LookupArgument : public nsISupports {
215
virtual ~LookupArgument() = default;
216
217
public:
218
NS_DECL_THREADSAFE_ISUPPORTS
219
220
LookupArgument(nsIDNSRecord* aRecord, LookupHelper* aHelper) {
221
mRecord = aRecord;
222
mHelper = aHelper;
223
}
224
225
nsCOMPtr<nsIDNSRecord> mRecord;
226
RefPtr<LookupHelper> mHelper;
227
};
228
229
NS_IMPL_ISUPPORTS0(LookupArgument)
230
231
class LookupHelper final : public nsIDNSListener {
232
virtual ~LookupHelper() {
233
if (mCancel) {
234
mCancel->Cancel(NS_ERROR_ABORT);
235
}
236
}
237
238
public:
239
NS_DECL_THREADSAFE_ISUPPORTS
240
NS_DECL_NSIDNSLISTENER
241
242
LookupHelper() : mEventTarget{nullptr}, mStatus{NS_ERROR_NOT_INITIALIZED} {}
243
244
nsresult ConstructAnswer(LookupArgument* aArgument);
245
246
public:
247
nsCOMPtr<nsICancelable> mCancel;
248
nsMainThreadPtrHandle<nsINetDashboardCallback> mCallback;
249
nsIEventTarget* mEventTarget;
250
nsresult mStatus;
251
};
252
253
NS_IMPL_ISUPPORTS(LookupHelper, nsIDNSListener)
254
255
NS_IMETHODIMP
256
LookupHelper::OnLookupComplete(nsICancelable* aRequest, nsIDNSRecord* aRecord,
257
nsresult aStatus) {
258
MOZ_ASSERT(aRequest == mCancel);
259
mCancel = nullptr;
260
mStatus = aStatus;
261
262
RefPtr<LookupArgument> arg = new LookupArgument(aRecord, this);
263
mEventTarget->Dispatch(NewRunnableMethod<RefPtr<LookupArgument>>(
264
"net::LookupHelper::ConstructAnswer", this,
265
&LookupHelper::ConstructAnswer, arg),
266
NS_DISPATCH_NORMAL);
267
268
return NS_OK;
269
}
270
271
NS_IMETHODIMP
272
LookupHelper::OnLookupByTypeComplete(nsICancelable* aRequest,
273
nsIDNSByTypeRecord* aRes,
274
nsresult aStatus) {
275
return NS_OK;
276
}
277
278
nsresult LookupHelper::ConstructAnswer(LookupArgument* aArgument) {
279
nsIDNSRecord* aRecord = aArgument->mRecord;
280
AutoSafeJSContext cx;
281
282
mozilla::dom::DNSLookupDict dict;
283
dict.mAddress.Construct();
284
285
Sequence<nsString>& addresses = dict.mAddress.Value();
286
287
if (NS_SUCCEEDED(mStatus)) {
288
dict.mAnswer = true;
289
bool hasMore;
290
aRecord->HasMore(&hasMore);
291
while (hasMore) {
292
nsString* nextAddress = addresses.AppendElement(fallible);
293
if (!nextAddress) {
294
return NS_ERROR_OUT_OF_MEMORY;
295
}
296
297
nsCString nextAddressASCII;
298
aRecord->GetNextAddrAsString(nextAddressASCII);
299
CopyASCIItoUTF16(nextAddressASCII, *nextAddress);
300
aRecord->HasMore(&hasMore);
301
}
302
} else {
303
dict.mAnswer = false;
304
GetErrorString(mStatus, dict.mError);
305
}
306
307
JS::RootedValue val(cx);
308
if (!ToJSValue(cx, dict, &val)) {
309
return NS_ERROR_FAILURE;
310
}
311
312
this->mCallback->OnDashboardDataAvailable(val);
313
314
return NS_OK;
315
}
316
317
NS_IMPL_ISUPPORTS(Dashboard, nsIDashboard, nsIDashboardEventNotifier)
318
319
Dashboard::Dashboard() { mEnableLogging = false; }
320
321
NS_IMETHODIMP
322
Dashboard::RequestSockets(nsINetDashboardCallback* aCallback) {
323
RefPtr<SocketData> socketData = new SocketData();
324
socketData->mCallback = new nsMainThreadPtrHolder<nsINetDashboardCallback>(
325
"nsINetDashboardCallback", aCallback, true);
326
socketData->mEventTarget = GetCurrentThreadEventTarget();
327
gSocketTransportService->Dispatch(
328
NewRunnableMethod<RefPtr<SocketData>>(
329
"net::Dashboard::GetSocketsDispatch", this,
330
&Dashboard::GetSocketsDispatch, socketData),
331
NS_DISPATCH_NORMAL);
332
return NS_OK;
333
}
334
335
nsresult Dashboard::GetSocketsDispatch(SocketData* aSocketData) {
336
RefPtr<SocketData> socketData = aSocketData;
337
if (gSocketTransportService) {
338
gSocketTransportService->GetSocketConnections(&socketData->mData);
339
socketData->mTotalSent = gSocketTransportService->GetSentBytes();
340
socketData->mTotalRecv = gSocketTransportService->GetReceivedBytes();
341
}
342
socketData->mEventTarget->Dispatch(
343
NewRunnableMethod<RefPtr<SocketData>>("net::Dashboard::GetSockets", this,
344
&Dashboard::GetSockets, socketData),
345
NS_DISPATCH_NORMAL);
346
return NS_OK;
347
}
348
349
nsresult Dashboard::GetSockets(SocketData* aSocketData) {
350
RefPtr<SocketData> socketData = aSocketData;
351
AutoSafeJSContext cx;
352
353
mozilla::dom::SocketsDict dict;
354
dict.mSockets.Construct();
355
dict.mSent = 0;
356
dict.mReceived = 0;
357
358
Sequence<mozilla::dom::SocketElement>& sockets = dict.mSockets.Value();
359
360
uint32_t length = socketData->mData.Length();
361
if (!sockets.SetCapacity(length, fallible)) {
362
JS_ReportOutOfMemory(cx);
363
return NS_ERROR_OUT_OF_MEMORY;
364
}
365
366
for (uint32_t i = 0; i < socketData->mData.Length(); i++) {
367
dom::SocketElement& mSocket = *sockets.AppendElement(fallible);
368
CopyASCIItoUTF16(socketData->mData[i].host, mSocket.mHost);
369
mSocket.mPort = socketData->mData[i].port;
370
mSocket.mActive = socketData->mData[i].active;
371
mSocket.mTcp = socketData->mData[i].tcp;
372
mSocket.mSent = (double)socketData->mData[i].sent;
373
mSocket.mReceived = (double)socketData->mData[i].received;
374
dict.mSent += socketData->mData[i].sent;
375
dict.mReceived += socketData->mData[i].received;
376
}
377
378
dict.mSent += socketData->mTotalSent;
379
dict.mReceived += socketData->mTotalRecv;
380
JS::RootedValue val(cx);
381
if (!ToJSValue(cx, dict, &val)) return NS_ERROR_FAILURE;
382
socketData->mCallback->OnDashboardDataAvailable(val);
383
384
return NS_OK;
385
}
386
387
NS_IMETHODIMP
388
Dashboard::RequestHttpConnections(nsINetDashboardCallback* aCallback) {
389
RefPtr<HttpData> httpData = new HttpData();
390
httpData->mCallback = new nsMainThreadPtrHolder<nsINetDashboardCallback>(
391
"nsINetDashboardCallback", aCallback, true);
392
httpData->mEventTarget = GetCurrentThreadEventTarget();
393
394
gSocketTransportService->Dispatch(NewRunnableMethod<RefPtr<HttpData>>(
395
"net::Dashboard::GetHttpDispatch", this,
396
&Dashboard::GetHttpDispatch, httpData),
397
NS_DISPATCH_NORMAL);
398
return NS_OK;
399
}
400
401
nsresult Dashboard::GetHttpDispatch(HttpData* aHttpData) {
402
RefPtr<HttpData> httpData = aHttpData;
403
HttpInfo::GetHttpConnectionData(&httpData->mData);
404
httpData->mEventTarget->Dispatch(
405
NewRunnableMethod<RefPtr<HttpData>>("net::Dashboard::GetHttpConnections",
406
this, &Dashboard::GetHttpConnections,
407
httpData),
408
NS_DISPATCH_NORMAL);
409
return NS_OK;
410
}
411
412
nsresult Dashboard::GetHttpConnections(HttpData* aHttpData) {
413
RefPtr<HttpData> httpData = aHttpData;
414
AutoSafeJSContext cx;
415
416
mozilla::dom::HttpConnDict dict;
417
dict.mConnections.Construct();
418
419
using mozilla::dom::HalfOpenInfoDict;
420
using mozilla::dom::HttpConnectionElement;
421
using mozilla::dom::HttpConnInfo;
422
Sequence<HttpConnectionElement>& connections = dict.mConnections.Value();
423
424
uint32_t length = httpData->mData.Length();
425
if (!connections.SetCapacity(length, fallible)) {
426
JS_ReportOutOfMemory(cx);
427
return NS_ERROR_OUT_OF_MEMORY;
428
}
429
430
for (uint32_t i = 0; i < httpData->mData.Length(); i++) {
431
HttpConnectionElement& connection = *connections.AppendElement(fallible);
432
433
CopyASCIItoUTF16(httpData->mData[i].host, connection.mHost);
434
connection.mPort = httpData->mData[i].port;
435
CopyASCIItoUTF16(httpData->mData[i].httpVersion, connection.mHttpVersion);
436
connection.mSsl = httpData->mData[i].ssl;
437
438
connection.mActive.Construct();
439
connection.mIdle.Construct();
440
connection.mHalfOpens.Construct();
441
442
Sequence<HttpConnInfo>& active = connection.mActive.Value();
443
Sequence<HttpConnInfo>& idle = connection.mIdle.Value();
444
Sequence<HalfOpenInfoDict>& halfOpens = connection.mHalfOpens.Value();
445
446
if (!active.SetCapacity(httpData->mData[i].active.Length(), fallible) ||
447
!idle.SetCapacity(httpData->mData[i].idle.Length(), fallible) ||
448
!halfOpens.SetCapacity(httpData->mData[i].halfOpens.Length(),
449
fallible)) {
450
JS_ReportOutOfMemory(cx);
451
return NS_ERROR_OUT_OF_MEMORY;
452
}
453
454
for (uint32_t j = 0; j < httpData->mData[i].active.Length(); j++) {
455
HttpConnInfo& info = *active.AppendElement(fallible);
456
info.mRtt = httpData->mData[i].active[j].rtt;
457
info.mTtl = httpData->mData[i].active[j].ttl;
458
info.mProtocolVersion = httpData->mData[i].active[j].protocolVersion;
459
}
460
461
for (uint32_t j = 0; j < httpData->mData[i].idle.Length(); j++) {
462
HttpConnInfo& info = *idle.AppendElement(fallible);
463
info.mRtt = httpData->mData[i].idle[j].rtt;
464
info.mTtl = httpData->mData[i].idle[j].ttl;
465
info.mProtocolVersion = httpData->mData[i].idle[j].protocolVersion;
466
}
467
468
for (uint32_t j = 0; j < httpData->mData[i].halfOpens.Length(); j++) {
469
HalfOpenInfoDict& info = *halfOpens.AppendElement(fallible);
470
info.mSpeculative = httpData->mData[i].halfOpens[j].speculative;
471
}
472
}
473
474
JS::RootedValue val(cx);
475
if (!ToJSValue(cx, dict, &val)) {
476
return NS_ERROR_FAILURE;
477
}
478
479
httpData->mCallback->OnDashboardDataAvailable(val);
480
481
return NS_OK;
482
}
483
484
NS_IMETHODIMP
485
Dashboard::GetEnableLogging(bool* value) {
486
*value = mEnableLogging;
487
return NS_OK;
488
}
489
490
NS_IMETHODIMP
491
Dashboard::SetEnableLogging(const bool value) {
492
mEnableLogging = value;
493
return NS_OK;
494
}
495
496
NS_IMETHODIMP
497
Dashboard::AddHost(const nsACString& aHost, uint32_t aSerial, bool aEncrypted) {
498
if (mEnableLogging) {
499
mozilla::MutexAutoLock lock(mWs.lock);
500
LogData mData(nsCString(aHost), aSerial, aEncrypted);
501
if (mWs.data.Contains(mData)) {
502
return NS_OK;
503
}
504
if (!mWs.data.AppendElement(mData)) {
505
return NS_ERROR_OUT_OF_MEMORY;
506
}
507
return NS_OK;
508
}
509
return NS_ERROR_FAILURE;
510
}
511
512
NS_IMETHODIMP
513
Dashboard::RemoveHost(const nsACString& aHost, uint32_t aSerial) {
514
if (mEnableLogging) {
515
mozilla::MutexAutoLock lock(mWs.lock);
516
int32_t index = mWs.IndexOf(nsCString(aHost), aSerial);
517
if (index == -1) return NS_ERROR_FAILURE;
518
mWs.data.RemoveElementAt(index);
519
return NS_OK;
520
}
521
return NS_ERROR_FAILURE;
522
}
523
524
NS_IMETHODIMP
525
Dashboard::NewMsgSent(const nsACString& aHost, uint32_t aSerial,
526
uint32_t aLength) {
527
if (mEnableLogging) {
528
mozilla::MutexAutoLock lock(mWs.lock);
529
int32_t index = mWs.IndexOf(nsCString(aHost), aSerial);
530
if (index == -1) return NS_ERROR_FAILURE;
531
mWs.data[index].mMsgSent++;
532
mWs.data[index].mSizeSent += aLength;
533
return NS_OK;
534
}
535
return NS_ERROR_FAILURE;
536
}
537
538
NS_IMETHODIMP
539
Dashboard::NewMsgReceived(const nsACString& aHost, uint32_t aSerial,
540
uint32_t aLength) {
541
if (mEnableLogging) {
542
mozilla::MutexAutoLock lock(mWs.lock);
543
int32_t index = mWs.IndexOf(nsCString(aHost), aSerial);
544
if (index == -1) return NS_ERROR_FAILURE;
545
mWs.data[index].mMsgReceived++;
546
mWs.data[index].mSizeReceived += aLength;
547
return NS_OK;
548
}
549
return NS_ERROR_FAILURE;
550
}
551
552
NS_IMETHODIMP
553
Dashboard::RequestWebsocketConnections(nsINetDashboardCallback* aCallback) {
554
RefPtr<WebSocketRequest> wsRequest = new WebSocketRequest();
555
wsRequest->mCallback = new nsMainThreadPtrHolder<nsINetDashboardCallback>(
556
"nsINetDashboardCallback", aCallback, true);
557
wsRequest->mEventTarget = GetCurrentThreadEventTarget();
558
559
wsRequest->mEventTarget->Dispatch(
560
NewRunnableMethod<RefPtr<WebSocketRequest>>(
561
"net::Dashboard::GetWebSocketConnections", this,
562
&Dashboard::GetWebSocketConnections, wsRequest),
563
NS_DISPATCH_NORMAL);
564
return NS_OK;
565
}
566
567
nsresult Dashboard::GetWebSocketConnections(WebSocketRequest* aWsRequest) {
568
RefPtr<WebSocketRequest> wsRequest = aWsRequest;
569
AutoSafeJSContext cx;
570
571
mozilla::dom::WebSocketDict dict;
572
dict.mWebsockets.Construct();
573
Sequence<mozilla::dom::WebSocketElement>& websockets =
574
dict.mWebsockets.Value();
575
576
mozilla::MutexAutoLock lock(mWs.lock);
577
uint32_t length = mWs.data.Length();
578
if (!websockets.SetCapacity(length, fallible)) {
579
JS_ReportOutOfMemory(cx);
580
return NS_ERROR_OUT_OF_MEMORY;
581
}
582
583
for (uint32_t i = 0; i < mWs.data.Length(); i++) {
584
dom::WebSocketElement& websocket = *websockets.AppendElement(fallible);
585
CopyASCIItoUTF16(mWs.data[i].mHost, websocket.mHostport);
586
websocket.mMsgsent = mWs.data[i].mMsgSent;
587
websocket.mMsgreceived = mWs.data[i].mMsgReceived;
588
websocket.mSentsize = mWs.data[i].mSizeSent;
589
websocket.mReceivedsize = mWs.data[i].mSizeReceived;
590
websocket.mEncrypted = mWs.data[i].mEncrypted;
591
}
592
593
JS::RootedValue val(cx);
594
if (!ToJSValue(cx, dict, &val)) {
595
return NS_ERROR_FAILURE;
596
}
597
wsRequest->mCallback->OnDashboardDataAvailable(val);
598
599
return NS_OK;
600
}
601
602
NS_IMETHODIMP
603
Dashboard::RequestDNSInfo(nsINetDashboardCallback* aCallback) {
604
RefPtr<DnsData> dnsData = new DnsData();
605
dnsData->mCallback = new nsMainThreadPtrHolder<nsINetDashboardCallback>(
606
"nsINetDashboardCallback", aCallback, true);
607
608
nsresult rv;
609
dnsData->mData.Clear();
610
dnsData->mEventTarget = GetCurrentThreadEventTarget();
611
612
if (!mDnsService) {
613
mDnsService = do_GetService("@mozilla.org/network/dns-service;1", &rv);
614
if (NS_FAILED(rv)) {
615
return rv;
616
}
617
}
618
619
gSocketTransportService->Dispatch(
620
NewRunnableMethod<RefPtr<DnsData>>("net::Dashboard::GetDnsInfoDispatch",
621
this, &Dashboard::GetDnsInfoDispatch,
622
dnsData),
623
NS_DISPATCH_NORMAL);
624
return NS_OK;
625
}
626
627
nsresult Dashboard::GetDnsInfoDispatch(DnsData* aDnsData) {
628
RefPtr<DnsData> dnsData = aDnsData;
629
if (mDnsService) {
630
mDnsService->GetDNSCacheEntries(&dnsData->mData);
631
}
632
dnsData->mEventTarget->Dispatch(
633
NewRunnableMethod<RefPtr<DnsData>>("net::Dashboard::GetDNSCacheEntries",
634
this, &Dashboard::GetDNSCacheEntries,
635
dnsData),
636
NS_DISPATCH_NORMAL);
637
return NS_OK;
638
}
639
640
nsresult Dashboard::GetDNSCacheEntries(DnsData* dnsData) {
641
AutoSafeJSContext cx;
642
643
mozilla::dom::DNSCacheDict dict;
644
dict.mEntries.Construct();
645
Sequence<mozilla::dom::DnsCacheEntry>& entries = dict.mEntries.Value();
646
647
uint32_t length = dnsData->mData.Length();
648
if (!entries.SetCapacity(length, fallible)) {
649
JS_ReportOutOfMemory(cx);
650
return NS_ERROR_OUT_OF_MEMORY;
651
}
652
653
for (uint32_t i = 0; i < dnsData->mData.Length(); i++) {
654
dom::DnsCacheEntry& entry = *entries.AppendElement(fallible);
655
entry.mHostaddr.Construct();
656
657
Sequence<nsString>& addrs = entry.mHostaddr.Value();
658
if (!addrs.SetCapacity(dnsData->mData[i].hostaddr.Length(), fallible)) {
659
JS_ReportOutOfMemory(cx);
660
return NS_ERROR_OUT_OF_MEMORY;
661
}
662
663
CopyASCIItoUTF16(dnsData->mData[i].hostname, entry.mHostname);
664
entry.mExpiration = dnsData->mData[i].expiration;
665
entry.mTrr = dnsData->mData[i].TRR;
666
667
for (uint32_t j = 0; j < dnsData->mData[i].hostaddr.Length(); j++) {
668
nsString* addr = addrs.AppendElement(fallible);
669
if (!addr) {
670
JS_ReportOutOfMemory(cx);
671
return NS_ERROR_OUT_OF_MEMORY;
672
}
673
CopyASCIItoUTF16(dnsData->mData[i].hostaddr[j], *addr);
674
}
675
676
if (dnsData->mData[i].family == PR_AF_INET6) {
677
entry.mFamily.AssignLiteral(u"ipv6");
678
} else {
679
entry.mFamily.AssignLiteral(u"ipv4");
680
}
681
}
682
683
JS::RootedValue val(cx);
684
if (!ToJSValue(cx, dict, &val)) {
685
return NS_ERROR_FAILURE;
686
}
687
dnsData->mCallback->OnDashboardDataAvailable(val);
688
689
return NS_OK;
690
}
691
692
NS_IMETHODIMP
693
Dashboard::RequestDNSLookup(const nsACString& aHost,
694
nsINetDashboardCallback* aCallback) {
695
nsresult rv;
696
697
if (!mDnsService) {
698
mDnsService = do_GetService("@mozilla.org/network/dns-service;1", &rv);
699
if (NS_FAILED(rv)) {
700
return rv;
701
}
702
}
703
704
RefPtr<LookupHelper> helper = new LookupHelper();
705
helper->mCallback = new nsMainThreadPtrHolder<nsINetDashboardCallback>(
706
"nsINetDashboardCallback", aCallback, true);
707
helper->mEventTarget = GetCurrentThreadEventTarget();
708
OriginAttributes attrs;
709
rv = mDnsService->AsyncResolveNative(aHost, 0, helper.get(),
710
NS_GetCurrentThread(), attrs,
711
getter_AddRefs(helper->mCancel));
712
return rv;
713
}
714
715
NS_IMETHODIMP
716
Dashboard::RequestRcwnStats(nsINetDashboardCallback* aCallback) {
717
RefPtr<RcwnData> rcwnData = new RcwnData();
718
rcwnData->mEventTarget = GetCurrentThreadEventTarget();
719
rcwnData->mCallback = new nsMainThreadPtrHolder<nsINetDashboardCallback>(
720
"nsINetDashboardCallback", aCallback, true);
721
722
return rcwnData->mEventTarget->Dispatch(
723
NewRunnableMethod<RefPtr<RcwnData>>("net::Dashboard::GetRcwnData", this,
724
&Dashboard::GetRcwnData, rcwnData),
725
NS_DISPATCH_NORMAL);
726
}
727
728
nsresult Dashboard::GetRcwnData(RcwnData* aData) {
729
AutoSafeJSContext cx;
730
mozilla::dom::RcwnStatus dict;
731
732
dict.mTotalNetworkRequests = gIOService->GetTotalRequestNumber();
733
dict.mRcwnCacheWonCount = gIOService->GetCacheWonRequestNumber();
734
dict.mRcwnNetWonCount = gIOService->GetNetWonRequestNumber();
735
736
uint32_t cacheSlow, cacheNotSlow;
737
CacheFileUtils::CachePerfStats::GetSlowStats(&cacheSlow, &cacheNotSlow);
738
dict.mCacheSlowCount = cacheSlow;
739
dict.mCacheNotSlowCount = cacheNotSlow;
740
741
dict.mPerfStats.Construct();
742
Sequence<mozilla::dom::RcwnPerfStats>& perfStats = dict.mPerfStats.Value();
743
uint32_t length = CacheFileUtils::CachePerfStats::LAST;
744
if (!perfStats.SetCapacity(length, fallible)) {
745
JS_ReportOutOfMemory(cx);
746
return NS_ERROR_OUT_OF_MEMORY;
747
}
748
749
for (uint32_t i = 0; i < length; i++) {
750
CacheFileUtils::CachePerfStats::EDataType perfType =
751
static_cast<CacheFileUtils::CachePerfStats::EDataType>(i);
752
dom::RcwnPerfStats& elem = *perfStats.AppendElement(fallible);
753
elem.mAvgShort =
754
CacheFileUtils::CachePerfStats::GetAverage(perfType, false);
755
elem.mAvgLong = CacheFileUtils::CachePerfStats::GetAverage(perfType, true);
756
elem.mStddevLong =
757
CacheFileUtils::CachePerfStats::GetStdDev(perfType, true);
758
}
759
760
JS::RootedValue val(cx);
761
if (!ToJSValue(cx, dict, &val)) {
762
return NS_ERROR_FAILURE;
763
}
764
765
aData->mCallback->OnDashboardDataAvailable(val);
766
767
return NS_OK;
768
}
769
770
void HttpConnInfo::SetHTTP1ProtocolVersion(HttpVersion pv) {
771
switch (pv) {
772
case HttpVersion::v0_9:
773
protocolVersion.AssignLiteral(u"http/0.9");
774
break;
775
case HttpVersion::v1_0:
776
protocolVersion.AssignLiteral(u"http/1.0");
777
break;
778
case HttpVersion::v1_1:
779
protocolVersion.AssignLiteral(u"http/1.1");
780
break;
781
case HttpVersion::v2_0:
782
protocolVersion.AssignLiteral(u"http/2.0");
783
break;
784
case HttpVersion::v3_0:
785
protocolVersion.AssignLiteral(u"http/3.0");
786
break;
787
default:
788
protocolVersion.AssignLiteral(u"unknown protocol version");
789
}
790
}
791
792
void HttpConnInfo::SetHTTP2ProtocolVersion(SpdyVersion pv) {
793
MOZ_ASSERT(pv == SpdyVersion::HTTP_2);
794
protocolVersion.AssignLiteral(u"h2");
795
}
796
797
void HttpConnInfo::SetHTTP3ProtocolVersion() {
798
protocolVersion.AssignLiteral(u"h3");
799
}
800
801
NS_IMETHODIMP
802
Dashboard::GetLogPath(nsACString& aLogPath) {
803
aLogPath.SetLength(2048);
804
uint32_t len = LogModule::GetLogFile(aLogPath.BeginWriting(), 2048);
805
aLogPath.SetLength(len);
806
return NS_OK;
807
}
808
809
NS_IMETHODIMP
810
Dashboard::RequestConnection(const nsACString& aHost, uint32_t aPort,
811
const char* aProtocol, uint32_t aTimeout,
812
nsINetDashboardCallback* aCallback) {
813
nsresult rv;
814
RefPtr<ConnectionData> connectionData = new ConnectionData(this);
815
connectionData->mHost = aHost;
816
connectionData->mPort = aPort;
817
connectionData->mProtocol = aProtocol;
818
connectionData->mTimeout = aTimeout;
819
820
connectionData->mCallback =
821
new nsMainThreadPtrHolder<nsINetDashboardCallback>(
822
"nsINetDashboardCallback", aCallback, true);
823
connectionData->mEventTarget = GetCurrentThreadEventTarget();
824
825
rv = TestNewConnection(connectionData);
826
if (NS_FAILED(rv)) {
827
mozilla::net::GetErrorString(rv, connectionData->mStatus);
828
connectionData->mEventTarget->Dispatch(
829
NewRunnableMethod<RefPtr<ConnectionData>>(
830
"net::Dashboard::GetConnectionStatus", this,
831
&Dashboard::GetConnectionStatus, connectionData),
832
NS_DISPATCH_NORMAL);
833
return rv;
834
}
835
836
return NS_OK;
837
}
838
839
nsresult Dashboard::GetConnectionStatus(ConnectionData* aConnectionData) {
840
RefPtr<ConnectionData> connectionData = aConnectionData;
841
AutoSafeJSContext cx;
842
843
mozilla::dom::ConnStatusDict dict;
844
dict.mStatus = connectionData->mStatus;
845
846
JS::RootedValue val(cx);
847
if (!ToJSValue(cx, dict, &val)) return NS_ERROR_FAILURE;
848
849
connectionData->mCallback->OnDashboardDataAvailable(val);
850
851
return NS_OK;
852
}
853
854
nsresult Dashboard::TestNewConnection(ConnectionData* aConnectionData) {
855
RefPtr<ConnectionData> connectionData = aConnectionData;
856
857
nsresult rv;
858
if (!connectionData->mHost.Length() ||
859
!net_IsValidHostName(connectionData->mHost)) {
860
return NS_ERROR_UNKNOWN_HOST;
861
}
862
863
if (connectionData->mProtocol.EqualsLiteral("ssl")) {
864
AutoTArray<nsCString, 1> socketTypes = {connectionData->mProtocol};
865
rv = gSocketTransportService->CreateTransport(
866
socketTypes, connectionData->mHost, connectionData->mPort, nullptr,
867
getter_AddRefs(connectionData->mSocket));
868
} else {
869
rv = gSocketTransportService->CreateTransport(
870
nsTArray<nsCString>(), connectionData->mHost, connectionData->mPort,
871
nullptr, getter_AddRefs(connectionData->mSocket));
872
}
873
if (NS_FAILED(rv)) {
874
return rv;
875
}
876
877
rv = connectionData->mSocket->SetEventSink(connectionData,
878
GetCurrentThreadEventTarget());
879
if (NS_FAILED(rv)) {
880
return rv;
881
}
882
883
rv = connectionData->mSocket->OpenInputStream(
884
nsITransport::OPEN_BLOCKING, 0, 0,
885
getter_AddRefs(connectionData->mStreamIn));
886
if (NS_FAILED(rv)) {
887
return rv;
888
}
889
890
connectionData->StartTimer(connectionData->mTimeout);
891
892
return rv;
893
}
894
895
typedef struct {
896
nsresult key;
897
const char* error;
898
} ErrorEntry;
899
900
#undef ERROR
901
#define ERROR(key, val) \
902
{ key, #key }
903
904
ErrorEntry socketTransportStatuses[] = {
905
ERROR(NS_NET_STATUS_RESOLVING_HOST, FAILURE(3)),
906
ERROR(NS_NET_STATUS_RESOLVED_HOST, FAILURE(11)),
907
ERROR(NS_NET_STATUS_CONNECTING_TO, FAILURE(7)),
908
ERROR(NS_NET_STATUS_CONNECTED_TO, FAILURE(4)),
909
ERROR(NS_NET_STATUS_TLS_HANDSHAKE_STARTING, FAILURE(12)),
910
ERROR(NS_NET_STATUS_TLS_HANDSHAKE_ENDED, FAILURE(13)),
911
ERROR(NS_NET_STATUS_SENDING_TO, FAILURE(5)),
912
ERROR(NS_NET_STATUS_WAITING_FOR, FAILURE(10)),
913
ERROR(NS_NET_STATUS_RECEIVING_FROM, FAILURE(6)),
914
};
915
#undef ERROR
916
917
static void GetErrorString(nsresult rv, nsAString& errorString) {
918
for (auto& socketTransportStatus : socketTransportStatuses) {
919
if (socketTransportStatus.key == rv) {
920
errorString.AssignASCII(socketTransportStatus.error);
921
return;
922
}
923
}
924
nsAutoCString errorCString;
925
mozilla::GetErrorName(rv, errorCString);
926
CopyUTF8toUTF16(errorCString, errorString);
927
}
928
929
} // namespace net
930
} // namespace mozilla