Index: chrome/browser/chromeos/policy/remote_commands/device_command_set_volume_job.cc |
diff --git a/chrome/browser/chromeos/policy/remote_commands/device_command_set_volume_job.cc b/chrome/browser/chromeos/policy/remote_commands/device_command_set_volume_job.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..40a15d2e813872f578d3b8e3c441116f9021cdb4 |
--- /dev/null |
+++ b/chrome/browser/chromeos/policy/remote_commands/device_command_set_volume_job.cc |
@@ -0,0 +1,81 @@ |
+// Copyright 2017 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/policy/remote_commands/device_command_set_volume_job.h" |
+ |
+#include <utility> |
+ |
+#include "base/bind.h" |
+#include "base/json/json_reader.h" |
+#include "base/syslog_logging.h" |
+#include "base/threading/thread_task_runner_handle.h" |
+#include "base/values.h" |
+#include "chromeos/audio/cras_audio_handler.h" |
+#include "components/policy/proto/device_management_backend.pb.h" |
+ |
+namespace policy { |
+ |
+namespace { |
+ |
+// Determines the time, measured from the time of issue, after which the command |
+// queue will consider this command expired if the command has not been started. |
+const int kCommandExpirationTimeInMinutes = 10; |
+ |
+const char kVolumeFieldName[] = "volume"; |
+ |
+} // namespace |
+ |
+DeviceCommandSetVolumeJob::DeviceCommandSetVolumeJob() {} |
+ |
+DeviceCommandSetVolumeJob::~DeviceCommandSetVolumeJob() {} |
+ |
+void DeviceCommandSetVolumeJob::SetVolumeCallbackForTesting( |
+ const base::Callback<void(int)>& callback) { |
+ volume_callback_ = callback; |
+} |
+ |
+enterprise_management::RemoteCommand_Type DeviceCommandSetVolumeJob::GetType() |
+ const { |
+ return enterprise_management::RemoteCommand_Type_DEVICE_SET_VOLUME; |
+} |
+ |
+base::TimeDelta DeviceCommandSetVolumeJob::GetCommmandTimeout() const { |
+ return base::TimeDelta::FromMinutes(kCommandExpirationTimeInMinutes); |
+} |
+ |
+bool DeviceCommandSetVolumeJob::ParseCommandPayload( |
+ const std::string& command_payload) { |
+ std::unique_ptr<base::Value> root( |
+ base::JSONReader().ReadToValue(command_payload)); |
+ if (!root.get()) |
+ return false; |
+ base::DictionaryValue* payload = nullptr; |
+ if (!root->GetAsDictionary(&payload)) |
+ return false; |
+ if (!payload->GetInteger(kVolumeFieldName, &volume_)) |
+ return false; |
+ if (volume_ < 0 || volume_ > 100) |
Andrew T Wilson (Slow)
2017/03/08 12:50:56
I would probably log an error in this case?
Ivan Šandrk
2017/03/08 15:07:11
Added a more generic logging code at callsite (Rem
|
+ return false; |
+ return true; |
+} |
+ |
+bool DeviceCommandSetVolumeJob::IsExpired(base::TimeTicks now) { |
+ return now > issued_time() + base::TimeDelta::FromMinutes( |
+ kCommandExpirationTimeInMinutes); |
+} |
+ |
+void DeviceCommandSetVolumeJob::RunImpl( |
+ const CallbackWithResult& succeeded_callback, |
+ const CallbackWithResult& failed_callback) { |
+ SYSLOG(INFO) << "Running set volume command, volume = " << volume_; |
+ if (volume_callback_.is_null()) |
+ chromeos::CrasAudioHandler::Get()->SetOutputVolumePercent(volume_); |
+ else |
+ volume_callback_.Run(volume_); |
+ |
+ base::ThreadTaskRunnerHandle::Get()->PostTask( |
+ FROM_HERE, base::Bind(succeeded_callback, nullptr)); |
+} |
+ |
+} // namespace policy |