Cleanup: new common GetProfileSwitcherTextForItem for use on both Win and Mac
[chromium-blink-merge.git] / chrome / browser / profiles / profile.cc
blob4f65b32b2477650cac68743c30b5efcc9931fcbb
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"
7 #include <string>
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"
31 #endif
33 #if defined(ENABLE_EXTENSIONS)
34 #include "extensions/browser/pref_names.h"
35 #endif
37 Profile::Profile()
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) {
45 Profile::~Profile() {
48 // static
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);
54 // static
55 Profile* Profile::FromWebUI(content::WebUI* web_ui) {
56 return FromBrowserContext(web_ui->GetWebContents()->GetBrowserContext());
59 TestingProfile* Profile::AsTestingProfile() {
60 return NULL;
63 chrome::ChromeZoomLevelPrefs* Profile::GetZoomLevelPrefs() {
64 return NULL;
67 Profile::Delegate::~Delegate() {
70 // static
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";
75 // static
76 void Profile::RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
77 registry->RegisterBooleanPref(
78 prefs::kSearchSuggestEnabled,
79 true,
80 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
81 #if defined(OS_ANDROID)
82 registry->RegisterStringPref(
83 prefs::kContextualSearchEnabled,
84 std::string(),
85 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
86 #endif
87 registry->RegisterBooleanPref(
88 prefs::kSessionExitedCleanly,
89 true,
90 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
91 registry->RegisterStringPref(
92 prefs::kSessionExitType,
93 std::string(),
94 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
95 registry->RegisterBooleanPref(
96 prefs::kSafeBrowsingEnabled,
97 true,
98 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
99 registry->RegisterBooleanPref(
100 prefs::kSafeBrowsingExtendedReportingEnabled,
101 false,
102 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
103 registry->RegisterBooleanPref(
104 prefs::kSafeBrowsingProceedAnywayDisabled,
105 false,
106 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
107 registry->RegisterDictionaryPref(
108 prefs::kSafeBrowsingIncidentsSent,
109 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
110 registry->RegisterBooleanPref(
111 prefs::kSafeBrowsingExtendedReportingOptInAllowed, true,
112 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
113 #if defined(ENABLE_GOOGLE_NOW)
114 registry->RegisterBooleanPref(
115 prefs::kGoogleGeolocationAccessEnabled,
116 false,
117 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
118 #endif
119 // This pref is intentionally outside the above #if. That flag corresponds
120 // to the Notifier extension and does not gate the launcher page.
121 // TODO(skare): Remove or rename ENABLE_GOOGLE_NOW: http://crbug.com/459827.
122 registry->RegisterBooleanPref(
123 prefs::kGoogleNowLauncherEnabled,
124 true,
125 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
126 registry->RegisterBooleanPref(
127 prefs::kDisableExtensions,
128 false,
129 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
130 #if defined(ENABLE_EXTENSIONS)
131 registry->RegisterBooleanPref(
132 extensions::pref_names::kAlertsInitialized,
133 false,
134 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
135 #endif
136 registry->RegisterStringPref(
137 prefs::kSelectFileLastDirectory,
138 std::string(),
139 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
140 // TODO(wjmaclean): remove the following two prefs once migration to per-
141 // partition zoom is complete.
142 registry->RegisterDoublePref(
143 prefs::kDefaultZoomLevelDeprecated,
144 0.0,
145 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
146 registry->RegisterDictionaryPref(
147 prefs::kPerHostZoomLevelsDeprecated,
148 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
150 registry->RegisterDictionaryPref(
151 prefs::kPartitionDefaultZoomLevel,
152 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
153 registry->RegisterDictionaryPref(
154 prefs::kPartitionPerHostZoomLevels,
155 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
156 registry->RegisterStringPref(
157 prefs::kDefaultApps,
158 "install",
159 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
160 registry->RegisterBooleanPref(
161 prefs::kSpeechRecognitionFilterProfanities,
162 true,
163 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
164 registry->RegisterIntegerPref(
165 prefs::kProfileIconVersion,
167 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
168 #if defined(OS_CHROMEOS)
169 // TODO(dilmah): For OS_CHROMEOS we maintain kApplicationLocale in both
170 // local state and user's profile. For other platforms we maintain
171 // kApplicationLocale only in local state.
172 // In the future we may want to maintain kApplicationLocale
173 // in user's profile for other platforms as well.
174 registry->RegisterStringPref(
175 prefs::kApplicationLocale,
176 std::string(),
177 user_prefs::PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF);
178 registry->RegisterStringPref(
179 prefs::kApplicationLocaleBackup,
180 std::string(),
181 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
182 registry->RegisterStringPref(
183 prefs::kApplicationLocaleAccepted,
184 std::string(),
185 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
186 registry->RegisterStringPref(
187 prefs::kCurrentWallpaperAppName,
188 std::string(),
189 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
190 #endif
192 #if defined(OS_ANDROID)
193 registry->RegisterBooleanPref(
194 prefs::kDevToolsRemoteEnabled,
195 false,
196 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
197 #endif
199 data_reduction_proxy::RegisterSyncableProfilePrefs(registry);
201 #if !defined(OS_ANDROID) && !defined(OS_CHROMEOS) && !defined(OS_IOS)
202 // Preferences related to the avatar bubble and user manager tutorials.
203 registry->RegisterIntegerPref(
204 prefs::kProfileAvatarTutorialShown,
206 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
207 #endif
210 std::string Profile::GetDebugName() {
211 std::string name = GetPath().BaseName().MaybeAsASCII();
212 if (name.empty()) {
213 name = "UnknownProfile";
215 return name;
218 bool Profile::IsGuestSession() const {
219 #if defined(OS_CHROMEOS)
220 static bool is_guest_session =
221 base::CommandLine::ForCurrentProcess()->HasSwitch(
222 chromeos::switches::kGuestSession);
223 return is_guest_session;
224 #else
225 return is_guest_profile_;
226 #endif
229 bool Profile::IsSystemProfile() const {
230 return is_system_profile_;
233 bool Profile::IsNewProfile() {
234 // The profile has been shut down if the prefs were loaded from disk, unless
235 // first-run autoimport wrote them and reloaded the pref service.
236 // TODO(dconnelly): revisit this when crbug.com/22142 (unifying the profile
237 // import code) is fixed.
238 return GetOriginalProfile()->GetPrefs()->GetInitializationStatus() ==
239 PrefService::INITIALIZATION_STATUS_CREATED_NEW_PREF_STORE;
242 bool Profile::IsSyncAccessible() {
243 if (ProfileSyncServiceFactory::HasProfileSyncService(this))
244 return !ProfileSyncServiceFactory::GetForProfile(this)->IsManaged();
246 // No ProfileSyncService created yet - we don't want to create one, so just
247 // infer the accessible state by looking at prefs/command line flags.
248 sync_driver::SyncPrefs prefs(GetPrefs());
249 return ProfileSyncService::IsSyncEnabled() && !prefs.IsManaged();
252 void Profile::MaybeSendDestroyedNotification() {
253 if (!sent_destroyed_notification_) {
254 sent_destroyed_notification_ = true;
256 NotifyWillBeDestroyed(this);
257 content::NotificationService::current()->Notify(
258 chrome::NOTIFICATION_PROFILE_DESTROYED,
259 content::Source<Profile>(this),
260 content::NotificationService::NoDetails());
264 bool ProfileCompare::operator()(Profile* a, Profile* b) const {
265 DCHECK(a && b);
266 if (a->IsSameProfile(b))
267 return false;
268 return a->GetOriginalProfile() < b->GetOriginalProfile();
271 double Profile::GetDefaultZoomLevelForProfile() {
272 return GetDefaultStoragePartition(this)
273 ->GetHostZoomMap()
274 ->GetDefaultZoomLevel();