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 current_request_id_(-1), | |
| 24 is_show_queued_pending_(false), | |
| 25 weak_factory_(this) { | |
| 26 } | |
| 27 | |
| 28 PermissionInfoBarManager::~PermissionInfoBarManager() { | |
| 29 } | |
| 30 | |
| 31 void PermissionInfoBarManager::CreateInfoBarRequest( | |
| 32 const ContentSettingsType type, | |
| 33 const PermissionRequestID& request_id, | |
| 34 const GURL& requesting_frame, | |
| 35 const GURL& embedder, | |
| 36 const PermissionDecidedCallback& user_callback, | |
| 37 const PermissionDecidedCallback& non_user_callback) { | |
| 38 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 39 | |
| 40 if (requesting_frame.SchemeIs(content::kChromeUIScheme) || | |
| 41 embedder.SchemeIs(content::kChromeUIScheme)) | |
| 42 return; | |
|
mlamouri (slow - plz ping)
2015/09/16 16:21:00
style: wrap around { }
Lalit Maganti
2015/09/16 17:28:38
Done.
| |
| 43 | |
| 44 auto it = queued_requests_.find(request_id.request_id()); | |
| 45 PermissionInfoBarRequest* request; | |
| 46 if (it == queued_requests_.end()) { | |
| 47 request = new PermissionInfoBarRequest(requesting_frame, embedder); | |
| 48 queued_requests_.add(request_id.request_id(), make_scoped_ptr(request)); | |
| 49 | |
| 50 // Try to trigger a show for this infobar. | |
| 51 TriggerShowQueuedInfoBars(); | |
| 52 } else { | |
| 53 request = it->second; | |
| 54 } | |
| 55 request->AddPermission(type, user_callback, non_user_callback); | |
| 56 } | |
| 57 | |
| 58 void PermissionInfoBarManager::TriggerShowQueuedInfoBars() { | |
| 59 if (ShouldIgnoreQueuedRequests() || is_show_queued_pending_) | |
| 60 return; | |
| 61 | |
| 62 is_show_queued_pending_ = true; | |
| 63 content::BrowserThread::PostTask( | |
| 64 content::BrowserThread::UI, | |
| 65 FROM_HERE, | |
| 66 base::Bind(&PermissionInfoBarManager::ShowQueuedInfoBars, | |
| 67 weak_factory_.GetWeakPtr())); | |
| 68 } | |
| 69 | |
| 70 void PermissionInfoBarManager::ShowQueuedInfoBars() { | |
| 71 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 72 | |
| 73 is_show_queued_pending_ = false; | |
| 74 if (ShouldIgnoreQueuedRequests()) | |
| 75 return; | |
| 76 | |
| 77 InfoBarService* infobar_service = | |
| 78 InfoBarService::FromWebContents(web_contents()); | |
| 79 DCHECK(infobar_service); | |
| 80 | |
| 81 Profile* profile = | |
| 82 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | |
| 83 for (auto it = queued_requests_.begin(); it != queued_requests_.end();) { | |
| 84 // 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.
| |
| 85 int request_id = it->first; | |
| 86 scoped_ptr<PermissionInfoBarRequest> infobar_request = | |
| 87 queued_requests_.take_and_erase(it++); | |
| 88 | |
| 89 bool will_show = infobar_request->ShowInfobar( | |
| 90 infobar_service, | |
| 91 profile, | |
| 92 base::Bind(&PermissionInfoBarManager::OnInfoBarRequestCompleted, | |
| 93 weak_factory_.GetWeakPtr())); | |
| 94 if (will_show) { | |
| 95 current_request_id_ = request_id; | |
| 96 current_request_.reset(infobar_request.release()); | |
| 97 break; | |
| 98 } | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 void PermissionInfoBarManager::CancelInfoBarRequest( | |
| 103 const PermissionRequestID& request_id) { | |
| 104 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 105 | |
| 106 if (request_id.request_id() == current_request_id_) { | |
| 107 current_request_->Cancel(); | |
| 108 ClearCurrentRequest(); | |
| 109 return; | |
| 110 } | |
| 111 // 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.
| |
| 112 queued_requests_.erase(request_id.request_id()); | |
| 113 } | |
| 114 | |
| 115 void PermissionInfoBarManager::OnInfoBarRequestCompleted() { | |
| 116 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 117 ClearCurrentRequest(); | |
| 118 } | |
| 119 | |
| 120 void PermissionInfoBarManager::ClearCurrentRequest() { | |
| 121 current_request_.reset(); | |
| 122 current_request_id_ = -1; | |
| 123 | |
| 124 // Show any other pending infobars which may exist. | |
| 125 TriggerShowQueuedInfoBars(); | |
| 126 } | |
| 127 | |
| 128 bool PermissionInfoBarManager::ShouldIgnoreQueuedRequests() { | |
| 129 return current_request_id_ != -1 || queued_requests_.empty(); | |
| 130 } | |
| OLD | NEW |