| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/policy/remote_commands/device_command_reboot_j
ob.h" | 5 #include "chrome/browser/chromeos/policy/remote_commands/device_command_reboot_j
ob.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/chromeos/logging.h" | 10 #include "base/chromeos/logging.h" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
| 14 #include "base/sys_info.h" | 14 #include "base/sys_info.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "chromeos/dbus/power_manager_client.h" | 17 #include "chromeos/dbus/power_manager_client.h" |
| 18 #include "policy/proto/device_management_backend.pb.h" | 18 #include "policy/proto/device_management_backend.pb.h" |
| 19 | 19 |
| 20 namespace policy { | 20 namespace policy { |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 // Determines the time, measured from the time of issue, after which the command | 24 // Determines the time, measured from the time of issue, after which the command |
| 25 // queue will consider this command expired if the command has not been started. | 25 // queue will consider this command expired if the command has not been started. |
| 26 const int kCommandExpirationTimeInMinutes = 10; | 26 const int kCommandExpirationTimeInMinutes = 10; |
| 27 | 27 |
| 28 // Determines the minimum uptime after which a reboot might be scheduled. Note: | |
| 29 // |kCommandExpirationTimeInMinutes| >= |kMinimumUptimeInMinutes| as | |
| 30 // otherwise, a valid command issued right after boot may time out. | |
| 31 const int kMinimumUptimeInMinutes = 10; | |
| 32 | |
| 33 } // namespace | 28 } // namespace |
| 34 | 29 |
| 35 DeviceCommandRebootJob::DeviceCommandRebootJob( | 30 DeviceCommandRebootJob::DeviceCommandRebootJob( |
| 36 chromeos::PowerManagerClient* power_manager_client) | 31 chromeos::PowerManagerClient* power_manager_client) |
| 37 : power_manager_client_(power_manager_client), weak_ptr_factory_(this) { | 32 : power_manager_client_(power_manager_client) { |
| 38 CHECK(power_manager_client_); | 33 CHECK(power_manager_client_); |
| 39 } | 34 } |
| 40 | 35 |
| 41 DeviceCommandRebootJob::~DeviceCommandRebootJob() { | 36 DeviceCommandRebootJob::~DeviceCommandRebootJob() { |
| 42 } | 37 } |
| 43 | 38 |
| 44 enterprise_management::RemoteCommand_Type DeviceCommandRebootJob::GetType() | 39 enterprise_management::RemoteCommand_Type DeviceCommandRebootJob::GetType() |
| 45 const { | 40 const { |
| 46 return enterprise_management::RemoteCommand_Type_DEVICE_REBOOT; | 41 return enterprise_management::RemoteCommand_Type_DEVICE_REBOOT; |
| 47 } | 42 } |
| 48 | 43 |
| 49 bool DeviceCommandRebootJob::IsExpired(base::TimeTicks now) { | 44 bool DeviceCommandRebootJob::IsExpired(base::TimeTicks now) { |
| 50 return now > issued_time() + base::TimeDelta::FromMinutes( | 45 return now > issued_time() + base::TimeDelta::FromMinutes( |
| 51 kCommandExpirationTimeInMinutes); | 46 kCommandExpirationTimeInMinutes); |
| 52 } | 47 } |
| 53 | 48 |
| 54 void DeviceCommandRebootJob::RunImpl( | 49 void DeviceCommandRebootJob::RunImpl( |
| 55 const CallbackWithResult& succeeded_callback, | 50 const CallbackWithResult& succeeded_callback, |
| 56 const CallbackWithResult& failed_callback) { | 51 const CallbackWithResult& failed_callback) { |
| 57 CHROMEOS_SYSLOG(WARNING) << "Running reboot command."; | 52 CHROMEOS_SYSLOG(WARNING) << "Running reboot command."; |
| 58 | 53 |
| 59 // Determines the time delta between the command having been issued and the | 54 // Determines the time delta between the command having been issued and the |
| 60 // boot time of the system. | 55 // boot time of the system. |
| 61 const base::TimeDelta uptime = base::SysInfo::Uptime(); | 56 const base::TimeDelta uptime = base::SysInfo::Uptime(); |
| 62 const base::TimeTicks boot_time = base::TimeTicks::Now() - uptime; | 57 const base::TimeTicks boot_time = base::TimeTicks::Now() - uptime; |
| 63 const base::TimeDelta delta = boot_time - issued_time(); | 58 const base::TimeDelta delta = boot_time - issued_time(); |
| 64 // If the reboot command was issued before the system booted, we inform the | 59 // If the reboot command was issued before the system booted, we inform the |
| 65 // server that the reboot succeeded. Otherwise, the reboot must still be | 60 // server that the reboot succeeded. Otherwise, the reboot must still be |
| 66 // performed and we invoke it. |kMinimumUptimeInMinutes| defines a lower limit | 61 // performed and we invoke it. |
| 67 // on the uptime to avoid uninterruptable reboot loops. | |
| 68 if (delta > base::TimeDelta()) { | 62 if (delta > base::TimeDelta()) { |
| 69 CHROMEOS_SYSLOG(WARNING) << "Ignoring reboot command issued " << delta | 63 CHROMEOS_SYSLOG(WARNING) << "Ignoring reboot command issued " << delta |
| 70 << " before current boot time"; | 64 << " before current boot time"; |
| 71 base::ThreadTaskRunnerHandle::Get()->PostTask( | 65 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 72 FROM_HERE, base::Bind(succeeded_callback, nullptr)); | 66 FROM_HERE, base::Bind(succeeded_callback, nullptr)); |
| 73 return; | 67 return; |
| 74 } | 68 } |
| 75 | 69 |
| 76 const base::TimeDelta kZeroTimeDelta; | 70 CHROMEOS_SYSLOG(WARNING) << "Rebooting immediately."; |
| 77 base::TimeDelta reboot_delay = | 71 power_manager_client_->RequestRestart(); |
| 78 std::max(base::TimeDelta::FromMinutes(kMinimumUptimeInMinutes) - uptime, | |
| 79 kZeroTimeDelta); | |
| 80 if (reboot_delay > kZeroTimeDelta) { | |
| 81 CHROMEOS_SYSLOG(WARNING) << "Rebooting in " << reboot_delay << "."; | |
| 82 } else { | |
| 83 CHROMEOS_SYSLOG(WARNING) << "Rebooting immediately."; | |
| 84 } | |
| 85 reboot_timer_.Start(FROM_HERE, reboot_delay, | |
| 86 base::Bind(&DeviceCommandRebootJob::Reboot, | |
| 87 weak_ptr_factory_.GetWeakPtr())); | |
| 88 } | |
| 89 | |
| 90 void DeviceCommandRebootJob::TerminateImpl() { | |
| 91 weak_ptr_factory_.InvalidateWeakPtrs(); | |
| 92 } | 72 } |
| 93 | 73 |
| 94 base::TimeDelta DeviceCommandRebootJob::GetCommmandTimeout() const { | 74 base::TimeDelta DeviceCommandRebootJob::GetCommmandTimeout() const { |
| 95 return base::TimeDelta::FromMinutes(kMinimumUptimeInMinutes); | 75 return base::TimeDelta::FromMinutes(kCommandExpirationTimeInMinutes); |
| 96 } | |
| 97 | |
| 98 void DeviceCommandRebootJob::Reboot() const { | |
| 99 power_manager_client_->RequestRestart(); | |
| 100 } | 76 } |
| 101 | 77 |
| 102 } // namespace policy | 78 } // namespace policy |
| OLD | NEW |