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