1 // Copyright (c) 2013 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/extensions/api/audio/audio_api.h"
7 #include "base/lazy_instance.h"
8 #include "base/values.h"
9 #include "chrome/browser/extensions/extension_system.h"
10 #include "chrome/common/extensions/api/audio.h"
11 #include "extensions/browser/event_router.h"
13 namespace extensions {
15 namespace audio = api::audio;
17 static base::LazyInstance<ProfileKeyedAPIFactory<AudioAPI> >
18 g_factory = LAZY_INSTANCE_INITIALIZER;
21 ProfileKeyedAPIFactory<AudioAPI>* AudioAPI::GetFactoryInstance() {
22 return g_factory.Pointer();
25 AudioAPI::AudioAPI(Profile* profile)
27 service_(AudioService::CreateInstance()) {
28 service_->AddObserver(this);
31 AudioAPI::~AudioAPI() {
32 service_->RemoveObserver(this);
37 AudioService* AudioAPI::GetService() const {
41 void AudioAPI::OnDeviceChanged() {
42 if (profile_ && ExtensionSystem::Get(profile_)->event_router()) {
43 scoped_ptr<Event> event(new Event(
44 audio::OnDeviceChanged::kEventName,
45 scoped_ptr<base::ListValue>(new base::ListValue())));
46 ExtensionSystem::Get(profile_)->event_router()->BroadcastEvent(
51 bool AudioGetInfoFunction::RunImpl() {
52 AudioService* service =
53 AudioAPI::GetFactoryInstance()->GetForProfile(GetProfile())->GetService();
55 service->StartGetInfo(base::Bind(&AudioGetInfoFunction::OnGetInfoCompleted,
60 void AudioGetInfoFunction::OnGetInfoCompleted(const OutputInfo& output_info,
61 const InputInfo& input_info,
64 results_ = api::audio::GetInfo::Results::Create(output_info, input_info);
66 SetError("Error occurred when querying audio device information.");
67 SendResponse(success);
70 bool AudioSetActiveDevicesFunction::RunImpl() {
71 scoped_ptr<api::audio::SetActiveDevices::Params> params(
72 api::audio::SetActiveDevices::Params::Create(*args_));
73 EXTENSION_FUNCTION_VALIDATE(params.get());
75 AudioService* service =
76 AudioAPI::GetFactoryInstance()->GetForProfile(GetProfile())->GetService();
79 service->SetActiveDevices(params->ids);
83 bool AudioSetPropertiesFunction::RunImpl() {
84 scoped_ptr<api::audio::SetProperties::Params> params(
85 api::audio::SetProperties::Params::Create(*args_));
86 EXTENSION_FUNCTION_VALIDATE(params.get());
88 AudioService* service =
89 AudioAPI::GetFactoryInstance()->GetForProfile(GetProfile())->GetService();
92 int volume_value = params->properties.volume.get() ?
93 *params->properties.volume : -1;
95 int gain_value = params->properties.gain.get() ?
96 *params->properties.gain : -1;
98 if (!service->SetDeviceProperties(params->id,
99 params->properties.is_muted,
107 } // namespace extensions