Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3872)

Unified Diff: chrome/browser/permissions/permission_infobar_manager.cc

Issue 1337903002: permissions: remove PermissionQueueController and introduce PermissionInfoBarManager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@callbacks-delegates
Patch Set: Rewrite to allowing infobar queuing and non-prompting of allowed/denied permisisons Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..eddfc9ffb75cd5c76de01fe02cc8db5ff4040860
--- /dev/null
+++ b/chrome/browser/permissions/permission_infobar_manager.cc
@@ -0,0 +1,130 @@
+// 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"
+
+DEFINE_WEB_CONTENTS_USER_DATA_KEY(PermissionInfoBarManager);
+
+PermissionInfoBarManager::PermissionInfoBarManager(
+ content::WebContents* web_contents)
+ : content::WebContentsObserver(web_contents),
+ current_request_id_(-1),
+ 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);
+
+ if (requesting_frame.SchemeIs(content::kChromeUIScheme) ||
+ embedder.SchemeIs(content::kChromeUIScheme))
+ return;
mlamouri (slow - plz ping) 2015/09/16 16:21:00 style: wrap around { }
Lalit Maganti 2015/09/16 17:28:38 Done.
+
+ auto it = queued_requests_.find(request_id.request_id());
+ PermissionInfoBarRequest* request;
+ if (it == queued_requests_.end()) {
+ request = new PermissionInfoBarRequest(requesting_frame, embedder);
+ queued_requests_.add(request_id.request_id(), make_scoped_ptr(request));
+
+ // Try to trigger a show for this infobar.
+ TriggerShowQueuedInfoBars();
+ } else {
+ request = it->second;
+ }
+ request->AddPermission(type, user_callback, non_user_callback);
+}
+
+void PermissionInfoBarManager::TriggerShowQueuedInfoBars() {
+ if (ShouldIgnoreQueuedRequests() || is_show_queued_pending_)
+ return;
+
+ is_show_queued_pending_ = true;
+ content::BrowserThread::PostTask(
+ content::BrowserThread::UI,
+ FROM_HERE,
+ base::Bind(&PermissionInfoBarManager::ShowQueuedInfoBars,
+ weak_factory_.GetWeakPtr()));
+}
+
+void PermissionInfoBarManager::ShowQueuedInfoBars() {
+ 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();) {
+ // Increment the iterator as it will be invalidated.
mlamouri (slow - plz ping) 2015/09/16 16:21:00 nit: comment misplaced?
Lalit Maganti 2015/09/16 17:28:38 Slightly yes. Fixed.
+ int request_id = it->first;
+ scoped_ptr<PermissionInfoBarRequest> infobar_request =
+ queued_requests_.take_and_erase(it++);
+
+ bool will_show = infobar_request->ShowInfobar(
+ infobar_service,
+ profile,
+ base::Bind(&PermissionInfoBarManager::OnInfoBarRequestCompleted,
+ weak_factory_.GetWeakPtr()));
+ if (will_show) {
+ 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();
+ ClearCurrentRequest();
+ return;
+ }
+ // Request may not exist but that's OK.
mlamouri (slow - plz ping) 2015/09/16 16:21:00 rephrase: // No-op if the request does not exist.
Lalit Maganti 2015/09/16 17:28:38 Done.
+ queued_requests_.erase(request_id.request_id());
+}
+
+void PermissionInfoBarManager::OnInfoBarRequestCompleted() {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+ ClearCurrentRequest();
+}
+
+void PermissionInfoBarManager::ClearCurrentRequest() {
+ current_request_.reset();
+ current_request_id_ = -1;
+
+ // Show any other pending infobars which may exist.
+ TriggerShowQueuedInfoBars();
+}
+
+bool PermissionInfoBarManager::ShouldIgnoreQueuedRequests() {
+ return current_request_id_ != -1 || queued_requests_.empty();
+}

Powered by Google App Engine
This is Rietveld 408576698