Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / profiles / off_the_record_profile_impl.cc
blob400005ae2a4a0b2f85b2eae51cae49cec1debc3f
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 "components/content_settings/core/browser/host_content_settings_map.h"
43 #include "components/keyed_service/content/browser_context_dependency_manager.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 (host_content_settings_map_.get())
188 host_content_settings_map_->ShutdownOnUIThread();
190 if (pref_proxy_config_tracker_)
191 pref_proxy_config_tracker_->DetachFromPrefService();
193 // Clears any data the network stack contains that may be related to the
194 // OTR session.
195 g_browser_process->io_thread()->ChangedToOnTheRecord();
198 void OffTheRecordProfileImpl::InitIoData() {
199 io_data_.reset(new OffTheRecordProfileIOData::Handle(this));
202 void OffTheRecordProfileImpl::TrackZoomLevelsFromParent() {
203 DCHECK_NE(INCOGNITO_PROFILE, profile_->GetProfileType());
205 // Here we only want to use zoom levels stored in the main-context's default
206 // storage partition. We're not interested in zoom levels in special
207 // partitions, e.g. those used by WebViewGuests.
208 HostZoomMap* host_zoom_map = HostZoomMap::GetDefaultForBrowserContext(this);
209 HostZoomMap* parent_host_zoom_map =
210 HostZoomMap::GetDefaultForBrowserContext(profile_);
211 host_zoom_map->CopyFrom(parent_host_zoom_map);
212 // Observe parent profile's HostZoomMap changes so they can also be applied
213 // to this profile's HostZoomMap.
214 track_zoom_subscription_ = parent_host_zoom_map->AddZoomLevelChangedCallback(
215 base::Bind(&OffTheRecordProfileImpl::OnParentZoomLevelChanged,
216 base::Unretained(this)));
217 if (!profile_->GetZoomLevelPrefs())
218 return;
220 // Also track changes to the parent profile's default zoom level.
221 parent_default_zoom_level_subscription_ =
222 profile_->GetZoomLevelPrefs()->RegisterDefaultZoomLevelCallback(
223 base::Bind(&OffTheRecordProfileImpl::UpdateDefaultZoomLevel,
224 base::Unretained(this)));
227 std::string OffTheRecordProfileImpl::GetProfileUserName() const {
228 // Incognito profile should not return the username.
229 return std::string();
232 Profile::ProfileType OffTheRecordProfileImpl::GetProfileType() const {
233 #if !defined(OS_CHROMEOS)
234 return profile_->IsGuestSession() ? GUEST_PROFILE : INCOGNITO_PROFILE;
235 #else
236 return INCOGNITO_PROFILE;
237 #endif
240 base::FilePath OffTheRecordProfileImpl::GetPath() const {
241 return profile_->GetPath();
244 scoped_ptr<content::ZoomLevelDelegate>
245 OffTheRecordProfileImpl::CreateZoomLevelDelegate(
246 const base::FilePath& partition_path) {
247 return make_scoped_ptr(new chrome::ChromeZoomLevelOTRDelegate(
248 ui_zoom::ZoomEventManager::GetForBrowserContext(this)->GetWeakPtr()));
251 scoped_refptr<base::SequencedTaskRunner>
252 OffTheRecordProfileImpl::GetIOTaskRunner() {
253 return profile_->GetIOTaskRunner();
256 bool OffTheRecordProfileImpl::IsOffTheRecord() const {
257 return true;
260 Profile* OffTheRecordProfileImpl::GetOffTheRecordProfile() {
261 return this;
264 void OffTheRecordProfileImpl::DestroyOffTheRecordProfile() {
265 // Suicide is bad!
266 NOTREACHED();
269 bool OffTheRecordProfileImpl::HasOffTheRecordProfile() {
270 return true;
273 Profile* OffTheRecordProfileImpl::GetOriginalProfile() {
274 return profile_;
277 ExtensionSpecialStoragePolicy*
278 OffTheRecordProfileImpl::GetExtensionSpecialStoragePolicy() {
279 return GetOriginalProfile()->GetExtensionSpecialStoragePolicy();
282 bool OffTheRecordProfileImpl::IsSupervised() const {
283 return profile_->IsSupervised();
286 bool OffTheRecordProfileImpl::IsChild() const {
287 // TODO(treib): If we ever allow incognito for child accounts, evaluate
288 // whether we want to just return false here.
289 return profile_->IsChild();
292 bool OffTheRecordProfileImpl::IsLegacySupervised() const {
293 return profile_->IsLegacySupervised();
296 PrefService* OffTheRecordProfileImpl::GetPrefs() {
297 return prefs_;
300 const PrefService* OffTheRecordProfileImpl::GetPrefs() const {
301 return prefs_;
304 PrefService* OffTheRecordProfileImpl::GetOffTheRecordPrefs() {
305 return prefs_;
308 DownloadManagerDelegate* OffTheRecordProfileImpl::GetDownloadManagerDelegate() {
309 return DownloadServiceFactory::GetForBrowserContext(this)->
310 GetDownloadManagerDelegate();
313 net::URLRequestContextGetter* OffTheRecordProfileImpl::GetRequestContext() {
314 return GetDefaultStoragePartition(this)->GetURLRequestContext();
317 net::URLRequestContextGetter* OffTheRecordProfileImpl::CreateRequestContext(
318 content::ProtocolHandlerMap* protocol_handlers,
319 content::URLRequestInterceptorScopedVector request_interceptors) {
320 return io_data_->CreateMainRequestContextGetter(
321 protocol_handlers, request_interceptors.Pass()).get();
324 net::URLRequestContextGetter*
325 OffTheRecordProfileImpl::GetRequestContextForRenderProcess(
326 int renderer_child_id) {
327 content::RenderProcessHost* rph = content::RenderProcessHost::FromID(
328 renderer_child_id);
329 return rph->GetStoragePartition()->GetURLRequestContext();
332 net::URLRequestContextGetter*
333 OffTheRecordProfileImpl::GetMediaRequestContext() {
334 // In OTR mode, media request context is the same as the original one.
335 return GetRequestContext();
338 net::URLRequestContextGetter*
339 OffTheRecordProfileImpl::GetMediaRequestContextForRenderProcess(
340 int renderer_child_id) {
341 // In OTR mode, media request context is the same as the original one.
342 return GetRequestContextForRenderProcess(renderer_child_id);
345 net::URLRequestContextGetter*
346 OffTheRecordProfileImpl::GetMediaRequestContextForStoragePartition(
347 const base::FilePath& partition_path,
348 bool in_memory) {
349 return io_data_->GetIsolatedAppRequestContextGetter(partition_path, in_memory)
350 .get();
353 net::URLRequestContextGetter*
354 OffTheRecordProfileImpl::GetRequestContextForExtensions() {
355 return io_data_->GetExtensionsRequestContextGetter().get();
358 net::URLRequestContextGetter*
359 OffTheRecordProfileImpl::CreateRequestContextForStoragePartition(
360 const base::FilePath& partition_path,
361 bool in_memory,
362 content::ProtocolHandlerMap* protocol_handlers,
363 content::URLRequestInterceptorScopedVector request_interceptors) {
364 return io_data_->CreateIsolatedAppRequestContextGetter(
365 partition_path,
366 in_memory,
367 protocol_handlers,
368 request_interceptors.Pass()).get();
371 content::ResourceContext* OffTheRecordProfileImpl::GetResourceContext() {
372 return io_data_->GetResourceContext();
375 net::SSLConfigService* OffTheRecordProfileImpl::GetSSLConfigService() {
376 return profile_->GetSSLConfigService();
379 HostContentSettingsMap* OffTheRecordProfileImpl::GetHostContentSettingsMap() {
380 DCHECK_CURRENTLY_ON(BrowserThread::UI);
381 // Retrieve the host content settings map of the parent profile in order to
382 // ensure the preferences have been migrated.
383 profile_->GetHostContentSettingsMap();
384 if (!host_content_settings_map_.get()) {
385 host_content_settings_map_ = new HostContentSettingsMap(GetPrefs(), true);
386 #if defined(ENABLE_EXTENSIONS)
387 ExtensionService* extension_service =
388 extensions::ExtensionSystem::Get(this)->extension_service();
389 if (extension_service) {
390 extension_service->RegisterContentSettings(
391 host_content_settings_map_.get());
393 #endif
394 #if defined(ENABLE_SUPERVISED_USERS)
395 SupervisedUserSettingsService* supervised_service =
396 SupervisedUserSettingsServiceFactory::GetForProfile(this);
397 scoped_ptr<content_settings::SupervisedProvider> supervised_provider(
398 new content_settings::SupervisedProvider(supervised_service));
399 host_content_settings_map_->RegisterProvider(
400 HostContentSettingsMap::SUPERVISED_PROVIDER,
401 supervised_provider.Pass());
402 #endif
404 return host_content_settings_map_.get();
407 content::BrowserPluginGuestManager* OffTheRecordProfileImpl::GetGuestManager() {
408 #if defined(ENABLE_EXTENSIONS)
409 return guest_view::GuestViewManager::FromBrowserContext(this);
410 #else
411 return NULL;
412 #endif
415 storage::SpecialStoragePolicy*
416 OffTheRecordProfileImpl::GetSpecialStoragePolicy() {
417 #if defined(ENABLE_EXTENSIONS)
418 return GetExtensionSpecialStoragePolicy();
419 #else
420 return NULL;
421 #endif
424 content::PushMessagingService*
425 OffTheRecordProfileImpl::GetPushMessagingService() {
426 // TODO(johnme): Support push messaging in incognito if possible.
427 return NULL;
430 content::SSLHostStateDelegate*
431 OffTheRecordProfileImpl::GetSSLHostStateDelegate() {
432 return ChromeSSLHostStateDelegateFactory::GetForProfile(this);
435 // TODO(mlamouri): we should all these BrowserContext implementation to Profile
436 // instead of repeating them inside all Profile implementations.
437 content::PermissionManager* OffTheRecordProfileImpl::GetPermissionManager() {
438 return PermissionManagerFactory::GetForProfile(this);
441 bool OffTheRecordProfileImpl::IsSameProfile(Profile* profile) {
442 return (profile == this) || (profile == profile_);
445 Time OffTheRecordProfileImpl::GetStartTime() const {
446 return start_time_;
449 void OffTheRecordProfileImpl::SetExitType(ExitType exit_type) {
452 base::FilePath OffTheRecordProfileImpl::last_selected_directory() {
453 const base::FilePath& directory = last_selected_directory_;
454 if (directory.empty()) {
455 return profile_->last_selected_directory();
457 return directory;
460 void OffTheRecordProfileImpl::set_last_selected_directory(
461 const base::FilePath& path) {
462 last_selected_directory_ = path;
465 bool OffTheRecordProfileImpl::WasCreatedByVersionOrLater(
466 const std::string& version) {
467 return profile_->WasCreatedByVersionOrLater(version);
470 Profile::ExitType OffTheRecordProfileImpl::GetLastSessionExitType() {
471 return profile_->GetLastSessionExitType();
474 #if defined(OS_CHROMEOS)
475 void OffTheRecordProfileImpl::ChangeAppLocale(const std::string& locale,
476 AppLocaleChangedVia) {
479 void OffTheRecordProfileImpl::OnLogin() {
482 void OffTheRecordProfileImpl::InitChromeOSPreferences() {
483 // The incognito profile shouldn't have Chrome OS's preferences.
484 // The preferences are associated with the regular user profile.
486 #endif // defined(OS_CHROMEOS)
488 PrefProxyConfigTracker* OffTheRecordProfileImpl::GetProxyConfigTracker() {
489 if (!pref_proxy_config_tracker_)
490 pref_proxy_config_tracker_.reset(CreateProxyConfigTracker());
491 return pref_proxy_config_tracker_.get();
494 chrome_browser_net::Predictor* OffTheRecordProfileImpl::GetNetworkPredictor() {
495 // We do not store information about websites visited in OTR profiles which
496 // is necessary for a Predictor, so we do not have a Predictor at all.
497 return NULL;
500 DevToolsNetworkController*
501 OffTheRecordProfileImpl::GetDevToolsNetworkController() {
502 return io_data_->GetDevToolsNetworkController();
505 void OffTheRecordProfileImpl::ClearNetworkingHistorySince(
506 base::Time time,
507 const base::Closure& completion) {
508 // Nothing to do here, our transport security state is read-only.
509 // Still, fire the callback to indicate we have finished, otherwise the
510 // BrowsingDataRemover will never be destroyed and the dialog will never be
511 // closed. We must do this asynchronously in order to avoid reentrancy issues.
512 if (!completion.is_null()) {
513 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion);
517 GURL OffTheRecordProfileImpl::GetHomePage() {
518 return profile_->GetHomePage();
521 #if defined(OS_CHROMEOS)
522 // Special case of the OffTheRecordProfileImpl which is used while Guest
523 // session in CrOS.
524 class GuestSessionProfile : public OffTheRecordProfileImpl {
525 public:
526 explicit GuestSessionProfile(Profile* real_profile)
527 : OffTheRecordProfileImpl(real_profile) {
528 set_is_guest_profile(true);
531 ProfileType GetProfileType() const override { return GUEST_PROFILE; }
533 void InitChromeOSPreferences() override {
534 chromeos_preferences_.reset(new chromeos::Preferences());
535 chromeos_preferences_->Init(
536 this, user_manager::UserManager::Get()->GetActiveUser());
539 private:
540 // The guest user should be able to customize Chrome OS preferences.
541 scoped_ptr<chromeos::Preferences> chromeos_preferences_;
543 #endif
545 Profile* Profile::CreateOffTheRecordProfile() {
546 OffTheRecordProfileImpl* profile = NULL;
547 #if defined(OS_CHROMEOS)
548 if (IsGuestSession())
549 profile = new GuestSessionProfile(this);
550 #endif
551 if (!profile)
552 profile = new OffTheRecordProfileImpl(this);
553 profile->Init();
554 return profile;
557 void OffTheRecordProfileImpl::OnParentZoomLevelChanged(
558 const HostZoomMap::ZoomLevelChange& change) {
559 HostZoomMap* host_zoom_map = HostZoomMap::GetDefaultForBrowserContext(this);
560 switch (change.mode) {
561 case HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM:
562 return;
563 case HostZoomMap::ZOOM_CHANGED_FOR_HOST:
564 host_zoom_map->SetZoomLevelForHost(change.host, change.zoom_level);
565 return;
566 case HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST:
567 host_zoom_map->SetZoomLevelForHostAndScheme(change.scheme,
568 change.host,
569 change.zoom_level);
570 return;
571 case HostZoomMap::PAGE_SCALE_IS_ONE_CHANGED:
572 return;
576 void OffTheRecordProfileImpl::UpdateDefaultZoomLevel() {
577 HostZoomMap* host_zoom_map = HostZoomMap::GetDefaultForBrowserContext(this);
578 double default_zoom_level =
579 profile_->GetZoomLevelPrefs()->GetDefaultZoomLevelPref();
580 host_zoom_map->SetDefaultZoomLevel(default_zoom_level);
583 PrefProxyConfigTracker* OffTheRecordProfileImpl::CreateProxyConfigTracker() {
584 #if defined(OS_CHROMEOS)
585 if (chromeos::ProfileHelper::IsSigninProfile(this)) {
586 return ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
587 g_browser_process->local_state());
589 #endif // defined(OS_CHROMEOS)
590 return ProxyServiceFactory::CreatePrefProxyConfigTrackerOfProfile(
591 GetPrefs(), g_browser_process->local_state());