File: supervised_user_blacklist.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (94 lines) | stat: -rw-r--r-- 2,906 bytes parent folder | download
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
// 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/experimental/supervised_user_blacklist.h"

#include <algorithm>
#include <cstring>
#include <fstream>

#include "base/files/file_path.h"
#include "base/files/memory_mapped_file.h"
#include "content/public/browser/browser_thread.h"
#include "url/gurl.h"

using content::BrowserThread;

namespace {

scoped_ptr<std::vector<SupervisedUserBlacklist::Hash> >
ReadFromBinaryFileOnFileThread(const base::FilePath& path) {
  DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());

  scoped_ptr<std::vector<SupervisedUserBlacklist::Hash> > host_hashes(
      new std::vector<SupervisedUserBlacklist::Hash>);

  base::MemoryMappedFile file;
  file.Initialize(path);
  if (!file.IsValid())
    return host_hashes.Pass();

  size_t size = file.length();
  if (size <= 0 || size % base::kSHA1Length != 0)
    return host_hashes.Pass();

  size_t hash_count = size / base::kSHA1Length;
  host_hashes->resize(hash_count);

  for (size_t i = 0; i < hash_count; i++) {
    memcpy((*host_hashes.get())[i].data,
           file.data() + i * base::kSHA1Length,
           base::kSHA1Length);
  }

  std::sort(host_hashes->begin(), host_hashes->end());

  return host_hashes.Pass();
}

} // namespace

SupervisedUserBlacklist::Hash::Hash(const std::string& host) {
  const unsigned char* host_bytes =
      reinterpret_cast<const unsigned char*>(host.c_str());
  base::SHA1HashBytes(host_bytes, host.length(), data);
}

bool SupervisedUserBlacklist::Hash::operator<(const Hash& rhs) const {
  return memcmp(data, rhs.data, base::kSHA1Length) < 0;
}

SupervisedUserBlacklist::SupervisedUserBlacklist() : weak_ptr_factory_(this) {}

SupervisedUserBlacklist::~SupervisedUserBlacklist() {}

bool SupervisedUserBlacklist::HasURL(const GURL& url) const {
  Hash hash(url.host());
  return std::binary_search(host_hashes_.begin(), host_hashes_.end(), hash);
}

size_t SupervisedUserBlacklist::GetEntryCount() const {
  return host_hashes_.size();
}

void SupervisedUserBlacklist::ReadFromFile(const base::FilePath& path,
                                           const base::Closure& done_callback) {
  base::PostTaskAndReplyWithResult(
      BrowserThread::GetBlockingPool(),
      FROM_HERE,
      base::Bind(&ReadFromBinaryFileOnFileThread, path),
      base::Bind(&SupervisedUserBlacklist::OnReadFromFileCompleted,
                 weak_ptr_factory_.GetWeakPtr(),
                 done_callback));
}

void SupervisedUserBlacklist::OnReadFromFileCompleted(
    const base::Closure& done_callback,
    scoped_ptr<std::vector<Hash> > host_hashes) {
  host_hashes_.swap(*host_hashes);
  LOG_IF(WARNING, host_hashes_.empty()) << "Got empty blacklist";

  if (!done_callback.is_null())
    done_callback.Run();
}