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

Side by Side Diff: chrome/browser/permissions/permission_queue_controller.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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2013 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_queue_controller.h"
6
7 #include "base/prefs/pref_service.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/geolocation/geolocation_infobar_delegate.h"
10 #include "chrome/browser/infobars/infobar_service.h"
11 #include "chrome/browser/media/midi_permission_infobar_delegate.h"
12 #include "chrome/browser/notifications/notification_permission_infobar_delegate. h"
13 #include "chrome/browser/permissions/permission_context_uma_util.h"
14 #include "chrome/browser/permissions/permission_request_id.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/storage/durable_storage_permission_infobar_delegate.h"
17 #include "chrome/browser/tab_contents/tab_util.h"
18 #include "chrome/common/pref_names.h"
19 #include "components/content_settings/core/browser/host_content_settings_map.h"
20 #include "components/content_settings/core/common/content_settings.h"
21 #include "components/infobars/core/infobar.h"
22 #include "content/public/browser/browser_thread.h"
23 #include "content/public/browser/notification_details.h"
24 #include "content/public/browser/notification_source.h"
25 #include "content/public/browser/notification_types.h"
26 #include "content/public/browser/web_contents.h"
27 #include "content/public/common/url_constants.h"
28
29 #if defined(OS_ANDROID) || defined(OS_CHROMEOS)
30 #include "chrome/browser/media/protected_media_identifier_infobar_delegate.h"
31 #endif
32
33 namespace {
34
35 InfoBarService* GetInfoBarService(const PermissionRequestID& id) {
36 content::WebContents* web_contents = tab_util::GetWebContentsByFrameID(
37 id.render_process_id(), id.render_frame_id());
38 return web_contents ? InfoBarService::FromWebContents(web_contents) : NULL;
39 }
40
41 bool ArePermissionRequestsForSameTab(
42 const PermissionRequestID& request,
43 const PermissionRequestID& other_request) {
44 content::WebContents* web_contents = tab_util::GetWebContentsByFrameID(
45 request.render_process_id(), request.render_frame_id());
46 content::WebContents* other_web_contents = tab_util::GetWebContentsByFrameID(
47 other_request.render_process_id(), other_request.render_frame_id());
48
49 return web_contents == other_web_contents;
50 }
51
52 } // anonymous namespace
53
54 class PermissionQueueController::PendingInfobarRequest {
55 public:
56 PendingInfobarRequest(ContentSettingsType type,
57 const PermissionRequestID& id,
58 const GURL& requesting_frame,
59 const GURL& embedder,
60 const PermissionDecidedCallback& callback);
61 ~PendingInfobarRequest();
62
63 bool IsForPair(const GURL& requesting_frame,
64 const GURL& embedder) const;
65
66 const PermissionRequestID& id() const { return id_; }
67 const GURL& requesting_frame() const { return requesting_frame_; }
68 bool has_infobar() const { return !!infobar_; }
69 infobars::InfoBar* infobar() { return infobar_; }
70
71 void RunCallback(ContentSetting content_setting);
72 void CreateInfoBar(PermissionQueueController* controller,
73 const std::string& display_languages);
74
75 private:
76 ContentSettingsType type_;
77 PermissionRequestID id_;
78 GURL requesting_frame_;
79 GURL embedder_;
80 PermissionDecidedCallback callback_;
81 infobars::InfoBar* infobar_;
82
83 // Purposefully do not disable copying, as this is stored in STL containers.
84 };
85
86 PermissionQueueController::PendingInfobarRequest::PendingInfobarRequest(
87 ContentSettingsType type,
88 const PermissionRequestID& id,
89 const GURL& requesting_frame,
90 const GURL& embedder,
91 const PermissionDecidedCallback& callback)
92 : type_(type),
93 id_(id),
94 requesting_frame_(requesting_frame),
95 embedder_(embedder),
96 callback_(callback),
97 infobar_(NULL) {
98 }
99
100 PermissionQueueController::PendingInfobarRequest::~PendingInfobarRequest() {
101 }
102
103 bool PermissionQueueController::PendingInfobarRequest::IsForPair(
104 const GURL& requesting_frame,
105 const GURL& embedder) const {
106 return (requesting_frame_ == requesting_frame) && (embedder_ == embedder);
107 }
108
109 void PermissionQueueController::PendingInfobarRequest::RunCallback(
110 ContentSetting content_setting) {
111 callback_.Run(content_setting);
112 }
113
114 void PermissionQueueController::PendingInfobarRequest::CreateInfoBar(
115 PermissionQueueController* controller,
116 const std::string& display_languages) {
117 // Controller can be Unretained because the lifetime of the infobar
118 // is tied to that of the queue controller. Before QueueController
119 // is destroyed, all requests will be cancelled and so all delegates
120 // will be destroyed.
121 PermissionInfobarDelegate::PermissionSetCallback callback =
122 base::Bind(&PermissionQueueController::OnPermissionSet,
123 base::Unretained(controller),
124 id_,
125 requesting_frame_,
126 embedder_);
127 switch (type_) {
128 case CONTENT_SETTINGS_TYPE_GEOLOCATION:
129 infobar_ = GeolocationInfoBarDelegate::Create(
130 GetInfoBarService(id_), requesting_frame_,
131 display_languages, callback);
132 break;
133 #if defined(ENABLE_NOTIFICATIONS)
134 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS:
135 infobar_ = NotificationPermissionInfobarDelegate::Create(
136 GetInfoBarService(id_), requesting_frame_,
137 display_languages, callback);
138 break;
139 #endif // ENABLE_NOTIFICATIONS
140 case CONTENT_SETTINGS_TYPE_MIDI_SYSEX:
141 infobar_ = MidiPermissionInfoBarDelegate::Create(
142 GetInfoBarService(id_), requesting_frame_,
143 display_languages, type_, callback);
144 break;
145 #if defined(OS_ANDROID) || defined(OS_CHROMEOS)
146 case CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER:
147 infobar_ = ProtectedMediaIdentifierInfoBarDelegate::Create(
148 GetInfoBarService(id_), requesting_frame_,
149 display_languages, callback);
150 break;
151 #endif
152 case CONTENT_SETTINGS_TYPE_DURABLE_STORAGE:
153 infobar_ = DurableStoragePermissionInfoBarDelegate::Create(
154 GetInfoBarService(id_), requesting_frame_,
155 display_languages, type_, callback);
156 break;
157 default:
158 NOTREACHED();
159 break;
160 }
161 }
162
163
164 PermissionQueueController::PermissionQueueController(Profile* profile,
165 ContentSettingsType type)
166 : profile_(profile),
167 type_(type),
168 in_shutdown_(false) {
169 }
170
171 PermissionQueueController::~PermissionQueueController() {
172 // Cancel all outstanding requests.
173 in_shutdown_ = true;
174 while (!pending_infobar_requests_.empty())
175 CancelInfoBarRequest(pending_infobar_requests_.front().id());
176 }
177
178 void PermissionQueueController::CreateInfoBarRequest(
179 const PermissionRequestID& id,
180 const GURL& requesting_frame,
181 const GURL& embedder,
182 const PermissionDecidedCallback& callback) {
183 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
184
185 if (requesting_frame.SchemeIs(content::kChromeUIScheme) ||
186 embedder.SchemeIs(content::kChromeUIScheme))
187 return;
188
189 pending_infobar_requests_.push_back(PendingInfobarRequest(
190 type_, id, requesting_frame, embedder, callback));
191 if (!AlreadyShowingInfoBarForTab(id))
192 ShowQueuedInfoBarForTab(id);
193 }
194
195 void PermissionQueueController::CancelInfoBarRequest(
196 const PermissionRequestID& id) {
197 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
198
199 for (PendingInfobarRequests::iterator i(pending_infobar_requests_.begin());
200 i != pending_infobar_requests_.end(); ++i) {
201 if (id != i->id())
202 continue;
203
204 InfoBarService* infobar_service = GetInfoBarService(id);
205 if (infobar_service && i->has_infobar())
206 infobar_service->RemoveInfoBar(i->infobar());
207 else
208 pending_infobar_requests_.erase(i);
209 return;
210 }
211 }
212
213 void PermissionQueueController::OnPermissionSet(
214 const PermissionRequestID& id,
215 const GURL& requesting_frame,
216 const GURL& embedder,
217 bool update_content_setting,
218 bool allowed) {
219 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
220
221 // TODO(miguelg): move the permission persistence to
222 // PermissionContextBase once all the types are moved there.
223 if (update_content_setting) {
224 UpdateContentSetting(requesting_frame, embedder, allowed);
225 if (allowed)
226 PermissionContextUmaUtil::PermissionGranted(type_, requesting_frame);
227 else
228 PermissionContextUmaUtil::PermissionDenied(type_, requesting_frame);
229 } else {
230 PermissionContextUmaUtil::PermissionDismissed(type_, requesting_frame);
231 }
232
233 // Cancel this request first, then notify listeners. TODO(pkasting): Why
234 // is this order important?
235 PendingInfobarRequests requests_to_notify;
236 PendingInfobarRequests infobars_to_remove;
237 std::vector<PendingInfobarRequests::iterator> pending_requests_to_remove;
238 for (PendingInfobarRequests::iterator i = pending_infobar_requests_.begin();
239 i != pending_infobar_requests_.end(); ++i) {
240 if (!i->IsForPair(requesting_frame, embedder))
241 continue;
242 requests_to_notify.push_back(*i);
243 if (!i->has_infobar()) {
244 // We haven't created an infobar yet, just record the pending request
245 // index and remove it later.
246 pending_requests_to_remove.push_back(i);
247 continue;
248 }
249 if (id == i->id()) {
250 // The infobar that called us is i->infobar(), and its delegate is
251 // currently in either Accept() or Cancel(). This means that
252 // RemoveInfoBar() will be called later on, and that will trigger a
253 // notification we're observing.
254 continue;
255 }
256
257 // This infobar is for the same frame/embedder pair, but in a different
258 // tab. We should remove it now that we've got an answer for it.
259 infobars_to_remove.push_back(*i);
260 }
261
262 // Remove all infobars for the same |requesting_frame| and |embedder|.
263 for (PendingInfobarRequests::iterator i = infobars_to_remove.begin();
264 i != infobars_to_remove.end(); ++i)
265 GetInfoBarService(i->id())->RemoveInfoBar(i->infobar());
266
267 // PermissionContextBase needs to know about the new ContentSetting value,
268 // CONTENT_SETTING_DEFAULT being the value for nothing happened. The callers
269 // of ::OnPermissionSet passes { true, true } for allow, { true, false } for
270 // block and { false, * } for dismissed. The tuple being
271 // { update_content_setting, allowed }.
272 ContentSetting content_setting = CONTENT_SETTING_DEFAULT;
273 if (update_content_setting) {
274 content_setting = allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK;
275 }
276
277 // Send out the permission notifications.
278 for (PendingInfobarRequests::iterator i = requests_to_notify.begin();
279 i != requests_to_notify.end(); ++i)
280 i->RunCallback(content_setting);
281
282 // Remove the pending requests in reverse order.
283 for (int i = pending_requests_to_remove.size() - 1; i >= 0; --i)
284 pending_infobar_requests_.erase(pending_requests_to_remove[i]);
285 }
286
287 void PermissionQueueController::Observe(
288 int type,
289 const content::NotificationSource& source,
290 const content::NotificationDetails& details) {
291 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type);
292 // We will receive this notification for all infobar closures, so we need to
293 // check whether this is the geolocation infobar we're tracking. Note that the
294 // InfoBarContainer (if any) may have received this notification before us and
295 // caused the infobar to be deleted, so it's not safe to dereference the
296 // contents of the infobar. The address of the infobar, however, is OK to
297 // use to find the PendingInfobarRequest to remove because
298 // pending_infobar_requests_ will not have received any new entries between
299 // the NotificationService's call to InfoBarContainer::Observe and this
300 // method.
301 infobars::InfoBar* infobar =
302 content::Details<infobars::InfoBar::RemovedDetails>(details)->first;
303 for (PendingInfobarRequests::iterator i = pending_infobar_requests_.begin();
304 i != pending_infobar_requests_.end(); ++i) {
305 if (i->infobar() == infobar) {
306 PermissionRequestID id(i->id());
307 pending_infobar_requests_.erase(i);
308 ShowQueuedInfoBarForTab(id);
309 return;
310 }
311 }
312 }
313
314 bool PermissionQueueController::AlreadyShowingInfoBarForTab(
315 const PermissionRequestID& id) const {
316 for (PendingInfobarRequests::const_iterator i(
317 pending_infobar_requests_.begin());
318 i != pending_infobar_requests_.end(); ++i) {
319 if (ArePermissionRequestsForSameTab(i->id(), id) && i->has_infobar())
320 return true;
321 }
322 return false;
323 }
324
325 void PermissionQueueController::ShowQueuedInfoBarForTab(
326 const PermissionRequestID& id) {
327 DCHECK(!AlreadyShowingInfoBarForTab(id));
328
329 // We can get here for example during tab shutdown, when the InfoBarService is
330 // removing all existing infobars, thus calling back to Observe(). In this
331 // case the service still exists, and is supplied as the source of the
332 // notification we observed, but is no longer accessible from its WebContents.
333 // In this case we should just go ahead and cancel further infobars for this
334 // tab instead of trying to access the service.
335 //
336 // Similarly, if we're being destroyed, we should also avoid showing further
337 // infobars.
338 InfoBarService* infobar_service = GetInfoBarService(id);
339 if (!infobar_service || in_shutdown_) {
340 ClearPendingInfobarRequestsForTab(id);
341 return;
342 }
343
344 for (PendingInfobarRequests::iterator i = pending_infobar_requests_.begin();
345 i != pending_infobar_requests_.end(); ++i) {
346 if (ArePermissionRequestsForSameTab(i->id(), id) && !i->has_infobar()) {
347 RegisterForInfoBarNotifications(infobar_service);
348 i->CreateInfoBar(
349 this, profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
350 return;
351 }
352 }
353
354 UnregisterForInfoBarNotifications(infobar_service);
355 }
356
357 void PermissionQueueController::ClearPendingInfobarRequestsForTab(
358 const PermissionRequestID& id) {
359 for (PendingInfobarRequests::iterator i = pending_infobar_requests_.begin();
360 i != pending_infobar_requests_.end(); ) {
361 if (ArePermissionRequestsForSameTab(i->id(), id)) {
362 DCHECK(!i->has_infobar());
363 i = pending_infobar_requests_.erase(i);
364 } else {
365 ++i;
366 }
367 }
368 }
369
370 void PermissionQueueController::RegisterForInfoBarNotifications(
371 InfoBarService* infobar_service) {
372 if (!registrar_.IsRegistered(
373 this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
374 content::Source<InfoBarService>(infobar_service))) {
375 registrar_.Add(this,
376 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
377 content::Source<InfoBarService>(infobar_service));
378 }
379 }
380
381 void PermissionQueueController::UnregisterForInfoBarNotifications(
382 InfoBarService* infobar_service) {
383 if (registrar_.IsRegistered(
384 this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
385 content::Source<InfoBarService>(infobar_service))) {
386 registrar_.Remove(this,
387 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
388 content::Source<InfoBarService>(infobar_service));
389 }
390 }
391
392 void PermissionQueueController::UpdateContentSetting(
393 const GURL& requesting_frame,
394 const GURL& embedder,
395 bool allowed) {
396 if (requesting_frame.GetOrigin().SchemeIsFile()) {
397 // Chrome can be launched with --disable-web-security which allows
398 // geolocation requests from file:// URLs. We don't want to store these
399 // in the host content settings map.
400 return;
401 }
402
403 ContentSetting content_setting =
404 allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK;
405
406 ContentSettingsPattern embedder_pattern =
407 (type_ == CONTENT_SETTINGS_TYPE_NOTIFICATIONS) ?
408 ContentSettingsPattern::Wildcard() :
409 ContentSettingsPattern::FromURLNoWildcard(embedder.GetOrigin());
410
411 profile_->GetHostContentSettingsMap()->SetContentSetting(
412 ContentSettingsPattern::FromURLNoWildcard(requesting_frame.GetOrigin()),
413 embedder_pattern,
414 type_,
415 std::string(),
416 content_setting);
417 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698