Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / content_settings / tab_specific_content_settings.cc
blob364c7d52692e87dda785b37131281c297b2e742b
1 // Copyright (c) 2012 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/tab_specific_content_settings.h"
7 #include <list>
9 #include "base/command_line.h"
10 #include "base/lazy_instance.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/browsing_data/browsing_data_appcache_helper.h"
16 #include "chrome/browser/browsing_data/browsing_data_cookie_helper.h"
17 #include "chrome/browser/browsing_data/browsing_data_database_helper.h"
18 #include "chrome/browser/browsing_data/browsing_data_file_system_helper.h"
19 #include "chrome/browser/browsing_data/browsing_data_indexed_db_helper.h"
20 #include "chrome/browser/browsing_data/browsing_data_local_storage_helper.h"
21 #include "chrome/browser/browsing_data/cookies_tree_model.h"
22 #include "chrome/browser/chrome_notification_types.h"
23 #include "chrome/browser/content_settings/chrome_content_settings_utils.h"
24 #include "chrome/browser/media/media_capture_devices_dispatcher.h"
25 #include "chrome/browser/media/media_stream_capture_indicator.h"
26 #include "chrome/browser/profiles/profile.h"
27 #include "chrome/common/chrome_switches.h"
28 #include "chrome/common/pref_names.h"
29 #include "chrome/common/render_messages.h"
30 #include "components/content_settings/core/browser/content_settings_details.h"
31 #include "components/content_settings/core/browser/content_settings_utils.h"
32 #include "components/content_settings/core/browser/host_content_settings_map.h"
33 #include "components/rappor/rappor_service.h"
34 #include "content/public/browser/browser_thread.h"
35 #include "content/public/browser/navigation_controller.h"
36 #include "content/public/browser/navigation_details.h"
37 #include "content/public/browser/navigation_entry.h"
38 #include "content/public/browser/notification_registrar.h"
39 #include "content/public/browser/notification_service.h"
40 #include "content/public/browser/render_frame_host.h"
41 #include "content/public/browser/render_view_host.h"
42 #include "content/public/browser/web_contents.h"
43 #include "content/public/browser/web_contents_delegate.h"
44 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
45 #include "net/cookies/canonical_cookie.h"
46 #include "storage/common/fileapi/file_system_types.h"
48 using content::BrowserThread;
49 using content::NavigationController;
50 using content::NavigationEntry;
51 using content::RenderViewHost;
52 using content::WebContents;
54 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TabSpecificContentSettings);
56 namespace {
58 // Returns the object given a render frame's id.
59 TabSpecificContentSettings* GetForFrame(int render_process_id,
60 int render_frame_id) {
61 DCHECK_CURRENTLY_ON(BrowserThread::UI);
63 content::RenderFrameHost* frame = content::RenderFrameHost::FromID(
64 render_process_id, render_frame_id);
65 WebContents* web_contents = WebContents::FromRenderFrameHost(frame);
66 if (!web_contents)
67 return nullptr;
69 return TabSpecificContentSettings::FromWebContents(web_contents);
72 ContentSettingsUsagesState::CommittedDetails GetCommittedDetails(
73 const content::LoadCommittedDetails& details) {
74 ContentSettingsUsagesState::CommittedDetails committed_details;
75 committed_details.current_url_valid = !!details.entry;
76 if (details.entry)
77 committed_details.current_url = details.entry->GetURL();
78 committed_details.previous_url = details.previous_url;
79 return committed_details;
82 } // namespace
84 TabSpecificContentSettings::SiteDataObserver::SiteDataObserver(
85 TabSpecificContentSettings* tab_specific_content_settings)
86 : tab_specific_content_settings_(tab_specific_content_settings) {
87 tab_specific_content_settings_->AddSiteDataObserver(this);
90 TabSpecificContentSettings::SiteDataObserver::~SiteDataObserver() {
91 if (tab_specific_content_settings_)
92 tab_specific_content_settings_->RemoveSiteDataObserver(this);
95 void TabSpecificContentSettings::SiteDataObserver::ContentSettingsDestroyed() {
96 tab_specific_content_settings_ = NULL;
99 TabSpecificContentSettings::TabSpecificContentSettings(WebContents* tab)
100 : content::WebContentsObserver(tab),
101 allowed_local_shared_objects_(
102 Profile::FromBrowserContext(tab->GetBrowserContext())),
103 blocked_local_shared_objects_(
104 Profile::FromBrowserContext(tab->GetBrowserContext())),
105 geolocation_usages_state_(
106 Profile::FromBrowserContext(tab->GetBrowserContext())
107 ->GetHostContentSettingsMap(),
108 CONTENT_SETTINGS_TYPE_GEOLOCATION,
109 prefs::kAcceptLanguages,
110 Profile::FromBrowserContext(tab->GetBrowserContext())->GetPrefs()),
111 midi_usages_state_(
112 Profile::FromBrowserContext(tab->GetBrowserContext())
113 ->GetHostContentSettingsMap(),
114 CONTENT_SETTINGS_TYPE_MIDI_SYSEX,
115 prefs::kAcceptLanguages,
116 Profile::FromBrowserContext(tab->GetBrowserContext())->GetPrefs()),
117 pending_protocol_handler_(ProtocolHandler::EmptyProtocolHandler()),
118 previous_protocol_handler_(ProtocolHandler::EmptyProtocolHandler()),
119 pending_protocol_handler_setting_(CONTENT_SETTING_DEFAULT),
120 load_plugins_link_enabled_(true),
121 microphone_camera_state_(MICROPHONE_CAMERA_NOT_ACCESSED),
122 observer_(this) {
123 ClearBlockedContentSettingsExceptForCookies();
124 ClearCookieSpecificContentSettings();
126 observer_.Add(Profile::FromBrowserContext(tab->GetBrowserContext())
127 ->GetHostContentSettingsMap());
130 TabSpecificContentSettings::~TabSpecificContentSettings() {
131 FOR_EACH_OBSERVER(
132 SiteDataObserver, observer_list_, ContentSettingsDestroyed());
135 // static
136 TabSpecificContentSettings* TabSpecificContentSettings::Get(
137 int render_process_id, int render_view_id) {
138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
140 RenderViewHost* view = RenderViewHost::FromID(render_process_id,
141 render_view_id);
142 if (!view)
143 return NULL;
145 WebContents* web_contents = WebContents::FromRenderViewHost(view);
146 if (!web_contents)
147 return NULL;
149 return TabSpecificContentSettings::FromWebContents(web_contents);
152 // static
153 void TabSpecificContentSettings::CookiesRead(int render_process_id,
154 int render_frame_id,
155 const GURL& url,
156 const GURL& frame_url,
157 const net::CookieList& cookie_list,
158 bool blocked_by_policy) {
159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
160 TabSpecificContentSettings* settings =
161 GetForFrame(render_process_id, render_frame_id);
162 if (settings) {
163 settings->OnCookiesRead(url, frame_url, cookie_list,
164 blocked_by_policy);
168 // static
169 void TabSpecificContentSettings::CookieChanged(
170 int render_process_id,
171 int render_frame_id,
172 const GURL& url,
173 const GURL& frame_url,
174 const std::string& cookie_line,
175 const net::CookieOptions& options,
176 bool blocked_by_policy) {
177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
178 TabSpecificContentSettings* settings =
179 GetForFrame(render_process_id, render_frame_id);
180 if (settings)
181 settings->OnCookieChanged(url, frame_url, cookie_line, options,
182 blocked_by_policy);
185 // static
186 void TabSpecificContentSettings::WebDatabaseAccessed(
187 int render_process_id,
188 int render_frame_id,
189 const GURL& url,
190 const base::string16& name,
191 const base::string16& display_name,
192 bool blocked_by_policy) {
193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
194 TabSpecificContentSettings* settings = GetForFrame(
195 render_process_id, render_frame_id);
196 if (settings)
197 settings->OnWebDatabaseAccessed(url, name, display_name, blocked_by_policy);
200 // static
201 void TabSpecificContentSettings::DOMStorageAccessed(int render_process_id,
202 int render_frame_id,
203 const GURL& url,
204 bool local,
205 bool blocked_by_policy) {
206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
207 TabSpecificContentSettings* settings = GetForFrame(
208 render_process_id, render_frame_id);
209 if (settings)
210 settings->OnLocalStorageAccessed(url, local, blocked_by_policy);
213 // static
214 void TabSpecificContentSettings::IndexedDBAccessed(
215 int render_process_id,
216 int render_frame_id,
217 const GURL& url,
218 const base::string16& description,
219 bool blocked_by_policy) {
220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
221 TabSpecificContentSettings* settings = GetForFrame(
222 render_process_id, render_frame_id);
223 if (settings)
224 settings->OnIndexedDBAccessed(url, description, blocked_by_policy);
227 // static
228 void TabSpecificContentSettings::FileSystemAccessed(int render_process_id,
229 int render_frame_id,
230 const GURL& url,
231 bool blocked_by_policy) {
232 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
233 TabSpecificContentSettings* settings = GetForFrame(
234 render_process_id, render_frame_id);
235 if (settings)
236 settings->OnFileSystemAccessed(url, blocked_by_policy);
239 bool TabSpecificContentSettings::IsContentBlocked(
240 ContentSettingsType content_type) const {
241 DCHECK(content_type != CONTENT_SETTINGS_TYPE_GEOLOCATION)
242 << "Geolocation settings handled by ContentSettingGeolocationImageModel";
243 DCHECK(content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS)
244 << "Notifications settings handled by "
245 << "ContentSettingsNotificationsImageModel";
247 if (content_type == CONTENT_SETTINGS_TYPE_IMAGES ||
248 content_type == CONTENT_SETTINGS_TYPE_JAVASCRIPT ||
249 content_type == CONTENT_SETTINGS_TYPE_PLUGINS ||
250 content_type == CONTENT_SETTINGS_TYPE_COOKIES ||
251 content_type == CONTENT_SETTINGS_TYPE_POPUPS ||
252 content_type == CONTENT_SETTINGS_TYPE_MIXEDSCRIPT ||
253 content_type == CONTENT_SETTINGS_TYPE_MEDIASTREAM ||
254 content_type == CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC ||
255 content_type == CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA ||
256 content_type == CONTENT_SETTINGS_TYPE_PPAPI_BROKER ||
257 content_type == CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS ||
258 content_type == CONTENT_SETTINGS_TYPE_MIDI_SYSEX) {
259 return content_blocked_[content_type];
262 return false;
265 bool TabSpecificContentSettings::IsBlockageIndicated(
266 ContentSettingsType content_type) const {
267 return content_blockage_indicated_to_user_[content_type];
270 void TabSpecificContentSettings::SetBlockageHasBeenIndicated(
271 ContentSettingsType content_type) {
272 content_blockage_indicated_to_user_[content_type] = true;
275 bool TabSpecificContentSettings::IsContentAllowed(
276 ContentSettingsType content_type) const {
277 // This method currently only returns meaningful values for the content type
278 // cookies, mediastream, PPAPI broker, and downloads.
279 if (content_type != CONTENT_SETTINGS_TYPE_COOKIES &&
280 content_type != CONTENT_SETTINGS_TYPE_MEDIASTREAM &&
281 content_type != CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC &&
282 content_type != CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA &&
283 content_type != CONTENT_SETTINGS_TYPE_PPAPI_BROKER &&
284 content_type != CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS &&
285 content_type != CONTENT_SETTINGS_TYPE_MIDI_SYSEX) {
286 return false;
289 return content_allowed_[content_type];
292 void TabSpecificContentSettings::OnContentBlocked(ContentSettingsType type) {
293 OnContentBlockedWithDetail(type, base::string16());
296 void TabSpecificContentSettings::OnContentBlockedWithDetail(
297 ContentSettingsType type,
298 const base::string16& details) {
299 DCHECK(type != CONTENT_SETTINGS_TYPE_GEOLOCATION)
300 << "Geolocation settings handled by OnGeolocationPermissionSet";
301 DCHECK(type != CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC &&
302 type != CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA)
303 << "Media stream settings handled by OnMediaStreamPermissionSet";
304 if (type < 0 || type >= CONTENT_SETTINGS_NUM_TYPES)
305 return;
307 // TODO(robwu): Should this be restricted to cookies only?
308 // In the past, content_allowed_ was set to false, but this logic was inverted
309 // in https://codereview.chromium.org/13375004 to fix an issue with the cookie
310 // permission UI. This unconditional assignment seems incorrect, because the
311 // flag will now always be true after calling either OnContentBlocked or
312 // OnContentAllowed. Consequently IsContentAllowed will always return true
313 // for every supported setting that is not handled elsewhere.
314 content_allowed_[type] = true;
316 #if defined(OS_ANDROID)
317 if (type == CONTENT_SETTINGS_TYPE_POPUPS) {
318 // For Android we do not have a persistent button that will always be
319 // visible for blocked popups. Instead we have info bars which could be
320 // dismissed. Have to clear the blocked state so we properly notify the
321 // relevant pieces again.
322 content_blocked_[type] = false;
323 content_blockage_indicated_to_user_[type] = false;
325 #endif
327 if (type == CONTENT_SETTINGS_TYPE_PLUGINS && !details.empty() &&
328 std::find(blocked_plugin_names_.begin(), blocked_plugin_names_.end(),
329 details) == blocked_plugin_names_.end()) {
330 blocked_plugin_names_.push_back(details);
333 if (!content_blocked_[type]) {
334 content_blocked_[type] = true;
335 // TODO: it would be nice to have a way of mocking this in tests.
336 content::NotificationService::current()->Notify(
337 chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
338 content::Source<WebContents>(web_contents()),
339 content::NotificationService::NoDetails());
341 if (type == CONTENT_SETTINGS_TYPE_MIXEDSCRIPT) {
342 content_settings::RecordMixedScriptAction(
343 content_settings::MIXED_SCRIPT_ACTION_DISPLAYED_SHIELD);
345 rappor::RapporService* rappor_service =
346 g_browser_process->rappor_service();
347 if (rappor_service) {
348 rappor_service->RecordSample(
349 "ContentSettings.MixedScript.DisplayedShield",
350 rappor::ETLD_PLUS_ONE_RAPPOR_TYPE,
351 net::registry_controlled_domains::GetDomainAndRegistry(
352 base::UTF16ToUTF8(details),
353 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES));
359 void TabSpecificContentSettings::OnContentAllowed(ContentSettingsType type) {
360 DCHECK(type != CONTENT_SETTINGS_TYPE_GEOLOCATION)
361 << "Geolocation settings handled by OnGeolocationPermissionSet";
362 DCHECK(type != CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC &&
363 type != CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA)
364 << "Media stream settings handled by OnMediaStreamPermissionSet";
365 bool access_changed = false;
366 #if defined(OS_ANDROID)
367 if (type == CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER &&
368 content_blocked_[type]) {
369 // content_allowed_[type] is always set to true in OnContentBlocked, so we
370 // have to use content_blocked_ to detect whether the protected media
371 // setting has changed.
372 content_blocked_[type] = false;
373 access_changed = true;
375 #endif
377 if (!content_allowed_[type]) {
378 content_allowed_[type] = true;
379 access_changed = true;
382 if (access_changed) {
383 content::NotificationService::current()->Notify(
384 chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
385 content::Source<WebContents>(web_contents()),
386 content::NotificationService::NoDetails());
390 void TabSpecificContentSettings::OnCookiesRead(
391 const GURL& url,
392 const GURL& frame_url,
393 const net::CookieList& cookie_list,
394 bool blocked_by_policy) {
395 if (cookie_list.empty())
396 return;
397 if (blocked_by_policy) {
398 blocked_local_shared_objects_.cookies()->AddReadCookies(
399 frame_url, url, cookie_list);
400 OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES);
401 } else {
402 allowed_local_shared_objects_.cookies()->AddReadCookies(
403 frame_url, url, cookie_list);
404 OnContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES);
407 NotifySiteDataObservers();
410 void TabSpecificContentSettings::OnCookieChanged(
411 const GURL& url,
412 const GURL& frame_url,
413 const std::string& cookie_line,
414 const net::CookieOptions& options,
415 bool blocked_by_policy) {
416 if (blocked_by_policy) {
417 blocked_local_shared_objects_.cookies()->AddChangedCookie(
418 frame_url, url, cookie_line, options);
419 OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES);
420 } else {
421 allowed_local_shared_objects_.cookies()->AddChangedCookie(
422 frame_url, url, cookie_line, options);
423 OnContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES);
426 NotifySiteDataObservers();
429 void TabSpecificContentSettings::OnIndexedDBAccessed(
430 const GURL& url,
431 const base::string16& description,
432 bool blocked_by_policy) {
433 if (blocked_by_policy) {
434 blocked_local_shared_objects_.indexed_dbs()->AddIndexedDB(
435 url, description);
436 OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES);
437 } else {
438 allowed_local_shared_objects_.indexed_dbs()->AddIndexedDB(
439 url, description);
440 OnContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES);
443 NotifySiteDataObservers();
446 void TabSpecificContentSettings::OnLocalStorageAccessed(
447 const GURL& url,
448 bool local,
449 bool blocked_by_policy) {
450 LocalSharedObjectsContainer& container = blocked_by_policy ?
451 blocked_local_shared_objects_ : allowed_local_shared_objects_;
452 CannedBrowsingDataLocalStorageHelper* helper =
453 local ? container.local_storages() : container.session_storages();
454 helper->AddLocalStorage(url);
456 if (blocked_by_policy)
457 OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES);
458 else
459 OnContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES);
461 NotifySiteDataObservers();
464 void TabSpecificContentSettings::OnWebDatabaseAccessed(
465 const GURL& url,
466 const base::string16& name,
467 const base::string16& display_name,
468 bool blocked_by_policy) {
469 if (blocked_by_policy) {
470 blocked_local_shared_objects_.databases()->AddDatabase(
471 url, base::UTF16ToUTF8(name), base::UTF16ToUTF8(display_name));
472 OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES);
473 } else {
474 allowed_local_shared_objects_.databases()->AddDatabase(
475 url, base::UTF16ToUTF8(name), base::UTF16ToUTF8(display_name));
476 OnContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES);
479 NotifySiteDataObservers();
482 void TabSpecificContentSettings::OnFileSystemAccessed(
483 const GURL& url,
484 bool blocked_by_policy) {
485 if (blocked_by_policy) {
486 blocked_local_shared_objects_.file_systems()->AddFileSystem(
487 url, storage::kFileSystemTypeTemporary, 0);
488 OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES);
489 } else {
490 allowed_local_shared_objects_.file_systems()->AddFileSystem(
491 url, storage::kFileSystemTypeTemporary, 0);
492 OnContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES);
495 NotifySiteDataObservers();
498 void TabSpecificContentSettings::OnGeolocationPermissionSet(
499 const GURL& requesting_origin,
500 bool allowed) {
501 geolocation_usages_state_.OnPermissionSet(requesting_origin, allowed);
502 content::NotificationService::current()->Notify(
503 chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
504 content::Source<WebContents>(web_contents()),
505 content::NotificationService::NoDetails());
508 #if defined(OS_ANDROID) || defined(OS_CHROMEOS)
509 void TabSpecificContentSettings::OnProtectedMediaIdentifierPermissionSet(
510 const GURL& requesting_origin,
511 bool allowed) {
512 if (allowed) {
513 OnContentAllowed(CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER);
514 } else {
515 OnContentBlocked(CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER);
518 #endif
520 TabSpecificContentSettings::MicrophoneCameraState
521 TabSpecificContentSettings::GetMicrophoneCameraState() const {
522 MicrophoneCameraState state = microphone_camera_state_;
524 // Include capture devices in the state if there are still consumers of the
525 // approved media stream.
526 scoped_refptr<MediaStreamCaptureIndicator> media_indicator =
527 MediaCaptureDevicesDispatcher::GetInstance()->
528 GetMediaStreamCaptureIndicator();
529 if (media_indicator->IsCapturingAudio(web_contents()))
530 state |= MICROPHONE_ACCESSED;
531 if (media_indicator->IsCapturingVideo(web_contents()))
532 state |= CAMERA_ACCESSED;
534 return state;
537 bool TabSpecificContentSettings::IsMicrophoneCameraStateChanged() const {
538 if ((microphone_camera_state_ & MICROPHONE_ACCESSED) &&
539 ((microphone_camera_state_& MICROPHONE_BLOCKED) ?
540 !IsContentBlocked(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC) :
541 !IsContentAllowed(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC)))
542 return true;
544 if ((microphone_camera_state_ & CAMERA_ACCESSED) &&
545 ((microphone_camera_state_ & CAMERA_BLOCKED) ?
546 !IsContentBlocked(CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA) :
547 !IsContentAllowed(CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA)))
548 return true;
550 PrefService* prefs =
551 Profile::FromBrowserContext(web_contents()->GetBrowserContext())->
552 GetPrefs();
553 scoped_refptr<MediaStreamCaptureIndicator> media_indicator =
554 MediaCaptureDevicesDispatcher::GetInstance()->
555 GetMediaStreamCaptureIndicator();
557 if ((microphone_camera_state_ & MICROPHONE_ACCESSED) &&
558 prefs->GetString(prefs::kDefaultAudioCaptureDevice) !=
559 media_stream_selected_audio_device() &&
560 media_indicator->IsCapturingAudio(web_contents()))
561 return true;
563 if ((microphone_camera_state_ & CAMERA_ACCESSED) &&
564 prefs->GetString(prefs::kDefaultVideoCaptureDevice) !=
565 media_stream_selected_video_device() &&
566 media_indicator->IsCapturingVideo(web_contents()))
567 return true;
569 return false;
572 void TabSpecificContentSettings::OnMediaStreamPermissionSet(
573 const GURL& request_origin,
574 MicrophoneCameraState new_microphone_camera_state,
575 const std::string& media_stream_selected_audio_device,
576 const std::string& media_stream_selected_video_device,
577 const std::string& media_stream_requested_audio_device,
578 const std::string& media_stream_requested_video_device) {
579 media_stream_access_origin_ = request_origin;
581 if (new_microphone_camera_state & MICROPHONE_ACCESSED) {
582 media_stream_requested_audio_device_ = media_stream_requested_audio_device;
583 media_stream_selected_audio_device_ = media_stream_selected_audio_device;
584 bool mic_blocked = (new_microphone_camera_state & MICROPHONE_BLOCKED) != 0;
585 content_allowed_[CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC] = !mic_blocked;
586 content_blocked_[CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC] = mic_blocked;
589 if (new_microphone_camera_state & CAMERA_ACCESSED) {
590 media_stream_requested_video_device_ = media_stream_requested_video_device;
591 media_stream_selected_video_device_ = media_stream_selected_video_device;
592 bool cam_blocked = (new_microphone_camera_state & CAMERA_BLOCKED) != 0;
593 content_allowed_[CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA] = !cam_blocked;
594 content_blocked_[CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA] = cam_blocked;
597 if (microphone_camera_state_ != new_microphone_camera_state) {
598 microphone_camera_state_ = new_microphone_camera_state;
599 content::NotificationService::current()->Notify(
600 chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
601 content::Source<WebContents>(web_contents()),
602 content::NotificationService::NoDetails());
606 void TabSpecificContentSettings::OnMidiSysExAccessed(
607 const GURL& requesting_origin) {
608 midi_usages_state_.OnPermissionSet(requesting_origin, true);
609 OnContentAllowed(CONTENT_SETTINGS_TYPE_MIDI_SYSEX);
612 void TabSpecificContentSettings::OnMidiSysExAccessBlocked(
613 const GURL& requesting_origin) {
614 midi_usages_state_.OnPermissionSet(requesting_origin, false);
615 OnContentBlocked(CONTENT_SETTINGS_TYPE_MIDI_SYSEX);
618 void TabSpecificContentSettings::ClearBlockedContentSettingsExceptForCookies() {
619 for (size_t i = 0; i < arraysize(content_blocked_); ++i) {
620 if (i == CONTENT_SETTINGS_TYPE_COOKIES)
621 continue;
622 content_blocked_[i] = false;
623 content_allowed_[i] = false;
624 content_blockage_indicated_to_user_[i] = false;
626 microphone_camera_state_ = MICROPHONE_CAMERA_NOT_ACCESSED;
627 load_plugins_link_enabled_ = true;
628 content::NotificationService::current()->Notify(
629 chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
630 content::Source<WebContents>(web_contents()),
631 content::NotificationService::NoDetails());
634 void TabSpecificContentSettings::ClearCookieSpecificContentSettings() {
635 blocked_local_shared_objects_.Reset();
636 allowed_local_shared_objects_.Reset();
637 content_blocked_[CONTENT_SETTINGS_TYPE_COOKIES] = false;
638 content_allowed_[CONTENT_SETTINGS_TYPE_COOKIES] = false;
639 content_blockage_indicated_to_user_[CONTENT_SETTINGS_TYPE_COOKIES] = false;
640 content::NotificationService::current()->Notify(
641 chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
642 content::Source<WebContents>(web_contents()),
643 content::NotificationService::NoDetails());
646 void TabSpecificContentSettings::SetDownloadsBlocked(bool blocked) {
647 content_blocked_[CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS] = blocked;
648 content_allowed_[CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS] = !blocked;
649 content_blockage_indicated_to_user_[
650 CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS] = false;
651 content::NotificationService::current()->Notify(
652 chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
653 content::Source<WebContents>(web_contents()),
654 content::NotificationService::NoDetails());
657 void TabSpecificContentSettings::SetPopupsBlocked(bool blocked) {
658 content_blocked_[CONTENT_SETTINGS_TYPE_POPUPS] = blocked;
659 content_blockage_indicated_to_user_[CONTENT_SETTINGS_TYPE_POPUPS] = false;
660 content::NotificationService::current()->Notify(
661 chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
662 content::Source<WebContents>(web_contents()),
663 content::NotificationService::NoDetails());
666 void TabSpecificContentSettings::SetPepperBrokerAllowed(bool allowed) {
667 if (allowed) {
668 OnContentAllowed(CONTENT_SETTINGS_TYPE_PPAPI_BROKER);
669 } else {
670 OnContentBlocked(CONTENT_SETTINGS_TYPE_PPAPI_BROKER);
674 void TabSpecificContentSettings::OnContentSettingChanged(
675 const ContentSettingsPattern& primary_pattern,
676 const ContentSettingsPattern& secondary_pattern,
677 ContentSettingsType content_type,
678 std::string resource_identifier) {
679 const ContentSettingsDetails details(
680 primary_pattern, secondary_pattern, content_type, resource_identifier);
681 const NavigationController& controller = web_contents()->GetController();
682 NavigationEntry* entry = controller.GetVisibleEntry();
683 GURL entry_url;
684 if (entry)
685 entry_url = entry->GetURL();
686 if (details.update_all() ||
687 // The visible NavigationEntry is the URL in the URL field of a tab.
688 // Currently this should be matched by the |primary_pattern|.
689 details.primary_pattern().Matches(entry_url)) {
690 Profile* profile =
691 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
692 const HostContentSettingsMap* map = profile->GetHostContentSettingsMap();
694 if (content_type == CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC ||
695 content_type == CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA) {
696 const GURL media_origin = media_stream_access_origin();
697 ContentSetting setting = map->GetContentSetting(media_origin,
698 media_origin,
699 content_type,
700 std::string());
701 content_allowed_[content_type] = setting == CONTENT_SETTING_ALLOW;
702 content_blocked_[content_type] = setting == CONTENT_SETTING_BLOCK;
704 RendererContentSettingRules rules;
705 GetRendererContentSettingRules(map, &rules);
706 Send(new ChromeViewMsg_SetContentSettingRules(rules));
710 void TabSpecificContentSettings::RenderFrameForInterstitialPageCreated(
711 content::RenderFrameHost* render_frame_host) {
712 // We want to tell the renderer-side code to ignore content settings for this
713 // page.
714 render_frame_host->Send(new ChromeViewMsg_SetAsInterstitial(
715 render_frame_host->GetRoutingID()));
718 bool TabSpecificContentSettings::OnMessageReceived(
719 const IPC::Message& message,
720 content::RenderFrameHost* render_frame_host) {
721 bool handled = true;
722 IPC_BEGIN_MESSAGE_MAP(TabSpecificContentSettings, message)
723 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ContentBlocked,
724 OnContentBlockedWithDetail)
725 IPC_MESSAGE_UNHANDLED(handled = false)
726 IPC_END_MESSAGE_MAP()
727 return handled;
730 void TabSpecificContentSettings::DidNavigateMainFrame(
731 const content::LoadCommittedDetails& details,
732 const content::FrameNavigateParams& params) {
733 if (!details.is_in_page) {
734 // Clear "blocked" flags.
735 ClearBlockedContentSettingsExceptForCookies();
736 blocked_plugin_names_.clear();
737 GeolocationDidNavigate(details);
738 MidiDidNavigate(details);
742 void TabSpecificContentSettings::DidStartProvisionalLoadForFrame(
743 content::RenderFrameHost* render_frame_host,
744 const GURL& validated_url,
745 bool is_error_page,
746 bool is_iframe_srcdoc) {
747 if (render_frame_host->GetParent())
748 return;
750 // If we're displaying a network error page do not reset the content
751 // settings delegate's cookies so the user has a chance to modify cookie
752 // settings.
753 if (!is_error_page)
754 ClearCookieSpecificContentSettings();
755 ClearGeolocationContentSettings();
756 ClearMidiContentSettings();
757 ClearPendingProtocolHandler();
760 void TabSpecificContentSettings::AppCacheAccessed(const GURL& manifest_url,
761 bool blocked_by_policy) {
762 if (blocked_by_policy) {
763 blocked_local_shared_objects_.appcaches()->AddAppCache(manifest_url);
764 OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES);
765 } else {
766 allowed_local_shared_objects_.appcaches()->AddAppCache(manifest_url);
767 OnContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES);
771 void TabSpecificContentSettings::AddSiteDataObserver(
772 SiteDataObserver* observer) {
773 observer_list_.AddObserver(observer);
776 void TabSpecificContentSettings::RemoveSiteDataObserver(
777 SiteDataObserver* observer) {
778 observer_list_.RemoveObserver(observer);
781 void TabSpecificContentSettings::NotifySiteDataObservers() {
782 FOR_EACH_OBSERVER(SiteDataObserver, observer_list_, OnSiteDataAccessed());
785 void TabSpecificContentSettings::ClearGeolocationContentSettings() {
786 geolocation_usages_state_.ClearStateMap();
789 void TabSpecificContentSettings::ClearMidiContentSettings() {
790 midi_usages_state_.ClearStateMap();
793 void TabSpecificContentSettings::GeolocationDidNavigate(
794 const content::LoadCommittedDetails& details) {
795 geolocation_usages_state_.DidNavigate(GetCommittedDetails(details));
798 void TabSpecificContentSettings::MidiDidNavigate(
799 const content::LoadCommittedDetails& details) {
800 midi_usages_state_.DidNavigate(GetCommittedDetails(details));