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. | |
22 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.
| |
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::CreateRequest( | |
39 const ContentSettingsType type, | |
40 const PermissionRequestID& request_id, | |
41 const GURL& requesting_frame, | |
42 const GURL& embedder, | |
43 const PermissionDecidedCallback& response_callback) { | |
44 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
45 DCHECK(request_id.request_id() != kNoCurrentRequestId); | |
46 | |
47 if (requesting_frame.SchemeIs(content::kChromeUIScheme) || | |
48 embedder.SchemeIs(content::kChromeUIScheme)) { | |
49 return; | |
50 } | |
51 | |
52 auto it = queued_requests_.find(request_id.request_id()); | |
53 PermissionInfoBarRequest* request; | |
mlamouri (slow - plz ping)
2015/09/29 14:02:17
= nullptr;
Lalit Maganti
2015/10/01 17:05:13
Done.
| |
54 if (it == queued_requests_.end()) { | |
55 request = new PermissionInfoBarRequest(requesting_frame, embedder, | |
56 base::Bind(&PermissionInfoBarManager::OnInfoBarClosed, | |
57 weak_factory_.GetWeakPtr())); | |
58 queued_requests_.add(request_id.request_id(), make_scoped_ptr(request)); | |
59 | |
60 // 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
| |
61 TriggerShowNextQueuedRequest(); | |
62 } else { | |
63 request = it->second; | |
64 } | |
65 request->AddPermission(type, response_callback); | |
66 } | |
67 | |
68 void PermissionInfoBarManager::CancelInfoBarRequest( | |
69 const PermissionRequestID& request_id) { | |
70 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
71 | |
72 if (request_id.request_id() == current_request_id_) { | |
73 current_request_->Cancel(); | |
74 return; | |
75 } | |
76 // No-op if the request does not exist. | |
77 queued_requests_.erase(request_id.request_id()); | |
78 } | |
79 | |
80 void PermissionInfoBarManager::Accept() { | |
81 current_request_->Accept(); | |
82 } | |
83 | |
84 void PermissionInfoBarManager::Closing() { | |
85 current_request_->Closing(); | |
86 } | |
87 | |
88 void PermissionInfoBarManager::TriggerShowNextQueuedRequest() { | |
89 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
| |
90 return; | |
91 | |
92 is_show_queued_pending_ = true; | |
93 content::BrowserThread::PostTask( | |
94 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.
| |
95 FROM_HERE, | |
96 base::Bind(&PermissionInfoBarManager::ShowNextQueuedRequest, | |
97 weak_factory_.GetWeakPtr())); | |
98 } | |
99 | |
100 void PermissionInfoBarManager::ShowNextQueuedRequest() { | |
101 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
102 | |
103 is_show_queued_pending_ = false; | |
104 if (ShouldIgnoreQueuedRequests()) | |
105 return; | |
106 | |
107 InfoBarService* infobar_service = | |
108 InfoBarService::FromWebContents(web_contents()); | |
109 DCHECK(infobar_service); | |
110 | |
111 Profile* profile = | |
112 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | |
113 for (auto it = queued_requests_.begin(); it != queued_requests_.end();) { | |
114 int request_id = it->first; | |
115 | |
116 // Increment the iterator as it will be invalidated. | |
117 scoped_ptr<PermissionInfoBarRequest> infobar_request = | |
118 queued_requests_.take_and_erase(it++); | |
119 | |
120 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.
| |
121 current_request_id_ = request_id; | |
122 current_request_.reset(infobar_request.release()); | |
123 break; | |
124 } | |
125 } | |
126 } | |
127 | |
128 void PermissionInfoBarManager::OnInfoBarClosed() { | |
129 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
130 | |
131 current_request_.reset(); | |
132 current_request_id_ = kNoCurrentRequestId; | |
133 | |
134 // TODO(mlamouri, lalitm) TriggerShowNextQueuedRequest should be used here | |
135 // but tests expect a synchronous addition of the new infobar. Change when | |
136 // the tests are fixed. | |
137 ShowNextQueuedRequest(); | |
138 } | |
139 | |
140 bool PermissionInfoBarManager::ShouldIgnoreQueuedRequests() { | |
141 return current_request_id_ != kNoCurrentRequestId || queued_requests_.empty(); | |
142 } | |
OLD | NEW |