Commit 71df9622 authored by Nicholas Hollingum's avatar Nicholas Hollingum Committed by Chromium LUCI CQ

borealis: Add a monitor to assist shutting borealis down with a delay

This monitor will be sent requests to shutdown borealis with or without
a delay, and it will act on those requests at the appropriate times.

Bug: b/172006764
Change-Id: I81fb0cb73cfb1c868a6ebc8000cfa044a1569a21
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2569323
Commit-Queue: Nic Hollingum <hollingum@google.com>
Reviewed-by: default avatarDaniel Ng <danielng@google.com>
Cr-Commit-Position: refs/heads/master@{#833555}
parent c6542def
...@@ -891,6 +891,8 @@ source_set("chromeos") { ...@@ -891,6 +891,8 @@ source_set("chromeos") {
"borealis/borealis_service_factory.h", "borealis/borealis_service_factory.h",
"borealis/borealis_service_impl.cc", "borealis/borealis_service_impl.cc",
"borealis/borealis_service_impl.h", "borealis/borealis_service_impl.h",
"borealis/borealis_shutdown_monitor.cc",
"borealis/borealis_shutdown_monitor.h",
"borealis/borealis_task.cc", "borealis/borealis_task.cc",
"borealis/borealis_task.h", "borealis/borealis_task.h",
"borealis/borealis_util.cc", "borealis/borealis_util.cc",
...@@ -3387,6 +3389,7 @@ source_set("unit_tests") { ...@@ -3387,6 +3389,7 @@ source_set("unit_tests") {
"borealis/borealis_features_unittest.cc", "borealis/borealis_features_unittest.cc",
"borealis/borealis_installer_unittest.cc", "borealis/borealis_installer_unittest.cc",
"borealis/borealis_launch_watcher_unittest.cc", "borealis/borealis_launch_watcher_unittest.cc",
"borealis/borealis_shutdown_monitor_unittest.cc",
"borealis/borealis_task_unittest.cc", "borealis/borealis_task_unittest.cc",
"borealis/borealis_window_manager_unittest.cc", "borealis/borealis_window_manager_unittest.cc",
"borealis/infra/described_unittest.cc", "borealis/infra/described_unittest.cc",
......
// Copyright 2020 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 "chrome/browser/chromeos/borealis/borealis_shutdown_monitor.h"
#include "base/threading/thread_task_runner_handle.h"
#include "chrome/browser/chromeos/borealis/borealis_context_manager.h"
#include "chrome/browser/chromeos/borealis/borealis_service.h"
namespace {
// The default time period used when initiating delayed shutdowns.
constexpr base::TimeDelta kDefaultDelay = base::TimeDelta::FromSeconds(60);
} // namespace
namespace borealis {
BorealisShutdownMonitor::BorealisShutdownMonitor(Profile* profile)
: profile_(profile), delay_(kDefaultDelay) {}
BorealisShutdownMonitor::~BorealisShutdownMonitor() = default;
void BorealisShutdownMonitor::ShutdownWithDelay() {
// Reset() cancels the previous request if it was already there. Also,
// Unretained() is safe because the callback is cancelled when
// |in_progress_request_| is destroyed.
in_progress_request_.Reset(base::BindOnce(
&BorealisShutdownMonitor::ShutdownNow, base::Unretained(this)));
base::SequencedTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, in_progress_request_.callback(), delay_);
}
void BorealisShutdownMonitor::ShutdownNow() {
BorealisService::GetForProfile(profile_)->ContextManager().ShutDownBorealis();
}
void BorealisShutdownMonitor::CancelDelayedShutdown() {
in_progress_request_.Cancel();
}
void BorealisShutdownMonitor::SetShutdownDelayForTesting(
base::TimeDelta delay) {
delay_ = delay;
}
} // namespace borealis
// Copyright 2020 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.
#ifndef CHROME_BROWSER_CHROMEOS_BOREALIS_BOREALIS_SHUTDOWN_MONITOR_H_
#define CHROME_BROWSER_CHROMEOS_BOREALIS_BOREALIS_SHUTDOWN_MONITOR_H_
#include "base/cancelable_callback.h"
#include "base/time/time.h"
class Profile;
namespace borealis {
// Manages the automatic shutdown of borealis either immediately, or via a timed
// delay.
class BorealisShutdownMonitor {
public:
explicit BorealisShutdownMonitor(Profile* profile);
~BorealisShutdownMonitor();
// Initiate a delayed shutdown, which will trigger the real shutdown after a
// fixed time period, unless the shutdown is aborted in the interim. If a
// delayed shutdown is in progress, this will reset the delay.
void ShutdownWithDelay();
// Initiate a shutdown immediately, without the delay.
void ShutdownNow();
// Cancels any in-progress delayed shutdowns.
void CancelDelayedShutdown();
// Overrides the default delay of the shutdown.
void SetShutdownDelayForTesting(base::TimeDelta delay);
private:
// The profile which we will shutdown borealis for.
Profile* profile_;
// The length of time we wait before issuing a shutdown after a delayed
// shutdown is requested.
base::TimeDelta delay_;
// The currently in-flight request to shutdown borealis. This will be in the
// default state unless a request is actually underway.
base::CancelableOnceClosure in_progress_request_;
};
} // namespace borealis
#endif // CHROME_BROWSER_CHROMEOS_BOREALIS_BOREALIS_SHUTDOWN_MONITOR_H_
// Copyright 2020 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 "chrome/browser/chromeos/borealis/borealis_shutdown_monitor.h"
#include <memory>
#include "chrome/browser/chromeos/borealis/borealis_context_manager.h"
#include "chrome/browser/chromeos/borealis/borealis_features.h"
#include "chrome/browser/chromeos/borealis/borealis_service_fake.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace borealis {
namespace {
class BorealisContextManagerMock : public BorealisContextManager {
public:
MOCK_METHOD(void,
StartBorealis,
(BorealisContextManager::ResultCallback),
());
MOCK_METHOD(void, ShutDownBorealis, (), ());
};
class BorealisShutdownMonitorTest : public testing::Test {
protected:
BorealisShutdownMonitorTest()
: service_fake_(BorealisServiceFake::UseFakeForTesting(&profile_)),
features_(&profile_) {
service_fake_->SetFeaturesForTesting(&features_);
service_fake_->SetContextManagerForTesting(&context_manager_mock_);
}
Profile* profile() { return &profile_; }
content::BrowserTaskEnvironment task_environment_;
TestingProfile profile_;
BorealisServiceFake* service_fake_;
BorealisFeatures features_;
testing::StrictMock<BorealisContextManagerMock> context_manager_mock_;
};
TEST_F(BorealisShutdownMonitorTest, CanShutdownImmediately) {
BorealisShutdownMonitor monitor(profile());
EXPECT_CALL(context_manager_mock_, ShutDownBorealis());
monitor.ShutdownNow();
}
TEST_F(BorealisShutdownMonitorTest, CanShutdownWithDelay) {
BorealisShutdownMonitor monitor(profile());
monitor.SetShutdownDelayForTesting(base::TimeDelta::FromSeconds(0));
monitor.ShutdownWithDelay();
EXPECT_CALL(context_manager_mock_, ShutDownBorealis());
task_environment_.RunUntilIdle();
}
TEST_F(BorealisShutdownMonitorTest, CancelDelayedShutdownPreventsIt) {
BorealisShutdownMonitor monitor(profile());
EXPECT_CALL(context_manager_mock_, ShutDownBorealis()).Times(0);
monitor.SetShutdownDelayForTesting(base::TimeDelta::FromSeconds(0));
monitor.ShutdownWithDelay();
monitor.CancelDelayedShutdown();
task_environment_.RunUntilIdle();
}
TEST_F(BorealisShutdownMonitorTest, LaterShutdownOverridesEarlier) {
BorealisShutdownMonitor monitor(profile());
EXPECT_CALL(context_manager_mock_, ShutDownBorealis()).Times(0);
monitor.SetShutdownDelayForTesting(base::TimeDelta::FromSeconds(0));
monitor.ShutdownWithDelay();
// I'm assuming this thread won't be idle for 99 seconds.
monitor.SetShutdownDelayForTesting(base::TimeDelta::FromSeconds(99));
monitor.ShutdownWithDelay();
task_environment_.RunUntilIdle();
}
TEST_F(BorealisShutdownMonitorTest, DeletingMonitorCancelsShutdowns) {
auto monitor = std::make_unique<BorealisShutdownMonitor>(profile());
EXPECT_CALL(context_manager_mock_, ShutDownBorealis()).Times(0);
monitor->SetShutdownDelayForTesting(base::TimeDelta::FromSeconds(0));
monitor->ShutdownWithDelay();
monitor.reset();
task_environment_.RunUntilIdle();
}
} // namespace
} // namespace borealis
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment