blob: 5c7d261624f73231dea1f53261a0c48a38f25b7c [file] [log] [blame]
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/macros.h"
#include "base/time/time.h"
#include "content/common/content_export.h"
namespace content {
class ServiceWorkerVersion;
// A controller for periodically sending a ping to the worker to see if the
// worker is not stalling or is in a busy synchronous loop (possibly abusively).
class CONTENT_EXPORT ServiceWorkerPingController final {
explicit ServiceWorkerPingController(ServiceWorkerVersion* version);
void Activate();
void Deactivate();
void OnPongReceived();
bool IsActivated() const;
bool IsTimedOut() const;
// Checks ping status. This is supposed to be called periodically.
// This may call:
// - version_->OnPingTimeout() if the worker hasn't reponded within a
// certain period.
// - version_->PingWorker() if we're running ping timer and can send next
// ping.
void CheckPingStatus();
void SimulateTimeoutForTesting();
void ClearLastPingTime();
enum class PingState { kNotPinging, kPinging, kPingTimedOut };
ServiceWorkerVersion* version_; // Owns |this|.
// The time the most recent ping was sent.
base::TimeTicks last_ping_time_;
PingState ping_state_ = PingState::kNotPinging;
} // namespace content