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