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/prefs/incognito_mode_prefs.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/threading/thread_restrictions.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/common/pref_names.h"
14 #include "components/pref_registry/pref_registry_syncable.h"
15 #include "content/public/browser/browser_thread.h"
20 #include "base/win/scoped_comptr.h"
21 #include "base/win/windows_version.h"
24 #if defined(OS_ANDROID)
25 #include "chrome/browser/android/chromium_application.h"
28 using content::BrowserThread
;
33 // Returns true if Windows Parental control activity logging is enabled. This
34 // feature is available on Windows 7 and beyond. This function should be called
35 // on a COM Initialized thread and is potentially blocking.
36 bool IsParentalControlActivityLoggingOn() {
37 // Since we can potentially block, make sure the thread is okay with this.
38 base::ThreadRestrictions::AssertIOAllowed();
39 base::ThreadRestrictions::AssertWaitAllowed();
41 // Query this info on Windows 7 and above.
42 if (base::win::GetVersion() < base::win::VERSION_WIN7
)
45 base::win::ScopedComPtr
<IWindowsParentalControlsCore
> parent_controls
;
46 HRESULT hr
= parent_controls
.CreateInstance(
47 __uuidof(WindowsParentalControls
));
51 base::win::ScopedComPtr
<IWPCSettings
> settings
;
52 hr
= parent_controls
->GetUserSettings(nullptr, settings
.Receive());
56 unsigned long restrictions
= 0;
57 settings
->GetRestrictions(&restrictions
);
59 return (restrictions
& WPCFLAG_LOGGING_REQUIRED
) == WPCFLAG_LOGGING_REQUIRED
;
66 bool IncognitoModePrefs::IntToAvailability(int in_value
,
67 Availability
* out_value
) {
68 if (in_value
< 0 || in_value
>= AVAILABILITY_NUM_TYPES
) {
72 *out_value
= static_cast<Availability
>(in_value
);
77 IncognitoModePrefs::Availability
IncognitoModePrefs::GetAvailability(
78 const PrefService
* pref_service
) {
80 int pref_value
= pref_service
->GetInteger(prefs::kIncognitoModeAvailability
);
81 Availability result
= IncognitoModePrefs::ENABLED
;
82 bool valid
= IntToAvailability(pref_value
, &result
);
84 if (ArePlatformParentalControlsEnabled()) {
85 if (result
== IncognitoModePrefs::FORCED
)
86 LOG(ERROR
) << "Ignoring FORCED incognito. Parental control logging on";
87 return IncognitoModePrefs::DISABLED
;
93 void IncognitoModePrefs::SetAvailability(PrefService
* prefs
,
94 const Availability availability
) {
95 prefs
->SetInteger(prefs::kIncognitoModeAvailability
, availability
);
99 void IncognitoModePrefs::RegisterProfilePrefs(
100 user_prefs::PrefRegistrySyncable
* registry
) {
101 registry
->RegisterIntegerPref(
102 prefs::kIncognitoModeAvailability
,
103 IncognitoModePrefs::ENABLED
,
104 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
108 bool IncognitoModePrefs::ShouldLaunchIncognito(
109 const base::CommandLine
& command_line
,
110 const PrefService
* prefs
) {
111 Availability incognito_avail
= GetAvailability(prefs
);
112 return incognito_avail
!= IncognitoModePrefs::DISABLED
&&
113 (command_line
.HasSwitch(switches::kIncognito
) ||
114 incognito_avail
== IncognitoModePrefs::FORCED
);
118 bool IncognitoModePrefs::CanOpenBrowser(Profile
* profile
) {
119 if (profile
->IsGuestSession())
122 switch (GetAvailability(profile
->GetPrefs())) {
123 case IncognitoModePrefs::ENABLED
:
126 case IncognitoModePrefs::DISABLED
:
127 return !profile
->IsOffTheRecord();
129 case IncognitoModePrefs::FORCED
:
130 return profile
->IsOffTheRecord();
139 bool IncognitoModePrefs::ArePlatformParentalControlsEnabled() {
141 enum class ParentalControlsState
{
143 ACTIVITY_LOGGING_DISABLED
= 1,
144 ACTIVITY_LOGGING_ENABLED
= 2,
146 static ParentalControlsState parental_controls_state
=
147 ParentalControlsState::UNKNOWN
;
148 if (parental_controls_state
== ParentalControlsState::UNKNOWN
) {
149 // Production: The thread isn't initialized, so we're the only thread that
150 // should be able to update this.
151 // Test: The thread may be initialized, so check that it's the UI thread.
152 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI
) ||
153 BrowserThread::CurrentlyOn(BrowserThread::UI
));
154 parental_controls_state
=
155 IsParentalControlActivityLoggingOn() ?
156 ParentalControlsState::ACTIVITY_LOGGING_ENABLED
:
157 ParentalControlsState::ACTIVITY_LOGGING_DISABLED
;
159 return parental_controls_state
==
160 ParentalControlsState::ACTIVITY_LOGGING_ENABLED
;
161 #elif defined(OS_ANDROID)
162 return chrome::android::ChromiumApplication::AreParentalControlsEnabled();