off_the_record_profile_impl.cc revision 868fa2fe829687343ffae624259930155e16dbd8
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. 4 5#include "chrome/browser/profiles/off_the_record_profile_impl.h" 6 7#include "base/bind.h" 8#include "base/command_line.h" 9#include "base/compiler_specific.h" 10#include "base/files/file_path.h" 11#include "base/memory/scoped_ptr.h" 12#include "base/path_service.h" 13#include "base/prefs/json_pref_store.h" 14#include "base/strings/string_number_conversions.h" 15#include "base/strings/string_util.h" 16#include "build/build_config.h" 17#include "chrome/browser/background/background_contents_service_factory.h" 18#include "chrome/browser/browser_process.h" 19#include "chrome/browser/content_settings/host_content_settings_map.h" 20#include "chrome/browser/download/chrome_download_manager_delegate.h" 21#include "chrome/browser/download/download_service.h" 22#include "chrome/browser/download/download_service_factory.h" 23#include "chrome/browser/extensions/api/web_request/web_request_api.h" 24#include "chrome/browser/extensions/extension_info_map.h" 25#include "chrome/browser/extensions/extension_service.h" 26#include "chrome/browser/extensions/extension_special_storage_policy.h" 27#include "chrome/browser/extensions/extension_system.h" 28#include "chrome/browser/geolocation/chrome_geolocation_permission_context.h" 29#include "chrome/browser/geolocation/chrome_geolocation_permission_context_factory.h" 30#include "chrome/browser/io_thread.h" 31#include "chrome/browser/net/proxy_service_factory.h" 32#include "chrome/browser/plugins/chrome_plugin_service_filter.h" 33#include "chrome/browser/plugins/plugin_prefs.h" 34#include "chrome/browser/prefs/incognito_mode_prefs.h" 35#include "chrome/browser/prefs/pref_service_syncable.h" 36#include "chrome/browser/themes/theme_service.h" 37#include "chrome/browser/ui/webui/extensions/extension_icon_source.h" 38#include "chrome/common/chrome_constants.h" 39#include "chrome/common/chrome_paths.h" 40#include "chrome/common/chrome_switches.h" 41#include "chrome/common/extensions/extension.h" 42#include "chrome/common/pref_names.h" 43#include "chrome/common/render_messages.h" 44#include "components/browser_context_keyed_service/browser_context_dependency_manager.h" 45#include "components/user_prefs/user_prefs.h" 46#include "content/public/browser/browser_thread.h" 47#include "content/public/browser/host_zoom_map.h" 48#include "content/public/browser/render_process_host.h" 49#include "content/public/browser/storage_partition.h" 50#include "content/public/browser/url_data_source.h" 51#include "content/public/browser/web_contents.h" 52#include "net/http/http_server_properties.h" 53#include "net/http/transport_security_state.h" 54#include "webkit/browser/database/database_tracker.h" 55 56#if defined(OS_ANDROID) || defined(OS_IOS) 57#include "chrome/browser/prefs/scoped_user_pref_update.h" 58#endif // defined(OS_ANDROID) || defined(OS_IOS) 59 60#if defined(OS_CHROMEOS) 61#include "chrome/browser/chromeos/preferences.h" 62#include "chrome/browser/chromeos/proxy_config_service_impl.h" 63#endif 64 65using content::BrowserThread; 66using content::DownloadManagerDelegate; 67using content::HostZoomMap; 68 69namespace { 70 71void NotifyOTRProfileCreatedOnIOThread(void* original_profile, 72 void* otr_profile) { 73 ExtensionWebRequestEventRouter::GetInstance()->OnOTRProfileCreated( 74 original_profile, otr_profile); 75} 76 77void NotifyOTRProfileDestroyedOnIOThread(void* original_profile, 78 void* otr_profile) { 79 ExtensionWebRequestEventRouter::GetInstance()->OnOTRProfileDestroyed( 80 original_profile, otr_profile); 81} 82 83} // namespace 84 85OffTheRecordProfileImpl::OffTheRecordProfileImpl(Profile* real_profile) 86 : profile_(real_profile), 87 prefs_(PrefServiceSyncable::IncognitoFromProfile(real_profile)), 88 io_data_(this), 89 start_time_(Time::Now()), 90 zoom_callback_(base::Bind(&OffTheRecordProfileImpl::OnZoomLevelChanged, 91 base::Unretained(this))) { 92 // Register on BrowserContext. 93 components::UserPrefs::Set(this, prefs_); 94} 95 96void OffTheRecordProfileImpl::Init() { 97 BrowserContextDependencyManager::GetInstance()->CreateBrowserContextServices( 98 this, false); 99 100 DCHECK_NE(IncognitoModePrefs::DISABLED, 101 IncognitoModePrefs::GetAvailability(profile_->GetPrefs())); 102 103#if defined(OS_ANDROID) || defined(OS_IOS) 104 UseSystemProxy(); 105#endif // defined(OS_ANDROID) || defined(OS_IOS) 106 107 // TODO(oshima): Remove the need to eagerly initialize the request context 108 // getter. chromeos::OnlineAttempt is illegally trying to access this 109 // Profile member from a thread other than the UI thread, so we need to 110 // prevent a race. 111#if defined(OS_CHROMEOS) 112 GetRequestContext(); 113#endif // defined(OS_CHROMEOS) 114 115 InitHostZoomMap(); 116 117 // Make the chrome//extension-icon/ resource available. 118 ExtensionIconSource* icon_source = new ExtensionIconSource(profile_); 119 content::URLDataSource::Add(this, icon_source); 120 121#if defined(ENABLE_PLUGINS) 122 ChromePluginServiceFilter::GetInstance()->RegisterResourceContext( 123 PluginPrefs::GetForProfile(this), io_data_.GetResourceContextNoInit()); 124#endif 125 126 BrowserThread::PostTask( 127 BrowserThread::IO, FROM_HERE, 128 base::Bind(&NotifyOTRProfileCreatedOnIOThread, profile_, this)); 129} 130 131OffTheRecordProfileImpl::~OffTheRecordProfileImpl() { 132 MaybeSendDestroyedNotification(); 133 134 HostZoomMap::GetForBrowserContext(profile_)->RemoveZoomLevelChangedCallback( 135 zoom_callback_); 136 137#if defined(ENABLE_PLUGINS) 138 ChromePluginServiceFilter::GetInstance()->UnregisterResourceContext( 139 io_data_.GetResourceContextNoInit()); 140#endif 141 142 BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices( 143 this); 144 145 BrowserThread::PostTask( 146 BrowserThread::IO, FROM_HERE, 147 base::Bind(&NotifyOTRProfileDestroyedOnIOThread, profile_, this)); 148 149 if (host_content_settings_map_.get()) 150 host_content_settings_map_->ShutdownOnUIThread(); 151 152 if (pref_proxy_config_tracker_) 153 pref_proxy_config_tracker_->DetachFromPrefService(); 154 155 // Clears any data the network stack contains that may be related to the 156 // OTR session. 157 g_browser_process->io_thread()->ChangedToOnTheRecord(); 158} 159 160void OffTheRecordProfileImpl::InitHostZoomMap() { 161 HostZoomMap* host_zoom_map = HostZoomMap::GetForBrowserContext(this); 162 HostZoomMap* parent_host_zoom_map = 163 HostZoomMap::GetForBrowserContext(profile_); 164 host_zoom_map->CopyFrom(parent_host_zoom_map); 165 // Observe parent's HZM change for propagating change of parent's 166 // change to this HZM. 167 parent_host_zoom_map->AddZoomLevelChangedCallback(zoom_callback_); 168} 169 170#if defined(OS_ANDROID) || defined(OS_IOS) 171void OffTheRecordProfileImpl::UseSystemProxy() { 172 // Force the use of the system-assigned proxy when off the record. 173 const char kProxyMode[] = "mode"; 174 const char kProxyServer[] = "server"; 175 const char kProxyBypassList[] = "bypass_list"; 176 const char kProxyPacUrl[] = "pac_url"; 177 DictionaryPrefUpdate update(prefs_, prefs::kProxy); 178 DictionaryValue* dict = update.Get(); 179 dict->SetString(kProxyMode, ProxyModeToString(ProxyPrefs::MODE_SYSTEM)); 180 dict->SetString(kProxyPacUrl, ""); 181 dict->SetString(kProxyServer, ""); 182 dict->SetString(kProxyBypassList, ""); 183} 184#endif // defined(OS_ANDROID) || defined(OS_IOS) 185 186std::string OffTheRecordProfileImpl::GetProfileName() { 187 // Incognito profile should not return the profile name. 188 return std::string(); 189} 190 191base::FilePath OffTheRecordProfileImpl::GetPath() { 192 return profile_->GetPath(); 193} 194 195scoped_refptr<base::SequencedTaskRunner> 196OffTheRecordProfileImpl::GetIOTaskRunner() { 197 return profile_->GetIOTaskRunner(); 198} 199 200bool OffTheRecordProfileImpl::IsOffTheRecord() const { 201 return true; 202} 203 204Profile* OffTheRecordProfileImpl::GetOffTheRecordProfile() { 205 return this; 206} 207 208void OffTheRecordProfileImpl::DestroyOffTheRecordProfile() { 209 // Suicide is bad! 210 NOTREACHED(); 211} 212 213bool OffTheRecordProfileImpl::HasOffTheRecordProfile() { 214 return true; 215} 216 217Profile* OffTheRecordProfileImpl::GetOriginalProfile() { 218 return profile_; 219} 220 221ExtensionService* OffTheRecordProfileImpl::GetExtensionService() { 222 return extensions::ExtensionSystem::Get(this)->extension_service(); 223} 224 225ExtensionSpecialStoragePolicy* 226 OffTheRecordProfileImpl::GetExtensionSpecialStoragePolicy() { 227 return GetOriginalProfile()->GetExtensionSpecialStoragePolicy(); 228} 229 230PrefService* OffTheRecordProfileImpl::GetPrefs() { 231 return prefs_; 232} 233 234PrefService* OffTheRecordProfileImpl::GetOffTheRecordPrefs() { 235 return prefs_; 236} 237 238DownloadManagerDelegate* OffTheRecordProfileImpl::GetDownloadManagerDelegate() { 239 return DownloadServiceFactory::GetForProfile(this)-> 240 GetDownloadManagerDelegate(); 241} 242 243net::URLRequestContextGetter* OffTheRecordProfileImpl::GetRequestContext() { 244 return GetDefaultStoragePartition(this)->GetURLRequestContext(); 245} 246 247net::URLRequestContextGetter* OffTheRecordProfileImpl::CreateRequestContext( 248 content::ProtocolHandlerMap* protocol_handlers) { 249 return io_data_.CreateMainRequestContextGetter(protocol_handlers); 250} 251 252net::URLRequestContextGetter* 253 OffTheRecordProfileImpl::GetRequestContextForRenderProcess( 254 int renderer_child_id) { 255 content::RenderProcessHost* rph = content::RenderProcessHost::FromID( 256 renderer_child_id); 257 return rph->GetStoragePartition()->GetURLRequestContext(); 258} 259 260net::URLRequestContextGetter* 261 OffTheRecordProfileImpl::GetMediaRequestContext() { 262 // In OTR mode, media request context is the same as the original one. 263 return GetRequestContext(); 264} 265 266net::URLRequestContextGetter* 267 OffTheRecordProfileImpl::GetMediaRequestContextForRenderProcess( 268 int renderer_child_id) { 269 // In OTR mode, media request context is the same as the original one. 270 return GetRequestContextForRenderProcess(renderer_child_id); 271} 272 273net::URLRequestContextGetter* 274OffTheRecordProfileImpl::GetMediaRequestContextForStoragePartition( 275 const base::FilePath& partition_path, 276 bool in_memory) { 277 return io_data_.GetIsolatedAppRequestContextGetter(partition_path, in_memory); 278} 279 280net::URLRequestContextGetter* 281 OffTheRecordProfileImpl::GetRequestContextForExtensions() { 282 return io_data_.GetExtensionsRequestContextGetter(); 283} 284 285net::URLRequestContextGetter* 286 OffTheRecordProfileImpl::CreateRequestContextForStoragePartition( 287 const base::FilePath& partition_path, 288 bool in_memory, 289 content::ProtocolHandlerMap* protocol_handlers) { 290 return io_data_.CreateIsolatedAppRequestContextGetter( 291 partition_path, in_memory, protocol_handlers); 292} 293 294content::ResourceContext* OffTheRecordProfileImpl::GetResourceContext() { 295 return io_data_.GetResourceContext(); 296} 297 298net::SSLConfigService* OffTheRecordProfileImpl::GetSSLConfigService() { 299 return profile_->GetSSLConfigService(); 300} 301 302HostContentSettingsMap* OffTheRecordProfileImpl::GetHostContentSettingsMap() { 303 // Retrieve the host content settings map of the parent profile in order to 304 // ensure the preferences have been migrated. 305 profile_->GetHostContentSettingsMap(); 306 if (!host_content_settings_map_.get()) { 307 host_content_settings_map_ = new HostContentSettingsMap(GetPrefs(), true); 308#if defined(ENABLE_EXTENSIONS) 309 ExtensionService* extension_service = GetExtensionService(); 310 if (extension_service) 311 host_content_settings_map_->RegisterExtensionService(extension_service); 312#endif 313 } 314 return host_content_settings_map_.get(); 315} 316 317content::GeolocationPermissionContext* 318 OffTheRecordProfileImpl::GetGeolocationPermissionContext() { 319 return ChromeGeolocationPermissionContextFactory::GetForProfile(this); 320} 321 322content::SpeechRecognitionPreferences* 323 OffTheRecordProfileImpl::GetSpeechRecognitionPreferences() { 324 return profile_->GetSpeechRecognitionPreferences(); 325} 326 327quota::SpecialStoragePolicy* 328 OffTheRecordProfileImpl::GetSpecialStoragePolicy() { 329 return GetExtensionSpecialStoragePolicy(); 330} 331 332bool OffTheRecordProfileImpl::IsSameProfile(Profile* profile) { 333 return (profile == this) || (profile == profile_); 334} 335 336Time OffTheRecordProfileImpl::GetStartTime() const { 337 return start_time_; 338} 339 340history::TopSites* OffTheRecordProfileImpl::GetTopSitesWithoutCreating() { 341 return NULL; 342} 343 344history::TopSites* OffTheRecordProfileImpl::GetTopSites() { 345 return NULL; 346} 347 348void OffTheRecordProfileImpl::SetExitType(ExitType exit_type) { 349} 350 351base::FilePath OffTheRecordProfileImpl::last_selected_directory() { 352 const base::FilePath& directory = last_selected_directory_; 353 if (directory.empty()) { 354 return profile_->last_selected_directory(); 355 } 356 return directory; 357} 358 359void OffTheRecordProfileImpl::set_last_selected_directory( 360 const base::FilePath& path) { 361 last_selected_directory_ = path; 362} 363 364bool OffTheRecordProfileImpl::WasCreatedByVersionOrLater( 365 const std::string& version) { 366 return profile_->WasCreatedByVersionOrLater(version); 367} 368 369Profile::ExitType OffTheRecordProfileImpl::GetLastSessionExitType() { 370 return profile_->GetLastSessionExitType(); 371} 372 373#if defined(OS_CHROMEOS) 374void OffTheRecordProfileImpl::SetupChromeOSEnterpriseExtensionObserver() { 375 profile_->SetupChromeOSEnterpriseExtensionObserver(); 376} 377 378void OffTheRecordProfileImpl::InitChromeOSPreferences() { 379 // The incognito profile shouldn't have Chrome OS's preferences. 380 // The preferences are associated with the regular user profile. 381} 382#endif // defined(OS_CHROMEOS) 383 384#if defined(OS_CHROMEOS) 385void OffTheRecordProfileImpl::ChangeAppLocale(const std::string& locale, 386 AppLocaleChangedVia) { 387} 388 389void OffTheRecordProfileImpl::OnLogin() { 390} 391#endif // defined(OS_CHROMEOS) 392 393PrefProxyConfigTracker* OffTheRecordProfileImpl::GetProxyConfigTracker() { 394 if (!pref_proxy_config_tracker_) { 395 pref_proxy_config_tracker_.reset( 396 ProxyServiceFactory::CreatePrefProxyConfigTracker(GetPrefs())); 397 } 398 return pref_proxy_config_tracker_.get(); 399} 400 401chrome_browser_net::Predictor* OffTheRecordProfileImpl::GetNetworkPredictor() { 402 // We do not store information about websites visited in OTR profiles which 403 // is necessary for a Predictor, so we do not have a Predictor at all. 404 return NULL; 405} 406 407void OffTheRecordProfileImpl::ClearNetworkingHistorySince( 408 base::Time time, 409 const base::Closure& completion) { 410 // Nothing to do here, our transport security state is read-only. 411 // Still, fire the callback to indicate we have finished, otherwise the 412 // BrowsingDataRemover will never be destroyed and the dialog will never be 413 // closed. We must do this asynchronously in order to avoid reentrancy issues. 414 if (!completion.is_null()) { 415 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion); 416 } 417} 418 419GURL OffTheRecordProfileImpl::GetHomePage() { 420 return profile_->GetHomePage(); 421} 422 423#if defined(OS_CHROMEOS) 424// Special case of the OffTheRecordProfileImpl which is used while Guest 425// session in CrOS. 426class GuestSessionProfile : public OffTheRecordProfileImpl { 427 public: 428 explicit GuestSessionProfile(Profile* real_profile) 429 : OffTheRecordProfileImpl(real_profile) { 430 } 431 432 virtual void InitChromeOSPreferences() OVERRIDE { 433 chromeos_preferences_.reset(new chromeos::Preferences()); 434 chromeos_preferences_->Init(static_cast<PrefServiceSyncable*>(GetPrefs())); 435 } 436 437 private: 438 // The guest user should be able to customize Chrome OS preferences. 439 scoped_ptr<chromeos::Preferences> chromeos_preferences_; 440}; 441#endif 442 443Profile* Profile::CreateOffTheRecordProfile() { 444 OffTheRecordProfileImpl* profile = NULL; 445#if defined(OS_CHROMEOS) 446 if (IsGuestSession()) 447 profile = new GuestSessionProfile(this); 448#endif 449 if (!profile) 450 profile = new OffTheRecordProfileImpl(this); 451 profile->Init(); 452 return profile; 453} 454 455void OffTheRecordProfileImpl::OnZoomLevelChanged( 456 const HostZoomMap::ZoomLevelChange& change) { 457 HostZoomMap* host_zoom_map = HostZoomMap::GetForBrowserContext(this); 458 switch (change.mode) { 459 case HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM: 460 return; 461 case HostZoomMap::ZOOM_CHANGED_FOR_HOST: 462 host_zoom_map->SetZoomLevelForHost(change.host, change.zoom_level); 463 return; 464 case HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST: 465 host_zoom_map->SetZoomLevelForHostAndScheme(change.scheme, 466 change.host, 467 change.zoom_level); 468 return; 469 } 470} 471