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 "nsTHashMap.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 using Activities = nsTHashMap<nsCStringHashKey, dom::IOActivityDataDictionary>; 33 34 // IOActivityMonitor has several roles: 35 // - maintains an IOActivity per resource and updates it 36 // - sends a dump of the activities to a promise via RequestActivities 37 class IOActivityMonitor final : public nsINamed { 38 public: 39 IOActivityMonitor(); 40 41 NS_DECL_THREADSAFE_ISUPPORTS 42 NS_DECL_NSINAMED 43 44 // initializes and destroys the singleton 45 static nsresult Init(); 46 static nsresult Shutdown(); 47 48 // collect amounts of data that are written/read by location 49 static nsresult Read(const nsACString& location, uint32_t aAmount); 50 static nsresult Write(const nsACString& location, uint32_t aAmount); 51 52 static nsresult MonitorFile(PRFileDesc* aFd, const char* aPath); 53 static nsresult MonitorSocket(PRFileDesc* aFd); 54 static nsresult Read(PRFileDesc* fd, uint32_t aAmount); 55 static nsresult Write(PRFileDesc* fd, uint32_t aAmount); 56 57 static bool IsActive(); 58 static void RequestActivities(dom::Promise* aPromise); 59 60 private: 61 ~IOActivityMonitor() = default; 62 nsresult InitInternal(); 63 nsresult ShutdownInternal(); 64 bool IncrementActivity(const nsACString& location, uint32_t aRx, 65 uint32_t aTx); 66 nsresult WriteInternal(const nsACString& location, uint32_t aAmount); 67 nsresult ReadInternal(const nsACString& location, uint32_t aAmount); 68 void RequestActivitiesInternal(dom::Promise* aPromise); 69 70 Activities mActivities; 71 // protects mActivities accesses 72 Mutex mLock; 73 }; 74 75 } // namespace net 76 } // namespace mozilla 77 78 #endif /* IOActivityMonitor_h___ */ 79