Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / content_settings / permission_queue_controller.cc
blob1ba389adaf60c7ef7216d63373040cdb21c3d2d8
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/content_settings/permission_queue_controller.h"
7 #include "base/prefs/pref_service.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/content_settings/host_content_settings_map.h"
10 #include "chrome/browser/geolocation/geolocation_infobar_delegate.h"
11 #include "chrome/browser/infobars/infobar.h"
12 #include "chrome/browser/infobars/infobar_service.h"
13 #include "chrome/browser/media/midi_permission_infobar_delegate.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/tab_contents/tab_util.h"
16 #include "chrome/common/content_settings.h"
17 #include "chrome/common/pref_names.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/notification_details.h"
20 #include "content/public/browser/notification_source.h"
21 #include "content/public/browser/notification_types.h"
22 #include "content/public/browser/web_contents.h"
24 #if defined(OS_ANDROID)
25 #include "chrome/browser/media/protected_media_identifier_infobar_delegate.h"
26 #endif
28 namespace {
30 InfoBarService* GetInfoBarService(const PermissionRequestID& id) {
31 content::WebContents* web_contents =
32 tab_util::GetWebContentsByID(id.render_process_id(), id.render_view_id());
33 return web_contents ? InfoBarService::FromWebContents(web_contents) : NULL;
39 class PermissionQueueController::PendingInfobarRequest {
40 public:
41 PendingInfobarRequest(ContentSettingsType type,
42 const PermissionRequestID& id,
43 const GURL& requesting_frame,
44 const GURL& embedder,
45 PermissionDecidedCallback callback);
46 ~PendingInfobarRequest();
48 bool IsForPair(const GURL& requesting_frame,
49 const GURL& embedder) const;
51 const PermissionRequestID& id() const { return id_; }
52 const GURL& requesting_frame() const { return requesting_frame_; }
53 bool has_infobar() const { return !!infobar_; }
54 InfoBar* infobar() { return infobar_; }
56 void RunCallback(bool allowed);
57 void CreateInfoBar(PermissionQueueController* controller,
58 const std::string& display_languages);
60 private:
61 ContentSettingsType type_;
62 PermissionRequestID id_;
63 GURL requesting_frame_;
64 GURL embedder_;
65 PermissionDecidedCallback callback_;
66 InfoBar* infobar_;
68 // Purposefully do not disable copying, as this is stored in STL containers.
71 PermissionQueueController::PendingInfobarRequest::PendingInfobarRequest(
72 ContentSettingsType type,
73 const PermissionRequestID& id,
74 const GURL& requesting_frame,
75 const GURL& embedder,
76 PermissionDecidedCallback callback)
77 : type_(type),
78 id_(id),
79 requesting_frame_(requesting_frame),
80 embedder_(embedder),
81 callback_(callback),
82 infobar_(NULL) {
85 PermissionQueueController::PendingInfobarRequest::~PendingInfobarRequest() {
88 bool PermissionQueueController::PendingInfobarRequest::IsForPair(
89 const GURL& requesting_frame,
90 const GURL& embedder) const {
91 return (requesting_frame_ == requesting_frame) && (embedder_ == embedder);
94 void PermissionQueueController::PendingInfobarRequest::RunCallback(
95 bool allowed) {
96 callback_.Run(allowed);
99 void PermissionQueueController::PendingInfobarRequest::CreateInfoBar(
100 PermissionQueueController* controller,
101 const std::string& display_languages) {
102 // TODO(toyoshim): Remove following ContentType dependent code.
103 // Also these InfoBarDelegate can share much more code each other.
104 // http://crbug.com/266743
105 switch (type_) {
106 case CONTENT_SETTINGS_TYPE_GEOLOCATION:
107 infobar_ = GeolocationInfoBarDelegate::Create(
108 GetInfoBarService(id_), controller, id_, requesting_frame_,
109 display_languages);
110 break;
111 case CONTENT_SETTINGS_TYPE_MIDI_SYSEX:
112 infobar_ = MIDIPermissionInfoBarDelegate::Create(
113 GetInfoBarService(id_), controller, id_, requesting_frame_,
114 display_languages);
115 break;
116 #if defined(OS_ANDROID)
117 case CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER:
118 infobar_ = ProtectedMediaIdentifierInfoBarDelegate::Create(
119 GetInfoBarService(id_), controller, id_, requesting_frame_,
120 display_languages);
121 break;
122 #endif
123 default:
124 NOTREACHED();
125 break;
130 PermissionQueueController::PermissionQueueController(Profile* profile,
131 ContentSettingsType type)
132 : profile_(profile),
133 type_(type),
134 in_shutdown_(false) {
137 PermissionQueueController::~PermissionQueueController() {
138 // Cancel all outstanding requests.
139 in_shutdown_ = true;
140 while (!pending_infobar_requests_.empty())
141 CancelInfoBarRequest(pending_infobar_requests_.front().id());
144 void PermissionQueueController::CreateInfoBarRequest(
145 const PermissionRequestID& id,
146 const GURL& requesting_frame,
147 const GURL& embedder,
148 PermissionDecidedCallback callback) {
149 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
151 // We shouldn't get duplicate requests.
152 for (PendingInfobarRequests::const_iterator i(
153 pending_infobar_requests_.begin());
154 i != pending_infobar_requests_.end(); ++i)
155 DCHECK(!i->id().Equals(id));
157 pending_infobar_requests_.push_back(PendingInfobarRequest(
158 type_, id, requesting_frame, embedder, callback));
159 if (!AlreadyShowingInfoBarForTab(id))
160 ShowQueuedInfoBarForTab(id);
163 void PermissionQueueController::CancelInfoBarRequest(
164 const PermissionRequestID& id) {
165 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
167 for (PendingInfobarRequests::iterator i(pending_infobar_requests_.begin());
168 i != pending_infobar_requests_.end(); ++i) {
169 if (i->id().Equals(id)) {
170 if (i->has_infobar())
171 GetInfoBarService(id)->RemoveInfoBar(i->infobar());
172 else
173 pending_infobar_requests_.erase(i);
174 return;
179 void PermissionQueueController::CancelInfoBarRequests(int group_id) {
180 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
182 // If we remove an infobar in the following loop, the next pending infobar
183 // will be shown. Therefore, we erase all the pending infobars first and
184 // remove an infobar later.
185 PendingInfobarRequests infobar_requests_to_cancel;
186 for (PendingInfobarRequests::iterator i = pending_infobar_requests_.begin();
187 i != pending_infobar_requests_.end();) {
188 if (i->id().group_id() == group_id) {
189 if (i->has_infobar()) {
190 // |i| will be erased from |pending_infobar_requests_|
191 // in |PermissionQueueController::Observe| when the infobar is removed.
192 infobar_requests_to_cancel.push_back(*i);
193 ++i;
194 } else {
195 i = pending_infobar_requests_.erase(i);
197 } else {
198 ++i;
202 for (PendingInfobarRequests::iterator i = infobar_requests_to_cancel.begin();
203 i != infobar_requests_to_cancel.end();
204 ++i) {
205 GetInfoBarService(i->id())->RemoveInfoBar(i->infobar());
209 void PermissionQueueController::OnPermissionSet(
210 const PermissionRequestID& id,
211 const GURL& requesting_frame,
212 const GURL& embedder,
213 bool update_content_setting,
214 bool allowed) {
215 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
217 if (update_content_setting)
218 UpdateContentSetting(requesting_frame, embedder, allowed);
220 // Cancel this request first, then notify listeners. TODO(pkasting): Why
221 // is this order important?
222 PendingInfobarRequests requests_to_notify;
223 PendingInfobarRequests infobars_to_remove;
224 for (PendingInfobarRequests::iterator i = pending_infobar_requests_.begin();
225 i != pending_infobar_requests_.end(); ) {
226 if (i->IsForPair(requesting_frame, embedder)) {
227 requests_to_notify.push_back(*i);
228 if (i->id().Equals(id)) {
229 // The infobar that called us is i->infobar(), and its delegate is
230 // currently in either Accept() or Cancel(). This means that
231 // RemoveInfoBar() will be called later on, and that will trigger a
232 // notification we're observing.
233 ++i;
234 } else if (i->has_infobar()) {
235 // This infobar is for the same frame/embedder pair, but in a different
236 // tab. We should remove it now that we've got an answer for it.
237 infobars_to_remove.push_back(*i);
238 ++i;
239 } else {
240 // We haven't created an infobar yet, just remove the pending request.
241 i = pending_infobar_requests_.erase(i);
243 } else {
244 ++i;
248 // Remove all infobars for the same |requesting_frame| and |embedder|.
249 for (PendingInfobarRequests::iterator i = infobars_to_remove.begin();
250 i != infobars_to_remove.end(); ++i)
251 GetInfoBarService(i->id())->RemoveInfoBar(i->infobar());
253 // Send out the permission notifications.
254 for (PendingInfobarRequests::iterator i = requests_to_notify.begin();
255 i != requests_to_notify.end(); ++i)
256 i->RunCallback(allowed);
259 void PermissionQueueController::Observe(
260 int type,
261 const content::NotificationSource& source,
262 const content::NotificationDetails& details) {
263 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type);
264 // We will receive this notification for all infobar closures, so we need to
265 // check whether this is the geolocation infobar we're tracking. Note that the
266 // InfoBarContainer (if any) may have received this notification before us and
267 // caused the infobar to be deleted, so it's not safe to dereference the
268 // contents of the infobar. The address of the infobar, however, is OK to
269 // use to find the PendingInfobarRequest to remove because
270 // pending_infobar_requests_ will not have received any new entries between
271 // the NotificationService's call to InfoBarContainer::Observe and this
272 // method.
273 InfoBar* infobar = content::Details<InfoBar::RemovedDetails>(details)->first;
274 for (PendingInfobarRequests::iterator i = pending_infobar_requests_.begin();
275 i != pending_infobar_requests_.end(); ++i) {
276 if (i->infobar() == infobar) {
277 PermissionRequestID id(i->id());
278 pending_infobar_requests_.erase(i);
279 ShowQueuedInfoBarForTab(id);
280 return;
285 bool PermissionQueueController::AlreadyShowingInfoBarForTab(
286 const PermissionRequestID& id) const {
287 for (PendingInfobarRequests::const_iterator i(
288 pending_infobar_requests_.begin());
289 i != pending_infobar_requests_.end(); ++i) {
290 if (i->id().IsForSameTabAs(id) && i->has_infobar())
291 return true;
293 return false;
296 void PermissionQueueController::ShowQueuedInfoBarForTab(
297 const PermissionRequestID& id) {
298 DCHECK(!AlreadyShowingInfoBarForTab(id));
300 // We can get here for example during tab shutdown, when the InfoBarService is
301 // removing all existing infobars, thus calling back to Observe(). In this
302 // case the service still exists, and is supplied as the source of the
303 // notification we observed, but is no longer accessible from its WebContents.
304 // In this case we should just go ahead and cancel further infobars for this
305 // tab instead of trying to access the service.
307 // Similarly, if we're being destroyed, we should also avoid showing further
308 // infobars.
309 InfoBarService* infobar_service = GetInfoBarService(id);
310 if (!infobar_service || in_shutdown_) {
311 ClearPendingInfobarRequestsForTab(id);
312 return;
315 for (PendingInfobarRequests::iterator i = pending_infobar_requests_.begin();
316 i != pending_infobar_requests_.end(); ++i) {
317 if (i->id().IsForSameTabAs(id) && !i->has_infobar()) {
318 RegisterForInfoBarNotifications(infobar_service);
319 i->CreateInfoBar(
320 this, profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
321 return;
325 UnregisterForInfoBarNotifications(infobar_service);
328 void PermissionQueueController::ClearPendingInfobarRequestsForTab(
329 const PermissionRequestID& id) {
330 for (PendingInfobarRequests::iterator i = pending_infobar_requests_.begin();
331 i != pending_infobar_requests_.end(); ) {
332 if (i->id().IsForSameTabAs(id)) {
333 DCHECK(!i->has_infobar());
334 i = pending_infobar_requests_.erase(i);
335 } else {
336 ++i;
341 void PermissionQueueController::RegisterForInfoBarNotifications(
342 InfoBarService* infobar_service) {
343 if (!registrar_.IsRegistered(
344 this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
345 content::Source<InfoBarService>(infobar_service))) {
346 registrar_.Add(this,
347 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
348 content::Source<InfoBarService>(infobar_service));
352 void PermissionQueueController::UnregisterForInfoBarNotifications(
353 InfoBarService* infobar_service) {
354 if (registrar_.IsRegistered(
355 this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
356 content::Source<InfoBarService>(infobar_service))) {
357 registrar_.Remove(this,
358 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
359 content::Source<InfoBarService>(infobar_service));
363 void PermissionQueueController::UpdateContentSetting(
364 const GURL& requesting_frame,
365 const GURL& embedder,
366 bool allowed) {
367 if (requesting_frame.GetOrigin().SchemeIsFile()) {
368 // Chrome can be launched with --disable-web-security which allows
369 // geolocation requests from file:// URLs. We don't want to store these
370 // in the host content settings map.
371 return;
374 ContentSetting content_setting =
375 allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK;
376 profile_->GetHostContentSettingsMap()->SetContentSetting(
377 ContentSettingsPattern::FromURLNoWildcard(requesting_frame.GetOrigin()),
378 ContentSettingsPattern::FromURLNoWildcard(embedder.GetOrigin()),
379 type_,
380 std::string(),
381 content_setting);