Remove ExtensionPrefs::SetDidExtensionEscalatePermissions.
[chromium-blink-merge.git] / extensions / shell / browser / media_capture_util.cc
blobd8a11a8082c661f657cecb605a414f0735289bdc
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/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);
28 if (!devices.empty())
29 return &devices[0];
31 return NULL;
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);
52 if (device)
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);
61 if (device)
62 devices.push_back(*device);
65 // TODO(jamescook): Should we show a recording icon somewhere? If so, where?
66 scoped_ptr<MediaStreamUI> ui;
67 callback.Run(devices,
68 devices.empty() ? content::MEDIA_DEVICE_INVALID_STATE
69 : content::MEDIA_DEVICE_OK,
70 ui.Pass());
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.";
81 } else {
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