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 (c68fe15a81fc)

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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

#include "ServiceWorkerShutdownBlocker.h"

#include <chrono>
#include <utility>

#include "MainThreadUtils.h"
#include "nsDebug.h"
#include "nsError.h"
#include "nsIWritablePropertyBag2.h"
#include "nsThreadUtils.h"

#include "mozilla/Assertions.h"
#include "mozilla/RefPtr.h"

namespace mozilla {
namespace dom {

NS_IMPL_ISUPPORTS(ServiceWorkerShutdownBlocker, nsIAsyncShutdownBlocker)

NS_IMETHODIMP ServiceWorkerShutdownBlocker::GetName(nsAString& aNameOut) {
  aNameOut = NS_LITERAL_STRING(
      "ServiceWorkerShutdownBlocker: shutting down Service Workers");
  return NS_OK;
}

NS_IMETHODIMP
ServiceWorkerShutdownBlocker::BlockShutdown(nsIAsyncShutdownClient* aClient) {
  AssertIsOnMainThread();
  MOZ_ASSERT(!mShutdownClient);

  mShutdownClient = aClient;

  MaybeUnblockShutdown();
  MaybeInitUnblockShutdownTimer();

  return NS_OK;
}

NS_IMETHODIMP ServiceWorkerShutdownBlocker::GetState(nsIPropertyBag** aBagOut) {
  AssertIsOnMainThread();
  MOZ_ASSERT(aBagOut);

  nsCOMPtr<nsIWritablePropertyBag2> propertyBag =
      do_CreateInstance("@mozilla.org/hash-property-bag;1");

  if (NS_WARN_IF(!propertyBag)) {
    return NS_ERROR_OUT_OF_MEMORY;
  }

  nsresult rv = propertyBag->SetPropertyAsBool(
      NS_LITERAL_STRING("acceptingPromises"), IsAcceptingPromises());

  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }

  rv = propertyBag->SetPropertyAsUint32(NS_LITERAL_STRING("pendingPromises"),
                                        GetPendingPromises());

  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }

  nsAutoCString shutdownStates;

  for (auto iter = mShutdownStates.iter(); !iter.done(); iter.next()) {
    shutdownStates.Append(iter.get().value().GetProgressString());
    shutdownStates.Append(", ");
  }

  rv = propertyBag->SetPropertyAsACString(NS_LITERAL_STRING("shutdownStates"),
                                          shutdownStates);

  if (NS_WARN_IF(NS_FAILED(rv))) {
    return rv;
  }

  propertyBag.forget(aBagOut);

  return NS_OK;
}

/* static */ already_AddRefed<ServiceWorkerShutdownBlocker>
ServiceWorkerShutdownBlocker::CreateAndRegisterOn(
    nsIAsyncShutdownClient* aShutdownBarrier) {
  AssertIsOnMainThread();
  MOZ_ASSERT(aShutdownBarrier);

  RefPtr<ServiceWorkerShutdownBlocker> blocker =
      new ServiceWorkerShutdownBlocker();

  nsresult rv = aShutdownBarrier->AddBlocker(
      blocker.get(), NS_LITERAL_STRING(__FILE__), __LINE__,
      NS_LITERAL_STRING("Service Workers shutdown"));

  if (NS_WARN_IF(NS_FAILED(rv))) {
    return nullptr;
  }

  return blocker.forget();
}

void ServiceWorkerShutdownBlocker::WaitOnPromise(
    GenericNonExclusivePromise* aPromise, uint32_t aShutdownStateId) {
  AssertIsOnMainThread();
  MOZ_DIAGNOSTIC_ASSERT(IsAcceptingPromises());
  MOZ_ASSERT(aPromise);
  MOZ_ASSERT(mShutdownStates.has(aShutdownStateId));

  ++mState.as<AcceptingPromises>().mPendingPromises;

  RefPtr<ServiceWorkerShutdownBlocker> self = this;

  aPromise->Then(GetCurrentThreadSerialEventTarget(), __func__,
                 [self = std::move(self), shutdownStateId = aShutdownStateId](
                     const GenericNonExclusivePromise::ResolveOrRejectValue&) {
                   // Progress reporting might race with aPromise settling.
                   self->mShutdownStates.remove(shutdownStateId);

                   if (!self->PromiseSettled()) {
                     self->MaybeUnblockShutdown();
                   }
                 });
}

void ServiceWorkerShutdownBlocker::StopAcceptingPromises() {
  AssertIsOnMainThread();
  MOZ_ASSERT(IsAcceptingPromises());

  mState = AsVariant(NotAcceptingPromises(mState.as<AcceptingPromises>()));

  MaybeUnblockShutdown();
  MaybeInitUnblockShutdownTimer();
}

uint32_t ServiceWorkerShutdownBlocker::CreateShutdownState() {
  AssertIsOnMainThread();

  static uint32_t nextShutdownStateId = 1;

  MOZ_ALWAYS_TRUE(mShutdownStates.putNew(nextShutdownStateId,
                                         ServiceWorkerShutdownState()));

  return nextShutdownStateId++;
}

void ServiceWorkerShutdownBlocker::ReportShutdownProgress(
    uint32_t aShutdownStateId, Progress aProgress) {
  AssertIsOnMainThread();
  MOZ_RELEASE_ASSERT(aShutdownStateId != kInvalidShutdownStateId);

  auto lookup = mShutdownStates.lookup(aShutdownStateId);

  // Progress reporting might race with the promise that WaitOnPromise is called
  // with settling.
  if (!lookup) {
    return;
  }

  // This will check for a valid progress transition with assertions.
  lookup->value().SetProgress(aProgress);

  if (aProgress == Progress::ShutdownCompleted) {
    mShutdownStates.remove(lookup);
  }
}

ServiceWorkerShutdownBlocker::ServiceWorkerShutdownBlocker()
    : mState(VariantType<AcceptingPromises>()) {
  AssertIsOnMainThread();
}

ServiceWorkerShutdownBlocker::~ServiceWorkerShutdownBlocker() {
  MOZ_ASSERT(!IsAcceptingPromises());
  MOZ_ASSERT(!GetPendingPromises());
  MOZ_ASSERT(!mShutdownClient);
}

void ServiceWorkerShutdownBlocker::MaybeUnblockShutdown() {
  AssertIsOnMainThread();

  if (!mShutdownClient || IsAcceptingPromises() || GetPendingPromises()) {
    return;
  }

  UnblockShutdown();
}

void ServiceWorkerShutdownBlocker::UnblockShutdown() {
  MOZ_ASSERT(mShutdownClient);

  mShutdownClient->RemoveBlocker(this);
  mShutdownClient = nullptr;

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

uint32_t ServiceWorkerShutdownBlocker::PromiseSettled() {
  AssertIsOnMainThread();
  MOZ_ASSERT(GetPendingPromises());

  if (IsAcceptingPromises()) {
    return --mState.as<AcceptingPromises>().mPendingPromises;
  }

  return --mState.as<NotAcceptingPromises>().mPendingPromises;
}

bool ServiceWorkerShutdownBlocker::IsAcceptingPromises() const {
  AssertIsOnMainThread();

  return mState.is<AcceptingPromises>();
}

uint32_t ServiceWorkerShutdownBlocker::GetPendingPromises() const {
  AssertIsOnMainThread();

  if (IsAcceptingPromises()) {
    return mState.as<AcceptingPromises>().mPendingPromises;
  }

  return mState.as<NotAcceptingPromises>().mPendingPromises;
}

ServiceWorkerShutdownBlocker::NotAcceptingPromises::NotAcceptingPromises(
    AcceptingPromises aPreviousState)
    : mPendingPromises(aPreviousState.mPendingPromises) {
  AssertIsOnMainThread();
}

NS_IMETHODIMP ServiceWorkerShutdownBlocker::Notify(nsITimer*) {
  // TODO: this method being called indicates that there are ServiceWorkers
  // that did not complete shutdown before the timer expired - there should be
  // a telemetry ping or some other way of recording the state of when this
  // happens (e.g. what's returned by GetState()).
  UnblockShutdown();
  return NS_OK;
}

void ServiceWorkerShutdownBlocker::MaybeInitUnblockShutdownTimer() {
#ifdef RELEASE_OR_BETA
  AssertIsOnMainThread();
  MOZ_ASSERT(!mTimer);

  if (!mShutdownClient || IsAcceptingPromises()) {
    return;
  }

  MOZ_ASSERT(GetPendingPromises(),
             "Shouldn't be blocking shutdown with zero pending promises.");

  using namespace std::chrono_literals;

  static constexpr auto delay =
      std::chrono::duration_cast<std::chrono::milliseconds>(10s);

  mTimer = NS_NewTimer();

  mTimer->InitWithCallback(this, delay.count(), nsITimer::TYPE_ONE_SHOT);
#endif
}

}  // namespace dom
}  // namespace mozilla