Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(892)

Side by Side Diff: extensions/browser/api/audio/audio_api.cc

Issue 2635983006: Final cleanup pass over audio device API (Closed)
Patch Set: rebase Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "extensions/browser/api/audio/audio_api.h" 5 #include "extensions/browser/api/audio/audio_api.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/lazy_instance.h" 9 #include "base/lazy_instance.h"
10 #include "base/values.h" 10 #include "base/values.h"
(...skipping 30 matching lines...) Expand all
41 void AudioAPI::OnDeviceChanged() { 41 void AudioAPI::OnDeviceChanged() {
42 if (EventRouter::Get(browser_context_)) { 42 if (EventRouter::Get(browser_context_)) {
43 std::unique_ptr<Event> event(new Event( 43 std::unique_ptr<Event> event(new Event(
44 events::AUDIO_ON_DEVICE_CHANGED, audio::OnDeviceChanged::kEventName, 44 events::AUDIO_ON_DEVICE_CHANGED, audio::OnDeviceChanged::kEventName,
45 std::unique_ptr<base::ListValue>(new base::ListValue()))); 45 std::unique_ptr<base::ListValue>(new base::ListValue())));
46 EventRouter::Get(browser_context_)->BroadcastEvent(std::move(event)); 46 EventRouter::Get(browser_context_)->BroadcastEvent(std::move(event));
47 } 47 }
48 } 48 }
49 49
50 void AudioAPI::OnLevelChanged(const std::string& id, int level) { 50 void AudioAPI::OnLevelChanged(const std::string& id, int level) {
51 if (EventRouter::Get(browser_context_)) { 51 EventRouter* event_router = EventRouter::Get(browser_context_);
52 std::unique_ptr<base::ListValue> args = 52 if (!event_router)
53 audio::OnLevelChanged::Create(id, level); 53 return;
54 std::unique_ptr<Event> event(new Event(events::AUDIO_ON_LEVEL_CHANGED, 54
55 audio::OnLevelChanged::kEventName, 55 audio::LevelChangedEvent raw_event;
56 std::move(args))); 56 raw_event.device_id = id;
57 EventRouter::Get(browser_context_)->BroadcastEvent(std::move(event)); 57 raw_event.level = level;
58 } 58
59 std::unique_ptr<base::ListValue> event_args =
60 audio::OnLevelChanged::Create(raw_event);
61 std::unique_ptr<Event> event(new Event(events::AUDIO_ON_LEVEL_CHANGED,
62 audio::OnLevelChanged::kEventName,
63 std::move(event_args)));
64 event_router->BroadcastEvent(std::move(event));
59 } 65 }
60 66
61 void AudioAPI::OnMuteChanged(bool is_input, bool is_muted) { 67 void AudioAPI::OnMuteChanged(bool is_input, bool is_muted) {
62 EventRouter* event_router = EventRouter::Get(browser_context_); 68 EventRouter* event_router = EventRouter::Get(browser_context_);
63 if (!event_router) 69 if (!event_router)
64 return; 70 return;
65 71
66 // Dispatch onMuteChanged event. 72 // Dispatch onMuteChanged event.
67 audio::MuteChangedEvent raw_event; 73 audio::MuteChangedEvent raw_event;
68 raw_event.stream_type = 74 raw_event.stream_type =
69 is_input ? audio::STREAM_TYPE_INPUT : audio::STREAM_TYPE_OUTPUT; 75 is_input ? audio::STREAM_TYPE_INPUT : audio::STREAM_TYPE_OUTPUT;
70 raw_event.is_muted = is_muted; 76 raw_event.is_muted = is_muted;
71 std::unique_ptr<base::ListValue> event_args = 77 std::unique_ptr<base::ListValue> event_args =
72 audio::OnMuteChanged::Create(raw_event); 78 audio::OnMuteChanged::Create(raw_event);
73 std::unique_ptr<Event> event(new Event(events::AUDIO_ON_MUTE_CHANGED, 79 std::unique_ptr<Event> event(new Event(events::AUDIO_ON_MUTE_CHANGED,
74 audio::OnMuteChanged::kEventName, 80 audio::OnMuteChanged::kEventName,
75 std::move(event_args))); 81 std::move(event_args)));
76 event_router->BroadcastEvent(std::move(event)); 82 event_router->BroadcastEvent(std::move(event));
77 } 83 }
78 84
79 void AudioAPI::OnDevicesChanged(const DeviceInfoList& devices) { 85 void AudioAPI::OnDevicesChanged(const DeviceInfoList& devices) {
80 if (EventRouter::Get(browser_context_)) { 86 EventRouter* event_router = EventRouter::Get(browser_context_);
81 std::unique_ptr<base::ListValue> args = 87 if (!event_router)
82 audio::OnDevicesChanged::Create(devices); 88 return;
83 std::unique_ptr<Event> event(new Event(events::AUDIO_ON_DEVICES_CHANGED, 89
84 audio::OnDevicesChanged::kEventName, 90 std::unique_ptr<base::ListValue> args =
85 std::move(args))); 91 audio::OnDeviceListChanged::Create(devices);
86 EventRouter::Get(browser_context_)->BroadcastEvent(std::move(event)); 92 std::unique_ptr<Event> event(new Event(events::AUDIO_ON_DEVICES_CHANGED,
87 } 93 audio::OnDeviceListChanged::kEventName,
94 std::move(args)));
95 event_router->BroadcastEvent(std::move(event));
88 } 96 }
89 97
90 /////////////////////////////////////////////////////////////////////////////// 98 ///////////////////////////////////////////////////////////////////////////////
91 99
92 ExtensionFunction::ResponseAction AudioGetInfoFunction::Run() { 100 ExtensionFunction::ResponseAction AudioGetInfoFunction::Run() {
93 AudioService* service = 101 AudioService* service =
94 AudioAPI::GetFactoryInstance()->Get(browser_context())->GetService(); 102 AudioAPI::GetFactoryInstance()->Get(browser_context())->GetService();
95 DCHECK(service); 103 DCHECK(service);
96 OutputInfo output_info; 104 OutputInfo output_info;
97 InputInfo input_info; 105 InputInfo input_info;
98 if (!service->GetInfo(&output_info, &input_info)) { 106 if (!service->GetInfo(&output_info, &input_info)) {
99 return RespondNow( 107 return RespondNow(
100 Error("Error occurred when querying audio device information.")); 108 Error("Error occurred when querying audio device information."));
101 } 109 }
102 110
103 return RespondNow( 111 return RespondNow(
104 ArgumentList(audio::GetInfo::Results::Create(output_info, input_info))); 112 ArgumentList(audio::GetInfo::Results::Create(output_info, input_info)));
105 } 113 }
106 114
107 /////////////////////////////////////////////////////////////////////////////// 115 ///////////////////////////////////////////////////////////////////////////////
108 116
117 ExtensionFunction::ResponseAction AudioGetDevicesFunction::Run() {
118 std::unique_ptr<audio::GetDevices::Params> params(
119 audio::GetDevices::Params::Create(*args_));
120 EXTENSION_FUNCTION_VALIDATE(params.get());
121
122 AudioService* service =
123 AudioAPI::GetFactoryInstance()->Get(browser_context())->GetService();
124 DCHECK(service);
125
126 std::vector<api::audio::AudioDeviceInfo> devices;
127 if (!service->GetDevices(params->filter, &devices)) {
128 return RespondNow(
129 Error("Error occurred when querying audio device information."));
130 }
131
132 return RespondNow(ArgumentList(audio::GetDevices::Results::Create(devices)));
133 }
134
135 ///////////////////////////////////////////////////////////////////////////////
136
109 ExtensionFunction::ResponseAction AudioSetActiveDevicesFunction::Run() { 137 ExtensionFunction::ResponseAction AudioSetActiveDevicesFunction::Run() {
110 std::unique_ptr<audio::SetActiveDevices::Params> params( 138 std::unique_ptr<audio::SetActiveDevices::Params> params(
111 audio::SetActiveDevices::Params::Create(*args_)); 139 audio::SetActiveDevices::Params::Create(*args_));
112 EXTENSION_FUNCTION_VALIDATE(params.get()); 140 EXTENSION_FUNCTION_VALIDATE(params.get());
113 141
114 AudioService* service = 142 AudioService* service =
115 AudioAPI::GetFactoryInstance()->Get(browser_context())->GetService(); 143 AudioAPI::GetFactoryInstance()->Get(browser_context())->GetService();
116 DCHECK(service); 144 DCHECK(service);
117 145
118 if (params->ids.as_device_id_lists) { 146 if (params->ids.as_device_id_lists) {
(...skipping 15 matching lines...) Expand all
134 162
135 ExtensionFunction::ResponseAction AudioSetPropertiesFunction::Run() { 163 ExtensionFunction::ResponseAction AudioSetPropertiesFunction::Run() {
136 std::unique_ptr<audio::SetProperties::Params> params( 164 std::unique_ptr<audio::SetProperties::Params> params(
137 audio::SetProperties::Params::Create(*args_)); 165 audio::SetProperties::Params::Create(*args_));
138 EXTENSION_FUNCTION_VALIDATE(params.get()); 166 EXTENSION_FUNCTION_VALIDATE(params.get());
139 167
140 AudioService* service = 168 AudioService* service =
141 AudioAPI::GetFactoryInstance()->Get(browser_context())->GetService(); 169 AudioAPI::GetFactoryInstance()->Get(browser_context())->GetService();
142 DCHECK(service); 170 DCHECK(service);
143 171
172 int level_value =
173 params->properties.level.get() ? *params->properties.level : -1;
174
144 int volume_value = params->properties.volume.get() ? 175 int volume_value = params->properties.volume.get() ?
145 *params->properties.volume : -1; 176 *params->properties.volume : -1;
146 177
147 int gain_value = params->properties.gain.get() ? 178 int gain_value = params->properties.gain.get() ?
148 *params->properties.gain : -1; 179 *params->properties.gain : -1;
149 180
150 if (!service->SetDeviceSoundLevel(params->id, volume_value, gain_value)) 181 if (!service->SetDeviceSoundLevel(
182 params->id, level_value >= 0 ? level_value : volume_value,
183 level_value >= 0 ? level_value : gain_value))
jennyz 2017/02/02 01:10:44 Please add a comment about the migration.
tbarzic 2017/02/02 01:24:41 Done.
151 return RespondNow(Error("Could not set volume/gain properties")); 184 return RespondNow(Error("Could not set volume/gain properties"));
152 185
153 if (params->properties.is_muted.get() && 186 if (params->properties.is_muted.get() &&
154 !service->SetMuteForDevice(params->id, *params->properties.is_muted)) { 187 !service->SetMuteForDevice(params->id, *params->properties.is_muted)) {
155 return RespondNow(Error("Could not set mute property.")); 188 return RespondNow(Error("Could not set mute property."));
156 } 189 }
157 190
158 return RespondNow(NoArguments()); 191 return RespondNow(NoArguments());
159 } 192 }
160 193
(...skipping 28 matching lines...) Expand all
189 222
190 bool value = false; 223 bool value = false;
191 if (!service->GetMute(params->stream_type == audio::STREAM_TYPE_INPUT, 224 if (!service->GetMute(params->stream_type == audio::STREAM_TYPE_INPUT,
192 &value)) { 225 &value)) {
193 return RespondNow(Error("Could not get mute state.")); 226 return RespondNow(Error("Could not get mute state."));
194 } 227 }
195 return RespondNow(ArgumentList(audio::GetMute::Results::Create(value))); 228 return RespondNow(ArgumentList(audio::GetMute::Results::Create(value)));
196 } 229 }
197 230
198 } // namespace extensions 231 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698