Home | History | Annotate | Download | only in prefs
      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/prefs/incognito_mode_prefs.h"
      6 
      7 #include "base/command_line.h"
      8 #include "base/logging.h"
      9 #include "base/prefs/pref_service.h"
     10 #include "chrome/browser/profiles/profile.h"
     11 #include "chrome/common/chrome_switches.h"
     12 #include "chrome/common/pref_names.h"
     13 #include "components/user_prefs/pref_registry_syncable.h"
     14 
     15 #if defined(OS_WIN)
     16 #include "base/win/metro.h"
     17 #endif  // OS_WIN
     18 
     19 #if defined(OS_ANDROID)
     20 #include "chrome/browser/android/chromium_application.h"
     21 #endif  // OS_ANDROID
     22 
     23 // static
     24 bool IncognitoModePrefs::IntToAvailability(int in_value,
     25                                            Availability* out_value) {
     26   if (in_value < 0 || in_value >= AVAILABILITY_NUM_TYPES) {
     27     *out_value = ENABLED;
     28     return false;
     29   }
     30   *out_value = static_cast<Availability>(in_value);
     31   return true;
     32 }
     33 
     34 // static
     35 IncognitoModePrefs::Availability IncognitoModePrefs::GetAvailability(
     36     const PrefService* pref_service) {
     37   DCHECK(pref_service);
     38   int pref_value = pref_service->GetInteger(prefs::kIncognitoModeAvailability);
     39   Availability result = IncognitoModePrefs::ENABLED;
     40   bool valid = IntToAvailability(pref_value, &result);
     41   DCHECK(valid);
     42   if (ArePlatformParentalControlsEnabled()) {
     43     if (result == IncognitoModePrefs::FORCED)
     44       LOG(ERROR) << "Ignoring FORCED incognito. Parental control logging on";
     45     return IncognitoModePrefs::DISABLED;
     46   }
     47   return result;
     48 }
     49 
     50 // static
     51 void IncognitoModePrefs::SetAvailability(PrefService* prefs,
     52                                          const Availability availability) {
     53   prefs->SetInteger(prefs::kIncognitoModeAvailability, availability);
     54 }
     55 
     56 // static
     57 void IncognitoModePrefs::RegisterProfilePrefs(
     58     user_prefs::PrefRegistrySyncable* registry) {
     59   registry->RegisterIntegerPref(
     60       prefs::kIncognitoModeAvailability,
     61       IncognitoModePrefs::ENABLED,
     62       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     63 }
     64 
     65 // static
     66 bool IncognitoModePrefs::ShouldLaunchIncognito(
     67     const CommandLine& command_line,
     68     const PrefService* prefs) {
     69   Availability incognito_avail = GetAvailability(prefs);
     70   return incognito_avail != IncognitoModePrefs::DISABLED &&
     71          (command_line.HasSwitch(switches::kIncognito) ||
     72           incognito_avail == IncognitoModePrefs::FORCED);
     73 }
     74 
     75 // static
     76 bool IncognitoModePrefs::CanOpenBrowser(Profile* profile) {
     77   switch (GetAvailability(profile->GetPrefs())) {
     78     case IncognitoModePrefs::ENABLED:
     79       return true;
     80     case IncognitoModePrefs::DISABLED:
     81       return !profile->IsOffTheRecord();
     82     case IncognitoModePrefs::FORCED:
     83       return profile->IsOffTheRecord();
     84     case IncognitoModePrefs::AVAILABILITY_NUM_TYPES:
     85       NOTREACHED();
     86   }
     87   NOTREACHED();
     88   return false;
     89 }
     90 
     91 // static
     92 bool IncognitoModePrefs::ArePlatformParentalControlsEnabled() {
     93 #if defined(OS_WIN)
     94   // Disable incognito mode windows if parental controls are on. This is only
     95   // for Windows Vista and above.
     96   return base::win::IsParentalControlActivityLoggingOn();
     97 #elif defined(OS_ANDROID)
     98   return chrome::android::ChromiumApplication::AreParentalControlsEnabled();
     99 #endif
    100   return false;
    101 }
    102