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/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
24#if defined(OS_CHROMEOS)
25#include "base/command_line.h"
26#include "chrome/common/chrome_switches.h"
27#include "chromeos/chromeos_switches.h"
28#endif
29
30Profile::Profile()
31    : restored_last_session_(false),
32      sent_destroyed_notification_(false),
33      accessibility_pause_level_(0) {
34}
35
36Profile::~Profile() {
37}
38
39// static
40Profile* 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);
43}
44
45// static
46Profile* Profile::FromWebUI(content::WebUI* web_ui) {
47  return FromBrowserContext(web_ui->GetWebContents()->GetBrowserContext());
48}
49
50TestingProfile* Profile::AsTestingProfile() {
51  return NULL;
52}
53
54Profile::Delegate::~Delegate() {
55}
56
57// static
58const char Profile::kProfileKey[] = "__PROFILE__";
59
60// static
61void Profile::RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
62  registry->RegisterBooleanPref(
63      prefs::kSearchSuggestEnabled,
64      true,
65      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
66  registry->RegisterBooleanPref(
67      prefs::kSessionExitedCleanly,
68      true,
69      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
70  registry->RegisterStringPref(
71      prefs::kSessionExitType,
72      std::string(),
73      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
74  registry->RegisterBooleanPref(
75      prefs::kSafeBrowsingEnabled,
76      true,
77      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
78  registry->RegisterBooleanPref(
79      prefs::kSafeBrowsingReportingEnabled,
80      false,
81      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
82  registry->RegisterBooleanPref(
83      prefs::kSafeBrowsingProceedAnywayDisabled,
84      false,
85      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
86#if defined(ENABLE_GOOGLE_NOW)
87  registry->RegisterBooleanPref(
88      prefs::kGoogleGeolocationAccessEnabled,
89      false,
90      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
91#endif
92  registry->RegisterBooleanPref(
93      prefs::kDisableExtensions,
94      false,
95      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
96  registry->RegisterBooleanPref(
97      prefs::kExtensionAlertsInitializedPref,
98      false,
99      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
100  registry->RegisterStringPref(
101      prefs::kSelectFileLastDirectory,
102      std::string(),
103      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
104  registry->RegisterDoublePref(
105      prefs::kDefaultZoomLevel,
106      0.0,
107      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
108  registry->RegisterDictionaryPref(
109      prefs::kPerHostZoomLevels,
110      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
111  registry->RegisterStringPref(
112      prefs::kDefaultApps,
113      "install",
114      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
115  registry->RegisterBooleanPref(
116      prefs::kSpeechRecognitionFilterProfanities,
117      true,
118      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
119  registry->RegisterIntegerPref(
120      prefs::kProfileIconVersion,
121      0,
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,
131      std::string(),
132      user_prefs::PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF);
133  registry->RegisterStringPref(
134      prefs::kApplicationLocaleBackup,
135      std::string(),
136      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
137  registry->RegisterStringPref(
138      prefs::kApplicationLocaleAccepted,
139      std::string(),
140      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
141#endif
142
143#if defined(OS_ANDROID)
144  registry->RegisterBooleanPref(
145      prefs::kDevToolsRemoteEnabled,
146      false,
147      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
148#endif
149#if defined(OS_ANDROID) || defined(OS_IOS)
150  registry->RegisterBooleanPref(
151      prefs::kSpdyProxyAuthEnabled,
152      false,
153      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
154  registry->RegisterBooleanPref(
155      prefs::kSpdyProxyAuthWasEnabledBefore,
156      false,
157      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
158
159#endif  // defined(OS_ANDROID) || defined(OS_IOS)
160}
161
162
163std::string Profile::GetDebugName() {
164  std::string name = GetPath().BaseName().MaybeAsASCII();
165  if (name.empty()) {
166    name = "UnknownProfile";
167  }
168  return name;
169}
170
171bool 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;
176#else
177  return GetPath() == ProfileManager::GetGuestProfilePath();
178#endif
179}
180
181bool 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;
188}
189
190bool Profile::IsSyncAccessible() {
191  if (ProfileSyncServiceFactory::HasProfileSyncService(this))
192    return !ProfileSyncServiceFactory::GetForProfile(this)->IsManaged();
193
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();
198}
199
200void 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());
207  }
208}
209