1 // Copyright 2015 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 "chrome/browser/media/permission_bubble_media_access_handler.h"
7 #include "base/metrics/field_trial.h"
8 #include "chrome/browser/media/media_stream_device_permissions.h"
9 #include "chrome/browser/media/media_stream_infobar_delegate.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
12 #include "chrome/common/pref_names.h"
13 #include "components/content_settings/core/browser/host_content_settings_map.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/notification_service.h"
16 #include "content/public/browser/notification_types.h"
17 #include "content/public/browser/web_contents.h"
19 using content::BrowserThread
;
21 struct PermissionBubbleMediaAccessHandler::PendingAccessRequest
{
22 PendingAccessRequest(const content::MediaStreamRequest
& request
,
23 const content::MediaResponseCallback
& callback
)
24 : request(request
), callback(callback
) {}
25 ~PendingAccessRequest() {}
27 // TODO(gbillock): make the MediaStreamDevicesController owned by
28 // this object when we're using bubbles.
29 content::MediaStreamRequest request
;
30 content::MediaResponseCallback callback
;
33 PermissionBubbleMediaAccessHandler::PermissionBubbleMediaAccessHandler() {
34 // PermissionBubbleMediaAccessHandler should be created on UI thread.
35 // Otherwise, it will not receive
36 // content::NOTIFICATION_WEB_CONTENTS_DESTROYED, and that will result in
37 // possible use after free.
38 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
39 notifications_registrar_
.Add(this,
40 content::NOTIFICATION_WEB_CONTENTS_DESTROYED
,
41 content::NotificationService::AllSources());
44 PermissionBubbleMediaAccessHandler::~PermissionBubbleMediaAccessHandler() {
47 bool PermissionBubbleMediaAccessHandler::SupportsStreamType(
48 const content::MediaStreamType type
,
49 const extensions::Extension
* extension
) {
50 return type
== content::MEDIA_DEVICE_VIDEO_CAPTURE
||
51 type
== content::MEDIA_DEVICE_AUDIO_CAPTURE
;
54 bool PermissionBubbleMediaAccessHandler::CheckMediaAccessPermission(
55 content::WebContents
* web_contents
,
56 const GURL
& security_origin
,
57 content::MediaStreamType type
,
58 const extensions::Extension
* extension
) {
60 Profile::FromBrowserContext(web_contents
->GetBrowserContext());
62 ContentSettingsType content_settings_type
=
63 type
== content::MEDIA_DEVICE_AUDIO_CAPTURE
64 ? CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
65 : CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
;
67 if (CheckAllowAllMediaStreamContentForOrigin(profile
, security_origin
,
68 content_settings_type
)) {
72 const char* policy_name
= type
== content::MEDIA_DEVICE_AUDIO_CAPTURE
73 ? prefs::kAudioCaptureAllowed
74 : prefs::kVideoCaptureAllowed
;
75 const char* list_policy_name
= type
== content::MEDIA_DEVICE_AUDIO_CAPTURE
76 ? prefs::kAudioCaptureAllowedUrls
77 : prefs::kVideoCaptureAllowedUrls
;
78 if (GetDevicePolicy(profile
, security_origin
, policy_name
,
79 list_policy_name
) == ALWAYS_ALLOW
) {
83 // There's no secondary URL for these content types, hence duplicating
85 if (profile
->GetHostContentSettingsMap()->GetContentSetting(
86 security_origin
, security_origin
, content_settings_type
,
87 content_settings::ResourceIdentifier()) == CONTENT_SETTING_ALLOW
) {
94 void PermissionBubbleMediaAccessHandler::HandleRequest(
95 content::WebContents
* web_contents
,
96 const content::MediaStreamRequest
& request
,
97 const content::MediaResponseCallback
& callback
,
98 const extensions::Extension
* extension
) {
99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
101 RequestsQueue
& queue
= pending_requests_
[web_contents
];
102 queue
.push_back(PendingAccessRequest(request
, callback
));
104 // If this is the only request then show the infobar.
105 if (queue
.size() == 1)
106 ProcessQueuedAccessRequest(web_contents
);
109 void PermissionBubbleMediaAccessHandler::ProcessQueuedAccessRequest(
110 content::WebContents
* web_contents
) {
111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
113 std::map
<content::WebContents
*, RequestsQueue
>::iterator it
=
114 pending_requests_
.find(web_contents
);
116 if (it
== pending_requests_
.end() || it
->second
.empty()) {
117 // Don't do anything if the tab was closed.
121 DCHECK(!it
->second
.empty());
123 if (PermissionBubbleManager::Enabled()) {
124 scoped_ptr
<MediaStreamDevicesController
> controller(
125 new MediaStreamDevicesController(
126 web_contents
, it
->second
.front().request
,
128 &PermissionBubbleMediaAccessHandler::OnAccessRequestResponse
,
129 base::Unretained(this), web_contents
)));
130 if (!controller
->IsAskingForAudio() && !controller
->IsAskingForVideo())
132 PermissionBubbleManager
* bubble_manager
=
133 PermissionBubbleManager::FromWebContents(web_contents
);
135 bubble_manager
->AddRequest(controller
.release());
139 // TODO(gbillock): delete this block and the MediaStreamInfoBarDelegate
140 // when we've transitioned to bubbles. (crbug/337458)
141 MediaStreamInfoBarDelegate::Create(
142 web_contents
, it
->second
.front().request
,
143 base::Bind(&PermissionBubbleMediaAccessHandler::OnAccessRequestResponse
,
144 base::Unretained(this), web_contents
));
147 void PermissionBubbleMediaAccessHandler::UpdateMediaRequestState(
148 int render_process_id
,
151 content::MediaStreamType stream_type
,
152 content::MediaRequestState state
) {
153 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
154 if (state
!= content::MEDIA_REQUEST_STATE_CLOSING
)
158 for (RequestsQueues::iterator rqs_it
= pending_requests_
.begin();
159 rqs_it
!= pending_requests_
.end(); ++rqs_it
) {
160 RequestsQueue
& queue
= rqs_it
->second
;
161 for (RequestsQueue::iterator it
= queue
.begin(); it
!= queue
.end(); ++it
) {
162 if (it
->request
.render_process_id
== render_process_id
&&
163 it
->request
.render_frame_id
== render_frame_id
&&
164 it
->request
.page_request_id
== page_request_id
) {
175 void PermissionBubbleMediaAccessHandler::OnAccessRequestResponse(
176 content::WebContents
* web_contents
,
177 const content::MediaStreamDevices
& devices
,
178 content::MediaStreamRequestResult result
,
179 scoped_ptr
<content::MediaStreamUI
> ui
) {
180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
182 std::map
<content::WebContents
*, RequestsQueue
>::iterator it
=
183 pending_requests_
.find(web_contents
);
184 if (it
== pending_requests_
.end()) {
185 // WebContents has been destroyed. Don't need to do anything.
189 RequestsQueue
& queue(it
->second
);
193 content::MediaResponseCallback callback
= queue
.front().callback
;
196 if (!queue
.empty()) {
197 // Post a task to process next queued request. It has to be done
198 // asynchronously to make sure that calling infobar is not destroyed until
199 // after this function returns.
200 BrowserThread::PostTask(
201 BrowserThread::UI
, FROM_HERE
,
203 &PermissionBubbleMediaAccessHandler::ProcessQueuedAccessRequest
,
204 base::Unretained(this), web_contents
));
207 callback
.Run(devices
, result
, ui
.Pass());
210 void PermissionBubbleMediaAccessHandler::Observe(
212 const content::NotificationSource
& source
,
213 const content::NotificationDetails
& details
) {
214 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
215 if (type
== content::NOTIFICATION_WEB_CONTENTS_DESTROYED
) {
216 content::WebContents
* web_contents
=
217 content::Source
<content::WebContents
>(source
).ptr();
218 pending_requests_
.erase(web_contents
);