1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  *
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef IOActivityMonitor_h___
8 #define IOActivityMonitor_h___
9 
10 #include "mozilla/dom/ChromeUtilsBinding.h"
11 #include "nsCOMPtr.h"
12 #include "nscore.h"
13 #include "nsClassHashtable.h"
14 #include "nsDataHashtable.h"
15 #include "nsHashKeys.h"
16 #include "nsISupports.h"
17 #include "prinrval.h"
18 #include "prio.h"
19 #include "private/pprio.h"
20 #include <stdint.h>
21 
22 namespace mozilla {
23 
24 namespace dom {
25 class Promise;
26 }
27 
28 namespace net {
29 
30 #define IO_ACTIVITY_ENABLED_PREF "io.activity.enabled"
31 
32 typedef nsDataHashtable<nsCStringHashKey, dom::IOActivityDataDictionary>
33     Activities;
34 
35 // IOActivityMonitor has several roles:
36 // - maintains an IOActivity per resource and updates it
37 // - sends a dump of the activities to a promise via RequestActivities
38 class IOActivityMonitor final : public nsINamed {
39  public:
40   IOActivityMonitor();
41 
42   NS_DECL_THREADSAFE_ISUPPORTS
43   NS_DECL_NSINAMED
44 
45   // initializes and destroys the singleton
46   static nsresult Init();
47   static nsresult Shutdown();
48 
49   // collect amounts of data that are written/read by location
50   static nsresult Read(const nsACString& location, uint32_t aAmount);
51   static nsresult Write(const nsACString& location, uint32_t aAmount);
52 
53   static nsresult MonitorFile(PRFileDesc* aFd, const char* aPath);
54   static nsresult MonitorSocket(PRFileDesc* aFd);
55   static nsresult Read(PRFileDesc* fd, uint32_t aAmount);
56   static nsresult Write(PRFileDesc* fd, uint32_t aAmount);
57 
58   static bool IsActive();
59   static void RequestActivities(dom::Promise* aPromise);
60 
61  private:
62   ~IOActivityMonitor() = default;
63   nsresult InitInternal();
64   nsresult ShutdownInternal();
65   bool IncrementActivity(const nsACString& location, uint32_t aRx,
66                          uint32_t aTx);
67   nsresult WriteInternal(const nsACString& location, uint32_t aAmount);
68   nsresult ReadInternal(const nsACString& location, uint32_t aAmount);
69   void RequestActivitiesInternal(dom::Promise* aPromise);
70 
71   Activities mActivities;
72   // protects mActivities accesses
73   Mutex mLock;
74 };
75 
76 }  // namespace net
77 }  // namespace mozilla
78 
79 #endif /* IOActivityMonitor_h___ */
80