| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/safe_browsing/safe_browsing_database_impl.h" | 5 #include "chrome/browser/safe_browsing/safe_browsing_database_impl.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" |
| 7 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 8 #include "base/logging.h" | 9 #include "base/logging.h" |
| 9 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/platform_thread.h" |
| 10 #include "base/sha2.h" | 12 #include "base/sha2.h" |
| 11 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 12 #include "chrome/browser/safe_browsing/bloom_filter.h" | 14 #include "chrome/browser/safe_browsing/bloom_filter.h" |
| 13 #include "chrome/browser/safe_browsing/chunk_range.h" | 15 #include "chrome/browser/safe_browsing/chunk_range.h" |
| 14 #include "chrome/common/sqlite_compiled_statement.h" | 16 #include "chrome/common/sqlite_compiled_statement.h" |
| 15 #include "chrome/common/sqlite_utils.h" | 17 #include "chrome/common/sqlite_utils.h" |
| 16 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
| 17 | 19 |
| 18 // Database version. If this is different than what's stored on disk, the | 20 // Database version. If this is different than what's stored on disk, the |
| 19 // database is reset. | 21 // database is reset. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 49 // How long to wait after updating the database to write the bloom filter. | 51 // How long to wait after updating the database to write the bloom filter. |
| 50 static const int kBloomFilterWriteDelayMs = (60 * 1000); | 52 static const int kBloomFilterWriteDelayMs = (60 * 1000); |
| 51 | 53 |
| 52 // The maximum staleness for a cached entry. | 54 // The maximum staleness for a cached entry. |
| 53 static const int kMaxStalenessMinutes = 45; | 55 static const int kMaxStalenessMinutes = 45; |
| 54 | 56 |
| 55 // Implementation -------------------------------------------------------------- | 57 // Implementation -------------------------------------------------------------- |
| 56 | 58 |
| 57 SafeBrowsingDatabaseImpl::SafeBrowsingDatabaseImpl() | 59 SafeBrowsingDatabaseImpl::SafeBrowsingDatabaseImpl() |
| 58 : db_(NULL), | 60 : db_(NULL), |
| 61 transaction_count_(0), |
| 59 init_(false), | 62 init_(false), |
| 60 transaction_count_(0), | |
| 61 asynchronous_(true), | 63 asynchronous_(true), |
| 62 chunk_inserted_callback_(NULL), | 64 chunk_inserted_callback_(NULL), |
| 63 #pragma warning(suppress: 4355) // can use this | 65 ALLOW_THIS_IN_INITIALIZER_LIST(process_factory_(this)), |
| 64 bloom_read_factory_(this), | 66 ALLOW_THIS_IN_INITIALIZER_LIST(bloom_read_factory_(this)), |
| 65 #pragma warning(suppress: 4355) // can use this | 67 ALLOW_THIS_IN_INITIALIZER_LIST(bloom_write_factory_(this)), |
| 66 bloom_write_factory_(this), | 68 ALLOW_THIS_IN_INITIALIZER_LIST(reset_factory_(this)), |
| 67 #pragma warning(suppress: 4355) // can use this | 69 ALLOW_THIS_IN_INITIALIZER_LIST(resume_factory_(this)), |
| 68 process_factory_(this), | |
| 69 #pragma warning(suppress: 4355) // can use this | |
| 70 reset_factory_(this), | |
| 71 #pragma warning(suppress: 4355) // can use this | |
| 72 resume_factory_(this), | |
| 73 disk_delay_(kMaxThreadHoldupMs) { | 70 disk_delay_(kMaxThreadHoldupMs) { |
| 74 } | 71 } |
| 75 | 72 |
| 76 SafeBrowsingDatabaseImpl::~SafeBrowsingDatabaseImpl() { | 73 SafeBrowsingDatabaseImpl::~SafeBrowsingDatabaseImpl() { |
| 77 Close(); | 74 Close(); |
| 78 } | 75 } |
| 79 | 76 |
| 80 bool SafeBrowsingDatabaseImpl::Init(const std::wstring& filename, | 77 bool SafeBrowsingDatabaseImpl::Init(const std::wstring& filename, |
| 81 Callback0::Type* chunk_inserted_callback) { | 78 Callback0::Type* chunk_inserted_callback) { |
| 82 DCHECK(!init_ && filename_.empty()); | 79 DCHECK(!init_ && filename_.empty()); |
| (...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 466 | 463 |
| 467 if (asynchronous_) { | 464 if (asynchronous_) { |
| 468 // For production code, we want to throttle by calling InvokeLater to | 465 // For production code, we want to throttle by calling InvokeLater to |
| 469 // continue the work after a delay. However for unit tests we depend on | 466 // continue the work after a delay. However for unit tests we depend on |
| 470 // updates to happen synchronously. | 467 // updates to happen synchronously. |
| 471 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 468 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 472 process_factory_.NewRunnableMethod( | 469 process_factory_.NewRunnableMethod( |
| 473 &SafeBrowsingDatabaseImpl::RunThrottledWork), disk_delay_); | 470 &SafeBrowsingDatabaseImpl::RunThrottledWork), disk_delay_); |
| 474 break; | 471 break; |
| 475 } else { | 472 } else { |
| 476 Sleep(kMaxThreadHoldupMs); | 473 PlatformThread::Sleep(kMaxThreadHoldupMs); |
| 477 } | 474 } |
| 478 } | 475 } |
| 479 } | 476 } |
| 480 | 477 |
| 481 void SafeBrowsingDatabaseImpl::InsertChunks(const std::string& list_name, | 478 void SafeBrowsingDatabaseImpl::InsertChunks(const std::string& list_name, |
| 482 std::deque<SBChunk>* chunks) { | 479 std::deque<SBChunk>* chunks) { |
| 483 // We've going to be updating the bloom filter, so delete the on-disk | 480 // We've going to be updating the bloom filter, so delete the on-disk |
| 484 // serialization so that if the process crashes we'll generate a new one on | 481 // serialization so that if the process crashes we'll generate a new one on |
| 485 // startup, instead of reading a stale filter. | 482 // startup, instead of reading a stale filter. |
| 486 DeleteBloomFilter(); | 483 DeleteBloomFilter(); |
| (...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1007 break; | 1004 break; |
| 1008 } | 1005 } |
| 1009 | 1006 |
| 1010 count++; | 1007 count++; |
| 1011 bloom_filter_temp_hostkeys_.push_back(statement->column_int(0)); | 1008 bloom_filter_temp_hostkeys_.push_back(statement->column_int(0)); |
| 1012 next_id = statement->column_int(1) + 1; | 1009 next_id = statement->column_int(1) + 1; |
| 1013 if ((Time::Now() - before).InMilliseconds() > kMaxThreadHoldupMs) { | 1010 if ((Time::Now() - before).InMilliseconds() > kMaxThreadHoldupMs) { |
| 1014 if (asynchronous_) { | 1011 if (asynchronous_) { |
| 1015 break; | 1012 break; |
| 1016 } else { | 1013 } else { |
| 1017 Sleep(kMaxThreadHoldupMs); | 1014 PlatformThread::Sleep(kMaxThreadHoldupMs); |
| 1018 } | 1015 } |
| 1019 } | 1016 } |
| 1020 } | 1017 } |
| 1021 | 1018 |
| 1022 TimeDelta chunk_time = Time::Now() - before; | 1019 TimeDelta chunk_time = Time::Now() - before; |
| 1023 int time_ms = static_cast<int>(chunk_time.InMilliseconds()); | 1020 int time_ms = static_cast<int>(chunk_time.InMilliseconds()); |
| 1024 SB_DLOG(INFO) << "SafeBrowsingDatabaseImpl read " << count | 1021 SB_DLOG(INFO) << "SafeBrowsingDatabaseImpl read " << count |
| 1025 << " hostkeys in " << time_ms << " ms"; | 1022 << " hostkeys in " << time_ms << " ms"; |
| 1026 | 1023 |
| 1027 if (!count || !asynchronous_) { | 1024 if (!count || !asynchronous_) { |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1173 while (it != hash_cache_.end()) { | 1170 while (it != hash_cache_.end()) { |
| 1174 HashList& entries = it->second; | 1171 HashList& entries = it->second; |
| 1175 HashList::iterator eit = entries.begin(); | 1172 HashList::iterator eit = entries.begin(); |
| 1176 while (eit != entries.end()) { | 1173 while (eit != entries.end()) { |
| 1177 if (eit->list_id == list_id && eit->add_chunk_id == add_chunk_id) | 1174 if (eit->list_id == list_id && eit->add_chunk_id == add_chunk_id) |
| 1178 eit = entries.erase(eit); | 1175 eit = entries.erase(eit); |
| 1179 else | 1176 else |
| 1180 ++eit; | 1177 ++eit; |
| 1181 } | 1178 } |
| 1182 if (entries.empty()) | 1179 if (entries.empty()) |
| 1183 it = hash_cache_.erase(it); | 1180 hash_cache_.erase(it++); |
| 1184 else | 1181 else |
| 1185 ++it; | 1182 ++it; |
| 1186 } | 1183 } |
| 1187 } | 1184 } |
| 1188 | 1185 |
| 1189 void SafeBrowsingDatabaseImpl::HandleCorruptDatabase() { | 1186 void SafeBrowsingDatabaseImpl::HandleCorruptDatabase() { |
| 1190 MessageLoop::current()->PostTask(FROM_HERE, | 1187 MessageLoop::current()->PostTask(FROM_HERE, |
| 1191 reset_factory_.NewRunnableMethod( | 1188 reset_factory_.NewRunnableMethod( |
| 1192 &SafeBrowsingDatabaseImpl::OnHandleCorruptDatabase)); | 1189 &SafeBrowsingDatabaseImpl::OnHandleCorruptDatabase)); |
| 1193 } | 1190 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1206 kOnResumeHoldupMs); | 1203 kOnResumeHoldupMs); |
| 1207 } | 1204 } |
| 1208 | 1205 |
| 1209 void SafeBrowsingDatabaseImpl::OnResumeDone() { | 1206 void SafeBrowsingDatabaseImpl::OnResumeDone() { |
| 1210 disk_delay_ = kMaxThreadHoldupMs; | 1207 disk_delay_ = kMaxThreadHoldupMs; |
| 1211 } | 1208 } |
| 1212 | 1209 |
| 1213 void SafeBrowsingDatabaseImpl::SetSynchronous() { | 1210 void SafeBrowsingDatabaseImpl::SetSynchronous() { |
| 1214 asynchronous_ = false; | 1211 asynchronous_ = false; |
| 1215 } | 1212 } |
| OLD | NEW |