OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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. |
| 4 |
| 5 #include "chrome/browser/chromeos/policy/remote_commands/device_command_set_volu
me_job.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/json/json_reader.h" |
| 11 #include "base/syslog_logging.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "base/values.h" |
| 14 #include "chromeos/audio/cras_audio_handler.h" |
| 15 #include "components/policy/proto/device_management_backend.pb.h" |
| 16 |
| 17 namespace policy { |
| 18 |
| 19 namespace { |
| 20 |
| 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; |
| 24 |
| 25 const char kVolumeFieldName[] = "volume"; |
| 26 |
| 27 } // namespace |
| 28 |
| 29 DeviceCommandSetVolumeJob::DeviceCommandSetVolumeJob() {} |
| 30 |
| 31 DeviceCommandSetVolumeJob::~DeviceCommandSetVolumeJob() {} |
| 32 |
| 33 void DeviceCommandSetVolumeJob::SetVolumeCallbackForTesting( |
| 34 const VolumeCallback& callback) { |
| 35 volume_callback_ = callback; |
| 36 } |
| 37 |
| 38 enterprise_management::RemoteCommand_Type DeviceCommandSetVolumeJob::GetType() |
| 39 const { |
| 40 return enterprise_management::RemoteCommand_Type_DEVICE_SET_VOLUME; |
| 41 } |
| 42 |
| 43 base::TimeDelta DeviceCommandSetVolumeJob::GetCommmandTimeout() const { |
| 44 return base::TimeDelta::FromMinutes(kCommandExpirationTimeInMinutes); |
| 45 } |
| 46 |
| 47 bool DeviceCommandSetVolumeJob::ParseCommandPayload( |
| 48 const std::string& command_payload) { |
| 49 std::unique_ptr<base::Value> root( |
| 50 base::JSONReader().ReadToValue(command_payload)); |
| 51 if (!root.get()) |
| 52 return false; |
| 53 base::DictionaryValue* payload = nullptr; |
| 54 if (!root->GetAsDictionary(&payload)) |
| 55 return false; |
| 56 if (!payload->GetInteger(kVolumeFieldName, &volume_)) |
| 57 return false; |
| 58 if (volume_ < 0 || volume_ > 100) |
| 59 return false; |
| 60 return true; |
| 61 } |
| 62 |
| 63 bool DeviceCommandSetVolumeJob::IsExpired(base::TimeTicks now) { |
| 64 return now > issued_time() + base::TimeDelta::FromMinutes( |
| 65 kCommandExpirationTimeInMinutes); |
| 66 } |
| 67 |
| 68 void DeviceCommandSetVolumeJob::RunImpl( |
| 69 const CallbackWithResult& succeeded_callback, |
| 70 const CallbackWithResult& failed_callback) { |
| 71 SYSLOG(INFO) << "Running set volume command, volume = " << volume_; |
| 72 if (volume_callback_.is_null()) { |
| 73 auto* audio_handler = chromeos::CrasAudioHandler::Get(); |
| 74 audio_handler->SetOutputVolumePercent(volume_); |
| 75 bool mute = audio_handler->IsOutputVolumeBelowDefaultMuteLevel(); |
| 76 audio_handler->SetOutputMute(mute); |
| 77 } else { |
| 78 volume_callback_.Run(volume_); |
| 79 } |
| 80 |
| 81 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 82 FROM_HERE, base::Bind(succeeded_callback, nullptr)); |
| 83 } |
| 84 |
| 85 } // namespace policy |
OLD | NEW |