dom/base/Timeout.cpp
author Andreas Farre <farre@mozilla.com>
Wed, 05 Oct 2016 14:26:08 +0200
changeset 320171 030e1cc3b1c1feede3182d67adc12a1d6f80c746
parent 319194 24839edcf0efc585c655760128b202df88041933
child 319195 ffaf3d371130200ed62ffb462386a191901d1421
permissions -rw-r--r--
Bug 1198381 - Extract nsITimeoutHandler from nsIScriptTimeoutHandler. r=smaug MozReview-Commit-ID: HJHrbodWYVf

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

#include "nsGlobalWindow.h"
#include "nsITimeoutHandler.h"
#include "nsITimer.h"
#include "nsPIDOMWindow.h"

namespace mozilla {
namespace dom {

Timeout::Timeout()
  : mCleared(false),
    mRunning(false),
    mIsInterval(false),
    mPublicId(0),
    mInterval(0),
    mFiringDepth(0),
    mNestingLevel(0),
    mPopupState(openAllowed)
{
  MOZ_COUNT_CTOR(Timeout);
}

Timeout::~Timeout()
{
  if (mTimer) {
    mTimer->Cancel();
    mTimer = nullptr;
  }

  MOZ_COUNT_DTOR(Timeout);
}

NS_IMPL_CYCLE_COLLECTION_CLASS(Timeout)

NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Timeout)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mPrincipal)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mScriptHandler)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END

NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Timeout)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPrincipal)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mScriptHandler)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END

NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(Timeout, AddRef)
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(Timeout, Release)

nsresult
Timeout::InitTimer(uint32_t aDelay)
{
  return mTimer->InitWithNameableFuncCallback(
    nsGlobalWindow::TimerCallback, this, aDelay,
    nsITimer::TYPE_ONE_SHOT, Timeout::TimerNameCallback);
}

// static
void
Timeout::TimerNameCallback(nsITimer* aTimer, void* aClosure, char* aBuf,
                           size_t aLen)
{
  RefPtr<Timeout> timeout = (Timeout*)aClosure;

  const char* filename;
  uint32_t lineNum, column;
  timeout->mScriptHandler->GetLocation(&filename, &lineNum, &column);
  snprintf(aBuf, aLen, "[content] %s:%u:%u", filename, lineNum, column);
}


// Return true if this timeout has a refcount of 1. This is used to check
// that dummy_timeout doesn't leak from nsGlobalWindow::RunTimeout.
#ifdef DEBUG
bool
Timeout::HasRefCntOne() const
{
  return mRefCnt.get() == 1;
}
#endif // DEBUG

} // namespace dom
} // namespace mozilla