| 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 #ifndef CHROME_BROWSER_VISITEDLINK_MASTER_H__ | 5 #ifndef CHROME_BROWSER_VISITEDLINK_MASTER_H__ |
| 6 #define CHROME_BROWSER_VISITEDLINK_MASTER_H__ | 6 #define CHROME_BROWSER_VISITEDLINK_MASTER_H__ |
| 7 | 7 |
| 8 #if defined(OS_WIN) | |
| 9 #include <windows.h> | |
| 10 #endif | |
| 11 #include <set> | 8 #include <set> |
| 12 #include <string> | 9 #include <string> |
| 13 #include <vector> | 10 #include <vector> |
| 14 | 11 |
| 15 #include "base/file_path.h" | |
| 16 #include "base/ref_counted.h" | 12 #include "base/ref_counted.h" |
| 17 #include "base/shared_memory.h" | 13 #include "base/shared_memory.h" |
| 18 #if defined(OS_WIN) | |
| 19 #include "chrome/browser/history/history.h" | 14 #include "chrome/browser/history/history.h" |
| 20 #else | |
| 21 // TODO(port): remove scaffolding, use history.h for both POSIX and WIN. | |
| 22 #include "chrome/common/temp_scaffolding_stubs.h" | |
| 23 #endif // !defined(OS_WIN) | |
| 24 #include "chrome/common/visitedlink_common.h" | 15 #include "chrome/common/visitedlink_common.h" |
| 25 #include "testing/gtest/include/gtest/gtest_prod.h" | 16 #include "testing/gtest/include/gtest/gtest_prod.h" |
| 26 | 17 |
| 27 class GURL; | 18 class GURL; |
| 28 class Profile; | 19 class Profile; |
| 29 | 20 |
| 30 // Controls the link coloring database. The master controls all writing to the | 21 // Controls the link coloring database. The master controls all writing to the |
| 31 // database as well as disk I/O. There should be only one master. | 22 // database as well as disk I/O. There should be only one master. |
| 32 // | 23 // |
| 33 // This class will optionally defer writing operations to another thread. This | 24 // This class will optionally defer writing operations to another thread. This |
| (...skipping 21 matching lines...) Expand all Loading... |
| 55 // the main version, which will probably fail (which can be good for testing | 46 // the main version, which will probably fail (which can be good for testing |
| 56 // this failure mode). | 47 // this failure mode). |
| 57 // | 48 // |
| 58 // When |suppress_rebuild| is set, we'll not attempt to load data from | 49 // When |suppress_rebuild| is set, we'll not attempt to load data from |
| 59 // history if the file can't be loaded. This should generally be set for | 50 // history if the file can't be loaded. This should generally be set for |
| 60 // testing except when you want to test the rebuild process explicitly. | 51 // testing except when you want to test the rebuild process explicitly. |
| 61 VisitedLinkMaster(base::Thread* file_thread, | 52 VisitedLinkMaster(base::Thread* file_thread, |
| 62 PostNewTableEvent* poster, | 53 PostNewTableEvent* poster, |
| 63 HistoryService* history_service, | 54 HistoryService* history_service, |
| 64 bool suppress_rebuild, | 55 bool suppress_rebuild, |
| 65 const FilePath& filename, | 56 const std::wstring& filename, |
| 66 int32 default_table_size); | 57 int32 default_table_size); |
| 67 virtual ~VisitedLinkMaster(); | 58 virtual ~VisitedLinkMaster(); |
| 68 | 59 |
| 69 // Must be called immediately after object creation. Nothing else will work | 60 // Must be called immediately after object creation. Nothing else will work |
| 70 // until this is called. Returns true on success, false means that this | 61 // until this is called. Returns true on success, false means that this |
| 71 // object won't work. | 62 // object won't work. |
| 72 bool Init(); | 63 bool Init(); |
| 73 | 64 |
| 74 // Duplicates the handle to the shared memory to another process. | 65 // Duplicates the handle to the shared memory to another process. |
| 75 // Returns true on success. | 66 // Returns true on success. |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 | 134 |
| 144 // Bytes in the file header, including the salt. | 135 // Bytes in the file header, including the salt. |
| 145 static const size_t kFileHeaderSize; | 136 static const size_t kFileHeaderSize; |
| 146 | 137 |
| 147 // When creating a fresh new table, we use this many entries. | 138 // When creating a fresh new table, we use this many entries. |
| 148 static const unsigned kDefaultTableSize; | 139 static const unsigned kDefaultTableSize; |
| 149 | 140 |
| 150 // When the user is deleting a boatload of URLs, we don't really want to do | 141 // When the user is deleting a boatload of URLs, we don't really want to do |
| 151 // individual writes for each of them. When the count exceeds this threshold, | 142 // individual writes for each of them. When the count exceeds this threshold, |
| 152 // we will write the whole table to disk at once instead of individual items. | 143 // we will write the whole table to disk at once instead of individual items. |
| 153 static const size_t kBigDeleteThreshold; | 144 static const int32 kBigDeleteThreshold; |
| 154 | 145 |
| 155 // Backend for the constructors initializing the members. | 146 // Backend for the constructors initializing the members. |
| 156 void InitMembers(base::Thread* file_thread, | 147 void InitMembers(base::Thread* file_thread, |
| 157 PostNewTableEvent* poster, | 148 PostNewTableEvent* poster, |
| 158 Profile* profile); | 149 Profile* profile); |
| 159 | 150 |
| 160 // If a rebuild is in progress, we save the URL in the temporary list. | 151 // If a rebuild is in progress, we save the URL in the temporary list. |
| 161 // Otherwise, we add this to the table. Returns the index of the | 152 // Otherwise, we add this to the table. Returns the index of the |
| 162 // inserted fingerprint or null_hash_ on failure. | 153 // inserted fingerprint or null_hash_ on failure. |
| 163 Hash TryToAddURL(const GURL& url); | 154 Hash TryToAddURL(const GURL& url); |
| 164 | 155 |
| 165 // File I/O functions | 156 // File I/O functions |
| 166 // ------------------ | 157 // ------------------ |
| 167 | 158 |
| 168 // Writes the entire table to disk, returning true on success. It will leave | 159 // Writes the entire table to disk, returning true on success. It will leave |
| 169 // the table file open and the handle to it in file_ | 160 // the table file open and the handle to it in file_ |
| 170 bool WriteFullTable(); | 161 bool WriteFullTable(); |
| 171 | 162 |
| 172 // Try to load the table from the database file. If the file doesn't exist or | 163 // Try to load the table from the database file. If the file doesn't exist or |
| 173 // is corrupt, this will return failure. | 164 // is corrupt, this will return failure. |
| 174 bool InitFromFile(); | 165 bool InitFromFile(); |
| 175 | 166 |
| 176 // Reads the header of the link coloring database from disk. Assumes the | 167 // Reads the header of the link coloring database from disk. Assumes the |
| 177 // file pointer is at the beginning of the file and that there are no pending | 168 // file pointer is at the beginning of the file and that there are no pending |
| 178 // asynchronous I/O operations. | 169 // asynchronous I/O operations. |
| 179 // | 170 // |
| 180 // Returns true on success and places the size of the table in num_entries | 171 // Returns true on success and places the size of the table in num_entries |
| 181 // and the number of nonzero fingerprints in used_count. This will fail if | 172 // and the number of nonzero fingerprints in used_count. This will fail if |
| 182 // the version of the file is not the current version of the database. | 173 // the version of the file is not the current version of the database. |
| 183 bool ReadFileHeader(FILE* hfile, int32* num_entries, int32* used_count, | 174 bool ReadFileHeader(HANDLE hfile, int32* num_entries, int32* used_count, |
| 184 uint8 salt[LINK_SALT_LENGTH]); | 175 uint8 salt[LINK_SALT_LENGTH]); |
| 185 | 176 |
| 186 // Fills *filename with the name of the link database filename | 177 // Fills *filename with the name of the link database filename |
| 187 bool GetDatabaseFileName(FilePath* filename); | 178 bool GetDatabaseFileName(std::wstring* filename); |
| 188 | 179 |
| 189 // Wrapper around Window's WriteFile using asynchronous I/O. This will proxy | 180 // Wrapper around Window's WriteFile using asynchronous I/O. This will proxy |
| 190 // the write to a background thread. | 181 // the write to a background thread. |
| 191 void WriteToFile(FILE* hfile, off_t offset, void* data, int32 data_size); | 182 void WriteToFile(HANDLE hfile, int32 offset, void* data, int32 data_size); |
| 192 | 183 |
| 193 // Helper function to schedule and asynchronous write of the used count to | 184 // Helper function to schedule and asynchronous write of the used count to |
| 194 // disk (this is a common operation). | 185 // disk (this is a common operation). |
| 195 void WriteUsedItemCountToFile(); | 186 void WriteUsedItemCountToFile(); |
| 196 | 187 |
| 197 // Helper function to schedule an asynchronous write of the given range of | 188 // Helper function to schedule an asynchronous write of the given range of |
| 198 // hash functions to disk. The range is inclusive on both ends. The range can | 189 // hash functions to disk. The range is inclusive on both ends. The range can |
| 199 // wrap around at 0 and this function will handle it. | 190 // wrap around at 0 and this function will handle it. |
| 200 void WriteHashRangeToFile(Hash first_hash, Hash last_hash); | 191 void WriteHashRangeToFile(Hash first_hash, Hash last_hash); |
| 201 | 192 |
| 202 // Synchronous read from the file. Assumes there are no pending asynchronous | 193 // Synchronous read from the file. Assumes there are no pending asynchronous |
| 203 // I/O functions. Returns true if the entire buffer was successfully filled. | 194 // I/O functions. Returns true if the entire buffer was successfully filled. |
| 204 bool ReadFromFile(FILE* hfile, off_t offset, void* data, size_t data_size); | 195 bool ReadFromFile(HANDLE hfile, int32 offset, void* data, int32 data_size); |
| 205 | 196 |
| 206 // General table handling | 197 // General table handling |
| 207 // ---------------------- | 198 // ---------------------- |
| 208 | 199 |
| 209 // Called to add a fingerprint to the table. Returns the index of the | 200 // Called to add a fingerprint to the table. Returns the index of the |
| 210 // inserted fingerprint or null_hash_ if there was a duplicate and this item | 201 // inserted fingerprint or null_hash_ if there was a duplicate and this item |
| 211 // was skippped. | 202 // was skippped. |
| 212 Hash AddFingerprint(Fingerprint fingerprint); | 203 Hash AddFingerprint(Fingerprint fingerprint); |
| 213 | 204 |
| 214 // Deletes all fingerprints from the given vector from the current hash table | 205 // Deletes all fingerprints from the given vector from the current hash table |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 std::set<Fingerprint> deleted_since_rebuild_; | 319 std::set<Fingerprint> deleted_since_rebuild_; |
| 329 | 320 |
| 330 // TODO(brettw) Support deletion, we need to track whether anything was | 321 // TODO(brettw) Support deletion, we need to track whether anything was |
| 331 // deleted during the rebuild here. Then we should delete any of these | 322 // deleted during the rebuild here. Then we should delete any of these |
| 332 // entries from the complete table later. | 323 // entries from the complete table later. |
| 333 //std::vector<Fingerprint> removed_since_rebuild_; | 324 //std::vector<Fingerprint> removed_since_rebuild_; |
| 334 | 325 |
| 335 // The currently open file with the table in it. This may be NULL if we're | 326 // The currently open file with the table in it. This may be NULL if we're |
| 336 // rebuilding and haven't written a new version yet. Writing to the file may | 327 // rebuilding and haven't written a new version yet. Writing to the file may |
| 337 // be safely ignored in this case. | 328 // be safely ignored in this case. |
| 338 FILE* file_; | 329 HANDLE file_; |
| 339 | 330 |
| 340 // Shared memory consists of a SharedHeader followed by the table. | 331 // Shared memory consists of a SharedHeader followed by the table. |
| 341 base::SharedMemory *shared_memory_; | 332 base::SharedMemory *shared_memory_; |
| 342 | 333 |
| 343 // When we generate new tables, we increment the serial number of the | 334 // When we generate new tables, we increment the serial number of the |
| 344 // shared memory object. | 335 // shared memory object. |
| 345 int32 shared_memory_serial_; | 336 int32 shared_memory_serial_; |
| 346 | 337 |
| 347 // Number of non-empty items in the table, used to compute fullness. | 338 // Number of non-empty items in the table, used to compute fullness. |
| 348 int32 used_items_; | 339 int32 used_items_; |
| 349 | 340 |
| 350 // Testing values ----------------------------------------------------------- | 341 // Testing values ----------------------------------------------------------- |
| 351 // | 342 // |
| 352 // The following fields exist for testing purposes. They are not used in | 343 // The following fields exist for testing purposes. They are not used in |
| 353 // release builds. It'd be nice to eliminate them in release builds, but we | 344 // release builds. It'd be nice to eliminate them in release builds, but we |
| 354 // don't want to change the signature of the object between the unit test and | 345 // don't want to change the signature of the object between the unit test and |
| 355 // regular builds. Instead, we just have "default" values that these take | 346 // regular builds. Instead, we just have "default" values that these take |
| 356 // in release builds that give "regular" behavior. | 347 // in release builds that give "regular" behavior. |
| 357 | 348 |
| 358 // Overridden database file name for testing | 349 // Overridden database file name for testing |
| 359 FilePath database_name_override_; | 350 std::wstring database_name_override_; |
| 360 | 351 |
| 361 // When nonzero, overrides the table size for new databases for testing | 352 // When nonzero, overrides the table size for new databases for testing |
| 362 int32 table_size_override_; | 353 int32 table_size_override_; |
| 363 | 354 |
| 364 // When non-NULL, overrides the history service to use rather than as the | 355 // When non-NULL, overrides the history service to use rather than as the |
| 365 // BrowserProcess. This is provided for unit tests. | 356 // BrowserProcess. This is provided for unit tests. |
| 366 HistoryService* history_service_override_; | 357 HistoryService* history_service_override_; |
| 367 | 358 |
| 368 // When non-NULL, indicates the task that should be run after the next | 359 // When non-NULL, indicates the task that should be run after the next |
| 369 // rebuild from history is complete. | 360 // rebuild from history is complete. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 386 for (int32 i = 0; i < table_length_; i++) { | 377 for (int32 i = 0; i < table_length_; i++) { |
| 387 if (hash_table_[i]) | 378 if (hash_table_[i]) |
| 388 used_count++; | 379 used_count++; |
| 389 } | 380 } |
| 390 DCHECK(used_count == used_items_); | 381 DCHECK(used_count == used_items_); |
| 391 } | 382 } |
| 392 #endif | 383 #endif |
| 393 | 384 |
| 394 #endif // CHROME_BROWSER_VISITEDLINK_MASTER_H__ | 385 #endif // CHROME_BROWSER_VISITEDLINK_MASTER_H__ |
| 395 | 386 |
| OLD | NEW |