DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (d38398e5144e)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
/* vim: set ts=2 sts=2 et sw=2: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */


 // Things to think about
 //  * do we need to be multithreaded, or is mt-only ok?

#include "ThrottlingService.h"

#include "nsIHttpChannel.h"
#include "nsIObserverService.h"

#include "mozilla/Preferences.h"
#include "mozilla/Services.h"

#include "mozilla/net/NeckoChild.h"

namespace mozilla {
namespace net{

static const char kEnabledPref[] = "network.throttle.enable";
static const bool kDefaultEnabled = true;
static const char kSuspendPeriodPref[] = "network.throttle.suspend-for";
static const uint32_t kDefaultSuspendPeriod = 2000;
static const char kResumePeriodPref[] = "network.throttle.resume-for";
static const uint32_t kDefaultResumePeriod = 2000;

NS_IMPL_ISUPPORTS(ThrottlingService, nsIThrottlingService, nsIObserver, nsITimerCallback)

ThrottlingService::ThrottlingService()
  :mEnabled(kDefaultEnabled)
  ,mInitCalled(false)
  ,mSuspended(false)
  ,mPressureCount(0)
  ,mSuspendPeriod(kDefaultSuspendPeriod)
  ,mResumePeriod(kDefaultResumePeriod)
  ,mIteratingHash(false)
{
}

ThrottlingService::~ThrottlingService()
{
  Shutdown();
}


nsresult
ThrottlingService::Init()
{
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(!mInitCalled);

  mInitCalled = true;

  nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
  if (!obs) {
    return NS_ERROR_UNEXPECTED;
  }

  nsresult rv = obs->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
  NS_ENSURE_SUCCESS(rv, rv);

  mEnabled = Preferences::GetBool(kEnabledPref, kDefaultEnabled);
  rv = Preferences::AddStrongObserver(this, kEnabledPref);
  NS_ENSURE_SUCCESS(rv, rv);
  Preferences::AddUintVarCache(&mSuspendPeriod, kSuspendPeriodPref, kDefaultSuspendPeriod);
  Preferences::AddUintVarCache(&mResumePeriod, kResumePeriodPref, kDefaultResumePeriod);

  mTimer = do_CreateInstance(NS_TIMER_CONTRACTID);

  return NS_OK;
}

void
ThrottlingService::Shutdown()
{
  if (!mInitCalled) {
    return;
  }

  if (mTimer) {
    mTimer->Cancel();
  }

  nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
  if (obs) {
    obs->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID);
  }

  Preferences::RemoveObserver(this, kEnabledPref);

  MaybeResumeAll();
  mChannelHash.Clear();
}

nsresult
ThrottlingService::Create(nsISupports *outer, const nsIID& iid, void **result)
{
  MOZ_ASSERT(NS_IsMainThread());

  if (outer != nullptr) {
    return NS_ERROR_NO_AGGREGATION;
  }

  RefPtr<ThrottlingService> svc = new ThrottlingService();
  if (!IsNeckoChild()) {
    // We only need to do any work on the parent, so only bother initializing
    // there. Child-side, we'll just error out since we only deal with parent
    // channels.)
    nsresult rv = svc->Init();
    NS_ENSURE_SUCCESS(rv, rv);
  }

  return svc->QueryInterface(iid, result);
}

// nsIThrottlingService

nsresult
ThrottlingService::AddChannel(nsIHttpChannel *channel)
{
  MOZ_ASSERT(NS_IsMainThread());

  // We don't check mEnabled, because we always want to put channels in the hash
  // to avoid potential inconsistencies in the case where the user changes the
  // enabled pref at run-time.

  if (IsNeckoChild()) {
    return NS_ERROR_NOT_AVAILABLE;
  }

  nsAutoCString strKey;
  nsresult rv = channel->GetChannelId(strKey);
  NS_ENSURE_SUCCESS(rv, rv);

  nsID key;
  if (!key.Parse(strKey.get())) {
    return NS_ERROR_UNEXPECTED;
  }

  if (mChannelHash.Get(key, nullptr)) {
    // We already have this channel under our control, not adding it again.
    MOZ_ASSERT(false, "Trying to throttle an already-throttled channel");
    return NS_ERROR_ILLEGAL_VALUE;
  }

  if (!mIteratingHash) {
    // This should be the common case, and as such is easy to handle
    mChannelHash.Put(key, channel);

    if (mSuspended) {
      channel->Suspend();
    }
  } else {
    // This gets tricky - we've somehow re-entrantly gotten here through the
    // hash iteration in one of MaybeSuspendAll or MaybeResumeAll. Keep track
    // of the fact that this add came in now, and once we're done iterating, we
    // can add this into the hash. This avoids unexpectedly modifying the hash
    // while it's being iterated over, which could lead to inconsistencies.
    mChannelsToAddRemove.AppendElement(channel);
    mChannelIsAdd.AppendElement(true);
  }

  return NS_OK;
}

nsresult
ThrottlingService::RemoveChannel(nsIHttpChannel *channel)
{
  MOZ_ASSERT(NS_IsMainThread());

  // Just like above, don't worry about mEnabled to avoid inconsistencies when
  // the pref changes at run-time

  if (IsNeckoChild()) {
    return NS_ERROR_NOT_AVAILABLE;
  }

  nsAutoCString strKey;
  nsresult rv = channel->GetChannelId(strKey);
  NS_ENSURE_SUCCESS(rv, rv);

  nsID key;
  if (!key.Parse(strKey.get())) {
    return NS_ERROR_UNEXPECTED;
  }

  if (!mChannelHash.Get(key, nullptr)) {
    // TODO - warn?
    return NS_ERROR_ILLEGAL_VALUE;
  }

  if (!mIteratingHash) {
    // This should be the common case, and easy to handle.
    mChannelHash.Remove(key);

    if (mSuspended) {
      // This channel is no longer under our control for suspend/resume, but
      // we've suspended it. Time to let it go.
      channel->Resume();
    }
  } else {
    // This gets tricky - we've somehow re-entrantly gotten here through the
    // hash iteration in one of MaybeSuspendAll or MaybeResumeAll. Keep track
    // of the fact that this add came in now, and once we're done iterating, we
    // can add this into the hash. This avoids unexpectedly modifying the hash
    // while it's being iterated over, which could lead to inconsistencies.
    mChannelsToAddRemove.AppendElement(channel);
    mChannelIsAdd.AppendElement(false);
  }

  return NS_OK;
}

nsresult
ThrottlingService::IncreasePressure()
{
  MOZ_ASSERT(NS_IsMainThread());

  // Just like add/removing channels, we don't check mEnabled here in order to
  // avoid inconsistencies that could occur if the pref is flipped at runtime

  if (IsNeckoChild()) {
    return NS_ERROR_NOT_AVAILABLE;
  }

  if (mPressureCount++ == 0) {
    MOZ_ASSERT(!mSuspended, "Suspended with 0 pressure?");
    MaybeSuspendAll();
    if (mSuspended) {
      // MaybeSuspendAll() may not actually suspend things, and we only want to
      // bother setting a timer to resume if we actually suspended.
      mTimer->InitWithCallback(this, mSuspendPeriod, nsITimer::TYPE_ONE_SHOT);
    }
  }

  return NS_OK;
}

nsresult
ThrottlingService::DecreasePressure()
{
  MOZ_ASSERT(NS_IsMainThread());

  // Just like add/removing channels, we don't check mEnabled here in order to
  // avoid inconsistencies that could occur if the pref is flipped at runtime

  if (IsNeckoChild()) {
    return NS_ERROR_NOT_AVAILABLE;
  }

  MOZ_ASSERT(mPressureCount > 0, "Unbalanced throttle pressure");

  if (--mPressureCount == 0) {
    MaybeResumeAll();
    mTimer->Cancel();
  }

  return NS_OK;
}

// nsIObserver

nsresult
ThrottlingService::Observe(nsISupports *subject, const char *topic,
                           const char16_t *data_unicode)
{
  MOZ_ASSERT(!IsNeckoChild());
  MOZ_ASSERT(NS_IsMainThread());

  if (!strcmp(NS_XPCOM_SHUTDOWN_OBSERVER_ID, topic)) {
    Shutdown();
  } else if (!strcmp("nsPref:changed", topic)) {
    mEnabled = Preferences::GetBool(kEnabledPref, mEnabled);
    if (mEnabled && mPressureCount) {
      // We weren't enabled, but we are now, AND we're under pressure. Go ahead
      // and suspend things.
      MaybeSuspendAll();
      if (mSuspended) {
        mTimer->InitWithCallback(this, mSuspendPeriod, nsITimer::TYPE_ONE_SHOT);
      }
    } else if (!mEnabled) {
      // We were enabled, but we aren't any longer. Make sure we aren't
      // suspending channels and that we don't have any timer that wants to
      // change things unexpectedly.
      mTimer->Cancel();
      MaybeResumeAll();
    }
  }

  return NS_OK;
}

// nsITimerCallback

nsresult
ThrottlingService::Notify(nsITimer *timer)
{
  MOZ_ASSERT(!IsNeckoChild());
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(timer == mTimer);

  if (mSuspended) {
    MaybeResumeAll();
    // Always try to resume if we were suspended, but only time-limit the
    // resumption if we're under pressure and we're enabled. If either of those
    // conditions is false, it doesn't make any sense to set a timer to suspend
    // things when we don't want to be suspended anyway.
    if (mPressureCount && mEnabled) {
      mTimer->InitWithCallback(this, mResumePeriod, nsITimer::TYPE_ONE_SHOT);
    }
  } else if (mPressureCount) {
    MaybeSuspendAll();
    if (mSuspended) {
      // MaybeSuspendAll() may not actually suspend, and it only makes sense to
      // set a timer to resume if we actually suspended the channels.
      mTimer->InitWithCallback(this, mSuspendPeriod, nsITimer::TYPE_ONE_SHOT);
    }
  }

  return NS_OK;
}

// Internal methods

void
ThrottlingService::MaybeSuspendAll()
{
  MOZ_ASSERT(!IsNeckoChild());
  MOZ_ASSERT(NS_IsMainThread());

  if (!mEnabled) {
    // We don't actually suspend when disabled, even though it's possible we get
    // called in that state in order to avoid inconsistencies in the hash and
    // the count if the pref changes at runtime.
    return;
  }

  if (mSuspended) {
    // Already suspended, nothing to do!
    return;
  }
  mSuspended = true;

  IterateHash([](ChannelHash::Iterator &iter) -> void {
    const nsCOMPtr<nsIHttpChannel> channel = iter.UserData();
    channel->Suspend();
  });
}

void
ThrottlingService::MaybeResumeAll()
{
  MOZ_ASSERT(!IsNeckoChild());
  MOZ_ASSERT(NS_IsMainThread());

  if (!mSuspended) {
    // Already resumed, nothing to do!
    return;
  }
  mSuspended = false;

  IterateHash([](ChannelHash::Iterator &iter) -> void {
    const nsCOMPtr<nsIHttpChannel> channel = iter.UserData();
    channel->Resume();
  });
}

void
ThrottlingService::IterateHash(void (* callback)(ChannelHash::Iterator &iter))
{
  MOZ_ASSERT(!mIteratingHash);
  mIteratingHash = true;
  for (ChannelHash::Iterator iter = mChannelHash.ConstIter(); !iter.Done(); iter.Next()) {
    callback(iter);
  }
  mIteratingHash = false;
  HandleExtraAddRemove();
}

void
ThrottlingService::HandleExtraAddRemove()
{
  MOZ_ASSERT(!mIteratingHash);
  MOZ_ASSERT(mChannelsToAddRemove.Length() == mChannelIsAdd.Length());

  nsCOMArray<nsIHttpChannel> channelsToAddRemove;
  channelsToAddRemove.SwapElements(mChannelsToAddRemove);

  nsTArray<bool> channelIsAdd;
  channelIsAdd.SwapElements(mChannelIsAdd);

  for (size_t i = 0; i < channelsToAddRemove.Length(); ++i) {
    if (channelIsAdd[i]) {
      AddChannel(channelsToAddRemove[i]);
    } else {
      RemoveChannel(channelsToAddRemove[i]);
    }
  }

  channelsToAddRemove.Clear();
  channelIsAdd.Clear();
}

// The publicly available way to throttle things

Throttler::Throttler()
{
  MOZ_ASSERT(NS_IsMainThread());
  if (IsNeckoChild()) {
    if (gNeckoChild) {
      // The child object may have already gone away, so we need to guard
      // guard against deref'ing a nullptr here. If that's what happened, then
      // our pageload won't be continuing anyway, so what we do is pretty much
      // irrelevant.
      gNeckoChild->SendIncreaseThrottlePressure();
    }
  } else {
    mThrottlingService = do_GetService("@mozilla.org/network/throttling-service;1");
    mThrottlingService->IncreasePressure();
  }
}

Throttler::~Throttler()
{
  MOZ_ASSERT(NS_IsMainThread());
  if (IsNeckoChild()) {
    if (gNeckoChild) {
      // The child object may have already gone away, so we need to guard
      // guard against deref'ing a nullptr here. If that's what happened, then
      // NeckoParent::ActorDestroy will take care of releasing the pressure we
      // created.
      gNeckoChild->SendDecreaseThrottlePressure();
    }
  } else {
    MOZ_RELEASE_ASSERT(mThrottlingService);
    mThrottlingService->DecreasePressure();
    mThrottlingService = nullptr;
  }
}

}
}