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"
24 #if defined(OS_CHROMEOS)
25 #include "base/command_line.h"
26 #include "chrome/common/chrome_switches.h"
27 #include "chromeos/chromeos_switches.h"
31 : restored_last_session_(false),
32 sent_destroyed_notification_(false),
33 accessibility_pause_level_(0) {
40 Profile* Profile::FromBrowserContext(content::BrowserContext* browser_context) {
41 // This is safe; this is the only implementation of the browser context.
42 return static_cast<Profile*>(browser_context);
46 Profile* Profile::FromWebUI(content::WebUI* web_ui) {
47 return FromBrowserContext(web_ui->GetWebContents()->GetBrowserContext());
50 TestingProfile* Profile::AsTestingProfile() {
54 Profile::Delegate::~Delegate() {
58 const char Profile::kProfileKey[] = "__PROFILE__";
61 void Profile::RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
62 registry->RegisterBooleanPref(
63 prefs::kSearchSuggestEnabled,
65 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
66 registry->RegisterBooleanPref(
67 prefs::kSessionExitedCleanly,
69 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
70 registry->RegisterStringPref(
71 prefs::kSessionExitType,
73 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
74 registry->RegisterBooleanPref(
75 prefs::kSafeBrowsingEnabled,
77 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
78 registry->RegisterBooleanPref(
79 prefs::kSafeBrowsingReportingEnabled,
81 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
82 registry->RegisterBooleanPref(
83 prefs::kSafeBrowsingProceedAnywayDisabled,
85 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
86 #if defined(ENABLE_GOOGLE_NOW)
87 registry->RegisterBooleanPref(
88 prefs::kGoogleGeolocationAccessEnabled,
90 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
92 registry->RegisterBooleanPref(
93 prefs::kDisableExtensions,
95 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
96 registry->RegisterBooleanPref(
97 prefs::kExtensionAlertsInitializedPref,
99 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
100 registry->RegisterStringPref(
101 prefs::kSelectFileLastDirectory,
103 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
104 registry->RegisterDoublePref(
105 prefs::kDefaultZoomLevel,
107 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
108 registry->RegisterDictionaryPref(
109 prefs::kPerHostZoomLevels,
110 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
111 registry->RegisterStringPref(
114 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
115 registry->RegisterBooleanPref(
116 prefs::kSpeechRecognitionFilterProfanities,
118 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
119 registry->RegisterIntegerPref(
120 prefs::kProfileIconVersion,
122 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
123 #if defined(OS_CHROMEOS)
124 // TODO(dilmah): For OS_CHROMEOS we maintain kApplicationLocale in both
125 // local state and user's profile. For other platforms we maintain
126 // kApplicationLocale only in local state.
127 // In the future we may want to maintain kApplicationLocale
128 // in user's profile for other platforms as well.
129 registry->RegisterStringPref(
130 prefs::kApplicationLocale,
132 user_prefs::PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF);
133 registry->RegisterStringPref(
134 prefs::kApplicationLocaleBackup,
136 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
137 registry->RegisterStringPref(
138 prefs::kApplicationLocaleAccepted,
140 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
143 #if defined(OS_ANDROID)
144 registry->RegisterBooleanPref(
145 prefs::kDevToolsRemoteEnabled,
147 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
149 #if defined(OS_ANDROID) || defined(OS_IOS)
150 registry->RegisterBooleanPref(
151 prefs::kSpdyProxyAuthEnabled,
153 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
154 registry->RegisterBooleanPref(
155 prefs::kSpdyProxyAuthWasEnabledBefore,
157 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
159 #endif // defined(OS_ANDROID) || defined(OS_IOS)
163 std::string Profile::GetDebugName() {
164 std::string name = GetPath().BaseName().MaybeAsASCII();
166 name = "UnknownProfile";
171 bool Profile::IsGuestSession() const {
172 #if defined(OS_CHROMEOS)
173 static bool is_guest_session = CommandLine::ForCurrentProcess()->HasSwitch(
174 chromeos::switches::kGuestSession);
175 return is_guest_session;
177 return GetPath() == ProfileManager::GetGuestProfilePath();
181 bool Profile::IsNewProfile() {
182 // The profile has been shut down if the prefs were loaded from disk, unless
183 // first-run autoimport wrote them and reloaded the pref service.
184 // TODO(dconnelly): revisit this when crbug.com/22142 (unifying the profile
185 // import code) is fixed.
186 return GetOriginalProfile()->GetPrefs()->GetInitializationStatus() ==
187 PrefService::INITIALIZATION_STATUS_CREATED_NEW_PREF_STORE;
190 bool Profile::IsSyncAccessible() {
191 if (ProfileSyncServiceFactory::HasProfileSyncService(this))
192 return !ProfileSyncServiceFactory::GetForProfile(this)->IsManaged();
194 // No ProfileSyncService created yet - we don't want to create one, so just
195 // infer the accessible state by looking at prefs/command line flags.
196 browser_sync::SyncPrefs prefs(GetPrefs());
197 return ProfileSyncService::IsSyncEnabled() && !prefs.IsManaged();
200 void Profile::MaybeSendDestroyedNotification() {
201 if (!sent_destroyed_notification_) {
202 sent_destroyed_notification_ = true;
203 content::NotificationService::current()->Notify(
204 chrome::NOTIFICATION_PROFILE_DESTROYED,
205 content::Source<Profile>(this),
206 content::NotificationService::NoDetails());