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 (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 void AudioAPI::OnLevelChanged(const std::string
& id
, int level
) {
49 if (EventRouter::Get(browser_context_
)) {
50 scoped_ptr
<base::ListValue
> args
= audio::OnLevelChanged::Create(id
, level
);
51 scoped_ptr
<Event
> event(
52 new Event(audio::OnLevelChanged::kEventName
, args
.Pass()));
53 EventRouter::Get(browser_context_
)->BroadcastEvent(event
.Pass());
57 void AudioAPI::OnMuteChanged(bool is_input
, bool is_muted
) {
58 if (EventRouter::Get(browser_context_
)) {
59 scoped_ptr
<base::ListValue
> args
=
60 audio::OnMuteChanged::Create(is_input
, is_muted
);
61 scoped_ptr
<Event
> event(
62 new Event(audio::OnMuteChanged::kEventName
, args
.Pass()));
63 EventRouter::Get(browser_context_
)->BroadcastEvent(event
.Pass());
67 void AudioAPI::OnDevicesChanged(const DeviceInfoList
& devices
) {
68 if (EventRouter::Get(browser_context_
)) {
69 scoped_ptr
<base::ListValue
> args
= audio::OnDevicesChanged::Create(devices
);
70 scoped_ptr
<Event
> event(
71 new Event(audio::OnDevicesChanged::kEventName
, args
.Pass()));
72 EventRouter::Get(browser_context_
)->BroadcastEvent(event
.Pass());
76 ///////////////////////////////////////////////////////////////////////////////
78 bool AudioGetInfoFunction::RunAsync() {
79 AudioService
* service
=
80 AudioAPI::GetFactoryInstance()->Get(browser_context())->GetService();
82 service
->StartGetInfo(base::Bind(&AudioGetInfoFunction::OnGetInfoCompleted
,
87 void AudioGetInfoFunction::OnGetInfoCompleted(const OutputInfo
& output_info
,
88 const InputInfo
& input_info
,
91 results_
= audio::GetInfo::Results::Create(output_info
, input_info
);
93 SetError("Error occurred when querying audio device information.");
94 SendResponse(success
);
97 ///////////////////////////////////////////////////////////////////////////////
99 bool AudioSetActiveDevicesFunction::RunSync() {
100 scoped_ptr
<audio::SetActiveDevices::Params
> params(
101 audio::SetActiveDevices::Params::Create(*args_
));
102 EXTENSION_FUNCTION_VALIDATE(params
.get());
104 AudioService
* service
=
105 AudioAPI::GetFactoryInstance()->Get(browser_context())->GetService();
108 service
->SetActiveDevices(params
->ids
);
112 ///////////////////////////////////////////////////////////////////////////////
114 bool AudioSetPropertiesFunction::RunSync() {
115 scoped_ptr
<audio::SetProperties::Params
> params(
116 audio::SetProperties::Params::Create(*args_
));
117 EXTENSION_FUNCTION_VALIDATE(params
.get());
119 AudioService
* service
=
120 AudioAPI::GetFactoryInstance()->Get(browser_context())->GetService();
123 int volume_value
= params
->properties
.volume
.get() ?
124 *params
->properties
.volume
: -1;
126 int gain_value
= params
->properties
.gain
.get() ?
127 *params
->properties
.gain
: -1;
129 if (!service
->SetDeviceProperties(params
->id
,
130 params
->properties
.is_muted
,
138 ///////////////////////////////////////////////////////////////////////////////
140 } // namespace extensions