1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "android_webview/native/permission/media_access_permission_request.h"
7 #include "android_webview/native/permission/aw_permission_request.h"
8 #include "content/public/browser/media_capture_devices.h"
10 using content::MediaCaptureDevices
;
11 using content::MediaStreamDevice
;
12 using content::MediaStreamDevices
;
14 namespace android_webview
{
18 // Return the device specified by |device_id| if exists, otherwise the first
19 // available device is returned.
20 const MediaStreamDevice
* GetDeviceByIdOrFirstAvailable(
21 const MediaStreamDevices
& devices
,
22 const std::string
& device_id
) {
26 if (!device_id
.empty()) {
27 for (size_t i
= 0; i
< devices
.size(); ++i
) {
28 if (devices
[i
].id
== device_id
)
38 MediaAccessPermissionRequest::MediaAccessPermissionRequest(
39 const content::MediaStreamRequest
& request
,
40 const content::MediaResponseCallback
& callback
)
45 MediaAccessPermissionRequest::~MediaAccessPermissionRequest() {
48 void MediaAccessPermissionRequest::NotifyRequestResult(bool allowed
) {
49 scoped_ptr
<content::MediaStreamUI
> ui
;
50 MediaStreamDevices devices
;
52 callback_
.Run(devices
, content::MEDIA_DEVICE_PERMISSION_DENIED
, ui
.Pass());
56 if (request_
.audio_type
== content::MEDIA_DEVICE_AUDIO_CAPTURE
) {
57 const MediaStreamDevices
& audio_devices
= audio_test_devices_
.empty()?
58 MediaCaptureDevices::GetInstance()->GetAudioCaptureDevices() :
60 const MediaStreamDevice
* device
= GetDeviceByIdOrFirstAvailable(
61 audio_devices
, request_
.requested_audio_device_id
);
63 devices
.push_back(*device
);
66 if (request_
.video_type
== content::MEDIA_DEVICE_VIDEO_CAPTURE
) {
67 const MediaStreamDevices
& video_devices
= video_test_devices_
.empty()?
68 MediaCaptureDevices::GetInstance()->GetVideoCaptureDevices() :
70 const MediaStreamDevice
* device
= GetDeviceByIdOrFirstAvailable(
71 video_devices
, request_
.requested_video_device_id
);
73 devices
.push_back(*device
);
75 callback_
.Run(devices
, devices
.empty() ?
76 content::MEDIA_DEVICE_NO_HARDWARE
: content::MEDIA_DEVICE_OK
,
80 const GURL
& MediaAccessPermissionRequest::GetOrigin() {
81 return request_
.security_origin
;
84 int64
MediaAccessPermissionRequest::GetResources() {
85 return (request_
.audio_type
== content::MEDIA_DEVICE_AUDIO_CAPTURE
?
86 AwPermissionRequest::AudioCapture
: 0) |
87 (request_
.video_type
== content::MEDIA_DEVICE_VIDEO_CAPTURE
?
88 AwPermissionRequest::VideoCapture
: 0);
91 } // namespace android_webview