OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/supervised_user/experimental/supervised_user_blacklist.
h" | 5 #include "chrome/browser/supervised_user/experimental/supervised_user_blacklist.
h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cstring> | 8 #include <cstring> |
9 #include <fstream> | 9 #include <fstream> |
10 | 10 |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/files/memory_mapped_file.h" | 12 #include "base/files/memory_mapped_file.h" |
13 #include "base/threading/sequenced_worker_pool.h" | 13 #include "base/task_scheduler/post_task.h" |
14 #include "content/public/browser/browser_thread.h" | |
15 #include "url/gurl.h" | 14 #include "url/gurl.h" |
16 | 15 |
17 using content::BrowserThread; | |
18 | |
19 namespace { | 16 namespace { |
20 | 17 |
21 std::unique_ptr<std::vector<SupervisedUserBlacklist::Hash>> | 18 std::unique_ptr<std::vector<SupervisedUserBlacklist::Hash>> |
22 ReadFromBinaryFileOnFileThread(const base::FilePath& path) { | 19 ReadFromBinaryFileOnFileThread(const base::FilePath& path) { |
23 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
24 | |
25 std::unique_ptr<std::vector<SupervisedUserBlacklist::Hash>> host_hashes( | 20 std::unique_ptr<std::vector<SupervisedUserBlacklist::Hash>> host_hashes( |
26 new std::vector<SupervisedUserBlacklist::Hash>); | 21 new std::vector<SupervisedUserBlacklist::Hash>); |
27 | 22 |
28 base::MemoryMappedFile file; | 23 base::MemoryMappedFile file; |
29 file.Initialize(path); | 24 file.Initialize(path); |
30 if (!file.IsValid()) | 25 if (!file.IsValid()) |
31 return host_hashes; | 26 return host_hashes; |
32 | 27 |
33 size_t size = file.length(); | 28 size_t size = file.length(); |
34 if (size <= 0 || size % base::kSHA1Length != 0) | 29 if (size <= 0 || size % base::kSHA1Length != 0) |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 Hash hash(url.host()); | 63 Hash hash(url.host()); |
69 return std::binary_search(host_hashes_.begin(), host_hashes_.end(), hash); | 64 return std::binary_search(host_hashes_.begin(), host_hashes_.end(), hash); |
70 } | 65 } |
71 | 66 |
72 size_t SupervisedUserBlacklist::GetEntryCount() const { | 67 size_t SupervisedUserBlacklist::GetEntryCount() const { |
73 return host_hashes_.size(); | 68 return host_hashes_.size(); |
74 } | 69 } |
75 | 70 |
76 void SupervisedUserBlacklist::ReadFromFile(const base::FilePath& path, | 71 void SupervisedUserBlacklist::ReadFromFile(const base::FilePath& path, |
77 const base::Closure& done_callback) { | 72 const base::Closure& done_callback) { |
78 base::PostTaskAndReplyWithResult( | 73 base::PostTaskWithTraitsAndReplyWithResult( |
79 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( | |
80 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN).get(), | |
81 FROM_HERE, | 74 FROM_HERE, |
82 base::Bind(&ReadFromBinaryFileOnFileThread, path), | 75 base::TaskTraits() |
83 base::Bind(&SupervisedUserBlacklist::OnReadFromFileCompleted, | 76 .MayBlock() |
84 weak_ptr_factory_.GetWeakPtr(), | 77 .WithPriority(base::TaskPriority::BACKGROUND) |
85 done_callback)); | 78 .WithShutdownBehavior( |
| 79 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN), |
| 80 base::BindOnce(&ReadFromBinaryFileOnFileThread, path), |
| 81 base::BindOnce(&SupervisedUserBlacklist::OnReadFromFileCompleted, |
| 82 weak_ptr_factory_.GetWeakPtr(), done_callback)); |
86 } | 83 } |
87 | 84 |
88 void SupervisedUserBlacklist::OnReadFromFileCompleted( | 85 void SupervisedUserBlacklist::OnReadFromFileCompleted( |
89 const base::Closure& done_callback, | 86 const base::Closure& done_callback, |
90 std::unique_ptr<std::vector<Hash>> host_hashes) { | 87 std::unique_ptr<std::vector<Hash>> host_hashes) { |
91 host_hashes_.swap(*host_hashes); | 88 host_hashes_.swap(*host_hashes); |
92 LOG_IF(WARNING, host_hashes_.empty()) << "Got empty blacklist"; | 89 LOG_IF(WARNING, host_hashes_.empty()) << "Got empty blacklist"; |
93 | 90 |
94 if (!done_callback.is_null()) | 91 if (!done_callback.is_null()) |
95 done_callback.Run(); | 92 done_callback.Run(); |
96 } | 93 } |
OLD | NEW |