Chromium Code Reviews| Index: android_webview/native/permission/media_access_permission_request.cc |
| diff --git a/android_webview/native/permission/media_access_permission_request.cc b/android_webview/native/permission/media_access_permission_request.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ad841af1511ef040d41bf1698fdffc4964dea9bd |
| --- /dev/null |
| +++ b/android_webview/native/permission/media_access_permission_request.cc |
| @@ -0,0 +1,103 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "android_webview/native/permission/media_access_permission_request.h" |
| + |
| +#include "android_webview/native/permission/aw_permission_request.h" |
| +#include "content/public/browser/media_capture_devices.h" |
| + |
| +using content::MediaCaptureDevices; |
| +using content::MediaStreamDevice; |
| +using content::MediaStreamDevices; |
| + |
| +namespace android_webview { |
| + |
| +namespace { |
| + |
| +// Finds a device in |devices| that has |device_id|, or NULL if not found. |
| +const content::MediaStreamDevice* FindDeviceById( |
| + const MediaStreamDevices& devices, |
| + const std::string& device_id) { |
| + MediaStreamDevices::const_iterator iter = devices.begin(); |
| + for (; iter != devices.end(); ++iter) { |
| + if (iter->id == device_id) { |
| + return &(*iter); |
| + } |
| + } |
| + return NULL; |
| +}; |
| + |
| +} // namespace |
| + |
| +MediaAccessPermissionRequest::MediaAccessPermissionRequest( |
| + const content::MediaStreamRequest& request, |
| + const content::MediaResponseCallback& callback) |
| + : request_(request), |
| + callback_(callback) { |
| + MediaCaptureDevices::GetInstance()->GetAudioCaptureDevices(); |
|
benm (inactive)
2014/04/16 14:51:10
Should we store the result? And why not video too?
michaelbai
2014/04/22 20:53:51
Sorry, we don't need this, removed.
On 2014/04/16
|
| +} |
| + |
| +MediaAccessPermissionRequest::~MediaAccessPermissionRequest() { |
| +} |
| + |
| +void MediaAccessPermissionRequest::OnRequestResult(bool allowed) { |
| + scoped_ptr<content::MediaStreamUI> ui; |
| + MediaStreamDevices devices; |
| + if (!allowed) { |
| + callback_.Run(devices, content::MEDIA_DEVICE_PERMISSION_DENIED, ui.Pass()); |
| + return; |
| + } |
| + |
| + if (request_.audio_type == content::MEDIA_DEVICE_AUDIO_CAPTURE) { |
| + const MediaStreamDevices& audio_devices = |
| + MediaCaptureDevices::GetInstance()->GetAudioCaptureDevices(); |
| + if (!audio_devices.empty()) { |
|
mkosiba (inactive)
2014/04/17 10:34:06
nit: early return would be cleaner
michaelbai
2014/04/22 20:53:51
Can't early return here, as video_devices might no
|
| + bool audio_device_found = false; |
| + if (!request_.requested_audio_device_id.empty()) { |
| + const MediaStreamDevice* audio_device = FindDeviceById( |
| + audio_devices, request_.requested_audio_device_id); |
| + if (audio_device != NULL) { |
| + devices.push_back(*audio_device); |
| + audio_device_found = true; |
| + } |
| + } |
| + if (!audio_device_found) |
|
mkosiba (inactive)
2014/04/17 10:34:06
could you explain why this is desired?
michaelbai
2014/04/22 20:53:51
The device id in request is optional, clank choose
|
| + devices.push_back(audio_devices[0]); |
| + } |
| + } |
| + |
| + if (request_.video_type == content::MEDIA_DEVICE_VIDEO_CAPTURE) { |
| + const MediaStreamDevices& video_devices = |
| + MediaCaptureDevices::GetInstance()->GetVideoCaptureDevices(); |
| + if (!video_devices.empty()) { |
| + bool video_device_found = false; |
| + if (!request_.requested_video_device_id.empty()) { |
|
mkosiba (inactive)
2014/04/17 10:34:06
maybe encapsulate this into a findByIdOrGetFirst f
michaelbai
2014/04/22 20:53:51
Done.
|
| + const MediaStreamDevice* video_device = FindDeviceById( |
| + video_devices, request_.requested_video_device_id); |
| + if (video_device != NULL) { |
| + devices.push_back(*video_device); |
| + video_device_found = true; |
| + } |
| + } |
| + if (!video_device_found) |
| + devices.push_back(video_devices[0]); |
| + } |
| + } |
| + callback_.Run(devices, devices.empty() ? |
| + content::MEDIA_DEVICE_NO_HARDWARE : content::MEDIA_DEVICE_OK, |
| + ui.Pass()); |
| +} |
| + |
| +const GURL& MediaAccessPermissionRequest::GetOrigin() { |
| + return request_.security_origin; |
| +} |
| + |
| +int64 MediaAccessPermissionRequest::GetResources() { |
| + return (request_.audio_type == content::MEDIA_DEVICE_AUDIO_CAPTURE ? |
| + AwPermissionRequest::AudioCapture : 0) | |
| + (request_.video_type == content::MEDIA_DEVICE_VIDEO_CAPTURE ? |
| + AwPermissionRequest::VideoCapture : 0); |
| +} |
| + |
| +} // namespace android_webview |