BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / profiles / off_the_record_profile_impl.cc
blob3560021aa8f7f027c55a4f5e16ece3f7b9a16761
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_util.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/syncable_prefs/pref_service_syncable.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_(PrefServiceSyncableIncognitoFromProfile(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 TrackZoomLevelsFromParent();
149 #if defined(ENABLE_PLUGINS)
150 ChromePluginServiceFilter::GetInstance()->RegisterResourceContext(
151 PluginPrefs::GetForProfile(this).get(),
152 io_data_->GetResourceContextNoInit());
153 #endif
155 #if defined(ENABLE_EXTENSIONS)
156 // Make the chrome//extension-icon/ resource available.
157 extensions::ExtensionIconSource* icon_source =
158 new extensions::ExtensionIconSource(profile_);
159 content::URLDataSource::Add(this, icon_source);
161 BrowserThread::PostTask(
162 BrowserThread::IO, FROM_HERE,
163 base::Bind(&NotifyOTRProfileCreatedOnIOThread, profile_, this));
164 #endif
166 // The DomDistillerViewerSource is not a normal WebUI so it must be registered
167 // as a URLDataSource early.
168 RegisterDomDistillerViewerSource(this);
171 OffTheRecordProfileImpl::~OffTheRecordProfileImpl() {
172 MaybeSendDestroyedNotification();
174 #if defined(ENABLE_PLUGINS)
175 ChromePluginServiceFilter::GetInstance()->UnregisterResourceContext(
176 io_data_->GetResourceContextNoInit());
177 #endif
179 BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
180 this);
182 #if defined(ENABLE_EXTENSIONS)
183 BrowserThread::PostTask(
184 BrowserThread::IO, FROM_HERE,
185 base::Bind(&NotifyOTRProfileDestroyedOnIOThread, profile_, this));
186 #endif
188 if (pref_proxy_config_tracker_)
189 pref_proxy_config_tracker_->DetachFromPrefService();
191 // Clears any data the network stack contains that may be related to the
192 // OTR session.
193 g_browser_process->io_thread()->ChangedToOnTheRecord();
196 void OffTheRecordProfileImpl::InitIoData() {
197 io_data_.reset(new OffTheRecordProfileIOData::Handle(this));
200 void OffTheRecordProfileImpl::TrackZoomLevelsFromParent() {
201 DCHECK_NE(INCOGNITO_PROFILE, profile_->GetProfileType());
203 // Here we only want to use zoom levels stored in the main-context's default
204 // storage partition. We're not interested in zoom levels in special
205 // partitions, e.g. those used by WebViewGuests.
206 HostZoomMap* host_zoom_map = HostZoomMap::GetDefaultForBrowserContext(this);
207 HostZoomMap* parent_host_zoom_map =
208 HostZoomMap::GetDefaultForBrowserContext(profile_);
209 host_zoom_map->CopyFrom(parent_host_zoom_map);
210 // Observe parent profile's HostZoomMap changes so they can also be applied
211 // to this profile's HostZoomMap.
212 track_zoom_subscription_ = parent_host_zoom_map->AddZoomLevelChangedCallback(
213 base::Bind(&OffTheRecordProfileImpl::OnParentZoomLevelChanged,
214 base::Unretained(this)));
215 if (!profile_->GetZoomLevelPrefs())
216 return;
218 // Also track changes to the parent profile's default zoom level.
219 parent_default_zoom_level_subscription_ =
220 profile_->GetZoomLevelPrefs()->RegisterDefaultZoomLevelCallback(
221 base::Bind(&OffTheRecordProfileImpl::UpdateDefaultZoomLevel,
222 base::Unretained(this)));
225 std::string OffTheRecordProfileImpl::GetProfileUserName() const {
226 // Incognito profile should not return the username.
227 return std::string();
230 Profile::ProfileType OffTheRecordProfileImpl::GetProfileType() const {
231 #if !defined(OS_CHROMEOS)
232 return profile_->IsGuestSession() ? GUEST_PROFILE : INCOGNITO_PROFILE;
233 #else
234 return INCOGNITO_PROFILE;
235 #endif
238 base::FilePath OffTheRecordProfileImpl::GetPath() const {
239 return profile_->GetPath();
242 scoped_ptr<content::ZoomLevelDelegate>
243 OffTheRecordProfileImpl::CreateZoomLevelDelegate(
244 const base::FilePath& partition_path) {
245 return make_scoped_ptr(new ChromeZoomLevelOTRDelegate(
246 ui_zoom::ZoomEventManager::GetForBrowserContext(this)->GetWeakPtr()));
249 scoped_refptr<base::SequencedTaskRunner>
250 OffTheRecordProfileImpl::GetIOTaskRunner() {
251 return profile_->GetIOTaskRunner();
254 bool OffTheRecordProfileImpl::IsOffTheRecord() const {
255 return true;
258 Profile* OffTheRecordProfileImpl::GetOffTheRecordProfile() {
259 return this;
262 void OffTheRecordProfileImpl::DestroyOffTheRecordProfile() {
263 // Suicide is bad!
264 NOTREACHED();
267 bool OffTheRecordProfileImpl::HasOffTheRecordProfile() {
268 return true;
271 Profile* OffTheRecordProfileImpl::GetOriginalProfile() {
272 return profile_;
275 ExtensionSpecialStoragePolicy*
276 OffTheRecordProfileImpl::GetExtensionSpecialStoragePolicy() {
277 return GetOriginalProfile()->GetExtensionSpecialStoragePolicy();
280 bool OffTheRecordProfileImpl::IsSupervised() const {
281 return profile_->IsSupervised();
284 bool OffTheRecordProfileImpl::IsChild() const {
285 // TODO(treib): If we ever allow incognito for child accounts, evaluate
286 // whether we want to just return false here.
287 return profile_->IsChild();
290 bool OffTheRecordProfileImpl::IsLegacySupervised() const {
291 return profile_->IsLegacySupervised();
294 PrefService* OffTheRecordProfileImpl::GetPrefs() {
295 return prefs_;
298 const PrefService* OffTheRecordProfileImpl::GetPrefs() const {
299 return prefs_;
302 PrefService* OffTheRecordProfileImpl::GetOffTheRecordPrefs() {
303 return prefs_;
306 DownloadManagerDelegate* OffTheRecordProfileImpl::GetDownloadManagerDelegate() {
307 return DownloadServiceFactory::GetForBrowserContext(this)->
308 GetDownloadManagerDelegate();
311 net::URLRequestContextGetter* OffTheRecordProfileImpl::GetRequestContext() {
312 return GetDefaultStoragePartition(this)->GetURLRequestContext();
315 net::URLRequestContextGetter* OffTheRecordProfileImpl::CreateRequestContext(
316 content::ProtocolHandlerMap* protocol_handlers,
317 content::URLRequestInterceptorScopedVector request_interceptors) {
318 return io_data_->CreateMainRequestContextGetter(
319 protocol_handlers, request_interceptors.Pass()).get();
322 net::URLRequestContextGetter*
323 OffTheRecordProfileImpl::GetRequestContextForRenderProcess(
324 int renderer_child_id) {
325 content::RenderProcessHost* rph = content::RenderProcessHost::FromID(
326 renderer_child_id);
327 return rph->GetStoragePartition()->GetURLRequestContext();
330 net::URLRequestContextGetter*
331 OffTheRecordProfileImpl::GetMediaRequestContext() {
332 // In OTR mode, media request context is the same as the original one.
333 return GetRequestContext();
336 net::URLRequestContextGetter*
337 OffTheRecordProfileImpl::GetMediaRequestContextForRenderProcess(
338 int renderer_child_id) {
339 // In OTR mode, media request context is the same as the original one.
340 return GetRequestContextForRenderProcess(renderer_child_id);
343 net::URLRequestContextGetter*
344 OffTheRecordProfileImpl::GetMediaRequestContextForStoragePartition(
345 const base::FilePath& partition_path,
346 bool in_memory) {
347 return io_data_->GetIsolatedAppRequestContextGetter(partition_path, in_memory)
348 .get();
351 net::URLRequestContextGetter*
352 OffTheRecordProfileImpl::GetRequestContextForExtensions() {
353 return io_data_->GetExtensionsRequestContextGetter().get();
356 net::URLRequestContextGetter*
357 OffTheRecordProfileImpl::CreateRequestContextForStoragePartition(
358 const base::FilePath& partition_path,
359 bool in_memory,
360 content::ProtocolHandlerMap* protocol_handlers,
361 content::URLRequestInterceptorScopedVector request_interceptors) {
362 return io_data_->CreateIsolatedAppRequestContextGetter(
363 partition_path,
364 in_memory,
365 protocol_handlers,
366 request_interceptors.Pass()).get();
369 content::ResourceContext* OffTheRecordProfileImpl::GetResourceContext() {
370 return io_data_->GetResourceContext();
373 net::SSLConfigService* OffTheRecordProfileImpl::GetSSLConfigService() {
374 return profile_->GetSSLConfigService();
377 content::BrowserPluginGuestManager* OffTheRecordProfileImpl::GetGuestManager() {
378 #if defined(ENABLE_EXTENSIONS)
379 return guest_view::GuestViewManager::FromBrowserContext(this);
380 #else
381 return NULL;
382 #endif
385 storage::SpecialStoragePolicy*
386 OffTheRecordProfileImpl::GetSpecialStoragePolicy() {
387 #if defined(ENABLE_EXTENSIONS)
388 return GetExtensionSpecialStoragePolicy();
389 #else
390 return NULL;
391 #endif
394 content::PushMessagingService*
395 OffTheRecordProfileImpl::GetPushMessagingService() {
396 // TODO(johnme): Support push messaging in incognito if possible.
397 return NULL;
400 content::SSLHostStateDelegate*
401 OffTheRecordProfileImpl::GetSSLHostStateDelegate() {
402 return ChromeSSLHostStateDelegateFactory::GetForProfile(this);
405 // TODO(mlamouri): we should all these BrowserContext implementation to Profile
406 // instead of repeating them inside all Profile implementations.
407 content::PermissionManager* OffTheRecordProfileImpl::GetPermissionManager() {
408 return PermissionManagerFactory::GetForProfile(this);
411 bool OffTheRecordProfileImpl::IsSameProfile(Profile* profile) {
412 return (profile == this) || (profile == profile_);
415 Time OffTheRecordProfileImpl::GetStartTime() const {
416 return start_time_;
419 void OffTheRecordProfileImpl::SetExitType(ExitType exit_type) {
422 base::FilePath OffTheRecordProfileImpl::last_selected_directory() {
423 const base::FilePath& directory = last_selected_directory_;
424 if (directory.empty()) {
425 return profile_->last_selected_directory();
427 return directory;
430 void OffTheRecordProfileImpl::set_last_selected_directory(
431 const base::FilePath& path) {
432 last_selected_directory_ = path;
435 bool OffTheRecordProfileImpl::WasCreatedByVersionOrLater(
436 const std::string& version) {
437 return profile_->WasCreatedByVersionOrLater(version);
440 Profile::ExitType OffTheRecordProfileImpl::GetLastSessionExitType() {
441 return profile_->GetLastSessionExitType();
444 #if defined(OS_CHROMEOS)
445 void OffTheRecordProfileImpl::ChangeAppLocale(const std::string& locale,
446 AppLocaleChangedVia) {
449 void OffTheRecordProfileImpl::OnLogin() {
452 void OffTheRecordProfileImpl::InitChromeOSPreferences() {
453 // The incognito profile shouldn't have Chrome OS's preferences.
454 // The preferences are associated with the regular user profile.
456 #endif // defined(OS_CHROMEOS)
458 PrefProxyConfigTracker* OffTheRecordProfileImpl::GetProxyConfigTracker() {
459 if (!pref_proxy_config_tracker_)
460 pref_proxy_config_tracker_.reset(CreateProxyConfigTracker());
461 return pref_proxy_config_tracker_.get();
464 chrome_browser_net::Predictor* OffTheRecordProfileImpl::GetNetworkPredictor() {
465 // We do not store information about websites visited in OTR profiles which
466 // is necessary for a Predictor, so we do not have a Predictor at all.
467 return NULL;
470 DevToolsNetworkControllerHandle*
471 OffTheRecordProfileImpl::GetDevToolsNetworkControllerHandle() {
472 return io_data_->GetDevToolsNetworkControllerHandle();
475 void OffTheRecordProfileImpl::ClearNetworkingHistorySince(
476 base::Time time,
477 const base::Closure& completion) {
478 // Nothing to do here, our transport security state is read-only.
479 // Still, fire the callback to indicate we have finished, otherwise the
480 // BrowsingDataRemover will never be destroyed and the dialog will never be
481 // closed. We must do this asynchronously in order to avoid reentrancy issues.
482 if (!completion.is_null()) {
483 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion);
487 GURL OffTheRecordProfileImpl::GetHomePage() {
488 return profile_->GetHomePage();
491 #if defined(OS_CHROMEOS)
492 // Special case of the OffTheRecordProfileImpl which is used while Guest
493 // session in CrOS.
494 class GuestSessionProfile : public OffTheRecordProfileImpl {
495 public:
496 explicit GuestSessionProfile(Profile* real_profile)
497 : OffTheRecordProfileImpl(real_profile) {
498 set_is_guest_profile(true);
501 ProfileType GetProfileType() const override { return GUEST_PROFILE; }
503 void InitChromeOSPreferences() override {
504 chromeos_preferences_.reset(new chromeos::Preferences());
505 chromeos_preferences_->Init(
506 this, user_manager::UserManager::Get()->GetActiveUser());
509 private:
510 // The guest user should be able to customize Chrome OS preferences.
511 scoped_ptr<chromeos::Preferences> chromeos_preferences_;
513 #endif
515 Profile* Profile::CreateOffTheRecordProfile() {
516 OffTheRecordProfileImpl* profile = NULL;
517 #if defined(OS_CHROMEOS)
518 if (IsGuestSession())
519 profile = new GuestSessionProfile(this);
520 #endif
521 if (!profile)
522 profile = new OffTheRecordProfileImpl(this);
523 profile->Init();
524 return profile;
527 void OffTheRecordProfileImpl::OnParentZoomLevelChanged(
528 const HostZoomMap::ZoomLevelChange& change) {
529 HostZoomMap* host_zoom_map = HostZoomMap::GetDefaultForBrowserContext(this);
530 switch (change.mode) {
531 case HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM:
532 return;
533 case HostZoomMap::ZOOM_CHANGED_FOR_HOST:
534 host_zoom_map->SetZoomLevelForHost(change.host, change.zoom_level);
535 return;
536 case HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST:
537 host_zoom_map->SetZoomLevelForHostAndScheme(change.scheme,
538 change.host,
539 change.zoom_level);
540 return;
541 case HostZoomMap::PAGE_SCALE_IS_ONE_CHANGED:
542 return;
546 void OffTheRecordProfileImpl::UpdateDefaultZoomLevel() {
547 HostZoomMap* host_zoom_map = HostZoomMap::GetDefaultForBrowserContext(this);
548 double default_zoom_level =
549 profile_->GetZoomLevelPrefs()->GetDefaultZoomLevelPref();
550 host_zoom_map->SetDefaultZoomLevel(default_zoom_level);
551 // HostZoomMap does not trigger zoom notification events when the default
552 // zoom level is set, so we need to do it here.
553 ui_zoom::ZoomEventManager::GetForBrowserContext(this)
554 ->OnDefaultZoomLevelChanged();
557 PrefProxyConfigTracker* OffTheRecordProfileImpl::CreateProxyConfigTracker() {
558 #if defined(OS_CHROMEOS)
559 if (chromeos::ProfileHelper::IsSigninProfile(this)) {
560 return ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
561 g_browser_process->local_state());
563 #endif // defined(OS_CHROMEOS)
564 return ProxyServiceFactory::CreatePrefProxyConfigTrackerOfProfile(
565 GetPrefs(), g_browser_process->local_state());