[Cronet] Delay StartNetLog and StopNetLog until native request context is initialized
[chromium-blink-merge.git] / chrome / browser / profiles / off_the_record_profile_impl.cc
blob909b894a1d5105663612d55852d55f7c0d60526e
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 #if defined(ENABLE_SUPERVISED_USERS)
84 #include "chrome/browser/content_settings/content_settings_supervised_provider.h"
85 #include "chrome/browser/supervised_user/supervised_user_settings_service.h"
86 #include "chrome/browser/supervised_user/supervised_user_settings_service_factory.h"
87 #endif
89 using content::BrowserThread;
90 using content::DownloadManagerDelegate;
91 using content::HostZoomMap;
93 #if defined(ENABLE_EXTENSIONS)
94 namespace {
96 void NotifyOTRProfileCreatedOnIOThread(void* original_profile,
97 void* otr_profile) {
98 ExtensionWebRequestEventRouter::GetInstance()->OnOTRBrowserContextCreated(
99 original_profile, otr_profile);
102 void NotifyOTRProfileDestroyedOnIOThread(void* original_profile,
103 void* otr_profile) {
104 ExtensionWebRequestEventRouter::GetInstance()->OnOTRBrowserContextDestroyed(
105 original_profile, otr_profile);
108 } // namespace
109 #endif
111 OffTheRecordProfileImpl::OffTheRecordProfileImpl(Profile* real_profile)
112 : profile_(real_profile),
113 prefs_(PrefServiceSyncable::IncognitoFromProfile(real_profile)),
114 start_time_(Time::Now()) {
115 // Register on BrowserContext.
116 user_prefs::UserPrefs::Set(this, prefs_);
119 void OffTheRecordProfileImpl::Init() {
120 // The construction of OffTheRecordProfileIOData::Handle needs the profile
121 // type returned by this->GetProfileType(). Since GetProfileType() is a
122 // virtual member function, we cannot call the function defined in the most
123 // derived class (e.g. GuestSessionProfile) until a ctor finishes. Thus,
124 // we have to instantiate OffTheRecordProfileIOData::Handle here after a ctor.
125 InitIoData();
127 #if defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS)
128 // Because UserCloudPolicyManager is in a component, it cannot access
129 // GetOriginalProfile. Instead, we have to inject this relation here.
130 policy::UserCloudPolicyManagerFactory::RegisterForOffTheRecordBrowserContext(
131 this->GetOriginalProfile(), this);
132 #endif
134 BrowserContextDependencyManager::GetInstance()->CreateBrowserContextServices(
135 this);
137 set_is_guest_profile(
138 profile_->GetPath() == ProfileManager::GetGuestProfilePath());
140 // Guest profiles may always be OTR. Check IncognitoModePrefs otherwise.
141 DCHECK(profile_->IsGuestSession() ||
142 IncognitoModePrefs::GetAvailability(profile_->GetPrefs()) !=
143 IncognitoModePrefs::DISABLED);
145 // TODO(oshima): Remove the need to eagerly initialize the request context
146 // getter. chromeos::OnlineAttempt is illegally trying to access this
147 // Profile member from a thread other than the UI thread, so we need to
148 // prevent a race.
149 #if defined(OS_CHROMEOS)
150 GetRequestContext();
151 #endif // defined(OS_CHROMEOS)
153 TrackZoomLevelsFromParent();
155 #if defined(ENABLE_PLUGINS)
156 ChromePluginServiceFilter::GetInstance()->RegisterResourceContext(
157 PluginPrefs::GetForProfile(this).get(),
158 io_data_->GetResourceContextNoInit());
159 #endif
161 #if defined(ENABLE_EXTENSIONS)
162 // Make the chrome//extension-icon/ resource available.
163 extensions::ExtensionIconSource* icon_source =
164 new extensions::ExtensionIconSource(profile_);
165 content::URLDataSource::Add(this, icon_source);
167 BrowserThread::PostTask(
168 BrowserThread::IO, FROM_HERE,
169 base::Bind(&NotifyOTRProfileCreatedOnIOThread, profile_, this));
170 #endif
172 // The DomDistillerViewerSource is not a normal WebUI so it must be registered
173 // as a URLDataSource early.
174 RegisterDomDistillerViewerSource(this);
177 OffTheRecordProfileImpl::~OffTheRecordProfileImpl() {
178 MaybeSendDestroyedNotification();
180 #if defined(ENABLE_PLUGINS)
181 ChromePluginServiceFilter::GetInstance()->UnregisterResourceContext(
182 io_data_->GetResourceContextNoInit());
183 #endif
185 BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
186 this);
188 #if defined(ENABLE_EXTENSIONS)
189 BrowserThread::PostTask(
190 BrowserThread::IO, FROM_HERE,
191 base::Bind(&NotifyOTRProfileDestroyedOnIOThread, profile_, this));
192 #endif
194 if (host_content_settings_map_.get())
195 host_content_settings_map_->ShutdownOnUIThread();
197 if (pref_proxy_config_tracker_)
198 pref_proxy_config_tracker_->DetachFromPrefService();
200 // Clears any data the network stack contains that may be related to the
201 // OTR session.
202 g_browser_process->io_thread()->ChangedToOnTheRecord();
205 void OffTheRecordProfileImpl::InitIoData() {
206 io_data_.reset(new OffTheRecordProfileIOData::Handle(this));
209 void OffTheRecordProfileImpl::TrackZoomLevelsFromParent() {
210 DCHECK_NE(INCOGNITO_PROFILE, profile_->GetProfileType());
212 // Here we only want to use zoom levels stored in the main-context's default
213 // storage partition. We're not interested in zoom levels in special
214 // partitions, e.g. those used by WebViewGuests.
215 HostZoomMap* host_zoom_map = HostZoomMap::GetDefaultForBrowserContext(this);
216 HostZoomMap* parent_host_zoom_map =
217 HostZoomMap::GetDefaultForBrowserContext(profile_);
218 host_zoom_map->CopyFrom(parent_host_zoom_map);
219 // Observe parent profile's HostZoomMap changes so they can also be applied
220 // to this profile's HostZoomMap.
221 track_zoom_subscription_ = parent_host_zoom_map->AddZoomLevelChangedCallback(
222 base::Bind(&OffTheRecordProfileImpl::OnParentZoomLevelChanged,
223 base::Unretained(this)));
224 if (!profile_->GetZoomLevelPrefs())
225 return;
227 // Also track changes to the parent profile's default zoom level.
228 parent_default_zoom_level_subscription_ =
229 profile_->GetZoomLevelPrefs()->RegisterDefaultZoomLevelCallback(
230 base::Bind(&OffTheRecordProfileImpl::UpdateDefaultZoomLevel,
231 base::Unretained(this)));
234 std::string OffTheRecordProfileImpl::GetProfileUserName() const {
235 // Incognito profile should not return the username.
236 return std::string();
239 Profile::ProfileType OffTheRecordProfileImpl::GetProfileType() const {
240 #if !defined(OS_CHROMEOS)
241 return profile_->IsGuestSession() ? GUEST_PROFILE : INCOGNITO_PROFILE;
242 #else
243 return INCOGNITO_PROFILE;
244 #endif
247 base::FilePath OffTheRecordProfileImpl::GetPath() const {
248 return profile_->GetPath();
251 scoped_ptr<content::ZoomLevelDelegate>
252 OffTheRecordProfileImpl::CreateZoomLevelDelegate(
253 const base::FilePath& partition_path) {
254 return make_scoped_ptr(new chrome::ChromeZoomLevelOTRDelegate(
255 ui_zoom::ZoomEventManager::GetForBrowserContext(this)->GetWeakPtr()));
258 scoped_refptr<base::SequencedTaskRunner>
259 OffTheRecordProfileImpl::GetIOTaskRunner() {
260 return profile_->GetIOTaskRunner();
263 bool OffTheRecordProfileImpl::IsOffTheRecord() const {
264 return true;
267 Profile* OffTheRecordProfileImpl::GetOffTheRecordProfile() {
268 return this;
271 void OffTheRecordProfileImpl::DestroyOffTheRecordProfile() {
272 // Suicide is bad!
273 NOTREACHED();
276 bool OffTheRecordProfileImpl::HasOffTheRecordProfile() {
277 return true;
280 Profile* OffTheRecordProfileImpl::GetOriginalProfile() {
281 return profile_;
284 ExtensionSpecialStoragePolicy*
285 OffTheRecordProfileImpl::GetExtensionSpecialStoragePolicy() {
286 return GetOriginalProfile()->GetExtensionSpecialStoragePolicy();
289 bool OffTheRecordProfileImpl::IsSupervised() {
290 return GetOriginalProfile()->IsSupervised();
293 bool OffTheRecordProfileImpl::IsChild() {
294 // TODO(treib): If we ever allow incognito for child accounts, evaluate
295 // whether we want to just return false here.
296 return GetOriginalProfile()->IsChild();
299 bool OffTheRecordProfileImpl::IsLegacySupervised() {
300 return GetOriginalProfile()->IsLegacySupervised();
303 PrefService* OffTheRecordProfileImpl::GetPrefs() {
304 return prefs_;
307 const PrefService* OffTheRecordProfileImpl::GetPrefs() const {
308 return prefs_;
311 PrefService* OffTheRecordProfileImpl::GetOffTheRecordPrefs() {
312 return prefs_;
315 DownloadManagerDelegate* OffTheRecordProfileImpl::GetDownloadManagerDelegate() {
316 return DownloadServiceFactory::GetForBrowserContext(this)->
317 GetDownloadManagerDelegate();
320 net::URLRequestContextGetter* OffTheRecordProfileImpl::GetRequestContext() {
321 return GetDefaultStoragePartition(this)->GetURLRequestContext();
324 net::URLRequestContextGetter* OffTheRecordProfileImpl::CreateRequestContext(
325 content::ProtocolHandlerMap* protocol_handlers,
326 content::URLRequestInterceptorScopedVector request_interceptors) {
327 return io_data_->CreateMainRequestContextGetter(
328 protocol_handlers, request_interceptors.Pass()).get();
331 net::URLRequestContextGetter*
332 OffTheRecordProfileImpl::GetRequestContextForRenderProcess(
333 int renderer_child_id) {
334 content::RenderProcessHost* rph = content::RenderProcessHost::FromID(
335 renderer_child_id);
336 return rph->GetStoragePartition()->GetURLRequestContext();
339 net::URLRequestContextGetter*
340 OffTheRecordProfileImpl::GetMediaRequestContext() {
341 // In OTR mode, media request context is the same as the original one.
342 return GetRequestContext();
345 net::URLRequestContextGetter*
346 OffTheRecordProfileImpl::GetMediaRequestContextForRenderProcess(
347 int renderer_child_id) {
348 // In OTR mode, media request context is the same as the original one.
349 return GetRequestContextForRenderProcess(renderer_child_id);
352 net::URLRequestContextGetter*
353 OffTheRecordProfileImpl::GetMediaRequestContextForStoragePartition(
354 const base::FilePath& partition_path,
355 bool in_memory) {
356 return io_data_->GetIsolatedAppRequestContextGetter(partition_path, in_memory)
357 .get();
360 net::URLRequestContextGetter*
361 OffTheRecordProfileImpl::GetRequestContextForExtensions() {
362 return io_data_->GetExtensionsRequestContextGetter().get();
365 net::URLRequestContextGetter*
366 OffTheRecordProfileImpl::CreateRequestContextForStoragePartition(
367 const base::FilePath& partition_path,
368 bool in_memory,
369 content::ProtocolHandlerMap* protocol_handlers,
370 content::URLRequestInterceptorScopedVector request_interceptors) {
371 return io_data_->CreateIsolatedAppRequestContextGetter(
372 partition_path,
373 in_memory,
374 protocol_handlers,
375 request_interceptors.Pass()).get();
378 content::ResourceContext* OffTheRecordProfileImpl::GetResourceContext() {
379 return io_data_->GetResourceContext();
382 net::SSLConfigService* OffTheRecordProfileImpl::GetSSLConfigService() {
383 return profile_->GetSSLConfigService();
386 HostContentSettingsMap* OffTheRecordProfileImpl::GetHostContentSettingsMap() {
387 DCHECK_CURRENTLY_ON(BrowserThread::UI);
388 // Retrieve the host content settings map of the parent profile in order to
389 // ensure the preferences have been migrated.
390 profile_->GetHostContentSettingsMap();
391 if (!host_content_settings_map_.get()) {
392 host_content_settings_map_ = new HostContentSettingsMap(GetPrefs(), true);
393 #if defined(ENABLE_EXTENSIONS)
394 ExtensionService* extension_service =
395 extensions::ExtensionSystem::Get(this)->extension_service();
396 if (extension_service) {
397 extension_service->RegisterContentSettings(
398 host_content_settings_map_.get());
400 #endif
401 #if defined(ENABLE_SUPERVISED_USERS)
402 SupervisedUserSettingsService* supervised_service =
403 SupervisedUserSettingsServiceFactory::GetForProfile(this);
404 scoped_ptr<content_settings::SupervisedProvider> supervised_provider(
405 new content_settings::SupervisedProvider(supervised_service));
406 host_content_settings_map_->RegisterProvider(
407 HostContentSettingsMap::SUPERVISED_PROVIDER,
408 supervised_provider.Pass());
409 #endif
411 return host_content_settings_map_.get();
414 content::BrowserPluginGuestManager* OffTheRecordProfileImpl::GetGuestManager() {
415 #if defined(ENABLE_EXTENSIONS)
416 return extensions::GuestViewManager::FromBrowserContext(this);
417 #else
418 return NULL;
419 #endif
422 storage::SpecialStoragePolicy*
423 OffTheRecordProfileImpl::GetSpecialStoragePolicy() {
424 #if defined(ENABLE_EXTENSIONS)
425 return GetExtensionSpecialStoragePolicy();
426 #else
427 return NULL;
428 #endif
431 content::PushMessagingService*
432 OffTheRecordProfileImpl::GetPushMessagingService() {
433 // TODO(johnme): Support push messaging in incognito if possible.
434 return NULL;
437 content::SSLHostStateDelegate*
438 OffTheRecordProfileImpl::GetSSLHostStateDelegate() {
439 return ChromeSSLHostStateDelegateFactory::GetForProfile(this);
442 bool OffTheRecordProfileImpl::IsSameProfile(Profile* profile) {
443 return (profile == this) || (profile == profile_);
446 Time OffTheRecordProfileImpl::GetStartTime() const {
447 return start_time_;
450 void OffTheRecordProfileImpl::SetExitType(ExitType exit_type) {
453 base::FilePath OffTheRecordProfileImpl::last_selected_directory() {
454 const base::FilePath& directory = last_selected_directory_;
455 if (directory.empty()) {
456 return profile_->last_selected_directory();
458 return directory;
461 void OffTheRecordProfileImpl::set_last_selected_directory(
462 const base::FilePath& path) {
463 last_selected_directory_ = path;
466 bool OffTheRecordProfileImpl::WasCreatedByVersionOrLater(
467 const std::string& version) {
468 return profile_->WasCreatedByVersionOrLater(version);
471 Profile::ExitType OffTheRecordProfileImpl::GetLastSessionExitType() {
472 return profile_->GetLastSessionExitType();
475 #if defined(OS_CHROMEOS)
476 void OffTheRecordProfileImpl::ChangeAppLocale(const std::string& locale,
477 AppLocaleChangedVia) {
480 void OffTheRecordProfileImpl::OnLogin() {
483 void OffTheRecordProfileImpl::InitChromeOSPreferences() {
484 // The incognito profile shouldn't have Chrome OS's preferences.
485 // The preferences are associated with the regular user profile.
487 #endif // defined(OS_CHROMEOS)
489 PrefProxyConfigTracker* OffTheRecordProfileImpl::GetProxyConfigTracker() {
490 if (!pref_proxy_config_tracker_)
491 pref_proxy_config_tracker_.reset(CreateProxyConfigTracker());
492 return pref_proxy_config_tracker_.get();
495 chrome_browser_net::Predictor* OffTheRecordProfileImpl::GetNetworkPredictor() {
496 // We do not store information about websites visited in OTR profiles which
497 // is necessary for a Predictor, so we do not have a Predictor at all.
498 return NULL;
501 DevToolsNetworkController*
502 OffTheRecordProfileImpl::GetDevToolsNetworkController() {
503 return io_data_->GetDevToolsNetworkController();
506 void OffTheRecordProfileImpl::ClearNetworkingHistorySince(
507 base::Time time,
508 const base::Closure& completion) {
509 // Nothing to do here, our transport security state is read-only.
510 // Still, fire the callback to indicate we have finished, otherwise the
511 // BrowsingDataRemover will never be destroyed and the dialog will never be
512 // closed. We must do this asynchronously in order to avoid reentrancy issues.
513 if (!completion.is_null()) {
514 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion);
518 GURL OffTheRecordProfileImpl::GetHomePage() {
519 return profile_->GetHomePage();
522 #if defined(OS_CHROMEOS)
523 // Special case of the OffTheRecordProfileImpl which is used while Guest
524 // session in CrOS.
525 class GuestSessionProfile : public OffTheRecordProfileImpl {
526 public:
527 explicit GuestSessionProfile(Profile* real_profile)
528 : OffTheRecordProfileImpl(real_profile) {
529 set_is_guest_profile(true);
532 ProfileType GetProfileType() const override { return GUEST_PROFILE; }
534 void InitChromeOSPreferences() override {
535 chromeos_preferences_.reset(new chromeos::Preferences());
536 chromeos_preferences_->Init(
537 this, user_manager::UserManager::Get()->GetActiveUser());
540 private:
541 // The guest user should be able to customize Chrome OS preferences.
542 scoped_ptr<chromeos::Preferences> chromeos_preferences_;
544 #endif
546 Profile* Profile::CreateOffTheRecordProfile() {
547 OffTheRecordProfileImpl* profile = NULL;
548 #if defined(OS_CHROMEOS)
549 if (IsGuestSession())
550 profile = new GuestSessionProfile(this);
551 #endif
552 if (!profile)
553 profile = new OffTheRecordProfileImpl(this);
554 profile->Init();
555 return profile;
558 void OffTheRecordProfileImpl::OnParentZoomLevelChanged(
559 const HostZoomMap::ZoomLevelChange& change) {
560 HostZoomMap* host_zoom_map = HostZoomMap::GetDefaultForBrowserContext(this);
561 switch (change.mode) {
562 case HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM:
563 return;
564 case HostZoomMap::ZOOM_CHANGED_FOR_HOST:
565 host_zoom_map->SetZoomLevelForHost(change.host, change.zoom_level);
566 return;
567 case HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST:
568 host_zoom_map->SetZoomLevelForHostAndScheme(change.scheme,
569 change.host,
570 change.zoom_level);
571 return;
572 case HostZoomMap::PAGE_SCALE_IS_ONE_CHANGED:
573 return;
577 void OffTheRecordProfileImpl::UpdateDefaultZoomLevel() {
578 HostZoomMap* host_zoom_map = HostZoomMap::GetDefaultForBrowserContext(this);
579 double default_zoom_level =
580 profile_->GetZoomLevelPrefs()->GetDefaultZoomLevelPref();
581 host_zoom_map->SetDefaultZoomLevel(default_zoom_level);
584 PrefProxyConfigTracker* OffTheRecordProfileImpl::CreateProxyConfigTracker() {
585 #if defined(OS_CHROMEOS)
586 if (chromeos::ProfileHelper::IsSigninProfile(this)) {
587 return ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
588 g_browser_process->local_state());
590 #endif // defined(OS_CHROMEOS)
591 return ProxyServiceFactory::CreatePrefProxyConfigTrackerOfProfile(
592 GetPrefs(), g_browser_process->local_state());