Give names to all utility processes.
[chromium-blink-merge.git] / chrome / browser / prefs / incognito_mode_prefs.cc
blob06bb0e05d5e848c9f063a6e11820102b87f655e8
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"
17 #if defined(OS_WIN)
18 #include <windows.h>
19 #include <wpcapi.h>
20 #include "base/win/scoped_comptr.h"
21 #include "base/win/windows_version.h"
22 #endif // OS_WIN
24 #if defined(OS_ANDROID)
25 #include "chrome/browser/android/chromium_application.h"
26 #endif // OS_ANDROID
28 using content::BrowserThread;
30 #if defined(OS_WIN)
31 namespace {
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)
43 return false;
45 base::win::ScopedComPtr<IWindowsParentalControlsCore> parent_controls;
46 HRESULT hr = parent_controls.CreateInstance(
47 __uuidof(WindowsParentalControls));
48 if (FAILED(hr))
49 return false;
51 base::win::ScopedComPtr<IWPCSettings> settings;
52 hr = parent_controls->GetUserSettings(nullptr, settings.Receive());
53 if (FAILED(hr))
54 return false;
56 unsigned long restrictions = 0;
57 settings->GetRestrictions(&restrictions);
59 return (restrictions & WPCFLAG_LOGGING_REQUIRED) == WPCFLAG_LOGGING_REQUIRED;
62 } // namespace
63 #endif // OS_WIN
65 // static
66 bool IncognitoModePrefs::IntToAvailability(int in_value,
67 Availability* out_value) {
68 if (in_value < 0 || in_value >= AVAILABILITY_NUM_TYPES) {
69 *out_value = ENABLED;
70 return false;
72 *out_value = static_cast<Availability>(in_value);
73 return true;
76 // static
77 IncognitoModePrefs::Availability IncognitoModePrefs::GetAvailability(
78 const PrefService* pref_service) {
79 DCHECK(pref_service);
80 int pref_value = pref_service->GetInteger(prefs::kIncognitoModeAvailability);
81 Availability result = IncognitoModePrefs::ENABLED;
82 bool valid = IntToAvailability(pref_value, &result);
83 DCHECK(valid);
84 if (ArePlatformParentalControlsEnabled()) {
85 if (result == IncognitoModePrefs::FORCED)
86 LOG(ERROR) << "Ignoring FORCED incognito. Parental control logging on";
87 return IncognitoModePrefs::DISABLED;
89 return result;
92 // static
93 void IncognitoModePrefs::SetAvailability(PrefService* prefs,
94 const Availability availability) {
95 prefs->SetInteger(prefs::kIncognitoModeAvailability, availability);
98 // static
99 void IncognitoModePrefs::RegisterProfilePrefs(
100 user_prefs::PrefRegistrySyncable* registry) {
101 registry->RegisterIntegerPref(
102 prefs::kIncognitoModeAvailability,
103 IncognitoModePrefs::ENABLED,
104 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
107 // static
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);
117 // static
118 bool IncognitoModePrefs::CanOpenBrowser(Profile* profile) {
119 if (profile->IsGuestSession())
120 return true;
122 switch (GetAvailability(profile->GetPrefs())) {
123 case IncognitoModePrefs::ENABLED:
124 return true;
126 case IncognitoModePrefs::DISABLED:
127 return !profile->IsOffTheRecord();
129 case IncognitoModePrefs::FORCED:
130 return profile->IsOffTheRecord();
132 default:
133 NOTREACHED();
134 return false;
138 // static
139 bool IncognitoModePrefs::ArePlatformParentalControlsEnabled() {
140 #if defined(OS_WIN)
141 enum class ParentalControlsState {
142 UNKNOWN = 0,
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();
163 #else
164 return false;
165 #endif