netwerk/base/src/NetworkActivityMonitor.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 *
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "NetworkActivityMonitor.h"
michael@0 8 #include "prmem.h"
michael@0 9 #include "nsIObserverService.h"
michael@0 10 #include "nsPISocketTransportService.h"
michael@0 11 #include "nsSocketTransportService2.h"
michael@0 12 #include "nsThreadUtils.h"
michael@0 13 #include "mozilla/Services.h"
michael@0 14 #include "prerror.h"
michael@0 15
michael@0 16 using namespace mozilla::net;
michael@0 17
michael@0 18 static PRStatus
michael@0 19 nsNetMon_Connect(PRFileDesc *fd, const PRNetAddr *addr, PRIntervalTime timeout)
michael@0 20 {
michael@0 21 PRStatus ret;
michael@0 22 PRErrorCode code;
michael@0 23 ret = fd->lower->methods->connect(fd->lower, addr, timeout);
michael@0 24 if (ret == PR_SUCCESS || (code = PR_GetError()) == PR_WOULD_BLOCK_ERROR ||
michael@0 25 code == PR_IN_PROGRESS_ERROR)
michael@0 26 NetworkActivityMonitor::DataInOut(NetworkActivityMonitor::kUpload);
michael@0 27 return ret;
michael@0 28 }
michael@0 29
michael@0 30 static int32_t
michael@0 31 nsNetMon_Read(PRFileDesc *fd, void *buf, int32_t len)
michael@0 32 {
michael@0 33 int32_t ret;
michael@0 34 ret = fd->lower->methods->read(fd->lower, buf, len);
michael@0 35 if (ret >= 0)
michael@0 36 NetworkActivityMonitor::DataInOut(NetworkActivityMonitor::kDownload);
michael@0 37 return ret;
michael@0 38 }
michael@0 39
michael@0 40 static int32_t
michael@0 41 nsNetMon_Write(PRFileDesc *fd, const void *buf, int32_t len)
michael@0 42 {
michael@0 43 int32_t ret;
michael@0 44 ret = fd->lower->methods->write(fd->lower, buf, len);
michael@0 45 if (ret > 0)
michael@0 46 NetworkActivityMonitor::DataInOut(NetworkActivityMonitor::kUpload);
michael@0 47 return ret;
michael@0 48 }
michael@0 49
michael@0 50 static int32_t
michael@0 51 nsNetMon_Writev(PRFileDesc *fd,
michael@0 52 const PRIOVec *iov,
michael@0 53 int32_t size,
michael@0 54 PRIntervalTime timeout)
michael@0 55 {
michael@0 56 int32_t ret;
michael@0 57 ret = fd->lower->methods->writev(fd->lower, iov, size, timeout);
michael@0 58 if (ret > 0)
michael@0 59 NetworkActivityMonitor::DataInOut(NetworkActivityMonitor::kUpload);
michael@0 60 return ret;
michael@0 61 }
michael@0 62
michael@0 63 static int32_t
michael@0 64 nsNetMon_Recv(PRFileDesc *fd,
michael@0 65 void *buf,
michael@0 66 int32_t amount,
michael@0 67 int flags,
michael@0 68 PRIntervalTime timeout)
michael@0 69 {
michael@0 70 int32_t ret;
michael@0 71 ret = fd->lower->methods->recv(fd->lower, buf, amount, flags, timeout);
michael@0 72 if (ret >= 0)
michael@0 73 NetworkActivityMonitor::DataInOut(NetworkActivityMonitor::kDownload);
michael@0 74 return ret;
michael@0 75 }
michael@0 76
michael@0 77 static int32_t
michael@0 78 nsNetMon_Send(PRFileDesc *fd,
michael@0 79 const void *buf,
michael@0 80 int32_t amount,
michael@0 81 int flags,
michael@0 82 PRIntervalTime timeout)
michael@0 83 {
michael@0 84 int32_t ret;
michael@0 85 ret = fd->lower->methods->send(fd->lower, buf, amount, flags, timeout);
michael@0 86 if (ret > 0)
michael@0 87 NetworkActivityMonitor::DataInOut(NetworkActivityMonitor::kUpload);
michael@0 88 return ret;
michael@0 89 }
michael@0 90
michael@0 91 static int32_t
michael@0 92 nsNetMon_RecvFrom(PRFileDesc *fd,
michael@0 93 void *buf,
michael@0 94 int32_t amount,
michael@0 95 int flags,
michael@0 96 PRNetAddr *addr,
michael@0 97 PRIntervalTime timeout)
michael@0 98 {
michael@0 99 int32_t ret;
michael@0 100 ret = fd->lower->methods->recvfrom(fd->lower,
michael@0 101 buf,
michael@0 102 amount,
michael@0 103 flags,
michael@0 104 addr,
michael@0 105 timeout);
michael@0 106 if (ret >= 0)
michael@0 107 NetworkActivityMonitor::DataInOut(NetworkActivityMonitor::kDownload);
michael@0 108 return ret;
michael@0 109 }
michael@0 110
michael@0 111 static int32_t
michael@0 112 nsNetMon_SendTo(PRFileDesc *fd,
michael@0 113 const void *buf,
michael@0 114 int32_t amount,
michael@0 115 int flags,
michael@0 116 const PRNetAddr *addr,
michael@0 117 PRIntervalTime timeout)
michael@0 118 {
michael@0 119 int32_t ret;
michael@0 120 ret = fd->lower->methods->sendto(fd->lower,
michael@0 121 buf,
michael@0 122 amount,
michael@0 123 flags,
michael@0 124 addr,
michael@0 125 timeout);
michael@0 126 if (ret > 0)
michael@0 127 NetworkActivityMonitor::DataInOut(NetworkActivityMonitor::kUpload);
michael@0 128 return ret;
michael@0 129 }
michael@0 130
michael@0 131 static int32_t
michael@0 132 nsNetMon_AcceptRead(PRFileDesc *listenSock,
michael@0 133 PRFileDesc **acceptedSock,
michael@0 134 PRNetAddr **peerAddr,
michael@0 135 void *buf,
michael@0 136 int32_t amount,
michael@0 137 PRIntervalTime timeout)
michael@0 138 {
michael@0 139 int32_t ret;
michael@0 140 ret = listenSock->lower->methods->acceptread(listenSock->lower,
michael@0 141 acceptedSock,
michael@0 142 peerAddr,
michael@0 143 buf,
michael@0 144 amount,
michael@0 145 timeout);
michael@0 146 if (ret > 0)
michael@0 147 NetworkActivityMonitor::DataInOut(NetworkActivityMonitor::kDownload);
michael@0 148 return ret;
michael@0 149 }
michael@0 150
michael@0 151
michael@0 152 class NotifyNetworkActivity : public nsRunnable {
michael@0 153 public:
michael@0 154 NotifyNetworkActivity(NetworkActivityMonitor::Direction aDirection)
michael@0 155 : mDirection(aDirection)
michael@0 156 {}
michael@0 157 NS_IMETHOD Run()
michael@0 158 {
michael@0 159 MOZ_ASSERT(NS_IsMainThread());
michael@0 160
michael@0 161 nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
michael@0 162 if (!obs)
michael@0 163 return NS_ERROR_FAILURE;
michael@0 164
michael@0 165 obs->NotifyObservers(nullptr,
michael@0 166 mDirection == NetworkActivityMonitor::kUpload
michael@0 167 ? NS_NETWORK_ACTIVITY_BLIP_UPLOAD_TOPIC
michael@0 168 : NS_NETWORK_ACTIVITY_BLIP_DOWNLOAD_TOPIC,
michael@0 169 nullptr);
michael@0 170 return NS_OK;
michael@0 171 }
michael@0 172 private:
michael@0 173 nsCOMPtr<nsIObserverService> mObs;
michael@0 174 NetworkActivityMonitor::Direction mDirection;
michael@0 175 };
michael@0 176
michael@0 177 NetworkActivityMonitor * NetworkActivityMonitor::gInstance = nullptr;
michael@0 178
michael@0 179 NetworkActivityMonitor::NetworkActivityMonitor()
michael@0 180 : mLayerIdentity(PR_INVALID_IO_LAYER)
michael@0 181 , mBlipInterval(PR_INTERVAL_NO_TIMEOUT)
michael@0 182 {
michael@0 183 MOZ_COUNT_CTOR(NetworkActivityMonitor);
michael@0 184
michael@0 185 NS_ASSERTION(gInstance==nullptr,
michael@0 186 "multiple NetworkActivityMonitor instances!");
michael@0 187 }
michael@0 188
michael@0 189 NetworkActivityMonitor::~NetworkActivityMonitor()
michael@0 190 {
michael@0 191 MOZ_COUNT_DTOR(NetworkActivityMonitor);
michael@0 192 gInstance = nullptr;
michael@0 193 }
michael@0 194
michael@0 195 nsresult
michael@0 196 NetworkActivityMonitor::Init(int32_t blipInterval)
michael@0 197 {
michael@0 198 nsresult rv;
michael@0 199
michael@0 200 if (gInstance)
michael@0 201 return NS_ERROR_ALREADY_INITIALIZED;
michael@0 202
michael@0 203 NetworkActivityMonitor * mon = new NetworkActivityMonitor();
michael@0 204 rv = mon->Init_Internal(blipInterval);
michael@0 205 if (NS_FAILED(rv)) {
michael@0 206 delete mon;
michael@0 207 return rv;
michael@0 208 }
michael@0 209
michael@0 210 gInstance = mon;
michael@0 211 return NS_OK;
michael@0 212 }
michael@0 213
michael@0 214 nsresult
michael@0 215 NetworkActivityMonitor::Shutdown()
michael@0 216 {
michael@0 217 if (!gInstance)
michael@0 218 return NS_ERROR_NOT_INITIALIZED;
michael@0 219
michael@0 220 delete gInstance;
michael@0 221 return NS_OK;
michael@0 222 }
michael@0 223
michael@0 224 nsresult
michael@0 225 NetworkActivityMonitor::Init_Internal(int32_t blipInterval)
michael@0 226 {
michael@0 227 mLayerIdentity = PR_GetUniqueIdentity("network activity monitor layer");
michael@0 228 mLayerMethods = *PR_GetDefaultIOMethods();
michael@0 229 mLayerMethods.connect = nsNetMon_Connect;
michael@0 230 mLayerMethods.read = nsNetMon_Read;
michael@0 231 mLayerMethods.write = nsNetMon_Write;
michael@0 232 mLayerMethods.writev = nsNetMon_Writev;
michael@0 233 mLayerMethods.recv = nsNetMon_Recv;
michael@0 234 mLayerMethods.send = nsNetMon_Send;
michael@0 235 mLayerMethods.recvfrom = nsNetMon_RecvFrom;
michael@0 236 mLayerMethods.sendto = nsNetMon_SendTo;
michael@0 237 mLayerMethods.acceptread = nsNetMon_AcceptRead;
michael@0 238
michael@0 239 mBlipInterval = PR_MillisecondsToInterval(blipInterval);
michael@0 240 // Set the last notification times to time that has just expired, so any
michael@0 241 // activity even right now will trigger notification.
michael@0 242 mLastNotificationTime[kUpload] = PR_IntervalNow() - mBlipInterval;
michael@0 243 mLastNotificationTime[kDownload] = mLastNotificationTime[kUpload];
michael@0 244
michael@0 245 return NS_OK;
michael@0 246 }
michael@0 247
michael@0 248 nsresult
michael@0 249 NetworkActivityMonitor::AttachIOLayer(PRFileDesc *fd)
michael@0 250 {
michael@0 251 if (!gInstance)
michael@0 252 return NS_OK;
michael@0 253
michael@0 254 PRFileDesc * layer;
michael@0 255 PRStatus status;
michael@0 256
michael@0 257 layer = PR_CreateIOLayerStub(gInstance->mLayerIdentity,
michael@0 258 &gInstance->mLayerMethods);
michael@0 259 if (!layer) {
michael@0 260 return NS_ERROR_FAILURE;
michael@0 261 }
michael@0 262
michael@0 263 status = PR_PushIOLayer(fd, PR_NSPR_IO_LAYER, layer);
michael@0 264
michael@0 265 if (status == PR_FAILURE) {
michael@0 266 PR_DELETE(layer);
michael@0 267 return NS_ERROR_FAILURE;
michael@0 268 }
michael@0 269
michael@0 270 return NS_OK;
michael@0 271 }
michael@0 272
michael@0 273 nsresult
michael@0 274 NetworkActivityMonitor::DataInOut(Direction direction)
michael@0 275 {
michael@0 276 NS_ASSERTION(PR_GetCurrentThread() == gSocketThread, "wrong thread");
michael@0 277
michael@0 278 if (gInstance) {
michael@0 279 PRIntervalTime now = PR_IntervalNow();
michael@0 280 if ((now - gInstance->mLastNotificationTime[direction]) >
michael@0 281 gInstance->mBlipInterval) {
michael@0 282 gInstance->mLastNotificationTime[direction] = now;
michael@0 283 gInstance->PostNotification(direction);
michael@0 284 }
michael@0 285 }
michael@0 286
michael@0 287 return NS_OK;
michael@0 288 }
michael@0 289
michael@0 290 void
michael@0 291 NetworkActivityMonitor::PostNotification(Direction direction)
michael@0 292 {
michael@0 293 nsRefPtr<nsIRunnable> ev = new NotifyNetworkActivity(direction);
michael@0 294 NS_DispatchToMainThread(ev);
michael@0 295 }

mercurial