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 registry
->RegisterBooleanPref(
68 prefs::kSessionExitedCleanly
,
70 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
71 registry
->RegisterStringPref(
72 prefs::kSessionExitType
,
74 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
75 registry
->RegisterBooleanPref(
76 prefs::kSafeBrowsingEnabled
,
78 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
79 registry
->RegisterBooleanPref(
80 prefs::kSafeBrowsingReportingEnabled
,
82 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
83 registry
->RegisterBooleanPref(
84 prefs::kSafeBrowsingProceedAnywayDisabled
,
86 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
87 #if defined(ENABLE_GOOGLE_NOW)
88 registry
->RegisterBooleanPref(
89 prefs::kGoogleGeolocationAccessEnabled
,
91 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
93 registry
->RegisterBooleanPref(
94 prefs::kDisableExtensions
,
96 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
97 registry
->RegisterBooleanPref(
98 extensions::pref_names::kAlertsInitialized
,
100 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
101 registry
->RegisterStringPref(
102 prefs::kSelectFileLastDirectory
,
104 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
105 registry
->RegisterDoublePref(
106 prefs::kDefaultZoomLevel
,
108 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
109 registry
->RegisterDictionaryPref(
110 prefs::kPerHostZoomLevels
,
111 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
112 registry
->RegisterStringPref(
115 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
116 registry
->RegisterBooleanPref(
117 prefs::kSpeechRecognitionFilterProfanities
,
119 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
120 registry
->RegisterIntegerPref(
121 prefs::kProfileIconVersion
,
123 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
124 #if defined(OS_CHROMEOS)
125 // TODO(dilmah): For OS_CHROMEOS we maintain kApplicationLocale in both
126 // local state and user's profile. For other platforms we maintain
127 // kApplicationLocale only in local state.
128 // In the future we may want to maintain kApplicationLocale
129 // in user's profile for other platforms as well.
130 registry
->RegisterStringPref(
131 prefs::kApplicationLocale
,
133 user_prefs::PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF
);
134 registry
->RegisterStringPref(
135 prefs::kApplicationLocaleBackup
,
137 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
138 registry
->RegisterStringPref(
139 prefs::kApplicationLocaleAccepted
,
141 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
144 #if defined(OS_ANDROID)
145 registry
->RegisterBooleanPref(
146 prefs::kDevToolsRemoteEnabled
,
148 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
150 #if defined(OS_ANDROID) || defined(OS_IOS)
151 registry
->RegisterBooleanPref(
152 prefs::kSpdyProxyAuthEnabled
,
154 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
155 registry
->RegisterBooleanPref(
156 prefs::kSpdyProxyAuthWasEnabledBefore
,
158 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
160 #endif // defined(OS_ANDROID) || defined(OS_IOS)
164 std::string
Profile::GetDebugName() {
165 std::string name
= GetPath().BaseName().MaybeAsASCII();
167 name
= "UnknownProfile";
172 bool Profile::IsGuestSession() const {
173 #if defined(OS_CHROMEOS)
174 static bool is_guest_session
= CommandLine::ForCurrentProcess()->HasSwitch(
175 chromeos::switches::kGuestSession
);
176 return is_guest_session
;
178 return GetPath() == ProfileManager::GetGuestProfilePath();
182 bool Profile::IsNewProfile() {
183 // The profile has been shut down if the prefs were loaded from disk, unless
184 // first-run autoimport wrote them and reloaded the pref service.
185 // TODO(dconnelly): revisit this when crbug.com/22142 (unifying the profile
186 // import code) is fixed.
187 return GetOriginalProfile()->GetPrefs()->GetInitializationStatus() ==
188 PrefService::INITIALIZATION_STATUS_CREATED_NEW_PREF_STORE
;
191 bool Profile::IsSyncAccessible() {
192 if (ProfileSyncServiceFactory::HasProfileSyncService(this))
193 return !ProfileSyncServiceFactory::GetForProfile(this)->IsManaged();
195 // No ProfileSyncService created yet - we don't want to create one, so just
196 // infer the accessible state by looking at prefs/command line flags.
197 browser_sync::SyncPrefs
prefs(GetPrefs());
198 return ProfileSyncService::IsSyncEnabled() && !prefs
.IsManaged();
201 void Profile::MaybeSendDestroyedNotification() {
202 if (!sent_destroyed_notification_
) {
203 sent_destroyed_notification_
= true;
204 content::NotificationService::current()->Notify(
205 chrome::NOTIFICATION_PROFILE_DESTROYED
,
206 content::Source
<Profile
>(this),
207 content::NotificationService::NoDetails());