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