profile.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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
36Profile::Profile()
37    : restored_last_session_(false),
38      sent_destroyed_notification_(false),
39      accessibility_pause_level_(0) {
40}
41
42Profile::~Profile() {
43}
44
45// static
46Profile* 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
52Profile* Profile::FromWebUI(content::WebUI* web_ui) {
53  return FromBrowserContext(web_ui->GetWebContents()->GetBrowserContext());
54}
55
56TestingProfile* Profile::AsTestingProfile() {
57  return NULL;
58}
59
60Profile::Delegate::~Delegate() {
61}
62
63// static
64const char Profile::kProfileKey[] = "__PROFILE__";
65
66// static
67void 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->RegisterStringPref(
74      prefs::kContextualSearchEnabled,
75      std::string(),
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  registry->RegisterDictionaryPref(
122      prefs::kSafeBrowsingIncidentsSent,
123      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
124#if defined(ENABLE_GOOGLE_NOW)
125  registry->RegisterBooleanPref(
126      prefs::kGoogleGeolocationAccessEnabled,
127      false,
128      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
129#endif
130  registry->RegisterBooleanPref(
131      prefs::kDisableExtensions,
132      false,
133      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
134  registry->RegisterBooleanPref(
135      extensions::pref_names::kAlertsInitialized,
136      false,
137      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
138  registry->RegisterStringPref(
139      prefs::kSelectFileLastDirectory,
140      std::string(),
141      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
142  registry->RegisterDoublePref(
143      prefs::kDefaultZoomLevel,
144      0.0,
145      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
146  registry->RegisterDictionaryPref(
147      prefs::kPerHostZoomLevels,
148      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
149  registry->RegisterStringPref(
150      prefs::kDefaultApps,
151      "install",
152      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
153  registry->RegisterBooleanPref(
154      prefs::kSpeechRecognitionFilterProfanities,
155      true,
156      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
157  registry->RegisterIntegerPref(
158      prefs::kProfileIconVersion,
159      0,
160      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
161#if defined(OS_CHROMEOS)
162  // TODO(dilmah): For OS_CHROMEOS we maintain kApplicationLocale in both
163  // local state and user's profile.  For other platforms we maintain
164  // kApplicationLocale only in local state.
165  // In the future we may want to maintain kApplicationLocale
166  // in user's profile for other platforms as well.
167  registry->RegisterStringPref(
168      prefs::kApplicationLocale,
169      std::string(),
170      user_prefs::PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF);
171  registry->RegisterStringPref(
172      prefs::kApplicationLocaleBackup,
173      std::string(),
174      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
175  registry->RegisterStringPref(
176      prefs::kApplicationLocaleAccepted,
177      std::string(),
178      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
179  registry->RegisterStringPref(
180      prefs::kCurrentWallpaperAppName,
181      std::string(),
182      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
183#endif
184
185#if defined(OS_ANDROID)
186  registry->RegisterBooleanPref(
187      prefs::kDevToolsRemoteEnabled,
188      false,
189      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
190#endif
191#if defined(OS_ANDROID) || defined(OS_IOS)
192  data_reduction_proxy::RegisterSyncableProfilePrefs(registry);
193#endif  // defined(OS_ANDROID) || defined(OS_IOS)
194#if !defined(OS_ANDROID) && !defined(OS_CHROMEOS) && !defined(OS_IOS)
195  // Preferences related to the avatar bubble and user manager tutorials.
196  registry->RegisterIntegerPref(
197      prefs::kProfileAvatarTutorialShown,
198      0,
199      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
200  registry->RegisterBooleanPref(
201      prefs::kProfileUserManagerTutorialShown,
202      false,
203      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
204#endif
205}
206
207std::string Profile::GetDebugName() {
208  std::string name = GetPath().BaseName().MaybeAsASCII();
209  if (name.empty()) {
210    name = "UnknownProfile";
211  }
212  return name;
213}
214
215bool Profile::IsGuestSession() const {
216#if defined(OS_CHROMEOS)
217  static bool is_guest_session = CommandLine::ForCurrentProcess()->HasSwitch(
218      chromeos::switches::kGuestSession);
219  return is_guest_session;
220#else
221  return GetPath() == ProfileManager::GetGuestProfilePath();
222#endif
223}
224
225bool Profile::IsNewProfile() {
226  // The profile has been shut down if the prefs were loaded from disk, unless
227  // first-run autoimport wrote them and reloaded the pref service.
228  // TODO(dconnelly): revisit this when crbug.com/22142 (unifying the profile
229  // import code) is fixed.
230  return GetOriginalProfile()->GetPrefs()->GetInitializationStatus() ==
231      PrefService::INITIALIZATION_STATUS_CREATED_NEW_PREF_STORE;
232}
233
234bool Profile::IsSyncAccessible() {
235  if (ProfileSyncServiceFactory::HasProfileSyncService(this))
236    return !ProfileSyncServiceFactory::GetForProfile(this)->IsManaged();
237
238  // No ProfileSyncService created yet - we don't want to create one, so just
239  // infer the accessible state by looking at prefs/command line flags.
240  sync_driver::SyncPrefs prefs(GetPrefs());
241  return ProfileSyncService::IsSyncEnabled() && !prefs.IsManaged();
242}
243
244void Profile::MaybeSendDestroyedNotification() {
245  if (!sent_destroyed_notification_) {
246    sent_destroyed_notification_ = true;
247    content::NotificationService::current()->Notify(
248        chrome::NOTIFICATION_PROFILE_DESTROYED,
249        content::Source<Profile>(this),
250        content::NotificationService::NoDetails());
251  }
252}
253
254bool ProfileCompare::operator()(Profile* a, Profile* b) const {
255  DCHECK(a && b);
256  if (a->IsSameProfile(b))
257    return false;
258  return a->GetOriginalProfile() < b->GetOriginalProfile();
259}
260