• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/browser/profiles/profile.h"
6 
7 #include <string>
8 
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/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/notification_service.h"
21 #include "content/public/browser/notification_source.h"
22 #include "content/public/browser/web_contents.h"
23 #include "content/public/browser/web_ui.h"
24 #include "extensions/browser/pref_names.h"
25 
26 #if defined(OS_CHROMEOS)
27 #include "base/command_line.h"
28 #include "chrome/common/chrome_switches.h"
29 #include "chromeos/chromeos_switches.h"
30 #endif
31 
32 #if defined(OS_ANDROID) && defined(FULL_SAFE_BROWSING)
33 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
34 #endif
35 
Profile()36 Profile::Profile()
37     : restored_last_session_(false),
38       sent_destroyed_notification_(false),
39       accessibility_pause_level_(0) {
40 }
41 
~Profile()42 Profile::~Profile() {
43 }
44 
45 // static
FromBrowserContext(content::BrowserContext * browser_context)46 Profile* Profile::FromBrowserContext(content::BrowserContext* browser_context) {
47   // This is safe; this is the only implementation of the browser context.
48   return static_cast<Profile*>(browser_context);
49 }
50 
51 // static
FromWebUI(content::WebUI * web_ui)52 Profile* Profile::FromWebUI(content::WebUI* web_ui) {
53   return FromBrowserContext(web_ui->GetWebContents()->GetBrowserContext());
54 }
55 
AsTestingProfile()56 TestingProfile* Profile::AsTestingProfile() {
57   return NULL;
58 }
59 
~Delegate()60 Profile::Delegate::~Delegate() {
61 }
62 
63 // static
64 const char Profile::kProfileKey[] = "__PROFILE__";
65 
66 // static
RegisterProfilePrefs(user_prefs::PrefRegistrySyncable * registry)67 void Profile::RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
68   registry->RegisterBooleanPref(
69       prefs::kSearchSuggestEnabled,
70       true,
71       user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
72 #if defined(OS_ANDROID)
73   registry->RegisterIntegerPref(
74       prefs::kContextualSearchEnabled,
75       0,
76       user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
77 #endif
78   registry->RegisterBooleanPref(
79       prefs::kSessionExitedCleanly,
80       true,
81       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
82   registry->RegisterStringPref(
83       prefs::kSessionExitType,
84       std::string(),
85       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
86 #if defined(OS_ANDROID) && defined(FULL_SAFE_BROWSING)
87   // During Finch trail, safe browsing should be turned off
88   // by default, and not sync'ed with desktop.
89   // If we want to enable safe browsing on Android, we will
90   // need to remove this Android-specific code.
91   registry->RegisterBooleanPref(
92       prefs::kSafeBrowsingEnabled,
93       SafeBrowsingService::IsEnabledByFieldTrial(),
94       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
95 #else
96   registry->RegisterBooleanPref(
97       prefs::kSafeBrowsingEnabled,
98       true,
99       user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
100 #endif
101   registry->RegisterBooleanPref(
102       prefs::kSafeBrowsingExtendedReportingEnabled,
103       false,
104       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
105   registry->RegisterBooleanPref(
106       prefs::kSafeBrowsingDownloadFeedbackEnabled,
107       false,
108       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
109   registry->RegisterBooleanPref(
110       prefs::kSafeBrowsingReportingEnabled,
111       false,
112       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
113   registry->RegisterBooleanPref(
114       prefs::kSafeBrowsingProceedAnywayDisabled,
115       false,
116       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
117   registry->RegisterBooleanPref(
118       prefs::kSafeBrowsingIncidentReportSent,
119       false,
120       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
121 #if defined(ENABLE_GOOGLE_NOW)
122   registry->RegisterBooleanPref(
123       prefs::kGoogleGeolocationAccessEnabled,
124       false,
125       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
126 #endif
127   registry->RegisterBooleanPref(
128       prefs::kDisableExtensions,
129       false,
130       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
131   registry->RegisterBooleanPref(
132       extensions::pref_names::kAlertsInitialized,
133       false,
134       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
135   registry->RegisterStringPref(
136       prefs::kSelectFileLastDirectory,
137       std::string(),
138       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
139   registry->RegisterDoublePref(
140       prefs::kDefaultZoomLevel,
141       0.0,
142       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
143   registry->RegisterDictionaryPref(
144       prefs::kPerHostZoomLevels,
145       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
146   registry->RegisterStringPref(
147       prefs::kDefaultApps,
148       "install",
149       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
150   registry->RegisterBooleanPref(
151       prefs::kSpeechRecognitionFilterProfanities,
152       true,
153       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
154   registry->RegisterIntegerPref(
155       prefs::kProfileIconVersion,
156       0,
157       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
158 #if defined(OS_CHROMEOS)
159   // TODO(dilmah): For OS_CHROMEOS we maintain kApplicationLocale in both
160   // local state and user's profile.  For other platforms we maintain
161   // kApplicationLocale only in local state.
162   // In the future we may want to maintain kApplicationLocale
163   // in user's profile for other platforms as well.
164   registry->RegisterStringPref(
165       prefs::kApplicationLocale,
166       std::string(),
167       user_prefs::PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF);
168   registry->RegisterStringPref(
169       prefs::kApplicationLocaleBackup,
170       std::string(),
171       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
172   registry->RegisterStringPref(
173       prefs::kApplicationLocaleAccepted,
174       std::string(),
175       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
176   registry->RegisterStringPref(
177       prefs::kCurrentWallpaperAppName,
178       std::string(),
179       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
180 #endif
181 
182 #if defined(OS_ANDROID)
183   registry->RegisterBooleanPref(
184       prefs::kDevToolsRemoteEnabled,
185       false,
186       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
187 #endif
188 #if defined(OS_ANDROID) || defined(OS_IOS)
189   data_reduction_proxy::RegisterSyncableProfilePrefs(registry);
190 #endif  // defined(OS_ANDROID) || defined(OS_IOS)
191 #if !defined(OS_ANDROID) && !defined(OS_CHROMEOS) && !defined(OS_IOS)
192   // Preferences related to the avatar bubble and user manager tutorials.
193   registry->RegisterIntegerPref(
194       prefs::kProfileAvatarTutorialShown,
195       0,
196       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
197   registry->RegisterBooleanPref(
198       prefs::kProfileUserManagerTutorialShown,
199       false,
200       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
201 #endif
202 }
203 
GetDebugName()204 std::string Profile::GetDebugName() {
205   std::string name = GetPath().BaseName().MaybeAsASCII();
206   if (name.empty()) {
207     name = "UnknownProfile";
208   }
209   return name;
210 }
211 
IsGuestSession() const212 bool Profile::IsGuestSession() const {
213 #if defined(OS_CHROMEOS)
214   static bool is_guest_session = CommandLine::ForCurrentProcess()->HasSwitch(
215       chromeos::switches::kGuestSession);
216   return is_guest_session;
217 #else
218   return GetPath() == ProfileManager::GetGuestProfilePath();
219 #endif
220 }
221 
IsNewProfile()222 bool Profile::IsNewProfile() {
223   // The profile has been shut down if the prefs were loaded from disk, unless
224   // first-run autoimport wrote them and reloaded the pref service.
225   // TODO(dconnelly): revisit this when crbug.com/22142 (unifying the profile
226   // import code) is fixed.
227   return GetOriginalProfile()->GetPrefs()->GetInitializationStatus() ==
228       PrefService::INITIALIZATION_STATUS_CREATED_NEW_PREF_STORE;
229 }
230 
IsSyncAccessible()231 bool Profile::IsSyncAccessible() {
232   if (ProfileSyncServiceFactory::HasProfileSyncService(this))
233     return !ProfileSyncServiceFactory::GetForProfile(this)->IsManaged();
234 
235   // No ProfileSyncService created yet - we don't want to create one, so just
236   // infer the accessible state by looking at prefs/command line flags.
237   sync_driver::SyncPrefs prefs(GetPrefs());
238   return ProfileSyncService::IsSyncEnabled() && !prefs.IsManaged();
239 }
240 
MaybeSendDestroyedNotification()241 void Profile::MaybeSendDestroyedNotification() {
242   if (!sent_destroyed_notification_) {
243     sent_destroyed_notification_ = true;
244     content::NotificationService::current()->Notify(
245         chrome::NOTIFICATION_PROFILE_DESTROYED,
246         content::Source<Profile>(this),
247         content::NotificationService::NoDetails());
248   }
249 }
250 
operator ()(Profile * a,Profile * b) const251 bool ProfileCompare::operator()(Profile* a, Profile* b) const {
252   DCHECK(a && b);
253   if (a->IsSameProfile(b))
254     return false;
255   return a->GetOriginalProfile() < b->GetOriginalProfile();
256 }
257