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/profile.h"
9 #include "base/prefs/pref_service.h"
10 #include "build/build_config.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/first_run/first_run.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/browser/sync/profile_sync_service.h"
15 #include "chrome/browser/sync/profile_sync_service_factory.h"
16 #include "chrome/common/pref_names.h"
17 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h"
18 #include "components/pref_registry/pref_registry_syncable.h"
19 #include "components/sync_driver/sync_prefs.h"
20 #include "content/public/browser/host_zoom_map.h"
21 #include "content/public/browser/notification_service.h"
22 #include "content/public/browser/notification_source.h"
23 #include "content/public/browser/storage_partition.h"
24 #include "content/public/browser/web_contents.h"
25 #include "content/public/browser/web_ui.h"
27 #if defined(OS_CHROMEOS)
28 #include "base/command_line.h"
29 #include "chrome/common/chrome_switches.h"
30 #include "chromeos/chromeos_switches.h"
33 #if defined(ENABLE_EXTENSIONS)
34 #include "extensions/browser/pref_names.h"
38 : restored_last_session_(false),
39 sent_destroyed_notification_(false),
40 accessibility_pause_level_(0),
41 is_guest_profile_(false),
42 is_system_profile_(false) {
49 Profile
* Profile::FromBrowserContext(content::BrowserContext
* browser_context
) {
50 // This is safe; this is the only implementation of the browser context.
51 return static_cast<Profile
*>(browser_context
);
55 Profile
* Profile::FromWebUI(content::WebUI
* web_ui
) {
56 return FromBrowserContext(web_ui
->GetWebContents()->GetBrowserContext());
59 TestingProfile
* Profile::AsTestingProfile() {
63 chrome::ChromeZoomLevelPrefs
* Profile::GetZoomLevelPrefs() {
67 Profile::Delegate::~Delegate() {
71 const char Profile::kProfileKey
[] = "__PROFILE__";
72 // This must be a string which can never be a valid domain.
73 const char Profile::kNoHostedDomainFound
[] = "NO_HOSTED_DOMAIN";
76 void Profile::RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
) {
77 registry
->RegisterBooleanPref(
78 prefs::kSearchSuggestEnabled
,
80 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
81 #if defined(OS_ANDROID)
82 registry
->RegisterStringPref(
83 prefs::kContextualSearchEnabled
,
85 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
87 registry
->RegisterBooleanPref(prefs::kSessionExitedCleanly
, true);
88 registry
->RegisterStringPref(prefs::kSessionExitType
, std::string());
89 registry
->RegisterBooleanPref(
90 prefs::kSafeBrowsingEnabled
,
92 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
93 registry
->RegisterBooleanPref(prefs::kSafeBrowsingExtendedReportingEnabled
,
95 registry
->RegisterBooleanPref(prefs::kSafeBrowsingProceedAnywayDisabled
,
97 registry
->RegisterBooleanPref(prefs::kSSLErrorOverrideAllowed
, true);
98 registry
->RegisterDictionaryPref(prefs::kSafeBrowsingIncidentsSent
);
99 registry
->RegisterBooleanPref(
100 prefs::kSafeBrowsingExtendedReportingOptInAllowed
, true);
101 #if defined(ENABLE_GOOGLE_NOW)
102 registry
->RegisterBooleanPref(prefs::kGoogleGeolocationAccessEnabled
, false);
104 // This pref is intentionally outside the above #if. That flag corresponds
105 // to the Notifier extension and does not gate the launcher page.
106 // TODO(skare): Remove or rename ENABLE_GOOGLE_NOW: http://crbug.com/459827.
107 registry
->RegisterBooleanPref(prefs::kGoogleNowLauncherEnabled
, true);
108 registry
->RegisterBooleanPref(prefs::kDisableExtensions
, false);
109 #if defined(ENABLE_EXTENSIONS)
110 registry
->RegisterBooleanPref(extensions::pref_names::kAlertsInitialized
,
113 registry
->RegisterStringPref(prefs::kSelectFileLastDirectory
, std::string());
114 // TODO(wjmaclean): remove the following two prefs once migration to per-
115 // partition zoom is complete.
116 registry
->RegisterDoublePref(prefs::kDefaultZoomLevelDeprecated
, 0.0);
117 registry
->RegisterDictionaryPref(prefs::kPerHostZoomLevelsDeprecated
);
119 registry
->RegisterDictionaryPref(prefs::kPartitionDefaultZoomLevel
);
120 registry
->RegisterDictionaryPref(prefs::kPartitionPerHostZoomLevels
);
121 registry
->RegisterStringPref(prefs::kDefaultApps
, "install");
122 registry
->RegisterBooleanPref(prefs::kSpeechRecognitionFilterProfanities
,
124 registry
->RegisterIntegerPref(prefs::kProfileIconVersion
, 0);
125 #if defined(OS_CHROMEOS)
126 // TODO(dilmah): For OS_CHROMEOS we maintain kApplicationLocale in both
127 // local state and user's profile. For other platforms we maintain
128 // kApplicationLocale only in local state.
129 // In the future we may want to maintain kApplicationLocale
130 // in user's profile for other platforms as well.
131 registry
->RegisterStringPref(
132 prefs::kApplicationLocale
,
134 user_prefs::PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF
);
135 registry
->RegisterStringPref(prefs::kApplicationLocaleBackup
, std::string());
136 registry
->RegisterStringPref(prefs::kApplicationLocaleAccepted
,
138 registry
->RegisterStringPref(prefs::kCurrentWallpaperAppName
, std::string());
141 #if defined(OS_ANDROID)
142 registry
->RegisterBooleanPref(prefs::kDevToolsRemoteEnabled
, false);
145 data_reduction_proxy::RegisterSyncableProfilePrefs(registry
);
147 #if !defined(OS_ANDROID) && !defined(OS_CHROMEOS) && !defined(OS_IOS)
148 // Preferences related to the avatar bubble and user manager tutorials.
149 registry
->RegisterIntegerPref(prefs::kProfileAvatarTutorialShown
, 0);
153 std::string
Profile::GetDebugName() {
154 std::string name
= GetPath().BaseName().MaybeAsASCII();
156 name
= "UnknownProfile";
161 bool Profile::IsGuestSession() const {
162 #if defined(OS_CHROMEOS)
163 static bool is_guest_session
=
164 base::CommandLine::ForCurrentProcess()->HasSwitch(
165 chromeos::switches::kGuestSession
);
166 return is_guest_session
;
168 return is_guest_profile_
;
172 bool Profile::IsSystemProfile() const {
173 return is_system_profile_
;
176 bool Profile::IsNewProfile() {
177 // The profile has been shut down if the prefs were loaded from disk, unless
178 // first-run autoimport wrote them and reloaded the pref service.
179 // TODO(dconnelly): revisit this when crbug.com/22142 (unifying the profile
180 // import code) is fixed.
181 return GetOriginalProfile()->GetPrefs()->GetInitializationStatus() ==
182 PrefService::INITIALIZATION_STATUS_CREATED_NEW_PREF_STORE
;
185 bool Profile::IsSyncAllowed() {
186 if (ProfileSyncServiceFactory::HasProfileSyncService(this)) {
187 return ProfileSyncServiceFactory::GetForProfile(this)->IsSyncAllowed();
190 // No ProfileSyncService created yet - we don't want to create one, so just
191 // infer the accessible state by looking at prefs/command line flags.
192 sync_driver::SyncPrefs
prefs(GetPrefs());
193 return ProfileSyncService::IsSyncAllowedByFlag() && !prefs
.IsManaged();
196 void Profile::MaybeSendDestroyedNotification() {
197 if (!sent_destroyed_notification_
) {
198 sent_destroyed_notification_
= true;
200 NotifyWillBeDestroyed(this);
201 content::NotificationService::current()->Notify(
202 chrome::NOTIFICATION_PROFILE_DESTROYED
,
203 content::Source
<Profile
>(this),
204 content::NotificationService::NoDetails());
208 bool ProfileCompare::operator()(Profile
* a
, Profile
* b
) const {
210 if (a
->IsSameProfile(b
))
212 return a
->GetOriginalProfile() < b
->GetOriginalProfile();
215 double Profile::GetDefaultZoomLevelForProfile() {
216 return GetDefaultStoragePartition(this)
218 ->GetDefaultZoomLevel();