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/browser/sync/sync_prefs.h"
17 #include "chrome/common/pref_names.h"
18 #include "components/user_prefs/pref_registry_syncable.h"
19 #include "content/public/browser/notification_service.h"
20 #include "content/public/browser/notification_source.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/browser/web_ui.h"
23 #include "extensions/browser/pref_names.h"
25 #if defined(OS_CHROMEOS)
26 #include "base/command_line.h"
27 #include "chrome/common/chrome_switches.h"
28 #include "chromeos/chromeos_switches.h"
32 : restored_last_session_(false),
33 sent_destroyed_notification_(false),
34 accessibility_pause_level_(0) {
41 Profile* Profile::FromBrowserContext(content::BrowserContext* browser_context) {
42 // This is safe; this is the only implementation of the browser context.
43 return static_cast<Profile*>(browser_context);
47 Profile* Profile::FromWebUI(content::WebUI* web_ui) {
48 return FromBrowserContext(web_ui->GetWebContents()->GetBrowserContext());
51 TestingProfile* Profile::AsTestingProfile() {
55 Profile::Delegate::~Delegate() {
59 const char Profile::kProfileKey[] = "__PROFILE__";
62 void Profile::RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
63 registry->RegisterBooleanPref(
64 prefs::kSearchSuggestEnabled,
66 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
67 #if defined(OS_ANDROID)
68 registry->RegisterIntegerPref(
69 prefs::kContextualSearchEnabled,
71 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
73 registry->RegisterBooleanPref(
74 prefs::kSessionExitedCleanly,
76 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
77 registry->RegisterStringPref(
78 prefs::kSessionExitType,
80 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
81 registry->RegisterBooleanPref(
82 prefs::kSafeBrowsingEnabled,
84 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
85 registry->RegisterBooleanPref(
86 prefs::kSafeBrowsingDownloadFeedbackEnabled,
88 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
89 registry->RegisterBooleanPref(
90 prefs::kSafeBrowsingReportingEnabled,
92 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
93 registry->RegisterBooleanPref(
94 prefs::kSafeBrowsingProceedAnywayDisabled,
96 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
97 #if defined(ENABLE_GOOGLE_NOW)
98 registry->RegisterBooleanPref(
99 prefs::kGoogleGeolocationAccessEnabled,
101 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
103 registry->RegisterBooleanPref(
104 prefs::kDisableExtensions,
106 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
107 registry->RegisterBooleanPref(
108 extensions::pref_names::kAlertsInitialized,
110 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
111 registry->RegisterStringPref(
112 prefs::kSelectFileLastDirectory,
114 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
115 registry->RegisterDoublePref(
116 prefs::kDefaultZoomLevel,
118 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
119 registry->RegisterDictionaryPref(
120 prefs::kPerHostZoomLevels,
121 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
122 registry->RegisterStringPref(
125 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
126 registry->RegisterBooleanPref(
127 prefs::kSpeechRecognitionFilterProfanities,
129 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
130 registry->RegisterIntegerPref(
131 prefs::kProfileIconVersion,
133 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
134 #if defined(OS_CHROMEOS)
135 // TODO(dilmah): For OS_CHROMEOS we maintain kApplicationLocale in both
136 // local state and user's profile. For other platforms we maintain
137 // kApplicationLocale only in local state.
138 // In the future we may want to maintain kApplicationLocale
139 // in user's profile for other platforms as well.
140 registry->RegisterStringPref(
141 prefs::kApplicationLocale,
143 user_prefs::PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF);
144 registry->RegisterStringPref(
145 prefs::kApplicationLocaleBackup,
147 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
148 registry->RegisterStringPref(
149 prefs::kApplicationLocaleAccepted,
151 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
154 #if defined(OS_ANDROID)
155 registry->RegisterBooleanPref(
156 prefs::kDevToolsRemoteEnabled,
158 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
160 #if defined(OS_ANDROID) || defined(OS_IOS)
161 registry->RegisterBooleanPref(
162 prefs::kSpdyProxyAuthEnabled,
164 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
165 registry->RegisterBooleanPref(
166 prefs::kSpdyProxyAuthWasEnabledBefore,
168 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
170 #endif // defined(OS_ANDROID) || defined(OS_IOS)
174 std::string Profile::GetDebugName() {
175 std::string name = GetPath().BaseName().MaybeAsASCII();
177 name = "UnknownProfile";
182 bool Profile::IsGuestSession() const {
183 #if defined(OS_CHROMEOS)
184 static bool is_guest_session = CommandLine::ForCurrentProcess()->HasSwitch(
185 chromeos::switches::kGuestSession);
186 return is_guest_session;
188 return GetPath() == ProfileManager::GetGuestProfilePath();
192 bool Profile::IsNewProfile() {
193 // The profile has been shut down if the prefs were loaded from disk, unless
194 // first-run autoimport wrote them and reloaded the pref service.
195 // TODO(dconnelly): revisit this when crbug.com/22142 (unifying the profile
196 // import code) is fixed.
197 return GetOriginalProfile()->GetPrefs()->GetInitializationStatus() ==
198 PrefService::INITIALIZATION_STATUS_CREATED_NEW_PREF_STORE;
201 bool Profile::IsSyncAccessible() {
202 if (ProfileSyncServiceFactory::HasProfileSyncService(this))
203 return !ProfileSyncServiceFactory::GetForProfile(this)->IsManaged();
205 // No ProfileSyncService created yet - we don't want to create one, so just
206 // infer the accessible state by looking at prefs/command line flags.
207 browser_sync::SyncPrefs prefs(GetPrefs());
208 return ProfileSyncService::IsSyncEnabled() && !prefs.IsManaged();
211 void Profile::MaybeSendDestroyedNotification() {
212 if (!sent_destroyed_notification_) {
213 sent_destroyed_notification_ = true;
214 content::NotificationService::current()->Notify(
215 chrome::NOTIFICATION_PROFILE_DESTROYED,
216 content::Source<Profile>(this),
217 content::NotificationService::NoDetails());