Index: chrome/browser/permissions/permission_infobar_manager.cc |
diff --git a/chrome/browser/permissions/permission_infobar_manager.cc b/chrome/browser/permissions/permission_infobar_manager.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..abf8baaaba0987f8b560bf38a5a4c9871d5f83fe |
--- /dev/null |
+++ b/chrome/browser/permissions/permission_infobar_manager.cc |
@@ -0,0 +1,139 @@ |
+// Copyright 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/permissions/permission_infobar_manager.h" |
+ |
+#include "chrome/browser/chrome_notification_types.h" |
+#include "chrome/browser/infobars/infobar_service.h" |
+#include "chrome/browser/permissions/permission_infobar_request.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/tab_contents/tab_util.h" |
+#include "components/content_settings/core/common/content_settings.h" |
+#include "components/infobars/core/infobar.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/browser/web_contents.h" |
+#include "content/public/common/url_constants.h" |
+ |
+namespace { |
+// TODO(lalitm) The odd choice for the constant here is because of old code |
+// passing -1 as a valid request_id. Until that is tracked down and fixed |
+// this should not be set to -1. |
mlamouri (slow - plz ping)
2015/09/28 13:30:14
Where is that code?
Lalit Maganti
2015/09/28 15:32:38
Mainly test code.
|
+const int kNoCurrentRequestId = -2; |
+} |
+ |
+DEFINE_WEB_CONTENTS_USER_DATA_KEY(PermissionInfoBarManager); |
+ |
+PermissionInfoBarManager::PermissionInfoBarManager( |
+ content::WebContents* web_contents) |
+ : content::WebContentsObserver(web_contents), |
+ current_request_id_(kNoCurrentRequestId), |
+ is_show_queued_pending_(false), |
+ weak_factory_(this) { |
+} |
+ |
+PermissionInfoBarManager::~PermissionInfoBarManager() { |
+} |
+ |
+void PermissionInfoBarManager::CreateInfoBarRequest( |
+ const ContentSettingsType type, |
+ const PermissionRequestID& request_id, |
+ const GURL& requesting_frame, |
+ const GURL& embedder, |
+ const PermissionDecidedCallback& user_callback, |
+ const PermissionDecidedCallback& non_user_callback) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ DCHECK(request_id.request_id() != kNoCurrentRequestId); |
+ |
+ if (requesting_frame.SchemeIs(content::kChromeUIScheme) || |
+ embedder.SchemeIs(content::kChromeUIScheme)) { |
+ return; |
+ } |
+ |
+ auto it = queued_requests_.find(request_id.request_id()); |
+ PermissionInfoBarRequest* request; |
+ if (it == queued_requests_.end()) { |
+ request = new PermissionInfoBarRequest(requesting_frame, embedder, |
+ base::Bind(&PermissionInfoBarManager::OnInfoBarClosed, |
+ weak_factory_.GetWeakPtr())); |
+ queued_requests_.add(request_id.request_id(), make_scoped_ptr(request)); |
+ |
+ // Try to trigger a show for this infobar. |
+ TriggerShowNextQueuedRequest(); |
+ } else { |
+ request = it->second; |
+ } |
+ request->AddPermission(type, user_callback, non_user_callback); |
+} |
+ |
+void PermissionInfoBarManager::TriggerShowNextQueuedRequest() { |
+ if (ShouldIgnoreQueuedRequests() || is_show_queued_pending_) |
+ return; |
+ |
+ is_show_queued_pending_ = true; |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::UI, |
mlamouri (slow - plz ping)
2015/09/28 13:30:15
Given that we handle threads, it would be nice if
Lalit Maganti
2015/09/28 15:32:38
Everything should be on the UI thread in this clas
|
+ FROM_HERE, |
+ base::Bind(&PermissionInfoBarManager::ShowNextQueuedRequest, |
mlamouri (slow - plz ping)
2015/09/28 13:30:14
Why do we do that? Is it just to be compatible wit
Lalit Maganti
2015/09/28 15:32:38
That and if I didn't post, coalescing would not wo
|
+ weak_factory_.GetWeakPtr())); |
+} |
+ |
+void PermissionInfoBarManager::ShowNextQueuedRequest() { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ |
+ is_show_queued_pending_ = false; |
+ if (ShouldIgnoreQueuedRequests()) |
+ return; |
+ |
+ InfoBarService* infobar_service = |
+ InfoBarService::FromWebContents(web_contents()); |
+ DCHECK(infobar_service); |
+ |
+ Profile* profile = |
+ Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
+ for (auto it = queued_requests_.begin(); it != queued_requests_.end();) { |
+ int request_id = it->first; |
+ |
+ // Increment the iterator as it will be invalidated. |
+ scoped_ptr<PermissionInfoBarRequest> infobar_request = |
+ queued_requests_.take_and_erase(it++); |
+ |
+ bool will_show = infobar_request->ShowInfobar(infobar_service, profile); |
+ if (will_show) { |
mlamouri (slow - plz ping)
2015/09/28 13:30:15
You might be able to do that in one line.
Lalit Maganti
2015/09/28 15:32:38
Done.
|
+ current_request_id_ = request_id; |
+ current_request_.reset(infobar_request.release()); |
+ break; |
+ } |
+ } |
+} |
+ |
+void PermissionInfoBarManager::CancelInfoBarRequest( |
+ const PermissionRequestID& request_id) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ |
+ if (request_id.request_id() == current_request_id_) { |
+ current_request_->Cancel(false); |
+ return; |
+ } |
+ // No-op if the request does not exist. |
+ queued_requests_.erase(request_id.request_id()); |
+} |
+ |
+void PermissionInfoBarManager::OnInfoBarClosed(bool cancelled) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ |
+ current_request_.reset(); |
+ current_request_id_ = kNoCurrentRequestId; |
+ |
+ // If the current request was cancelled then it is also likely |
+ // that subsequent requests will be cancelled so hold off on |
+ // showing the next infobar straight away. |
+ if (cancelled) |
+ TriggerShowNextQueuedRequest(); |
+ else |
+ ShowNextQueuedRequest(); |
mlamouri (slow - plz ping)
2015/09/28 13:30:14
Could we call Trigger... in both cases? Worse case
Lalit Maganti
2015/09/28 15:32:38
Changed as discussed.
|
+} |
+ |
+bool PermissionInfoBarManager::ShouldIgnoreQueuedRequests() { |
+ return current_request_id_ != kNoCurrentRequestId || queued_requests_.empty(); |
+} |