dom/workers/WorkerHolderToken.h
author Nicolas B. Pierron <nicolas.b.pierron@nbp.name>
Wed, 24 Oct 2018 15:07:53 +0200
changeset 443125 1bd322732a6e67a0caab2e0bf26ff0b20f01fc1a
parent 401687 22ab83c80c5e38ab1b32b7e8186825ecd855ee1f
child 448947 6f3709b3878117466168c40affa7bca0b60cf75b
permissions -rw-r--r--
Bug 1502106 - Add a configure option to disable assume_unreachable spew. r=iain

/* -*- 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/. */

#ifndef mozilla_dom_workers_WorkerHolderToken_h
#define mozilla_dom_workers_WorkerHolderToken_h

#include "nsISupportsImpl.h"
#include "nsTObserverArray.h"
#include "WorkerHolder.h"

namespace mozilla {
namespace dom {

class WorkerPrivate;

// This is a ref-counted WorkerHolder implementation.  If you wish
// to be notified of worker shutdown beginning, then you can implement
// the Listener interface and call AddListener().
//
// This is purely a convenience class to avoid requiring code to
// extend WorkerHolder all the time.
class WorkerHolderToken final : public WorkerHolder
{
public:
  // Pure virtual class defining the interface for objects that
  // wish to be notified of worker shutdown.
  class Listener
  {
  public:
    virtual void
    WorkerShuttingDown() = 0;
  };

  // Attempt to create a WorkerHolderToken().  If the shutdown has already
  // passed the given shutdown phase or fails for another reason then
  // nullptr is returned.
  static already_AddRefed<WorkerHolderToken>
  Create(WorkerPrivate* aWorkerPrivate,
         WorkerStatus aShutdownStatus,
         Behavior aBehavior = PreventIdleShutdownStart);

  // Add a listener to the token.  Note, this does not hold a strong
  // reference to the listener.  You must call RemoveListener() before
  // the listener is destroyed.  This can only be called on the owning
  // worker thread.
  void
  AddListener(Listener* aListener);

  // Remove a previously added listener.  This can only be called on the
  // owning worker thread.
  void
  RemoveListener(Listener* aListener);

  bool
  IsShuttingDown() const;

  WorkerPrivate*
  GetWorkerPrivate() const;

private:
  WorkerHolderToken(WorkerStatus aShutdownStatus, Behavior aBehavior);

  ~WorkerHolderToken();

  // WorkerHolder methods
  virtual bool
  Notify(WorkerStatus aStatus) override;

  nsTObserverArray<Listener*> mListenerList;
  const WorkerStatus mShutdownStatus;
  bool mShuttingDown;

public:
  NS_INLINE_DECL_REFCOUNTING(WorkerHolderToken)
};

} // dom namespace
} // mozilla namespace

#endif // mozilla_dom_workers_WorkerHolderToken_h