| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "chrome/browser/extensions/api/webrtc_audio_private/webrtc_audio_privat
e_api.h" | 5 #include "chrome/browser/extensions/api/webrtc_audio_private/webrtc_audio_privat
e_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/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/task_runner_util.h" | 11 #include "base/task_runner_util.h" |
| 12 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 12 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
| 13 #include "chrome/browser/extensions/extension_tab_util.h" | 13 #include "chrome/browser/extensions/extension_tab_util.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "content/public/browser/media_device_id.h" | 15 #include "content/public/browser/media_device_id.h" |
| 16 #include "content/public/browser/resource_context.h" | |
| 17 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
| 18 #include "extensions/browser/event_router.h" | 17 #include "extensions/browser/event_router.h" |
| 19 #include "extensions/browser/extension_registry.h" | 18 #include "extensions/browser/extension_registry.h" |
| 20 #include "extensions/common/error_utils.h" | 19 #include "extensions/common/error_utils.h" |
| 21 #include "extensions/common/permissions/permissions_data.h" | 20 #include "extensions/common/permissions/permissions_data.h" |
| 22 #include "media/audio/audio_manager_base.h" | 21 #include "media/audio/audio_manager_base.h" |
| 23 #include "media/audio/audio_output_controller.h" | 22 #include "media/audio/audio_output_controller.h" |
| 24 | 23 |
| 25 namespace extensions { | 24 namespace extensions { |
| 26 | 25 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 if (router->ExtensionHasEventListener(extension_id, kEventName) && | 93 if (router->ExtensionHasEventListener(extension_id, kEventName) && |
| 95 extension->permissions_data()->HasAPIPermission("webrtcAudioPrivate")) { | 94 extension->permissions_data()->HasAPIPermission("webrtcAudioPrivate")) { |
| 96 scoped_ptr<Event> event( | 95 scoped_ptr<Event> event( |
| 97 new Event(events::WEBRTC_AUDIO_PRIVATE_ON_SINKS_CHANGED, kEventName, | 96 new Event(events::WEBRTC_AUDIO_PRIVATE_ON_SINKS_CHANGED, kEventName, |
| 98 make_scoped_ptr(new base::ListValue()))); | 97 make_scoped_ptr(new base::ListValue()))); |
| 99 router->DispatchEventToExtension(extension_id, std::move(event)); | 98 router->DispatchEventToExtension(extension_id, std::move(event)); |
| 100 } | 99 } |
| 101 } | 100 } |
| 102 } | 101 } |
| 103 | 102 |
| 104 WebrtcAudioPrivateFunction::WebrtcAudioPrivateFunction() | 103 WebrtcAudioPrivateFunction::WebrtcAudioPrivateFunction() {} |
| 105 : resource_context_(NULL) { | |
| 106 } | |
| 107 | 104 |
| 108 WebrtcAudioPrivateFunction::~WebrtcAudioPrivateFunction() { | 105 WebrtcAudioPrivateFunction::~WebrtcAudioPrivateFunction() { |
| 109 } | 106 } |
| 110 | 107 |
| 111 void WebrtcAudioPrivateFunction::GetOutputDeviceNames() { | 108 void WebrtcAudioPrivateFunction::GetOutputDeviceNames() { |
| 112 scoped_refptr<base::SingleThreadTaskRunner> audio_manager_runner = | 109 scoped_refptr<base::SingleThreadTaskRunner> audio_manager_runner = |
| 113 AudioManager::Get()->GetTaskRunner(); | 110 AudioManager::Get()->GetTaskRunner(); |
| 114 if (!audio_manager_runner->BelongsToCurrentThread()) { | 111 if (!audio_manager_runner->BelongsToCurrentThread()) { |
| 115 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 112 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 116 audio_manager_runner->PostTask( | 113 audio_manager_runner->PostTask( |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 | 191 |
| 195 // We don't hash the default device name, and we always return | 192 // We don't hash the default device name, and we always return |
| 196 // "default" for the default device. There is code in SetActiveSink | 193 // "default" for the default device. There is code in SetActiveSink |
| 197 // that transforms "default" to the empty string, and code in | 194 // that transforms "default" to the empty string, and code in |
| 198 // GetActiveSink that ensures we return "default" if we get the | 195 // GetActiveSink that ensures we return "default" if we get the |
| 199 // empty string as the current device ID. | 196 // empty string as the current device ID. |
| 200 if (raw_id.empty() || raw_id == media::AudioManagerBase::kDefaultDeviceId) | 197 if (raw_id.empty() || raw_id == media::AudioManagerBase::kDefaultDeviceId) |
| 201 return media::AudioManagerBase::kDefaultDeviceId; | 198 return media::AudioManagerBase::kDefaultDeviceId; |
| 202 | 199 |
| 203 GURL security_origin(source_url().GetOrigin()); | 200 GURL security_origin(source_url().GetOrigin()); |
| 204 return content::GetHMACForMediaDeviceID( | 201 return content::GetHMACForMediaDeviceID(device_id_salt(), security_origin, |
| 205 resource_context()->GetMediaDeviceIDSalt(), | 202 raw_id); |
| 206 security_origin, | |
| 207 raw_id); | |
| 208 } | 203 } |
| 209 | 204 |
| 210 void WebrtcAudioPrivateFunction::InitResourceContext() { | 205 void WebrtcAudioPrivateFunction::InitDeviceIDSalt() { |
| 211 resource_context_ = GetProfile()->GetResourceContext(); | 206 device_id_salt_ = GetProfile()->GetResourceContext()->GetMediaDeviceIDSalt(); |
| 212 } | 207 } |
| 213 | 208 |
| 214 content::ResourceContext* WebrtcAudioPrivateFunction::resource_context() const { | 209 const content::ResourceContext::SaltCallback& |
| 215 DCHECK(resource_context_); // Did you forget to InitResourceContext()? | 210 WebrtcAudioPrivateFunction::device_id_salt() const { |
| 216 return resource_context_; | 211 DCHECK(!device_id_salt_.is_null()); |
| 212 return device_id_salt_; |
| 217 } | 213 } |
| 218 | 214 |
| 219 bool WebrtcAudioPrivateGetSinksFunction::RunAsync() { | 215 bool WebrtcAudioPrivateGetSinksFunction::RunAsync() { |
| 220 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 216 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 221 | 217 |
| 222 InitResourceContext(); | 218 InitDeviceIDSalt(); |
| 223 GetOutputDeviceNames(); | 219 GetOutputDeviceNames(); |
| 224 | 220 |
| 225 return true; | 221 return true; |
| 226 } | 222 } |
| 227 | 223 |
| 228 void WebrtcAudioPrivateGetSinksFunction::OnOutputDeviceNames( | 224 void WebrtcAudioPrivateGetSinksFunction::OnOutputDeviceNames( |
| 229 scoped_ptr<AudioDeviceNames> raw_ids) { | 225 scoped_ptr<AudioDeviceNames> raw_ids) { |
| 230 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 226 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 231 | 227 |
| 232 std::vector<linked_ptr<wap::SinkInfo> > results; | 228 std::vector<linked_ptr<wap::SinkInfo> > results; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 255 FROM_HERE, | 251 FROM_HERE, |
| 256 base::Bind(&WebrtcAudioPrivateGetSinksFunction::DoneOnUIThread, this)); | 252 base::Bind(&WebrtcAudioPrivateGetSinksFunction::DoneOnUIThread, this)); |
| 257 } | 253 } |
| 258 | 254 |
| 259 void WebrtcAudioPrivateGetSinksFunction::DoneOnUIThread() { | 255 void WebrtcAudioPrivateGetSinksFunction::DoneOnUIThread() { |
| 260 SendResponse(true); | 256 SendResponse(true); |
| 261 } | 257 } |
| 262 | 258 |
| 263 bool WebrtcAudioPrivateGetActiveSinkFunction::RunAsync() { | 259 bool WebrtcAudioPrivateGetActiveSinkFunction::RunAsync() { |
| 264 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 260 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 265 InitResourceContext(); | 261 InitDeviceIDSalt(); |
| 266 | 262 |
| 267 scoped_ptr<wap::GetActiveSink::Params> params( | 263 scoped_ptr<wap::GetActiveSink::Params> params( |
| 268 wap::GetActiveSink::Params::Create(*args_)); | 264 wap::GetActiveSink::Params::Create(*args_)); |
| 269 EXTENSION_FUNCTION_VALIDATE(params.get()); | 265 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 270 | 266 |
| 271 return GetControllerList(params->request); | 267 return GetControllerList(params->request); |
| 272 } | 268 } |
| 273 | 269 |
| 274 void WebrtcAudioPrivateGetActiveSinkFunction::OnControllerList( | 270 void WebrtcAudioPrivateGetActiveSinkFunction::OnControllerList( |
| 275 const RenderProcessHost::AudioOutputControllerList& controllers) { | 271 const RenderProcessHost::AudioOutputControllerList& controllers) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 WebrtcAudioPrivateSetActiveSinkFunction:: | 312 WebrtcAudioPrivateSetActiveSinkFunction:: |
| 317 ~WebrtcAudioPrivateSetActiveSinkFunction() { | 313 ~WebrtcAudioPrivateSetActiveSinkFunction() { |
| 318 } | 314 } |
| 319 | 315 |
| 320 bool WebrtcAudioPrivateSetActiveSinkFunction::RunAsync() { | 316 bool WebrtcAudioPrivateSetActiveSinkFunction::RunAsync() { |
| 321 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 317 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 322 scoped_ptr<wap::SetActiveSink::Params> params( | 318 scoped_ptr<wap::SetActiveSink::Params> params( |
| 323 wap::SetActiveSink::Params::Create(*args_)); | 319 wap::SetActiveSink::Params::Create(*args_)); |
| 324 EXTENSION_FUNCTION_VALIDATE(params.get()); | 320 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 325 | 321 |
| 326 InitResourceContext(); | 322 InitDeviceIDSalt(); |
| 327 | 323 |
| 328 if (params->request.guest_process_id.get()) { | 324 if (params->request.guest_process_id.get()) { |
| 329 request_info_.guest_process_id.reset( | 325 request_info_.guest_process_id.reset( |
| 330 new int(*params->request.guest_process_id.get())); | 326 new int(*params->request.guest_process_id.get())); |
| 331 } else if (params->request.tab_id.get()) { | 327 } else if (params->request.tab_id.get()) { |
| 332 request_info_.tab_id.reset(new int(*params->request.tab_id.get())); | 328 request_info_.tab_id.reset(new int(*params->request.tab_id.get())); |
| 333 } else { | 329 } else { |
| 334 return false; | 330 return false; |
| 335 } | 331 } |
| 336 | 332 |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 | 414 |
| 419 WebrtcAudioPrivateGetAssociatedSinkFunction:: | 415 WebrtcAudioPrivateGetAssociatedSinkFunction:: |
| 420 ~WebrtcAudioPrivateGetAssociatedSinkFunction() { | 416 ~WebrtcAudioPrivateGetAssociatedSinkFunction() { |
| 421 } | 417 } |
| 422 | 418 |
| 423 bool WebrtcAudioPrivateGetAssociatedSinkFunction::RunAsync() { | 419 bool WebrtcAudioPrivateGetAssociatedSinkFunction::RunAsync() { |
| 424 params_ = wap::GetAssociatedSink::Params::Create(*args_); | 420 params_ = wap::GetAssociatedSink::Params::Create(*args_); |
| 425 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 421 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 426 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 422 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 427 | 423 |
| 428 InitResourceContext(); | 424 InitDeviceIDSalt(); |
| 429 | 425 |
| 430 AudioManager::Get()->GetTaskRunner()->PostTask( | 426 AudioManager::Get()->GetTaskRunner()->PostTask( |
| 431 FROM_HERE, | 427 FROM_HERE, |
| 432 base::Bind(&WebrtcAudioPrivateGetAssociatedSinkFunction:: | 428 base::Bind(&WebrtcAudioPrivateGetAssociatedSinkFunction:: |
| 433 GetDevicesOnDeviceThread, this)); | 429 GetDevicesOnDeviceThread, this)); |
| 434 | 430 |
| 435 return true; | 431 return true; |
| 436 } | 432 } |
| 437 | 433 |
| 438 void WebrtcAudioPrivateGetAssociatedSinkFunction::GetDevicesOnDeviceThread() { | 434 void WebrtcAudioPrivateGetAssociatedSinkFunction::GetDevicesOnDeviceThread() { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 453 | 449 |
| 454 GURL security_origin(params_->security_origin); | 450 GURL security_origin(params_->security_origin); |
| 455 std::string source_id_in_origin(params_->source_id_in_origin); | 451 std::string source_id_in_origin(params_->source_id_in_origin); |
| 456 | 452 |
| 457 // Find the raw source ID for source_id_in_origin. | 453 // Find the raw source ID for source_id_in_origin. |
| 458 std::string raw_source_id; | 454 std::string raw_source_id; |
| 459 for (AudioDeviceNames::const_iterator it = source_devices_.begin(); | 455 for (AudioDeviceNames::const_iterator it = source_devices_.begin(); |
| 460 it != source_devices_.end(); | 456 it != source_devices_.end(); |
| 461 ++it) { | 457 ++it) { |
| 462 const std::string& id = it->unique_id; | 458 const std::string& id = it->unique_id; |
| 463 if (content::DoesMediaDeviceIDMatchHMAC( | 459 if (content::DoesMediaDeviceIDMatchHMAC(device_id_salt(), security_origin, |
| 464 resource_context()->GetMediaDeviceIDSalt(), | 460 source_id_in_origin, id)) { |
| 465 security_origin, | |
| 466 source_id_in_origin, | |
| 467 id)) { | |
| 468 raw_source_id = id; | 461 raw_source_id = id; |
| 469 DVLOG(2) << "Found raw ID " << raw_source_id | 462 DVLOG(2) << "Found raw ID " << raw_source_id |
| 470 << " for source ID in origin " << source_id_in_origin; | 463 << " for source ID in origin " << source_id_in_origin; |
| 471 break; | 464 break; |
| 472 } | 465 } |
| 473 } | 466 } |
| 474 | 467 |
| 475 AudioManager::Get()->GetTaskRunner()->PostTask( | 468 AudioManager::Get()->GetTaskRunner()->PostTask( |
| 476 FROM_HERE, | 469 FROM_HERE, |
| 477 base::Bind(&WebrtcAudioPrivateGetAssociatedSinkFunction:: | 470 base::Bind(&WebrtcAudioPrivateGetAssociatedSinkFunction:: |
| (...skipping 26 matching lines...) Expand all Loading... |
| 504 results_.reset(wap::GetAssociatedSink::Results::Create("").release()); | 497 results_.reset(wap::GetAssociatedSink::Results::Create("").release()); |
| 505 } else { | 498 } else { |
| 506 results_.reset( | 499 results_.reset( |
| 507 wap::GetAssociatedSink::Results::Create(associated_sink_id).release()); | 500 wap::GetAssociatedSink::Results::Create(associated_sink_id).release()); |
| 508 } | 501 } |
| 509 | 502 |
| 510 SendResponse(true); | 503 SendResponse(true); |
| 511 } | 504 } |
| 512 | 505 |
| 513 } // namespace extensions | 506 } // namespace extensions |
| OLD | NEW |