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/task_scheduler/post_task.h" | |
13 #include "base/threading/sequenced_worker_pool.h" | 14 #include "base/threading/sequenced_worker_pool.h" |
14 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
15 #include "url/gurl.h" | 16 #include "url/gurl.h" |
16 | 17 |
17 using content::BrowserThread; | 18 using content::BrowserThread; |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 std::unique_ptr<std::vector<SupervisedUserBlacklist::Hash>> | 22 std::unique_ptr<std::vector<SupervisedUserBlacklist::Hash>> |
22 ReadFromBinaryFileOnFileThread(const base::FilePath& path) { | 23 ReadFromBinaryFileOnFileThread(const base::FilePath& path) { |
23 DCHECK(BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
24 | |
25 std::unique_ptr<std::vector<SupervisedUserBlacklist::Hash>> host_hashes( | 24 std::unique_ptr<std::vector<SupervisedUserBlacklist::Hash>> host_hashes( |
26 new std::vector<SupervisedUserBlacklist::Hash>); | 25 new std::vector<SupervisedUserBlacklist::Hash>); |
27 | 26 |
28 base::MemoryMappedFile file; | 27 base::MemoryMappedFile file; |
29 file.Initialize(path); | 28 file.Initialize(path); |
30 if (!file.IsValid()) | 29 if (!file.IsValid()) |
31 return host_hashes; | 30 return host_hashes; |
32 | 31 |
33 size_t size = file.length(); | 32 size_t size = file.length(); |
34 if (size <= 0 || size % base::kSHA1Length != 0) | 33 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()); | 67 Hash hash(url.host()); |
69 return std::binary_search(host_hashes_.begin(), host_hashes_.end(), hash); | 68 return std::binary_search(host_hashes_.begin(), host_hashes_.end(), hash); |
70 } | 69 } |
71 | 70 |
72 size_t SupervisedUserBlacklist::GetEntryCount() const { | 71 size_t SupervisedUserBlacklist::GetEntryCount() const { |
73 return host_hashes_.size(); | 72 return host_hashes_.size(); |
74 } | 73 } |
75 | 74 |
76 void SupervisedUserBlacklist::ReadFromFile(const base::FilePath& path, | 75 void SupervisedUserBlacklist::ReadFromFile(const base::FilePath& path, |
77 const base::Closure& done_callback) { | 76 const base::Closure& done_callback) { |
78 base::PostTaskAndReplyWithResult( | 77 base::PostTaskAndReplyWithResult( |
Bernhard Bauer
2017/04/27 09:13:38
Sorry, I thought I had replied here as well. Can y
| |
79 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( | 78 base::CreateTaskRunnerWithTraits( |
80 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN).get(), | 79 base::TaskTraits() |
81 FROM_HERE, | 80 .MayBlock() |
82 base::Bind(&ReadFromBinaryFileOnFileThread, path), | 81 .WithPriority(base::TaskPriority::BACKGROUND) |
82 .WithShutdownBehavior( | |
83 base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN)) | |
84 .get(), | |
85 FROM_HERE, base::Bind(&ReadFromBinaryFileOnFileThread, path), | |
83 base::Bind(&SupervisedUserBlacklist::OnReadFromFileCompleted, | 86 base::Bind(&SupervisedUserBlacklist::OnReadFromFileCompleted, |
84 weak_ptr_factory_.GetWeakPtr(), | 87 weak_ptr_factory_.GetWeakPtr(), done_callback)); |
85 done_callback)); | |
86 } | 88 } |
87 | 89 |
88 void SupervisedUserBlacklist::OnReadFromFileCompleted( | 90 void SupervisedUserBlacklist::OnReadFromFileCompleted( |
89 const base::Closure& done_callback, | 91 const base::Closure& done_callback, |
90 std::unique_ptr<std::vector<Hash>> host_hashes) { | 92 std::unique_ptr<std::vector<Hash>> host_hashes) { |
91 host_hashes_.swap(*host_hashes); | 93 host_hashes_.swap(*host_hashes); |
92 LOG_IF(WARNING, host_hashes_.empty()) << "Got empty blacklist"; | 94 LOG_IF(WARNING, host_hashes_.empty()) << "Got empty blacklist"; |
93 | 95 |
94 if (!done_callback.is_null()) | 96 if (!done_callback.is_null()) |
95 done_callback.Run(); | 97 done_callback.Run(); |
96 } | 98 } |
OLD | NEW |