OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/visitedlink/visitedlink_master.h" | 5 #include "chrome/browser/visitedlink/visitedlink_master.h" |
6 | 6 |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <windows.h> | 8 #include <windows.h> |
9 #include <io.h> | 9 #include <io.h> |
10 #include <shlobj.h> | 10 #include <shlobj.h> |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 // If the table is "full", we don't add URLs and just drop them on the floor. | 235 // If the table is "full", we don't add URLs and just drop them on the floor. |
236 // This can happen if we get thousands of new URLs and something causes | 236 // This can happen if we get thousands of new URLs and something causes |
237 // the table resizing to fail. This check prevents a hang in that case. Note | 237 // the table resizing to fail. This check prevents a hang in that case. Note |
238 // that this is *not* the resize limit, this is just a sanity check. | 238 // that this is *not* the resize limit, this is just a sanity check. |
239 if (used_items_ / 8 > table_length_ / 10) | 239 if (used_items_ / 8 > table_length_ / 10) |
240 return null_hash_; // Table is more than 80% full. | 240 return null_hash_; // Table is more than 80% full. |
241 | 241 |
242 return AddFingerprint(fingerprint, true); | 242 return AddFingerprint(fingerprint, true); |
243 } | 243 } |
244 | 244 |
| 245 void VisitedLinkMaster::PostIOTask(const tracked_objects::Location& from_here, |
| 246 const base::Closure& task) { |
| 247 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 248 if (sequence_token_.is_null()) |
| 249 sequence_token_ = pool->GetSequenceToken(); |
| 250 pool->PostSequencedWorkerTask(sequence_token_, from_here, task); |
| 251 } |
| 252 |
245 void VisitedLinkMaster::AddURL(const GURL& url) { | 253 void VisitedLinkMaster::AddURL(const GURL& url) { |
246 Hash index = TryToAddURL(url); | 254 Hash index = TryToAddURL(url); |
247 if (!table_builder_ && index != null_hash_) { | 255 if (!table_builder_ && index != null_hash_) { |
248 // Not rebuilding, so we want to keep the file on disk up-to-date. | 256 // Not rebuilding, so we want to keep the file on disk up-to-date. |
249 WriteUsedItemCountToFile(); | 257 WriteUsedItemCountToFile(); |
250 WriteHashRangeToFile(index, index); | 258 WriteHashRangeToFile(index, index); |
251 ResizeTableIfNecessary(); | 259 ResizeTableIfNecessary(); |
252 } | 260 } |
253 } | 261 } |
254 | 262 |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 header[2] = table_length_; | 481 header[2] = table_length_; |
474 header[3] = used_items_; | 482 header[3] = used_items_; |
475 WriteToFile(file_, 0, header, sizeof(header)); | 483 WriteToFile(file_, 0, header, sizeof(header)); |
476 WriteToFile(file_, sizeof(header), salt_, LINK_SALT_LENGTH); | 484 WriteToFile(file_, sizeof(header), salt_, LINK_SALT_LENGTH); |
477 | 485 |
478 // Write the hash data. | 486 // Write the hash data. |
479 WriteToFile(file_, kFileHeaderSize, | 487 WriteToFile(file_, kFileHeaderSize, |
480 hash_table_, table_length_ * sizeof(Fingerprint)); | 488 hash_table_, table_length_ * sizeof(Fingerprint)); |
481 | 489 |
482 // The hash table may have shrunk, so make sure this is the end. | 490 // The hash table may have shrunk, so make sure this is the end. |
483 BrowserThread::PostTask( | 491 PostIOTask(FROM_HERE, base::Bind(base::IgnoreResult(&TruncateFile), file_)); |
484 BrowserThread::FILE, | |
485 FROM_HERE, | |
486 base::Bind(base::IgnoreResult(&TruncateFile), file_)); | |
487 return true; | 492 return true; |
488 } | 493 } |
489 | 494 |
490 bool VisitedLinkMaster::InitFromFile() { | 495 bool VisitedLinkMaster::InitFromFile() { |
491 DCHECK(file_ == NULL); | 496 DCHECK(file_ == NULL); |
492 | 497 |
493 FilePath filename; | 498 FilePath filename; |
494 GetDatabaseFileName(&filename); | 499 GetDatabaseFileName(&filename); |
495 ScopedFILE file_closer(OpenFile(filename, "rb+")); | 500 ScopedFILE file_closer(OpenFile(filename, "rb+")); |
496 if (!file_closer.get()) | 501 if (!file_closer.get()) |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
666 return true; | 671 return true; |
667 } | 672 } |
668 | 673 |
669 void VisitedLinkMaster::FreeURLTable() { | 674 void VisitedLinkMaster::FreeURLTable() { |
670 if (shared_memory_) { | 675 if (shared_memory_) { |
671 delete shared_memory_; | 676 delete shared_memory_; |
672 shared_memory_ = NULL; | 677 shared_memory_ = NULL; |
673 } | 678 } |
674 if (!file_) | 679 if (!file_) |
675 return; | 680 return; |
676 | 681 PostIOTask(FROM_HERE, base::Bind(base::IgnoreResult(&fclose), file_)); |
677 BrowserThread::PostTask( | |
678 BrowserThread::FILE, | |
679 FROM_HERE, | |
680 base::Bind(base::IgnoreResult(&fclose), file_)); | |
681 } | 682 } |
682 | 683 |
683 bool VisitedLinkMaster::ResizeTableIfNecessary() { | 684 bool VisitedLinkMaster::ResizeTableIfNecessary() { |
684 DCHECK(table_length_ > 0) << "Must have a table"; | 685 DCHECK(table_length_ > 0) << "Must have a table"; |
685 | 686 |
686 // Load limits for good performance/space. We are pretty conservative about | 687 // Load limits for good performance/space. We are pretty conservative about |
687 // keeping the table not very full. This is because we use linear probing | 688 // keeping the table not very full. This is because we use linear probing |
688 // which increases the likelihood of clumps of entries which will reduce | 689 // which increases the likelihood of clumps of entries which will reduce |
689 // performance. | 690 // performance. |
690 const float max_table_load = 0.5f; // Grow when we're > this full. | 691 const float max_table_load = 0.5f; // Grow when we're > this full. |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
853 } | 854 } |
854 } | 855 } |
855 | 856 |
856 void VisitedLinkMaster::WriteToFile(FILE* file, | 857 void VisitedLinkMaster::WriteToFile(FILE* file, |
857 off_t offset, | 858 off_t offset, |
858 void* data, | 859 void* data, |
859 int32 data_size) { | 860 int32 data_size) { |
860 #ifndef NDEBUG | 861 #ifndef NDEBUG |
861 posted_asynchronous_operation_ = true; | 862 posted_asynchronous_operation_ = true; |
862 #endif | 863 #endif |
863 | 864 PostIOTask(FROM_HERE, |
864 BrowserThread::PostTask( | |
865 BrowserThread::FILE, FROM_HERE, | |
866 base::Bind(&AsyncWrite, file, offset, | 865 base::Bind(&AsyncWrite, file, offset, |
867 std::string(static_cast<const char*>(data), data_size))); | 866 std::string(static_cast<const char*>(data), data_size))); |
868 } | 867 } |
869 | 868 |
870 void VisitedLinkMaster::WriteUsedItemCountToFile() { | 869 void VisitedLinkMaster::WriteUsedItemCountToFile() { |
871 if (!file_) | 870 if (!file_) |
872 return; // See comment on the file_ variable for why this might happen. | 871 return; // See comment on the file_ variable for why this might happen. |
873 WriteToFile(file_, kFileHeaderUsedOffset, &used_items_, sizeof(used_items_)); | 872 WriteToFile(file_, kFileHeaderUsedOffset, &used_items_, sizeof(used_items_)); |
874 } | 873 } |
875 | 874 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
946 } | 945 } |
947 | 946 |
948 void VisitedLinkMaster::TableBuilder::OnCompleteMainThread() { | 947 void VisitedLinkMaster::TableBuilder::OnCompleteMainThread() { |
949 if (master_) | 948 if (master_) |
950 master_->OnTableRebuildComplete(success_, fingerprints_); | 949 master_->OnTableRebuildComplete(success_, fingerprints_); |
951 | 950 |
952 // WILL (generally) DELETE THIS! This balances the AddRef in | 951 // WILL (generally) DELETE THIS! This balances the AddRef in |
953 // VisitedLinkMaster::RebuildTableFromHistory. | 952 // VisitedLinkMaster::RebuildTableFromHistory. |
954 Release(); | 953 Release(); |
955 } | 954 } |
OLD | NEW |