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