Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / extensions / shell / browser / media_capture_util.cc
blob364e19df8a7c6ecc74dc971cfdb60fc61ceabe48
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"
7 #include <string>
9 #include "base/callback.h"
10 #include "base/logging.h"
11 #include "content/public/browser/media_capture_devices.h"
12 #include "extensions/common/permissions/permissions_data.h"
14 using content::MediaCaptureDevices;
15 using content::MediaStreamDevice;
16 using content::MediaStreamDevices;
17 using content::MediaStreamUI;
19 namespace extensions {
21 const MediaStreamDevice* GetRequestedDeviceOrDefault(
22 const MediaStreamDevices& devices,
23 const std::string& requested_device_id) {
24 if (!requested_device_id.empty())
25 return devices.FindById(requested_device_id);
27 if (!devices.empty())
28 return &devices[0];
30 return NULL;
33 namespace media_capture_util {
35 // See also Chrome's MediaCaptureDevicesDispatcher.
36 void GrantMediaStreamRequest(content::WebContents* web_contents,
37 const content::MediaStreamRequest& request,
38 const content::MediaResponseCallback& callback,
39 const Extension* extension) {
40 // app_shell only supports audio and video capture, not tab or screen capture.
41 DCHECK(request.audio_type == content::MEDIA_DEVICE_AUDIO_CAPTURE ||
42 request.video_type == content::MEDIA_DEVICE_VIDEO_CAPTURE);
44 MediaStreamDevices devices;
45 const PermissionsData* permissions_data = extension->permissions_data();
47 if (request.audio_type == content::MEDIA_DEVICE_AUDIO_CAPTURE) {
48 // app_shell has no UI surface to show an error, and on an embedded device
49 // it's better to crash than to have a feature not work.
50 CHECK(permissions_data->HasAPIPermission(APIPermission::kAudioCapture))
51 << "Audio capture request but no audioCapture permission in manifest.";
53 const MediaStreamDevice* device = GetRequestedDeviceOrDefault(
54 MediaCaptureDevices::GetInstance()->GetAudioCaptureDevices(),
55 request.requested_audio_device_id);
56 if (device)
57 devices.push_back(*device);
60 if (request.video_type == content::MEDIA_DEVICE_VIDEO_CAPTURE) {
61 // See APIPermission::kAudioCapture check above.
62 CHECK(permissions_data->HasAPIPermission(APIPermission::kVideoCapture))
63 << "Video capture request but no videoCapture permission in manifest.";
65 const MediaStreamDevice* device = GetRequestedDeviceOrDefault(
66 MediaCaptureDevices::GetInstance()->GetVideoCaptureDevices(),
67 request.requested_video_device_id);
68 if (device)
69 devices.push_back(*device);
72 // TODO(jamescook): Should we show a recording icon somewhere? If so, where?
73 scoped_ptr<MediaStreamUI> ui;
74 callback.Run(devices,
75 devices.empty() ? content::MEDIA_DEVICE_INVALID_STATE
76 : content::MEDIA_DEVICE_OK,
77 ui.Pass());
80 } // namespace media_capture_util
81 } // namespace extensions