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 registry
->RegisterDictionaryPref(prefs::kPartitionDefaultZoomLevel
);
115 registry
->RegisterDictionaryPref(prefs::kPartitionPerHostZoomLevels
);
116 registry
->RegisterStringPref(prefs::kDefaultApps
, "install");
117 registry
->RegisterBooleanPref(prefs::kSpeechRecognitionFilterProfanities
,
119 registry
->RegisterIntegerPref(prefs::kProfileIconVersion
, 0);
120 #if defined(OS_CHROMEOS)
121 // TODO(dilmah): For OS_CHROMEOS we maintain kApplicationLocale in both
122 // local state and user's profile. For other platforms we maintain
123 // kApplicationLocale only in local state.
124 // In the future we may want to maintain kApplicationLocale
125 // in user's profile for other platforms as well.
126 registry
->RegisterStringPref(
127 prefs::kApplicationLocale
,
129 user_prefs::PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF
);
130 registry
->RegisterStringPref(prefs::kApplicationLocaleBackup
, std::string());
131 registry
->RegisterStringPref(prefs::kApplicationLocaleAccepted
,
133 registry
->RegisterStringPref(prefs::kCurrentWallpaperAppName
, std::string());
136 #if defined(OS_ANDROID)
137 registry
->RegisterBooleanPref(prefs::kDevToolsRemoteEnabled
, false);
140 data_reduction_proxy::RegisterSyncableProfilePrefs(registry
);
142 #if !defined(OS_ANDROID) && !defined(OS_CHROMEOS) && !defined(OS_IOS)
143 // Preferences related to the avatar bubble and user manager tutorials.
144 registry
->RegisterIntegerPref(prefs::kProfileAvatarTutorialShown
, 0);
148 std::string
Profile::GetDebugName() {
149 std::string name
= GetPath().BaseName().MaybeAsASCII();
151 name
= "UnknownProfile";
156 bool Profile::IsGuestSession() const {
157 #if defined(OS_CHROMEOS)
158 static bool is_guest_session
=
159 base::CommandLine::ForCurrentProcess()->HasSwitch(
160 chromeos::switches::kGuestSession
);
161 return is_guest_session
;
163 return is_guest_profile_
;
167 bool Profile::IsSystemProfile() const {
168 return is_system_profile_
;
171 bool Profile::IsNewProfile() {
172 // The profile has been shut down if the prefs were loaded from disk, unless
173 // first-run autoimport wrote them and reloaded the pref service.
174 // TODO(dconnelly): revisit this when crbug.com/22142 (unifying the profile
175 // import code) is fixed.
176 return GetOriginalProfile()->GetPrefs()->GetInitializationStatus() ==
177 PrefService::INITIALIZATION_STATUS_CREATED_NEW_PREF_STORE
;
180 bool Profile::IsSyncAllowed() {
181 if (ProfileSyncServiceFactory::HasProfileSyncService(this)) {
182 return ProfileSyncServiceFactory::GetForProfile(this)->IsSyncAllowed();
185 // No ProfileSyncService created yet - we don't want to create one, so just
186 // infer the accessible state by looking at prefs/command line flags.
187 sync_driver::SyncPrefs
prefs(GetPrefs());
188 return ProfileSyncService::IsSyncAllowedByFlag() && !prefs
.IsManaged();
191 void Profile::MaybeSendDestroyedNotification() {
192 if (!sent_destroyed_notification_
) {
193 sent_destroyed_notification_
= true;
195 NotifyWillBeDestroyed(this);
196 content::NotificationService::current()->Notify(
197 chrome::NOTIFICATION_PROFILE_DESTROYED
,
198 content::Source
<Profile
>(this),
199 content::NotificationService::NoDetails());
203 bool ProfileCompare::operator()(Profile
* a
, Profile
* b
) const {
205 if (a
->IsSameProfile(b
))
207 return a
->GetOriginalProfile() < b
->GetOriginalProfile();
210 double Profile::GetDefaultZoomLevelForProfile() {
211 return GetDefaultStoragePartition(this)
213 ->GetDefaultZoomLevel();