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 DEFINE_WEB_CONTENTS_USER_DATA_KEY(PermissionInfoBarManager); | |
| 19 | |
| 20 PermissionInfoBarManager::PermissionInfoBarManager( | |
| 21 content::WebContents* web_contents) | |
| 22 : content::WebContentsObserver(web_contents), | |
| 23 is_show_pending_(false), | |
| 24 weak_factory_(this) { | |
| 25 } | |
| 26 | |
| 27 PermissionInfoBarManager::~PermissionInfoBarManager() { | |
| 28 } | |
| 29 | |
| 30 void PermissionInfoBarManager::CreateRequest( | |
| 31 const ContentSettingsType type, | |
| 32 const PermissionRequestID& request_id, | |
| 33 const GURL& requesting_frame, | |
| 34 const GURL& embedder, | |
| 35 const PermissionDecidedCallback& response_callback) { | |
|
mlamouri (slow - plz ping)
2015/10/02 15:51:06
nit: rename to callback
Lalit Maganti
2015/10/02 16:06:43
Done.
| |
| 36 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 37 DCHECK(!current_request_.get() || | |
| 38 current_request_->request_id() != request_id.request_id()); | |
| 39 | |
| 40 if (requesting_frame.SchemeIs(content::kChromeUIScheme) || | |
| 41 embedder.SchemeIs(content::kChromeUIScheme)) { | |
| 42 return; | |
| 43 } | |
| 44 | |
| 45 auto it = queued_requests_.find(request_id.request_id()); | |
| 46 PermissionInfoBarRequest* request = nullptr; | |
| 47 if (it == queued_requests_.end()) { | |
| 48 request = new PermissionInfoBarRequest(request_id.request_id(), | |
| 49 requesting_frame, embedder, | |
| 50 base::Bind(&PermissionInfoBarManager::OnInfoBarClosed, | |
| 51 base::Unretained(this))); | |
| 52 queued_requests_.add(request_id.request_id(), make_scoped_ptr(request)); | |
| 53 | |
| 54 // Try to trigger a show for this infobar. | |
| 55 TriggerShowNextQueuedRequest(); | |
| 56 } else { | |
| 57 request = it->second; | |
| 58 } | |
| 59 request->AddPermission(type, response_callback); | |
| 60 } | |
| 61 | |
| 62 void PermissionInfoBarManager::CancelRequest( | |
| 63 const PermissionRequestID& request_id) { | |
| 64 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 65 | |
| 66 if (current_request_.get() && | |
| 67 current_request_->request_id() == request_id.request_id()) { | |
| 68 current_request_->Cancel(); | |
| 69 return; | |
| 70 } | |
| 71 | |
| 72 // No-op if the request does not exist. | |
| 73 queued_requests_.erase(request_id.request_id()); | |
| 74 } | |
| 75 | |
| 76 void PermissionInfoBarManager::TriggerShowNextQueuedRequest() { | |
| 77 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 78 if (is_show_pending_ || ShouldIgnoreQueuedRequests()) | |
| 79 return; | |
| 80 | |
| 81 is_show_pending_ = true; | |
| 82 content::BrowserThread::PostTask( | |
| 83 content::BrowserThread::UI, | |
| 84 FROM_HERE, | |
| 85 base::Bind(&PermissionInfoBarManager::ShowNextQueuedRequest, | |
| 86 weak_factory_.GetWeakPtr())); | |
| 87 } | |
| 88 | |
| 89 void PermissionInfoBarManager::ShowNextQueuedRequest() { | |
| 90 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 91 | |
| 92 is_show_pending_ = false; | |
| 93 if (ShouldIgnoreQueuedRequests()) | |
| 94 return; | |
| 95 | |
| 96 for (auto it = queued_requests_.begin(); it != queued_requests_.end();) { | |
| 97 // Increment the iterator as it will be invalidated. | |
| 98 scoped_ptr<PermissionInfoBarRequest> infobar_request = | |
| 99 queued_requests_.take_and_erase(it++); | |
| 100 | |
| 101 if (infobar_request->ShowInfobar(web_contents())) { | |
|
mlamouri (slow - plz ping)
2015/10/02 15:51:06
Could you add a comment explaining that if ShowInf
Lalit Maganti
2015/10/02 16:06:43
Done.
| |
| 102 current_request_.reset(infobar_request.release()); | |
| 103 break; | |
| 104 } | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 void PermissionInfoBarManager::OnInfoBarClosed() { | |
| 109 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 110 | |
| 111 current_request_.reset(); | |
| 112 TriggerShowNextQueuedRequest(); | |
| 113 } | |
| 114 | |
| 115 bool PermissionInfoBarManager::ShouldIgnoreQueuedRequests() { | |
| 116 return current_request_.get() || queued_requests_.empty(); | |
| 117 } | |
| OLD | NEW |