Add more checks to investigate SupervisedUserPrefStore crash at startup.
[chromium-blink-merge.git] / chrome / browser / profiles / off_the_record_profile_impl.cc
blobe998fafac2cd26ca34d3e5f928523738829ceac1
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/profiles/off_the_record_profile_impl.h"
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/prefs/json_pref_store.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h"
15 #include "build/build_config.h"
16 #include "chrome/browser/background/background_contents_service_factory.h"
17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/dom_distiller/profile_utils.h"
19 #include "chrome/browser/download/chrome_download_manager_delegate.h"
20 #include "chrome/browser/download/download_service.h"
21 #include "chrome/browser/download/download_service_factory.h"
22 #include "chrome/browser/io_thread.h"
23 #include "chrome/browser/net/chrome_url_request_context_getter.h"
24 #include "chrome/browser/net/pref_proxy_config_tracker.h"
25 #include "chrome/browser/net/proxy_service_factory.h"
26 #include "chrome/browser/plugins/chrome_plugin_service_filter.h"
27 #include "chrome/browser/plugins/plugin_prefs.h"
28 #include "chrome/browser/prefs/incognito_mode_prefs.h"
29 #include "chrome/browser/prefs/pref_service_syncable.h"
30 #include "chrome/browser/profiles/profile_manager.h"
31 #include "chrome/browser/ssl/chrome_ssl_host_state_delegate.h"
32 #include "chrome/browser/ssl/chrome_ssl_host_state_delegate_factory.h"
33 #include "chrome/browser/themes/theme_service.h"
34 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
35 #include "chrome/browser/ui/zoom/chrome_zoom_level_otr_delegate.h"
36 #include "chrome/common/chrome_constants.h"
37 #include "chrome/common/chrome_paths.h"
38 #include "chrome/common/chrome_switches.h"
39 #include "chrome/common/pref_names.h"
40 #include "chrome/common/render_messages.h"
41 #include "components/content_settings/core/browser/host_content_settings_map.h"
42 #include "components/keyed_service/content/browser_context_dependency_manager.h"
43 #include "components/ui/zoom/zoom_event_manager.h"
44 #include "components/user_prefs/user_prefs.h"
45 #include "content/public/browser/browser_thread.h"
46 #include "content/public/browser/host_zoom_map.h"
47 #include "content/public/browser/render_process_host.h"
48 #include "content/public/browser/storage_partition.h"
49 #include "content/public/browser/url_data_source.h"
50 #include "content/public/browser/web_contents.h"
51 #include "net/http/http_server_properties.h"
52 #include "net/http/transport_security_state.h"
53 #include "storage/browser/database/database_tracker.h"
55 #if defined(OS_ANDROID)
56 #include "chrome/browser/media/protected_media_identifier_permission_context.h"
57 #include "chrome/browser/media/protected_media_identifier_permission_context_factory.h"
58 #endif // defined(OS_ANDROID)
60 #if defined(OS_ANDROID) || defined(OS_IOS)
61 #include "base/prefs/scoped_user_pref_update.h"
62 #include "chrome/browser/prefs/proxy_prefs.h"
63 #endif // defined(OS_ANDROID) || defined(OS_IOS)
65 #if defined(OS_CHROMEOS)
66 #include "chrome/browser/chromeos/preferences.h"
67 #include "chrome/browser/chromeos/profiles/profile_helper.h"
68 #endif
70 #if defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS)
71 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
72 #endif
74 #if defined(ENABLE_EXTENSIONS)
75 #include "chrome/browser/extensions/extension_service.h"
76 #include "chrome/browser/extensions/extension_special_storage_policy.h"
77 #include "extensions/browser/api/web_request/web_request_api.h"
78 #include "extensions/browser/extension_system.h"
79 #include "extensions/browser/guest_view/guest_view_manager.h"
80 #include "extensions/common/extension.h"
81 #endif
83 using content::BrowserThread;
84 using content::DownloadManagerDelegate;
85 using content::HostZoomMap;
87 #if defined(ENABLE_EXTENSIONS)
88 namespace {
90 void NotifyOTRProfileCreatedOnIOThread(void* original_profile,
91 void* otr_profile) {
92 ExtensionWebRequestEventRouter::GetInstance()->OnOTRBrowserContextCreated(
93 original_profile, otr_profile);
96 void NotifyOTRProfileDestroyedOnIOThread(void* original_profile,
97 void* otr_profile) {
98 ExtensionWebRequestEventRouter::GetInstance()->OnOTRBrowserContextDestroyed(
99 original_profile, otr_profile);
102 } // namespace
103 #endif
105 OffTheRecordProfileImpl::OffTheRecordProfileImpl(Profile* real_profile)
106 : profile_(real_profile),
107 prefs_(PrefServiceSyncable::IncognitoFromProfile(real_profile)),
108 start_time_(Time::Now()) {
109 // Register on BrowserContext.
110 user_prefs::UserPrefs::Set(this, prefs_);
113 void OffTheRecordProfileImpl::Init() {
114 // The construction of OffTheRecordProfileIOData::Handle needs the profile
115 // type returned by this->GetProfileType(). Since GetProfileType() is a
116 // virtual member function, we cannot call the function defined in the most
117 // derived class (e.g. GuestSessionProfile) until a ctor finishes. Thus,
118 // we have to instantiate OffTheRecordProfileIOData::Handle here after a ctor.
119 InitIoData();
121 #if defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS)
122 // Because UserCloudPolicyManager is in a component, it cannot access
123 // GetOriginalProfile. Instead, we have to inject this relation here.
124 policy::UserCloudPolicyManagerFactory::RegisterForOffTheRecordBrowserContext(
125 this->GetOriginalProfile(), this);
126 #endif
128 BrowserContextDependencyManager::GetInstance()->CreateBrowserContextServices(
129 this);
131 set_is_guest_profile(
132 profile_->GetPath() == ProfileManager::GetGuestProfilePath());
134 // Guest profiles may always be OTR. Check IncognitoModePrefs otherwise.
135 DCHECK(profile_->IsGuestSession() ||
136 IncognitoModePrefs::GetAvailability(profile_->GetPrefs()) !=
137 IncognitoModePrefs::DISABLED);
139 // TODO(oshima): Remove the need to eagerly initialize the request context
140 // getter. chromeos::OnlineAttempt is illegally trying to access this
141 // Profile member from a thread other than the UI thread, so we need to
142 // prevent a race.
143 #if defined(OS_CHROMEOS)
144 GetRequestContext();
145 #endif // defined(OS_CHROMEOS)
147 TrackZoomLevelsFromParent();
149 #if defined(ENABLE_PLUGINS)
150 ChromePluginServiceFilter::GetInstance()->RegisterResourceContext(
151 PluginPrefs::GetForProfile(this).get(),
152 io_data_->GetResourceContextNoInit());
153 #endif
155 #if defined(ENABLE_EXTENSIONS)
156 // Make the chrome//extension-icon/ resource available.
157 extensions::ExtensionIconSource* icon_source =
158 new extensions::ExtensionIconSource(profile_);
159 content::URLDataSource::Add(this, icon_source);
161 BrowserThread::PostTask(
162 BrowserThread::IO, FROM_HERE,
163 base::Bind(&NotifyOTRProfileCreatedOnIOThread, profile_, this));
164 #endif
166 // The DomDistillerViewerSource is not a normal WebUI so it must be registered
167 // as a URLDataSource early.
168 RegisterDomDistillerViewerSource(this);
171 OffTheRecordProfileImpl::~OffTheRecordProfileImpl() {
172 MaybeSendDestroyedNotification();
174 #if defined(ENABLE_PLUGINS)
175 ChromePluginServiceFilter::GetInstance()->UnregisterResourceContext(
176 io_data_->GetResourceContextNoInit());
177 #endif
179 BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
180 this);
182 #if defined(ENABLE_EXTENSIONS)
183 BrowserThread::PostTask(
184 BrowserThread::IO, FROM_HERE,
185 base::Bind(&NotifyOTRProfileDestroyedOnIOThread, profile_, this));
186 #endif
188 if (host_content_settings_map_.get())
189 host_content_settings_map_->ShutdownOnUIThread();
191 if (pref_proxy_config_tracker_)
192 pref_proxy_config_tracker_->DetachFromPrefService();
194 // Clears any data the network stack contains that may be related to the
195 // OTR session.
196 g_browser_process->io_thread()->ChangedToOnTheRecord();
199 void OffTheRecordProfileImpl::InitIoData() {
200 io_data_.reset(new OffTheRecordProfileIOData::Handle(this));
203 void OffTheRecordProfileImpl::TrackZoomLevelsFromParent() {
204 DCHECK_NE(INCOGNITO_PROFILE, profile_->GetProfileType());
206 // Here we only want to use zoom levels stored in the main-context's default
207 // storage partition. We're not interested in zoom levels in special
208 // partitions, e.g. those used by WebViewGuests.
209 HostZoomMap* host_zoom_map = HostZoomMap::GetDefaultForBrowserContext(this);
210 HostZoomMap* parent_host_zoom_map =
211 HostZoomMap::GetDefaultForBrowserContext(profile_);
212 host_zoom_map->CopyFrom(parent_host_zoom_map);
213 // Observe parent profile's HostZoomMap changes so they can also be applied
214 // to this profile's HostZoomMap.
215 track_zoom_subscription_ = parent_host_zoom_map->AddZoomLevelChangedCallback(
216 base::Bind(&OffTheRecordProfileImpl::OnParentZoomLevelChanged,
217 base::Unretained(this)));
218 if (!profile_->GetZoomLevelPrefs())
219 return;
221 // Also track changes to the parent profile's default zoom level.
222 parent_default_zoom_level_subscription_ =
223 profile_->GetZoomLevelPrefs()->RegisterDefaultZoomLevelCallback(
224 base::Bind(&OffTheRecordProfileImpl::UpdateDefaultZoomLevel,
225 base::Unretained(this)));
228 std::string OffTheRecordProfileImpl::GetProfileUserName() const {
229 // Incognito profile should not return the username.
230 return std::string();
233 Profile::ProfileType OffTheRecordProfileImpl::GetProfileType() const {
234 #if !defined(OS_CHROMEOS)
235 return profile_->IsGuestSession() ? GUEST_PROFILE : INCOGNITO_PROFILE;
236 #else
237 return INCOGNITO_PROFILE;
238 #endif
241 base::FilePath OffTheRecordProfileImpl::GetPath() const {
242 return profile_->GetPath();
245 scoped_ptr<content::ZoomLevelDelegate>
246 OffTheRecordProfileImpl::CreateZoomLevelDelegate(
247 const base::FilePath& partition_path) {
248 return make_scoped_ptr(new chrome::ChromeZoomLevelOTRDelegate(
249 ui_zoom::ZoomEventManager::GetForBrowserContext(this)->GetWeakPtr()));
252 scoped_refptr<base::SequencedTaskRunner>
253 OffTheRecordProfileImpl::GetIOTaskRunner() {
254 return profile_->GetIOTaskRunner();
257 bool OffTheRecordProfileImpl::IsOffTheRecord() const {
258 return true;
261 Profile* OffTheRecordProfileImpl::GetOffTheRecordProfile() {
262 return this;
265 void OffTheRecordProfileImpl::DestroyOffTheRecordProfile() {
266 // Suicide is bad!
267 NOTREACHED();
270 bool OffTheRecordProfileImpl::HasOffTheRecordProfile() {
271 return true;
274 Profile* OffTheRecordProfileImpl::GetOriginalProfile() {
275 return profile_;
278 ExtensionSpecialStoragePolicy*
279 OffTheRecordProfileImpl::GetExtensionSpecialStoragePolicy() {
280 return GetOriginalProfile()->GetExtensionSpecialStoragePolicy();
283 bool OffTheRecordProfileImpl::IsSupervised() {
284 return GetOriginalProfile()->IsSupervised();
287 bool OffTheRecordProfileImpl::IsChild() {
288 // TODO(treib): If we ever allow incognito for child accounts, evaluate
289 // whether we want to just return false here.
290 return GetOriginalProfile()->IsChild();
293 bool OffTheRecordProfileImpl::IsLegacySupervised() {
294 return GetOriginalProfile()->IsLegacySupervised();
297 PrefService* OffTheRecordProfileImpl::GetPrefs() {
298 return prefs_;
301 const PrefService* OffTheRecordProfileImpl::GetPrefs() const {
302 return prefs_;
305 PrefService* OffTheRecordProfileImpl::GetOffTheRecordPrefs() {
306 return prefs_;
309 DownloadManagerDelegate* OffTheRecordProfileImpl::GetDownloadManagerDelegate() {
310 return DownloadServiceFactory::GetForBrowserContext(this)->
311 GetDownloadManagerDelegate();
314 net::URLRequestContextGetter* OffTheRecordProfileImpl::GetRequestContext() {
315 return GetDefaultStoragePartition(this)->GetURLRequestContext();
318 net::URLRequestContextGetter* OffTheRecordProfileImpl::CreateRequestContext(
319 content::ProtocolHandlerMap* protocol_handlers,
320 content::URLRequestInterceptorScopedVector request_interceptors) {
321 return io_data_->CreateMainRequestContextGetter(
322 protocol_handlers, request_interceptors.Pass()).get();
325 net::URLRequestContextGetter*
326 OffTheRecordProfileImpl::GetRequestContextForRenderProcess(
327 int renderer_child_id) {
328 content::RenderProcessHost* rph = content::RenderProcessHost::FromID(
329 renderer_child_id);
330 return rph->GetStoragePartition()->GetURLRequestContext();
333 net::URLRequestContextGetter*
334 OffTheRecordProfileImpl::GetMediaRequestContext() {
335 // In OTR mode, media request context is the same as the original one.
336 return GetRequestContext();
339 net::URLRequestContextGetter*
340 OffTheRecordProfileImpl::GetMediaRequestContextForRenderProcess(
341 int renderer_child_id) {
342 // In OTR mode, media request context is the same as the original one.
343 return GetRequestContextForRenderProcess(renderer_child_id);
346 net::URLRequestContextGetter*
347 OffTheRecordProfileImpl::GetMediaRequestContextForStoragePartition(
348 const base::FilePath& partition_path,
349 bool in_memory) {
350 return io_data_->GetIsolatedAppRequestContextGetter(partition_path, in_memory)
351 .get();
354 net::URLRequestContextGetter*
355 OffTheRecordProfileImpl::GetRequestContextForExtensions() {
356 return io_data_->GetExtensionsRequestContextGetter().get();
359 net::URLRequestContextGetter*
360 OffTheRecordProfileImpl::CreateRequestContextForStoragePartition(
361 const base::FilePath& partition_path,
362 bool in_memory,
363 content::ProtocolHandlerMap* protocol_handlers,
364 content::URLRequestInterceptorScopedVector request_interceptors) {
365 return io_data_->CreateIsolatedAppRequestContextGetter(
366 partition_path,
367 in_memory,
368 protocol_handlers,
369 request_interceptors.Pass()).get();
372 content::ResourceContext* OffTheRecordProfileImpl::GetResourceContext() {
373 return io_data_->GetResourceContext();
376 net::SSLConfigService* OffTheRecordProfileImpl::GetSSLConfigService() {
377 return profile_->GetSSLConfigService();
380 HostContentSettingsMap* OffTheRecordProfileImpl::GetHostContentSettingsMap() {
381 DCHECK_CURRENTLY_ON(BrowserThread::UI);
382 // Retrieve the host content settings map of the parent profile in order to
383 // ensure the preferences have been migrated.
384 profile_->GetHostContentSettingsMap();
385 if (!host_content_settings_map_.get()) {
386 host_content_settings_map_ = new HostContentSettingsMap(GetPrefs(), true);
387 #if defined(ENABLE_EXTENSIONS)
388 ExtensionService* extension_service =
389 extensions::ExtensionSystem::Get(this)->extension_service();
390 if (extension_service) {
391 extension_service->RegisterContentSettings(
392 host_content_settings_map_.get());
394 #endif
396 return host_content_settings_map_.get();
399 content::BrowserPluginGuestManager* OffTheRecordProfileImpl::GetGuestManager() {
400 #if defined(ENABLE_EXTENSIONS)
401 return extensions::GuestViewManager::FromBrowserContext(this);
402 #else
403 return NULL;
404 #endif
407 storage::SpecialStoragePolicy*
408 OffTheRecordProfileImpl::GetSpecialStoragePolicy() {
409 #if defined(ENABLE_EXTENSIONS)
410 return GetExtensionSpecialStoragePolicy();
411 #else
412 return NULL;
413 #endif
416 content::PushMessagingService*
417 OffTheRecordProfileImpl::GetPushMessagingService() {
418 // TODO(johnme): Support push messaging in incognito if possible.
419 return NULL;
422 content::SSLHostStateDelegate*
423 OffTheRecordProfileImpl::GetSSLHostStateDelegate() {
424 return ChromeSSLHostStateDelegateFactory::GetForProfile(this);
427 bool OffTheRecordProfileImpl::IsSameProfile(Profile* profile) {
428 return (profile == this) || (profile == profile_);
431 Time OffTheRecordProfileImpl::GetStartTime() const {
432 return start_time_;
435 void OffTheRecordProfileImpl::SetExitType(ExitType exit_type) {
438 base::FilePath OffTheRecordProfileImpl::last_selected_directory() {
439 const base::FilePath& directory = last_selected_directory_;
440 if (directory.empty()) {
441 return profile_->last_selected_directory();
443 return directory;
446 void OffTheRecordProfileImpl::set_last_selected_directory(
447 const base::FilePath& path) {
448 last_selected_directory_ = path;
451 bool OffTheRecordProfileImpl::WasCreatedByVersionOrLater(
452 const std::string& version) {
453 return profile_->WasCreatedByVersionOrLater(version);
456 Profile::ExitType OffTheRecordProfileImpl::GetLastSessionExitType() {
457 return profile_->GetLastSessionExitType();
460 #if defined(OS_CHROMEOS)
461 void OffTheRecordProfileImpl::ChangeAppLocale(const std::string& locale,
462 AppLocaleChangedVia) {
465 void OffTheRecordProfileImpl::OnLogin() {
468 void OffTheRecordProfileImpl::InitChromeOSPreferences() {
469 // The incognito profile shouldn't have Chrome OS's preferences.
470 // The preferences are associated with the regular user profile.
472 #endif // defined(OS_CHROMEOS)
474 PrefProxyConfigTracker* OffTheRecordProfileImpl::GetProxyConfigTracker() {
475 if (!pref_proxy_config_tracker_)
476 pref_proxy_config_tracker_.reset(CreateProxyConfigTracker());
477 return pref_proxy_config_tracker_.get();
480 chrome_browser_net::Predictor* OffTheRecordProfileImpl::GetNetworkPredictor() {
481 // We do not store information about websites visited in OTR profiles which
482 // is necessary for a Predictor, so we do not have a Predictor at all.
483 return NULL;
486 DevToolsNetworkController*
487 OffTheRecordProfileImpl::GetDevToolsNetworkController() {
488 return io_data_->GetDevToolsNetworkController();
491 void OffTheRecordProfileImpl::ClearNetworkingHistorySince(
492 base::Time time,
493 const base::Closure& completion) {
494 // Nothing to do here, our transport security state is read-only.
495 // Still, fire the callback to indicate we have finished, otherwise the
496 // BrowsingDataRemover will never be destroyed and the dialog will never be
497 // closed. We must do this asynchronously in order to avoid reentrancy issues.
498 if (!completion.is_null()) {
499 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion);
503 GURL OffTheRecordProfileImpl::GetHomePage() {
504 return profile_->GetHomePage();
507 #if defined(OS_CHROMEOS)
508 // Special case of the OffTheRecordProfileImpl which is used while Guest
509 // session in CrOS.
510 class GuestSessionProfile : public OffTheRecordProfileImpl {
511 public:
512 explicit GuestSessionProfile(Profile* real_profile)
513 : OffTheRecordProfileImpl(real_profile) {
514 set_is_guest_profile(true);
517 ProfileType GetProfileType() const override { return GUEST_PROFILE; }
519 void InitChromeOSPreferences() override {
520 chromeos_preferences_.reset(new chromeos::Preferences());
521 chromeos_preferences_->Init(
522 this, user_manager::UserManager::Get()->GetActiveUser());
525 private:
526 // The guest user should be able to customize Chrome OS preferences.
527 scoped_ptr<chromeos::Preferences> chromeos_preferences_;
529 #endif
531 Profile* Profile::CreateOffTheRecordProfile() {
532 OffTheRecordProfileImpl* profile = NULL;
533 #if defined(OS_CHROMEOS)
534 if (IsGuestSession())
535 profile = new GuestSessionProfile(this);
536 #endif
537 if (!profile)
538 profile = new OffTheRecordProfileImpl(this);
539 profile->Init();
540 return profile;
543 void OffTheRecordProfileImpl::OnParentZoomLevelChanged(
544 const HostZoomMap::ZoomLevelChange& change) {
545 HostZoomMap* host_zoom_map = HostZoomMap::GetDefaultForBrowserContext(this);
546 switch (change.mode) {
547 case HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM:
548 return;
549 case HostZoomMap::ZOOM_CHANGED_FOR_HOST:
550 host_zoom_map->SetZoomLevelForHost(change.host, change.zoom_level);
551 return;
552 case HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST:
553 host_zoom_map->SetZoomLevelForHostAndScheme(change.scheme,
554 change.host,
555 change.zoom_level);
556 return;
560 void OffTheRecordProfileImpl::UpdateDefaultZoomLevel() {
561 HostZoomMap* host_zoom_map = HostZoomMap::GetDefaultForBrowserContext(this);
562 double default_zoom_level =
563 profile_->GetZoomLevelPrefs()->GetDefaultZoomLevelPref();
564 host_zoom_map->SetDefaultZoomLevel(default_zoom_level);
567 PrefProxyConfigTracker* OffTheRecordProfileImpl::CreateProxyConfigTracker() {
568 #if defined(OS_CHROMEOS)
569 if (chromeos::ProfileHelper::IsSigninProfile(this)) {
570 return ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
571 g_browser_process->local_state());
573 #endif // defined(OS_CHROMEOS)
574 return ProxyServiceFactory::CreatePrefProxyConfigTrackerOfProfile(
575 GetPrefs(), g_browser_process->local_state());