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 #ifndef nsDashboard_h__
6 #define nsDashboard_h__
7 
8 #include "mozilla/Mutex.h"
9 #include "mozilla/net/DashboardTypes.h"
10 #include "nsIDashboard.h"
11 #include "nsIDashboardEventNotifier.h"
12 
13 class nsIDNSService;
14 
15 namespace mozilla {
16 namespace net {
17 
18 class SocketData;
19 class HttpData;
20 class DnsData;
21 class WebSocketRequest;
22 class ConnectionData;
23 class RcwnData;
24 
25 class Dashboard final : public nsIDashboard, public nsIDashboardEventNotifier {
26  public:
27   NS_DECL_THREADSAFE_ISUPPORTS
28   NS_DECL_NSIDASHBOARD
29   NS_DECL_NSIDASHBOARDEVENTNOTIFIER
30 
31   Dashboard();
32   static const char* GetErrorString(nsresult rv);
33   nsresult GetConnectionStatus(ConnectionData* aConnectionData);
34 
35  private:
36   struct LogData {
LogDataLogData37     LogData(nsCString host, uint32_t serial, bool encryption)
38         : mHost(host),
39           mSerial(serial),
40           mMsgSent(0),
41           mMsgReceived(0),
42           mSizeSent(0),
43           mSizeReceived(0),
44           mEncrypted(encryption) {}
45     nsCString mHost;
46     uint32_t mSerial;
47     uint32_t mMsgSent;
48     uint32_t mMsgReceived;
49     uint64_t mSizeSent;
50     uint64_t mSizeReceived;
51     bool mEncrypted;
52     bool operator==(const LogData& a) const {
53       return mHost.Equals(a.mHost) && (mSerial == a.mSerial);
54     }
55   };
56 
57   struct WebSocketData {
WebSocketDataWebSocketData58     WebSocketData() : lock("Dashboard.webSocketData") {}
IndexOfWebSocketData59     uint32_t IndexOf(const nsCString& hostname, uint32_t mSerial) {
60       LogData temp(hostname, mSerial, false);
61       return data.IndexOf(temp);
62     }
63     nsTArray<LogData> data;
64     mozilla::Mutex lock;
65   };
66 
67   bool mEnableLogging;
68   WebSocketData mWs;
69 
70  private:
71   virtual ~Dashboard() = default;
72 
73   nsresult GetSocketsDispatch(SocketData*);
74   nsresult GetHttpDispatch(HttpData*);
75   nsresult GetDnsInfoDispatch(DnsData*);
76   nsresult TestNewConnection(ConnectionData*);
77 
78   /* Helper methods that pass the JSON to the callback function. */
79   nsresult GetSockets(SocketData*);
80   nsresult GetHttpConnections(HttpData*);
81   nsresult GetDNSCacheEntries(DnsData*);
82   nsresult GetWebSocketConnections(WebSocketRequest*);
83   nsresult GetRcwnData(RcwnData*);
84 
85   nsCOMPtr<nsIDNSService> mDnsService;
86 };
87 
88 }  // namespace net
89 }  // namespace mozilla
90 
91 #endif  // nsDashboard_h__
92