Convert env to a defaultdict in run_executable() to fix other callers of that function.
[chromium-blink-merge.git] / extensions / shell / browser / media_capture_util.cc
blob30e3fb63c78203e2c82269c19a0a1742e0a28564
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;
46 if (request.audio_type == content::MEDIA_DEVICE_AUDIO_CAPTURE) {
47 VerifyMediaAccessPermission(request.audio_type, extension);
48 const MediaStreamDevice* device = GetRequestedDeviceOrDefault(
49 MediaCaptureDevices::GetInstance()->GetAudioCaptureDevices(),
50 request.requested_audio_device_id);
51 if (device)
52 devices.push_back(*device);
55 if (request.video_type == content::MEDIA_DEVICE_VIDEO_CAPTURE) {
56 VerifyMediaAccessPermission(request.video_type, extension);
57 const MediaStreamDevice* device = GetRequestedDeviceOrDefault(
58 MediaCaptureDevices::GetInstance()->GetVideoCaptureDevices(),
59 request.requested_video_device_id);
60 if (device)
61 devices.push_back(*device);
64 // TODO(jamescook): Should we show a recording icon somewhere? If so, where?
65 scoped_ptr<MediaStreamUI> ui;
66 callback.Run(devices,
67 devices.empty() ? content::MEDIA_DEVICE_INVALID_STATE
68 : content::MEDIA_DEVICE_OK,
69 ui.Pass());
72 void VerifyMediaAccessPermission(content::MediaStreamType type,
73 const Extension* extension) {
74 const PermissionsData* permissions_data = extension->permissions_data();
75 if (type == content::MEDIA_DEVICE_AUDIO_CAPTURE) {
76 // app_shell has no UI surface to show an error, and on an embedded device
77 // it's better to crash than to have a feature not work.
78 CHECK(permissions_data->HasAPIPermission(APIPermission::kAudioCapture))
79 << "Audio capture request but no audioCapture permission in manifest.";
80 } else {
81 DCHECK(type == content::MEDIA_DEVICE_VIDEO_CAPTURE);
82 CHECK(permissions_data->HasAPIPermission(APIPermission::kVideoCapture))
83 << "Video capture request but no videoCapture permission in manifest.";
87 } // namespace media_capture_util
88 } // namespace extensions