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. 4 5 #include "chrome/browser/extensions/api/audio/audio_api.h" 6 7 #include "base/lazy_instance.h" 8 #include "base/values.h" 9 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/common/extensions/api/audio.h" 11 #include "extensions/browser/event_router.h" 12 13 namespace extensions { 14 15 namespace audio = api::audio; 16 17 static base::LazyInstance<BrowserContextKeyedAPIFactory<AudioAPI> > g_factory = 18 LAZY_INSTANCE_INITIALIZER; 19 20 // static 21 BrowserContextKeyedAPIFactory<AudioAPI>* AudioAPI::GetFactoryInstance() { 22 return g_factory.Pointer(); 23 } 24 25 AudioAPI::AudioAPI(content::BrowserContext* context) 26 : browser_context_(context), service_(AudioService::CreateInstance()) { 27 service_->AddObserver(this); 28 } 29 30 AudioAPI::~AudioAPI() { 31 service_->RemoveObserver(this); 32 delete service_; 33 service_ = NULL; 34 } 35 36 AudioService* AudioAPI::GetService() const { 37 return service_; 38 } 39 40 void AudioAPI::OnDeviceChanged() { 41 if (browser_context_ && EventRouter::Get(browser_context_)) { 42 scoped_ptr<Event> event(new Event( 43 audio::OnDeviceChanged::kEventName, 44 scoped_ptr<base::ListValue>(new base::ListValue()))); 45 EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass()); 46 } 47 } 48 49 bool AudioGetInfoFunction::RunAsync() { 50 AudioService* service = 51 AudioAPI::GetFactoryInstance()->Get(GetProfile())->GetService(); 52 DCHECK(service); 53 service->StartGetInfo(base::Bind(&AudioGetInfoFunction::OnGetInfoCompleted, 54 this)); 55 return true; 56 } 57 58 void AudioGetInfoFunction::OnGetInfoCompleted(const OutputInfo& output_info, 59 const InputInfo& input_info, 60 bool success) { 61 if (success) 62 results_ = api::audio::GetInfo::Results::Create(output_info, input_info); 63 else 64 SetError("Error occurred when querying audio device information."); 65 SendResponse(success); 66 } 67 68 bool AudioSetActiveDevicesFunction::RunSync() { 69 scoped_ptr<api::audio::SetActiveDevices::Params> params( 70 api::audio::SetActiveDevices::Params::Create(*args_)); 71 EXTENSION_FUNCTION_VALIDATE(params.get()); 72 73 AudioService* service = 74 AudioAPI::GetFactoryInstance()->Get(GetProfile())->GetService(); 75 DCHECK(service); 76 77 service->SetActiveDevices(params->ids); 78 return true; 79 } 80 81 bool AudioSetPropertiesFunction::RunSync() { 82 scoped_ptr<api::audio::SetProperties::Params> params( 83 api::audio::SetProperties::Params::Create(*args_)); 84 EXTENSION_FUNCTION_VALIDATE(params.get()); 85 86 AudioService* service = 87 AudioAPI::GetFactoryInstance()->Get(GetProfile())->GetService(); 88 DCHECK(service); 89 90 int volume_value = params->properties.volume.get() ? 91 *params->properties.volume : -1; 92 93 int gain_value = params->properties.gain.get() ? 94 *params->properties.gain : -1; 95 96 if (!service->SetDeviceProperties(params->id, 97 params->properties.is_muted, 98 volume_value, 99 gain_value)) 100 return false; 101 else 102 return true; 103 } 104 105 } // namespace extensions 106