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 "extensions/shell/browser/media_capture_util.h"
9 #include "base/callback.h"
10 #include "base/logging.h"
11 #include "content/public/browser/media_capture_devices.h"
12 #include "extensions/common/extension.h"
13 #include "extensions/common/permissions/permissions_data.h"
15 using content::MediaCaptureDevices
;
16 using content::MediaStreamDevice
;
17 using content::MediaStreamDevices
;
18 using content::MediaStreamUI
;
20 namespace extensions
{
22 const MediaStreamDevice
* GetRequestedDeviceOrDefault(
23 const MediaStreamDevices
& devices
,
24 const std::string
& requested_device_id
) {
25 if (!requested_device_id
.empty())
26 return devices
.FindById(requested_device_id
);
34 namespace media_capture_util
{
36 // See also Chrome's MediaCaptureDevicesDispatcher.
37 void GrantMediaStreamRequest(content::WebContents
* web_contents
,
38 const content::MediaStreamRequest
& request
,
39 const content::MediaResponseCallback
& callback
,
40 const Extension
* extension
) {
41 // app_shell only supports audio and video capture, not tab or screen capture.
42 DCHECK(request
.audio_type
== content::MEDIA_DEVICE_AUDIO_CAPTURE
||
43 request
.video_type
== content::MEDIA_DEVICE_VIDEO_CAPTURE
);
45 MediaStreamDevices devices
;
47 if (request
.audio_type
== content::MEDIA_DEVICE_AUDIO_CAPTURE
) {
48 VerifyMediaAccessPermission(request
.audio_type
, extension
);
49 const MediaStreamDevice
* device
= GetRequestedDeviceOrDefault(
50 MediaCaptureDevices::GetInstance()->GetAudioCaptureDevices(),
51 request
.requested_audio_device_id
);
53 devices
.push_back(*device
);
56 if (request
.video_type
== content::MEDIA_DEVICE_VIDEO_CAPTURE
) {
57 VerifyMediaAccessPermission(request
.video_type
, extension
);
58 const MediaStreamDevice
* device
= GetRequestedDeviceOrDefault(
59 MediaCaptureDevices::GetInstance()->GetVideoCaptureDevices(),
60 request
.requested_video_device_id
);
62 devices
.push_back(*device
);
65 // TODO(jamescook): Should we show a recording icon somewhere? If so, where?
66 scoped_ptr
<MediaStreamUI
> ui
;
68 devices
.empty() ? content::MEDIA_DEVICE_INVALID_STATE
69 : content::MEDIA_DEVICE_OK
,
73 void VerifyMediaAccessPermission(content::MediaStreamType type
,
74 const Extension
* extension
) {
75 const PermissionsData
* permissions_data
= extension
->permissions_data();
76 if (type
== content::MEDIA_DEVICE_AUDIO_CAPTURE
) {
77 // app_shell has no UI surface to show an error, and on an embedded device
78 // it's better to crash than to have a feature not work.
79 CHECK(permissions_data
->HasAPIPermission(APIPermission::kAudioCapture
))
80 << "Audio capture request but no audioCapture permission in manifest.";
82 DCHECK(type
== content::MEDIA_DEVICE_VIDEO_CAPTURE
);
83 CHECK(permissions_data
->HasAPIPermission(APIPermission::kVideoCapture
))
84 << "Video capture request but no videoCapture permission in manifest.";
88 } // namespace media_capture_util
89 } // namespace extensions