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"
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/content_settings/host_content_settings_map_factory.h"
19 #include "chrome/browser/dom_distiller/profile_utils.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/io_thread.h"
24 #include "chrome/browser/net/chrome_url_request_context_getter.h"
25 #include "chrome/browser/net/proxy_service_factory.h"
26 #include "chrome/browser/permissions/permission_manager.h"
27 #include "chrome/browser/permissions/permission_manager_factory.h"
28 #include "chrome/browser/plugins/chrome_plugin_service_filter.h"
29 #include "chrome/browser/plugins/plugin_prefs.h"
30 #include "chrome/browser/prefs/incognito_mode_prefs.h"
31 #include "chrome/browser/prefs/pref_service_syncable.h"
32 #include "chrome/browser/profiles/profile_manager.h"
33 #include "chrome/browser/ssl/chrome_ssl_host_state_delegate.h"
34 #include "chrome/browser/ssl/chrome_ssl_host_state_delegate_factory.h"
35 #include "chrome/browser/themes/theme_service.h"
36 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
37 #include "chrome/browser/ui/zoom/chrome_zoom_level_otr_delegate.h"
38 #include "chrome/common/chrome_constants.h"
39 #include "chrome/common/chrome_paths.h"
40 #include "chrome/common/chrome_switches.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/proxy_config/pref_proxy_config_tracker.h"
44 #include "components/ui/zoom/zoom_event_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 "storage/browser/database/database_tracker.h"
56 #if defined(OS_ANDROID)
57 #include "chrome/browser/media/protected_media_identifier_permission_context.h"
58 #include "chrome/browser/media/protected_media_identifier_permission_context_factory.h"
59 #endif // defined(OS_ANDROID)
61 #if defined(OS_ANDROID) || defined(OS_IOS)
62 #include "base/prefs/scoped_user_pref_update.h"
63 #include "components/proxy_config/proxy_prefs.h"
64 #endif // defined(OS_ANDROID) || defined(OS_IOS)
66 #if defined(OS_CHROMEOS)
67 #include "chrome/browser/chromeos/preferences.h"
68 #include "chrome/browser/chromeos/profiles/profile_helper.h"
71 #if defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS)
72 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
75 #if defined(ENABLE_EXTENSIONS)
76 #include "chrome/browser/extensions/extension_service.h"
77 #include "chrome/browser/extensions/extension_special_storage_policy.h"
78 #include "components/guest_view/browser/guest_view_manager.h"
79 #include "extensions/browser/api/web_request/web_request_api.h"
80 #include "extensions/browser/extension_system.h"
81 #include "extensions/common/extension.h"
84 #if defined(ENABLE_SUPERVISED_USERS)
85 #include "chrome/browser/content_settings/content_settings_supervised_provider.h"
86 #include "chrome/browser/supervised_user/supervised_user_settings_service.h"
87 #include "chrome/browser/supervised_user/supervised_user_settings_service_factory.h"
90 using content::BrowserThread
;
91 using content::DownloadManagerDelegate
;
92 using content::HostZoomMap
;
94 #if defined(ENABLE_EXTENSIONS)
97 void NotifyOTRProfileCreatedOnIOThread(void* original_profile
,
99 extensions::ExtensionWebRequestEventRouter::GetInstance()
100 ->OnOTRBrowserContextCreated(original_profile
, otr_profile
);
103 void NotifyOTRProfileDestroyedOnIOThread(void* original_profile
,
105 extensions::ExtensionWebRequestEventRouter::GetInstance()
106 ->OnOTRBrowserContextDestroyed(original_profile
, otr_profile
);
112 OffTheRecordProfileImpl::OffTheRecordProfileImpl(Profile
* real_profile
)
113 : profile_(real_profile
),
114 prefs_(PrefServiceSyncable::IncognitoFromProfile(real_profile
)),
115 start_time_(Time::Now()) {
116 // Register on BrowserContext.
117 user_prefs::UserPrefs::Set(this, prefs_
);
120 void OffTheRecordProfileImpl::Init() {
121 // The construction of OffTheRecordProfileIOData::Handle needs the profile
122 // type returned by this->GetProfileType(). Since GetProfileType() is a
123 // virtual member function, we cannot call the function defined in the most
124 // derived class (e.g. GuestSessionProfile) until a ctor finishes. Thus,
125 // we have to instantiate OffTheRecordProfileIOData::Handle here after a ctor.
128 #if defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS)
129 // Because UserCloudPolicyManager is in a component, it cannot access
130 // GetOriginalProfile. Instead, we have to inject this relation here.
131 policy::UserCloudPolicyManagerFactory::RegisterForOffTheRecordBrowserContext(
132 this->GetOriginalProfile(), this);
135 BrowserContextDependencyManager::GetInstance()->CreateBrowserContextServices(
138 set_is_guest_profile(
139 profile_
->GetPath() == ProfileManager::GetGuestProfilePath());
141 // Guest profiles may always be OTR. Check IncognitoModePrefs otherwise.
142 DCHECK(profile_
->IsGuestSession() ||
143 IncognitoModePrefs::GetAvailability(profile_
->GetPrefs()) !=
144 IncognitoModePrefs::DISABLED
);
146 TrackZoomLevelsFromParent();
148 #if defined(ENABLE_PLUGINS)
149 ChromePluginServiceFilter::GetInstance()->RegisterResourceContext(
150 PluginPrefs::GetForProfile(this).get(),
151 io_data_
->GetResourceContextNoInit());
154 #if defined(ENABLE_EXTENSIONS)
155 // Make the chrome//extension-icon/ resource available.
156 extensions::ExtensionIconSource
* icon_source
=
157 new extensions::ExtensionIconSource(profile_
);
158 content::URLDataSource::Add(this, icon_source
);
160 BrowserThread::PostTask(
161 BrowserThread::IO
, FROM_HERE
,
162 base::Bind(&NotifyOTRProfileCreatedOnIOThread
, profile_
, this));
165 // The DomDistillerViewerSource is not a normal WebUI so it must be registered
166 // as a URLDataSource early.
167 RegisterDomDistillerViewerSource(this);
170 OffTheRecordProfileImpl::~OffTheRecordProfileImpl() {
171 MaybeSendDestroyedNotification();
173 #if defined(ENABLE_PLUGINS)
174 ChromePluginServiceFilter::GetInstance()->UnregisterResourceContext(
175 io_data_
->GetResourceContextNoInit());
178 BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
181 #if defined(ENABLE_EXTENSIONS)
182 BrowserThread::PostTask(
183 BrowserThread::IO
, FROM_HERE
,
184 base::Bind(&NotifyOTRProfileDestroyedOnIOThread
, profile_
, this));
187 if (pref_proxy_config_tracker_
)
188 pref_proxy_config_tracker_
->DetachFromPrefService();
190 // Clears any data the network stack contains that may be related to the
192 g_browser_process
->io_thread()->ChangedToOnTheRecord();
195 void OffTheRecordProfileImpl::InitIoData() {
196 io_data_
.reset(new OffTheRecordProfileIOData::Handle(this));
199 void OffTheRecordProfileImpl::TrackZoomLevelsFromParent() {
200 DCHECK_NE(INCOGNITO_PROFILE
, profile_
->GetProfileType());
202 // Here we only want to use zoom levels stored in the main-context's default
203 // storage partition. We're not interested in zoom levels in special
204 // partitions, e.g. those used by WebViewGuests.
205 HostZoomMap
* host_zoom_map
= HostZoomMap::GetDefaultForBrowserContext(this);
206 HostZoomMap
* parent_host_zoom_map
=
207 HostZoomMap::GetDefaultForBrowserContext(profile_
);
208 host_zoom_map
->CopyFrom(parent_host_zoom_map
);
209 // Observe parent profile's HostZoomMap changes so they can also be applied
210 // to this profile's HostZoomMap.
211 track_zoom_subscription_
= parent_host_zoom_map
->AddZoomLevelChangedCallback(
212 base::Bind(&OffTheRecordProfileImpl::OnParentZoomLevelChanged
,
213 base::Unretained(this)));
214 if (!profile_
->GetZoomLevelPrefs())
217 // Also track changes to the parent profile's default zoom level.
218 parent_default_zoom_level_subscription_
=
219 profile_
->GetZoomLevelPrefs()->RegisterDefaultZoomLevelCallback(
220 base::Bind(&OffTheRecordProfileImpl::UpdateDefaultZoomLevel
,
221 base::Unretained(this)));
224 std::string
OffTheRecordProfileImpl::GetProfileUserName() const {
225 // Incognito profile should not return the username.
226 return std::string();
229 Profile::ProfileType
OffTheRecordProfileImpl::GetProfileType() const {
230 #if !defined(OS_CHROMEOS)
231 return profile_
->IsGuestSession() ? GUEST_PROFILE
: INCOGNITO_PROFILE
;
233 return INCOGNITO_PROFILE
;
237 base::FilePath
OffTheRecordProfileImpl::GetPath() const {
238 return profile_
->GetPath();
241 scoped_ptr
<content::ZoomLevelDelegate
>
242 OffTheRecordProfileImpl::CreateZoomLevelDelegate(
243 const base::FilePath
& partition_path
) {
244 return make_scoped_ptr(new chrome::ChromeZoomLevelOTRDelegate(
245 ui_zoom::ZoomEventManager::GetForBrowserContext(this)->GetWeakPtr()));
248 scoped_refptr
<base::SequencedTaskRunner
>
249 OffTheRecordProfileImpl::GetIOTaskRunner() {
250 return profile_
->GetIOTaskRunner();
253 bool OffTheRecordProfileImpl::IsOffTheRecord() const {
257 Profile
* OffTheRecordProfileImpl::GetOffTheRecordProfile() {
261 void OffTheRecordProfileImpl::DestroyOffTheRecordProfile() {
266 bool OffTheRecordProfileImpl::HasOffTheRecordProfile() {
270 Profile
* OffTheRecordProfileImpl::GetOriginalProfile() {
274 ExtensionSpecialStoragePolicy
*
275 OffTheRecordProfileImpl::GetExtensionSpecialStoragePolicy() {
276 return GetOriginalProfile()->GetExtensionSpecialStoragePolicy();
279 bool OffTheRecordProfileImpl::IsSupervised() const {
280 return profile_
->IsSupervised();
283 bool OffTheRecordProfileImpl::IsChild() const {
284 // TODO(treib): If we ever allow incognito for child accounts, evaluate
285 // whether we want to just return false here.
286 return profile_
->IsChild();
289 bool OffTheRecordProfileImpl::IsLegacySupervised() const {
290 return profile_
->IsLegacySupervised();
293 PrefService
* OffTheRecordProfileImpl::GetPrefs() {
297 const PrefService
* OffTheRecordProfileImpl::GetPrefs() const {
301 PrefService
* OffTheRecordProfileImpl::GetOffTheRecordPrefs() {
305 DownloadManagerDelegate
* OffTheRecordProfileImpl::GetDownloadManagerDelegate() {
306 return DownloadServiceFactory::GetForBrowserContext(this)->
307 GetDownloadManagerDelegate();
310 net::URLRequestContextGetter
* OffTheRecordProfileImpl::GetRequestContext() {
311 return GetDefaultStoragePartition(this)->GetURLRequestContext();
314 net::URLRequestContextGetter
* OffTheRecordProfileImpl::CreateRequestContext(
315 content::ProtocolHandlerMap
* protocol_handlers
,
316 content::URLRequestInterceptorScopedVector request_interceptors
) {
317 return io_data_
->CreateMainRequestContextGetter(
318 protocol_handlers
, request_interceptors
.Pass()).get();
321 net::URLRequestContextGetter
*
322 OffTheRecordProfileImpl::GetRequestContextForRenderProcess(
323 int renderer_child_id
) {
324 content::RenderProcessHost
* rph
= content::RenderProcessHost::FromID(
326 return rph
->GetStoragePartition()->GetURLRequestContext();
329 net::URLRequestContextGetter
*
330 OffTheRecordProfileImpl::GetMediaRequestContext() {
331 // In OTR mode, media request context is the same as the original one.
332 return GetRequestContext();
335 net::URLRequestContextGetter
*
336 OffTheRecordProfileImpl::GetMediaRequestContextForRenderProcess(
337 int renderer_child_id
) {
338 // In OTR mode, media request context is the same as the original one.
339 return GetRequestContextForRenderProcess(renderer_child_id
);
342 net::URLRequestContextGetter
*
343 OffTheRecordProfileImpl::GetMediaRequestContextForStoragePartition(
344 const base::FilePath
& partition_path
,
346 return io_data_
->GetIsolatedAppRequestContextGetter(partition_path
, in_memory
)
350 net::URLRequestContextGetter
*
351 OffTheRecordProfileImpl::GetRequestContextForExtensions() {
352 return io_data_
->GetExtensionsRequestContextGetter().get();
355 net::URLRequestContextGetter
*
356 OffTheRecordProfileImpl::CreateRequestContextForStoragePartition(
357 const base::FilePath
& partition_path
,
359 content::ProtocolHandlerMap
* protocol_handlers
,
360 content::URLRequestInterceptorScopedVector request_interceptors
) {
361 return io_data_
->CreateIsolatedAppRequestContextGetter(
365 request_interceptors
.Pass()).get();
368 content::ResourceContext
* OffTheRecordProfileImpl::GetResourceContext() {
369 return io_data_
->GetResourceContext();
372 net::SSLConfigService
* OffTheRecordProfileImpl::GetSSLConfigService() {
373 return profile_
->GetSSLConfigService();
376 HostContentSettingsMap
* OffTheRecordProfileImpl::GetHostContentSettingsMap() {
377 // TODO(peconn): Once HostContentSettingsMapFactory works for TestingProfiles
378 // remove Profile::GetHostContentSettingsMap and replace all references to it.
379 // Don't forget to remove the #include "host_content_settings_map_factory"!
380 return HostContentSettingsMapFactory::GetForProfile(this);
383 content::BrowserPluginGuestManager
* OffTheRecordProfileImpl::GetGuestManager() {
384 #if defined(ENABLE_EXTENSIONS)
385 return guest_view::GuestViewManager::FromBrowserContext(this);
391 storage::SpecialStoragePolicy
*
392 OffTheRecordProfileImpl::GetSpecialStoragePolicy() {
393 #if defined(ENABLE_EXTENSIONS)
394 return GetExtensionSpecialStoragePolicy();
400 content::PushMessagingService
*
401 OffTheRecordProfileImpl::GetPushMessagingService() {
402 // TODO(johnme): Support push messaging in incognito if possible.
406 content::SSLHostStateDelegate
*
407 OffTheRecordProfileImpl::GetSSLHostStateDelegate() {
408 return ChromeSSLHostStateDelegateFactory::GetForProfile(this);
411 // TODO(mlamouri): we should all these BrowserContext implementation to Profile
412 // instead of repeating them inside all Profile implementations.
413 content::PermissionManager
* OffTheRecordProfileImpl::GetPermissionManager() {
414 return PermissionManagerFactory::GetForProfile(this);
417 bool OffTheRecordProfileImpl::IsSameProfile(Profile
* profile
) {
418 return (profile
== this) || (profile
== profile_
);
421 Time
OffTheRecordProfileImpl::GetStartTime() const {
425 void OffTheRecordProfileImpl::SetExitType(ExitType exit_type
) {
428 base::FilePath
OffTheRecordProfileImpl::last_selected_directory() {
429 const base::FilePath
& directory
= last_selected_directory_
;
430 if (directory
.empty()) {
431 return profile_
->last_selected_directory();
436 void OffTheRecordProfileImpl::set_last_selected_directory(
437 const base::FilePath
& path
) {
438 last_selected_directory_
= path
;
441 bool OffTheRecordProfileImpl::WasCreatedByVersionOrLater(
442 const std::string
& version
) {
443 return profile_
->WasCreatedByVersionOrLater(version
);
446 Profile::ExitType
OffTheRecordProfileImpl::GetLastSessionExitType() {
447 return profile_
->GetLastSessionExitType();
450 #if defined(OS_CHROMEOS)
451 void OffTheRecordProfileImpl::ChangeAppLocale(const std::string
& locale
,
452 AppLocaleChangedVia
) {
455 void OffTheRecordProfileImpl::OnLogin() {
458 void OffTheRecordProfileImpl::InitChromeOSPreferences() {
459 // The incognito profile shouldn't have Chrome OS's preferences.
460 // The preferences are associated with the regular user profile.
462 #endif // defined(OS_CHROMEOS)
464 PrefProxyConfigTracker
* OffTheRecordProfileImpl::GetProxyConfigTracker() {
465 if (!pref_proxy_config_tracker_
)
466 pref_proxy_config_tracker_
.reset(CreateProxyConfigTracker());
467 return pref_proxy_config_tracker_
.get();
470 chrome_browser_net::Predictor
* OffTheRecordProfileImpl::GetNetworkPredictor() {
471 // We do not store information about websites visited in OTR profiles which
472 // is necessary for a Predictor, so we do not have a Predictor at all.
476 DevToolsNetworkControllerHandle
*
477 OffTheRecordProfileImpl::GetDevToolsNetworkControllerHandle() {
478 return io_data_
->GetDevToolsNetworkControllerHandle();
481 void OffTheRecordProfileImpl::ClearNetworkingHistorySince(
483 const base::Closure
& completion
) {
484 // Nothing to do here, our transport security state is read-only.
485 // Still, fire the callback to indicate we have finished, otherwise the
486 // BrowsingDataRemover will never be destroyed and the dialog will never be
487 // closed. We must do this asynchronously in order to avoid reentrancy issues.
488 if (!completion
.is_null()) {
489 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
, completion
);
493 GURL
OffTheRecordProfileImpl::GetHomePage() {
494 return profile_
->GetHomePage();
497 #if defined(OS_CHROMEOS)
498 // Special case of the OffTheRecordProfileImpl which is used while Guest
500 class GuestSessionProfile
: public OffTheRecordProfileImpl
{
502 explicit GuestSessionProfile(Profile
* real_profile
)
503 : OffTheRecordProfileImpl(real_profile
) {
504 set_is_guest_profile(true);
507 ProfileType
GetProfileType() const override
{ return GUEST_PROFILE
; }
509 void InitChromeOSPreferences() override
{
510 chromeos_preferences_
.reset(new chromeos::Preferences());
511 chromeos_preferences_
->Init(
512 this, user_manager::UserManager::Get()->GetActiveUser());
516 // The guest user should be able to customize Chrome OS preferences.
517 scoped_ptr
<chromeos::Preferences
> chromeos_preferences_
;
521 Profile
* Profile::CreateOffTheRecordProfile() {
522 OffTheRecordProfileImpl
* profile
= NULL
;
523 #if defined(OS_CHROMEOS)
524 if (IsGuestSession())
525 profile
= new GuestSessionProfile(this);
528 profile
= new OffTheRecordProfileImpl(this);
533 void OffTheRecordProfileImpl::OnParentZoomLevelChanged(
534 const HostZoomMap::ZoomLevelChange
& change
) {
535 HostZoomMap
* host_zoom_map
= HostZoomMap::GetDefaultForBrowserContext(this);
536 switch (change
.mode
) {
537 case HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM
:
539 case HostZoomMap::ZOOM_CHANGED_FOR_HOST
:
540 host_zoom_map
->SetZoomLevelForHost(change
.host
, change
.zoom_level
);
542 case HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST
:
543 host_zoom_map
->SetZoomLevelForHostAndScheme(change
.scheme
,
547 case HostZoomMap::PAGE_SCALE_IS_ONE_CHANGED
:
552 void OffTheRecordProfileImpl::UpdateDefaultZoomLevel() {
553 HostZoomMap
* host_zoom_map
= HostZoomMap::GetDefaultForBrowserContext(this);
554 double default_zoom_level
=
555 profile_
->GetZoomLevelPrefs()->GetDefaultZoomLevelPref();
556 host_zoom_map
->SetDefaultZoomLevel(default_zoom_level
);
559 PrefProxyConfigTracker
* OffTheRecordProfileImpl::CreateProxyConfigTracker() {
560 #if defined(OS_CHROMEOS)
561 if (chromeos::ProfileHelper::IsSigninProfile(this)) {
562 return ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
563 g_browser_process
->local_state());
565 #endif // defined(OS_CHROMEOS)
566 return ProxyServiceFactory::CreatePrefProxyConfigTrackerOfProfile(
567 GetPrefs(), g_browser_process
->local_state());