OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "components/visitedlink/browser/visitedlink_master.h" | 5 #include "components/visitedlink/browser/visitedlink_master.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 #include <string.h> | 8 #include <string.h> |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <utility> | 10 #include <utility> |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 if (*file) | 116 if (*file) |
117 base::IgnoreResult(fclose(*file)); | 117 base::IgnoreResult(fclose(*file)); |
118 free(file); | 118 free(file); |
119 } | 119 } |
120 | 120 |
121 } // namespace | 121 } // namespace |
122 | 122 |
123 struct VisitedLinkMaster::LoadFromFileResult | 123 struct VisitedLinkMaster::LoadFromFileResult |
124 : public base::RefCountedThreadSafe<LoadFromFileResult> { | 124 : public base::RefCountedThreadSafe<LoadFromFileResult> { |
125 LoadFromFileResult(base::ScopedFILE file, | 125 LoadFromFileResult(base::ScopedFILE file, |
126 scoped_ptr<base::SharedMemory> shared_memory, | 126 std::unique_ptr<base::SharedMemory> shared_memory, |
127 Fingerprint* hash_table, | 127 Fingerprint* hash_table, |
128 int32_t num_entries, | 128 int32_t num_entries, |
129 int32_t used_count, | 129 int32_t used_count, |
130 uint8_t salt[LINK_SALT_LENGTH]); | 130 uint8_t salt[LINK_SALT_LENGTH]); |
131 | 131 |
132 base::ScopedFILE file; | 132 base::ScopedFILE file; |
133 scoped_ptr<base::SharedMemory> shared_memory; | 133 std::unique_ptr<base::SharedMemory> shared_memory; |
134 Fingerprint* hash_table; | 134 Fingerprint* hash_table; |
135 int32_t num_entries; | 135 int32_t num_entries; |
136 int32_t used_count; | 136 int32_t used_count; |
137 uint8_t salt[LINK_SALT_LENGTH]; | 137 uint8_t salt[LINK_SALT_LENGTH]; |
138 | 138 |
139 private: | 139 private: |
140 friend class base::RefCountedThreadSafe<LoadFromFileResult>; | 140 friend class base::RefCountedThreadSafe<LoadFromFileResult>; |
141 virtual ~LoadFromFileResult(); | 141 virtual ~LoadFromFileResult(); |
142 | 142 |
143 DISALLOW_COPY_AND_ASSIGN(LoadFromFileResult); | 143 DISALLOW_COPY_AND_ASSIGN(LoadFromFileResult); |
144 }; | 144 }; |
145 | 145 |
146 VisitedLinkMaster::LoadFromFileResult::LoadFromFileResult( | 146 VisitedLinkMaster::LoadFromFileResult::LoadFromFileResult( |
147 base::ScopedFILE file, | 147 base::ScopedFILE file, |
148 scoped_ptr<base::SharedMemory> shared_memory, | 148 std::unique_ptr<base::SharedMemory> shared_memory, |
149 Fingerprint* hash_table, | 149 Fingerprint* hash_table, |
150 int32_t num_entries, | 150 int32_t num_entries, |
151 int32_t used_count, | 151 int32_t used_count, |
152 uint8_t salt[LINK_SALT_LENGTH]) | 152 uint8_t salt[LINK_SALT_LENGTH]) |
153 : file(std::move(file)), | 153 : file(std::move(file)), |
154 shared_memory(std::move(shared_memory)), | 154 shared_memory(std::move(shared_memory)), |
155 hash_table(hash_table), | 155 hash_table(hash_table), |
156 num_entries(num_entries), | 156 num_entries(num_entries), |
157 used_count(used_count) { | 157 used_count(used_count) { |
158 memcpy(this->salt, salt, LINK_SALT_LENGTH); | 158 memcpy(this->salt, salt, LINK_SALT_LENGTH); |
(...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
655 base::ScopedFILE file_closer(base::OpenFile(filename, "rb+")); | 655 base::ScopedFILE file_closer(base::OpenFile(filename, "rb+")); |
656 if (!file_closer.get()) | 656 if (!file_closer.get()) |
657 return false; | 657 return false; |
658 | 658 |
659 int32_t num_entries, used_count; | 659 int32_t num_entries, used_count; |
660 uint8_t salt[LINK_SALT_LENGTH]; | 660 uint8_t salt[LINK_SALT_LENGTH]; |
661 if (!ReadFileHeader(file_closer.get(), &num_entries, &used_count, salt)) | 661 if (!ReadFileHeader(file_closer.get(), &num_entries, &used_count, salt)) |
662 return false; // Header isn't valid. | 662 return false; // Header isn't valid. |
663 | 663 |
664 // Allocate and read the table. | 664 // Allocate and read the table. |
665 scoped_ptr<base::SharedMemory> shared_memory; | 665 std::unique_ptr<base::SharedMemory> shared_memory; |
666 VisitedLinkCommon::Fingerprint* hash_table; | 666 VisitedLinkCommon::Fingerprint* hash_table; |
667 if (!CreateApartURLTable(num_entries, salt, &shared_memory, &hash_table)) | 667 if (!CreateApartURLTable(num_entries, salt, &shared_memory, &hash_table)) |
668 return false; | 668 return false; |
669 | 669 |
670 if (!ReadFromFile(file_closer.get(), kFileHeaderSize, hash_table, | 670 if (!ReadFromFile(file_closer.get(), kFileHeaderSize, hash_table, |
671 num_entries * sizeof(Fingerprint))) { | 671 num_entries * sizeof(Fingerprint))) { |
672 return false; | 672 return false; |
673 } | 673 } |
674 | 674 |
675 *load_from_file_result = | 675 *load_from_file_result = |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
852 return false; | 852 return false; |
853 | 853 |
854 base::FilePath profile_dir = browser_context_->GetPath(); | 854 base::FilePath profile_dir = browser_context_->GetPath(); |
855 *filename = profile_dir.Append(FILE_PATH_LITERAL("Visited Links")); | 855 *filename = profile_dir.Append(FILE_PATH_LITERAL("Visited Links")); |
856 return true; | 856 return true; |
857 } | 857 } |
858 | 858 |
859 // Initializes the shared memory structure. The salt should already be filled | 859 // Initializes the shared memory structure. The salt should already be filled |
860 // in so that it can be written to the shared memory | 860 // in so that it can be written to the shared memory |
861 bool VisitedLinkMaster::CreateURLTable(int32_t num_entries) { | 861 bool VisitedLinkMaster::CreateURLTable(int32_t num_entries) { |
862 scoped_ptr<base::SharedMemory> shared_memory; | 862 std::unique_ptr<base::SharedMemory> shared_memory; |
863 VisitedLinkCommon::Fingerprint* hash_table; | 863 VisitedLinkCommon::Fingerprint* hash_table; |
864 if (CreateApartURLTable(num_entries, salt_, &shared_memory, &hash_table)) { | 864 if (CreateApartURLTable(num_entries, salt_, &shared_memory, &hash_table)) { |
865 shared_memory_ = shared_memory.release(); | 865 shared_memory_ = shared_memory.release(); |
866 hash_table_ = hash_table; | 866 hash_table_ = hash_table; |
867 table_length_ = num_entries; | 867 table_length_ = num_entries; |
868 used_items_ = 0; | 868 used_items_ = 0; |
869 return true; | 869 return true; |
870 } | 870 } |
871 | 871 |
872 return false; | 872 return false; |
873 } | 873 } |
874 | 874 |
875 // static | 875 // static |
876 bool VisitedLinkMaster::CreateApartURLTable( | 876 bool VisitedLinkMaster::CreateApartURLTable( |
877 int32_t num_entries, | 877 int32_t num_entries, |
878 const uint8_t salt[LINK_SALT_LENGTH], | 878 const uint8_t salt[LINK_SALT_LENGTH], |
879 scoped_ptr<base::SharedMemory>* shared_memory, | 879 std::unique_ptr<base::SharedMemory>* shared_memory, |
880 VisitedLinkCommon::Fingerprint** hash_table) { | 880 VisitedLinkCommon::Fingerprint** hash_table) { |
881 DCHECK(salt); | 881 DCHECK(salt); |
882 DCHECK(shared_memory); | 882 DCHECK(shared_memory); |
883 DCHECK(hash_table); | 883 DCHECK(hash_table); |
884 | 884 |
885 // The table is the size of the table followed by the entries. | 885 // The table is the size of the table followed by the entries. |
886 uint32_t alloc_size = | 886 uint32_t alloc_size = |
887 num_entries * sizeof(Fingerprint) + sizeof(SharedHeader); | 887 num_entries * sizeof(Fingerprint) + sizeof(SharedHeader); |
888 | 888 |
889 // Create the shared memory object. | 889 // Create the shared memory object. |
890 scoped_ptr<base::SharedMemory> sh_mem(new base::SharedMemory()); | 890 std::unique_ptr<base::SharedMemory> sh_mem(new base::SharedMemory()); |
891 if (!sh_mem) | 891 if (!sh_mem) |
892 return false; | 892 return false; |
893 | 893 |
894 base::SharedMemoryCreateOptions options; | 894 base::SharedMemoryCreateOptions options; |
895 options.size = alloc_size; | 895 options.size = alloc_size; |
896 options.share_read_only = true; | 896 options.share_read_only = true; |
897 | 897 |
898 if (!sh_mem->Create(options) || !sh_mem->Map(alloc_size)) | 898 if (!sh_mem->Create(options) || !sh_mem->Map(alloc_size)) |
899 return false; | 899 return false; |
900 | 900 |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1195 BrowserThread::UI, FROM_HERE, | 1195 BrowserThread::UI, FROM_HERE, |
1196 base::Bind(&TableBuilder::OnCompleteMainThread, this)); | 1196 base::Bind(&TableBuilder::OnCompleteMainThread, this)); |
1197 } | 1197 } |
1198 | 1198 |
1199 void VisitedLinkMaster::TableBuilder::OnCompleteMainThread() { | 1199 void VisitedLinkMaster::TableBuilder::OnCompleteMainThread() { |
1200 if (master_) | 1200 if (master_) |
1201 master_->OnTableRebuildComplete(success_, fingerprints_); | 1201 master_->OnTableRebuildComplete(success_, fingerprints_); |
1202 } | 1202 } |
1203 | 1203 |
1204 } // namespace visitedlink | 1204 } // namespace visitedlink |
OLD | NEW |