1 // Copyright 2013 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/profiles/incognito_mode_policy_handler.h"
7 #include "base/logging.h"
8 #include "base/prefs/pref_value_map.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/values.h"
11 #include "chrome/browser/prefs/incognito_mode_prefs.h"
12 #include "chrome/common/pref_names.h"
13 #include "components/policy/core/browser/policy_error_map.h"
14 #include "components/policy/core/common/policy_map.h"
15 #include "grit/components_strings.h"
16 #include "policy/policy_constants.h"
20 IncognitoModePolicyHandler::IncognitoModePolicyHandler() {}
22 IncognitoModePolicyHandler::~IncognitoModePolicyHandler() {}
24 bool IncognitoModePolicyHandler::CheckPolicySettings(const PolicyMap
& policies
,
25 PolicyErrorMap
* errors
) {
26 int int_value
= IncognitoModePrefs::ENABLED
;
27 const base::Value
* availability
=
28 policies
.GetValue(key::kIncognitoModeAvailability
);
31 if (availability
->GetAsInteger(&int_value
)) {
32 IncognitoModePrefs::Availability availability_enum_value
;
33 if (!IncognitoModePrefs::IntToAvailability(int_value
,
34 &availability_enum_value
)) {
35 errors
->AddError(key::kIncognitoModeAvailability
,
36 IDS_POLICY_OUT_OF_RANGE_ERROR
,
37 base::IntToString(int_value
));
41 errors
->AddError(key::kIncognitoModeAvailability
,
42 IDS_POLICY_TYPE_ERROR
,
43 ValueTypeToString(base::Value::TYPE_INTEGER
));
47 const base::Value
* deprecated_enabled
=
48 policies
.GetValue(key::kIncognitoEnabled
);
49 if (deprecated_enabled
&&
50 !deprecated_enabled
->IsType(base::Value::TYPE_BOOLEAN
)) {
51 errors
->AddError(key::kIncognitoEnabled
,
52 IDS_POLICY_TYPE_ERROR
,
53 ValueTypeToString(base::Value::TYPE_BOOLEAN
));
60 void IncognitoModePolicyHandler::ApplyPolicySettings(const PolicyMap
& policies
,
61 PrefValueMap
* prefs
) {
62 const base::Value
* availability
=
63 policies
.GetValue(key::kIncognitoModeAvailability
);
64 const base::Value
* deprecated_enabled
=
65 policies
.GetValue(key::kIncognitoEnabled
);
67 int int_value
= IncognitoModePrefs::ENABLED
;
68 IncognitoModePrefs::Availability availability_enum_value
;
69 if (availability
->GetAsInteger(&int_value
) &&
70 IncognitoModePrefs::IntToAvailability(int_value
,
71 &availability_enum_value
)) {
72 prefs
->SetInteger(prefs::kIncognitoModeAvailability
,
73 availability_enum_value
);
77 } else if (deprecated_enabled
) {
78 // If kIncognitoModeAvailability is not specified, check the obsolete
81 if (deprecated_enabled
->GetAsBoolean(&enabled
)) {
83 prefs::kIncognitoModeAvailability
,
84 enabled
? IncognitoModePrefs::ENABLED
: IncognitoModePrefs::DISABLED
);