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/policy/javascript_policy_handler.h"
6
7#include "base/prefs/pref_value_map.h"
8#include "base/values.h"
9#include "chrome/common/pref_names.h"
10#include "components/content_settings/core/common/content_settings.h"
11#include "components/policy/core/browser/policy_error_map.h"
12#include "components/policy/core/common/policy_map.h"
13#include "grit/components_strings.h"
14#include "policy/policy_constants.h"
15
16namespace policy {
17
18JavascriptPolicyHandler::JavascriptPolicyHandler() {}
19
20JavascriptPolicyHandler::~JavascriptPolicyHandler() {}
21
22bool JavascriptPolicyHandler::CheckPolicySettings(const PolicyMap& policies,
23                                                  PolicyErrorMap* errors) {
24  const base::Value* javascript_enabled =
25      policies.GetValue(key::kJavascriptEnabled);
26  const base::Value* default_setting =
27      policies.GetValue(key::kDefaultJavaScriptSetting);
28
29  if (javascript_enabled &&
30      !javascript_enabled->IsType(base::Value::TYPE_BOOLEAN)) {
31    errors->AddError(key::kJavascriptEnabled,
32                     IDS_POLICY_TYPE_ERROR,
33                     ValueTypeToString(base::Value::TYPE_BOOLEAN));
34  }
35
36  if (default_setting && !default_setting->IsType(base::Value::TYPE_INTEGER)) {
37    errors->AddError(key::kDefaultJavaScriptSetting,
38                     IDS_POLICY_TYPE_ERROR,
39                     ValueTypeToString(base::Value::TYPE_INTEGER));
40  }
41
42  if (javascript_enabled && default_setting) {
43    errors->AddError(key::kJavascriptEnabled,
44                     IDS_POLICY_OVERRIDDEN,
45                     key::kDefaultJavaScriptSetting);
46  }
47
48  return true;
49}
50
51void JavascriptPolicyHandler::ApplyPolicySettings(const PolicyMap& policies,
52                                                  PrefValueMap* prefs) {
53  int setting = CONTENT_SETTING_DEFAULT;
54  const base::Value* default_setting =
55      policies.GetValue(key::kDefaultJavaScriptSetting);
56
57  if (default_setting) {
58    default_setting->GetAsInteger(&setting);
59  } else {
60    const base::Value* javascript_enabled =
61        policies.GetValue(key::kJavascriptEnabled);
62    bool enabled = true;
63    if (javascript_enabled &&
64        javascript_enabled->GetAsBoolean(&enabled) &&
65        !enabled) {
66      setting = CONTENT_SETTING_BLOCK;
67    }
68  }
69
70  if (setting != CONTENT_SETTING_DEFAULT)
71    prefs->SetInteger(prefs::kManagedDefaultJavaScriptSetting, setting);
72}
73
74}  // namespace policy
75