Move Webstore URL concepts to //extensions and out
[chromium-blink-merge.git] / chrome / browser / profiles / off_the_record_profile_impl.cc
blobffbfbb97d407cff209ae46cea7db790d0f3199e0
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/extension_service.h"
24 #include "chrome/browser/extensions/extension_special_storage_policy.h"
25 #include "chrome/browser/io_thread.h"
26 #include "chrome/browser/net/chrome_url_request_context_getter.h"
27 #include "chrome/browser/net/pref_proxy_config_tracker.h"
28 #include "chrome/browser/net/proxy_service_factory.h"
29 #include "chrome/browser/plugins/chrome_plugin_service_filter.h"
30 #include "chrome/browser/plugins/plugin_prefs.h"
31 #include "chrome/browser/prefs/incognito_mode_prefs.h"
32 #include "chrome/browser/prefs/pref_service_syncable.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/common/chrome_constants.h"
38 #include "chrome/common/chrome_paths.h"
39 #include "chrome/common/chrome_switches.h"
40 #include "chrome/common/pref_names.h"
41 #include "chrome/common/render_messages.h"
42 #include "components/keyed_service/content/browser_context_dependency_manager.h"
43 #include "components/user_prefs/user_prefs.h"
44 #include "content/public/browser/browser_thread.h"
45 #include "content/public/browser/host_zoom_map.h"
46 #include "content/public/browser/render_process_host.h"
47 #include "content/public/browser/storage_partition.h"
48 #include "content/public/browser/url_data_source.h"
49 #include "content/public/browser/web_contents.h"
50 #include "net/http/http_server_properties.h"
51 #include "net/http/transport_security_state.h"
52 #include "storage/browser/database/database_tracker.h"
54 #if defined(OS_ANDROID)
55 #include "chrome/browser/media/protected_media_identifier_permission_context.h"
56 #include "chrome/browser/media/protected_media_identifier_permission_context_factory.h"
57 #endif // defined(OS_ANDROID)
59 #if defined(OS_ANDROID) || defined(OS_IOS)
60 #include "base/prefs/scoped_user_pref_update.h"
61 #include "chrome/browser/prefs/proxy_prefs.h"
62 #endif // defined(OS_ANDROID) || defined(OS_IOS)
64 #if defined(OS_CHROMEOS)
65 #include "chrome/browser/chromeos/preferences.h"
66 #include "chrome/browser/chromeos/profiles/profile_helper.h"
67 #endif
69 #if defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS)
70 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
71 #endif
73 #if defined(ENABLE_EXTENSIONS)
74 #include "chrome/browser/extensions/api/web_request/web_request_api.h"
75 #include "extensions/browser/extension_system.h"
76 #include "extensions/browser/guest_view/guest_view_manager.h"
77 #include "extensions/common/extension.h"
78 #endif
80 using content::BrowserThread;
81 using content::DownloadManagerDelegate;
82 using content::HostZoomMap;
84 #if defined(ENABLE_EXTENSIONS)
85 namespace {
87 void NotifyOTRProfileCreatedOnIOThread(void* original_profile,
88 void* otr_profile) {
89 ExtensionWebRequestEventRouter::GetInstance()->OnOTRProfileCreated(
90 original_profile, otr_profile);
93 void NotifyOTRProfileDestroyedOnIOThread(void* original_profile,
94 void* otr_profile) {
95 ExtensionWebRequestEventRouter::GetInstance()->OnOTRProfileDestroyed(
96 original_profile, otr_profile);
99 } // namespace
100 #endif
102 OffTheRecordProfileImpl::OffTheRecordProfileImpl(Profile* real_profile)
103 : profile_(real_profile),
104 prefs_(PrefServiceSyncable::IncognitoFromProfile(real_profile)),
105 start_time_(Time::Now()) {
106 // Register on BrowserContext.
107 user_prefs::UserPrefs::Set(this, prefs_);
110 void OffTheRecordProfileImpl::Init() {
111 // The construction of OffTheRecordProfileIOData::Handle needs the profile
112 // type returned by this->GetProfileType(). Since GetProfileType() is a
113 // virtual member function, we cannot call the function defined in the most
114 // derived class (e.g. GuestSessionProfile) until a ctor finishes. Thus,
115 // we have to instantiate OffTheRecordProfileIOData::Handle here after a ctor.
116 InitIoData();
118 #if defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS)
119 // Because UserCloudPolicyManager is in a component, it cannot access
120 // GetOriginalProfile. Instead, we have to inject this relation here.
121 policy::UserCloudPolicyManagerFactory::RegisterForOffTheRecordBrowserContext(
122 this->GetOriginalProfile(), this);
123 #endif
125 BrowserContextDependencyManager::GetInstance()->CreateBrowserContextServices(
126 this);
128 // Guest profiles may always be OTR. Check IncognitoModePrefs otherwise.
129 DCHECK(profile_->IsGuestSession() ||
130 IncognitoModePrefs::GetAvailability(profile_->GetPrefs()) !=
131 IncognitoModePrefs::DISABLED);
133 #if defined(OS_ANDROID) || defined(OS_IOS)
134 UseSystemProxy();
135 #endif // defined(OS_ANDROID) || defined(OS_IOS)
137 // TODO(oshima): Remove the need to eagerly initialize the request context
138 // getter. chromeos::OnlineAttempt is illegally trying to access this
139 // Profile member from a thread other than the UI thread, so we need to
140 // prevent a race.
141 #if defined(OS_CHROMEOS)
142 GetRequestContext();
143 #endif // defined(OS_CHROMEOS)
145 InitHostZoomMap();
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
165 OffTheRecordProfileImpl::~OffTheRecordProfileImpl() {
166 MaybeSendDestroyedNotification();
168 #if defined(ENABLE_PLUGINS)
169 ChromePluginServiceFilter::GetInstance()->UnregisterResourceContext(
170 io_data_->GetResourceContextNoInit());
171 #endif
173 BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
174 this);
176 #if defined(ENABLE_EXTENSIONS)
177 BrowserThread::PostTask(
178 BrowserThread::IO, FROM_HERE,
179 base::Bind(&NotifyOTRProfileDestroyedOnIOThread, profile_, this));
180 #endif
182 if (host_content_settings_map_.get())
183 host_content_settings_map_->ShutdownOnUIThread();
185 if (pref_proxy_config_tracker_)
186 pref_proxy_config_tracker_->DetachFromPrefService();
188 // Clears any data the network stack contains that may be related to the
189 // OTR session.
190 g_browser_process->io_thread()->ChangedToOnTheRecord();
193 void OffTheRecordProfileImpl::InitIoData() {
194 io_data_.reset(new OffTheRecordProfileIOData::Handle(this));
197 void OffTheRecordProfileImpl::InitHostZoomMap() {
198 HostZoomMap* host_zoom_map = HostZoomMap::GetDefaultForBrowserContext(this);
199 HostZoomMap* parent_host_zoom_map =
200 HostZoomMap::GetDefaultForBrowserContext(profile_);
201 host_zoom_map->CopyFrom(parent_host_zoom_map);
202 // Observe parent's HZM change for propagating change of parent's
203 // change to this HZM.
204 zoom_subscription_ = parent_host_zoom_map->AddZoomLevelChangedCallback(
205 base::Bind(&OffTheRecordProfileImpl::OnZoomLevelChanged,
206 base::Unretained(this)));
209 #if defined(OS_ANDROID) || defined(OS_IOS)
210 void OffTheRecordProfileImpl::UseSystemProxy() {
211 // Force the use of the system-assigned proxy when off the record.
212 const char kProxyMode[] = "mode";
213 const char kProxyServer[] = "server";
214 const char kProxyBypassList[] = "bypass_list";
215 const char kProxyPacUrl[] = "pac_url";
216 DictionaryPrefUpdate update(prefs_, prefs::kProxy);
217 base::DictionaryValue* dict = update.Get();
218 dict->SetString(kProxyMode, ProxyModeToString(ProxyPrefs::MODE_SYSTEM));
219 dict->SetString(kProxyPacUrl, "");
220 dict->SetString(kProxyServer, "");
221 dict->SetString(kProxyBypassList, "");
223 #endif // defined(OS_ANDROID) || defined(OS_IOS)
225 std::string OffTheRecordProfileImpl::GetProfileName() {
226 // Incognito profile should not return the profile name.
227 return std::string();
230 Profile::ProfileType OffTheRecordProfileImpl::GetProfileType() const {
231 return INCOGNITO_PROFILE;
234 base::FilePath OffTheRecordProfileImpl::GetPath() const {
235 return profile_->GetPath();
238 scoped_refptr<base::SequencedTaskRunner>
239 OffTheRecordProfileImpl::GetIOTaskRunner() {
240 return profile_->GetIOTaskRunner();
243 bool OffTheRecordProfileImpl::IsOffTheRecord() const {
244 return true;
247 Profile* OffTheRecordProfileImpl::GetOffTheRecordProfile() {
248 return this;
251 void OffTheRecordProfileImpl::DestroyOffTheRecordProfile() {
252 // Suicide is bad!
253 NOTREACHED();
256 bool OffTheRecordProfileImpl::HasOffTheRecordProfile() {
257 return true;
260 Profile* OffTheRecordProfileImpl::GetOriginalProfile() {
261 return profile_;
264 ExtensionSpecialStoragePolicy*
265 OffTheRecordProfileImpl::GetExtensionSpecialStoragePolicy() {
266 return GetOriginalProfile()->GetExtensionSpecialStoragePolicy();
269 bool OffTheRecordProfileImpl::IsSupervised() {
270 return GetOriginalProfile()->IsSupervised();
273 PrefService* OffTheRecordProfileImpl::GetPrefs() {
274 return prefs_;
277 PrefService* OffTheRecordProfileImpl::GetOffTheRecordPrefs() {
278 return prefs_;
281 DownloadManagerDelegate* OffTheRecordProfileImpl::GetDownloadManagerDelegate() {
282 return DownloadServiceFactory::GetForBrowserContext(this)->
283 GetDownloadManagerDelegate();
286 net::URLRequestContextGetter* OffTheRecordProfileImpl::GetRequestContext() {
287 return GetDefaultStoragePartition(this)->GetURLRequestContext();
290 net::URLRequestContextGetter* OffTheRecordProfileImpl::CreateRequestContext(
291 content::ProtocolHandlerMap* protocol_handlers,
292 content::URLRequestInterceptorScopedVector request_interceptors) {
293 return io_data_->CreateMainRequestContextGetter(
294 protocol_handlers, request_interceptors.Pass()).get();
297 net::URLRequestContextGetter*
298 OffTheRecordProfileImpl::GetRequestContextForRenderProcess(
299 int renderer_child_id) {
300 content::RenderProcessHost* rph = content::RenderProcessHost::FromID(
301 renderer_child_id);
302 return rph->GetStoragePartition()->GetURLRequestContext();
305 net::URLRequestContextGetter*
306 OffTheRecordProfileImpl::GetMediaRequestContext() {
307 // In OTR mode, media request context is the same as the original one.
308 return GetRequestContext();
311 net::URLRequestContextGetter*
312 OffTheRecordProfileImpl::GetMediaRequestContextForRenderProcess(
313 int renderer_child_id) {
314 // In OTR mode, media request context is the same as the original one.
315 return GetRequestContextForRenderProcess(renderer_child_id);
318 net::URLRequestContextGetter*
319 OffTheRecordProfileImpl::GetMediaRequestContextForStoragePartition(
320 const base::FilePath& partition_path,
321 bool in_memory) {
322 return io_data_->GetIsolatedAppRequestContextGetter(partition_path, in_memory)
323 .get();
326 net::URLRequestContextGetter*
327 OffTheRecordProfileImpl::GetRequestContextForExtensions() {
328 return io_data_->GetExtensionsRequestContextGetter().get();
331 net::URLRequestContextGetter*
332 OffTheRecordProfileImpl::CreateRequestContextForStoragePartition(
333 const base::FilePath& partition_path,
334 bool in_memory,
335 content::ProtocolHandlerMap* protocol_handlers,
336 content::URLRequestInterceptorScopedVector request_interceptors) {
337 return io_data_->CreateIsolatedAppRequestContextGetter(
338 partition_path,
339 in_memory,
340 protocol_handlers,
341 request_interceptors.Pass()).get();
344 content::ResourceContext* OffTheRecordProfileImpl::GetResourceContext() {
345 return io_data_->GetResourceContext();
348 net::SSLConfigService* OffTheRecordProfileImpl::GetSSLConfigService() {
349 return profile_->GetSSLConfigService();
352 HostContentSettingsMap* OffTheRecordProfileImpl::GetHostContentSettingsMap() {
353 // Retrieve the host content settings map of the parent profile in order to
354 // ensure the preferences have been migrated.
355 profile_->GetHostContentSettingsMap();
356 if (!host_content_settings_map_.get()) {
357 host_content_settings_map_ = new HostContentSettingsMap(GetPrefs(), true);
358 #if defined(ENABLE_EXTENSIONS)
359 ExtensionService* extension_service =
360 extensions::ExtensionSystem::Get(this)->extension_service();
361 if (extension_service) {
362 extension_service->RegisterContentSettings(
363 host_content_settings_map_.get());
365 #endif
367 return host_content_settings_map_.get();
370 content::BrowserPluginGuestManager* OffTheRecordProfileImpl::GetGuestManager() {
371 #if defined(ENABLE_EXTENSIONS)
372 return extensions::GuestViewManager::FromBrowserContext(this);
373 #else
374 return NULL;
375 #endif
378 storage::SpecialStoragePolicy*
379 OffTheRecordProfileImpl::GetSpecialStoragePolicy() {
380 return GetExtensionSpecialStoragePolicy();
383 content::PushMessagingService*
384 OffTheRecordProfileImpl::GetPushMessagingService() {
385 // TODO(johnme): Support push messaging in incognito if possible.
386 return NULL;
389 content::SSLHostStateDelegate*
390 OffTheRecordProfileImpl::GetSSLHostStateDelegate() {
391 return ChromeSSLHostStateDelegateFactory::GetForProfile(this);
394 bool OffTheRecordProfileImpl::IsSameProfile(Profile* profile) {
395 return (profile == this) || (profile == profile_);
398 Time OffTheRecordProfileImpl::GetStartTime() const {
399 return start_time_;
402 history::TopSites* OffTheRecordProfileImpl::GetTopSitesWithoutCreating() {
403 return NULL;
406 history::TopSites* OffTheRecordProfileImpl::GetTopSites() {
407 return NULL;
410 void OffTheRecordProfileImpl::SetExitType(ExitType exit_type) {
413 base::FilePath OffTheRecordProfileImpl::last_selected_directory() {
414 const base::FilePath& directory = last_selected_directory_;
415 if (directory.empty()) {
416 return profile_->last_selected_directory();
418 return directory;
421 void OffTheRecordProfileImpl::set_last_selected_directory(
422 const base::FilePath& path) {
423 last_selected_directory_ = path;
426 bool OffTheRecordProfileImpl::WasCreatedByVersionOrLater(
427 const std::string& version) {
428 return profile_->WasCreatedByVersionOrLater(version);
431 Profile::ExitType OffTheRecordProfileImpl::GetLastSessionExitType() {
432 return profile_->GetLastSessionExitType();
435 #if defined(OS_CHROMEOS)
436 void OffTheRecordProfileImpl::ChangeAppLocale(const std::string& locale,
437 AppLocaleChangedVia) {
440 void OffTheRecordProfileImpl::OnLogin() {
443 void OffTheRecordProfileImpl::InitChromeOSPreferences() {
444 // The incognito profile shouldn't have Chrome OS's preferences.
445 // The preferences are associated with the regular user profile.
447 #endif // defined(OS_CHROMEOS)
449 PrefProxyConfigTracker* OffTheRecordProfileImpl::GetProxyConfigTracker() {
450 if (!pref_proxy_config_tracker_)
451 pref_proxy_config_tracker_.reset(CreateProxyConfigTracker());
452 return pref_proxy_config_tracker_.get();
455 chrome_browser_net::Predictor* OffTheRecordProfileImpl::GetNetworkPredictor() {
456 // We do not store information about websites visited in OTR profiles which
457 // is necessary for a Predictor, so we do not have a Predictor at all.
458 return NULL;
461 DevToolsNetworkController*
462 OffTheRecordProfileImpl::GetDevToolsNetworkController() {
463 return io_data_->GetDevToolsNetworkController();
466 void OffTheRecordProfileImpl::ClearNetworkingHistorySince(
467 base::Time time,
468 const base::Closure& completion) {
469 // Nothing to do here, our transport security state is read-only.
470 // Still, fire the callback to indicate we have finished, otherwise the
471 // BrowsingDataRemover will never be destroyed and the dialog will never be
472 // closed. We must do this asynchronously in order to avoid reentrancy issues.
473 if (!completion.is_null()) {
474 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion);
478 GURL OffTheRecordProfileImpl::GetHomePage() {
479 return profile_->GetHomePage();
482 #if defined(OS_CHROMEOS)
483 // Special case of the OffTheRecordProfileImpl which is used while Guest
484 // session in CrOS.
485 class GuestSessionProfile : public OffTheRecordProfileImpl {
486 public:
487 explicit GuestSessionProfile(Profile* real_profile)
488 : OffTheRecordProfileImpl(real_profile) {
491 virtual ProfileType GetProfileType() const OVERRIDE {
492 return GUEST_PROFILE;
495 virtual void InitChromeOSPreferences() OVERRIDE {
496 chromeos_preferences_.reset(new chromeos::Preferences());
497 chromeos_preferences_->Init(
498 this, user_manager::UserManager::Get()->GetActiveUser());
501 private:
502 // The guest user should be able to customize Chrome OS preferences.
503 scoped_ptr<chromeos::Preferences> chromeos_preferences_;
505 #endif
507 Profile* Profile::CreateOffTheRecordProfile() {
508 OffTheRecordProfileImpl* profile = NULL;
509 #if defined(OS_CHROMEOS)
510 if (IsGuestSession())
511 profile = new GuestSessionProfile(this);
512 #endif
513 if (!profile)
514 profile = new OffTheRecordProfileImpl(this);
515 profile->Init();
516 return profile;
519 void OffTheRecordProfileImpl::OnZoomLevelChanged(
520 const HostZoomMap::ZoomLevelChange& change) {
521 HostZoomMap* host_zoom_map = HostZoomMap::GetDefaultForBrowserContext(this);
522 switch (change.mode) {
523 case HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM:
524 return;
525 case HostZoomMap::ZOOM_CHANGED_FOR_HOST:
526 host_zoom_map->SetZoomLevelForHost(change.host, change.zoom_level);
527 return;
528 case HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST:
529 host_zoom_map->SetZoomLevelForHostAndScheme(change.scheme,
530 change.host,
531 change.zoom_level);
532 return;
536 PrefProxyConfigTracker* OffTheRecordProfileImpl::CreateProxyConfigTracker() {
537 #if defined(OS_CHROMEOS)
538 if (chromeos::ProfileHelper::IsSigninProfile(this)) {
539 return ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
540 g_browser_process->local_state());
542 #endif // defined(OS_CHROMEOS)
543 return ProxyServiceFactory::CreatePrefProxyConfigTrackerOfProfile(
544 GetPrefs(), g_browser_process->local_state());