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"
10 #include "base/location.h"
11 #include "base/logging.h"
12 #include "base/sys_info.h"
13 #include "base/time/time.h"
14 #include "chromeos/dbus/power_manager_client.h"
15 #include "policy/proto/device_management_backend.pb.h"
21 // Determines the time, measured from the time of issue, after which the command
22 // queue will consider this command expired if the command has not been started.
23 const int kCommandExpirationTimeInMinutes
= 10;
25 // Determines the minimum uptime after which a reboot might be scheduled. Note:
26 // |kCommandExpirationTimeInMinutes| >= |kMinimumUptimeInMinutes| as
27 // otherwise, a valid command issued right after boot may time out.
28 const int kMinimumUptimeInMinutes
= 10;
32 DeviceCommandRebootJob::DeviceCommandRebootJob(
33 chromeos::PowerManagerClient
* power_manager_client
)
34 : power_manager_client_(power_manager_client
), weak_ptr_factory_(this) {
35 CHECK(power_manager_client_
);
38 DeviceCommandRebootJob::~DeviceCommandRebootJob() {
41 enterprise_management::RemoteCommand_Type
DeviceCommandRebootJob::GetType()
43 return enterprise_management::RemoteCommand_Type_DEVICE_REBOOT
;
46 bool DeviceCommandRebootJob::IsExpired(base::Time now
) {
47 return now
> issued_time() + base::TimeDelta::FromMinutes(
48 kCommandExpirationTimeInMinutes
);
51 void DeviceCommandRebootJob::RunImpl(
52 const SucceededCallback
& succeeded_callback
,
53 const FailedCallback
& failed_callback
) {
54 // Determines the time delta between the command having been issued and the
55 // boot time of the system.
56 const base::TimeDelta uptime
=
57 base::TimeDelta::FromMilliseconds(base::SysInfo::Uptime());
58 const base::Time boot_time
= base::Time::Now() - uptime
;
59 const base::TimeDelta delta
= boot_time
- issued_time();
60 // If the reboot command was issued before the system booted, we inform the
61 // server that the reboot succeeded. Otherwise, the reboot must still be
62 // performed and we invoke it. |kMinimumUptimeInMinutes| defines a lower limit
63 // on the uptime to avoid uninterruptable reboot loops.
64 if (delta
> base::TimeDelta()) {
65 succeeded_callback
.Run(nullptr);
69 const base::TimeDelta kZeroTimeDelta
;
72 std::max(base::TimeDelta::FromMinutes(kMinimumUptimeInMinutes
) - uptime
,
74 base::Bind(&DeviceCommandRebootJob::Reboot
,
75 weak_ptr_factory_
.GetWeakPtr()));
78 void DeviceCommandRebootJob::TerminateImpl() {
79 weak_ptr_factory_
.InvalidateWeakPtrs();
82 base::TimeDelta
DeviceCommandRebootJob::GetCommmandTimeout() const {
83 return base::TimeDelta::FromMinutes(kMinimumUptimeInMinutes
);
86 void DeviceCommandRebootJob::Reboot() const {
87 power_manager_client_
->RequestRestart();