ProfilePolicyConnectorFactory: Refactoring from Profile to BrowserContext.
[chromium-blink-merge.git] / android_webview / native / permission / media_access_permission_request.cc
blob6ef8c070f1df1a885b0397fd17a3c07a07ec4d7a
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 {
16 namespace {
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) {
23 if (devices.empty())
24 return NULL;
26 if (!device_id.empty()) {
27 for (size_t i = 0; i < devices.size(); ++i) {
28 if (devices[i].id == device_id)
29 return &devices[i];
33 return &devices[0];
36 } // namespace
38 MediaAccessPermissionRequest::MediaAccessPermissionRequest(
39 const content::MediaStreamRequest& request,
40 const content::MediaResponseCallback& callback)
41 : request_(request),
42 callback_(callback) {
45 MediaAccessPermissionRequest::~MediaAccessPermissionRequest() {
48 void MediaAccessPermissionRequest::NotifyRequestResult(bool allowed) {
49 scoped_ptr<content::MediaStreamUI> ui;
50 MediaStreamDevices devices;
51 if (!allowed) {
52 callback_.Run(devices, content::MEDIA_DEVICE_PERMISSION_DENIED, ui.Pass());
53 return;
56 if (request_.audio_type == content::MEDIA_DEVICE_AUDIO_CAPTURE) {
57 const MediaStreamDevices& audio_devices = audio_test_devices_.empty()?
58 MediaCaptureDevices::GetInstance()->GetAudioCaptureDevices() :
59 audio_test_devices_;
60 const MediaStreamDevice* device = GetDeviceByIdOrFirstAvailable(
61 audio_devices, request_.requested_audio_device_id);
62 if (device)
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() :
69 video_test_devices_;
70 const MediaStreamDevice* device = GetDeviceByIdOrFirstAvailable(
71 video_devices, request_.requested_video_device_id);
72 if (device)
73 devices.push_back(*device);
75 callback_.Run(devices, devices.empty() ?
76 content::MEDIA_DEVICE_NO_HARDWARE : content::MEDIA_DEVICE_OK,
77 ui.Pass());
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