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.
5 #include "chrome/browser/permissions/permission_queue_controller.h"
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/profiles/profile.h"
15 #include "chrome/browser/tab_contents/tab_util.h"
16 #include "chrome/common/pref_names.h"
17 #include "components/content_settings/core/browser/host_content_settings_map.h"
18 #include "components/content_settings/core/common/content_settings.h"
19 #include "components/infobars/core/infobar.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/browser/notification_details.h"
22 #include "content/public/browser/notification_source.h"
23 #include "content/public/browser/notification_types.h"
24 #include "content/public/browser/web_contents.h"
25 #include "content/public/common/url_constants.h"
27 #if defined(OS_ANDROID) || defined(OS_CHROMEOS)
28 #include "chrome/browser/media/protected_media_identifier_infobar_delegate.h"
33 InfoBarService
* GetInfoBarService(const PermissionRequestID
& id
) {
34 content::WebContents
* web_contents
= tab_util::GetWebContentsByFrameID(
35 id
.render_process_id(), id
.render_frame_id());
36 return web_contents
? InfoBarService::FromWebContents(web_contents
) : NULL
;
39 bool ArePermissionRequestsForSameTab(
40 const PermissionRequestID
& request
,
41 const PermissionRequestID
& other_request
) {
42 content::WebContents
* web_contents
= tab_util::GetWebContentsByFrameID(
43 request
.render_process_id(), request
.render_frame_id());
44 content::WebContents
* other_web_contents
= tab_util::GetWebContentsByFrameID(
45 other_request
.render_process_id(), other_request
.render_frame_id());
47 return web_contents
== other_web_contents
;
50 } // anonymous namespace
52 class PermissionQueueController::PendingInfobarRequest
{
54 PendingInfobarRequest(ContentSettingsType type
,
55 const PermissionRequestID
& id
,
56 const GURL
& requesting_frame
,
58 const PermissionDecidedCallback
& callback
);
59 ~PendingInfobarRequest();
61 bool IsForPair(const GURL
& requesting_frame
,
62 const GURL
& embedder
) const;
64 const PermissionRequestID
& id() const { return id_
; }
65 const GURL
& requesting_frame() const { return requesting_frame_
; }
66 bool has_infobar() const { return !!infobar_
; }
67 infobars::InfoBar
* infobar() { return infobar_
; }
69 void RunCallback(ContentSetting content_setting
);
70 void CreateInfoBar(PermissionQueueController
* controller
,
71 const std::string
& display_languages
);
74 ContentSettingsType type_
;
75 PermissionRequestID id_
;
76 GURL requesting_frame_
;
78 PermissionDecidedCallback callback_
;
79 infobars::InfoBar
* infobar_
;
81 // Purposefully do not disable copying, as this is stored in STL containers.
84 PermissionQueueController::PendingInfobarRequest::PendingInfobarRequest(
85 ContentSettingsType type
,
86 const PermissionRequestID
& id
,
87 const GURL
& requesting_frame
,
89 const PermissionDecidedCallback
& callback
)
92 requesting_frame_(requesting_frame
),
98 PermissionQueueController::PendingInfobarRequest::~PendingInfobarRequest() {
101 bool PermissionQueueController::PendingInfobarRequest::IsForPair(
102 const GURL
& requesting_frame
,
103 const GURL
& embedder
) const {
104 return (requesting_frame_
== requesting_frame
) && (embedder_
== embedder
);
107 void PermissionQueueController::PendingInfobarRequest::RunCallback(
108 ContentSetting content_setting
) {
109 callback_
.Run(content_setting
);
112 void PermissionQueueController::PendingInfobarRequest::CreateInfoBar(
113 PermissionQueueController
* controller
,
114 const std::string
& display_languages
) {
116 case CONTENT_SETTINGS_TYPE_GEOLOCATION
:
117 infobar_
= GeolocationInfoBarDelegate::Create(
118 GetInfoBarService(id_
), controller
, id_
, requesting_frame_
,
121 #if defined(ENABLE_NOTIFICATIONS)
122 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS
:
123 infobar_
= NotificationPermissionInfobarDelegate::Create(
124 GetInfoBarService(id_
), controller
, id_
, requesting_frame_
,
127 #endif // ENABLE_NOTIFICATIONS
128 case CONTENT_SETTINGS_TYPE_MIDI_SYSEX
:
129 infobar_
= MidiPermissionInfoBarDelegate::Create(
130 GetInfoBarService(id_
), controller
, id_
, requesting_frame_
,
131 display_languages
, type_
);
133 #if defined(OS_ANDROID) || defined(OS_CHROMEOS)
134 case CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER
:
135 infobar_
= ProtectedMediaIdentifierInfoBarDelegate::Create(
136 GetInfoBarService(id_
), controller
, id_
, requesting_frame_
,
147 PermissionQueueController::PermissionQueueController(Profile
* profile
,
148 ContentSettingsType type
)
151 in_shutdown_(false) {
154 PermissionQueueController::~PermissionQueueController() {
155 // Cancel all outstanding requests.
157 while (!pending_infobar_requests_
.empty())
158 CancelInfoBarRequest(pending_infobar_requests_
.front().id());
161 void PermissionQueueController::CreateInfoBarRequest(
162 const PermissionRequestID
& id
,
163 const GURL
& requesting_frame
,
164 const GURL
& embedder
,
165 const PermissionDecidedCallback
& callback
) {
166 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
168 if (requesting_frame
.SchemeIs(content::kChromeUIScheme
) ||
169 embedder
.SchemeIs(content::kChromeUIScheme
))
172 pending_infobar_requests_
.push_back(PendingInfobarRequest(
173 type_
, id
, requesting_frame
, embedder
, callback
));
174 if (!AlreadyShowingInfoBarForTab(id
))
175 ShowQueuedInfoBarForTab(id
);
178 void PermissionQueueController::CancelInfoBarRequest(
179 const PermissionRequestID
& id
) {
180 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
182 for (PendingInfobarRequests::iterator
i(pending_infobar_requests_
.begin());
183 i
!= pending_infobar_requests_
.end(); ++i
) {
187 InfoBarService
* infobar_service
= GetInfoBarService(id
);
188 if (infobar_service
&& i
->has_infobar())
189 infobar_service
->RemoveInfoBar(i
->infobar());
191 pending_infobar_requests_
.erase(i
);
196 void PermissionQueueController::OnPermissionSet(
197 const PermissionRequestID
& id
,
198 const GURL
& requesting_frame
,
199 const GURL
& embedder
,
200 bool update_content_setting
,
202 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
204 // TODO(miguelg): move the permission persistence to
205 // PermissionContextBase once all the types are moved there.
206 if (update_content_setting
) {
207 UpdateContentSetting(requesting_frame
, embedder
, allowed
);
209 PermissionContextUmaUtil::PermissionGranted(type_
, requesting_frame
);
211 PermissionContextUmaUtil::PermissionDenied(type_
, requesting_frame
);
213 PermissionContextUmaUtil::PermissionDismissed(type_
, requesting_frame
);
216 // Cancel this request first, then notify listeners. TODO(pkasting): Why
217 // is this order important?
218 PendingInfobarRequests requests_to_notify
;
219 PendingInfobarRequests infobars_to_remove
;
220 std::vector
<PendingInfobarRequests::iterator
> pending_requests_to_remove
;
221 for (PendingInfobarRequests::iterator i
= pending_infobar_requests_
.begin();
222 i
!= pending_infobar_requests_
.end(); ++i
) {
223 if (!i
->IsForPair(requesting_frame
, embedder
))
225 requests_to_notify
.push_back(*i
);
226 if (!i
->has_infobar()) {
227 // We haven't created an infobar yet, just record the pending request
228 // index and remove it later.
229 pending_requests_to_remove
.push_back(i
);
233 // The infobar that called us is i->infobar(), and its delegate is
234 // currently in either Accept() or Cancel(). This means that
235 // RemoveInfoBar() will be called later on, and that will trigger a
236 // notification we're observing.
240 // This infobar is for the same frame/embedder pair, but in a different
241 // tab. We should remove it now that we've got an answer for it.
242 infobars_to_remove
.push_back(*i
);
245 // Remove all infobars for the same |requesting_frame| and |embedder|.
246 for (PendingInfobarRequests::iterator i
= infobars_to_remove
.begin();
247 i
!= infobars_to_remove
.end(); ++i
)
248 GetInfoBarService(i
->id())->RemoveInfoBar(i
->infobar());
250 // PermissionContextBase needs to know about the new ContentSetting value,
251 // CONTENT_SETTING_DEFAULT being the value for nothing happened. The callers
252 // of ::OnPermissionSet passes { true, true } for allow, { true, false } for
253 // block and { false, * } for dismissed. The tuple being
254 // { update_content_setting, allowed }.
255 ContentSetting content_setting
= CONTENT_SETTING_DEFAULT
;
256 if (update_content_setting
) {
257 content_setting
= allowed
? CONTENT_SETTING_ALLOW
: CONTENT_SETTING_BLOCK
;
260 // Send out the permission notifications.
261 for (PendingInfobarRequests::iterator i
= requests_to_notify
.begin();
262 i
!= requests_to_notify
.end(); ++i
)
263 i
->RunCallback(content_setting
);
265 // Remove the pending requests in reverse order.
266 for (int i
= pending_requests_to_remove
.size() - 1; i
>= 0; --i
)
267 pending_infobar_requests_
.erase(pending_requests_to_remove
[i
]);
270 void PermissionQueueController::Observe(
272 const content::NotificationSource
& source
,
273 const content::NotificationDetails
& details
) {
274 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED
, type
);
275 // We will receive this notification for all infobar closures, so we need to
276 // check whether this is the geolocation infobar we're tracking. Note that the
277 // InfoBarContainer (if any) may have received this notification before us and
278 // caused the infobar to be deleted, so it's not safe to dereference the
279 // contents of the infobar. The address of the infobar, however, is OK to
280 // use to find the PendingInfobarRequest to remove because
281 // pending_infobar_requests_ will not have received any new entries between
282 // the NotificationService's call to InfoBarContainer::Observe and this
284 infobars::InfoBar
* infobar
=
285 content::Details
<infobars::InfoBar::RemovedDetails
>(details
)->first
;
286 for (PendingInfobarRequests::iterator i
= pending_infobar_requests_
.begin();
287 i
!= pending_infobar_requests_
.end(); ++i
) {
288 if (i
->infobar() == infobar
) {
289 PermissionRequestID
id(i
->id());
290 pending_infobar_requests_
.erase(i
);
291 ShowQueuedInfoBarForTab(id
);
297 bool PermissionQueueController::AlreadyShowingInfoBarForTab(
298 const PermissionRequestID
& id
) const {
299 for (PendingInfobarRequests::const_iterator
i(
300 pending_infobar_requests_
.begin());
301 i
!= pending_infobar_requests_
.end(); ++i
) {
302 if (ArePermissionRequestsForSameTab(i
->id(), id
) && i
->has_infobar())
308 void PermissionQueueController::ShowQueuedInfoBarForTab(
309 const PermissionRequestID
& id
) {
310 DCHECK(!AlreadyShowingInfoBarForTab(id
));
312 // We can get here for example during tab shutdown, when the InfoBarService is
313 // removing all existing infobars, thus calling back to Observe(). In this
314 // case the service still exists, and is supplied as the source of the
315 // notification we observed, but is no longer accessible from its WebContents.
316 // In this case we should just go ahead and cancel further infobars for this
317 // tab instead of trying to access the service.
319 // Similarly, if we're being destroyed, we should also avoid showing further
321 InfoBarService
* infobar_service
= GetInfoBarService(id
);
322 if (!infobar_service
|| in_shutdown_
) {
323 ClearPendingInfobarRequestsForTab(id
);
327 for (PendingInfobarRequests::iterator i
= pending_infobar_requests_
.begin();
328 i
!= pending_infobar_requests_
.end(); ++i
) {
329 if (ArePermissionRequestsForSameTab(i
->id(), id
) && !i
->has_infobar()) {
330 RegisterForInfoBarNotifications(infobar_service
);
332 this, profile_
->GetPrefs()->GetString(prefs::kAcceptLanguages
));
337 UnregisterForInfoBarNotifications(infobar_service
);
340 void PermissionQueueController::ClearPendingInfobarRequestsForTab(
341 const PermissionRequestID
& id
) {
342 for (PendingInfobarRequests::iterator i
= pending_infobar_requests_
.begin();
343 i
!= pending_infobar_requests_
.end(); ) {
344 if (ArePermissionRequestsForSameTab(i
->id(), id
)) {
345 DCHECK(!i
->has_infobar());
346 i
= pending_infobar_requests_
.erase(i
);
353 void PermissionQueueController::RegisterForInfoBarNotifications(
354 InfoBarService
* infobar_service
) {
355 if (!registrar_
.IsRegistered(
356 this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED
,
357 content::Source
<InfoBarService
>(infobar_service
))) {
359 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED
,
360 content::Source
<InfoBarService
>(infobar_service
));
364 void PermissionQueueController::UnregisterForInfoBarNotifications(
365 InfoBarService
* infobar_service
) {
366 if (registrar_
.IsRegistered(
367 this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED
,
368 content::Source
<InfoBarService
>(infobar_service
))) {
369 registrar_
.Remove(this,
370 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED
,
371 content::Source
<InfoBarService
>(infobar_service
));
375 void PermissionQueueController::UpdateContentSetting(
376 const GURL
& requesting_frame
,
377 const GURL
& embedder
,
379 if (requesting_frame
.GetOrigin().SchemeIsFile()) {
380 // Chrome can be launched with --disable-web-security which allows
381 // geolocation requests from file:// URLs. We don't want to store these
382 // in the host content settings map.
386 ContentSetting content_setting
=
387 allowed
? CONTENT_SETTING_ALLOW
: CONTENT_SETTING_BLOCK
;
389 ContentSettingsPattern embedder_pattern
=
390 (type_
== CONTENT_SETTINGS_TYPE_NOTIFICATIONS
) ?
391 ContentSettingsPattern::Wildcard() :
392 ContentSettingsPattern::FromURLNoWildcard(embedder
.GetOrigin());
394 profile_
->GetHostContentSettingsMap()->SetContentSetting(
395 ContentSettingsPattern::FromURLNoWildcard(requesting_frame
.GetOrigin()),