off_the_record_profile_impl.cc revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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 user_prefs::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).get(), 124 io_data_.GetResourceContextNoInit()); 125#endif 126 127 BrowserThread::PostTask( 128 BrowserThread::IO, FROM_HERE, 129 base::Bind(&NotifyOTRProfileCreatedOnIOThread, profile_, this)); 130} 131 132OffTheRecordProfileImpl::~OffTheRecordProfileImpl() { 133 MaybeSendDestroyedNotification(); 134 135 HostZoomMap::GetForBrowserContext(profile_)->RemoveZoomLevelChangedCallback( 136 zoom_callback_); 137 138#if defined(ENABLE_PLUGINS) 139 ChromePluginServiceFilter::GetInstance()->UnregisterResourceContext( 140 io_data_.GetResourceContextNoInit()); 141#endif 142 143 BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices( 144 this); 145 146 BrowserThread::PostTask( 147 BrowserThread::IO, FROM_HERE, 148 base::Bind(&NotifyOTRProfileDestroyedOnIOThread, profile_, this)); 149 150 if (host_content_settings_map_.get()) 151 host_content_settings_map_->ShutdownOnUIThread(); 152 153 if (pref_proxy_config_tracker_) 154 pref_proxy_config_tracker_->DetachFromPrefService(); 155 156 // Clears any data the network stack contains that may be related to the 157 // OTR session. 158 g_browser_process->io_thread()->ChangedToOnTheRecord(); 159} 160 161void OffTheRecordProfileImpl::InitHostZoomMap() { 162 HostZoomMap* host_zoom_map = HostZoomMap::GetForBrowserContext(this); 163 HostZoomMap* parent_host_zoom_map = 164 HostZoomMap::GetForBrowserContext(profile_); 165 host_zoom_map->CopyFrom(parent_host_zoom_map); 166 // Observe parent's HZM change for propagating change of parent's 167 // change to this HZM. 168 parent_host_zoom_map->AddZoomLevelChangedCallback(zoom_callback_); 169} 170 171#if defined(OS_ANDROID) || defined(OS_IOS) 172void OffTheRecordProfileImpl::UseSystemProxy() { 173 // Force the use of the system-assigned proxy when off the record. 174 const char kProxyMode[] = "mode"; 175 const char kProxyServer[] = "server"; 176 const char kProxyBypassList[] = "bypass_list"; 177 const char kProxyPacUrl[] = "pac_url"; 178 DictionaryPrefUpdate update(prefs_, prefs::kProxy); 179 DictionaryValue* dict = update.Get(); 180 dict->SetString(kProxyMode, ProxyModeToString(ProxyPrefs::MODE_SYSTEM)); 181 dict->SetString(kProxyPacUrl, ""); 182 dict->SetString(kProxyServer, ""); 183 dict->SetString(kProxyBypassList, ""); 184} 185#endif // defined(OS_ANDROID) || defined(OS_IOS) 186 187std::string OffTheRecordProfileImpl::GetProfileName() { 188 // Incognito profile should not return the profile name. 189 return std::string(); 190} 191 192base::FilePath OffTheRecordProfileImpl::GetPath() { 193 return profile_->GetPath(); 194} 195 196scoped_refptr<base::SequencedTaskRunner> 197OffTheRecordProfileImpl::GetIOTaskRunner() { 198 return profile_->GetIOTaskRunner(); 199} 200 201bool OffTheRecordProfileImpl::IsOffTheRecord() const { 202 return true; 203} 204 205Profile* OffTheRecordProfileImpl::GetOffTheRecordProfile() { 206 return this; 207} 208 209void OffTheRecordProfileImpl::DestroyOffTheRecordProfile() { 210 // Suicide is bad! 211 NOTREACHED(); 212} 213 214bool OffTheRecordProfileImpl::HasOffTheRecordProfile() { 215 return true; 216} 217 218Profile* OffTheRecordProfileImpl::GetOriginalProfile() { 219 return profile_; 220} 221 222ExtensionService* OffTheRecordProfileImpl::GetExtensionService() { 223 return extensions::ExtensionSystem::Get(this)->extension_service(); 224} 225 226ExtensionSpecialStoragePolicy* 227 OffTheRecordProfileImpl::GetExtensionSpecialStoragePolicy() { 228 return GetOriginalProfile()->GetExtensionSpecialStoragePolicy(); 229} 230 231PrefService* OffTheRecordProfileImpl::GetPrefs() { 232 return prefs_; 233} 234 235PrefService* OffTheRecordProfileImpl::GetOffTheRecordPrefs() { 236 return prefs_; 237} 238 239DownloadManagerDelegate* OffTheRecordProfileImpl::GetDownloadManagerDelegate() { 240 return DownloadServiceFactory::GetForProfile(this)-> 241 GetDownloadManagerDelegate(); 242} 243 244net::URLRequestContextGetter* OffTheRecordProfileImpl::GetRequestContext() { 245 return GetDefaultStoragePartition(this)->GetURLRequestContext(); 246} 247 248net::URLRequestContextGetter* OffTheRecordProfileImpl::CreateRequestContext( 249 content::ProtocolHandlerMap* protocol_handlers) { 250 return io_data_.CreateMainRequestContextGetter(protocol_handlers).get(); 251} 252 253net::URLRequestContextGetter* 254 OffTheRecordProfileImpl::GetRequestContextForRenderProcess( 255 int renderer_child_id) { 256 content::RenderProcessHost* rph = content::RenderProcessHost::FromID( 257 renderer_child_id); 258 return rph->GetStoragePartition()->GetURLRequestContext(); 259} 260 261net::URLRequestContextGetter* 262 OffTheRecordProfileImpl::GetMediaRequestContext() { 263 // In OTR mode, media request context is the same as the original one. 264 return GetRequestContext(); 265} 266 267net::URLRequestContextGetter* 268 OffTheRecordProfileImpl::GetMediaRequestContextForRenderProcess( 269 int renderer_child_id) { 270 // In OTR mode, media request context is the same as the original one. 271 return GetRequestContextForRenderProcess(renderer_child_id); 272} 273 274net::URLRequestContextGetter* 275OffTheRecordProfileImpl::GetMediaRequestContextForStoragePartition( 276 const base::FilePath& partition_path, 277 bool in_memory) { 278 return io_data_.GetIsolatedAppRequestContextGetter(partition_path, in_memory) 279 .get(); 280} 281 282net::URLRequestContextGetter* 283 OffTheRecordProfileImpl::GetRequestContextForExtensions() { 284 return io_data_.GetExtensionsRequestContextGetter().get(); 285} 286 287net::URLRequestContextGetter* 288 OffTheRecordProfileImpl::CreateRequestContextForStoragePartition( 289 const base::FilePath& partition_path, 290 bool in_memory, 291 content::ProtocolHandlerMap* protocol_handlers) { 292 return io_data_.CreateIsolatedAppRequestContextGetter( 293 partition_path, in_memory, protocol_handlers).get(); 294} 295 296content::ResourceContext* OffTheRecordProfileImpl::GetResourceContext() { 297 return io_data_.GetResourceContext(); 298} 299 300net::SSLConfigService* OffTheRecordProfileImpl::GetSSLConfigService() { 301 return profile_->GetSSLConfigService(); 302} 303 304HostContentSettingsMap* OffTheRecordProfileImpl::GetHostContentSettingsMap() { 305 // Retrieve the host content settings map of the parent profile in order to 306 // ensure the preferences have been migrated. 307 profile_->GetHostContentSettingsMap(); 308 if (!host_content_settings_map_.get()) { 309 host_content_settings_map_ = new HostContentSettingsMap(GetPrefs(), true); 310#if defined(ENABLE_EXTENSIONS) 311 ExtensionService* extension_service = GetExtensionService(); 312 if (extension_service) 313 host_content_settings_map_->RegisterExtensionService(extension_service); 314#endif 315 } 316 return host_content_settings_map_.get(); 317} 318 319content::GeolocationPermissionContext* 320 OffTheRecordProfileImpl::GetGeolocationPermissionContext() { 321 return ChromeGeolocationPermissionContextFactory::GetForProfile(this); 322} 323 324content::SpeechRecognitionPreferences* 325 OffTheRecordProfileImpl::GetSpeechRecognitionPreferences() { 326 return profile_->GetSpeechRecognitionPreferences(); 327} 328 329quota::SpecialStoragePolicy* 330 OffTheRecordProfileImpl::GetSpecialStoragePolicy() { 331 return GetExtensionSpecialStoragePolicy(); 332} 333 334bool OffTheRecordProfileImpl::IsSameProfile(Profile* profile) { 335 return (profile == this) || (profile == profile_); 336} 337 338Time OffTheRecordProfileImpl::GetStartTime() const { 339 return start_time_; 340} 341 342history::TopSites* OffTheRecordProfileImpl::GetTopSitesWithoutCreating() { 343 return NULL; 344} 345 346history::TopSites* OffTheRecordProfileImpl::GetTopSites() { 347 return NULL; 348} 349 350void OffTheRecordProfileImpl::SetExitType(ExitType exit_type) { 351} 352 353base::FilePath OffTheRecordProfileImpl::last_selected_directory() { 354 const base::FilePath& directory = last_selected_directory_; 355 if (directory.empty()) { 356 return profile_->last_selected_directory(); 357 } 358 return directory; 359} 360 361void OffTheRecordProfileImpl::set_last_selected_directory( 362 const base::FilePath& path) { 363 last_selected_directory_ = path; 364} 365 366bool OffTheRecordProfileImpl::WasCreatedByVersionOrLater( 367 const std::string& version) { 368 return profile_->WasCreatedByVersionOrLater(version); 369} 370 371Profile::ExitType OffTheRecordProfileImpl::GetLastSessionExitType() { 372 return profile_->GetLastSessionExitType(); 373} 374 375#if defined(OS_CHROMEOS) 376void OffTheRecordProfileImpl::SetupChromeOSEnterpriseExtensionObserver() { 377 profile_->SetupChromeOSEnterpriseExtensionObserver(); 378} 379 380void OffTheRecordProfileImpl::InitChromeOSPreferences() { 381 // The incognito profile shouldn't have Chrome OS's preferences. 382 // The preferences are associated with the regular user profile. 383} 384#endif // defined(OS_CHROMEOS) 385 386#if defined(OS_CHROMEOS) 387void OffTheRecordProfileImpl::ChangeAppLocale(const std::string& locale, 388 AppLocaleChangedVia) { 389} 390 391void OffTheRecordProfileImpl::OnLogin() { 392} 393#endif // defined(OS_CHROMEOS) 394 395PrefProxyConfigTracker* OffTheRecordProfileImpl::GetProxyConfigTracker() { 396 if (!pref_proxy_config_tracker_) { 397 pref_proxy_config_tracker_.reset( 398 ProxyServiceFactory::CreatePrefProxyConfigTracker(GetPrefs())); 399 } 400 return pref_proxy_config_tracker_.get(); 401} 402 403chrome_browser_net::Predictor* OffTheRecordProfileImpl::GetNetworkPredictor() { 404 // We do not store information about websites visited in OTR profiles which 405 // is necessary for a Predictor, so we do not have a Predictor at all. 406 return NULL; 407} 408 409void OffTheRecordProfileImpl::ClearNetworkingHistorySince( 410 base::Time time, 411 const base::Closure& completion) { 412 // Nothing to do here, our transport security state is read-only. 413 // Still, fire the callback to indicate we have finished, otherwise the 414 // BrowsingDataRemover will never be destroyed and the dialog will never be 415 // closed. We must do this asynchronously in order to avoid reentrancy issues. 416 if (!completion.is_null()) { 417 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion); 418 } 419} 420 421GURL OffTheRecordProfileImpl::GetHomePage() { 422 return profile_->GetHomePage(); 423} 424 425#if defined(OS_CHROMEOS) 426// Special case of the OffTheRecordProfileImpl which is used while Guest 427// session in CrOS. 428class GuestSessionProfile : public OffTheRecordProfileImpl { 429 public: 430 explicit GuestSessionProfile(Profile* real_profile) 431 : OffTheRecordProfileImpl(real_profile) { 432 } 433 434 virtual void InitChromeOSPreferences() OVERRIDE { 435 chromeos_preferences_.reset(new chromeos::Preferences()); 436 chromeos_preferences_->Init(static_cast<PrefServiceSyncable*>(GetPrefs())); 437 } 438 439 private: 440 // The guest user should be able to customize Chrome OS preferences. 441 scoped_ptr<chromeos::Preferences> chromeos_preferences_; 442}; 443#endif 444 445Profile* Profile::CreateOffTheRecordProfile() { 446 OffTheRecordProfileImpl* profile = NULL; 447#if defined(OS_CHROMEOS) 448 if (IsGuestSession()) 449 profile = new GuestSessionProfile(this); 450#endif 451 if (!profile) 452 profile = new OffTheRecordProfileImpl(this); 453 profile->Init(); 454 return profile; 455} 456 457void OffTheRecordProfileImpl::OnZoomLevelChanged( 458 const HostZoomMap::ZoomLevelChange& change) { 459 HostZoomMap* host_zoom_map = HostZoomMap::GetForBrowserContext(this); 460 switch (change.mode) { 461 case HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM: 462 return; 463 case HostZoomMap::ZOOM_CHANGED_FOR_HOST: 464 host_zoom_map->SetZoomLevelForHost(change.host, change.zoom_level); 465 return; 466 case HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST: 467 host_zoom_map->SetZoomLevelForHostAndScheme(change.scheme, 468 change.host, 469 change.zoom_level); 470 return; 471 } 472} 473