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
= 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 events::AUDIO_ON_DEVICE_CHANGED
, 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(new Event(events::AUDIO_ON_LEVEL_CHANGED
,
52 audio::OnLevelChanged::kEventName
,
54 EventRouter::Get(browser_context_
)->BroadcastEvent(event
.Pass());
58 void AudioAPI::OnMuteChanged(bool is_input
, bool is_muted
) {
59 if (EventRouter::Get(browser_context_
)) {
60 scoped_ptr
<base::ListValue
> args
=
61 audio::OnMuteChanged::Create(is_input
, is_muted
);
62 scoped_ptr
<Event
> event(new Event(events::AUDIO_ON_MUTE_CHANGED
,
63 audio::OnMuteChanged::kEventName
,
65 EventRouter::Get(browser_context_
)->BroadcastEvent(event
.Pass());
69 void AudioAPI::OnDevicesChanged(const DeviceInfoList
& devices
) {
70 if (EventRouter::Get(browser_context_
)) {
71 scoped_ptr
<base::ListValue
> args
= audio::OnDevicesChanged::Create(devices
);
72 scoped_ptr
<Event
> event(new Event(events::AUDIO_ON_DEVICES_CHANGED
,
73 audio::OnDevicesChanged::kEventName
,
75 EventRouter::Get(browser_context_
)->BroadcastEvent(event
.Pass());
79 ///////////////////////////////////////////////////////////////////////////////
81 bool AudioGetInfoFunction::RunAsync() {
82 AudioService
* service
=
83 AudioAPI::GetFactoryInstance()->Get(browser_context())->GetService();
85 service
->StartGetInfo(base::Bind(&AudioGetInfoFunction::OnGetInfoCompleted
,
90 void AudioGetInfoFunction::OnGetInfoCompleted(const OutputInfo
& output_info
,
91 const InputInfo
& input_info
,
94 results_
= audio::GetInfo::Results::Create(output_info
, input_info
);
96 SetError("Error occurred when querying audio device information.");
97 SendResponse(success
);
100 ///////////////////////////////////////////////////////////////////////////////
102 bool AudioSetActiveDevicesFunction::RunSync() {
103 scoped_ptr
<audio::SetActiveDevices::Params
> params(
104 audio::SetActiveDevices::Params::Create(*args_
));
105 EXTENSION_FUNCTION_VALIDATE(params
.get());
107 AudioService
* service
=
108 AudioAPI::GetFactoryInstance()->Get(browser_context())->GetService();
111 service
->SetActiveDevices(params
->ids
);
115 ///////////////////////////////////////////////////////////////////////////////
117 bool AudioSetPropertiesFunction::RunSync() {
118 scoped_ptr
<audio::SetProperties::Params
> params(
119 audio::SetProperties::Params::Create(*args_
));
120 EXTENSION_FUNCTION_VALIDATE(params
.get());
122 AudioService
* service
=
123 AudioAPI::GetFactoryInstance()->Get(browser_context())->GetService();
126 int volume_value
= params
->properties
.volume
.get() ?
127 *params
->properties
.volume
: -1;
129 int gain_value
= params
->properties
.gain
.get() ?
130 *params
->properties
.gain
: -1;
132 if (!service
->SetDeviceProperties(params
->id
,
133 params
->properties
.is_muted
,
141 ///////////////////////////////////////////////////////////////////////////////
143 } // namespace extensions