Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 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/permissions/permission_infobar_manager.h" | |
| 6 | |
| 7 #include "chrome/browser/chrome_notification_types.h" | |
| 8 #include "chrome/browser/infobars/infobar_service.h" | |
| 9 #include "chrome/browser/permissions/permission_infobar_request.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "chrome/browser/tab_contents/tab_util.h" | |
| 12 #include "components/content_settings/core/common/content_settings.h" | |
| 13 #include "components/infobars/core/infobar.h" | |
| 14 #include "content/public/browser/browser_thread.h" | |
| 15 #include "content/public/browser/web_contents.h" | |
| 16 #include "content/public/common/url_constants.h" | |
| 17 | |
| 18 namespace { | |
| 19 // TODO(lalitm) The odd choice for the constant here is because of old code | |
| 20 // passing -1 as a valid request_id. Until that is tracked down and fixed | |
| 21 // 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.
| |
| 22 const int kNoCurrentRequestId = -2; | |
| 23 } | |
| 24 | |
| 25 DEFINE_WEB_CONTENTS_USER_DATA_KEY(PermissionInfoBarManager); | |
| 26 | |
| 27 PermissionInfoBarManager::PermissionInfoBarManager( | |
| 28 content::WebContents* web_contents) | |
| 29 : content::WebContentsObserver(web_contents), | |
| 30 current_request_id_(kNoCurrentRequestId), | |
| 31 is_show_queued_pending_(false), | |
| 32 weak_factory_(this) { | |
| 33 } | |
| 34 | |
| 35 PermissionInfoBarManager::~PermissionInfoBarManager() { | |
| 36 } | |
| 37 | |
| 38 void PermissionInfoBarManager::CreateInfoBarRequest( | |
| 39 const ContentSettingsType type, | |
| 40 const PermissionRequestID& request_id, | |
| 41 const GURL& requesting_frame, | |
| 42 const GURL& embedder, | |
| 43 const PermissionDecidedCallback& user_callback, | |
| 44 const PermissionDecidedCallback& non_user_callback) { | |
| 45 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 46 DCHECK(request_id.request_id() != kNoCurrentRequestId); | |
| 47 | |
| 48 if (requesting_frame.SchemeIs(content::kChromeUIScheme) || | |
| 49 embedder.SchemeIs(content::kChromeUIScheme)) { | |
| 50 return; | |
| 51 } | |
| 52 | |
| 53 auto it = queued_requests_.find(request_id.request_id()); | |
| 54 PermissionInfoBarRequest* request; | |
| 55 if (it == queued_requests_.end()) { | |
| 56 request = new PermissionInfoBarRequest(requesting_frame, embedder, | |
| 57 base::Bind(&PermissionInfoBarManager::OnInfoBarClosed, | |
| 58 weak_factory_.GetWeakPtr())); | |
| 59 queued_requests_.add(request_id.request_id(), make_scoped_ptr(request)); | |
| 60 | |
| 61 // Try to trigger a show for this infobar. | |
| 62 TriggerShowNextQueuedRequest(); | |
| 63 } else { | |
| 64 request = it->second; | |
| 65 } | |
| 66 request->AddPermission(type, user_callback, non_user_callback); | |
| 67 } | |
| 68 | |
| 69 void PermissionInfoBarManager::TriggerShowNextQueuedRequest() { | |
| 70 if (ShouldIgnoreQueuedRequests() || is_show_queued_pending_) | |
| 71 return; | |
| 72 | |
| 73 is_show_queued_pending_ = true; | |
| 74 content::BrowserThread::PostTask( | |
| 75 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
| |
| 76 FROM_HERE, | |
| 77 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
| |
| 78 weak_factory_.GetWeakPtr())); | |
| 79 } | |
| 80 | |
| 81 void PermissionInfoBarManager::ShowNextQueuedRequest() { | |
| 82 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 83 | |
| 84 is_show_queued_pending_ = false; | |
| 85 if (ShouldIgnoreQueuedRequests()) | |
| 86 return; | |
| 87 | |
| 88 InfoBarService* infobar_service = | |
| 89 InfoBarService::FromWebContents(web_contents()); | |
| 90 DCHECK(infobar_service); | |
| 91 | |
| 92 Profile* profile = | |
| 93 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | |
| 94 for (auto it = queued_requests_.begin(); it != queued_requests_.end();) { | |
| 95 int request_id = it->first; | |
| 96 | |
| 97 // Increment the iterator as it will be invalidated. | |
| 98 scoped_ptr<PermissionInfoBarRequest> infobar_request = | |
| 99 queued_requests_.take_and_erase(it++); | |
| 100 | |
| 101 bool will_show = infobar_request->ShowInfobar(infobar_service, profile); | |
| 102 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.
| |
| 103 current_request_id_ = request_id; | |
| 104 current_request_.reset(infobar_request.release()); | |
| 105 break; | |
| 106 } | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 void PermissionInfoBarManager::CancelInfoBarRequest( | |
| 111 const PermissionRequestID& request_id) { | |
| 112 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 113 | |
| 114 if (request_id.request_id() == current_request_id_) { | |
| 115 current_request_->Cancel(false); | |
| 116 return; | |
| 117 } | |
| 118 // No-op if the request does not exist. | |
| 119 queued_requests_.erase(request_id.request_id()); | |
| 120 } | |
| 121 | |
| 122 void PermissionInfoBarManager::OnInfoBarClosed(bool cancelled) { | |
| 123 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 124 | |
| 125 current_request_.reset(); | |
| 126 current_request_id_ = kNoCurrentRequestId; | |
| 127 | |
| 128 // If the current request was cancelled then it is also likely | |
| 129 // that subsequent requests will be cancelled so hold off on | |
| 130 // showing the next infobar straight away. | |
| 131 if (cancelled) | |
| 132 TriggerShowNextQueuedRequest(); | |
| 133 else | |
| 134 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.
| |
| 135 } | |
| 136 | |
| 137 bool PermissionInfoBarManager::ShouldIgnoreQueuedRequests() { | |
| 138 return current_request_id_ != kNoCurrentRequestId || queued_requests_.empty(); | |
| 139 } | |
| OLD | NEW |