Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / supervised_user / experimental / supervised_user_blacklist.cc
blob14843ff68abe0df02bd3957420bb080b01773f1b
1 // Copyright 2014 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.
5 #include "chrome/browser/supervised_user/experimental/supervised_user_blacklist.h"
7 #include <algorithm>
8 #include <cstring>
9 #include <fstream>
11 #include "base/files/file_path.h"
12 #include "base/files/memory_mapped_file.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "url/gurl.h"
16 using content::BrowserThread;
18 namespace {
20 scoped_ptr<std::vector<SupervisedUserBlacklist::Hash> >
21 ReadFromBinaryFileOnFileThread(const base::FilePath& path) {
22 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
24 scoped_ptr<std::vector<SupervisedUserBlacklist::Hash> > host_hashes(
25 new std::vector<SupervisedUserBlacklist::Hash>);
27 base::MemoryMappedFile file;
28 file.Initialize(path);
29 if (!file.IsValid())
30 return host_hashes.Pass();
32 size_t size = file.length();
33 if (size <= 0 || size % base::kSHA1Length != 0)
34 return host_hashes.Pass();
36 size_t hash_count = size / base::kSHA1Length;
37 host_hashes->resize(hash_count);
39 for (size_t i = 0; i < hash_count; i++) {
40 memcpy((*host_hashes.get())[i].data,
41 file.data() + i * base::kSHA1Length,
42 base::kSHA1Length);
45 std::sort(host_hashes->begin(), host_hashes->end());
47 return host_hashes.Pass();
50 } // namespace
52 SupervisedUserBlacklist::Hash::Hash(const std::string& host) {
53 const unsigned char* host_bytes =
54 reinterpret_cast<const unsigned char*>(host.c_str());
55 base::SHA1HashBytes(host_bytes, host.length(), data);
58 bool SupervisedUserBlacklist::Hash::operator<(const Hash& rhs) const {
59 return memcmp(data, rhs.data, base::kSHA1Length) < 0;
62 SupervisedUserBlacklist::SupervisedUserBlacklist() : weak_ptr_factory_(this) {}
64 SupervisedUserBlacklist::~SupervisedUserBlacklist() {}
66 bool SupervisedUserBlacklist::HasURL(const GURL& url) const {
67 Hash hash(url.host());
68 return std::binary_search(host_hashes_.begin(), host_hashes_.end(), hash);
71 size_t SupervisedUserBlacklist::GetEntryCount() const {
72 return host_hashes_.size();
75 void SupervisedUserBlacklist::ReadFromFile(const base::FilePath& path,
76 const base::Closure& done_callback) {
77 base::PostTaskAndReplyWithResult(
78 BrowserThread::GetBlockingPool(),
79 FROM_HERE,
80 base::Bind(&ReadFromBinaryFileOnFileThread, path),
81 base::Bind(&SupervisedUserBlacklist::OnReadFromFileCompleted,
82 weak_ptr_factory_.GetWeakPtr(),
83 done_callback));
86 void SupervisedUserBlacklist::OnReadFromFileCompleted(
87 const base::Closure& done_callback,
88 scoped_ptr<std::vector<Hash> > host_hashes) {
89 host_hashes_.swap(*host_hashes);
90 LOG_IF(WARNING, host_hashes_.empty()) << "Got empty blacklist";
92 if (!done_callback.is_null())
93 done_callback.Run();