tab_specific_content_settings.cc revision 201ade2fbba22bfb27ae029f4d23fca6ded109a0
1// Copyright (c) 2010 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/tab_contents/tab_specific_content_settings.h" 6 7#include "base/utf_string_conversions.h" 8#include "chrome/browser/browsing_data_appcache_helper.h" 9#include "chrome/browser/browsing_data_database_helper.h" 10#include "chrome/browser/browsing_data_indexed_db_helper.h" 11#include "chrome/browser/browsing_data_local_storage_helper.h" 12#include "chrome/browser/cookies_tree_model.h" 13#include "net/base/cookie_monster.h" 14 15bool TabSpecificContentSettings::LocalSharedObjectsContainer::empty() const { 16 return cookies_->GetAllCookies().empty() && 17 appcaches_->empty() && 18 databases_->empty() && 19 indexed_dbs_->empty() && 20 local_storages_->empty() && 21 session_storages_->empty(); 22} 23 24bool TabSpecificContentSettings::IsContentBlocked( 25 ContentSettingsType content_type) const { 26 DCHECK(content_type != CONTENT_SETTINGS_TYPE_GEOLOCATION) 27 << "Geolocation settings handled by ContentSettingGeolocationImageModel"; 28 DCHECK(content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS) 29 << "Notifications settings handled by " 30 << "ContentSettingsNotificationsImageModel"; 31 32 if (content_type == CONTENT_SETTINGS_TYPE_IMAGES || 33 content_type == CONTENT_SETTINGS_TYPE_JAVASCRIPT || 34 content_type == CONTENT_SETTINGS_TYPE_PLUGINS || 35 content_type == CONTENT_SETTINGS_TYPE_COOKIES || 36 content_type == CONTENT_SETTINGS_TYPE_POPUPS) 37 return content_blocked_[content_type]; 38 39 NOTREACHED(); 40 return false; 41} 42 43bool TabSpecificContentSettings::IsBlockageIndicated( 44 ContentSettingsType content_type) const { 45 return content_blockage_indicated_to_user_[content_type]; 46} 47 48void TabSpecificContentSettings::SetBlockageHasBeenIndicated( 49 ContentSettingsType content_type) { 50 content_blockage_indicated_to_user_[content_type] = true; 51} 52 53bool TabSpecificContentSettings::IsContentAccessed( 54 ContentSettingsType content_type) const { 55 // This method currently only returns meaningful values for cookies. 56 if (content_type != CONTENT_SETTINGS_TYPE_COOKIES) 57 return false; 58 59 return content_accessed_[content_type]; 60} 61 62const std::set<std::string>& 63 TabSpecificContentSettings::BlockedResourcesForType( 64 ContentSettingsType content_type) const { 65 if (blocked_resources_[content_type].get()) { 66 return *blocked_resources_[content_type]; 67 } else { 68 static std::set<std::string> empty_set; 69 return empty_set; 70 } 71} 72 73void TabSpecificContentSettings::AddBlockedResource( 74 ContentSettingsType content_type, 75 const std::string& resource_identifier) { 76 if (!blocked_resources_[content_type].get()) 77 blocked_resources_[content_type].reset(new std::set<std::string>()); 78 blocked_resources_[content_type]->insert(resource_identifier); 79} 80 81void TabSpecificContentSettings::OnContentBlocked( 82 ContentSettingsType type, 83 const std::string& resource_identifier) { 84 DCHECK(type != CONTENT_SETTINGS_TYPE_GEOLOCATION) 85 << "Geolocation settings handled by OnGeolocationPermissionSet"; 86 content_accessed_[type] = true; 87 if (!resource_identifier.empty()) 88 AddBlockedResource(type, resource_identifier); 89 if (!content_blocked_[type]) { 90 content_blocked_[type] = true; 91 if (delegate_) 92 delegate_->OnContentSettingsAccessed(true); 93 } 94} 95 96void TabSpecificContentSettings::OnContentAccessed(ContentSettingsType type) { 97 DCHECK(type != CONTENT_SETTINGS_TYPE_GEOLOCATION) 98 << "Geolocation settings handled by OnGeolocationPermissionSet"; 99 if (!content_accessed_[type]) { 100 content_accessed_[type] = true; 101 if (delegate_) 102 delegate_->OnContentSettingsAccessed(false); 103 } 104} 105 106void TabSpecificContentSettings::OnCookieAccessed( 107 const GURL& url, 108 const std::string& cookie_line, 109 const net::CookieOptions& options, 110 bool blocked_by_policy) { 111 if (blocked_by_policy) { 112 blocked_local_shared_objects_.cookies()->SetCookieWithOptions( 113 url, cookie_line, options); 114 OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES, std::string()); 115 } else { 116 allowed_local_shared_objects_.cookies()->SetCookieWithOptions( 117 url, cookie_line, options); 118 OnContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES); 119 } 120} 121 122void TabSpecificContentSettings::OnIndexedDBAccessed( 123 const GURL& url, 124 const string16& description, 125 bool blocked_by_policy) { 126 if (blocked_by_policy) { 127 blocked_local_shared_objects_.indexed_dbs()->AddIndexedDB( 128 url, description); 129 OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES, std::string()); 130 }else { 131 allowed_local_shared_objects_.indexed_dbs()->AddIndexedDB( 132 url, description); 133 OnContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES); 134 } 135} 136 137void TabSpecificContentSettings::OnLocalStorageAccessed( 138 const GURL& url, 139 DOMStorageType storage_type, 140 bool blocked_by_policy) { 141 LocalSharedObjectsContainer& container = blocked_by_policy ? 142 blocked_local_shared_objects_ : allowed_local_shared_objects_; 143 CannedBrowsingDataLocalStorageHelper* helper = 144 storage_type == DOM_STORAGE_LOCAL ? 145 container.local_storages() : container.session_storages(); 146 helper->AddLocalStorage(url); 147 148 if (blocked_by_policy) 149 OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES, std::string()); 150 else 151 OnContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES); 152} 153 154void TabSpecificContentSettings::OnWebDatabaseAccessed( 155 const GURL& url, 156 const string16& name, 157 const string16& display_name, 158 unsigned long estimated_size, 159 bool blocked_by_policy) { 160 if (blocked_by_policy) { 161 blocked_local_shared_objects_.databases()->AddDatabase( 162 url, UTF16ToUTF8(name), UTF16ToUTF8(display_name)); 163 OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES, std::string()); 164 } else { 165 allowed_local_shared_objects_.databases()->AddDatabase( 166 url, UTF16ToUTF8(name), UTF16ToUTF8(display_name)); 167 OnContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES); 168 } 169} 170 171void TabSpecificContentSettings::OnAppCacheAccessed( 172 const GURL& manifest_url, bool blocked_by_policy) { 173 if (blocked_by_policy) { 174 blocked_local_shared_objects_.appcaches()->AddAppCache(manifest_url); 175 OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES, std::string()); 176 } else { 177 allowed_local_shared_objects_.appcaches()->AddAppCache(manifest_url); 178 OnContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES); 179 } 180} 181 182void TabSpecificContentSettings::OnGeolocationPermissionSet( 183 const GURL& requesting_origin, 184 bool allowed) { 185 geolocation_settings_state_.OnGeolocationPermissionSet(requesting_origin, 186 allowed); 187 if (delegate_) 188 delegate_->OnContentSettingsAccessed(!allowed); 189} 190 191TabSpecificContentSettings::TabSpecificContentSettings( 192 Delegate* delegate, Profile* profile) 193 : allowed_local_shared_objects_(profile), 194 blocked_local_shared_objects_(profile), 195 geolocation_settings_state_(profile), 196 load_plugins_link_enabled_(true), 197 delegate_(NULL) { 198 ClearBlockedContentSettingsExceptForCookies(); 199 ClearCookieSpecificContentSettings(); 200 delegate_ = delegate; 201} 202 203void TabSpecificContentSettings::ClearBlockedContentSettingsExceptForCookies() { 204 for (size_t i = 0; i < arraysize(content_blocked_); ++i) { 205 if (i == CONTENT_SETTINGS_TYPE_COOKIES) 206 continue; 207 blocked_resources_[i].reset(); 208 content_blocked_[i] = false; 209 content_accessed_[i] = false; 210 content_blockage_indicated_to_user_[i] = false; 211 } 212 load_plugins_link_enabled_ = true; 213 if (delegate_) 214 delegate_->OnContentSettingsAccessed(false); 215} 216 217void TabSpecificContentSettings::ClearCookieSpecificContentSettings() { 218 blocked_local_shared_objects_.Reset(); 219 allowed_local_shared_objects_.Reset(); 220 content_blocked_[CONTENT_SETTINGS_TYPE_COOKIES] = false; 221 content_accessed_[CONTENT_SETTINGS_TYPE_COOKIES] = false; 222 content_blockage_indicated_to_user_[CONTENT_SETTINGS_TYPE_COOKIES] = false; 223 if (delegate_) 224 delegate_->OnContentSettingsAccessed(false); 225} 226 227void TabSpecificContentSettings::SetPopupsBlocked(bool blocked) { 228 content_blocked_[CONTENT_SETTINGS_TYPE_POPUPS] = blocked; 229 content_blockage_indicated_to_user_[CONTENT_SETTINGS_TYPE_POPUPS] = false; 230 if (delegate_) 231 delegate_->OnContentSettingsAccessed(blocked); 232} 233 234void TabSpecificContentSettings::GeolocationDidNavigate( 235 const NavigationController::LoadCommittedDetails& details) { 236 geolocation_settings_state_.DidNavigate(details); 237} 238 239void TabSpecificContentSettings::ClearGeolocationContentSettings() { 240 geolocation_settings_state_.ClearStateMap(); 241} 242 243CookiesTreeModel* TabSpecificContentSettings::GetAllowedCookiesTreeModel() { 244 return allowed_local_shared_objects_.GetCookiesTreeModel(); 245} 246 247CookiesTreeModel* TabSpecificContentSettings::GetBlockedCookiesTreeModel() { 248 return blocked_local_shared_objects_.GetCookiesTreeModel(); 249} 250 251TabSpecificContentSettings::LocalSharedObjectsContainer:: 252 LocalSharedObjectsContainer(Profile* profile) 253 : cookies_(new net::CookieMonster(NULL, NULL)), 254 appcaches_(new CannedBrowsingDataAppCacheHelper(profile)), 255 databases_(new CannedBrowsingDataDatabaseHelper(profile)), 256 indexed_dbs_(new CannedBrowsingDataIndexedDBHelper(profile)), 257 local_storages_(new CannedBrowsingDataLocalStorageHelper(profile)), 258 session_storages_(new CannedBrowsingDataLocalStorageHelper(profile)) { 259} 260 261TabSpecificContentSettings::LocalSharedObjectsContainer:: 262 ~LocalSharedObjectsContainer() { 263} 264 265void TabSpecificContentSettings::LocalSharedObjectsContainer::Reset() { 266 cookies_->DeleteAll(false); 267 appcaches_->Reset(); 268 databases_->Reset(); 269 indexed_dbs_->Reset(); 270 local_storages_->Reset(); 271 session_storages_->Reset(); 272} 273 274CookiesTreeModel* 275TabSpecificContentSettings::LocalSharedObjectsContainer::GetCookiesTreeModel() { 276 return new CookiesTreeModel(cookies_, 277 databases_, 278 local_storages_, 279 session_storages_, 280 appcaches_, 281 indexed_dbs_); 282} 283