1 // Copyright (c) 2012 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/ui/ash/volume_controller_chromeos.h" 6 7 #include "ash/ash_switches.h" 8 #include "chrome/browser/browser_process.h" 9 #include "chrome/browser/extensions/api/system_private/system_private_api.h" 10 #include "content/public/browser/user_metrics.h" 11 12 using chromeos::CrasAudioHandler; 13 14 namespace { 15 16 // Percent by which the volume should be changed when a volume key is pressed. 17 const double kStepPercentage = 4.0; 18 19 } // namespace 20 21 VolumeController::VolumeController() { 22 CrasAudioHandler::Get()->AddAudioObserver(this); 23 } 24 25 VolumeController::~VolumeController() { 26 if (CrasAudioHandler::IsInitialized()) 27 CrasAudioHandler::Get()->RemoveAudioObserver(this); 28 } 29 30 bool VolumeController::HandleVolumeMute(const ui::Accelerator& accelerator) { 31 if (accelerator.key_code() == ui::VKEY_VOLUME_MUTE) 32 content::RecordAction(content::UserMetricsAction("Accel_VolumeMute_F8")); 33 34 CrasAudioHandler::Get()->SetOutputMute(true); 35 return true; 36 } 37 38 bool VolumeController::HandleVolumeDown(const ui::Accelerator& accelerator) { 39 if (accelerator.key_code() == ui::VKEY_VOLUME_DOWN) 40 content::RecordAction(content::UserMetricsAction("Accel_VolumeDown_F9")); 41 42 CrasAudioHandler* audio_handler = CrasAudioHandler::Get(); 43 if (audio_handler->IsOutputMuted()) { 44 audio_handler->SetOutputVolumePercent(0); 45 } else { 46 audio_handler->AdjustOutputVolumeByPercent(-kStepPercentage); 47 if (audio_handler->IsOutputVolumeBelowDefaultMuteLvel()) 48 audio_handler->SetOutputMute(true); 49 } 50 return true; 51 } 52 53 bool VolumeController::HandleVolumeUp(const ui::Accelerator& accelerator) { 54 if (accelerator.key_code() == ui::VKEY_VOLUME_UP) 55 content::RecordAction(content::UserMetricsAction("Accel_VolumeUp_F10")); 56 57 CrasAudioHandler* audio_handler = CrasAudioHandler::Get(); 58 if (audio_handler->IsOutputMuted()) { 59 audio_handler->SetOutputMute(false); 60 audio_handler->AdjustOutputVolumeToAudibleLevel(); 61 } else { 62 audio_handler->AdjustOutputVolumeByPercent(kStepPercentage); 63 } 64 65 return true; 66 } 67 68 void VolumeController::OnOutputVolumeChanged() { 69 CrasAudioHandler* audio_handler = CrasAudioHandler::Get(); 70 extensions::DispatchVolumeChangedEvent( 71 audio_handler->GetOutputVolumePercent(), 72 audio_handler->IsOutputMuted()); 73 } 74 75 void VolumeController::OnOutputMuteChanged() { 76 CrasAudioHandler* audio_handler = CrasAudioHandler::Get(); 77 extensions::DispatchVolumeChangedEvent( 78 audio_handler->GetOutputVolumePercent(), 79 audio_handler->IsOutputMuted()); 80 } 81