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/pref_registry/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 #if defined(OS_WIN)
     24 namespace {
     25 
     26 bool g_parental_control_on = false;
     27 
     28 } // empty namespace
     29 #endif  // OS_WIN
     30 
     31 // static
     32 bool IncognitoModePrefs::IntToAvailability(int in_value,
     33                                            Availability* out_value) {
     34   if (in_value < 0 || in_value >= AVAILABILITY_NUM_TYPES) {
     35     *out_value = ENABLED;
     36     return false;
     37   }
     38   *out_value = static_cast<Availability>(in_value);
     39   return true;
     40 }
     41 
     42 // static
     43 IncognitoModePrefs::Availability IncognitoModePrefs::GetAvailability(
     44     const PrefService* pref_service) {
     45   DCHECK(pref_service);
     46   int pref_value = pref_service->GetInteger(prefs::kIncognitoModeAvailability);
     47   Availability result = IncognitoModePrefs::ENABLED;
     48   bool valid = IntToAvailability(pref_value, &result);
     49   DCHECK(valid);
     50   if (ArePlatformParentalControlsEnabled()) {
     51     if (result == IncognitoModePrefs::FORCED)
     52       LOG(ERROR) << "Ignoring FORCED incognito. Parental control logging on";
     53     return IncognitoModePrefs::DISABLED;
     54   }
     55   return result;
     56 }
     57 
     58 // static
     59 void IncognitoModePrefs::SetAvailability(PrefService* prefs,
     60                                          const Availability availability) {
     61   prefs->SetInteger(prefs::kIncognitoModeAvailability, availability);
     62 }
     63 
     64 // static
     65 void IncognitoModePrefs::RegisterProfilePrefs(
     66     user_prefs::PrefRegistrySyncable* registry) {
     67   registry->RegisterIntegerPref(
     68       prefs::kIncognitoModeAvailability,
     69       IncognitoModePrefs::ENABLED,
     70       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     71 }
     72 
     73 // static
     74 bool IncognitoModePrefs::ShouldLaunchIncognito(
     75     const CommandLine& command_line,
     76     const PrefService* prefs) {
     77   Availability incognito_avail = GetAvailability(prefs);
     78   return incognito_avail != IncognitoModePrefs::DISABLED &&
     79          (command_line.HasSwitch(switches::kIncognito) ||
     80           incognito_avail == IncognitoModePrefs::FORCED);
     81 }
     82 
     83 // static
     84 bool IncognitoModePrefs::CanOpenBrowser(Profile* profile) {
     85   switch (GetAvailability(profile->GetPrefs())) {
     86     case IncognitoModePrefs::ENABLED:
     87       return true;
     88 
     89     case IncognitoModePrefs::DISABLED:
     90       return !profile->IsOffTheRecord();
     91 
     92     case IncognitoModePrefs::FORCED:
     93       return profile->IsOffTheRecord();
     94 
     95     default:
     96       NOTREACHED();
     97       return false;
     98   }
     99 }
    100 
    101 // static
    102 bool IncognitoModePrefs::ArePlatformParentalControlsEnabled() {
    103 #if defined(OS_WIN)
    104   // Disable incognito mode windows if parental controls are on. This is only
    105   // for Windows Vista and above.
    106   return base::win::IsParentalControlActivityLoggingOn();
    107 #elif defined(OS_ANDROID)
    108   return chrome::android::ChromiumApplication::AreParentalControlsEnabled();
    109 #else
    110   return false;
    111 #endif
    112 }
    113 
    114 #if defined(OS_WIN)
    115 void IncognitoModePrefs::InitializePlatformParentalControls() {
    116   g_parental_control_on = base::win::IsParentalControlActivityLoggingOn();
    117 }
    118 #endif // OS_WIN
    119 
    120 bool IncognitoModePrefs::ArePlatformParentalControlsEnabledCached() {
    121 #if defined(OS_WIN)
    122   return g_parental_control_on;
    123 #elif defined(OS_ANDROID)
    124   return chrome::android::ChromiumApplication::AreParentalControlsEnabled();
    125 #else
    126   return false;
    127 #endif
    128 }
    129 
    130