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 "extensions/browser/api/audio/audio_api.h"
7 #include "base/lazy_instance.h"
8 #include "base/values.h"
9 #include "extensions/browser/event_router.h"
10 #include "extensions/common/api/audio.h"
12 namespace extensions
{
14 namespace audio
= core_api::audio
;
16 static base::LazyInstance
<BrowserContextKeyedAPIFactory
<AudioAPI
> > g_factory
=
17 LAZY_INSTANCE_INITIALIZER
;
20 BrowserContextKeyedAPIFactory
<AudioAPI
>* AudioAPI::GetFactoryInstance() {
21 return g_factory
.Pointer();
24 AudioAPI::AudioAPI(content::BrowserContext
* context
)
25 : browser_context_(context
), service_(AudioService::CreateInstance()) {
26 service_
->AddObserver(this);
29 AudioAPI::~AudioAPI() {
30 service_
->RemoveObserver(this);
35 AudioService
* AudioAPI::GetService() const {
39 void AudioAPI::OnDeviceChanged() {
40 if (browser_context_
&& EventRouter::Get(browser_context_
)) {
41 scoped_ptr
<Event
> event(new Event(
42 audio::OnDeviceChanged::kEventName
,
43 scoped_ptr
<base::ListValue
>(new base::ListValue())));
44 EventRouter::Get(browser_context_
)->BroadcastEvent(event
.Pass());
48 ///////////////////////////////////////////////////////////////////////////////
50 bool AudioGetInfoFunction::RunAsync() {
51 AudioService
* service
=
52 AudioAPI::GetFactoryInstance()->Get(browser_context())->GetService();
54 service
->StartGetInfo(base::Bind(&AudioGetInfoFunction::OnGetInfoCompleted
,
59 void AudioGetInfoFunction::OnGetInfoCompleted(const OutputInfo
& output_info
,
60 const InputInfo
& input_info
,
63 results_
= audio::GetInfo::Results::Create(output_info
, input_info
);
65 SetError("Error occurred when querying audio device information.");
66 SendResponse(success
);
69 ///////////////////////////////////////////////////////////////////////////////
71 bool AudioSetActiveDevicesFunction::RunSync() {
72 scoped_ptr
<audio::SetActiveDevices::Params
> params(
73 audio::SetActiveDevices::Params::Create(*args_
));
74 EXTENSION_FUNCTION_VALIDATE(params
.get());
76 AudioService
* service
=
77 AudioAPI::GetFactoryInstance()->Get(browser_context())->GetService();
80 service
->SetActiveDevices(params
->ids
);
84 ///////////////////////////////////////////////////////////////////////////////
86 bool AudioSetPropertiesFunction::RunSync() {
87 scoped_ptr
<audio::SetProperties::Params
> params(
88 audio::SetProperties::Params::Create(*args_
));
89 EXTENSION_FUNCTION_VALIDATE(params
.get());
91 AudioService
* service
=
92 AudioAPI::GetFactoryInstance()->Get(browser_context())->GetService();
95 int volume_value
= params
->properties
.volume
.get() ?
96 *params
->properties
.volume
: -1;
98 int gain_value
= params
->properties
.gain
.get() ?
99 *params
->properties
.gain
: -1;
101 if (!service
->SetDeviceProperties(params
->id
,
102 params
->properties
.is_muted
,
110 } // namespace extensions