Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / ash / volume_controller_chromeos.cc
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 "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"
18
19 using chromeos::CrasAudioHandler;
20
21 namespace {
22
23 // Percent by which the volume should be changed when a volume key is pressed.
24 const double kStepPercentage = 4.0;
25
26 void PlayVolumeAdjustSound() {
27   if (CommandLine::ForCurrentProcess()->HasSwitch(
28           chromeos::switches::kEnableVolumeAdjustSound)) {
29     ash::PlaySystemSound(chromeos::SOUND_VOLUME_ADJUST,
30                          true /* honor_spoken_feedback */);
31   }
32 }
33
34 }  // namespace
35
36 VolumeController::VolumeController() {
37   CrasAudioHandler::Get()->AddAudioObserver(this);
38   ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
39   media::SoundsManager::Get()->Initialize(
40       chromeos::SOUND_VOLUME_ADJUST,
41       bundle.GetRawDataResource(IDR_SOUND_VOLUME_ADJUST_WAV));
42 }
43
44 VolumeController::~VolumeController() {
45   if (CrasAudioHandler::IsInitialized())
46     CrasAudioHandler::Get()->RemoveAudioObserver(this);
47 }
48
49 bool VolumeController::HandleVolumeMute(const ui::Accelerator& accelerator) {
50   if (accelerator.key_code() == ui::VKEY_VOLUME_MUTE)
51     content::RecordAction(base::UserMetricsAction("Accel_VolumeMute_F8"));
52
53   CrasAudioHandler::Get()->SetOutputMute(true);
54   return true;
55 }
56
57 bool VolumeController::HandleVolumeDown(const ui::Accelerator& accelerator) {
58   if (accelerator.key_code() == ui::VKEY_VOLUME_DOWN)
59     content::RecordAction(base::UserMetricsAction("Accel_VolumeDown_F9"));
60
61   CrasAudioHandler* audio_handler = CrasAudioHandler::Get();
62   if (audio_handler->IsOutputMuted()) {
63     audio_handler->SetOutputVolumePercent(0);
64   } else {
65     audio_handler->AdjustOutputVolumeByPercent(-kStepPercentage);
66     if (audio_handler->IsOutputVolumeBelowDefaultMuteLvel())
67       audio_handler->SetOutputMute(true);
68     else
69       PlayVolumeAdjustSound();
70   }
71   return true;
72 }
73
74 bool VolumeController::HandleVolumeUp(const ui::Accelerator& accelerator) {
75   if (accelerator.key_code() == ui::VKEY_VOLUME_UP)
76     content::RecordAction(base::UserMetricsAction("Accel_VolumeUp_F10"));
77
78   CrasAudioHandler* audio_handler = CrasAudioHandler::Get();
79   bool play_sound = false;
80   if (audio_handler->IsOutputMuted()) {
81     audio_handler->SetOutputMute(false);
82     audio_handler->AdjustOutputVolumeToAudibleLevel();
83     play_sound = true;
84   } else {
85     play_sound = audio_handler->GetOutputVolumePercent() != 100;
86     audio_handler->AdjustOutputVolumeByPercent(kStepPercentage);
87   }
88
89   if (play_sound)
90     PlayVolumeAdjustSound();
91   return true;
92 }
93
94 void VolumeController::OnOutputVolumeChanged() {
95   CrasAudioHandler* audio_handler = CrasAudioHandler::Get();
96   extensions::DispatchVolumeChangedEvent(
97       audio_handler->GetOutputVolumePercent(),
98       audio_handler->IsOutputMuted());
99 }
100
101 void VolumeController::OnOutputMuteChanged() {
102   CrasAudioHandler* audio_handler = CrasAudioHandler::Get();
103   extensions::DispatchVolumeChangedEvent(
104       audio_handler->GetOutputVolumePercent(),
105       audio_handler->IsOutputMuted());
106 }