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 #ifndef COMPONENTS_VISITEDLINK_BROWSER_VISITEDLINK_MASTER_H_ | 5 #ifndef COMPONENTS_VISITEDLINK_BROWSER_VISITEDLINK_MASTER_H_ |
6 #define COMPONENTS_VISITEDLINK_BROWSER_VISITEDLINK_MASTER_H_ | 6 #define COMPONENTS_VISITEDLINK_BROWSER_VISITEDLINK_MASTER_H_ |
7 | 7 |
8 #if defined(OS_WIN) | 8 #include <stddef.h> |
9 #include <windows.h> | 9 #include <stdint.h> |
10 #endif | 10 |
11 #include <set> | 11 #include <set> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/callback.h" | 14 #include "base/callback.h" |
15 #include "base/callback_forward.h" | 15 #include "base/callback_forward.h" |
16 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
17 #include "base/gtest_prod_util.h" | 17 #include "base/gtest_prod_util.h" |
| 18 #include "base/macros.h" |
18 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
19 #include "base/memory/shared_memory.h" | 20 #include "base/memory/shared_memory.h" |
20 #include "base/memory/weak_ptr.h" | 21 #include "base/memory/weak_ptr.h" |
21 #include "base/threading/sequenced_worker_pool.h" | 22 #include "base/threading/sequenced_worker_pool.h" |
| 23 #include "build/build_config.h" |
22 #include "components/visitedlink/common/visitedlink_common.h" | 24 #include "components/visitedlink/common/visitedlink_common.h" |
23 | 25 |
| 26 #if defined(OS_WIN) |
| 27 #include <windows.h> |
| 28 #endif |
| 29 |
24 #if defined(UNIT_TEST) || defined(PERF_TEST) || !defined(NDEBUG) | 30 #if defined(UNIT_TEST) || defined(PERF_TEST) || !defined(NDEBUG) |
25 #include "base/logging.h" | 31 #include "base/logging.h" |
26 #endif | 32 #endif |
27 | 33 |
28 class GURL; | 34 class GURL; |
29 | 35 |
30 namespace content { | 36 namespace content { |
31 class BrowserContext; | 37 class BrowserContext; |
32 } | 38 } |
33 | 39 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 // this failure mode). | 89 // this failure mode). |
84 // | 90 // |
85 // When |suppress_rebuild| is set, we'll not attempt to load data from | 91 // When |suppress_rebuild| is set, we'll not attempt to load data from |
86 // history if the file can't be loaded. This should generally be set for | 92 // history if the file can't be loaded. This should generally be set for |
87 // testing except when you want to test the rebuild process explicitly. | 93 // testing except when you want to test the rebuild process explicitly. |
88 VisitedLinkMaster(Listener* listener, | 94 VisitedLinkMaster(Listener* listener, |
89 VisitedLinkDelegate* delegate, | 95 VisitedLinkDelegate* delegate, |
90 bool persist_to_disk, | 96 bool persist_to_disk, |
91 bool suppress_rebuild, | 97 bool suppress_rebuild, |
92 const base::FilePath& filename, | 98 const base::FilePath& filename, |
93 int32 default_table_size); | 99 int32_t default_table_size); |
94 ~VisitedLinkMaster() override; | 100 ~VisitedLinkMaster() override; |
95 | 101 |
96 // Must be called immediately after object creation. Nothing else will work | 102 // Must be called immediately after object creation. Nothing else will work |
97 // until this is called. Returns true on success, false means that this | 103 // until this is called. Returns true on success, false means that this |
98 // object won't work. | 104 // object won't work. |
99 bool Init(); | 105 bool Init(); |
100 | 106 |
101 base::SharedMemory* shared_memory() { return shared_memory_; } | 107 base::SharedMemory* shared_memory() { return shared_memory_; } |
102 | 108 |
103 // Adds a URL to the table. | 109 // Adds a URL to the table. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 | 144 |
139 // Sets a task to execute when the next rebuild from history is complete. | 145 // Sets a task to execute when the next rebuild from history is complete. |
140 // This is used by unit tests to wait for the rebuild to complete before | 146 // This is used by unit tests to wait for the rebuild to complete before |
141 // they continue. The pointer will be owned by this object after the call. | 147 // they continue. The pointer will be owned by this object after the call. |
142 void set_rebuild_complete_task(const base::Closure& task) { | 148 void set_rebuild_complete_task(const base::Closure& task) { |
143 DCHECK(rebuild_complete_task_.is_null()); | 149 DCHECK(rebuild_complete_task_.is_null()); |
144 rebuild_complete_task_ = task; | 150 rebuild_complete_task_ = task; |
145 } | 151 } |
146 | 152 |
147 // returns the number of items in the table for testing verification | 153 // returns the number of items in the table for testing verification |
148 int32 GetUsedCount() const { | 154 int32_t GetUsedCount() const { return used_items_; } |
149 return used_items_; | |
150 } | |
151 | 155 |
152 // Returns the listener. | 156 // Returns the listener. |
153 VisitedLinkMaster::Listener* GetListener() const { | 157 VisitedLinkMaster::Listener* GetListener() const { |
154 return listener_.get(); | 158 return listener_.get(); |
155 } | 159 } |
156 | 160 |
157 // Call to cause the entire database file to be re-written from scratch | 161 // Call to cause the entire database file to be re-written from scratch |
158 // to disk. Used by the performance tester. | 162 // to disk. Used by the performance tester. |
159 void RewriteFile() { | 163 void RewriteFile() { |
160 WriteFullTable(); | 164 WriteFullTable(); |
(...skipping 10 matching lines...) Expand all Loading... |
171 struct LoadFromFileResult; | 175 struct LoadFromFileResult; |
172 | 176 |
173 using TableLoadCompleteCallback = base::Callback<void( | 177 using TableLoadCompleteCallback = base::Callback<void( |
174 bool success, | 178 bool success, |
175 scoped_refptr<LoadFromFileResult> load_from_file_result)>; | 179 scoped_refptr<LoadFromFileResult> load_from_file_result)>; |
176 | 180 |
177 // Object to rebuild the table on the history thread (see the .cc file). | 181 // Object to rebuild the table on the history thread (see the .cc file). |
178 class TableBuilder; | 182 class TableBuilder; |
179 | 183 |
180 // Byte offsets of values in the header. | 184 // Byte offsets of values in the header. |
181 static const int32 kFileHeaderSignatureOffset; | 185 static const int32_t kFileHeaderSignatureOffset; |
182 static const int32 kFileHeaderVersionOffset; | 186 static const int32_t kFileHeaderVersionOffset; |
183 static const int32 kFileHeaderLengthOffset; | 187 static const int32_t kFileHeaderLengthOffset; |
184 static const int32 kFileHeaderUsedOffset; | 188 static const int32_t kFileHeaderUsedOffset; |
185 static const int32 kFileHeaderSaltOffset; | 189 static const int32_t kFileHeaderSaltOffset; |
186 | 190 |
187 // The signature at the beginning of a file. | 191 // The signature at the beginning of a file. |
188 static const int32 kFileSignature; | 192 static const int32_t kFileSignature; |
189 | 193 |
190 // version of the file format this module currently uses | 194 // version of the file format this module currently uses |
191 static const int32 kFileCurrentVersion; | 195 static const int32_t kFileCurrentVersion; |
192 | 196 |
193 // Bytes in the file header, including the salt. | 197 // Bytes in the file header, including the salt. |
194 static const size_t kFileHeaderSize; | 198 static const size_t kFileHeaderSize; |
195 | 199 |
196 // When creating a fresh new table, we use this many entries. | 200 // When creating a fresh new table, we use this many entries. |
197 static const unsigned kDefaultTableSize; | 201 static const unsigned kDefaultTableSize; |
198 | 202 |
199 // When the user is deleting a boatload of URLs, we don't really want to do | 203 // When the user is deleting a boatload of URLs, we don't really want to do |
200 // individual writes for each of them. When the count exceeds this threshold, | 204 // individual writes for each of them. When the count exceeds this threshold, |
201 // we will write the whole table to disk at once instead of individual items. | 205 // we will write the whole table to disk at once instead of individual items. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 scoped_refptr<LoadFromFileResult> load_from_file_result); | 248 scoped_refptr<LoadFromFileResult> load_from_file_result); |
245 | 249 |
246 // Reads the header of the link coloring database from disk. Assumes the | 250 // Reads the header of the link coloring database from disk. Assumes the |
247 // file pointer is at the beginning of the file and that it is the first | 251 // file pointer is at the beginning of the file and that it is the first |
248 // asynchronous I/O operation on the background thread. | 252 // asynchronous I/O operation on the background thread. |
249 // | 253 // |
250 // Returns true on success and places the size of the table in num_entries | 254 // Returns true on success and places the size of the table in num_entries |
251 // and the number of nonzero fingerprints in used_count. This will fail if | 255 // and the number of nonzero fingerprints in used_count. This will fail if |
252 // the version of the file is not the current version of the database. | 256 // the version of the file is not the current version of the database. |
253 static bool ReadFileHeader(FILE* hfile, | 257 static bool ReadFileHeader(FILE* hfile, |
254 int32* num_entries, | 258 int32_t* num_entries, |
255 int32* used_count, | 259 int32_t* used_count, |
256 uint8 salt[LINK_SALT_LENGTH]); | 260 uint8_t salt[LINK_SALT_LENGTH]); |
257 | 261 |
258 // Fills *filename with the name of the link database filename | 262 // Fills *filename with the name of the link database filename |
259 bool GetDatabaseFileName(base::FilePath* filename); | 263 bool GetDatabaseFileName(base::FilePath* filename); |
260 | 264 |
261 // Wrapper around Window's WriteFile using asynchronous I/O. This will proxy | 265 // Wrapper around Window's WriteFile using asynchronous I/O. This will proxy |
262 // the write to a background thread. | 266 // the write to a background thread. |
263 void WriteToFile(FILE** hfile, off_t offset, void* data, int32 data_size); | 267 void WriteToFile(FILE** hfile, off_t offset, void* data, int32_t data_size); |
264 | 268 |
265 // Helper function to schedule and asynchronous write of the used count to | 269 // Helper function to schedule and asynchronous write of the used count to |
266 // disk (this is a common operation). | 270 // disk (this is a common operation). |
267 void WriteUsedItemCountToFile(); | 271 void WriteUsedItemCountToFile(); |
268 | 272 |
269 // Helper function to schedule an asynchronous write of the given range of | 273 // Helper function to schedule an asynchronous write of the given range of |
270 // hash functions to disk. The range is inclusive on both ends. The range can | 274 // hash functions to disk. The range is inclusive on both ends. The range can |
271 // wrap around at 0 and this function will handle it. | 275 // wrap around at 0 and this function will handle it. |
272 void WriteHashRangeToFile(Hash first_hash, Hash last_hash); | 276 void WriteHashRangeToFile(Hash first_hash, Hash last_hash); |
273 | 277 |
(...skipping 28 matching lines...) Expand all Loading... |
302 | 306 |
303 // Creates a new empty table, call if InitFromFile() fails. Normally, when | 307 // Creates a new empty table, call if InitFromFile() fails. Normally, when |
304 // |suppress_rebuild| is false, the table will be rebuilt from history, | 308 // |suppress_rebuild| is false, the table will be rebuilt from history, |
305 // keeping us in sync. When |suppress_rebuild| is true, the new table will be | 309 // keeping us in sync. When |suppress_rebuild| is true, the new table will be |
306 // empty and we will not consult history. This is used when clearing the | 310 // empty and we will not consult history. This is used when clearing the |
307 // database and for unit tests. | 311 // database and for unit tests. |
308 bool InitFromScratch(bool suppress_rebuild); | 312 bool InitFromScratch(bool suppress_rebuild); |
309 | 313 |
310 // Allocates the Fingerprint structure and length. Structure is filled with 0s | 314 // Allocates the Fingerprint structure and length. Structure is filled with 0s |
311 // and shared header with salt and used_items_ is set to 0. | 315 // and shared header with salt and used_items_ is set to 0. |
312 bool CreateURLTable(int32 num_entries); | 316 bool CreateURLTable(int32_t num_entries); |
313 | 317 |
314 // Allocates the Fingerprint structure and length. Returns true on success. | 318 // Allocates the Fingerprint structure and length. Returns true on success. |
315 // Structure is filled with 0s and shared header with salt. The result of | 319 // Structure is filled with 0s and shared header with salt. The result of |
316 // allocation is saved into |shared_memory| and |hash_table| points to the | 320 // allocation is saved into |shared_memory| and |hash_table| points to the |
317 // beginning of Fingerprint table in |shared_memory|. | 321 // beginning of Fingerprint table in |shared_memory|. |
318 static bool CreateApartURLTable(int32 num_entries, | 322 static bool CreateApartURLTable(int32_t num_entries, |
319 const uint8 salt[LINK_SALT_LENGTH], | 323 const uint8_t salt[LINK_SALT_LENGTH], |
320 scoped_ptr<base::SharedMemory>* shared_memory, | 324 scoped_ptr<base::SharedMemory>* shared_memory, |
321 VisitedLinkCommon::Fingerprint** hash_table); | 325 VisitedLinkCommon::Fingerprint** hash_table); |
322 | 326 |
323 // A wrapper for CreateURLTable, this will allocate a new table, initialized | 327 // A wrapper for CreateURLTable, this will allocate a new table, initialized |
324 // to empty. The caller is responsible for saving the shared memory pointer | 328 // to empty. The caller is responsible for saving the shared memory pointer |
325 // and handles before this call (they will be replaced with new ones) and | 329 // and handles before this call (they will be replaced with new ones) and |
326 // releasing them later. This is designed for callers that make a new table | 330 // releasing them later. This is designed for callers that make a new table |
327 // and then copy values from the old table to the new one, then release the | 331 // and then copy values from the old table to the new one, then release the |
328 // old table. | 332 // old table. |
329 // | 333 // |
330 // Returns true on success. On failure, the old table will be restored. The | 334 // Returns true on success. On failure, the old table will be restored. The |
331 // caller should not attemp to release the pointer/handle in this case. | 335 // caller should not attemp to release the pointer/handle in this case. |
332 bool BeginReplaceURLTable(int32 num_entries); | 336 bool BeginReplaceURLTable(int32_t num_entries); |
333 | 337 |
334 // unallocates the Fingerprint table | 338 // unallocates the Fingerprint table |
335 void FreeURLTable(); | 339 void FreeURLTable(); |
336 | 340 |
337 // For growing the table. ResizeTableIfNecessary will check to see if the | 341 // For growing the table. ResizeTableIfNecessary will check to see if the |
338 // table should be resized and calls ResizeTable if needed. Returns true if | 342 // table should be resized and calls ResizeTable if needed. Returns true if |
339 // we decided to resize the table. | 343 // we decided to resize the table. |
340 bool ResizeTableIfNecessary(); | 344 bool ResizeTableIfNecessary(); |
341 | 345 |
342 // Resizes the table (growing or shrinking) as necessary to accomodate the | 346 // Resizes the table (growing or shrinking) as necessary to accomodate the |
343 // current count. | 347 // current count. |
344 void ResizeTable(int32 new_size); | 348 void ResizeTable(int32_t new_size); |
345 | 349 |
346 // Returns the default table size. It can be overrided in unit tests. | 350 // Returns the default table size. It can be overrided in unit tests. |
347 uint32 DefaultTableSize() const; | 351 uint32_t DefaultTableSize() const; |
348 | 352 |
349 // Returns the desired table size for |item_count| URLs. | 353 // Returns the desired table size for |item_count| URLs. |
350 uint32 NewTableSizeForCount(int32 item_count) const; | 354 uint32_t NewTableSizeForCount(int32_t item_count) const; |
351 | 355 |
352 // Computes the table load as fraction. For example, if 1/4 of the entries are | 356 // Computes the table load as fraction. For example, if 1/4 of the entries are |
353 // full, this value will be 0.25 | 357 // full, this value will be 0.25 |
354 float ComputeTableLoad() const { | 358 float ComputeTableLoad() const { |
355 return static_cast<float>(used_items_) / static_cast<float>(table_length_); | 359 return static_cast<float>(used_items_) / static_cast<float>(table_length_); |
356 } | 360 } |
357 | 361 |
358 // Initializes a rebuild of the visited link database based on the browser | 362 // Initializes a rebuild of the visited link database based on the browser |
359 // history. This will set table_builder_ while working, and there should not | 363 // history. This will set table_builder_ while working, and there should not |
360 // already be a rebuild in place when called. See the definition for more | 364 // already be a rebuild in place when called. See the definition for more |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
426 | 430 |
427 // If true, will try to persist the hash table to disk. Will rebuild from | 431 // If true, will try to persist the hash table to disk. Will rebuild from |
428 // VisitedLinkDelegate::RebuildTable if there are disk corruptions. | 432 // VisitedLinkDelegate::RebuildTable if there are disk corruptions. |
429 bool persist_to_disk_; | 433 bool persist_to_disk_; |
430 | 434 |
431 // Shared memory consists of a SharedHeader followed by the table. | 435 // Shared memory consists of a SharedHeader followed by the table. |
432 base::SharedMemory *shared_memory_; | 436 base::SharedMemory *shared_memory_; |
433 | 437 |
434 // When we generate new tables, we increment the serial number of the | 438 // When we generate new tables, we increment the serial number of the |
435 // shared memory object. | 439 // shared memory object. |
436 int32 shared_memory_serial_; | 440 int32_t shared_memory_serial_; |
437 | 441 |
438 // Number of non-empty items in the table, used to compute fullness. | 442 // Number of non-empty items in the table, used to compute fullness. |
439 int32 used_items_; | 443 int32_t used_items_; |
440 | 444 |
441 // We set this to true to avoid writing to the database file. | 445 // We set this to true to avoid writing to the database file. |
442 bool table_is_loading_from_file_; | 446 bool table_is_loading_from_file_; |
443 | 447 |
444 // Testing values ----------------------------------------------------------- | 448 // Testing values ----------------------------------------------------------- |
445 // | 449 // |
446 // The following fields exist for testing purposes. They are not used in | 450 // The following fields exist for testing purposes. They are not used in |
447 // release builds. It'd be nice to eliminate them in release builds, but we | 451 // release builds. It'd be nice to eliminate them in release builds, but we |
448 // don't want to change the signature of the object between the unit test and | 452 // don't want to change the signature of the object between the unit test and |
449 // regular builds. Instead, we just have "default" values that these take | 453 // regular builds. Instead, we just have "default" values that these take |
450 // in release builds that give "regular" behavior. | 454 // in release builds that give "regular" behavior. |
451 | 455 |
452 // Overridden database file name for testing | 456 // Overridden database file name for testing |
453 base::FilePath database_name_override_; | 457 base::FilePath database_name_override_; |
454 | 458 |
455 // When nonzero, overrides the table size for new databases for testing | 459 // When nonzero, overrides the table size for new databases for testing |
456 int32 table_size_override_; | 460 int32_t table_size_override_; |
457 | 461 |
458 // When set, indicates the task that should be run after the next rebuild from | 462 // When set, indicates the task that should be run after the next rebuild from |
459 // history is complete. | 463 // history is complete. |
460 base::Closure rebuild_complete_task_; | 464 base::Closure rebuild_complete_task_; |
461 | 465 |
462 // Set to prevent us from attempting to rebuild the database from global | 466 // Set to prevent us from attempting to rebuild the database from global |
463 // history if we have an error opening the file. This is used for testing, | 467 // history if we have an error opening the file. This is used for testing, |
464 // will be false in production. | 468 // will be false in production. |
465 bool suppress_rebuild_; | 469 bool suppress_rebuild_; |
466 | 470 |
467 base::WeakPtrFactory<VisitedLinkMaster> weak_ptr_factory_; | 471 base::WeakPtrFactory<VisitedLinkMaster> weak_ptr_factory_; |
468 | 472 |
469 DISALLOW_COPY_AND_ASSIGN(VisitedLinkMaster); | 473 DISALLOW_COPY_AND_ASSIGN(VisitedLinkMaster); |
470 }; | 474 }; |
471 | 475 |
472 // NOTE: These methods are defined inline here, so we can share the compilation | 476 // NOTE: These methods are defined inline here, so we can share the compilation |
473 // of visitedlink_master.cc between the browser and the unit/perf tests. | 477 // of visitedlink_master.cc between the browser and the unit/perf tests. |
474 | 478 |
475 #if defined(UNIT_TEST) || defined(PERF_TEST) || !defined(NDEBUG) | 479 #if defined(UNIT_TEST) || defined(PERF_TEST) || !defined(NDEBUG) |
476 inline void VisitedLinkMaster::DebugValidate() { | 480 inline void VisitedLinkMaster::DebugValidate() { |
477 int32 used_count = 0; | 481 int32_t used_count = 0; |
478 for (int32 i = 0; i < table_length_; i++) { | 482 for (int32_t i = 0; i < table_length_; i++) { |
479 if (hash_table_[i]) | 483 if (hash_table_[i]) |
480 used_count++; | 484 used_count++; |
481 } | 485 } |
482 DCHECK_EQ(used_count, used_items_); | 486 DCHECK_EQ(used_count, used_items_); |
483 } | 487 } |
484 #endif | 488 #endif |
485 | 489 |
486 } // namespace visitedlink | 490 } // namespace visitedlink |
487 | 491 |
488 #endif // COMPONENTS_VISITEDLINK_BROWSER_VISITEDLINK_MASTER_H_ | 492 #endif // COMPONENTS_VISITEDLINK_BROWSER_VISITEDLINK_MASTER_H_ |
OLD | NEW |