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/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"
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;
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()
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));
72 const base::TimeDelta kZeroTimeDelta
;
75 std::max(base::TimeDelta::FromMinutes(kMinimumUptimeInMinutes
) - uptime
,
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();