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.
4 
5 #include "chrome/browser/profiles/incognito_mode_policy_handler.h"
6 
7 #include "base/command_line.h"
8 #include "base/notreached.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/values.h"
11 #include "build/build_config.h"
12 #include "chrome/browser/prefs/incognito_mode_prefs.h"
13 #include "chrome/common/pref_names.h"
14 #include "chrome/credential_provider/common/gcp_strings.h"
15 #include "components/policy/core/browser/policy_error_map.h"
16 #include "components/policy/core/common/policy_map.h"
17 #include "components/policy/policy_constants.h"
18 #include "components/prefs/pref_value_map.h"
19 #include "components/strings/grit/components_strings.h"
20 
21 namespace policy {
22 
IncognitoModePolicyHandler()23 IncognitoModePolicyHandler::IncognitoModePolicyHandler() {}
24 
~IncognitoModePolicyHandler()25 IncognitoModePolicyHandler::~IncognitoModePolicyHandler() {}
26 
CheckPolicySettings(const PolicyMap & policies,PolicyErrorMap * errors)27 bool IncognitoModePolicyHandler::CheckPolicySettings(const PolicyMap& policies,
28                                                      PolicyErrorMap* errors) {
29   const base::Value* availability =
30       policies.GetValue(key::kIncognitoModeAvailability);
31   if (availability) {
32     int int_value = IncognitoModePrefs::ENABLED;
33     if (!availability->GetAsInteger(&int_value)) {
34       errors->AddError(key::kIncognitoModeAvailability, IDS_POLICY_TYPE_ERROR,
35                        base::Value::GetTypeName(base::Value::Type::INTEGER));
36       return false;
37     }
38     IncognitoModePrefs::Availability availability_enum_value;
39     if (!IncognitoModePrefs::IntToAvailability(int_value,
40                                                &availability_enum_value)) {
41       errors->AddError(key::kIncognitoModeAvailability,
42                        IDS_POLICY_OUT_OF_RANGE_ERROR,
43                        base::NumberToString(int_value));
44       return false;
45     }
46     return true;
47   }
48 
49   const base::Value* deprecated_enabled =
50       policies.GetValue(key::kIncognitoEnabled);
51   if (deprecated_enabled && !deprecated_enabled->is_bool()) {
52     errors->AddError(key::kIncognitoEnabled, IDS_POLICY_TYPE_ERROR,
53                      base::Value::GetTypeName(base::Value::Type::BOOLEAN));
54     return false;
55   }
56   return true;
57 }
58 
ApplyPolicySettings(const PolicyMap & policies,PrefValueMap * prefs)59 void IncognitoModePolicyHandler::ApplyPolicySettings(const PolicyMap& policies,
60                                                      PrefValueMap* prefs) {
61 #if defined(OS_WIN)
62   // When browser starts with GCPW sign-in flag, it runs in incognito mode and
63   // gaia login page is loaded. With this flag, user can't use Chrome normally.
64   // However GCPW can't work in non-incognito mode and policy setting prevents
65   // Chrome from launching in incognito mode.To make this work, we should ignore
66   // setting inconito mode policy if GCPW sign-in flag is present.
67   if (base::CommandLine::ForCurrentProcess()->HasSwitch(
68           ::credential_provider::kGcpwSigninSwitch))
69     return;
70 #endif
71 
72   const base::Value* availability =
73       policies.GetValue(key::kIncognitoModeAvailability);
74   const base::Value* deprecated_enabled =
75       policies.GetValue(key::kIncognitoEnabled);
76   if (availability) {
77     int int_value = IncognitoModePrefs::ENABLED;
78     IncognitoModePrefs::Availability availability_enum_value;
79     if (availability->GetAsInteger(&int_value) &&
80         IncognitoModePrefs::IntToAvailability(int_value,
81                                               &availability_enum_value)) {
82       prefs->SetInteger(prefs::kIncognitoModeAvailability,
83                         availability_enum_value);
84     } else {
85       NOTREACHED();
86     }
87   } else if (deprecated_enabled) {
88     // If kIncognitoModeAvailability is not specified, check the obsolete
89     // kIncognitoEnabled.
90     bool enabled = true;
91     if (deprecated_enabled->GetAsBoolean(&enabled)) {
92       prefs->SetInteger(
93           prefs::kIncognitoModeAvailability,
94           enabled ? IncognitoModePrefs::ENABLED : IncognitoModePrefs::DISABLED);
95     } else {
96       NOTREACHED();
97     }
98   }
99 }
100 
101 }  // namespace policy
102