Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / profiles / off_the_record_profile_impl.cc
blob6ef47040465b1fcb5841fffc7cbe2b0507c604e9
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/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"
69 #endif
71 #if defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS)
72 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
73 #endif
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"
82 #endif
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"
88 #endif
90 using content::BrowserThread;
91 using content::DownloadManagerDelegate;
92 using content::HostZoomMap;
94 #if defined(ENABLE_EXTENSIONS)
95 namespace {
97 void NotifyOTRProfileCreatedOnIOThread(void* original_profile,
98 void* otr_profile) {
99 extensions::ExtensionWebRequestEventRouter::GetInstance()
100 ->OnOTRBrowserContextCreated(original_profile, otr_profile);
103 void NotifyOTRProfileDestroyedOnIOThread(void* original_profile,
104 void* otr_profile) {
105 extensions::ExtensionWebRequestEventRouter::GetInstance()
106 ->OnOTRBrowserContextDestroyed(original_profile, otr_profile);
109 } // namespace
110 #endif
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.
126 InitIoData();
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);
133 #endif
135 BrowserContextDependencyManager::GetInstance()->CreateBrowserContextServices(
136 this);
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());
152 #endif
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));
163 #endif
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());
176 #endif
178 BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
179 this);
181 #if defined(ENABLE_EXTENSIONS)
182 BrowserThread::PostTask(
183 BrowserThread::IO, FROM_HERE,
184 base::Bind(&NotifyOTRProfileDestroyedOnIOThread, profile_, this));
185 #endif
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
191 // OTR session.
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())
215 return;
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;
232 #else
233 return INCOGNITO_PROFILE;
234 #endif
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 {
254 return true;
257 Profile* OffTheRecordProfileImpl::GetOffTheRecordProfile() {
258 return this;
261 void OffTheRecordProfileImpl::DestroyOffTheRecordProfile() {
262 // Suicide is bad!
263 NOTREACHED();
266 bool OffTheRecordProfileImpl::HasOffTheRecordProfile() {
267 return true;
270 Profile* OffTheRecordProfileImpl::GetOriginalProfile() {
271 return profile_;
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() {
294 return prefs_;
297 const PrefService* OffTheRecordProfileImpl::GetPrefs() const {
298 return prefs_;
301 PrefService* OffTheRecordProfileImpl::GetOffTheRecordPrefs() {
302 return prefs_;
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(
325 renderer_child_id);
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,
345 bool in_memory) {
346 return io_data_->GetIsolatedAppRequestContextGetter(partition_path, in_memory)
347 .get();
350 net::URLRequestContextGetter*
351 OffTheRecordProfileImpl::GetRequestContextForExtensions() {
352 return io_data_->GetExtensionsRequestContextGetter().get();
355 net::URLRequestContextGetter*
356 OffTheRecordProfileImpl::CreateRequestContextForStoragePartition(
357 const base::FilePath& partition_path,
358 bool in_memory,
359 content::ProtocolHandlerMap* protocol_handlers,
360 content::URLRequestInterceptorScopedVector request_interceptors) {
361 return io_data_->CreateIsolatedAppRequestContextGetter(
362 partition_path,
363 in_memory,
364 protocol_handlers,
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);
386 #else
387 return NULL;
388 #endif
391 storage::SpecialStoragePolicy*
392 OffTheRecordProfileImpl::GetSpecialStoragePolicy() {
393 #if defined(ENABLE_EXTENSIONS)
394 return GetExtensionSpecialStoragePolicy();
395 #else
396 return NULL;
397 #endif
400 content::PushMessagingService*
401 OffTheRecordProfileImpl::GetPushMessagingService() {
402 // TODO(johnme): Support push messaging in incognito if possible.
403 return NULL;
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 {
422 return start_time_;
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();
433 return 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.
473 return NULL;
476 DevToolsNetworkControllerHandle*
477 OffTheRecordProfileImpl::GetDevToolsNetworkControllerHandle() {
478 return io_data_->GetDevToolsNetworkControllerHandle();
481 void OffTheRecordProfileImpl::ClearNetworkingHistorySince(
482 base::Time time,
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
499 // session in CrOS.
500 class GuestSessionProfile : public OffTheRecordProfileImpl {
501 public:
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());
515 private:
516 // The guest user should be able to customize Chrome OS preferences.
517 scoped_ptr<chromeos::Preferences> chromeos_preferences_;
519 #endif
521 Profile* Profile::CreateOffTheRecordProfile() {
522 OffTheRecordProfileImpl* profile = NULL;
523 #if defined(OS_CHROMEOS)
524 if (IsGuestSession())
525 profile = new GuestSessionProfile(this);
526 #endif
527 if (!profile)
528 profile = new OffTheRecordProfileImpl(this);
529 profile->Init();
530 return profile;
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:
538 return;
539 case HostZoomMap::ZOOM_CHANGED_FOR_HOST:
540 host_zoom_map->SetZoomLevelForHost(change.host, change.zoom_level);
541 return;
542 case HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST:
543 host_zoom_map->SetZoomLevelForHostAndScheme(change.scheme,
544 change.host,
545 change.zoom_level);
546 return;
547 case HostZoomMap::PAGE_SCALE_IS_ONE_CHANGED:
548 return;
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());