off_the_record_profile_impl.cc revision effb81e5f8246d0db0270817048dc992db66e9fb
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.
4
5#include "chrome/browser/profiles/off_the_record_profile_impl.h"
6
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/io_thread.h"
29#include "chrome/browser/media/chrome_midi_permission_context.h"
30#include "chrome/browser/media/chrome_midi_permission_context_factory.h"
31#include "chrome/browser/net/pref_proxy_config_tracker.h"
32#include "chrome/browser/net/proxy_service_factory.h"
33#include "chrome/browser/plugins/chrome_plugin_service_filter.h"
34#include "chrome/browser/plugins/plugin_prefs.h"
35#include "chrome/browser/prefs/incognito_mode_prefs.h"
36#include "chrome/browser/prefs/pref_service_syncable.h"
37#include "chrome/browser/themes/theme_service.h"
38#include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
39#include "chrome/common/chrome_constants.h"
40#include "chrome/common/chrome_paths.h"
41#include "chrome/common/chrome_switches.h"
42#include "chrome/common/pref_names.h"
43#include "chrome/common/render_messages.h"
44#include "components/keyed_service/content/browser_context_dependency_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 "extensions/browser/extension_system.h"
53#include "extensions/common/extension.h"
54#include "net/http/http_server_properties.h"
55#include "net/http/transport_security_state.h"
56#include "webkit/browser/database/database_tracker.h"
57
58#if defined(OS_ANDROID)
59#include "chrome/browser/media/protected_media_identifier_permission_context.h"
60#include "chrome/browser/media/protected_media_identifier_permission_context_factory.h"
61#endif  // defined(OS_ANDROID)
62
63#if defined(OS_ANDROID) || defined(OS_IOS)
64#include "base/prefs/scoped_user_pref_update.h"
65#include "chrome/browser/prefs/proxy_prefs.h"
66#endif  // defined(OS_ANDROID) || defined(OS_IOS)
67
68#if defined(OS_CHROMEOS)
69#include "chrome/browser/chromeos/preferences.h"
70#include "chrome/browser/chromeos/profiles/profile_helper.h"
71#endif
72
73#if defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS)
74#include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
75#endif
76
77using content::BrowserThread;
78using content::DownloadManagerDelegate;
79using content::HostZoomMap;
80
81namespace {
82
83void NotifyOTRProfileCreatedOnIOThread(void* original_profile,
84                                       void* otr_profile) {
85  ExtensionWebRequestEventRouter::GetInstance()->OnOTRProfileCreated(
86      original_profile, otr_profile);
87}
88
89void NotifyOTRProfileDestroyedOnIOThread(void* original_profile,
90                                         void* otr_profile) {
91  ExtensionWebRequestEventRouter::GetInstance()->OnOTRProfileDestroyed(
92      original_profile, otr_profile);
93}
94
95}  // namespace
96
97OffTheRecordProfileImpl::OffTheRecordProfileImpl(Profile* real_profile)
98    : profile_(real_profile),
99      prefs_(PrefServiceSyncable::IncognitoFromProfile(real_profile)),
100      start_time_(Time::Now()) {
101  // Register on BrowserContext.
102  user_prefs::UserPrefs::Set(this, prefs_);
103}
104
105void OffTheRecordProfileImpl::Init() {
106  // The construction of OffTheRecordProfileIOData::Handle needs the profile
107  // type returned by this->GetProfileType().  Since GetProfileType() is a
108  // virtual member function, we cannot call the function defined in the most
109  // derived class (e.g. GuestSessionProfile) until a ctor finishes.  Thus,
110  // we have to instantiate OffTheRecordProfileIOData::Handle here after a ctor.
111  InitIoData();
112
113#if defined(ENABLE_CONFIGURATION_POLICY) && !defined(OS_CHROMEOS)
114  // Because UserCloudPolicyManager is in a component, it cannot access
115  // GetOriginalProfile. Instead, we have to inject this relation here.
116  policy::UserCloudPolicyManagerFactory::RegisterForOffTheRecordBrowserContext(
117      this->GetOriginalProfile(), this);
118#endif
119
120  BrowserContextDependencyManager::GetInstance()->CreateBrowserContextServices(
121      this);
122
123  DCHECK_NE(IncognitoModePrefs::DISABLED,
124            IncognitoModePrefs::GetAvailability(profile_->GetPrefs()));
125
126#if defined(OS_ANDROID) || defined(OS_IOS)
127  UseSystemProxy();
128#endif  // defined(OS_ANDROID) || defined(OS_IOS)
129
130  // TODO(oshima): Remove the need to eagerly initialize the request context
131  // getter. chromeos::OnlineAttempt is illegally trying to access this
132  // Profile member from a thread other than the UI thread, so we need to
133  // prevent a race.
134#if defined(OS_CHROMEOS)
135  GetRequestContext();
136#endif  // defined(OS_CHROMEOS)
137
138  InitHostZoomMap();
139
140  // Make the chrome//extension-icon/ resource available.
141  extensions::ExtensionIconSource* icon_source =
142      new extensions::ExtensionIconSource(profile_);
143  content::URLDataSource::Add(this, icon_source);
144
145#if defined(ENABLE_PLUGINS)
146  ChromePluginServiceFilter::GetInstance()->RegisterResourceContext(
147      PluginPrefs::GetForProfile(this).get(),
148      io_data_->GetResourceContextNoInit());
149#endif
150
151  BrowserThread::PostTask(
152      BrowserThread::IO, FROM_HERE,
153      base::Bind(&NotifyOTRProfileCreatedOnIOThread, profile_, this));
154}
155
156OffTheRecordProfileImpl::~OffTheRecordProfileImpl() {
157  MaybeSendDestroyedNotification();
158
159#if defined(ENABLE_PLUGINS)
160  ChromePluginServiceFilter::GetInstance()->UnregisterResourceContext(
161      io_data_->GetResourceContextNoInit());
162#endif
163
164  BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
165      this);
166
167  BrowserThread::PostTask(
168      BrowserThread::IO, FROM_HERE,
169      base::Bind(&NotifyOTRProfileDestroyedOnIOThread, profile_, this));
170
171  if (host_content_settings_map_.get())
172    host_content_settings_map_->ShutdownOnUIThread();
173
174  if (pref_proxy_config_tracker_)
175    pref_proxy_config_tracker_->DetachFromPrefService();
176
177  // Clears any data the network stack contains that may be related to the
178  // OTR session.
179  g_browser_process->io_thread()->ChangedToOnTheRecord();
180}
181
182void OffTheRecordProfileImpl::InitIoData() {
183  io_data_.reset(new OffTheRecordProfileIOData::Handle(this));
184}
185
186void OffTheRecordProfileImpl::InitHostZoomMap() {
187  HostZoomMap* host_zoom_map = HostZoomMap::GetForBrowserContext(this);
188  HostZoomMap* parent_host_zoom_map =
189      HostZoomMap::GetForBrowserContext(profile_);
190  host_zoom_map->CopyFrom(parent_host_zoom_map);
191  // Observe parent's HZM change for propagating change of parent's
192  // change to this HZM.
193  zoom_subscription_ = parent_host_zoom_map->AddZoomLevelChangedCallback(
194      base::Bind(&OffTheRecordProfileImpl::OnZoomLevelChanged,
195                 base::Unretained(this)));
196}
197
198#if defined(OS_ANDROID) || defined(OS_IOS)
199void OffTheRecordProfileImpl::UseSystemProxy() {
200  // Force the use of the system-assigned proxy when off the record.
201  const char kProxyMode[] = "mode";
202  const char kProxyServer[] = "server";
203  const char kProxyBypassList[] = "bypass_list";
204  const char kProxyPacUrl[] = "pac_url";
205  DictionaryPrefUpdate update(prefs_, prefs::kProxy);
206  base::DictionaryValue* dict = update.Get();
207  dict->SetString(kProxyMode, ProxyModeToString(ProxyPrefs::MODE_SYSTEM));
208  dict->SetString(kProxyPacUrl, "");
209  dict->SetString(kProxyServer, "");
210  dict->SetString(kProxyBypassList, "");
211}
212#endif  // defined(OS_ANDROID) || defined(OS_IOS)
213
214std::string OffTheRecordProfileImpl::GetProfileName() {
215  // Incognito profile should not return the profile name.
216  return std::string();
217}
218
219Profile::ProfileType OffTheRecordProfileImpl::GetProfileType() const {
220  return INCOGNITO_PROFILE;
221}
222
223base::FilePath OffTheRecordProfileImpl::GetPath() const {
224  return profile_->GetPath();
225}
226
227scoped_refptr<base::SequencedTaskRunner>
228OffTheRecordProfileImpl::GetIOTaskRunner() {
229  return profile_->GetIOTaskRunner();
230}
231
232bool OffTheRecordProfileImpl::IsOffTheRecord() const {
233  return true;
234}
235
236Profile* OffTheRecordProfileImpl::GetOffTheRecordProfile() {
237  return this;
238}
239
240void OffTheRecordProfileImpl::DestroyOffTheRecordProfile() {
241  // Suicide is bad!
242  NOTREACHED();
243}
244
245bool OffTheRecordProfileImpl::HasOffTheRecordProfile() {
246  return true;
247}
248
249Profile* OffTheRecordProfileImpl::GetOriginalProfile() {
250  return profile_;
251}
252
253ExtensionService* OffTheRecordProfileImpl::GetExtensionService() {
254  return extensions::ExtensionSystem::Get(this)->extension_service();
255}
256
257ExtensionSpecialStoragePolicy*
258    OffTheRecordProfileImpl::GetExtensionSpecialStoragePolicy() {
259  return GetOriginalProfile()->GetExtensionSpecialStoragePolicy();
260}
261
262bool OffTheRecordProfileImpl::IsManaged() {
263  return GetOriginalProfile()->IsManaged();
264}
265
266PrefService* OffTheRecordProfileImpl::GetPrefs() {
267  return prefs_;
268}
269
270PrefService* OffTheRecordProfileImpl::GetOffTheRecordPrefs() {
271  return prefs_;
272}
273
274DownloadManagerDelegate* OffTheRecordProfileImpl::GetDownloadManagerDelegate() {
275  return DownloadServiceFactory::GetForBrowserContext(this)->
276      GetDownloadManagerDelegate();
277}
278
279net::URLRequestContextGetter* OffTheRecordProfileImpl::GetRequestContext() {
280  return GetDefaultStoragePartition(this)->GetURLRequestContext();
281}
282
283net::URLRequestContextGetter* OffTheRecordProfileImpl::CreateRequestContext(
284    content::ProtocolHandlerMap* protocol_handlers,
285    content::ProtocolHandlerScopedVector protocol_interceptors) {
286  return io_data_->CreateMainRequestContextGetter(
287      protocol_handlers, protocol_interceptors.Pass()).get();
288}
289
290net::URLRequestContextGetter*
291    OffTheRecordProfileImpl::GetRequestContextForRenderProcess(
292        int renderer_child_id) {
293  content::RenderProcessHost* rph = content::RenderProcessHost::FromID(
294      renderer_child_id);
295  return rph->GetStoragePartition()->GetURLRequestContext();
296}
297
298net::URLRequestContextGetter*
299    OffTheRecordProfileImpl::GetMediaRequestContext() {
300  // In OTR mode, media request context is the same as the original one.
301  return GetRequestContext();
302}
303
304net::URLRequestContextGetter*
305    OffTheRecordProfileImpl::GetMediaRequestContextForRenderProcess(
306        int renderer_child_id) {
307  // In OTR mode, media request context is the same as the original one.
308  return GetRequestContextForRenderProcess(renderer_child_id);
309}
310
311net::URLRequestContextGetter*
312OffTheRecordProfileImpl::GetMediaRequestContextForStoragePartition(
313    const base::FilePath& partition_path,
314    bool in_memory) {
315  return io_data_->GetIsolatedAppRequestContextGetter(partition_path, in_memory)
316      .get();
317}
318
319void OffTheRecordProfileImpl::RequestMidiSysExPermission(
320      int render_process_id,
321      int render_view_id,
322      int bridge_id,
323      const GURL& requesting_frame,
324      bool user_gesture,
325      const MidiSysExPermissionCallback& callback) {
326  ChromeMidiPermissionContext* context =
327      ChromeMidiPermissionContextFactory::GetForProfile(this);
328  context->RequestMidiSysExPermission(render_process_id,
329                                      render_view_id,
330                                      bridge_id,
331                                      requesting_frame,
332                                      user_gesture,
333                                      callback);
334}
335
336void OffTheRecordProfileImpl::CancelMidiSysExPermissionRequest(
337    int render_process_id,
338    int render_view_id,
339    int bridge_id,
340    const GURL& requesting_frame) {
341  ChromeMidiPermissionContext* context =
342      ChromeMidiPermissionContextFactory::GetForProfile(this);
343  context->CancelMidiSysExPermissionRequest(
344      render_process_id, render_view_id, bridge_id, requesting_frame);
345}
346
347void OffTheRecordProfileImpl::RequestProtectedMediaIdentifierPermission(
348    int render_process_id,
349    int render_view_id,
350    int bridge_id,
351    int group_id,
352    const GURL& requesting_frame,
353    const ProtectedMediaIdentifierPermissionCallback& callback) {
354#if defined(OS_ANDROID)
355  ProtectedMediaIdentifierPermissionContext* context =
356      ProtectedMediaIdentifierPermissionContextFactory::GetForProfile(this);
357  context->RequestProtectedMediaIdentifierPermission(render_process_id,
358                                                     render_view_id,
359                                                     bridge_id,
360                                                     group_id,
361                                                     requesting_frame,
362                                                     callback);
363#else
364  NOTIMPLEMENTED();
365  callback.Run(false);
366#endif  // defined(OS_ANDROID)
367}
368
369void OffTheRecordProfileImpl::CancelProtectedMediaIdentifierPermissionRequests(
370    int group_id) {
371#if defined(OS_ANDROID)
372  ProtectedMediaIdentifierPermissionContext* context =
373      ProtectedMediaIdentifierPermissionContextFactory::GetForProfile(this);
374  context->CancelProtectedMediaIdentifierPermissionRequests(group_id);
375#else
376  NOTIMPLEMENTED();
377#endif  // defined(OS_ANDROID)
378}
379
380net::URLRequestContextGetter*
381    OffTheRecordProfileImpl::GetRequestContextForExtensions() {
382  return io_data_->GetExtensionsRequestContextGetter().get();
383}
384
385net::URLRequestContextGetter*
386OffTheRecordProfileImpl::CreateRequestContextForStoragePartition(
387    const base::FilePath& partition_path,
388    bool in_memory,
389    content::ProtocolHandlerMap* protocol_handlers,
390    content::ProtocolHandlerScopedVector protocol_interceptors) {
391  return io_data_->CreateIsolatedAppRequestContextGetter(
392      partition_path,
393      in_memory,
394      protocol_handlers,
395      protocol_interceptors.Pass()).get();
396}
397
398content::ResourceContext* OffTheRecordProfileImpl::GetResourceContext() {
399  return io_data_->GetResourceContext();
400}
401
402net::SSLConfigService* OffTheRecordProfileImpl::GetSSLConfigService() {
403  return profile_->GetSSLConfigService();
404}
405
406HostContentSettingsMap* OffTheRecordProfileImpl::GetHostContentSettingsMap() {
407  // Retrieve the host content settings map of the parent profile in order to
408  // ensure the preferences have been migrated.
409  profile_->GetHostContentSettingsMap();
410  if (!host_content_settings_map_.get()) {
411    host_content_settings_map_ = new HostContentSettingsMap(GetPrefs(), true);
412#if defined(ENABLE_EXTENSIONS)
413    ExtensionService* extension_service = GetExtensionService();
414    if (extension_service)
415      host_content_settings_map_->RegisterExtensionService(extension_service);
416#endif
417  }
418  return host_content_settings_map_.get();
419}
420
421content::GeolocationPermissionContext*
422    OffTheRecordProfileImpl::GetGeolocationPermissionContext() {
423  return ChromeGeolocationPermissionContextFactory::GetForProfile(this);
424}
425
426quota::SpecialStoragePolicy*
427    OffTheRecordProfileImpl::GetSpecialStoragePolicy() {
428  return GetExtensionSpecialStoragePolicy();
429}
430
431bool OffTheRecordProfileImpl::IsSameProfile(Profile* profile) {
432  return (profile == this) || (profile == profile_);
433}
434
435Time OffTheRecordProfileImpl::GetStartTime() const {
436  return start_time_;
437}
438
439history::TopSites* OffTheRecordProfileImpl::GetTopSitesWithoutCreating() {
440  return NULL;
441}
442
443history::TopSites* OffTheRecordProfileImpl::GetTopSites() {
444  return NULL;
445}
446
447void OffTheRecordProfileImpl::SetExitType(ExitType exit_type) {
448}
449
450base::FilePath OffTheRecordProfileImpl::last_selected_directory() {
451  const base::FilePath& directory = last_selected_directory_;
452  if (directory.empty()) {
453    return profile_->last_selected_directory();
454  }
455  return directory;
456}
457
458void OffTheRecordProfileImpl::set_last_selected_directory(
459    const base::FilePath& path) {
460  last_selected_directory_ = path;
461}
462
463bool OffTheRecordProfileImpl::WasCreatedByVersionOrLater(
464    const std::string& version) {
465  return profile_->WasCreatedByVersionOrLater(version);
466}
467
468Profile::ExitType OffTheRecordProfileImpl::GetLastSessionExitType() {
469  return profile_->GetLastSessionExitType();
470}
471
472#if defined(OS_CHROMEOS)
473void OffTheRecordProfileImpl::ChangeAppLocale(const std::string& locale,
474                                              AppLocaleChangedVia) {
475}
476
477void OffTheRecordProfileImpl::OnLogin() {
478}
479
480void OffTheRecordProfileImpl::InitChromeOSPreferences() {
481  // The incognito profile shouldn't have Chrome OS's preferences.
482  // The preferences are associated with the regular user profile.
483}
484#endif  // defined(OS_CHROMEOS)
485
486PrefProxyConfigTracker* OffTheRecordProfileImpl::GetProxyConfigTracker() {
487  if (!pref_proxy_config_tracker_)
488    pref_proxy_config_tracker_.reset(CreateProxyConfigTracker());
489  return pref_proxy_config_tracker_.get();
490}
491
492chrome_browser_net::Predictor* OffTheRecordProfileImpl::GetNetworkPredictor() {
493  // We do not store information about websites visited in OTR profiles which
494  // is necessary for a Predictor, so we do not have a Predictor at all.
495  return NULL;
496}
497
498void OffTheRecordProfileImpl::ClearNetworkingHistorySince(
499    base::Time time,
500    const base::Closure& completion) {
501  // Nothing to do here, our transport security state is read-only.
502  // Still, fire the callback to indicate we have finished, otherwise the
503  // BrowsingDataRemover will never be destroyed and the dialog will never be
504  // closed. We must do this asynchronously in order to avoid reentrancy issues.
505  if (!completion.is_null()) {
506    BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, completion);
507  }
508}
509
510GURL OffTheRecordProfileImpl::GetHomePage() {
511  return profile_->GetHomePage();
512}
513
514#if defined(OS_CHROMEOS)
515// Special case of the OffTheRecordProfileImpl which is used while Guest
516// session in CrOS.
517class GuestSessionProfile : public OffTheRecordProfileImpl {
518 public:
519  explicit GuestSessionProfile(Profile* real_profile)
520      : OffTheRecordProfileImpl(real_profile) {
521  }
522
523  virtual ProfileType GetProfileType() const OVERRIDE {
524    return GUEST_PROFILE;
525  }
526
527  virtual void InitChromeOSPreferences() OVERRIDE {
528    chromeos_preferences_.reset(new chromeos::Preferences());
529    chromeos_preferences_->Init(static_cast<PrefServiceSyncable*>(GetPrefs()),
530                                chromeos::UserManager::Get()->GetActiveUser());
531  }
532
533 private:
534  // The guest user should be able to customize Chrome OS preferences.
535  scoped_ptr<chromeos::Preferences> chromeos_preferences_;
536};
537#endif
538
539Profile* Profile::CreateOffTheRecordProfile() {
540  OffTheRecordProfileImpl* profile = NULL;
541#if defined(OS_CHROMEOS)
542  if (IsGuestSession())
543    profile = new GuestSessionProfile(this);
544#endif
545  if (!profile)
546    profile = new OffTheRecordProfileImpl(this);
547  profile->Init();
548  return profile;
549}
550
551void OffTheRecordProfileImpl::OnZoomLevelChanged(
552    const HostZoomMap::ZoomLevelChange& change) {
553  HostZoomMap* host_zoom_map = HostZoomMap::GetForBrowserContext(this);
554  switch (change.mode) {
555    case HostZoomMap::ZOOM_CHANGED_TEMPORARY_ZOOM:
556       return;
557    case HostZoomMap::ZOOM_CHANGED_FOR_HOST:
558       host_zoom_map->SetZoomLevelForHost(change.host, change.zoom_level);
559       return;
560    case HostZoomMap::ZOOM_CHANGED_FOR_SCHEME_AND_HOST:
561       host_zoom_map->SetZoomLevelForHostAndScheme(change.scheme,
562           change.host,
563           change.zoom_level);
564       return;
565  }
566}
567
568PrefProxyConfigTracker* OffTheRecordProfileImpl::CreateProxyConfigTracker() {
569#if defined(OS_CHROMEOS)
570  if (chromeos::ProfileHelper::IsSigninProfile(this)) {
571    return ProxyServiceFactory::CreatePrefProxyConfigTrackerOfLocalState(
572        g_browser_process->local_state());
573  }
574#endif  // defined(OS_CHROMEOS)
575  return ProxyServiceFactory::CreatePrefProxyConfigTrackerOfProfile(
576      GetPrefs(), g_browser_process->local_state());
577}
578