Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / chrome / browser / media / permission_bubble_media_access_handler.cc
blobb32c59baac8afbea8c65a2b0e7bf68452991bb67
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_permission.h"
9 #include "chrome/browser/media/media_stream_device_permissions.h"
10 #include "chrome/browser/media/media_stream_infobar_delegate.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
13 #include "chrome/common/pref_names.h"
14 #include "components/content_settings/core/browser/host_content_settings_map.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/notification_service.h"
17 #include "content/public/browser/notification_types.h"
18 #include "content/public/browser/web_contents.h"
20 using content::BrowserThread;
22 struct PermissionBubbleMediaAccessHandler::PendingAccessRequest {
23 PendingAccessRequest(const content::MediaStreamRequest& request,
24 const content::MediaResponseCallback& callback)
25 : request(request), callback(callback) {}
26 ~PendingAccessRequest() {}
28 // TODO(gbillock): make the MediaStreamDevicesController owned by
29 // this object when we're using bubbles.
30 content::MediaStreamRequest request;
31 content::MediaResponseCallback callback;
34 PermissionBubbleMediaAccessHandler::PermissionBubbleMediaAccessHandler() {
35 // PermissionBubbleMediaAccessHandler should be created on UI thread.
36 // Otherwise, it will not receive
37 // content::NOTIFICATION_WEB_CONTENTS_DESTROYED, and that will result in
38 // possible use after free.
39 DCHECK_CURRENTLY_ON(BrowserThread::UI);
40 notifications_registrar_.Add(this,
41 content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
42 content::NotificationService::AllSources());
45 PermissionBubbleMediaAccessHandler::~PermissionBubbleMediaAccessHandler() {
48 bool PermissionBubbleMediaAccessHandler::SupportsStreamType(
49 const content::MediaStreamType type,
50 const extensions::Extension* extension) {
51 return type == content::MEDIA_DEVICE_VIDEO_CAPTURE ||
52 type == content::MEDIA_DEVICE_AUDIO_CAPTURE;
55 bool PermissionBubbleMediaAccessHandler::CheckMediaAccessPermission(
56 content::WebContents* web_contents,
57 const GURL& security_origin,
58 content::MediaStreamType type,
59 const extensions::Extension* extension) {
60 Profile* profile =
61 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;
66 MediaPermission permission(
67 content_settings_type, content::MEDIA_DEVICE_ACCESS, security_origin,
68 web_contents->GetLastCommittedURL().GetOrigin(), profile);
69 content::MediaStreamRequestResult unused;
70 if (permission.GetPermissionStatus(&unused) == CONTENT_SETTING_ALLOW)
71 return true;
73 return false;
76 void PermissionBubbleMediaAccessHandler::HandleRequest(
77 content::WebContents* web_contents,
78 const content::MediaStreamRequest& request,
79 const content::MediaResponseCallback& callback,
80 const extensions::Extension* extension) {
81 DCHECK_CURRENTLY_ON(BrowserThread::UI);
83 RequestsQueue& queue = pending_requests_[web_contents];
84 queue.push_back(PendingAccessRequest(request, callback));
86 // If this is the only request then show the infobar.
87 if (queue.size() == 1)
88 ProcessQueuedAccessRequest(web_contents);
91 void PermissionBubbleMediaAccessHandler::ProcessQueuedAccessRequest(
92 content::WebContents* web_contents) {
93 DCHECK_CURRENTLY_ON(BrowserThread::UI);
95 std::map<content::WebContents*, RequestsQueue>::iterator it =
96 pending_requests_.find(web_contents);
98 if (it == pending_requests_.end() || it->second.empty()) {
99 // Don't do anything if the tab was closed.
100 return;
103 DCHECK(!it->second.empty());
105 if (PermissionBubbleManager::Enabled()) {
106 scoped_ptr<MediaStreamDevicesController> controller(
107 new MediaStreamDevicesController(
108 web_contents, it->second.front().request,
109 base::Bind(
110 &PermissionBubbleMediaAccessHandler::OnAccessRequestResponse,
111 base::Unretained(this), web_contents)));
112 if (!controller->IsAskingForAudio() && !controller->IsAskingForVideo())
113 return;
114 PermissionBubbleManager* bubble_manager =
115 PermissionBubbleManager::FromWebContents(web_contents);
116 if (bubble_manager)
117 bubble_manager->AddRequest(controller.release());
118 return;
121 // TODO(gbillock): delete this block and the MediaStreamInfoBarDelegate
122 // when we've transitioned to bubbles. (crbug/337458)
123 MediaStreamInfoBarDelegate::Create(
124 web_contents, it->second.front().request,
125 base::Bind(&PermissionBubbleMediaAccessHandler::OnAccessRequestResponse,
126 base::Unretained(this), web_contents));
129 void PermissionBubbleMediaAccessHandler::UpdateMediaRequestState(
130 int render_process_id,
131 int render_frame_id,
132 int page_request_id,
133 content::MediaStreamType stream_type,
134 content::MediaRequestState state) {
135 DCHECK_CURRENTLY_ON(BrowserThread::UI);
136 if (state != content::MEDIA_REQUEST_STATE_CLOSING)
137 return;
139 bool found = false;
140 for (RequestsQueues::iterator rqs_it = pending_requests_.begin();
141 rqs_it != pending_requests_.end(); ++rqs_it) {
142 RequestsQueue& queue = rqs_it->second;
143 for (RequestsQueue::iterator it = queue.begin(); it != queue.end(); ++it) {
144 if (it->request.render_process_id == render_process_id &&
145 it->request.render_frame_id == render_frame_id &&
146 it->request.page_request_id == page_request_id) {
147 queue.erase(it);
148 found = true;
149 break;
152 if (found)
153 break;
157 void PermissionBubbleMediaAccessHandler::OnAccessRequestResponse(
158 content::WebContents* web_contents,
159 const content::MediaStreamDevices& devices,
160 content::MediaStreamRequestResult result,
161 scoped_ptr<content::MediaStreamUI> ui) {
162 DCHECK_CURRENTLY_ON(BrowserThread::UI);
164 std::map<content::WebContents*, RequestsQueue>::iterator it =
165 pending_requests_.find(web_contents);
166 if (it == pending_requests_.end()) {
167 // WebContents has been destroyed. Don't need to do anything.
168 return;
171 RequestsQueue& queue(it->second);
172 if (queue.empty())
173 return;
175 content::MediaResponseCallback callback = queue.front().callback;
176 queue.pop_front();
178 if (!queue.empty()) {
179 // Post a task to process next queued request. It has to be done
180 // asynchronously to make sure that calling infobar is not destroyed until
181 // after this function returns.
182 BrowserThread::PostTask(
183 BrowserThread::UI, FROM_HERE,
184 base::Bind(
185 &PermissionBubbleMediaAccessHandler::ProcessQueuedAccessRequest,
186 base::Unretained(this), web_contents));
189 callback.Run(devices, result, ui.Pass());
192 void PermissionBubbleMediaAccessHandler::Observe(
193 int type,
194 const content::NotificationSource& source,
195 const content::NotificationDetails& details) {
196 DCHECK_CURRENTLY_ON(BrowserThread::UI);
197 if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) {
198 content::WebContents* web_contents =
199 content::Source<content::WebContents>(source).ptr();
200 pending_requests_.erase(web_contents);