Rename GetIconID to GetIconId
[chromium-blink-merge.git] / chrome / browser / profiles / off_the_record_profile_impl.cc
blobc2f4863d68b3ab677937c830a014c12f57864c25
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/proxy_service_factory.h"
25 #include "chrome/browser/permissions/permission_manager.h"
26 #include "chrome/browser/permissions/permission_manager_factory.h"
27 #include "chrome/browser/plugins/chrome_plugin_service_filter.h"
28 #include "chrome/browser/plugins/plugin_prefs.h"
29 #include "chrome/browser/prefs/incognito_mode_prefs.h"
30 #include "chrome/browser/prefs/pref_service_syncable.h"
31 #include "chrome/browser/profiles/profile_manager.h"
32 #include "chrome/browser/ssl/chrome_ssl_host_state_delegate.h"
33 #include "chrome/browser/ssl/chrome_ssl_host_state_delegate_factory.h"
34 #include "chrome/browser/themes/theme_service.h"
35 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
36 #include "chrome/browser/ui/zoom/chrome_zoom_level_otr_delegate.h"
37 #include "chrome/common/chrome_constants.h"
38 #include "chrome/common/chrome_paths.h"
39 #include "chrome/common/chrome_switches.h"
40 #include "components/content_settings/core/browser/host_content_settings_map.h"
41 #include "components/keyed_service/content/browser_context_dependency_manager.h"
42 #include "components/proxy_config/pref_proxy_config_tracker.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 "components/proxy_config/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 "components/guest_view/browser/guest_view_manager.h"
78 #include "extensions/browser/api/web_request/web_request_api.h"
79 #include "extensions/browser/extension_system.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 extensions::ExtensionWebRequestEventRouter::GetInstance()
99 ->OnOTRBrowserContextCreated(original_profile, otr_profile);
102 void NotifyOTRProfileDestroyedOnIOThread(void* original_profile,
103 void* otr_profile) {
104 extensions::ExtensionWebRequestEventRouter::GetInstance()
105 ->OnOTRBrowserContextDestroyed(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 TrackZoomLevelsFromParent();
147 #if defined(ENABLE_PLUGINS)
148 ChromePluginServiceFilter::GetInstance()->RegisterResourceContext(
149 PluginPrefs::GetForProfile(this).get(),
150 io_data_->GetResourceContextNoInit());
151 #endif
153 #if defined(ENABLE_EXTENSIONS)
154 // Make the chrome//extension-icon/ resource available.
155 extensions::ExtensionIconSource* icon_source =
156 new extensions::ExtensionIconSource(profile_);
157 content::URLDataSource::Add(this, icon_source);
159 BrowserThread::PostTask(
160 BrowserThread::IO, FROM_HERE,
161 base::Bind(&NotifyOTRProfileCreatedOnIOThread, profile_, this));
162 #endif
164 // The DomDistillerViewerSource is not a normal WebUI so it must be registered
165 // as a URLDataSource early.
166 RegisterDomDistillerViewerSource(this);
169 OffTheRecordProfileImpl::~OffTheRecordProfileImpl() {
170 MaybeSendDestroyedNotification();
172 #if defined(ENABLE_PLUGINS)
173 ChromePluginServiceFilter::GetInstance()->UnregisterResourceContext(
174 io_data_->GetResourceContextNoInit());
175 #endif
177 BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
178 this);
180 #if defined(ENABLE_EXTENSIONS)
181 BrowserThread::PostTask(
182 BrowserThread::IO, FROM_HERE,
183 base::Bind(&NotifyOTRProfileDestroyedOnIOThread, profile_, this));
184 #endif
186 if (host_content_settings_map_.get())
187 host_content_settings_map_->ShutdownOnUIThread();
189 if (pref_proxy_config_tracker_)
190 pref_proxy_config_tracker_->DetachFromPrefService();
192 // Clears any data the network stack contains that may be related to the
193 // OTR session.
194 g_browser_process->io_thread()->ChangedToOnTheRecord();
197 void OffTheRecordProfileImpl::InitIoData() {
198 io_data_.reset(new OffTheRecordProfileIOData::Handle(this));
201 void OffTheRecordProfileImpl::TrackZoomLevelsFromParent() {
202 DCHECK_NE(INCOGNITO_PROFILE, profile_->GetProfileType());
204 // Here we only want to use zoom levels stored in the main-context's default
205 // storage partition. We're not interested in zoom levels in special
206 // partitions, e.g. those used by WebViewGuests.
207 HostZoomMap* host_zoom_map = HostZoomMap::GetDefaultForBrowserContext(this);
208 HostZoomMap* parent_host_zoom_map =
209 HostZoomMap::GetDefaultForBrowserContext(profile_);
210 host_zoom_map->CopyFrom(parent_host_zoom_map);
211 // Observe parent profile's HostZoomMap changes so they can also be applied
212 // to this profile's HostZoomMap.
213 track_zoom_subscription_ = parent_host_zoom_map->AddZoomLevelChangedCallback(
214 base::Bind(&OffTheRecordProfileImpl::OnParentZoomLevelChanged,
215 base::Unretained(this)));
216 if (!profile_->GetZoomLevelPrefs())
217 return;
219 // Also track changes to the parent profile's default zoom level.
220 parent_default_zoom_level_subscription_ =
221 profile_->GetZoomLevelPrefs()->RegisterDefaultZoomLevelCallback(
222 base::Bind(&OffTheRecordProfileImpl::UpdateDefaultZoomLevel,
223 base::Unretained(this)));
226 std::string OffTheRecordProfileImpl::GetProfileUserName() const {
227 // Incognito profile should not return the username.
228 return std::string();
231 Profile::ProfileType OffTheRecordProfileImpl::GetProfileType() const {
232 #if !defined(OS_CHROMEOS)
233 return profile_->IsGuestSession() ? GUEST_PROFILE : INCOGNITO_PROFILE;
234 #else
235 return INCOGNITO_PROFILE;
236 #endif
239 base::FilePath OffTheRecordProfileImpl::GetPath() const {
240 return profile_->GetPath();
243 scoped_ptr<content::ZoomLevelDelegate>
244 OffTheRecordProfileImpl::CreateZoomLevelDelegate(
245 const base::FilePath& partition_path) {
246 return make_scoped_ptr(new chrome::ChromeZoomLevelOTRDelegate(
247 ui_zoom::ZoomEventManager::GetForBrowserContext(this)->GetWeakPtr()));
250 scoped_refptr<base::SequencedTaskRunner>
251 OffTheRecordProfileImpl::GetIOTaskRunner() {
252 return profile_->GetIOTaskRunner();
255 bool OffTheRecordProfileImpl::IsOffTheRecord() const {
256 return true;
259 Profile* OffTheRecordProfileImpl::GetOffTheRecordProfile() {
260 return this;
263 void OffTheRecordProfileImpl::DestroyOffTheRecordProfile() {
264 // Suicide is bad!
265 NOTREACHED();
268 bool OffTheRecordProfileImpl::HasOffTheRecordProfile() {
269 return true;
272 Profile* OffTheRecordProfileImpl::GetOriginalProfile() {
273 return profile_;
276 ExtensionSpecialStoragePolicy*
277 OffTheRecordProfileImpl::GetExtensionSpecialStoragePolicy() {
278 return GetOriginalProfile()->GetExtensionSpecialStoragePolicy();
281 bool OffTheRecordProfileImpl::IsSupervised() const {
282 return profile_->IsSupervised();
285 bool OffTheRecordProfileImpl::IsChild() const {
286 // TODO(treib): If we ever allow incognito for child accounts, evaluate
287 // whether we want to just return false here.
288 return profile_->IsChild();
291 bool OffTheRecordProfileImpl::IsLegacySupervised() const {
292 return profile_->IsLegacySupervised();
295 PrefService* OffTheRecordProfileImpl::GetPrefs() {
296 return prefs_;
299 const PrefService* OffTheRecordProfileImpl::GetPrefs() const {
300 return prefs_;
303 PrefService* OffTheRecordProfileImpl::GetOffTheRecordPrefs() {
304 return prefs_;
307 DownloadManagerDelegate* OffTheRecordProfileImpl::GetDownloadManagerDelegate() {
308 return DownloadServiceFactory::GetForBrowserContext(this)->
309 GetDownloadManagerDelegate();
312 net::URLRequestContextGetter* OffTheRecordProfileImpl::GetRequestContext() {
313 return GetDefaultStoragePartition(this)->GetURLRequestContext();
316 net::URLRequestContextGetter* OffTheRecordProfileImpl::CreateRequestContext(
317 content::ProtocolHandlerMap* protocol_handlers,
318 content::URLRequestInterceptorScopedVector request_interceptors) {
319 return io_data_->CreateMainRequestContextGetter(
320 protocol_handlers, request_interceptors.Pass()).get();
323 net::URLRequestContextGetter*
324 OffTheRecordProfileImpl::GetRequestContextForRenderProcess(
325 int renderer_child_id) {
326 content::RenderProcessHost* rph = content::RenderProcessHost::FromID(
327 renderer_child_id);
328 return rph->GetStoragePartition()->GetURLRequestContext();
331 net::URLRequestContextGetter*
332 OffTheRecordProfileImpl::GetMediaRequestContext() {
333 // In OTR mode, media request context is the same as the original one.
334 return GetRequestContext();
337 net::URLRequestContextGetter*
338 OffTheRecordProfileImpl::GetMediaRequestContextForRenderProcess(
339 int renderer_child_id) {
340 // In OTR mode, media request context is the same as the original one.
341 return GetRequestContextForRenderProcess(renderer_child_id);
344 net::URLRequestContextGetter*
345 OffTheRecordProfileImpl::GetMediaRequestContextForStoragePartition(
346 const base::FilePath& partition_path,
347 bool in_memory) {
348 return io_data_->GetIsolatedAppRequestContextGetter(partition_path, in_memory)
349 .get();
352 net::URLRequestContextGetter*
353 OffTheRecordProfileImpl::GetRequestContextForExtensions() {
354 return io_data_->GetExtensionsRequestContextGetter().get();
357 net::URLRequestContextGetter*
358 OffTheRecordProfileImpl::CreateRequestContextForStoragePartition(
359 const base::FilePath& partition_path,
360 bool in_memory,
361 content::ProtocolHandlerMap* protocol_handlers,
362 content::URLRequestInterceptorScopedVector request_interceptors) {
363 return io_data_->CreateIsolatedAppRequestContextGetter(
364 partition_path,
365 in_memory,
366 protocol_handlers,
367 request_interceptors.Pass()).get();
370 content::ResourceContext* OffTheRecordProfileImpl::GetResourceContext() {
371 return io_data_->GetResourceContext();
374 net::SSLConfigService* OffTheRecordProfileImpl::GetSSLConfigService() {
375 return profile_->GetSSLConfigService();
378 HostContentSettingsMap* OffTheRecordProfileImpl::GetHostContentSettingsMap() {
379 DCHECK_CURRENTLY_ON(BrowserThread::UI);
380 // Retrieve the host content settings map of the parent profile in order to
381 // ensure the preferences have been migrated.
382 profile_->GetHostContentSettingsMap();
383 if (!host_content_settings_map_.get()) {
384 host_content_settings_map_ = new HostContentSettingsMap(GetPrefs(), true);
385 #if defined(ENABLE_EXTENSIONS)
386 ExtensionService* extension_service =
387 extensions::ExtensionSystem::Get(this)->extension_service();
388 if (extension_service) {
389 extension_service->RegisterContentSettings(
390 host_content_settings_map_.get());
392 #endif
393 #if defined(ENABLE_SUPERVISED_USERS)
394 SupervisedUserSettingsService* supervised_service =
395 SupervisedUserSettingsServiceFactory::GetForProfile(this);
396 scoped_ptr<content_settings::SupervisedProvider> supervised_provider(
397 new content_settings::SupervisedProvider(supervised_service));
398 host_content_settings_map_->RegisterProvider(
399 HostContentSettingsMap::SUPERVISED_PROVIDER,
400 supervised_provider.Pass());
401 #endif
403 return host_content_settings_map_.get();
406 content::BrowserPluginGuestManager* OffTheRecordProfileImpl::GetGuestManager() {
407 #if defined(ENABLE_EXTENSIONS)
408 return guest_view::GuestViewManager::FromBrowserContext(this);
409 #else
410 return NULL;
411 #endif
414 storage::SpecialStoragePolicy*
415 OffTheRecordProfileImpl::GetSpecialStoragePolicy() {
416 #if defined(ENABLE_EXTENSIONS)
417 return GetExtensionSpecialStoragePolicy();
418 #else
419 return NULL;
420 #endif
423 content::PushMessagingService*
424 OffTheRecordProfileImpl::GetPushMessagingService() {
425 // TODO(johnme): Support push messaging in incognito if possible.
426 return NULL;
429 content::SSLHostStateDelegate*
430 OffTheRecordProfileImpl::GetSSLHostStateDelegate() {
431 return ChromeSSLHostStateDelegateFactory::GetForProfile(this);
434 // TODO(mlamouri): we should all these BrowserContext implementation to Profile
435 // instead of repeating them inside all Profile implementations.
436 content::PermissionManager* OffTheRecordProfileImpl::GetPermissionManager() {
437 return PermissionManagerFactory::GetForProfile(this);
440 bool OffTheRecordProfileImpl::IsSameProfile(Profile* profile) {
441 return (profile == this) || (profile == profile_);
444 Time OffTheRecordProfileImpl::GetStartTime() const {
445 return start_time_;
448 void OffTheRecordProfileImpl::SetExitType(ExitType exit_type) {
451 base::FilePath OffTheRecordProfileImpl::last_selected_directory() {
452 const base::FilePath& directory = last_selected_directory_;
453 if (directory.empty()) {
454 return profile_->last_selected_directory();
456 return directory;
459 void OffTheRecordProfileImpl::set_last_selected_directory(
460 const base::FilePath& path) {
461 last_selected_directory_ = path;
464 bool OffTheRecordProfileImpl::WasCreatedByVersionOrLater(
465 const std::string& version) {
466 return profile_->WasCreatedByVersionOrLater(version);
469 Profile::ExitType OffTheRecordProfileImpl::GetLastSessionExitType() {
470 return profile_->GetLastSessionExitType();
473 #if defined(OS_CHROMEOS)
474 void OffTheRecordProfileImpl::ChangeAppLocale(const std::string& locale,
475 AppLocaleChangedVia) {
478 void OffTheRecordProfileImpl::OnLogin() {
481 void OffTheRecordProfileImpl::InitChromeOSPreferences() {
482 // The incognito profile shouldn't have Chrome OS's preferences.
483 // The preferences are associated with the regular user profile.
485 #endif // defined(OS_CHROMEOS)
487 PrefProxyConfigTracker* OffTheRecordProfileImpl::GetProxyConfigTracker() {
488 if (!pref_proxy_config_tracker_)
489 pref_proxy_config_tracker_.reset(CreateProxyConfigTracker());
490 return pref_proxy_config_tracker_.get();
493 chrome_browser_net::Predictor* OffTheRecordProfileImpl::GetNetworkPredictor() {
494 // We do not store information about websites visited in OTR profiles which
495 // is necessary for a Predictor, so we do not have a Predictor at all.
496 return NULL;
499 DevToolsNetworkControllerHandle*
500 OffTheRecordProfileImpl::GetDevToolsNetworkControllerHandle() {
501 return io_data_->GetDevToolsNetworkControllerHandle();
504 void OffTheRecordProfileImpl::ClearNetworkingHistorySince(
505 base::Time time,
506 const base::Closure& completion) {
507 // Nothing to do here, our transport security state is read-only.
508 // Still, fire the callback to indicate we have finished, otherwise the
509 // BrowsingDataRemover will never be destroyed and the dialog will never be
510 // closed. We must do this asynchronously in order to avoid reentrancy issues.
511 if (!completion.is_null()) {
512 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion);
516 GURL OffTheRecordProfileImpl::GetHomePage() {
517 return profile_->GetHomePage();
520 #if defined(OS_CHROMEOS)
521 // Special case of the OffTheRecordProfileImpl which is used while Guest
522 // session in CrOS.
523 class GuestSessionProfile : public OffTheRecordProfileImpl {
524 public:
525 explicit GuestSessionProfile(Profile* real_profile)
526 : OffTheRecordProfileImpl(real_profile) {
527 set_is_guest_profile(true);
530 ProfileType GetProfileType() const override { return GUEST_PROFILE; }
532 void InitChromeOSPreferences() override {
533 chromeos_preferences_.reset(new chromeos::Preferences());
534 chromeos_preferences_->Init(
535 this, user_manager::UserManager::Get()->GetActiveUser());
538 private:
539 // The guest user should be able to customize Chrome OS preferences.
540 scoped_ptr<chromeos::Preferences> chromeos_preferences_;
542 #endif
544 Profile* Profile::CreateOffTheRecordProfile() {
545 OffTheRecordProfileImpl* profile = NULL;
546 #if defined(OS_CHROMEOS)
547 if (IsGuestSession())
548 profile = new GuestSessionProfile(this);
549 #endif
550 if (!profile)
551 profile = new OffTheRecordProfileImpl(this);
552 profile->Init();
553 return profile;
556 void OffTheRecordProfileImpl::OnParentZoomLevelChanged(
557 const HostZoomMap::ZoomLevelChange& change) {
558 HostZoomMap* host_zoom_map = HostZoomMap::GetDefaultForBrowserContext(this);
559 switch (change.mode) {
560 case HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM:
561 return;
562 case HostZoomMap::ZOOM_CHANGED_FOR_HOST:
563 host_zoom_map->SetZoomLevelForHost(change.host, change.zoom_level);
564 return;
565 case HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST:
566 host_zoom_map->SetZoomLevelForHostAndScheme(change.scheme,
567 change.host,
568 change.zoom_level);
569 return;
570 case HostZoomMap::PAGE_SCALE_IS_ONE_CHANGED:
571 return;
575 void OffTheRecordProfileImpl::UpdateDefaultZoomLevel() {
576 HostZoomMap* host_zoom_map = HostZoomMap::GetDefaultForBrowserContext(this);
577 double default_zoom_level =
578 profile_->GetZoomLevelPrefs()->GetDefaultZoomLevelPref();
579 host_zoom_map->SetDefaultZoomLevel(default_zoom_level);
582 PrefProxyConfigTracker* OffTheRecordProfileImpl::CreateProxyConfigTracker() {
583 #if defined(OS_CHROMEOS)
584 if (chromeos::ProfileHelper::IsSigninProfile(this)) {
585 return ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
586 g_browser_process->local_state());
588 #endif // defined(OS_CHROMEOS)
589 return ProxyServiceFactory::CreatePrefProxyConfigTrackerOfProfile(
590 GetPrefs(), g_browser_process->local_state());