blob: 93a7123b76b37a4619cfb08f68195a46e4ab3fe3 [file] [log] [blame]
// Copyright 2020 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <cstdlib>
#include <memory>
#include <string>
#include <base/files/file.h>
#include <base/synchronization/lock.h>
#include <base/threading/platform_thread.h>
#include <base/time/time.h>
// ClobberUi's public class methods are not thread-safe.
class ClobberUi : private base::PlatformThread::Delegate {
explicit ClobberUi(base::File&& terminal);
bool StartWipeUi(int64_t bytes_to_write);
bool UpdateWipeProgress(int64_t total_bytes_written);
bool StopWipeUi();
bool ShowCountdownTimer(const base::TimeDelta& duration);
static std::string BuildUiStringForTest(int terminal_width,
const base::TimeDelta& elapsed,
double progress);
// base::PlatformThread::Delegate interface.
void ThreadMain() override;
enum UiMode {
kIdle, // No UI is currently being displayed.
kWipeUi, // The disk wipe progress UI is being shown.
kCountdownUi, // A countdown timer UI is being shown.
struct WipeState {
bool running;
int64_t total_bytes_written;
int64_t bytes_to_write;
base::File terminal_;
base::PlatformThreadHandle wipe_ui_thread_;
UiMode mode_;
base::Lock lock_;
WipeState state_; // Protected by lock_.