Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / profiles / off_the_record_profile_impl.cc
blobe61f0ac28de5131e03843ec884168a649af8d455
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/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 "chrome/common/pref_names.h"
42 #include "chrome/common/render_messages.h"
43 #include "components/content_settings/core/browser/host_content_settings_map.h"
44 #include "components/keyed_service/content/browser_context_dependency_manager.h"
45 #include "components/ui/zoom/zoom_event_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 "net/http/http_server_properties.h"
54 #include "net/http/transport_security_state.h"
55 #include "storage/browser/database/database_tracker.h"
57 #if defined(OS_ANDROID)
58 #include "chrome/browser/media/protected_media_identifier_permission_context.h"
59 #include "chrome/browser/media/protected_media_identifier_permission_context_factory.h"
60 #endif // defined(OS_ANDROID)
62 #if defined(OS_ANDROID) || defined(OS_IOS)
63 #include "base/prefs/scoped_user_pref_update.h"
64 #include "components/proxy_config/proxy_prefs.h"
65 #endif // defined(OS_ANDROID) || defined(OS_IOS)
67 #if defined(OS_CHROMEOS)
68 #include "chrome/browser/chromeos/preferences.h"
69 #include "chrome/browser/chromeos/profiles/profile_helper.h"
70 #endif
72 #if defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS)
73 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
74 #endif
76 #if defined(ENABLE_EXTENSIONS)
77 #include "chrome/browser/extensions/extension_service.h"
78 #include "chrome/browser/extensions/extension_special_storage_policy.h"
79 #include "components/guest_view/browser/guest_view_manager.h"
80 #include "extensions/browser/api/web_request/web_request_api.h"
81 #include "extensions/browser/extension_system.h"
82 #include "extensions/common/extension.h"
83 #endif
85 #if defined(ENABLE_SUPERVISED_USERS)
86 #include "chrome/browser/content_settings/content_settings_supervised_provider.h"
87 #include "chrome/browser/supervised_user/supervised_user_settings_service.h"
88 #include "chrome/browser/supervised_user/supervised_user_settings_service_factory.h"
89 #endif
91 using content::BrowserThread;
92 using content::DownloadManagerDelegate;
93 using content::HostZoomMap;
95 #if defined(ENABLE_EXTENSIONS)
96 namespace {
98 void NotifyOTRProfileCreatedOnIOThread(void* original_profile,
99 void* otr_profile) {
100 extensions::ExtensionWebRequestEventRouter::GetInstance()
101 ->OnOTRBrowserContextCreated(original_profile, otr_profile);
104 void NotifyOTRProfileDestroyedOnIOThread(void* original_profile,
105 void* otr_profile) {
106 extensions::ExtensionWebRequestEventRouter::GetInstance()
107 ->OnOTRBrowserContextDestroyed(original_profile, otr_profile);
110 } // namespace
111 #endif
113 OffTheRecordProfileImpl::OffTheRecordProfileImpl(Profile* real_profile)
114 : profile_(real_profile),
115 prefs_(PrefServiceSyncable::IncognitoFromProfile(real_profile)),
116 start_time_(Time::Now()) {
117 // Register on BrowserContext.
118 user_prefs::UserPrefs::Set(this, prefs_);
121 void OffTheRecordProfileImpl::Init() {
122 // The construction of OffTheRecordProfileIOData::Handle needs the profile
123 // type returned by this->GetProfileType(). Since GetProfileType() is a
124 // virtual member function, we cannot call the function defined in the most
125 // derived class (e.g. GuestSessionProfile) until a ctor finishes. Thus,
126 // we have to instantiate OffTheRecordProfileIOData::Handle here after a ctor.
127 InitIoData();
129 #if defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS)
130 // Because UserCloudPolicyManager is in a component, it cannot access
131 // GetOriginalProfile. Instead, we have to inject this relation here.
132 policy::UserCloudPolicyManagerFactory::RegisterForOffTheRecordBrowserContext(
133 this->GetOriginalProfile(), this);
134 #endif
136 BrowserContextDependencyManager::GetInstance()->CreateBrowserContextServices(
137 this);
139 set_is_guest_profile(
140 profile_->GetPath() == ProfileManager::GetGuestProfilePath());
142 // Guest profiles may always be OTR. Check IncognitoModePrefs otherwise.
143 DCHECK(profile_->IsGuestSession() ||
144 IncognitoModePrefs::GetAvailability(profile_->GetPrefs()) !=
145 IncognitoModePrefs::DISABLED);
147 // TODO(oshima): Remove the need to eagerly initialize the request context
148 // getter. chromeos::OnlineAttempt is illegally trying to access this
149 // Profile member from a thread other than the UI thread, so we need to
150 // prevent a race.
151 #if defined(OS_CHROMEOS)
152 GetRequestContext();
153 #endif // defined(OS_CHROMEOS)
155 TrackZoomLevelsFromParent();
157 #if defined(ENABLE_PLUGINS)
158 ChromePluginServiceFilter::GetInstance()->RegisterResourceContext(
159 PluginPrefs::GetForProfile(this).get(),
160 io_data_->GetResourceContextNoInit());
161 #endif
163 #if defined(ENABLE_EXTENSIONS)
164 // Make the chrome//extension-icon/ resource available.
165 extensions::ExtensionIconSource* icon_source =
166 new extensions::ExtensionIconSource(profile_);
167 content::URLDataSource::Add(this, icon_source);
169 BrowserThread::PostTask(
170 BrowserThread::IO, FROM_HERE,
171 base::Bind(&NotifyOTRProfileCreatedOnIOThread, profile_, this));
172 #endif
174 // The DomDistillerViewerSource is not a normal WebUI so it must be registered
175 // as a URLDataSource early.
176 RegisterDomDistillerViewerSource(this);
179 OffTheRecordProfileImpl::~OffTheRecordProfileImpl() {
180 MaybeSendDestroyedNotification();
182 #if defined(ENABLE_PLUGINS)
183 ChromePluginServiceFilter::GetInstance()->UnregisterResourceContext(
184 io_data_->GetResourceContextNoInit());
185 #endif
187 BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
188 this);
190 #if defined(ENABLE_EXTENSIONS)
191 BrowserThread::PostTask(
192 BrowserThread::IO, FROM_HERE,
193 base::Bind(&NotifyOTRProfileDestroyedOnIOThread, profile_, this));
194 #endif
196 if (host_content_settings_map_.get())
197 host_content_settings_map_->ShutdownOnUIThread();
199 if (pref_proxy_config_tracker_)
200 pref_proxy_config_tracker_->DetachFromPrefService();
202 // Clears any data the network stack contains that may be related to the
203 // OTR session.
204 g_browser_process->io_thread()->ChangedToOnTheRecord();
207 void OffTheRecordProfileImpl::InitIoData() {
208 io_data_.reset(new OffTheRecordProfileIOData::Handle(this));
211 void OffTheRecordProfileImpl::TrackZoomLevelsFromParent() {
212 DCHECK_NE(INCOGNITO_PROFILE, profile_->GetProfileType());
214 // Here we only want to use zoom levels stored in the main-context's default
215 // storage partition. We're not interested in zoom levels in special
216 // partitions, e.g. those used by WebViewGuests.
217 HostZoomMap* host_zoom_map = HostZoomMap::GetDefaultForBrowserContext(this);
218 HostZoomMap* parent_host_zoom_map =
219 HostZoomMap::GetDefaultForBrowserContext(profile_);
220 host_zoom_map->CopyFrom(parent_host_zoom_map);
221 // Observe parent profile's HostZoomMap changes so they can also be applied
222 // to this profile's HostZoomMap.
223 track_zoom_subscription_ = parent_host_zoom_map->AddZoomLevelChangedCallback(
224 base::Bind(&OffTheRecordProfileImpl::OnParentZoomLevelChanged,
225 base::Unretained(this)));
226 if (!profile_->GetZoomLevelPrefs())
227 return;
229 // Also track changes to the parent profile's default zoom level.
230 parent_default_zoom_level_subscription_ =
231 profile_->GetZoomLevelPrefs()->RegisterDefaultZoomLevelCallback(
232 base::Bind(&OffTheRecordProfileImpl::UpdateDefaultZoomLevel,
233 base::Unretained(this)));
236 std::string OffTheRecordProfileImpl::GetProfileUserName() const {
237 // Incognito profile should not return the username.
238 return std::string();
241 Profile::ProfileType OffTheRecordProfileImpl::GetProfileType() const {
242 #if !defined(OS_CHROMEOS)
243 return profile_->IsGuestSession() ? GUEST_PROFILE : INCOGNITO_PROFILE;
244 #else
245 return INCOGNITO_PROFILE;
246 #endif
249 base::FilePath OffTheRecordProfileImpl::GetPath() const {
250 return profile_->GetPath();
253 scoped_ptr<content::ZoomLevelDelegate>
254 OffTheRecordProfileImpl::CreateZoomLevelDelegate(
255 const base::FilePath& partition_path) {
256 return make_scoped_ptr(new chrome::ChromeZoomLevelOTRDelegate(
257 ui_zoom::ZoomEventManager::GetForBrowserContext(this)->GetWeakPtr()));
260 scoped_refptr<base::SequencedTaskRunner>
261 OffTheRecordProfileImpl::GetIOTaskRunner() {
262 return profile_->GetIOTaskRunner();
265 bool OffTheRecordProfileImpl::IsOffTheRecord() const {
266 return true;
269 Profile* OffTheRecordProfileImpl::GetOffTheRecordProfile() {
270 return this;
273 void OffTheRecordProfileImpl::DestroyOffTheRecordProfile() {
274 // Suicide is bad!
275 NOTREACHED();
278 bool OffTheRecordProfileImpl::HasOffTheRecordProfile() {
279 return true;
282 Profile* OffTheRecordProfileImpl::GetOriginalProfile() {
283 return profile_;
286 ExtensionSpecialStoragePolicy*
287 OffTheRecordProfileImpl::GetExtensionSpecialStoragePolicy() {
288 return GetOriginalProfile()->GetExtensionSpecialStoragePolicy();
291 bool OffTheRecordProfileImpl::IsSupervised() {
292 return GetOriginalProfile()->IsSupervised();
295 bool OffTheRecordProfileImpl::IsChild() {
296 // TODO(treib): If we ever allow incognito for child accounts, evaluate
297 // whether we want to just return false here.
298 return GetOriginalProfile()->IsChild();
301 bool OffTheRecordProfileImpl::IsLegacySupervised() {
302 return GetOriginalProfile()->IsLegacySupervised();
305 PrefService* OffTheRecordProfileImpl::GetPrefs() {
306 return prefs_;
309 const PrefService* OffTheRecordProfileImpl::GetPrefs() const {
310 return prefs_;
313 PrefService* OffTheRecordProfileImpl::GetOffTheRecordPrefs() {
314 return prefs_;
317 DownloadManagerDelegate* OffTheRecordProfileImpl::GetDownloadManagerDelegate() {
318 return DownloadServiceFactory::GetForBrowserContext(this)->
319 GetDownloadManagerDelegate();
322 net::URLRequestContextGetter* OffTheRecordProfileImpl::GetRequestContext() {
323 return GetDefaultStoragePartition(this)->GetURLRequestContext();
326 net::URLRequestContextGetter* OffTheRecordProfileImpl::CreateRequestContext(
327 content::ProtocolHandlerMap* protocol_handlers,
328 content::URLRequestInterceptorScopedVector request_interceptors) {
329 return io_data_->CreateMainRequestContextGetter(
330 protocol_handlers, request_interceptors.Pass()).get();
333 net::URLRequestContextGetter*
334 OffTheRecordProfileImpl::GetRequestContextForRenderProcess(
335 int renderer_child_id) {
336 content::RenderProcessHost* rph = content::RenderProcessHost::FromID(
337 renderer_child_id);
338 return rph->GetStoragePartition()->GetURLRequestContext();
341 net::URLRequestContextGetter*
342 OffTheRecordProfileImpl::GetMediaRequestContext() {
343 // In OTR mode, media request context is the same as the original one.
344 return GetRequestContext();
347 net::URLRequestContextGetter*
348 OffTheRecordProfileImpl::GetMediaRequestContextForRenderProcess(
349 int renderer_child_id) {
350 // In OTR mode, media request context is the same as the original one.
351 return GetRequestContextForRenderProcess(renderer_child_id);
354 net::URLRequestContextGetter*
355 OffTheRecordProfileImpl::GetMediaRequestContextForStoragePartition(
356 const base::FilePath& partition_path,
357 bool in_memory) {
358 return io_data_->GetIsolatedAppRequestContextGetter(partition_path, in_memory)
359 .get();
362 net::URLRequestContextGetter*
363 OffTheRecordProfileImpl::GetRequestContextForExtensions() {
364 return io_data_->GetExtensionsRequestContextGetter().get();
367 net::URLRequestContextGetter*
368 OffTheRecordProfileImpl::CreateRequestContextForStoragePartition(
369 const base::FilePath& partition_path,
370 bool in_memory,
371 content::ProtocolHandlerMap* protocol_handlers,
372 content::URLRequestInterceptorScopedVector request_interceptors) {
373 return io_data_->CreateIsolatedAppRequestContextGetter(
374 partition_path,
375 in_memory,
376 protocol_handlers,
377 request_interceptors.Pass()).get();
380 content::ResourceContext* OffTheRecordProfileImpl::GetResourceContext() {
381 return io_data_->GetResourceContext();
384 net::SSLConfigService* OffTheRecordProfileImpl::GetSSLConfigService() {
385 return profile_->GetSSLConfigService();
388 HostContentSettingsMap* OffTheRecordProfileImpl::GetHostContentSettingsMap() {
389 DCHECK_CURRENTLY_ON(BrowserThread::UI);
390 // Retrieve the host content settings map of the parent profile in order to
391 // ensure the preferences have been migrated.
392 profile_->GetHostContentSettingsMap();
393 if (!host_content_settings_map_.get()) {
394 host_content_settings_map_ = new HostContentSettingsMap(GetPrefs(), true);
395 #if defined(ENABLE_EXTENSIONS)
396 ExtensionService* extension_service =
397 extensions::ExtensionSystem::Get(this)->extension_service();
398 if (extension_service) {
399 extension_service->RegisterContentSettings(
400 host_content_settings_map_.get());
402 #endif
403 #if defined(ENABLE_SUPERVISED_USERS)
404 SupervisedUserSettingsService* supervised_service =
405 SupervisedUserSettingsServiceFactory::GetForProfile(this);
406 scoped_ptr<content_settings::SupervisedProvider> supervised_provider(
407 new content_settings::SupervisedProvider(supervised_service));
408 host_content_settings_map_->RegisterProvider(
409 HostContentSettingsMap::SUPERVISED_PROVIDER,
410 supervised_provider.Pass());
411 #endif
413 return host_content_settings_map_.get();
416 content::BrowserPluginGuestManager* OffTheRecordProfileImpl::GetGuestManager() {
417 #if defined(ENABLE_EXTENSIONS)
418 return guest_view::GuestViewManager::FromBrowserContext(this);
419 #else
420 return NULL;
421 #endif
424 storage::SpecialStoragePolicy*
425 OffTheRecordProfileImpl::GetSpecialStoragePolicy() {
426 #if defined(ENABLE_EXTENSIONS)
427 return GetExtensionSpecialStoragePolicy();
428 #else
429 return NULL;
430 #endif
433 content::PushMessagingService*
434 OffTheRecordProfileImpl::GetPushMessagingService() {
435 // TODO(johnme): Support push messaging in incognito if possible.
436 return NULL;
439 content::SSLHostStateDelegate*
440 OffTheRecordProfileImpl::GetSSLHostStateDelegate() {
441 return ChromeSSLHostStateDelegateFactory::GetForProfile(this);
444 // TODO(mlamouri): we should all these BrowserContext implementation to Profile
445 // instead of repeating them inside all Profile implementations.
446 content::PermissionManager* OffTheRecordProfileImpl::GetPermissionManager() {
447 return PermissionManagerFactory::GetForProfile(this);
450 bool OffTheRecordProfileImpl::IsSameProfile(Profile* profile) {
451 return (profile == this) || (profile == profile_);
454 Time OffTheRecordProfileImpl::GetStartTime() const {
455 return start_time_;
458 void OffTheRecordProfileImpl::SetExitType(ExitType exit_type) {
461 base::FilePath OffTheRecordProfileImpl::last_selected_directory() {
462 const base::FilePath& directory = last_selected_directory_;
463 if (directory.empty()) {
464 return profile_->last_selected_directory();
466 return directory;
469 void OffTheRecordProfileImpl::set_last_selected_directory(
470 const base::FilePath& path) {
471 last_selected_directory_ = path;
474 bool OffTheRecordProfileImpl::WasCreatedByVersionOrLater(
475 const std::string& version) {
476 return profile_->WasCreatedByVersionOrLater(version);
479 Profile::ExitType OffTheRecordProfileImpl::GetLastSessionExitType() {
480 return profile_->GetLastSessionExitType();
483 #if defined(OS_CHROMEOS)
484 void OffTheRecordProfileImpl::ChangeAppLocale(const std::string& locale,
485 AppLocaleChangedVia) {
488 void OffTheRecordProfileImpl::OnLogin() {
491 void OffTheRecordProfileImpl::InitChromeOSPreferences() {
492 // The incognito profile shouldn't have Chrome OS's preferences.
493 // The preferences are associated with the regular user profile.
495 #endif // defined(OS_CHROMEOS)
497 PrefProxyConfigTracker* OffTheRecordProfileImpl::GetProxyConfigTracker() {
498 if (!pref_proxy_config_tracker_)
499 pref_proxy_config_tracker_.reset(CreateProxyConfigTracker());
500 return pref_proxy_config_tracker_.get();
503 chrome_browser_net::Predictor* OffTheRecordProfileImpl::GetNetworkPredictor() {
504 // We do not store information about websites visited in OTR profiles which
505 // is necessary for a Predictor, so we do not have a Predictor at all.
506 return NULL;
509 DevToolsNetworkController*
510 OffTheRecordProfileImpl::GetDevToolsNetworkController() {
511 return io_data_->GetDevToolsNetworkController();
514 void OffTheRecordProfileImpl::ClearNetworkingHistorySince(
515 base::Time time,
516 const base::Closure& completion) {
517 // Nothing to do here, our transport security state is read-only.
518 // Still, fire the callback to indicate we have finished, otherwise the
519 // BrowsingDataRemover will never be destroyed and the dialog will never be
520 // closed. We must do this asynchronously in order to avoid reentrancy issues.
521 if (!completion.is_null()) {
522 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion);
526 GURL OffTheRecordProfileImpl::GetHomePage() {
527 return profile_->GetHomePage();
530 #if defined(OS_CHROMEOS)
531 // Special case of the OffTheRecordProfileImpl which is used while Guest
532 // session in CrOS.
533 class GuestSessionProfile : public OffTheRecordProfileImpl {
534 public:
535 explicit GuestSessionProfile(Profile* real_profile)
536 : OffTheRecordProfileImpl(real_profile) {
537 set_is_guest_profile(true);
540 ProfileType GetProfileType() const override { return GUEST_PROFILE; }
542 void InitChromeOSPreferences() override {
543 chromeos_preferences_.reset(new chromeos::Preferences());
544 chromeos_preferences_->Init(
545 this, user_manager::UserManager::Get()->GetActiveUser());
548 private:
549 // The guest user should be able to customize Chrome OS preferences.
550 scoped_ptr<chromeos::Preferences> chromeos_preferences_;
552 #endif
554 Profile* Profile::CreateOffTheRecordProfile() {
555 OffTheRecordProfileImpl* profile = NULL;
556 #if defined(OS_CHROMEOS)
557 if (IsGuestSession())
558 profile = new GuestSessionProfile(this);
559 #endif
560 if (!profile)
561 profile = new OffTheRecordProfileImpl(this);
562 profile->Init();
563 return profile;
566 void OffTheRecordProfileImpl::OnParentZoomLevelChanged(
567 const HostZoomMap::ZoomLevelChange& change) {
568 HostZoomMap* host_zoom_map = HostZoomMap::GetDefaultForBrowserContext(this);
569 switch (change.mode) {
570 case HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM:
571 return;
572 case HostZoomMap::ZOOM_CHANGED_FOR_HOST:
573 host_zoom_map->SetZoomLevelForHost(change.host, change.zoom_level);
574 return;
575 case HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST:
576 host_zoom_map->SetZoomLevelForHostAndScheme(change.scheme,
577 change.host,
578 change.zoom_level);
579 return;
580 case HostZoomMap::PAGE_SCALE_IS_ONE_CHANGED:
581 return;
585 void OffTheRecordProfileImpl::UpdateDefaultZoomLevel() {
586 HostZoomMap* host_zoom_map = HostZoomMap::GetDefaultForBrowserContext(this);
587 double default_zoom_level =
588 profile_->GetZoomLevelPrefs()->GetDefaultZoomLevelPref();
589 host_zoom_map->SetDefaultZoomLevel(default_zoom_level);
592 PrefProxyConfigTracker* OffTheRecordProfileImpl::CreateProxyConfigTracker() {
593 #if defined(OS_CHROMEOS)
594 if (chromeos::ProfileHelper::IsSigninProfile(this)) {
595 return ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
596 g_browser_process->local_state());
598 #endif // defined(OS_CHROMEOS)
599 return ProxyServiceFactory::CreatePrefProxyConfigTrackerOfProfile(
600 GetPrefs(), g_browser_process->local_state());