off_the_record_profile_impl.cc revision 010d83a9304c5a91596085d917d248abff47903a
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_service.h" 25#include "chrome/browser/extensions/extension_special_storage_policy.h" 26#include "chrome/browser/geolocation/chrome_geolocation_permission_context.h" 27#include "chrome/browser/geolocation/chrome_geolocation_permission_context_factory.h" 28#include "chrome/browser/guest_view/guest_view_manager.h" 29#include "chrome/browser/io_thread.h" 30#include "chrome/browser/media/chrome_midi_permission_context.h" 31#include "chrome/browser/media/chrome_midi_permission_context_factory.h" 32#include "chrome/browser/net/pref_proxy_config_tracker.h" 33#include "chrome/browser/net/proxy_service_factory.h" 34#include "chrome/browser/plugins/chrome_plugin_service_filter.h" 35#include "chrome/browser/plugins/plugin_prefs.h" 36#include "chrome/browser/prefs/incognito_mode_prefs.h" 37#include "chrome/browser/prefs/pref_service_syncable.h" 38#include "chrome/browser/themes/theme_service.h" 39#include "chrome/browser/ui/webui/extensions/extension_icon_source.h" 40#include "chrome/common/chrome_constants.h" 41#include "chrome/common/chrome_paths.h" 42#include "chrome/common/chrome_switches.h" 43#include "chrome/common/pref_names.h" 44#include "chrome/common/render_messages.h" 45#include "components/keyed_service/content/browser_context_dependency_manager.h" 46#include "components/user_prefs/user_prefs.h" 47#include "content/public/browser/browser_thread.h" 48#include "content/public/browser/host_zoom_map.h" 49#include "content/public/browser/render_process_host.h" 50#include "content/public/browser/storage_partition.h" 51#include "content/public/browser/url_data_source.h" 52#include "content/public/browser/web_contents.h" 53#include "extensions/browser/extension_system.h" 54#include "extensions/common/extension.h" 55#include "net/http/http_server_properties.h" 56#include "net/http/transport_security_state.h" 57#include "webkit/browser/database/database_tracker.h" 58 59#if defined(OS_ANDROID) 60#include "chrome/browser/media/protected_media_identifier_permission_context.h" 61#include "chrome/browser/media/protected_media_identifier_permission_context_factory.h" 62#endif // defined(OS_ANDROID) 63 64#if defined(OS_ANDROID) || defined(OS_IOS) 65#include "base/prefs/scoped_user_pref_update.h" 66#include "chrome/browser/prefs/proxy_prefs.h" 67#endif // defined(OS_ANDROID) || defined(OS_IOS) 68 69#if defined(OS_CHROMEOS) 70#include "chrome/browser/chromeos/preferences.h" 71#include "chrome/browser/chromeos/profiles/profile_helper.h" 72#endif 73 74#if defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS) 75#include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h" 76#endif 77 78using content::BrowserThread; 79using content::DownloadManagerDelegate; 80using content::HostZoomMap; 81 82namespace { 83 84void NotifyOTRProfileCreatedOnIOThread(void* original_profile, 85 void* otr_profile) { 86 ExtensionWebRequestEventRouter::GetInstance()->OnOTRProfileCreated( 87 original_profile, otr_profile); 88} 89 90void NotifyOTRProfileDestroyedOnIOThread(void* original_profile, 91 void* otr_profile) { 92 ExtensionWebRequestEventRouter::GetInstance()->OnOTRProfileDestroyed( 93 original_profile, otr_profile); 94} 95 96} // namespace 97 98OffTheRecordProfileImpl::OffTheRecordProfileImpl(Profile* real_profile) 99 : profile_(real_profile), 100 prefs_(PrefServiceSyncable::IncognitoFromProfile(real_profile)), 101 start_time_(Time::Now()) { 102 // Register on BrowserContext. 103 user_prefs::UserPrefs::Set(this, prefs_); 104} 105 106void OffTheRecordProfileImpl::Init() { 107 // The construction of OffTheRecordProfileIOData::Handle needs the profile 108 // type returned by this->GetProfileType(). Since GetProfileType() is a 109 // virtual member function, we cannot call the function defined in the most 110 // derived class (e.g. GuestSessionProfile) until a ctor finishes. Thus, 111 // we have to instantiate OffTheRecordProfileIOData::Handle here after a ctor. 112 InitIoData(); 113 114#if defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS) 115 // Because UserCloudPolicyManager is in a component, it cannot access 116 // GetOriginalProfile. Instead, we have to inject this relation here. 117 policy::UserCloudPolicyManagerFactory::RegisterForOffTheRecordBrowserContext( 118 this->GetOriginalProfile(), this); 119#endif 120 121 BrowserContextDependencyManager::GetInstance()->CreateBrowserContextServices( 122 this); 123 124 DCHECK_NE(IncognitoModePrefs::DISABLED, 125 IncognitoModePrefs::GetAvailability(profile_->GetPrefs())); 126 127#if defined(OS_ANDROID) || defined(OS_IOS) 128 UseSystemProxy(); 129#endif // defined(OS_ANDROID) || defined(OS_IOS) 130 131 // TODO(oshima): Remove the need to eagerly initialize the request context 132 // getter. chromeos::OnlineAttempt is illegally trying to access this 133 // Profile member from a thread other than the UI thread, so we need to 134 // prevent a race. 135#if defined(OS_CHROMEOS) 136 GetRequestContext(); 137#endif // defined(OS_CHROMEOS) 138 139 InitHostZoomMap(); 140 141 // Make the chrome//extension-icon/ resource available. 142 extensions::ExtensionIconSource* icon_source = 143 new extensions::ExtensionIconSource(profile_); 144 content::URLDataSource::Add(this, icon_source); 145 146#if defined(ENABLE_PLUGINS) 147 ChromePluginServiceFilter::GetInstance()->RegisterResourceContext( 148 PluginPrefs::GetForProfile(this).get(), 149 io_data_->GetResourceContextNoInit()); 150#endif 151 152 BrowserThread::PostTask( 153 BrowserThread::IO, FROM_HERE, 154 base::Bind(&NotifyOTRProfileCreatedOnIOThread, profile_, this)); 155} 156 157OffTheRecordProfileImpl::~OffTheRecordProfileImpl() { 158 MaybeSendDestroyedNotification(); 159 160#if defined(ENABLE_PLUGINS) 161 ChromePluginServiceFilter::GetInstance()->UnregisterResourceContext( 162 io_data_->GetResourceContextNoInit()); 163#endif 164 165 BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices( 166 this); 167 168 BrowserThread::PostTask( 169 BrowserThread::IO, FROM_HERE, 170 base::Bind(&NotifyOTRProfileDestroyedOnIOThread, profile_, this)); 171 172 if (host_content_settings_map_.get()) 173 host_content_settings_map_->ShutdownOnUIThread(); 174 175 if (pref_proxy_config_tracker_) 176 pref_proxy_config_tracker_->DetachFromPrefService(); 177 178 // Clears any data the network stack contains that may be related to the 179 // OTR session. 180 g_browser_process->io_thread()->ChangedToOnTheRecord(); 181} 182 183void OffTheRecordProfileImpl::InitIoData() { 184 io_data_.reset(new OffTheRecordProfileIOData::Handle(this)); 185} 186 187void OffTheRecordProfileImpl::InitHostZoomMap() { 188 HostZoomMap* host_zoom_map = HostZoomMap::GetForBrowserContext(this); 189 HostZoomMap* parent_host_zoom_map = 190 HostZoomMap::GetForBrowserContext(profile_); 191 host_zoom_map->CopyFrom(parent_host_zoom_map); 192 // Observe parent's HZM change for propagating change of parent's 193 // change to this HZM. 194 zoom_subscription_ = parent_host_zoom_map->AddZoomLevelChangedCallback( 195 base::Bind(&OffTheRecordProfileImpl::OnZoomLevelChanged, 196 base::Unretained(this))); 197} 198 199#if defined(OS_ANDROID) || defined(OS_IOS) 200void OffTheRecordProfileImpl::UseSystemProxy() { 201 // Force the use of the system-assigned proxy when off the record. 202 const char kProxyMode[] = "mode"; 203 const char kProxyServer[] = "server"; 204 const char kProxyBypassList[] = "bypass_list"; 205 const char kProxyPacUrl[] = "pac_url"; 206 DictionaryPrefUpdate update(prefs_, prefs::kProxy); 207 base::DictionaryValue* dict = update.Get(); 208 dict->SetString(kProxyMode, ProxyModeToString(ProxyPrefs::MODE_SYSTEM)); 209 dict->SetString(kProxyPacUrl, ""); 210 dict->SetString(kProxyServer, ""); 211 dict->SetString(kProxyBypassList, ""); 212} 213#endif // defined(OS_ANDROID) || defined(OS_IOS) 214 215std::string OffTheRecordProfileImpl::GetProfileName() { 216 // Incognito profile should not return the profile name. 217 return std::string(); 218} 219 220Profile::ProfileType OffTheRecordProfileImpl::GetProfileType() const { 221 return INCOGNITO_PROFILE; 222} 223 224base::FilePath OffTheRecordProfileImpl::GetPath() const { 225 return profile_->GetPath(); 226} 227 228scoped_refptr<base::SequencedTaskRunner> 229OffTheRecordProfileImpl::GetIOTaskRunner() { 230 return profile_->GetIOTaskRunner(); 231} 232 233bool OffTheRecordProfileImpl::IsOffTheRecord() const { 234 return true; 235} 236 237Profile* OffTheRecordProfileImpl::GetOffTheRecordProfile() { 238 return this; 239} 240 241void OffTheRecordProfileImpl::DestroyOffTheRecordProfile() { 242 // Suicide is bad! 243 NOTREACHED(); 244} 245 246bool OffTheRecordProfileImpl::HasOffTheRecordProfile() { 247 return true; 248} 249 250Profile* OffTheRecordProfileImpl::GetOriginalProfile() { 251 return profile_; 252} 253 254ExtensionService* OffTheRecordProfileImpl::GetExtensionService() { 255 return extensions::ExtensionSystem::Get(this)->extension_service(); 256} 257 258ExtensionSpecialStoragePolicy* 259 OffTheRecordProfileImpl::GetExtensionSpecialStoragePolicy() { 260 return GetOriginalProfile()->GetExtensionSpecialStoragePolicy(); 261} 262 263bool OffTheRecordProfileImpl::IsManaged() { 264 return GetOriginalProfile()->IsManaged(); 265} 266 267PrefService* OffTheRecordProfileImpl::GetPrefs() { 268 return prefs_; 269} 270 271PrefService* OffTheRecordProfileImpl::GetOffTheRecordPrefs() { 272 return prefs_; 273} 274 275DownloadManagerDelegate* OffTheRecordProfileImpl::GetDownloadManagerDelegate() { 276 return DownloadServiceFactory::GetForBrowserContext(this)-> 277 GetDownloadManagerDelegate(); 278} 279 280net::URLRequestContextGetter* OffTheRecordProfileImpl::GetRequestContext() { 281 return GetDefaultStoragePartition(this)->GetURLRequestContext(); 282} 283 284net::URLRequestContextGetter* OffTheRecordProfileImpl::CreateRequestContext( 285 content::ProtocolHandlerMap* protocol_handlers, 286 content::ProtocolHandlerScopedVector protocol_interceptors) { 287 return io_data_->CreateMainRequestContextGetter( 288 protocol_handlers, protocol_interceptors.Pass()).get(); 289} 290 291net::URLRequestContextGetter* 292 OffTheRecordProfileImpl::GetRequestContextForRenderProcess( 293 int renderer_child_id) { 294 content::RenderProcessHost* rph = content::RenderProcessHost::FromID( 295 renderer_child_id); 296 return rph->GetStoragePartition()->GetURLRequestContext(); 297} 298 299net::URLRequestContextGetter* 300 OffTheRecordProfileImpl::GetMediaRequestContext() { 301 // In OTR mode, media request context is the same as the original one. 302 return GetRequestContext(); 303} 304 305net::URLRequestContextGetter* 306 OffTheRecordProfileImpl::GetMediaRequestContextForRenderProcess( 307 int renderer_child_id) { 308 // In OTR mode, media request context is the same as the original one. 309 return GetRequestContextForRenderProcess(renderer_child_id); 310} 311 312net::URLRequestContextGetter* 313OffTheRecordProfileImpl::GetMediaRequestContextForStoragePartition( 314 const base::FilePath& partition_path, 315 bool in_memory) { 316 return io_data_->GetIsolatedAppRequestContextGetter(partition_path, in_memory) 317 .get(); 318} 319 320void OffTheRecordProfileImpl::RequestMidiSysExPermission( 321 int render_process_id, 322 int render_view_id, 323 int bridge_id, 324 const GURL& requesting_frame, 325 bool user_gesture, 326 const MidiSysExPermissionCallback& callback) { 327 ChromeMidiPermissionContext* context = 328 ChromeMidiPermissionContextFactory::GetForProfile(this); 329 context->RequestMidiSysExPermission(render_process_id, 330 render_view_id, 331 bridge_id, 332 requesting_frame, 333 user_gesture, 334 callback); 335} 336 337void OffTheRecordProfileImpl::CancelMidiSysExPermissionRequest( 338 int render_process_id, 339 int render_view_id, 340 int bridge_id, 341 const GURL& requesting_frame) { 342 ChromeMidiPermissionContext* context = 343 ChromeMidiPermissionContextFactory::GetForProfile(this); 344 context->CancelMidiSysExPermissionRequest( 345 render_process_id, render_view_id, bridge_id, requesting_frame); 346} 347 348void OffTheRecordProfileImpl::RequestProtectedMediaIdentifierPermission( 349 int render_process_id, 350 int render_view_id, 351 int bridge_id, 352 int group_id, 353 const GURL& requesting_frame, 354 const ProtectedMediaIdentifierPermissionCallback& callback) { 355#if defined(OS_ANDROID) 356 ProtectedMediaIdentifierPermissionContext* context = 357 ProtectedMediaIdentifierPermissionContextFactory::GetForProfile(this); 358 context->RequestProtectedMediaIdentifierPermission(render_process_id, 359 render_view_id, 360 bridge_id, 361 group_id, 362 requesting_frame, 363 callback); 364#else 365 NOTIMPLEMENTED(); 366 callback.Run(false); 367#endif // defined(OS_ANDROID) 368} 369 370void OffTheRecordProfileImpl::CancelProtectedMediaIdentifierPermissionRequests( 371 int group_id) { 372#if defined(OS_ANDROID) 373 ProtectedMediaIdentifierPermissionContext* context = 374 ProtectedMediaIdentifierPermissionContextFactory::GetForProfile(this); 375 context->CancelProtectedMediaIdentifierPermissionRequests(group_id); 376#else 377 NOTIMPLEMENTED(); 378#endif // defined(OS_ANDROID) 379} 380 381net::URLRequestContextGetter* 382 OffTheRecordProfileImpl::GetRequestContextForExtensions() { 383 return io_data_->GetExtensionsRequestContextGetter().get(); 384} 385 386net::URLRequestContextGetter* 387OffTheRecordProfileImpl::CreateRequestContextForStoragePartition( 388 const base::FilePath& partition_path, 389 bool in_memory, 390 content::ProtocolHandlerMap* protocol_handlers, 391 content::ProtocolHandlerScopedVector protocol_interceptors) { 392 return io_data_->CreateIsolatedAppRequestContextGetter( 393 partition_path, 394 in_memory, 395 protocol_handlers, 396 protocol_interceptors.Pass()).get(); 397} 398 399content::ResourceContext* OffTheRecordProfileImpl::GetResourceContext() { 400 return io_data_->GetResourceContext(); 401} 402 403net::SSLConfigService* OffTheRecordProfileImpl::GetSSLConfigService() { 404 return profile_->GetSSLConfigService(); 405} 406 407HostContentSettingsMap* OffTheRecordProfileImpl::GetHostContentSettingsMap() { 408 // Retrieve the host content settings map of the parent profile in order to 409 // ensure the preferences have been migrated. 410 profile_->GetHostContentSettingsMap(); 411 if (!host_content_settings_map_.get()) { 412 host_content_settings_map_ = new HostContentSettingsMap(GetPrefs(), true); 413#if defined(ENABLE_EXTENSIONS) 414 ExtensionService* extension_service = GetExtensionService(); 415 if (extension_service) 416 host_content_settings_map_->RegisterExtensionService(extension_service); 417#endif 418 } 419 return host_content_settings_map_.get(); 420} 421 422content::GeolocationPermissionContext* 423 OffTheRecordProfileImpl::GetGeolocationPermissionContext() { 424 return ChromeGeolocationPermissionContextFactory::GetForProfile(this); 425} 426 427content::BrowserPluginGuestManagerDelegate* 428 OffTheRecordProfileImpl::GetGuestManagerDelegate() { 429 return GuestViewManager::FromBrowserContext(this); 430} 431 432quota::SpecialStoragePolicy* 433 OffTheRecordProfileImpl::GetSpecialStoragePolicy() { 434 return GetExtensionSpecialStoragePolicy(); 435} 436 437bool OffTheRecordProfileImpl::IsSameProfile(Profile* profile) { 438 return (profile == this) || (profile == profile_); 439} 440 441Time OffTheRecordProfileImpl::GetStartTime() const { 442 return start_time_; 443} 444 445history::TopSites* OffTheRecordProfileImpl::GetTopSitesWithoutCreating() { 446 return NULL; 447} 448 449history::TopSites* OffTheRecordProfileImpl::GetTopSites() { 450 return NULL; 451} 452 453void OffTheRecordProfileImpl::SetExitType(ExitType exit_type) { 454} 455 456base::FilePath OffTheRecordProfileImpl::last_selected_directory() { 457 const base::FilePath& directory = last_selected_directory_; 458 if (directory.empty()) { 459 return profile_->last_selected_directory(); 460 } 461 return directory; 462} 463 464void OffTheRecordProfileImpl::set_last_selected_directory( 465 const base::FilePath& path) { 466 last_selected_directory_ = path; 467} 468 469bool OffTheRecordProfileImpl::WasCreatedByVersionOrLater( 470 const std::string& version) { 471 return profile_->WasCreatedByVersionOrLater(version); 472} 473 474Profile::ExitType OffTheRecordProfileImpl::GetLastSessionExitType() { 475 return profile_->GetLastSessionExitType(); 476} 477 478#if defined(OS_CHROMEOS) 479void OffTheRecordProfileImpl::ChangeAppLocale(const std::string& locale, 480 AppLocaleChangedVia) { 481} 482 483void OffTheRecordProfileImpl::OnLogin() { 484} 485 486void OffTheRecordProfileImpl::InitChromeOSPreferences() { 487 // The incognito profile shouldn't have Chrome OS's preferences. 488 // The preferences are associated with the regular user profile. 489} 490#endif // defined(OS_CHROMEOS) 491 492PrefProxyConfigTracker* OffTheRecordProfileImpl::GetProxyConfigTracker() { 493 if (!pref_proxy_config_tracker_) 494 pref_proxy_config_tracker_.reset(CreateProxyConfigTracker()); 495 return pref_proxy_config_tracker_.get(); 496} 497 498chrome_browser_net::Predictor* OffTheRecordProfileImpl::GetNetworkPredictor() { 499 // We do not store information about websites visited in OTR profiles which 500 // is necessary for a Predictor, so we do not have a Predictor at all. 501 return NULL; 502} 503 504void OffTheRecordProfileImpl::ClearNetworkingHistorySince( 505 base::Time time, 506 const base::Closure& completion) { 507 // Nothing to do here, our transport security state is read-only. 508 // Still, fire the callback to indicate we have finished, otherwise the 509 // BrowsingDataRemover will never be destroyed and the dialog will never be 510 // closed. We must do this asynchronously in order to avoid reentrancy issues. 511 if (!completion.is_null()) { 512 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion); 513 } 514} 515 516GURL OffTheRecordProfileImpl::GetHomePage() { 517 return profile_->GetHomePage(); 518} 519 520#if defined(OS_CHROMEOS) 521// Special case of the OffTheRecordProfileImpl which is used while Guest 522// session in CrOS. 523class GuestSessionProfile : public OffTheRecordProfileImpl { 524 public: 525 explicit GuestSessionProfile(Profile* real_profile) 526 : OffTheRecordProfileImpl(real_profile) { 527 } 528 529 virtual ProfileType GetProfileType() const OVERRIDE { 530 return GUEST_PROFILE; 531 } 532 533 virtual void InitChromeOSPreferences() OVERRIDE { 534 chromeos_preferences_.reset(new chromeos::Preferences()); 535 chromeos_preferences_->Init(static_cast<PrefServiceSyncable*>(GetPrefs()), 536 chromeos::UserManager::Get()->GetActiveUser()); 537 } 538 539 private: 540 // The guest user should be able to customize Chrome OS preferences. 541 scoped_ptr<chromeos::Preferences> chromeos_preferences_; 542}; 543#endif 544 545Profile* Profile::CreateOffTheRecordProfile() { 546 OffTheRecordProfileImpl* profile = NULL; 547#if defined(OS_CHROMEOS) 548 if (IsGuestSession()) 549 profile = new GuestSessionProfile(this); 550#endif 551 if (!profile) 552 profile = new OffTheRecordProfileImpl(this); 553 profile->Init(); 554 return profile; 555} 556 557void OffTheRecordProfileImpl::OnZoomLevelChanged( 558 const HostZoomMap::ZoomLevelChange& change) { 559 HostZoomMap* host_zoom_map = HostZoomMap::GetForBrowserContext(this); 560 switch (change.mode) { 561 case HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM: 562 return; 563 case HostZoomMap::ZOOM_CHANGED_FOR_HOST: 564 host_zoom_map->SetZoomLevelForHost(change.host, change.zoom_level); 565 return; 566 case HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST: 567 host_zoom_map->SetZoomLevelForHostAndScheme(change.scheme, 568 change.host, 569 change.zoom_level); 570 return; 571 } 572} 573 574PrefProxyConfigTracker* OffTheRecordProfileImpl::CreateProxyConfigTracker() { 575#if defined(OS_CHROMEOS) 576 if (chromeos::ProfileHelper::IsSigninProfile(this)) { 577 return ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState( 578 g_browser_process->local_state()); 579 } 580#endif // defined(OS_CHROMEOS) 581 return ProxyServiceFactory::CreatePrefProxyConfigTrackerOfProfile( 582 GetPrefs(), g_browser_process->local_state()); 583} 584