profile.cc revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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/first_run/first_run.h"
12#include "chrome/browser/sync/profile_sync_service.h"
13#include "chrome/browser/sync/sync_prefs.h"
14#include "chrome/common/chrome_notification_types.h"
15#include "chrome/common/pref_names.h"
16#include "components/user_prefs/pref_registry_syncable.h"
17#include "content/public/browser/notification_service.h"
18#include "content/public/browser/notification_source.h"
19#include "content/public/browser/web_contents.h"
20#include "content/public/browser/web_ui.h"
21
22#if defined(OS_CHROMEOS)
23#include "base/command_line.h"
24#include "chrome/common/chrome_switches.h"
25#include "chromeos/chromeos_switches.h"
26#endif
27
28Profile::Profile()
29    : restored_last_session_(false),
30      sent_destroyed_notification_(false),
31      accessibility_pause_level_(0) {
32}
33
34Profile::~Profile() {
35}
36
37// static
38Profile* Profile::FromBrowserContext(content::BrowserContext* browser_context) {
39  // This is safe; this is the only implementation of the browser context.
40  return static_cast<Profile*>(browser_context);
41}
42
43// static
44Profile* Profile::FromWebUI(content::WebUI* web_ui) {
45  return FromBrowserContext(web_ui->GetWebContents()->GetBrowserContext());
46}
47
48TestingProfile* Profile::AsTestingProfile() {
49  return NULL;
50}
51
52Profile::Delegate::~Delegate() {
53}
54
55// static
56const char Profile::kProfileKey[] = "__PROFILE__";
57
58// static
59void Profile::RegisterUserPrefs(user_prefs::PrefRegistrySyncable* registry) {
60  registry->RegisterBooleanPref(
61      prefs::kSearchSuggestEnabled,
62      true,
63      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
64  registry->RegisterBooleanPref(
65      prefs::kSessionExitedCleanly,
66      true,
67      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
68  registry->RegisterStringPref(
69      prefs::kSessionExitType,
70      std::string(),
71      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
72  registry->RegisterBooleanPref(
73      prefs::kSafeBrowsingEnabled,
74      true,
75      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
76  registry->RegisterBooleanPref(
77      prefs::kSafeBrowsingReportingEnabled,
78      false,
79      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
80  registry->RegisterBooleanPref(
81      prefs::kSafeBrowsingProceedAnywayDisabled,
82      false,
83      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
84  registry->RegisterBooleanPref(
85      prefs::kDisableExtensions,
86      false,
87      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
88  registry->RegisterBooleanPref(
89      prefs::kExtensionAlertsInitializedPref,
90      false,
91      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
92  registry->RegisterStringPref(
93      prefs::kSelectFileLastDirectory,
94      std::string(),
95      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
96  registry->RegisterDoublePref(
97      prefs::kDefaultZoomLevel,
98      0.0,
99      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
100  registry->RegisterDictionaryPref(
101      prefs::kPerHostZoomLevels,
102      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
103  registry->RegisterStringPref(
104      prefs::kDefaultApps,
105      "install",
106      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
107#if defined(OS_CHROMEOS)
108  // TODO(dilmah): For OS_CHROMEOS we maintain kApplicationLocale in both
109  // local state and user's profile.  For other platforms we maintain
110  // kApplicationLocale only in local state.
111  // In the future we may want to maintain kApplicationLocale
112  // in user's profile for other platforms as well.
113  registry->RegisterStringPref(prefs::kApplicationLocale,
114                               std::string(),
115                               user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
116  registry->RegisterStringPref(
117      prefs::kApplicationLocaleBackup,
118      std::string(),
119      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
120  registry->RegisterStringPref(
121      prefs::kApplicationLocaleAccepted,
122      std::string(),
123      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
124#endif
125
126#if defined(OS_ANDROID)
127  registry->RegisterBooleanPref(
128      prefs::kDevToolsRemoteEnabled,
129      false,
130      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
131#endif
132#if defined(OS_ANDROID) || defined(OS_IOS)
133  registry->RegisterBooleanPref(
134      prefs::kSpdyProxyAuthEnabled,
135      false,
136      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
137  registry->RegisterBooleanPref(
138      prefs::kSpdyProxyAuthWasEnabledBefore,
139      false,
140      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
141
142#endif  // defined(OS_ANDROID) || defined(OS_IOS)
143}
144
145
146std::string Profile::GetDebugName() {
147  std::string name = GetPath().BaseName().MaybeAsASCII();
148  if (name.empty()) {
149    name = "UnknownProfile";
150  }
151  return name;
152}
153
154bool Profile::IsGuestSession() const {
155#if defined(OS_CHROMEOS)
156  static bool is_guest_session = CommandLine::ForCurrentProcess()->HasSwitch(
157      chromeos::switches::kGuestSession);
158  return is_guest_session;
159#else
160  return false;
161#endif
162}
163
164bool Profile::IsNewProfile() {
165  // The profile has been shut down if the prefs were loaded from disk, unless
166  // first-run autoimport wrote them and reloaded the pref service.
167  // TODO(dconnelly): revisit this when crbug.com/22142 (unifying the profile
168  // import code) is fixed.
169  return GetOriginalProfile()->GetPrefs()->GetInitializationStatus() ==
170      PrefService::INITIALIZATION_STATUS_CREATED_NEW_PREF_STORE;
171}
172
173bool Profile::IsSyncAccessible() {
174  browser_sync::SyncPrefs prefs(GetPrefs());
175  return ProfileSyncService::IsSyncEnabled() && !prefs.IsManaged();
176}
177
178void Profile::MaybeSendDestroyedNotification() {
179  if (!sent_destroyed_notification_) {
180    sent_destroyed_notification_ = true;
181    content::NotificationService::current()->Notify(
182        chrome::NOTIFICATION_PROFILE_DESTROYED,
183        content::Source<Profile>(this),
184        content::NotificationService::NoDetails());
185  }
186}
187