OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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. |
| 4 |
| 5 #include "chrome/browser/media/regular_media_access_handler.h" |
| 6 |
| 7 #include "base/metrics/field_trial.h" |
| 8 #include "chrome/browser/media/media_stream_devices_controller.h" |
| 9 #include "chrome/browser/media/media_stream_infobar_delegate.h" |
| 10 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" |
| 11 #include "content/public/browser/browser_thread.h" |
| 12 |
| 13 using content::BrowserThread; |
| 14 |
| 15 namespace { |
| 16 // A finch experiment to enable the permission bubble for media requests only. |
| 17 bool MediaStreamPermissionBubbleExperimentEnabled() { |
| 18 const std::string group = |
| 19 base::FieldTrialList::FindFullName("MediaStreamPermissionBubble"); |
| 20 if (group == "enabled") |
| 21 return true; |
| 22 |
| 23 return false; |
| 24 } |
| 25 } // namespace |
| 26 |
| 27 RegularMediaAccessHandler::RegularMediaAccessHandler(RequestsQueues& requests) |
| 28 : pending_requests_(requests) { |
| 29 } |
| 30 |
| 31 RegularMediaAccessHandler::~RegularMediaAccessHandler() { |
| 32 } |
| 33 |
| 34 void RegularMediaAccessHandler::HandleRequest( |
| 35 content::WebContents* web_contents, |
| 36 const content::MediaStreamRequest& request, |
| 37 const content::MediaResponseCallback& callback, |
| 38 const extensions::Extension* extension) { |
| 39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 40 |
| 41 RequestsQueue& queue = pending_requests_[web_contents]; |
| 42 queue.push_back(PendingAccessRequest(request, callback)); |
| 43 |
| 44 // If this is the only request then show the infobar. |
| 45 if (queue.size() == 1) |
| 46 ProcessQueuedAccessRequest(web_contents); |
| 47 } |
| 48 |
| 49 void RegularMediaAccessHandler::ProcessQueuedAccessRequest( |
| 50 content::WebContents* web_contents) { |
| 51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 52 |
| 53 std::map<content::WebContents*, RequestsQueue>::iterator it = |
| 54 pending_requests_.find(web_contents); |
| 55 |
| 56 if (it == pending_requests_.end() || it->second.empty()) { |
| 57 // Don't do anything if the tab was closed. |
| 58 return; |
| 59 } |
| 60 |
| 61 DCHECK(!it->second.empty()); |
| 62 |
| 63 if (PermissionBubbleManager::Enabled() || |
| 64 MediaStreamPermissionBubbleExperimentEnabled()) { |
| 65 scoped_ptr<MediaStreamDevicesController> controller( |
| 66 new MediaStreamDevicesController( |
| 67 web_contents, it->second.front().request, |
| 68 base::Bind(&RegularMediaAccessHandler::OnAccessRequestResponse, |
| 69 base::Unretained(this), web_contents))); |
| 70 if (controller->DismissInfoBarAndTakeActionOnSettings()) |
| 71 return; |
| 72 PermissionBubbleManager* bubble_manager = |
| 73 PermissionBubbleManager::FromWebContents(web_contents); |
| 74 if (bubble_manager) |
| 75 bubble_manager->AddRequest(controller.release()); |
| 76 return; |
| 77 } |
| 78 |
| 79 // TODO(gbillock): delete this block and the MediaStreamInfoBarDelegate |
| 80 // when we've transitioned to bubbles. (crbug/337458) |
| 81 MediaStreamInfoBarDelegate::Create( |
| 82 web_contents, it->second.front().request, |
| 83 base::Bind(&RegularMediaAccessHandler::OnAccessRequestResponse, |
| 84 base::Unretained(this), web_contents)); |
| 85 } |
| 86 |
| 87 void RegularMediaAccessHandler::OnAccessRequestResponse( |
| 88 content::WebContents* web_contents, |
| 89 const content::MediaStreamDevices& devices, |
| 90 content::MediaStreamRequestResult result, |
| 91 scoped_ptr<content::MediaStreamUI> ui) { |
| 92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 93 |
| 94 std::map<content::WebContents*, RequestsQueue>::iterator it = |
| 95 pending_requests_.find(web_contents); |
| 96 if (it == pending_requests_.end()) { |
| 97 // WebContents has been destroyed. Don't need to do anything. |
| 98 return; |
| 99 } |
| 100 |
| 101 RequestsQueue& queue(it->second); |
| 102 if (queue.empty()) |
| 103 return; |
| 104 |
| 105 content::MediaResponseCallback callback = queue.front().callback; |
| 106 queue.pop_front(); |
| 107 |
| 108 if (!queue.empty()) { |
| 109 // Post a task to process next queued request. It has to be done |
| 110 // asynchronously to make sure that calling infobar is not destroyed until |
| 111 // after this function returns. |
| 112 BrowserThread::PostTask( |
| 113 BrowserThread::UI, FROM_HERE, |
| 114 base::Bind(&RegularMediaAccessHandler::ProcessQueuedAccessRequest, |
| 115 base::Unretained(this), web_contents)); |
| 116 } |
| 117 |
| 118 callback.Run(devices, result, ui.Pass()); |
| 119 } |
OLD | NEW |