summaryrefslogtreecommitdiffstats
path: root/chrome/browser/supervised_user/supervised_user_site_list.cc
blob: 2c1b89a35d8d3778d86f17b66c442cdd1401ce7d (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// 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/supervised_user/supervised_user_site_list.h"

#include "base/files/file_util.h"
#include "base/json/json_file_value_serializer.h"
#include "base/logging.h"
#include "base/metrics/histogram_macros.h"
#include "base/task_runner_util.h"
#include "base/values.h"
#include "chrome/browser/safe_json_parser.h"
#include "content/public/browser/browser_thread.h"
#include "url/gurl.h"

const int kSitelistFormatVersion = 1;

const char kHostnameHashesKey[] = "hostname_hashes";
const char kNameKey[] = "name";
const char kSitesKey[] = "sites";
const char kSitelistFormatVersionKey[] = "version";
const char kUrlKey[] = "url";
const char kWhitelistKey[] = "whitelist";

namespace {

bool g_load_in_process = false;

std::string ReadFileOnBlockingThread(const base::FilePath& path) {
  SCOPED_UMA_HISTOGRAM_TIMER("ManagedUsers.Whitelist.ReadDuration");
  std::string contents;
  base::ReadFileToString(path, &contents);
  return contents;
}

void HandleError(const base::FilePath& path, const std::string& error) {
  LOG(ERROR) << "Couldn't load site list " << path.value() << ": " << error;
}

// Takes a DictionaryValue entry from the JSON file and fills the whitelist
// (via URL patterns or hostname hashes) and the URL in the corresponding Site
// struct.
void AddWhitelistEntries(const base::DictionaryValue* site_dict,
                         SupervisedUserSiteList::Site* site) {
  bool found = false;

  const base::ListValue* whitelist = nullptr;
  if (site_dict->GetList(kWhitelistKey, &whitelist)) {
    found = true;
    for (const base::Value* entry : *whitelist) {
      std::string pattern;
      if (!entry->GetAsString(&pattern)) {
        LOG(ERROR) << "Invalid whitelist entry";
        continue;
      }

      site->patterns.push_back(pattern);
    }
  }

  const base::ListValue* hash_list = nullptr;
  if (site_dict->GetList(kHostnameHashesKey, &hash_list)) {
    found = true;
    for (const base::Value* entry : *hash_list) {
      std::string hash;
      if (!entry->GetAsString(&hash)) {
        LOG(ERROR) << "Invalid hostname_hashes entry";
        continue;
      }
      // TODO(treib): Check that |hash| has exactly 40 (2*base::kSHA1Length)
      // characters from [0-9a-fA-F]. Or just store the raw bytes (from
      // base::HexStringToBytes).

      site->hostname_hashes.push_back(hash);
    }
  }

  if (found)
    return;

  // Fall back to using a whitelist based on the URL.
  std::string url_str;
  if (!site_dict->GetString(kUrlKey, &url_str)) {
    LOG(ERROR) << "Whitelist is invalid";
    return;
  }

  GURL url(url_str);
  if (!url.is_valid()) {
    LOG(ERROR) << "URL " << url_str << " is invalid";
    return;
  }

  site->patterns.push_back(url.host());
}

}  // namespace

SupervisedUserSiteList::Site::Site(const base::string16& name) : name(name) {
}

SupervisedUserSiteList::Site::~Site() {
}

void SupervisedUserSiteList::Load(const base::FilePath& path,
                                  const LoadedCallback& callback) {
  base::PostTaskAndReplyWithResult(
      content::BrowserThread::GetBlockingPool()
          ->GetTaskRunnerWithShutdownBehavior(
              base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN).get(),
      FROM_HERE,
      base::Bind(&ReadFileOnBlockingThread, path),
      base::Bind(&SupervisedUserSiteList::ParseJson, path, callback));
}

// static
void SupervisedUserSiteList::SetLoadInProcessForTesting(bool in_process) {
  g_load_in_process = in_process;
}

SupervisedUserSiteList::SupervisedUserSiteList(const base::ListValue& sites) {
  for (const base::Value* site : sites) {
    const base::DictionaryValue* entry = nullptr;
    if (!site->GetAsDictionary(&entry)) {
      LOG(ERROR) << "Entry is invalid";
      continue;
    }

    base::string16 name;
    entry->GetString(kNameKey, &name);
    sites_.push_back(Site(name));
    AddWhitelistEntries(entry, &sites_.back());
  }
}

SupervisedUserSiteList::~SupervisedUserSiteList() {
}

// static
void SupervisedUserSiteList::ParseJson(
    const base::FilePath& path,
    const SupervisedUserSiteList::LoadedCallback& callback,
    const std::string& json) {
  if (g_load_in_process) {
    JSONFileValueDeserializer deserializer(path);
    std::string error;
    scoped_ptr<base::Value> value(deserializer.Deserialize(nullptr, &error));
    if (!value) {
      HandleError(path, error);
      return;
    }

    OnJsonParseSucceeded(path, base::TimeTicks(), callback, value.Pass());
    return;
  }

  // TODO(bauerb): Use batch mode to load multiple whitelists?
  scoped_refptr<SafeJsonParser> parser(new SafeJsonParser(
      json,
      base::Bind(&SupervisedUserSiteList::OnJsonParseSucceeded, path,
                 base::TimeTicks::Now(), callback),
      base::Bind(&HandleError, path)));
  parser->Start();
}

// static
void SupervisedUserSiteList::OnJsonParseSucceeded(
    const base::FilePath& path,
    base::TimeTicks start_time,
    const SupervisedUserSiteList::LoadedCallback& callback,
    scoped_ptr<base::Value> value) {
  if (!start_time.is_null()) {
    UMA_HISTOGRAM_TIMES("ManagedUsers.Whitelist.JsonParseDuration",
                        base::TimeTicks::Now() - start_time);
  }

  base::DictionaryValue* dict = nullptr;
  if (!value->GetAsDictionary(&dict)) {
    LOG(ERROR) << "Site list " << path.value() << " is invalid";
    return;
  }

  int version = 0;
  if (!dict->GetInteger(kSitelistFormatVersionKey, &version)) {
    LOG(ERROR) << "Site list " << path.value() << " has invalid version";
    return;
  }

  if (version > kSitelistFormatVersion) {
    LOG(ERROR) << "Site list " << path.value() << " has a too new format";
    return;
  }

  base::ListValue* sites = nullptr;
  if (!dict->GetList(kSitesKey, &sites)) {
    LOG(ERROR) << "Site list " << path.value() << " does not contain any sites";
    return;
  }

  callback.Run(make_scoped_refptr(new SupervisedUserSiteList(*sites)));
}