Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / profiles / profile.cc
blobefa31093cfdec8b01e95e912f59d49b418d53bda
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.
5 #include "chrome/browser/profiles/profile.h"
7 #include <string>
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/host_zoom_map.h"
21 #include "content/public/browser/notification_service.h"
22 #include "content/public/browser/notification_source.h"
23 #include "content/public/browser/storage_partition.h"
24 #include "content/public/browser/web_contents.h"
25 #include "content/public/browser/web_ui.h"
27 #if defined(OS_CHROMEOS)
28 #include "base/command_line.h"
29 #include "chrome/common/chrome_switches.h"
30 #include "chromeos/chromeos_switches.h"
31 #endif
33 #if defined(ENABLE_EXTENSIONS)
34 #include "extensions/browser/pref_names.h"
35 #endif
37 Profile::Profile()
38 : restored_last_session_(false),
39 sent_destroyed_notification_(false),
40 accessibility_pause_level_(0),
41 is_guest_profile_(false),
42 is_system_profile_(false) {
45 Profile::~Profile() {
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);
54 // static
55 Profile* Profile::FromWebUI(content::WebUI* web_ui) {
56 return FromBrowserContext(web_ui->GetWebContents()->GetBrowserContext());
59 TestingProfile* Profile::AsTestingProfile() {
60 return NULL;
63 chrome::ChromeZoomLevelPrefs* Profile::GetZoomLevelPrefs() {
64 return NULL;
67 Profile::Delegate::~Delegate() {
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";
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 registry->RegisterBooleanPref(
96 prefs::kSafeBrowsingEnabled,
97 true,
98 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
99 registry->RegisterBooleanPref(
100 prefs::kSafeBrowsingExtendedReportingEnabled,
101 false,
102 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
103 registry->RegisterBooleanPref(
104 prefs::kSafeBrowsingProceedAnywayDisabled,
105 false,
106 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
107 registry->RegisterDictionaryPref(
108 prefs::kSafeBrowsingIncidentsSent,
109 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
110 #if defined(ENABLE_GOOGLE_NOW)
111 registry->RegisterBooleanPref(
112 prefs::kGoogleGeolocationAccessEnabled,
113 false,
114 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
115 #endif
116 // This pref is intentionally outside the above #if. That flag corresponds
117 // to the Notifier extension and does not gate the launcher page.
118 // TODO(skare): Remove or rename ENABLE_GOOGLE_NOW: http://crbug.com/459827.
119 registry->RegisterBooleanPref(
120 prefs::kGoogleNowLauncherEnabled,
121 true,
122 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
123 registry->RegisterBooleanPref(
124 prefs::kDisableExtensions,
125 false,
126 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
127 #if defined(ENABLE_EXTENSIONS)
128 registry->RegisterBooleanPref(
129 extensions::pref_names::kAlertsInitialized,
130 false,
131 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
132 #endif
133 registry->RegisterStringPref(
134 prefs::kSelectFileLastDirectory,
135 std::string(),
136 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
137 // TODO(wjmaclean): remove the following two prefs once migration to per-
138 // partition zoom is complete.
139 registry->RegisterDoublePref(
140 prefs::kDefaultZoomLevelDeprecated,
141 0.0,
142 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
143 registry->RegisterDictionaryPref(
144 prefs::kPerHostZoomLevelsDeprecated,
145 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
147 registry->RegisterDictionaryPref(
148 prefs::kPartitionDefaultZoomLevel,
149 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
150 registry->RegisterDictionaryPref(
151 prefs::kPartitionPerHostZoomLevels,
152 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
153 registry->RegisterStringPref(
154 prefs::kDefaultApps,
155 "install",
156 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
157 registry->RegisterBooleanPref(
158 prefs::kSpeechRecognitionFilterProfanities,
159 true,
160 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
161 registry->RegisterIntegerPref(
162 prefs::kProfileIconVersion,
164 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
165 #if defined(OS_CHROMEOS)
166 // TODO(dilmah): For OS_CHROMEOS we maintain kApplicationLocale in both
167 // local state and user's profile. For other platforms we maintain
168 // kApplicationLocale only in local state.
169 // In the future we may want to maintain kApplicationLocale
170 // in user's profile for other platforms as well.
171 registry->RegisterStringPref(
172 prefs::kApplicationLocale,
173 std::string(),
174 user_prefs::PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF);
175 registry->RegisterStringPref(
176 prefs::kApplicationLocaleBackup,
177 std::string(),
178 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
179 registry->RegisterStringPref(
180 prefs::kApplicationLocaleAccepted,
181 std::string(),
182 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
183 registry->RegisterStringPref(
184 prefs::kCurrentWallpaperAppName,
185 std::string(),
186 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
187 #endif
189 #if defined(OS_ANDROID)
190 registry->RegisterBooleanPref(
191 prefs::kDevToolsRemoteEnabled,
192 false,
193 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
194 #endif
196 data_reduction_proxy::RegisterSyncableProfilePrefs(registry);
198 #if !defined(OS_ANDROID) && !defined(OS_CHROMEOS) && !defined(OS_IOS)
199 // Preferences related to the avatar bubble and user manager tutorials.
200 registry->RegisterIntegerPref(
201 prefs::kProfileAvatarTutorialShown,
203 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
204 #endif
207 std::string Profile::GetDebugName() {
208 std::string name = GetPath().BaseName().MaybeAsASCII();
209 if (name.empty()) {
210 name = "UnknownProfile";
212 return name;
215 bool Profile::IsGuestSession() const {
216 #if defined(OS_CHROMEOS)
217 static bool is_guest_session =
218 base::CommandLine::ForCurrentProcess()->HasSwitch(
219 chromeos::switches::kGuestSession);
220 return is_guest_session;
221 #else
222 return is_guest_profile_;
223 #endif
226 bool Profile::IsSystemProfile() const {
227 return is_system_profile_;
230 bool Profile::IsNewProfile() {
231 // The profile has been shut down if the prefs were loaded from disk, unless
232 // first-run autoimport wrote them and reloaded the pref service.
233 // TODO(dconnelly): revisit this when crbug.com/22142 (unifying the profile
234 // import code) is fixed.
235 return GetOriginalProfile()->GetPrefs()->GetInitializationStatus() ==
236 PrefService::INITIALIZATION_STATUS_CREATED_NEW_PREF_STORE;
239 bool Profile::IsSyncAccessible() {
240 if (ProfileSyncServiceFactory::HasProfileSyncService(this))
241 return !ProfileSyncServiceFactory::GetForProfile(this)->IsManaged();
243 // No ProfileSyncService created yet - we don't want to create one, so just
244 // infer the accessible state by looking at prefs/command line flags.
245 sync_driver::SyncPrefs prefs(GetPrefs());
246 return ProfileSyncService::IsSyncEnabled() && !prefs.IsManaged();
249 void Profile::MaybeSendDestroyedNotification() {
250 if (!sent_destroyed_notification_) {
251 sent_destroyed_notification_ = true;
253 NotifyWillBeDestroyed(this);
254 content::NotificationService::current()->Notify(
255 chrome::NOTIFICATION_PROFILE_DESTROYED,
256 content::Source<Profile>(this),
257 content::NotificationService::NoDetails());
261 bool ProfileCompare::operator()(Profile* a, Profile* b) const {
262 DCHECK(a && b);
263 if (a->IsSameProfile(b))
264 return false;
265 return a->GetOriginalProfile() < b->GetOriginalProfile();
268 double Profile::GetDefaultZoomLevelForProfile() {
269 return GetDefaultStoragePartition(this)
270 ->GetHostZoomMap()
271 ->GetDefaultZoomLevel();