[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / profiles / off_the_record_profile_impl.cc
blobdc8c3eef1805bf0a89b0c53857cdafea09a1c6e1
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/path_service.h"
13 #include "base/prefs/json_pref_store.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.h"
16 #include "build/build_config.h"
17 #include "chrome/browser/background/background_contents_service_factory.h"
18 #include "chrome/browser/browser_process.h"
19 #include "chrome/browser/content_settings/host_content_settings_map.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/extensions/api/web_request/web_request_api.h"
24 #include "chrome/browser/extensions/extension_service.h"
25 #include "chrome/browser/extensions/extension_special_storage_policy.h"
26 #include "chrome/browser/geolocation/chrome_geolocation_permission_context.h"
27 #include "chrome/browser/geolocation/chrome_geolocation_permission_context_factory.h"
28 #include "chrome/browser/guest_view/guest_view_manager.h"
29 #include "chrome/browser/io_thread.h"
30 #include "chrome/browser/media/chrome_midi_permission_context.h"
31 #include "chrome/browser/media/chrome_midi_permission_context_factory.h"
32 #include "chrome/browser/net/pref_proxy_config_tracker.h"
33 #include "chrome/browser/net/proxy_service_factory.h"
34 #include "chrome/browser/plugins/chrome_plugin_service_filter.h"
35 #include "chrome/browser/plugins/plugin_prefs.h"
36 #include "chrome/browser/prefs/incognito_mode_prefs.h"
37 #include "chrome/browser/prefs/pref_service_syncable.h"
38 #include "chrome/browser/themes/theme_service.h"
39 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
40 #include "chrome/common/chrome_constants.h"
41 #include "chrome/common/chrome_paths.h"
42 #include "chrome/common/chrome_switches.h"
43 #include "chrome/common/pref_names.h"
44 #include "chrome/common/render_messages.h"
45 #include "components/keyed_service/content/browser_context_dependency_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 "extensions/browser/extension_system.h"
54 #include "extensions/common/extension.h"
55 #include "net/http/http_server_properties.h"
56 #include "net/http/transport_security_state.h"
57 #include "webkit/browser/database/database_tracker.h"
59 #if defined(OS_ANDROID)
60 #include "chrome/browser/media/protected_media_identifier_permission_context.h"
61 #include "chrome/browser/media/protected_media_identifier_permission_context_factory.h"
62 #endif // defined(OS_ANDROID)
64 #if defined(OS_ANDROID) || defined(OS_IOS)
65 #include "base/prefs/scoped_user_pref_update.h"
66 #include "chrome/browser/prefs/proxy_prefs.h"
67 #endif // defined(OS_ANDROID) || defined(OS_IOS)
69 #if defined(OS_CHROMEOS)
70 #include "chrome/browser/chromeos/preferences.h"
71 #include "chrome/browser/chromeos/profiles/profile_helper.h"
72 #endif
74 #if defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS)
75 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
76 #endif
78 using content::BrowserThread;
79 using content::DownloadManagerDelegate;
80 using content::HostZoomMap;
82 namespace {
84 void NotifyOTRProfileCreatedOnIOThread(void* original_profile,
85 void* otr_profile) {
86 ExtensionWebRequestEventRouter::GetInstance()->OnOTRProfileCreated(
87 original_profile, otr_profile);
90 void NotifyOTRProfileDestroyedOnIOThread(void* original_profile,
91 void* otr_profile) {
92 ExtensionWebRequestEventRouter::GetInstance()->OnOTRProfileDestroyed(
93 original_profile, otr_profile);
96 } // namespace
98 OffTheRecordProfileImpl::OffTheRecordProfileImpl(Profile* real_profile)
99 : profile_(real_profile),
100 prefs_(PrefServiceSyncable::IncognitoFromProfile(real_profile)),
101 start_time_(Time::Now()) {
102 // Register on BrowserContext.
103 user_prefs::UserPrefs::Set(this, prefs_);
106 void OffTheRecordProfileImpl::Init() {
107 // The construction of OffTheRecordProfileIOData::Handle needs the profile
108 // type returned by this->GetProfileType(). Since GetProfileType() is a
109 // virtual member function, we cannot call the function defined in the most
110 // derived class (e.g. GuestSessionProfile) until a ctor finishes. Thus,
111 // we have to instantiate OffTheRecordProfileIOData::Handle here after a ctor.
112 InitIoData();
114 #if defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS)
115 // Because UserCloudPolicyManager is in a component, it cannot access
116 // GetOriginalProfile. Instead, we have to inject this relation here.
117 policy::UserCloudPolicyManagerFactory::RegisterForOffTheRecordBrowserContext(
118 this->GetOriginalProfile(), this);
119 #endif
121 BrowserContextDependencyManager::GetInstance()->CreateBrowserContextServices(
122 this);
124 DCHECK_NE(IncognitoModePrefs::DISABLED,
125 IncognitoModePrefs::GetAvailability(profile_->GetPrefs()));
127 #if defined(OS_ANDROID) || defined(OS_IOS)
128 UseSystemProxy();
129 #endif // defined(OS_ANDROID) || defined(OS_IOS)
131 // TODO(oshima): Remove the need to eagerly initialize the request context
132 // getter. chromeos::OnlineAttempt is illegally trying to access this
133 // Profile member from a thread other than the UI thread, so we need to
134 // prevent a race.
135 #if defined(OS_CHROMEOS)
136 GetRequestContext();
137 #endif // defined(OS_CHROMEOS)
139 InitHostZoomMap();
141 // Make the chrome//extension-icon/ resource available.
142 extensions::ExtensionIconSource* icon_source =
143 new extensions::ExtensionIconSource(profile_);
144 content::URLDataSource::Add(this, icon_source);
146 #if defined(ENABLE_PLUGINS)
147 ChromePluginServiceFilter::GetInstance()->RegisterResourceContext(
148 PluginPrefs::GetForProfile(this).get(),
149 io_data_->GetResourceContextNoInit());
150 #endif
152 BrowserThread::PostTask(
153 BrowserThread::IO, FROM_HERE,
154 base::Bind(&NotifyOTRProfileCreatedOnIOThread, profile_, this));
157 OffTheRecordProfileImpl::~OffTheRecordProfileImpl() {
158 MaybeSendDestroyedNotification();
160 #if defined(ENABLE_PLUGINS)
161 ChromePluginServiceFilter::GetInstance()->UnregisterResourceContext(
162 io_data_->GetResourceContextNoInit());
163 #endif
165 BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
166 this);
168 BrowserThread::PostTask(
169 BrowserThread::IO, FROM_HERE,
170 base::Bind(&NotifyOTRProfileDestroyedOnIOThread, profile_, this));
172 if (host_content_settings_map_.get())
173 host_content_settings_map_->ShutdownOnUIThread();
175 if (pref_proxy_config_tracker_)
176 pref_proxy_config_tracker_->DetachFromPrefService();
178 // Clears any data the network stack contains that may be related to the
179 // OTR session.
180 g_browser_process->io_thread()->ChangedToOnTheRecord();
183 void OffTheRecordProfileImpl::InitIoData() {
184 io_data_.reset(new OffTheRecordProfileIOData::Handle(this));
187 void OffTheRecordProfileImpl::InitHostZoomMap() {
188 HostZoomMap* host_zoom_map = HostZoomMap::GetForBrowserContext(this);
189 HostZoomMap* parent_host_zoom_map =
190 HostZoomMap::GetForBrowserContext(profile_);
191 host_zoom_map->CopyFrom(parent_host_zoom_map);
192 // Observe parent's HZM change for propagating change of parent's
193 // change to this HZM.
194 zoom_subscription_ = parent_host_zoom_map->AddZoomLevelChangedCallback(
195 base::Bind(&OffTheRecordProfileImpl::OnZoomLevelChanged,
196 base::Unretained(this)));
199 #if defined(OS_ANDROID) || defined(OS_IOS)
200 void OffTheRecordProfileImpl::UseSystemProxy() {
201 // Force the use of the system-assigned proxy when off the record.
202 const char kProxyMode[] = "mode";
203 const char kProxyServer[] = "server";
204 const char kProxyBypassList[] = "bypass_list";
205 const char kProxyPacUrl[] = "pac_url";
206 DictionaryPrefUpdate update(prefs_, prefs::kProxy);
207 base::DictionaryValue* dict = update.Get();
208 dict->SetString(kProxyMode, ProxyModeToString(ProxyPrefs::MODE_SYSTEM));
209 dict->SetString(kProxyPacUrl, "");
210 dict->SetString(kProxyServer, "");
211 dict->SetString(kProxyBypassList, "");
213 #endif // defined(OS_ANDROID) || defined(OS_IOS)
215 std::string OffTheRecordProfileImpl::GetProfileName() {
216 // Incognito profile should not return the profile name.
217 return std::string();
220 Profile::ProfileType OffTheRecordProfileImpl::GetProfileType() const {
221 return INCOGNITO_PROFILE;
224 base::FilePath OffTheRecordProfileImpl::GetPath() const {
225 return profile_->GetPath();
228 scoped_refptr<base::SequencedTaskRunner>
229 OffTheRecordProfileImpl::GetIOTaskRunner() {
230 return profile_->GetIOTaskRunner();
233 bool OffTheRecordProfileImpl::IsOffTheRecord() const {
234 return true;
237 Profile* OffTheRecordProfileImpl::GetOffTheRecordProfile() {
238 return this;
241 void OffTheRecordProfileImpl::DestroyOffTheRecordProfile() {
242 // Suicide is bad!
243 NOTREACHED();
246 bool OffTheRecordProfileImpl::HasOffTheRecordProfile() {
247 return true;
250 Profile* OffTheRecordProfileImpl::GetOriginalProfile() {
251 return profile_;
254 ExtensionService* OffTheRecordProfileImpl::GetExtensionService() {
255 return extensions::ExtensionSystem::Get(this)->extension_service();
258 ExtensionSpecialStoragePolicy*
259 OffTheRecordProfileImpl::GetExtensionSpecialStoragePolicy() {
260 return GetOriginalProfile()->GetExtensionSpecialStoragePolicy();
263 bool OffTheRecordProfileImpl::IsManaged() {
264 return GetOriginalProfile()->IsManaged();
267 PrefService* OffTheRecordProfileImpl::GetPrefs() {
268 return prefs_;
271 PrefService* OffTheRecordProfileImpl::GetOffTheRecordPrefs() {
272 return prefs_;
275 DownloadManagerDelegate* OffTheRecordProfileImpl::GetDownloadManagerDelegate() {
276 return DownloadServiceFactory::GetForBrowserContext(this)->
277 GetDownloadManagerDelegate();
280 net::URLRequestContextGetter* OffTheRecordProfileImpl::GetRequestContext() {
281 return GetDefaultStoragePartition(this)->GetURLRequestContext();
284 net::URLRequestContextGetter* OffTheRecordProfileImpl::CreateRequestContext(
285 content::ProtocolHandlerMap* protocol_handlers,
286 content::ProtocolHandlerScopedVector protocol_interceptors) {
287 return io_data_->CreateMainRequestContextGetter(
288 protocol_handlers, protocol_interceptors.Pass()).get();
291 net::URLRequestContextGetter*
292 OffTheRecordProfileImpl::GetRequestContextForRenderProcess(
293 int renderer_child_id) {
294 content::RenderProcessHost* rph = content::RenderProcessHost::FromID(
295 renderer_child_id);
296 return rph->GetStoragePartition()->GetURLRequestContext();
299 net::URLRequestContextGetter*
300 OffTheRecordProfileImpl::GetMediaRequestContext() {
301 // In OTR mode, media request context is the same as the original one.
302 return GetRequestContext();
305 net::URLRequestContextGetter*
306 OffTheRecordProfileImpl::GetMediaRequestContextForRenderProcess(
307 int renderer_child_id) {
308 // In OTR mode, media request context is the same as the original one.
309 return GetRequestContextForRenderProcess(renderer_child_id);
312 net::URLRequestContextGetter*
313 OffTheRecordProfileImpl::GetMediaRequestContextForStoragePartition(
314 const base::FilePath& partition_path,
315 bool in_memory) {
316 return io_data_->GetIsolatedAppRequestContextGetter(partition_path, in_memory)
317 .get();
320 void OffTheRecordProfileImpl::RequestMidiSysExPermission(
321 int render_process_id,
322 int render_view_id,
323 int bridge_id,
324 const GURL& requesting_frame,
325 bool user_gesture,
326 const MidiSysExPermissionCallback& callback) {
327 ChromeMidiPermissionContext* context =
328 ChromeMidiPermissionContextFactory::GetForProfile(this);
329 context->RequestMidiSysExPermission(render_process_id,
330 render_view_id,
331 bridge_id,
332 requesting_frame,
333 user_gesture,
334 callback);
337 void OffTheRecordProfileImpl::CancelMidiSysExPermissionRequest(
338 int render_process_id,
339 int render_view_id,
340 int bridge_id,
341 const GURL& requesting_frame) {
342 ChromeMidiPermissionContext* context =
343 ChromeMidiPermissionContextFactory::GetForProfile(this);
344 context->CancelMidiSysExPermissionRequest(
345 render_process_id, render_view_id, bridge_id, requesting_frame);
348 void OffTheRecordProfileImpl::RequestProtectedMediaIdentifierPermission(
349 int render_process_id,
350 int render_view_id,
351 int bridge_id,
352 int group_id,
353 const GURL& requesting_frame,
354 const ProtectedMediaIdentifierPermissionCallback& callback) {
355 #if defined(OS_ANDROID)
356 ProtectedMediaIdentifierPermissionContext* context =
357 ProtectedMediaIdentifierPermissionContextFactory::GetForProfile(this);
358 context->RequestProtectedMediaIdentifierPermission(render_process_id,
359 render_view_id,
360 bridge_id,
361 group_id,
362 requesting_frame,
363 callback);
364 #else
365 NOTIMPLEMENTED();
366 callback.Run(false);
367 #endif // defined(OS_ANDROID)
370 void OffTheRecordProfileImpl::CancelProtectedMediaIdentifierPermissionRequests(
371 int group_id) {
372 #if defined(OS_ANDROID)
373 ProtectedMediaIdentifierPermissionContext* context =
374 ProtectedMediaIdentifierPermissionContextFactory::GetForProfile(this);
375 context->CancelProtectedMediaIdentifierPermissionRequests(group_id);
376 #else
377 NOTIMPLEMENTED();
378 #endif // defined(OS_ANDROID)
381 net::URLRequestContextGetter*
382 OffTheRecordProfileImpl::GetRequestContextForExtensions() {
383 return io_data_->GetExtensionsRequestContextGetter().get();
386 net::URLRequestContextGetter*
387 OffTheRecordProfileImpl::CreateRequestContextForStoragePartition(
388 const base::FilePath& partition_path,
389 bool in_memory,
390 content::ProtocolHandlerMap* protocol_handlers,
391 content::ProtocolHandlerScopedVector protocol_interceptors) {
392 return io_data_->CreateIsolatedAppRequestContextGetter(
393 partition_path,
394 in_memory,
395 protocol_handlers,
396 protocol_interceptors.Pass()).get();
399 content::ResourceContext* OffTheRecordProfileImpl::GetResourceContext() {
400 return io_data_->GetResourceContext();
403 net::SSLConfigService* OffTheRecordProfileImpl::GetSSLConfigService() {
404 return profile_->GetSSLConfigService();
407 HostContentSettingsMap* OffTheRecordProfileImpl::GetHostContentSettingsMap() {
408 // Retrieve the host content settings map of the parent profile in order to
409 // ensure the preferences have been migrated.
410 profile_->GetHostContentSettingsMap();
411 if (!host_content_settings_map_.get()) {
412 host_content_settings_map_ = new HostContentSettingsMap(GetPrefs(), true);
413 #if defined(ENABLE_EXTENSIONS)
414 ExtensionService* extension_service = GetExtensionService();
415 if (extension_service)
416 host_content_settings_map_->RegisterExtensionService(extension_service);
417 #endif
419 return host_content_settings_map_.get();
422 content::GeolocationPermissionContext*
423 OffTheRecordProfileImpl::GetGeolocationPermissionContext() {
424 return ChromeGeolocationPermissionContextFactory::GetForProfile(this);
427 content::BrowserPluginGuestManager*
428 OffTheRecordProfileImpl::GetGuestManager() {
429 return GuestViewManager::FromBrowserContext(this);
432 quota::SpecialStoragePolicy*
433 OffTheRecordProfileImpl::GetSpecialStoragePolicy() {
434 return GetExtensionSpecialStoragePolicy();
437 bool OffTheRecordProfileImpl::IsSameProfile(Profile* profile) {
438 return (profile == this) || (profile == profile_);
441 Time OffTheRecordProfileImpl::GetStartTime() const {
442 return start_time_;
445 history::TopSites* OffTheRecordProfileImpl::GetTopSitesWithoutCreating() {
446 return NULL;
449 history::TopSites* OffTheRecordProfileImpl::GetTopSites() {
450 return NULL;
453 void OffTheRecordProfileImpl::SetExitType(ExitType exit_type) {
456 base::FilePath OffTheRecordProfileImpl::last_selected_directory() {
457 const base::FilePath& directory = last_selected_directory_;
458 if (directory.empty()) {
459 return profile_->last_selected_directory();
461 return directory;
464 void OffTheRecordProfileImpl::set_last_selected_directory(
465 const base::FilePath& path) {
466 last_selected_directory_ = path;
469 bool OffTheRecordProfileImpl::WasCreatedByVersionOrLater(
470 const std::string& version) {
471 return profile_->WasCreatedByVersionOrLater(version);
474 Profile::ExitType OffTheRecordProfileImpl::GetLastSessionExitType() {
475 return profile_->GetLastSessionExitType();
478 #if defined(OS_CHROMEOS)
479 void OffTheRecordProfileImpl::ChangeAppLocale(const std::string& locale,
480 AppLocaleChangedVia) {
483 void OffTheRecordProfileImpl::OnLogin() {
486 void OffTheRecordProfileImpl::InitChromeOSPreferences() {
487 // The incognito profile shouldn't have Chrome OS's preferences.
488 // The preferences are associated with the regular user profile.
490 #endif // defined(OS_CHROMEOS)
492 PrefProxyConfigTracker* OffTheRecordProfileImpl::GetProxyConfigTracker() {
493 if (!pref_proxy_config_tracker_)
494 pref_proxy_config_tracker_.reset(CreateProxyConfigTracker());
495 return pref_proxy_config_tracker_.get();
498 chrome_browser_net::Predictor* OffTheRecordProfileImpl::GetNetworkPredictor() {
499 // We do not store information about websites visited in OTR profiles which
500 // is necessary for a Predictor, so we do not have a Predictor at all.
501 return NULL;
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 void OffTheRecordProfileImpl::ClearDomainReliabilityMonitor(
517 domain_reliability::DomainReliabilityClearMode mode,
518 const base::Closure& completion) {
519 // Incognito profiles don't have Domain Reliability Monitors, so there's
520 // nothing to do here.
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) {
539 virtual ProfileType GetProfileType() const OVERRIDE {
540 return GUEST_PROFILE;
543 virtual void InitChromeOSPreferences() OVERRIDE {
544 chromeos_preferences_.reset(new chromeos::Preferences());
545 chromeos_preferences_->Init(static_cast<PrefServiceSyncable*>(GetPrefs()),
546 chromeos::UserManager::Get()->GetActiveUser());
549 private:
550 // The guest user should be able to customize Chrome OS preferences.
551 scoped_ptr<chromeos::Preferences> chromeos_preferences_;
553 #endif
555 Profile* Profile::CreateOffTheRecordProfile() {
556 OffTheRecordProfileImpl* profile = NULL;
557 #if defined(OS_CHROMEOS)
558 if (IsGuestSession())
559 profile = new GuestSessionProfile(this);
560 #endif
561 if (!profile)
562 profile = new OffTheRecordProfileImpl(this);
563 profile->Init();
564 return profile;
567 void OffTheRecordProfileImpl::OnZoomLevelChanged(
568 const HostZoomMap::ZoomLevelChange& change) {
569 HostZoomMap* host_zoom_map = HostZoomMap::GetForBrowserContext(this);
570 switch (change.mode) {
571 case HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM:
572 return;
573 case HostZoomMap::ZOOM_CHANGED_FOR_HOST:
574 host_zoom_map->SetZoomLevelForHost(change.host, change.zoom_level);
575 return;
576 case HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST:
577 host_zoom_map->SetZoomLevelForHostAndScheme(change.scheme,
578 change.host,
579 change.zoom_level);
580 return;
584 PrefProxyConfigTracker* OffTheRecordProfileImpl::CreateProxyConfigTracker() {
585 #if defined(OS_CHROMEOS)
586 if (chromeos::ProfileHelper::IsSigninProfile(this)) {
587 return ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
588 g_browser_process->local_state());
590 #endif // defined(OS_CHROMEOS)
591 return ProxyServiceFactory::CreatePrefProxyConfigTrackerOfProfile(
592 GetPrefs(), g_browser_process->local_state());