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..5c241f632fdf7e415c895c150206a29f05dc6c25 |
--- /dev/null |
+++ b/chrome/browser/permissions/permission_infobar_manager.cc |
@@ -0,0 +1,142 @@ |
+// 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. |
+const int kNoCurrentRequestId = -2; |
mlamouri (slow - plz ping)
2015/09/29 14:02:17
Could you have a CL that fixes that? Before or aft
Lalit Maganti
2015/10/01 17:05:13
Will do.
|
+} |
+ |
+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::CreateRequest( |
+ const ContentSettingsType type, |
+ const PermissionRequestID& request_id, |
+ const GURL& requesting_frame, |
+ const GURL& embedder, |
+ const PermissionDecidedCallback& response_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; |
mlamouri (slow - plz ping)
2015/09/29 14:02:17
= nullptr;
Lalit Maganti
2015/10/01 17:05:13
Done.
|
+ 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. |
mlamouri (slow - plz ping)
2015/09/29 14:02:17
nit: why "Try"?
Lalit Maganti
2015/10/01 17:05:13
Because it could fail due to a show being pending
|
+ TriggerShowNextQueuedRequest(); |
+ } else { |
+ request = it->second; |
+ } |
+ request->AddPermission(type, response_callback); |
+} |
+ |
+void PermissionInfoBarManager::CancelInfoBarRequest( |
+ const PermissionRequestID& request_id) { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ |
+ if (request_id.request_id() == current_request_id_) { |
+ current_request_->Cancel(); |
+ return; |
+ } |
+ // No-op if the request does not exist. |
+ queued_requests_.erase(request_id.request_id()); |
+} |
+ |
+void PermissionInfoBarManager::Accept() { |
+ current_request_->Accept(); |
+} |
+ |
+void PermissionInfoBarManager::Closing() { |
+ current_request_->Closing(); |
+} |
+ |
+void PermissionInfoBarManager::TriggerShowNextQueuedRequest() { |
+ if (ShouldIgnoreQueuedRequests() || is_show_queued_pending_) |
mlamouri (slow - plz ping)
2015/09/29 14:02:17
Should |ShouldIgnoreQueuedRequests()| take |is_sho
Lalit Maganti
2015/10/01 17:05:13
I did think about this but it made the code in |Sh
|
+ return; |
+ |
+ is_show_queued_pending_ = true; |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::UI, |
mlamouri (slow - plz ping)
2015/09/29 14:02:18
Please, add a DCHECK_CURRENTLY_ON(content::Browser
Lalit Maganti
2015/10/01 17:05:13
Done.
|
+ FROM_HERE, |
+ base::Bind(&PermissionInfoBarManager::ShowNextQueuedRequest, |
+ 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++); |
+ |
+ if (infobar_request->ShowInfobar(infobar_service, profile)) { |
mlamouri (slow - plz ping)
2015/09/29 14:02:17
You could pass |web_contents| only here. You can g
Lalit Maganti
2015/10/01 17:05:13
Done.
|
+ current_request_id_ = request_id; |
+ current_request_.reset(infobar_request.release()); |
+ break; |
+ } |
+ } |
+} |
+ |
+void PermissionInfoBarManager::OnInfoBarClosed() { |
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
+ |
+ current_request_.reset(); |
+ current_request_id_ = kNoCurrentRequestId; |
+ |
+ // TODO(mlamouri, lalitm) TriggerShowNextQueuedRequest should be used here |
+ // but tests expect a synchronous addition of the new infobar. Change when |
+ // the tests are fixed. |
+ ShowNextQueuedRequest(); |
+} |
+ |
+bool PermissionInfoBarManager::ShouldIgnoreQueuedRequests() { |
+ return current_request_id_ != kNoCurrentRequestId || queued_requests_.empty(); |
+} |