| OLD | NEW |
| 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" |
| 11 #include "extensions/browser/event_router.h" | 11 #include "extensions/browser/event_router.h" |
| 12 #include "extensions/common/api/audio.h" | 12 #include "extensions/common/api/audio.h" |
| 13 #include "extensions/common/extension.h" |
| 14 #include "extensions/common/features/behavior_feature.h" |
| 15 #include "extensions/common/features/feature_provider.h" |
| 13 | 16 |
| 14 namespace extensions { | 17 namespace extensions { |
| 15 | 18 |
| 16 namespace audio = api::audio; | 19 namespace audio = api::audio; |
| 17 | 20 |
| 21 namespace { |
| 22 |
| 23 // Checks if an extension is whitelisted to use deprecated version of audio API. |
| 24 // TODO(tbarzic): Retire this whitelist and remove the deprecated API methods, |
| 25 // properties and events. This is currently targeted for M-60 |
| 26 // (http://crbug.com/697279). |
| 27 bool CanUseDeprecatedAudioApi(const Extension* extension) { |
| 28 if (!extension) |
| 29 return false; |
| 30 |
| 31 const Feature* allow_deprecated_audio_api_feature = |
| 32 FeatureProvider::GetBehaviorFeatures()->GetFeature( |
| 33 behavior_feature::kAllowDeprecatedAudioApi); |
| 34 if (!allow_deprecated_audio_api_feature) |
| 35 return false; |
| 36 |
| 37 return allow_deprecated_audio_api_feature->IsAvailableToExtension(extension) |
| 38 .is_available(); |
| 39 } |
| 40 |
| 41 bool CanReceiveDeprecatedAudioEvent(content::BrowserContext* context, |
| 42 const Extension* extension, |
| 43 Event* event, |
| 44 const base::DictionaryValue* filter) { |
| 45 return CanUseDeprecatedAudioApi(extension); |
| 46 } |
| 47 |
| 48 } // namespace |
| 49 |
| 18 static base::LazyInstance<BrowserContextKeyedAPIFactory<AudioAPI> > g_factory = | 50 static base::LazyInstance<BrowserContextKeyedAPIFactory<AudioAPI> > g_factory = |
| 19 LAZY_INSTANCE_INITIALIZER; | 51 LAZY_INSTANCE_INITIALIZER; |
| 20 | 52 |
| 21 // static | 53 // static |
| 22 BrowserContextKeyedAPIFactory<AudioAPI>* AudioAPI::GetFactoryInstance() { | 54 BrowserContextKeyedAPIFactory<AudioAPI>* AudioAPI::GetFactoryInstance() { |
| 23 return g_factory.Pointer(); | 55 return g_factory.Pointer(); |
| 24 } | 56 } |
| 25 | 57 |
| 26 AudioAPI::AudioAPI(content::BrowserContext* context) | 58 AudioAPI::AudioAPI(content::BrowserContext* context) |
| 27 : browser_context_(context), service_(AudioService::CreateInstance()) { | 59 : browser_context_(context), service_(AudioService::CreateInstance()) { |
| 28 service_->AddObserver(this); | 60 service_->AddObserver(this); |
| 29 } | 61 } |
| 30 | 62 |
| 31 AudioAPI::~AudioAPI() { | 63 AudioAPI::~AudioAPI() { |
| 32 service_->RemoveObserver(this); | 64 service_->RemoveObserver(this); |
| 33 delete service_; | 65 delete service_; |
| 34 service_ = NULL; | 66 service_ = NULL; |
| 35 } | 67 } |
| 36 | 68 |
| 37 AudioService* AudioAPI::GetService() const { | 69 AudioService* AudioAPI::GetService() const { |
| 38 return service_; | 70 return service_; |
| 39 } | 71 } |
| 40 | 72 |
| 41 void AudioAPI::OnDeviceChanged() { | 73 void AudioAPI::OnDeviceChanged() { |
| 42 if (EventRouter::Get(browser_context_)) { | 74 EventRouter* event_router = EventRouter::Get(browser_context_); |
| 43 std::unique_ptr<Event> event(new Event( | 75 if (!event_router) |
| 44 events::AUDIO_ON_DEVICE_CHANGED, audio::OnDeviceChanged::kEventName, | 76 return; |
| 45 std::unique_ptr<base::ListValue>(new base::ListValue()))); | 77 |
| 46 EventRouter::Get(browser_context_)->BroadcastEvent(std::move(event)); | 78 std::unique_ptr<Event> event(new Event( |
| 47 } | 79 events::AUDIO_ON_DEVICE_CHANGED, audio::OnDeviceChanged::kEventName, |
| 80 std::unique_ptr<base::ListValue>(new base::ListValue()))); |
| 81 event->will_dispatch_callback = base::Bind(&CanReceiveDeprecatedAudioEvent); |
| 82 event_router->BroadcastEvent(std::move(event)); |
| 48 } | 83 } |
| 49 | 84 |
| 50 void AudioAPI::OnLevelChanged(const std::string& id, int level) { | 85 void AudioAPI::OnLevelChanged(const std::string& id, int level) { |
| 51 EventRouter* event_router = EventRouter::Get(browser_context_); | 86 EventRouter* event_router = EventRouter::Get(browser_context_); |
| 52 if (!event_router) | 87 if (!event_router) |
| 53 return; | 88 return; |
| 54 | 89 |
| 55 audio::LevelChangedEvent raw_event; | 90 audio::LevelChangedEvent raw_event; |
| 56 raw_event.device_id = id; | 91 raw_event.device_id = id; |
| 57 raw_event.level = level; | 92 raw_event.level = level; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 audio::OnDeviceListChanged::Create(devices); | 126 audio::OnDeviceListChanged::Create(devices); |
| 92 std::unique_ptr<Event> event(new Event(events::AUDIO_ON_DEVICES_CHANGED, | 127 std::unique_ptr<Event> event(new Event(events::AUDIO_ON_DEVICES_CHANGED, |
| 93 audio::OnDeviceListChanged::kEventName, | 128 audio::OnDeviceListChanged::kEventName, |
| 94 std::move(args))); | 129 std::move(args))); |
| 95 event_router->BroadcastEvent(std::move(event)); | 130 event_router->BroadcastEvent(std::move(event)); |
| 96 } | 131 } |
| 97 | 132 |
| 98 /////////////////////////////////////////////////////////////////////////////// | 133 /////////////////////////////////////////////////////////////////////////////// |
| 99 | 134 |
| 100 ExtensionFunction::ResponseAction AudioGetInfoFunction::Run() { | 135 ExtensionFunction::ResponseAction AudioGetInfoFunction::Run() { |
| 136 if (!CanUseDeprecatedAudioApi(extension())) { |
| 137 return RespondNow( |
| 138 Error("audio.getInfo is deprecated, use audio.getDevices instead.")); |
| 139 } |
| 140 |
| 101 AudioService* service = | 141 AudioService* service = |
| 102 AudioAPI::GetFactoryInstance()->Get(browser_context())->GetService(); | 142 AudioAPI::GetFactoryInstance()->Get(browser_context())->GetService(); |
| 103 DCHECK(service); | 143 DCHECK(service); |
| 104 OutputInfo output_info; | 144 OutputInfo output_info; |
| 105 InputInfo input_info; | 145 InputInfo input_info; |
| 106 if (!service->GetInfo(&output_info, &input_info)) { | 146 if (!service->GetInfo(&output_info, &input_info)) { |
| 107 return RespondNow( | 147 return RespondNow( |
| 108 Error("Error occurred when querying audio device information.")); | 148 Error("Error occurred when querying audio device information.")); |
| 109 } | 149 } |
| 110 | 150 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 AudioAPI::GetFactoryInstance()->Get(browser_context())->GetService(); | 183 AudioAPI::GetFactoryInstance()->Get(browser_context())->GetService(); |
| 144 DCHECK(service); | 184 DCHECK(service); |
| 145 | 185 |
| 146 if (params->ids.as_device_id_lists) { | 186 if (params->ids.as_device_id_lists) { |
| 147 if (!service->SetActiveDeviceLists( | 187 if (!service->SetActiveDeviceLists( |
| 148 params->ids.as_device_id_lists->input, | 188 params->ids.as_device_id_lists->input, |
| 149 params->ids.as_device_id_lists->output)) { | 189 params->ids.as_device_id_lists->output)) { |
| 150 return RespondNow(Error("Failed to set active devices.")); | 190 return RespondNow(Error("Failed to set active devices.")); |
| 151 } | 191 } |
| 152 } else if (params->ids.as_strings) { | 192 } else if (params->ids.as_strings) { |
| 153 // TODO(tbarzic): This way of setting active devices is deprecated - have | 193 if (!CanUseDeprecatedAudioApi(extension())) { |
| 154 // this return error for apps that were not whitelisted for deprecated | 194 return RespondNow( |
| 155 // version of audio API. | 195 Error("String list |ids| is deprecated, use DeviceIdLists type.")); |
| 196 } |
| 156 service->SetActiveDevices(*params->ids.as_strings); | 197 service->SetActiveDevices(*params->ids.as_strings); |
| 157 } | 198 } |
| 158 return RespondNow(NoArguments()); | 199 return RespondNow(NoArguments()); |
| 159 } | 200 } |
| 160 | 201 |
| 161 /////////////////////////////////////////////////////////////////////////////// | 202 /////////////////////////////////////////////////////////////////////////////// |
| 162 | 203 |
| 163 ExtensionFunction::ResponseAction AudioSetPropertiesFunction::Run() { | 204 ExtensionFunction::ResponseAction AudioSetPropertiesFunction::Run() { |
| 164 std::unique_ptr<audio::SetProperties::Params> params( | 205 std::unique_ptr<audio::SetProperties::Params> params( |
| 165 audio::SetProperties::Params::Create(*args_)); | 206 audio::SetProperties::Params::Create(*args_)); |
| 166 EXTENSION_FUNCTION_VALIDATE(params.get()); | 207 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 167 | 208 |
| 168 AudioService* service = | 209 AudioService* service = |
| 169 AudioAPI::GetFactoryInstance()->Get(browser_context())->GetService(); | 210 AudioAPI::GetFactoryInstance()->Get(browser_context())->GetService(); |
| 170 DCHECK(service); | 211 DCHECK(service); |
| 171 | 212 |
| 213 if (!CanUseDeprecatedAudioApi(extension())) { |
| 214 if (params->properties.volume) |
| 215 return RespondNow(Error("|volume| property is deprecated, use |level|.")); |
| 216 |
| 217 if (params->properties.gain) |
| 218 return RespondNow(Error("|gain| property is deprecated, use |level|.")); |
| 219 |
| 220 if (params->properties.is_muted) { |
| 221 return RespondNow( |
| 222 Error("|isMuted| property is deprecated, use |audio.setMute|.")); |
| 223 } |
| 224 } |
| 225 |
| 172 bool level_set = !!params->properties.level; | 226 bool level_set = !!params->properties.level; |
| 173 int level_value = level_set ? *params->properties.level : -1; | 227 int level_value = level_set ? *params->properties.level : -1; |
| 174 | 228 |
| 175 int volume_value = params->properties.volume.get() ? | 229 int volume_value = params->properties.volume.get() ? |
| 176 *params->properties.volume : -1; | 230 *params->properties.volume : -1; |
| 177 | 231 |
| 178 int gain_value = params->properties.gain.get() ? | 232 int gain_value = params->properties.gain.get() ? |
| 179 *params->properties.gain : -1; | 233 *params->properties.gain : -1; |
| 180 | 234 |
| 181 // |volume_value| and |gain_value| are deprecated in favor of |level_value|; | 235 // |volume_value| and |gain_value| are deprecated in favor of |level_value|; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 | 279 |
| 226 bool value = false; | 280 bool value = false; |
| 227 if (!service->GetMute(params->stream_type == audio::STREAM_TYPE_INPUT, | 281 if (!service->GetMute(params->stream_type == audio::STREAM_TYPE_INPUT, |
| 228 &value)) { | 282 &value)) { |
| 229 return RespondNow(Error("Could not get mute state.")); | 283 return RespondNow(Error("Could not get mute state.")); |
| 230 } | 284 } |
| 231 return RespondNow(ArgumentList(audio::GetMute::Results::Create(value))); | 285 return RespondNow(ArgumentList(audio::GetMute::Results::Create(value))); |
| 232 } | 286 } |
| 233 | 287 |
| 234 } // namespace extensions | 288 } // namespace extensions |
| OLD | NEW |