- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / profiles / incognito_mode_policy_handler.cc
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/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/policy/policy_error_map.h"
12 #include "chrome/browser/policy/policy_map.h"
13 #include "chrome/browser/prefs/incognito_mode_prefs.h"
14 #include "chrome/common/pref_names.h"
15 #include "grit/generated_resources.h"
16 #include "policy/policy_constants.h"
17
18 namespace policy {
19
20 IncognitoModePolicyHandler::IncognitoModePolicyHandler() {}
21
22 IncognitoModePolicyHandler::~IncognitoModePolicyHandler() {}
23
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);
29
30   if (availability) {
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));
38         return false;
39       }
40     } else {
41       errors->AddError(key::kIncognitoModeAvailability,
42                        IDS_POLICY_TYPE_ERROR,
43                        ValueTypeToString(base::Value::TYPE_INTEGER));
44       return false;
45     }
46   } else {
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));
54       return false;
55     }
56   }
57   return true;
58 }
59
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);
66   if (availability) {
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->SetValue(prefs::kIncognitoModeAvailability,
73                       base::Value::CreateIntegerValue(availability_enum_value));
74     } else {
75       NOTREACHED();
76     }
77   } else if (deprecated_enabled) {
78     // If kIncognitoModeAvailability is not specified, check the obsolete
79     // kIncognitoEnabled.
80     bool enabled = true;
81     if (deprecated_enabled->GetAsBoolean(&enabled)) {
82       prefs->SetInteger(
83           prefs::kIncognitoModeAvailability,
84           enabled ? IncognitoModePrefs::ENABLED : IncognitoModePrefs::DISABLED);
85     } else {
86       NOTREACHED();
87     }
88   }
89 }
90
91 }  // namespace policy