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