cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / remote_commands / device_command_reboot_job.cc
blob587b1b6df6803b322f1e6599c987171600fa19b5
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/policy/remote_commands/device_command_reboot_job.h"
7 #include <algorithm>
9 #include "base/bind.h"
10 #include "base/location.h"
11 #include "base/logging.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/sys_info.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "base/time/time.h"
16 #include "chromeos/dbus/power_manager_client.h"
17 #include "policy/proto/device_management_backend.pb.h"
19 namespace policy {
21 namespace {
23 // Determines the time, measured from the time of issue, after which the command
24 // queue will consider this command expired if the command has not been started.
25 const int kCommandExpirationTimeInMinutes = 10;
27 // Determines the minimum uptime after which a reboot might be scheduled. Note:
28 // |kCommandExpirationTimeInMinutes| >= |kMinimumUptimeInMinutes| as
29 // otherwise, a valid command issued right after boot may time out.
30 const int kMinimumUptimeInMinutes = 10;
32 } // namespace
34 DeviceCommandRebootJob::DeviceCommandRebootJob(
35 chromeos::PowerManagerClient* power_manager_client)
36 : power_manager_client_(power_manager_client), weak_ptr_factory_(this) {
37 CHECK(power_manager_client_);
40 DeviceCommandRebootJob::~DeviceCommandRebootJob() {
43 enterprise_management::RemoteCommand_Type DeviceCommandRebootJob::GetType()
44 const {
45 return enterprise_management::RemoteCommand_Type_DEVICE_REBOOT;
48 bool DeviceCommandRebootJob::IsExpired(base::TimeTicks now) {
49 return now > issued_time() + base::TimeDelta::FromMinutes(
50 kCommandExpirationTimeInMinutes);
53 void DeviceCommandRebootJob::RunImpl(
54 const CallbackWithResult& succeeded_callback,
55 const CallbackWithResult& failed_callback) {
56 // Determines the time delta between the command having been issued and the
57 // boot time of the system.
58 const base::TimeDelta uptime =
59 base::TimeDelta::FromMilliseconds(base::SysInfo::Uptime());
60 const base::TimeTicks boot_time = base::TimeTicks::Now() - uptime;
61 const base::TimeDelta delta = boot_time - issued_time();
62 // If the reboot command was issued before the system booted, we inform the
63 // server that the reboot succeeded. Otherwise, the reboot must still be
64 // performed and we invoke it. |kMinimumUptimeInMinutes| defines a lower limit
65 // on the uptime to avoid uninterruptable reboot loops.
66 if (delta > base::TimeDelta()) {
67 base::ThreadTaskRunnerHandle::Get()->PostTask(
68 FROM_HERE, base::Bind(succeeded_callback, nullptr));
69 return;
72 const base::TimeDelta kZeroTimeDelta;
73 reboot_timer_.Start(
74 FROM_HERE,
75 std::max(base::TimeDelta::FromMinutes(kMinimumUptimeInMinutes) - uptime,
76 kZeroTimeDelta),
77 base::Bind(&DeviceCommandRebootJob::Reboot,
78 weak_ptr_factory_.GetWeakPtr()));
81 void DeviceCommandRebootJob::TerminateImpl() {
82 weak_ptr_factory_.InvalidateWeakPtrs();
85 base::TimeDelta DeviceCommandRebootJob::GetCommmandTimeout() const {
86 return base::TimeDelta::FromMinutes(kMinimumUptimeInMinutes);
89 void DeviceCommandRebootJob::Reboot() const {
90 power_manager_client_->RequestRestart();
93 } // namespace policy