summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/managed_bookmarks_policy_handler.cc
blob: 7c4abafdb75e9256147814f3f71a3f548cbea5c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/policy/managed_bookmarks_policy_handler.h"

#include "base/prefs/pref_value_map.h"
#include "base/values.h"
#include "chrome/common/net/url_fixer_upper.h"
#include "chrome/common/pref_names.h"
#include "components/policy/core/browser/policy_error_map.h"
#include "components/policy/core/common/policy_map.h"
#include "grit/component_strings.h"
#include "policy/policy_constants.h"
#include "url/gurl.h"

namespace policy {

namespace {

bool GetBookmark(const base::Value& value,
                 std::string* name,
                 std::string* url) {
  const base::DictionaryValue* dict = NULL;
  if (!value.GetAsDictionary(&dict))
    return false;
  std::string url_string;
  if (!dict->GetStringWithoutPathExpansion(ManagedBookmarksPolicyHandler::kName,
                                           name) ||
      !dict->GetStringWithoutPathExpansion(ManagedBookmarksPolicyHandler::kUrl,
                                           &url_string)) {
    return false;
  }
  GURL gurl = URLFixerUpper::FixupURL(url_string, "");
  if (!gurl.is_valid())
    return false;
  *url = gurl.spec();
  return true;
}

}  // namespace

const char ManagedBookmarksPolicyHandler::kName[] = "name";
const char ManagedBookmarksPolicyHandler::kUrl[] = "url";

ManagedBookmarksPolicyHandler::ManagedBookmarksPolicyHandler()
    : TypeCheckingPolicyHandler(key::kManagedBookmarks,
                                base::Value::TYPE_LIST) {}

ManagedBookmarksPolicyHandler::~ManagedBookmarksPolicyHandler() {}

bool ManagedBookmarksPolicyHandler::CheckPolicySettings(
    const PolicyMap& policies,
    PolicyErrorMap* errors) {
  const base::Value* value = NULL;
  if (!CheckAndGetValue(policies, errors, &value))
    return false;

  if (!value)
    return true;

  const base::ListValue* list = NULL;
  value->GetAsList(&list);
  DCHECK(list);

  for (base::ListValue::const_iterator it = list->begin();
       it != list->end(); ++it) {
    std::string name;
    std::string url;
    if (!*it || !GetBookmark(**it, &name, &url)) {
      size_t index = it - list->begin();
      errors->AddError(policy_name(), index, IDS_POLICY_INVALID_BOOKMARK);
    }
  }

  return true;
}

void ManagedBookmarksPolicyHandler::ApplyPolicySettings(
    const PolicyMap& policies,
    PrefValueMap* prefs) {
  const base::Value* value = policies.GetValue(policy_name());
  const base::ListValue* list = NULL;
  if (!value || !value->GetAsList(&list))
    return;

  base::ListValue* bookmarks = new base::ListValue();
  for (base::ListValue::const_iterator it = list->begin();
       it != list->end(); ++it) {
    std::string name;
    std::string url;
    if (*it && GetBookmark(**it, &name, &url)) {
      base::DictionaryValue* dict = new base::DictionaryValue();
      dict->SetString(kName, name);
      dict->SetString(kUrl, url);
      bookmarks->Append(dict);
    }
  }

  prefs->SetValue(prefs::kManagedBookmarks, bookmarks);
}

}  // namespace policy