| 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/visitedlink_master.h" | 5 #include "chrome/browser/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 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 // The thread the visited link master is on where we will notify it. | 192 // The thread the visited link master is on where we will notify it. |
| 193 MessageLoop* main_message_loop_; | 193 MessageLoop* main_message_loop_; |
| 194 | 194 |
| 195 // Indicates whether the operation has failed or not. | 195 // Indicates whether the operation has failed or not. |
| 196 bool success_; | 196 bool success_; |
| 197 | 197 |
| 198 // Salt for this new table. | 198 // Salt for this new table. |
| 199 uint8 salt_[LINK_SALT_LENGTH]; | 199 uint8 salt_[LINK_SALT_LENGTH]; |
| 200 | 200 |
| 201 // Stores the fingerprints we computed on the background thread. | 201 // Stores the fingerprints we computed on the background thread. |
| 202 std::vector<VisitedLinkMaster::Fingerprint> fingerprints_; | 202 VisitedLinkCommon::Fingerprints fingerprints_; |
| 203 }; | 203 }; |
| 204 | 204 |
| 205 // VisitedLinkMaster ---------------------------------------------------------- | 205 // VisitedLinkMaster ---------------------------------------------------------- |
| 206 | 206 |
| 207 VisitedLinkMaster::VisitedLinkMaster(base::Thread* file_thread, | 207 VisitedLinkMaster::VisitedLinkMaster(base::Thread* file_thread, |
| 208 PostNewTableEvent* poster, | 208 Listener* listener, |
| 209 Profile* profile) { | 209 Profile* profile) { |
| 210 InitMembers(file_thread, poster, profile); | 210 InitMembers(file_thread, listener, profile); |
| 211 } | 211 } |
| 212 | 212 |
| 213 VisitedLinkMaster::VisitedLinkMaster(base::Thread* file_thread, | 213 VisitedLinkMaster::VisitedLinkMaster(base::Thread* file_thread, |
| 214 PostNewTableEvent* poster, | 214 Listener* listener, |
| 215 HistoryService* history_service, | 215 HistoryService* history_service, |
| 216 bool suppress_rebuild, | 216 bool suppress_rebuild, |
| 217 const FilePath& filename, | 217 const FilePath& filename, |
| 218 int32 default_table_size) { | 218 int32 default_table_size) { |
| 219 InitMembers(file_thread, poster, NULL); | 219 InitMembers(file_thread, listener, NULL); |
| 220 | 220 |
| 221 database_name_override_ = filename; | 221 database_name_override_ = filename; |
| 222 table_size_override_ = default_table_size; | 222 table_size_override_ = default_table_size; |
| 223 history_service_override_ = history_service; | 223 history_service_override_ = history_service; |
| 224 suppress_rebuild_ = suppress_rebuild; | 224 suppress_rebuild_ = suppress_rebuild; |
| 225 } | 225 } |
| 226 | 226 |
| 227 VisitedLinkMaster::~VisitedLinkMaster() { | 227 VisitedLinkMaster::~VisitedLinkMaster() { |
| 228 if (table_builder_.get()) { | 228 if (table_builder_.get()) { |
| 229 // Prevent the table builder from calling us back now that we're being | 229 // Prevent the table builder from calling us back now that we're being |
| 230 // destroyed. Note that we DON'T delete the object, since the history | 230 // destroyed. Note that we DON'T delete the object, since the history |
| 231 // system is still writing into it. When that is complete, the table | 231 // system is still writing into it. When that is complete, the table |
| 232 // builder will destroy itself when it finds we are gone. | 232 // builder will destroy itself when it finds we are gone. |
| 233 table_builder_->DisownMaster(); | 233 table_builder_->DisownMaster(); |
| 234 } | 234 } |
| 235 FreeURLTable(); | 235 FreeURLTable(); |
| 236 } | 236 } |
| 237 | 237 |
| 238 void VisitedLinkMaster::InitMembers(base::Thread* file_thread, | 238 void VisitedLinkMaster::InitMembers(base::Thread* file_thread, |
| 239 PostNewTableEvent* poster, | 239 Listener* listener, |
| 240 Profile* profile) { | 240 Profile* profile) { |
| 241 DCHECK(listener); |
| 242 |
| 241 if (file_thread) | 243 if (file_thread) |
| 242 file_thread_ = file_thread->message_loop(); | 244 file_thread_ = file_thread->message_loop(); |
| 243 else | 245 else |
| 244 file_thread_ = NULL; | 246 file_thread_ = NULL; |
| 245 | 247 |
| 246 post_new_table_event_ = poster; | 248 listener_ = listener; |
| 247 file_ = NULL; | 249 file_ = NULL; |
| 248 shared_memory_ = NULL; | 250 shared_memory_ = NULL; |
| 249 shared_memory_serial_ = 0; | 251 shared_memory_serial_ = 0; |
| 250 used_items_ = 0; | 252 used_items_ = 0; |
| 251 table_size_override_ = 0; | 253 table_size_override_ = 0; |
| 252 history_service_override_ = NULL; | 254 history_service_override_ = NULL; |
| 253 suppress_rebuild_ = false; | 255 suppress_rebuild_ = false; |
| 254 profile_ = profile; | 256 profile_ = profile; |
| 255 | 257 |
| 256 #ifndef NDEBUG | 258 #ifndef NDEBUG |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 deleted_since_rebuild_.clear(); | 343 deleted_since_rebuild_.clear(); |
| 342 | 344 |
| 343 // Clear the hash table. | 345 // Clear the hash table. |
| 344 used_items_ = 0; | 346 used_items_ = 0; |
| 345 memset(hash_table_, 0, this->table_length_ * sizeof(Fingerprint)); | 347 memset(hash_table_, 0, this->table_length_ * sizeof(Fingerprint)); |
| 346 | 348 |
| 347 // Resize it if it is now too empty. Resize may write the new table out for | 349 // Resize it if it is now too empty. Resize may write the new table out for |
| 348 // us, otherwise, schedule writing the new table to disk ourselves. | 350 // us, otherwise, schedule writing the new table to disk ourselves. |
| 349 if (!ResizeTableIfNecessary()) | 351 if (!ResizeTableIfNecessary()) |
| 350 WriteFullTable(); | 352 WriteFullTable(); |
| 353 |
| 354 listener_->Reset(); |
| 351 } | 355 } |
| 352 | 356 |
| 353 void VisitedLinkMaster::DeleteURLs(const std::set<GURL>& urls) { | 357 void VisitedLinkMaster::DeleteURLs(const std::set<GURL>& urls) { |
| 354 typedef std::set<GURL>::const_iterator SetIterator; | 358 typedef std::set<GURL>::const_iterator SetIterator; |
| 355 | 359 |
| 356 if (urls.empty()) | 360 if (urls.empty()) |
| 357 return; | 361 return; |
| 358 | 362 |
| 363 listener_->Reset(); |
| 364 |
| 359 if (table_builder_) { | 365 if (table_builder_) { |
| 360 // A rebuild is in progress, save this deletion in the temporary list so | 366 // A rebuild is in progress, save this deletion in the temporary list so |
| 361 // it can be added once rebuild is complete. | 367 // it can be added once rebuild is complete. |
| 362 for (SetIterator i = urls.begin(); i != urls.end(); ++i) { | 368 for (SetIterator i = urls.begin(); i != urls.end(); ++i) { |
| 363 if (!i->is_valid()) | 369 if (!i->is_valid()) |
| 364 continue; | 370 continue; |
| 365 | 371 |
| 366 Fingerprint fingerprint = | 372 Fingerprint fingerprint = |
| 367 ComputeURLFingerprint(i->spec().data(), i->spec().size(), salt_); | 373 ComputeURLFingerprint(i->spec().data(), i->spec().size(), salt_); |
| 368 deleted_since_rebuild_.insert(fingerprint); | 374 deleted_since_rebuild_.insert(fingerprint); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 Hash first_hash = cur_hash; | 410 Hash first_hash = cur_hash; |
| 405 while (true) { | 411 while (true) { |
| 406 Fingerprint cur_fingerprint = FingerprintAt(cur_hash); | 412 Fingerprint cur_fingerprint = FingerprintAt(cur_hash); |
| 407 if (cur_fingerprint == fingerprint) | 413 if (cur_fingerprint == fingerprint) |
| 408 return null_hash_; // This fingerprint is already in there, do nothing. | 414 return null_hash_; // This fingerprint is already in there, do nothing. |
| 409 | 415 |
| 410 if (cur_fingerprint == null_fingerprint_) { | 416 if (cur_fingerprint == null_fingerprint_) { |
| 411 // End of probe sequence found, insert here. | 417 // End of probe sequence found, insert here. |
| 412 hash_table_[cur_hash] = fingerprint; | 418 hash_table_[cur_hash] = fingerprint; |
| 413 used_items_++; | 419 used_items_++; |
| 420 // Notify listener that a new visited link was added. |
| 421 listener_->Add(fingerprint); |
| 414 return cur_hash; | 422 return cur_hash; |
| 415 } | 423 } |
| 416 | 424 |
| 417 // Advance in the probe sequence. | 425 // Advance in the probe sequence. |
| 418 cur_hash = IncrementHash(cur_hash); | 426 cur_hash = IncrementHash(cur_hash); |
| 419 if (cur_hash == first_hash) { | 427 if (cur_hash == first_hash) { |
| 420 // This means that we've wrapped around and are about to go into an | 428 // This means that we've wrapped around and are about to go into an |
| 421 // infinite loop. Something was wrong with the hashtable resizing | 429 // infinite loop. Something was wrong with the hashtable resizing |
| 422 // logic, so stop here. | 430 // logic, so stop here. |
| 423 NOTREACHED(); | 431 NOTREACHED(); |
| (...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 798 if (cur) | 806 if (cur) |
| 799 AddFingerprint(cur); | 807 AddFingerprint(cur); |
| 800 } | 808 } |
| 801 | 809 |
| 802 // On error unmapping, just forget about it since we can't do anything | 810 // On error unmapping, just forget about it since we can't do anything |
| 803 // else to release it. | 811 // else to release it. |
| 804 delete old_shared_memory; | 812 delete old_shared_memory; |
| 805 | 813 |
| 806 // Send an update notification to all child processes so they read the new | 814 // Send an update notification to all child processes so they read the new |
| 807 // table. | 815 // table. |
| 808 post_new_table_event_(shared_memory_); | 816 listener_->NewTable(shared_memory_); |
| 809 | 817 |
| 810 #ifndef NDEBUG | 818 #ifndef NDEBUG |
| 811 DebugValidate(); | 819 DebugValidate(); |
| 812 #endif | 820 #endif |
| 813 | 821 |
| 814 // The new table needs to be written to disk. | 822 // The new table needs to be written to disk. |
| 815 WriteFullTable(); | 823 WriteFullTable(); |
| 816 } | 824 } |
| 817 | 825 |
| 818 uint32 VisitedLinkMaster::NewTableSizeForCount(int32 item_count) const { | 826 uint32 VisitedLinkMaster::NewTableSizeForCount(int32 item_count) const { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 901 for (std::set<Fingerprint>::iterator i = added_since_rebuild_.begin(); | 909 for (std::set<Fingerprint>::iterator i = added_since_rebuild_.begin(); |
| 902 i != added_since_rebuild_.end(); ++i) | 910 i != added_since_rebuild_.end(); ++i) |
| 903 AddFingerprint(*i); | 911 AddFingerprint(*i); |
| 904 added_since_rebuild_.clear(); | 912 added_since_rebuild_.clear(); |
| 905 | 913 |
| 906 // Now handle deletions. | 914 // Now handle deletions. |
| 907 DeleteFingerprintsFromCurrentTable(deleted_since_rebuild_); | 915 DeleteFingerprintsFromCurrentTable(deleted_since_rebuild_); |
| 908 deleted_since_rebuild_.clear(); | 916 deleted_since_rebuild_.clear(); |
| 909 | 917 |
| 910 // Send an update notification to all child processes. | 918 // Send an update notification to all child processes. |
| 911 post_new_table_event_(shared_memory_); | 919 listener_->NewTable(shared_memory_); |
| 912 | 920 |
| 913 WriteFullTable(); | 921 WriteFullTable(); |
| 914 } | 922 } |
| 915 } | 923 } |
| 916 table_builder_ = NULL; // Will release our reference to the builder. | 924 table_builder_ = NULL; // Will release our reference to the builder. |
| 917 | 925 |
| 918 // Notify the unit test that the rebuild is complete (will be NULL in prod.) | 926 // Notify the unit test that the rebuild is complete (will be NULL in prod.) |
| 919 if (rebuild_complete_task_.get()) { | 927 if (rebuild_complete_task_.get()) { |
| 920 rebuild_complete_task_->Run(); | 928 rebuild_complete_task_->Run(); |
| 921 rebuild_complete_task_.reset(NULL); | 929 rebuild_complete_task_.reset(NULL); |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1019 } | 1027 } |
| 1020 | 1028 |
| 1021 void VisitedLinkMaster::TableBuilder::OnCompleteMainThread() { | 1029 void VisitedLinkMaster::TableBuilder::OnCompleteMainThread() { |
| 1022 if (master_) | 1030 if (master_) |
| 1023 master_->OnTableRebuildComplete(success_, fingerprints_); | 1031 master_->OnTableRebuildComplete(success_, fingerprints_); |
| 1024 | 1032 |
| 1025 // WILL (generally) DELETE THIS! This balances the AddRef in | 1033 // WILL (generally) DELETE THIS! This balances the AddRef in |
| 1026 // VisitedLinkMaster::RebuildTableFromHistory. | 1034 // VisitedLinkMaster::RebuildTableFromHistory. |
| 1027 Release(); | 1035 Release(); |
| 1028 } | 1036 } |
| OLD | NEW |