[sql] Remove _HAS_EXCEPTIONS=0 from build info.
[chromium-blink-merge.git] / chrome / browser / profiles / off_the_record_profile_impl.cc
blob32bc6248c57f1741ede56038c3c2bd5b0400a0b3
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 // TODO(oshima): Remove the need to eagerly initialize the request context
147 // getter. chromeos::OnlineAttempt is illegally trying to access this
148 // Profile member from a thread other than the UI thread, so we need to
149 // prevent a race.
150 #if defined(OS_CHROMEOS)
151 GetRequestContext();
152 #endif // defined(OS_CHROMEOS)
154 TrackZoomLevelsFromParent();
156 #if defined(ENABLE_PLUGINS)
157 ChromePluginServiceFilter::GetInstance()->RegisterResourceContext(
158 PluginPrefs::GetForProfile(this).get(),
159 io_data_->GetResourceContextNoInit());
160 #endif
162 #if defined(ENABLE_EXTENSIONS)
163 // Make the chrome//extension-icon/ resource available.
164 extensions::ExtensionIconSource* icon_source =
165 new extensions::ExtensionIconSource(profile_);
166 content::URLDataSource::Add(this, icon_source);
168 BrowserThread::PostTask(
169 BrowserThread::IO, FROM_HERE,
170 base::Bind(&NotifyOTRProfileCreatedOnIOThread, profile_, this));
171 #endif
173 // The DomDistillerViewerSource is not a normal WebUI so it must be registered
174 // as a URLDataSource early.
175 RegisterDomDistillerViewerSource(this);
178 OffTheRecordProfileImpl::~OffTheRecordProfileImpl() {
179 MaybeSendDestroyedNotification();
181 #if defined(ENABLE_PLUGINS)
182 ChromePluginServiceFilter::GetInstance()->UnregisterResourceContext(
183 io_data_->GetResourceContextNoInit());
184 #endif
186 BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
187 this);
189 #if defined(ENABLE_EXTENSIONS)
190 BrowserThread::PostTask(
191 BrowserThread::IO, FROM_HERE,
192 base::Bind(&NotifyOTRProfileDestroyedOnIOThread, profile_, this));
193 #endif
195 if (host_content_settings_map_.get())
196 host_content_settings_map_->ShutdownOnUIThread();
198 if (pref_proxy_config_tracker_)
199 pref_proxy_config_tracker_->DetachFromPrefService();
201 // Clears any data the network stack contains that may be related to the
202 // OTR session.
203 g_browser_process->io_thread()->ChangedToOnTheRecord();
206 void OffTheRecordProfileImpl::InitIoData() {
207 io_data_.reset(new OffTheRecordProfileIOData::Handle(this));
210 void OffTheRecordProfileImpl::TrackZoomLevelsFromParent() {
211 DCHECK_NE(INCOGNITO_PROFILE, profile_->GetProfileType());
213 // Here we only want to use zoom levels stored in the main-context's default
214 // storage partition. We're not interested in zoom levels in special
215 // partitions, e.g. those used by WebViewGuests.
216 HostZoomMap* host_zoom_map = HostZoomMap::GetDefaultForBrowserContext(this);
217 HostZoomMap* parent_host_zoom_map =
218 HostZoomMap::GetDefaultForBrowserContext(profile_);
219 host_zoom_map->CopyFrom(parent_host_zoom_map);
220 // Observe parent profile's HostZoomMap changes so they can also be applied
221 // to this profile's HostZoomMap.
222 track_zoom_subscription_ = parent_host_zoom_map->AddZoomLevelChangedCallback(
223 base::Bind(&OffTheRecordProfileImpl::OnParentZoomLevelChanged,
224 base::Unretained(this)));
225 if (!profile_->GetZoomLevelPrefs())
226 return;
228 // Also track changes to the parent profile's default zoom level.
229 parent_default_zoom_level_subscription_ =
230 profile_->GetZoomLevelPrefs()->RegisterDefaultZoomLevelCallback(
231 base::Bind(&OffTheRecordProfileImpl::UpdateDefaultZoomLevel,
232 base::Unretained(this)));
235 std::string OffTheRecordProfileImpl::GetProfileUserName() const {
236 // Incognito profile should not return the username.
237 return std::string();
240 Profile::ProfileType OffTheRecordProfileImpl::GetProfileType() const {
241 #if !defined(OS_CHROMEOS)
242 return profile_->IsGuestSession() ? GUEST_PROFILE : INCOGNITO_PROFILE;
243 #else
244 return INCOGNITO_PROFILE;
245 #endif
248 base::FilePath OffTheRecordProfileImpl::GetPath() const {
249 return profile_->GetPath();
252 scoped_ptr<content::ZoomLevelDelegate>
253 OffTheRecordProfileImpl::CreateZoomLevelDelegate(
254 const base::FilePath& partition_path) {
255 return make_scoped_ptr(new chrome::ChromeZoomLevelOTRDelegate(
256 ui_zoom::ZoomEventManager::GetForBrowserContext(this)->GetWeakPtr()));
259 scoped_refptr<base::SequencedTaskRunner>
260 OffTheRecordProfileImpl::GetIOTaskRunner() {
261 return profile_->GetIOTaskRunner();
264 bool OffTheRecordProfileImpl::IsOffTheRecord() const {
265 return true;
268 Profile* OffTheRecordProfileImpl::GetOffTheRecordProfile() {
269 return this;
272 void OffTheRecordProfileImpl::DestroyOffTheRecordProfile() {
273 // Suicide is bad!
274 NOTREACHED();
277 bool OffTheRecordProfileImpl::HasOffTheRecordProfile() {
278 return true;
281 Profile* OffTheRecordProfileImpl::GetOriginalProfile() {
282 return profile_;
285 ExtensionSpecialStoragePolicy*
286 OffTheRecordProfileImpl::GetExtensionSpecialStoragePolicy() {
287 return GetOriginalProfile()->GetExtensionSpecialStoragePolicy();
290 bool OffTheRecordProfileImpl::IsSupervised() const {
291 return profile_->IsSupervised();
294 bool OffTheRecordProfileImpl::IsChild() const {
295 // TODO(treib): If we ever allow incognito for child accounts, evaluate
296 // whether we want to just return false here.
297 return profile_->IsChild();
300 bool OffTheRecordProfileImpl::IsLegacySupervised() const {
301 return profile_->IsLegacySupervised();
304 PrefService* OffTheRecordProfileImpl::GetPrefs() {
305 return prefs_;
308 const PrefService* OffTheRecordProfileImpl::GetPrefs() const {
309 return prefs_;
312 PrefService* OffTheRecordProfileImpl::GetOffTheRecordPrefs() {
313 return prefs_;
316 DownloadManagerDelegate* OffTheRecordProfileImpl::GetDownloadManagerDelegate() {
317 return DownloadServiceFactory::GetForBrowserContext(this)->
318 GetDownloadManagerDelegate();
321 net::URLRequestContextGetter* OffTheRecordProfileImpl::GetRequestContext() {
322 return GetDefaultStoragePartition(this)->GetURLRequestContext();
325 net::URLRequestContextGetter* OffTheRecordProfileImpl::CreateRequestContext(
326 content::ProtocolHandlerMap* protocol_handlers,
327 content::URLRequestInterceptorScopedVector request_interceptors) {
328 return io_data_->CreateMainRequestContextGetter(
329 protocol_handlers, request_interceptors.Pass()).get();
332 net::URLRequestContextGetter*
333 OffTheRecordProfileImpl::GetRequestContextForRenderProcess(
334 int renderer_child_id) {
335 content::RenderProcessHost* rph = content::RenderProcessHost::FromID(
336 renderer_child_id);
337 return rph->GetStoragePartition()->GetURLRequestContext();
340 net::URLRequestContextGetter*
341 OffTheRecordProfileImpl::GetMediaRequestContext() {
342 // In OTR mode, media request context is the same as the original one.
343 return GetRequestContext();
346 net::URLRequestContextGetter*
347 OffTheRecordProfileImpl::GetMediaRequestContextForRenderProcess(
348 int renderer_child_id) {
349 // In OTR mode, media request context is the same as the original one.
350 return GetRequestContextForRenderProcess(renderer_child_id);
353 net::URLRequestContextGetter*
354 OffTheRecordProfileImpl::GetMediaRequestContextForStoragePartition(
355 const base::FilePath& partition_path,
356 bool in_memory) {
357 return io_data_->GetIsolatedAppRequestContextGetter(partition_path, in_memory)
358 .get();
361 net::URLRequestContextGetter*
362 OffTheRecordProfileImpl::GetRequestContextForExtensions() {
363 return io_data_->GetExtensionsRequestContextGetter().get();
366 net::URLRequestContextGetter*
367 OffTheRecordProfileImpl::CreateRequestContextForStoragePartition(
368 const base::FilePath& partition_path,
369 bool in_memory,
370 content::ProtocolHandlerMap* protocol_handlers,
371 content::URLRequestInterceptorScopedVector request_interceptors) {
372 return io_data_->CreateIsolatedAppRequestContextGetter(
373 partition_path,
374 in_memory,
375 protocol_handlers,
376 request_interceptors.Pass()).get();
379 content::ResourceContext* OffTheRecordProfileImpl::GetResourceContext() {
380 return io_data_->GetResourceContext();
383 net::SSLConfigService* OffTheRecordProfileImpl::GetSSLConfigService() {
384 return profile_->GetSSLConfigService();
387 HostContentSettingsMap* OffTheRecordProfileImpl::GetHostContentSettingsMap() {
388 DCHECK_CURRENTLY_ON(BrowserThread::UI);
389 // Retrieve the host content settings map of the parent profile in order to
390 // ensure the preferences have been migrated.
391 profile_->GetHostContentSettingsMap();
392 if (!host_content_settings_map_.get()) {
393 host_content_settings_map_ = new HostContentSettingsMap(GetPrefs(), true);
394 #if defined(ENABLE_EXTENSIONS)
395 ExtensionService* extension_service =
396 extensions::ExtensionSystem::Get(this)->extension_service();
397 if (extension_service) {
398 extension_service->RegisterContentSettings(
399 host_content_settings_map_.get());
401 #endif
402 #if defined(ENABLE_SUPERVISED_USERS)
403 SupervisedUserSettingsService* supervised_service =
404 SupervisedUserSettingsServiceFactory::GetForProfile(this);
405 scoped_ptr<content_settings::SupervisedProvider> supervised_provider(
406 new content_settings::SupervisedProvider(supervised_service));
407 host_content_settings_map_->RegisterProvider(
408 HostContentSettingsMap::SUPERVISED_PROVIDER,
409 supervised_provider.Pass());
410 #endif
412 return host_content_settings_map_.get();
415 content::BrowserPluginGuestManager* OffTheRecordProfileImpl::GetGuestManager() {
416 #if defined(ENABLE_EXTENSIONS)
417 return guest_view::GuestViewManager::FromBrowserContext(this);
418 #else
419 return NULL;
420 #endif
423 storage::SpecialStoragePolicy*
424 OffTheRecordProfileImpl::GetSpecialStoragePolicy() {
425 #if defined(ENABLE_EXTENSIONS)
426 return GetExtensionSpecialStoragePolicy();
427 #else
428 return NULL;
429 #endif
432 content::PushMessagingService*
433 OffTheRecordProfileImpl::GetPushMessagingService() {
434 // TODO(johnme): Support push messaging in incognito if possible.
435 return NULL;
438 content::SSLHostStateDelegate*
439 OffTheRecordProfileImpl::GetSSLHostStateDelegate() {
440 return ChromeSSLHostStateDelegateFactory::GetForProfile(this);
443 // TODO(mlamouri): we should all these BrowserContext implementation to Profile
444 // instead of repeating them inside all Profile implementations.
445 content::PermissionManager* OffTheRecordProfileImpl::GetPermissionManager() {
446 return PermissionManagerFactory::GetForProfile(this);
449 bool OffTheRecordProfileImpl::IsSameProfile(Profile* profile) {
450 return (profile == this) || (profile == profile_);
453 Time OffTheRecordProfileImpl::GetStartTime() const {
454 return start_time_;
457 void OffTheRecordProfileImpl::SetExitType(ExitType exit_type) {
460 base::FilePath OffTheRecordProfileImpl::last_selected_directory() {
461 const base::FilePath& directory = last_selected_directory_;
462 if (directory.empty()) {
463 return profile_->last_selected_directory();
465 return directory;
468 void OffTheRecordProfileImpl::set_last_selected_directory(
469 const base::FilePath& path) {
470 last_selected_directory_ = path;
473 bool OffTheRecordProfileImpl::WasCreatedByVersionOrLater(
474 const std::string& version) {
475 return profile_->WasCreatedByVersionOrLater(version);
478 Profile::ExitType OffTheRecordProfileImpl::GetLastSessionExitType() {
479 return profile_->GetLastSessionExitType();
482 #if defined(OS_CHROMEOS)
483 void OffTheRecordProfileImpl::ChangeAppLocale(const std::string& locale,
484 AppLocaleChangedVia) {
487 void OffTheRecordProfileImpl::OnLogin() {
490 void OffTheRecordProfileImpl::InitChromeOSPreferences() {
491 // The incognito profile shouldn't have Chrome OS's preferences.
492 // The preferences are associated with the regular user profile.
494 #endif // defined(OS_CHROMEOS)
496 PrefProxyConfigTracker* OffTheRecordProfileImpl::GetProxyConfigTracker() {
497 if (!pref_proxy_config_tracker_)
498 pref_proxy_config_tracker_.reset(CreateProxyConfigTracker());
499 return pref_proxy_config_tracker_.get();
502 chrome_browser_net::Predictor* OffTheRecordProfileImpl::GetNetworkPredictor() {
503 // We do not store information about websites visited in OTR profiles which
504 // is necessary for a Predictor, so we do not have a Predictor at all.
505 return NULL;
508 DevToolsNetworkController*
509 OffTheRecordProfileImpl::GetDevToolsNetworkController() {
510 return io_data_->GetDevToolsNetworkController();
513 void OffTheRecordProfileImpl::ClearNetworkingHistorySince(
514 base::Time time,
515 const base::Closure& completion) {
516 // Nothing to do here, our transport security state is read-only.
517 // Still, fire the callback to indicate we have finished, otherwise the
518 // BrowsingDataRemover will never be destroyed and the dialog will never be
519 // closed. We must do this asynchronously in order to avoid reentrancy issues.
520 if (!completion.is_null()) {
521 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion);
525 GURL OffTheRecordProfileImpl::GetHomePage() {
526 return profile_->GetHomePage();
529 #if defined(OS_CHROMEOS)
530 // Special case of the OffTheRecordProfileImpl which is used while Guest
531 // session in CrOS.
532 class GuestSessionProfile : public OffTheRecordProfileImpl {
533 public:
534 explicit GuestSessionProfile(Profile* real_profile)
535 : OffTheRecordProfileImpl(real_profile) {
536 set_is_guest_profile(true);
539 ProfileType GetProfileType() const override { return GUEST_PROFILE; }
541 void InitChromeOSPreferences() override {
542 chromeos_preferences_.reset(new chromeos::Preferences());
543 chromeos_preferences_->Init(
544 this, user_manager::UserManager::Get()->GetActiveUser());
547 private:
548 // The guest user should be able to customize Chrome OS preferences.
549 scoped_ptr<chromeos::Preferences> chromeos_preferences_;
551 #endif
553 Profile* Profile::CreateOffTheRecordProfile() {
554 OffTheRecordProfileImpl* profile = NULL;
555 #if defined(OS_CHROMEOS)
556 if (IsGuestSession())
557 profile = new GuestSessionProfile(this);
558 #endif
559 if (!profile)
560 profile = new OffTheRecordProfileImpl(this);
561 profile->Init();
562 return profile;
565 void OffTheRecordProfileImpl::OnParentZoomLevelChanged(
566 const HostZoomMap::ZoomLevelChange& change) {
567 HostZoomMap* host_zoom_map = HostZoomMap::GetDefaultForBrowserContext(this);
568 switch (change.mode) {
569 case HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM:
570 return;
571 case HostZoomMap::ZOOM_CHANGED_FOR_HOST:
572 host_zoom_map->SetZoomLevelForHost(change.host, change.zoom_level);
573 return;
574 case HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST:
575 host_zoom_map->SetZoomLevelForHostAndScheme(change.scheme,
576 change.host,
577 change.zoom_level);
578 return;
579 case HostZoomMap::PAGE_SCALE_IS_ONE_CHANGED:
580 return;
584 void OffTheRecordProfileImpl::UpdateDefaultZoomLevel() {
585 HostZoomMap* host_zoom_map = HostZoomMap::GetDefaultForBrowserContext(this);
586 double default_zoom_level =
587 profile_->GetZoomLevelPrefs()->GetDefaultZoomLevelPref();
588 host_zoom_map->SetDefaultZoomLevel(default_zoom_level);
591 PrefProxyConfigTracker* OffTheRecordProfileImpl::CreateProxyConfigTracker() {
592 #if defined(OS_CHROMEOS)
593 if (chromeos::ProfileHelper::IsSigninProfile(this)) {
594 return ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
595 g_browser_process->local_state());
597 #endif // defined(OS_CHROMEOS)
598 return ProxyServiceFactory::CreatePrefProxyConfigTrackerOfProfile(
599 GetPrefs(), g_browser_process->local_state());