Index: chrome/browser/media/regular_media_access_handler.cc |
diff --git a/chrome/browser/media/regular_media_access_handler.cc b/chrome/browser/media/regular_media_access_handler.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ea0525e895c55c8faf931b3350b564c312c4a5ad |
--- /dev/null |
+++ b/chrome/browser/media/regular_media_access_handler.cc |
@@ -0,0 +1,119 @@ |
+// Copyright (c) 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/media/regular_media_access_handler.h" |
+ |
+#include "base/metrics/field_trial.h" |
+#include "chrome/browser/media/media_stream_devices_controller.h" |
+#include "chrome/browser/media/media_stream_infobar_delegate.h" |
+#include "chrome/browser/ui/website_settings/permission_bubble_manager.h" |
+#include "content/public/browser/browser_thread.h" |
+ |
+using content::BrowserThread; |
+ |
+namespace { |
+// A finch experiment to enable the permission bubble for media requests only. |
+bool MediaStreamPermissionBubbleExperimentEnabled() { |
+ const std::string group = |
+ base::FieldTrialList::FindFullName("MediaStreamPermissionBubble"); |
+ if (group == "enabled") |
+ return true; |
+ |
+ return false; |
+} |
+} // namespace |
+ |
+RegularMediaAccessHandler::RegularMediaAccessHandler(RequestsQueues& requests) |
+ : pending_requests_(requests) { |
+} |
+ |
+RegularMediaAccessHandler::~RegularMediaAccessHandler() { |
+} |
+ |
+void RegularMediaAccessHandler::HandleRequest( |
+ content::WebContents* web_contents, |
+ const content::MediaStreamRequest& request, |
+ const content::MediaResponseCallback& callback, |
+ const extensions::Extension* extension) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ |
+ RequestsQueue& queue = pending_requests_[web_contents]; |
+ queue.push_back(PendingAccessRequest(request, callback)); |
+ |
+ // If this is the only request then show the infobar. |
+ if (queue.size() == 1) |
+ ProcessQueuedAccessRequest(web_contents); |
+} |
+ |
+void RegularMediaAccessHandler::ProcessQueuedAccessRequest( |
+ content::WebContents* web_contents) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ |
+ std::map<content::WebContents*, RequestsQueue>::iterator it = |
+ pending_requests_.find(web_contents); |
+ |
+ if (it == pending_requests_.end() || it->second.empty()) { |
+ // Don't do anything if the tab was closed. |
+ return; |
+ } |
+ |
+ DCHECK(!it->second.empty()); |
+ |
+ if (PermissionBubbleManager::Enabled() || |
+ MediaStreamPermissionBubbleExperimentEnabled()) { |
+ scoped_ptr<MediaStreamDevicesController> controller( |
+ new MediaStreamDevicesController( |
+ web_contents, it->second.front().request, |
+ base::Bind(&RegularMediaAccessHandler::OnAccessRequestResponse, |
+ base::Unretained(this), web_contents))); |
+ if (controller->DismissInfoBarAndTakeActionOnSettings()) |
+ return; |
+ PermissionBubbleManager* bubble_manager = |
+ PermissionBubbleManager::FromWebContents(web_contents); |
+ if (bubble_manager) |
+ bubble_manager->AddRequest(controller.release()); |
+ return; |
+ } |
+ |
+ // TODO(gbillock): delete this block and the MediaStreamInfoBarDelegate |
+ // when we've transitioned to bubbles. (crbug/337458) |
+ MediaStreamInfoBarDelegate::Create( |
+ web_contents, it->second.front().request, |
+ base::Bind(&RegularMediaAccessHandler::OnAccessRequestResponse, |
+ base::Unretained(this), web_contents)); |
+} |
+ |
+void RegularMediaAccessHandler::OnAccessRequestResponse( |
+ content::WebContents* web_contents, |
+ const content::MediaStreamDevices& devices, |
+ content::MediaStreamRequestResult result, |
+ scoped_ptr<content::MediaStreamUI> ui) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ |
+ std::map<content::WebContents*, RequestsQueue>::iterator it = |
+ pending_requests_.find(web_contents); |
+ if (it == pending_requests_.end()) { |
+ // WebContents has been destroyed. Don't need to do anything. |
+ return; |
+ } |
+ |
+ RequestsQueue& queue(it->second); |
+ if (queue.empty()) |
+ return; |
+ |
+ content::MediaResponseCallback callback = queue.front().callback; |
+ queue.pop_front(); |
+ |
+ if (!queue.empty()) { |
+ // Post a task to process next queued request. It has to be done |
+ // asynchronously to make sure that calling infobar is not destroyed until |
+ // after this function returns. |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ base::Bind(&RegularMediaAccessHandler::ProcessQueuedAccessRequest, |
+ base::Unretained(this), web_contents)); |
+ } |
+ |
+ callback.Run(devices, result, ui.Pass()); |
+} |