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.
5 #include "chrome/browser/ui/ash/volume_controller_chromeos.h"
7 #include "ash/ash_switches.h"
8 #include "ash/audio/sounds.h"
9 #include "base/command_line.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/extensions/api/system_private/system_private_api.h"
12 #include "chromeos/audio/chromeos_sounds.h"
13 #include "chromeos/chromeos_switches.h"
14 #include "content/public/browser/user_metrics.h"
15 #include "grit/browser_resources.h"
16 #include "media/audio/sounds/sounds_manager.h"
17 #include "ui/base/resource/resource_bundle.h"
19 using chromeos::CrasAudioHandler
;
23 // Percent by which the volume should be changed when a volume key is pressed.
24 const double kStepPercentage
= 4.0;
26 bool VolumeAdjustSoundEnabled() {
27 return !CommandLine::ForCurrentProcess()->HasSwitch(
28 chromeos::switches::kDisableVolumeAdjustSound
);
31 void PlayVolumeAdjustSound() {
32 if (VolumeAdjustSoundEnabled())
33 ash::PlaySystemSoundIfSpokenFeedback(chromeos::SOUND_VOLUME_ADJUST
);
38 VolumeController::VolumeController() {
39 CrasAudioHandler::Get()->AddAudioObserver(this);
40 ui::ResourceBundle
& bundle
= ui::ResourceBundle::GetSharedInstance();
41 if (VolumeAdjustSoundEnabled()) {
42 media::SoundsManager::Get()->Initialize(
43 chromeos::SOUND_VOLUME_ADJUST
,
44 bundle
.GetRawDataResource(IDR_SOUND_VOLUME_ADJUST_WAV
));
48 VolumeController::~VolumeController() {
49 if (CrasAudioHandler::IsInitialized())
50 CrasAudioHandler::Get()->RemoveAudioObserver(this);
53 bool VolumeController::HandleVolumeMute(const ui::Accelerator
& accelerator
) {
54 if (accelerator
.key_code() == ui::VKEY_VOLUME_MUTE
)
55 content::RecordAction(base::UserMetricsAction("Accel_VolumeMute_F8"));
57 CrasAudioHandler::Get()->SetOutputMute(true);
61 bool VolumeController::HandleVolumeDown(const ui::Accelerator
& accelerator
) {
62 if (accelerator
.key_code() == ui::VKEY_VOLUME_DOWN
)
63 content::RecordAction(base::UserMetricsAction("Accel_VolumeDown_F9"));
65 CrasAudioHandler
* audio_handler
= CrasAudioHandler::Get();
66 if (audio_handler
->IsOutputMuted()) {
67 audio_handler
->SetOutputVolumePercent(0);
69 audio_handler
->AdjustOutputVolumeByPercent(-kStepPercentage
);
70 if (audio_handler
->IsOutputVolumeBelowDefaultMuteLevel())
71 audio_handler
->SetOutputMute(true);
73 PlayVolumeAdjustSound();
78 bool VolumeController::HandleVolumeUp(const ui::Accelerator
& accelerator
) {
79 if (accelerator
.key_code() == ui::VKEY_VOLUME_UP
)
80 content::RecordAction(base::UserMetricsAction("Accel_VolumeUp_F10"));
82 CrasAudioHandler
* audio_handler
= CrasAudioHandler::Get();
83 bool play_sound
= false;
84 if (audio_handler
->IsOutputMuted()) {
85 audio_handler
->SetOutputMute(false);
86 audio_handler
->AdjustOutputVolumeToAudibleLevel();
89 play_sound
= audio_handler
->GetOutputVolumePercent() != 100;
90 audio_handler
->AdjustOutputVolumeByPercent(kStepPercentage
);
94 PlayVolumeAdjustSound();
98 void VolumeController::OnOutputVolumeChanged() {
99 CrasAudioHandler
* audio_handler
= CrasAudioHandler::Get();
100 extensions::DispatchVolumeChangedEvent(
101 audio_handler
->GetOutputVolumePercent(),
102 audio_handler
->IsOutputMuted());
105 void VolumeController::OnOutputMuteChanged() {
106 CrasAudioHandler
* audio_handler
= CrasAudioHandler::Get();
107 extensions::DispatchVolumeChangedEvent(
108 audio_handler
->GetOutputVolumePercent(),
109 audio_handler
->IsOutputMuted());