Chromium Code Reviews| Index: extensions/browser/api/audio/audio_api.cc |
| diff --git a/extensions/browser/api/audio/audio_api.cc b/extensions/browser/api/audio/audio_api.cc |
| index cd6c11ea24a102a12c4357b07eb4770cf8ad1c42..05abd6ae770f056d8e44994b33fb5b2f1e74e860 100644 |
| --- a/extensions/browser/api/audio/audio_api.cc |
| +++ b/extensions/browser/api/audio/audio_api.cc |
| @@ -10,11 +10,39 @@ |
| #include "base/values.h" |
| #include "extensions/browser/event_router.h" |
| #include "extensions/common/api/audio.h" |
| +#include "extensions/common/extension.h" |
| +#include "extensions/common/features/behavior_feature.h" |
| +#include "extensions/common/features/feature_provider.h" |
| namespace extensions { |
| namespace audio = api::audio; |
| +namespace { |
| + |
| +bool CanUseDeprecatedAudioApi(const Extension* extension) { |
|
Devlin
2017/02/22 14:13:14
Add a TODO to remove this, preferably with a concr
tbarzic
2017/02/22 19:35:18
Done.
I've pinged mnilsson about a reasonable tim
tbarzic
2017/03/01 17:36:24
I've added a comment to target removal of this for
|
| + if (!extension) |
| + return false; |
| + |
| + const Feature* allow_deprecated_audio_api_feature = |
| + FeatureProvider::GetBehaviorFeatures()->GetFeature( |
| + behavior_feature::kAllowDeprecatedAudioApi); |
| + if (!allow_deprecated_audio_api_feature) |
| + return false; |
| + |
| + return allow_deprecated_audio_api_feature->IsAvailableToExtension(extension) |
| + .is_available(); |
| +} |
| + |
| +bool CanReceiveDeprecatedAudioEvent(content::BrowserContext* context, |
| + const Extension* extension, |
| + Event* event, |
| + const base::DictionaryValue* filter) { |
| + return CanUseDeprecatedAudioApi(extension); |
| +} |
| + |
| +} // namespace |
| + |
| static base::LazyInstance<BrowserContextKeyedAPIFactory<AudioAPI> > g_factory = |
| LAZY_INSTANCE_INITIALIZER; |
| @@ -39,12 +67,15 @@ AudioService* AudioAPI::GetService() const { |
| } |
| void AudioAPI::OnDeviceChanged() { |
| - if (EventRouter::Get(browser_context_)) { |
| - std::unique_ptr<Event> event(new Event( |
| - events::AUDIO_ON_DEVICE_CHANGED, audio::OnDeviceChanged::kEventName, |
| - std::unique_ptr<base::ListValue>(new base::ListValue()))); |
| - EventRouter::Get(browser_context_)->BroadcastEvent(std::move(event)); |
| - } |
| + EventRouter* event_router = EventRouter::Get(browser_context_); |
| + if (!event_router) |
| + return; |
| + |
| + std::unique_ptr<Event> event(new Event( |
| + events::AUDIO_ON_DEVICE_CHANGED, audio::OnDeviceChanged::kEventName, |
| + std::unique_ptr<base::ListValue>(new base::ListValue()))); |
| + event->will_dispatch_callback = base::Bind(&CanReceiveDeprecatedAudioEvent); |
| + event_router->BroadcastEvent(std::move(event)); |
| } |
| void AudioAPI::OnLevelChanged(const std::string& id, int level) { |
| @@ -98,6 +129,9 @@ void AudioAPI::OnDevicesChanged(const DeviceInfoList& devices) { |
| /////////////////////////////////////////////////////////////////////////////// |
| ExtensionFunction::ResponseAction AudioGetInfoFunction::Run() { |
| + if (!CanUseDeprecatedAudioApi(extension())) |
| + return RespondNow(Error("Not allowed.")); |
|
Devlin
2017/02/22 14:13:14
Should this be a little more detailed? E.g. "audi
tbarzic
2017/02/22 19:35:18
Done.
|
| + |
| AudioService* service = |
| AudioAPI::GetFactoryInstance()->Get(browser_context())->GetService(); |
| DCHECK(service); |
| @@ -150,9 +184,9 @@ ExtensionFunction::ResponseAction AudioSetActiveDevicesFunction::Run() { |
| return RespondNow(Error("Failed to set active devices.")); |
| } |
| } else if (params->ids.as_strings) { |
| - // TODO(tbarzic): This way of setting active devices is deprecated - have |
| - // this return error for apps that were not whitelisted for deprecated |
| - // version of audio API. |
| + if (!CanUseDeprecatedAudioApi(extension())) { |
| + return RespondNow(Error("|ids| should have DeviceIdLists type.")); |
| + } |
| service->SetActiveDevices(*params->ids.as_strings); |
| } |
| return RespondNow(NoArguments()); |
| @@ -169,6 +203,19 @@ ExtensionFunction::ResponseAction AudioSetPropertiesFunction::Run() { |
| AudioAPI::GetFactoryInstance()->Get(browser_context())->GetService(); |
| DCHECK(service); |
| + if (!CanUseDeprecatedAudioApi(extension())) { |
| + if (params->properties.volume) |
| + return RespondNow(Error("|volume| property not allowed - use |level|.")); |
| + |
| + if (params->properties.gain) |
| + return RespondNow(Error("|gain| property not allowed - use |level|.")); |
| + |
| + if (params->properties.is_muted) { |
| + return RespondNow( |
| + Error("|isMuted| property not allowed - use |audio.setMute|.")); |
| + } |
| + } |
| + |
| bool level_set = !!params->properties.level; |
| int level_value = level_set ? *params->properties.level : -1; |