Reland the ULONG -> SIZE_T change from 317177
[chromium-blink-merge.git] / extensions / browser / api / audio / audio_api.cc
blob659ae6ce67637ac20228a53e47681c1ab558b37a
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;
19 // static
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);
31 delete service_;
32 service_ = NULL;
35 AudioService* AudioAPI::GetService() const {
36 return service_;
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();
53 DCHECK(service);
54 service->StartGetInfo(base::Bind(&AudioGetInfoFunction::OnGetInfoCompleted,
55 this));
56 return true;
59 void AudioGetInfoFunction::OnGetInfoCompleted(const OutputInfo& output_info,
60 const InputInfo& input_info,
61 bool success) {
62 if (success)
63 results_ = audio::GetInfo::Results::Create(output_info, input_info);
64 else
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();
78 DCHECK(service);
80 service->SetActiveDevices(params->ids);
81 return true;
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();
93 DCHECK(service);
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,
103 volume_value,
104 gain_value))
105 return false;
106 else
107 return true;
110 } // namespace extensions