| 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 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <set> | 12 #include <set> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/callback.h" | 15 #include "base/callback.h" |
| 16 #include "base/callback_forward.h" | 16 #include "base/callback_forward.h" |
| 17 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
| 18 #include "base/gtest_prod_util.h" | 18 #include "base/gtest_prod_util.h" |
| 19 #include "base/macros.h" | 19 #include "base/macros.h" |
| 20 #include "base/memory/ref_counted.h" | 20 #include "base/memory/ref_counted.h" |
| 21 #include "base/memory/shared_memory.h" | |
| 22 #include "base/memory/weak_ptr.h" | 21 #include "base/memory/weak_ptr.h" |
| 23 #include "base/threading/sequenced_worker_pool.h" | 22 #include "base/threading/sequenced_worker_pool.h" |
| 24 #include "build/build_config.h" | 23 #include "build/build_config.h" |
| 25 #include "components/visitedlink/common/visitedlink_common.h" | 24 #include "components/visitedlink/common/visitedlink_common.h" |
| 25 #include "mojo/public/cpp/system/buffer.h" |
| 26 | 26 |
| 27 #if defined(OS_WIN) | 27 #if defined(OS_WIN) |
| 28 #include <windows.h> | 28 #include <windows.h> |
| 29 #endif | 29 #endif |
| 30 | 30 |
| 31 #if defined(UNIT_TEST) || defined(PERF_TEST) || !defined(NDEBUG) | 31 #if defined(UNIT_TEST) || defined(PERF_TEST) || !defined(NDEBUG) |
| 32 #include "base/logging.h" | 32 #include "base/logging.h" |
| 33 #endif | 33 #endif |
| 34 | 34 |
| 35 class GURL; | 35 class GURL; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 51 class VisitedLinkMaster : public VisitedLinkCommon { | 51 class VisitedLinkMaster : public VisitedLinkCommon { |
| 52 public: | 52 public: |
| 53 // Listens to the link coloring database events. The master is given this | 53 // Listens to the link coloring database events. The master is given this |
| 54 // event as a constructor argument and dispatches events using it. | 54 // event as a constructor argument and dispatches events using it. |
| 55 class Listener { | 55 class Listener { |
| 56 public: | 56 public: |
| 57 virtual ~Listener() {} | 57 virtual ~Listener() {} |
| 58 | 58 |
| 59 // Called when link coloring database has been created or replaced. The | 59 // Called when link coloring database has been created or replaced. The |
| 60 // argument is the new table handle. | 60 // argument is the new table handle. |
| 61 virtual void NewTable(base::SharedMemory*) = 0; | 61 virtual void NewTable(mojo::SharedBufferHandle table) = 0; |
| 62 | 62 |
| 63 // Called when new link has been added. The argument is the fingerprint | 63 // Called when new link has been added. The argument is the fingerprint |
| 64 // (hash) of the link. | 64 // (hash) of the link. |
| 65 virtual void Add(Fingerprint fingerprint) = 0; | 65 virtual void Add(Fingerprint fingerprint) = 0; |
| 66 | 66 |
| 67 // Called when link coloring state has been reset. This may occur when | 67 // Called when link coloring state has been reset. This may occur when |
| 68 // entire or parts of history were deleted. Also this may occur when | 68 // entire or parts of history were deleted. Also this may occur when |
| 69 // the table was rebuilt or loaded. The salt is stored in the database file. | 69 // the table was rebuilt or loaded. The salt is stored in the database file. |
| 70 // As a result the salt will change after loading the table from the | 70 // As a result the salt will change after loading the table from the |
| 71 // database file. In this case we use |invalidate_hashes| to inform that | 71 // database file. In this case we use |invalidate_hashes| to inform that |
| (...skipping 26 matching lines...) Expand all Loading... |
| 98 bool suppress_rebuild, | 98 bool suppress_rebuild, |
| 99 const base::FilePath& filename, | 99 const base::FilePath& filename, |
| 100 int32_t default_table_size); | 100 int32_t default_table_size); |
| 101 ~VisitedLinkMaster() override; | 101 ~VisitedLinkMaster() override; |
| 102 | 102 |
| 103 // Must be called immediately after object creation. Nothing else will work | 103 // Must be called immediately after object creation. Nothing else will work |
| 104 // until this is called. Returns true on success, false means that this | 104 // until this is called. Returns true on success, false means that this |
| 105 // object won't work. | 105 // object won't work. |
| 106 bool Init(); | 106 bool Init(); |
| 107 | 107 |
| 108 base::SharedMemory* shared_memory() { return shared_memory_; } | 108 const mojo::SharedBufferHandle& shared_memory() { |
| 109 return shared_memory_.get(); |
| 110 } |
| 109 | 111 |
| 110 // Adds a URL to the table. | 112 // Adds a URL to the table. |
| 111 void AddURL(const GURL& url); | 113 void AddURL(const GURL& url); |
| 112 | 114 |
| 113 // Adds a set of URLs to the table. | 115 // Adds a set of URLs to the table. |
| 114 void AddURLs(const std::vector<GURL>& urls); | 116 void AddURLs(const std::vector<GURL>& urls); |
| 115 | 117 |
| 116 // See DeleteURLs. | 118 // See DeleteURLs. |
| 117 class URLIterator { | 119 class URLIterator { |
| 118 public: | 120 public: |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 bool InitFromScratch(bool suppress_rebuild); | 315 bool InitFromScratch(bool suppress_rebuild); |
| 314 | 316 |
| 315 // Allocates the Fingerprint structure and length. Structure is filled with 0s | 317 // Allocates the Fingerprint structure and length. Structure is filled with 0s |
| 316 // and shared header with salt and used_items_ is set to 0. | 318 // and shared header with salt and used_items_ is set to 0. |
| 317 bool CreateURLTable(int32_t num_entries); | 319 bool CreateURLTable(int32_t num_entries); |
| 318 | 320 |
| 319 // Allocates the Fingerprint structure and length. Returns true on success. | 321 // Allocates the Fingerprint structure and length. Returns true on success. |
| 320 // Structure is filled with 0s and shared header with salt. The result of | 322 // Structure is filled with 0s and shared header with salt. The result of |
| 321 // allocation is saved into |shared_memory| and |hash_table| points to the | 323 // allocation is saved into |shared_memory| and |hash_table| points to the |
| 322 // beginning of Fingerprint table in |shared_memory|. | 324 // beginning of Fingerprint table in |shared_memory|. |
| 323 static bool CreateApartURLTable( | 325 static bool CreateApartURLTable(int32_t num_entries, |
| 324 int32_t num_entries, | 326 const uint8_t salt[LINK_SALT_LENGTH], |
| 325 const uint8_t salt[LINK_SALT_LENGTH], | 327 mojo::ScopedSharedBufferHandle* shared_memory, |
| 326 std::unique_ptr<base::SharedMemory>* shared_memory, | 328 mojo::ScopedSharedBufferMapping* hash_table); |
| 327 VisitedLinkCommon::Fingerprint** hash_table); | |
| 328 | 329 |
| 329 // A wrapper for CreateURLTable, this will allocate a new table, initialized | 330 // A wrapper for CreateURLTable, this will allocate a new table, initialized |
| 330 // to empty. The caller is responsible for saving the shared memory pointer | 331 // to empty. The caller is responsible for saving the shared memory pointer |
| 331 // and handles before this call (they will be replaced with new ones) and | 332 // and handles before this call (they will be replaced with new ones) and |
| 332 // releasing them later. This is designed for callers that make a new table | 333 // releasing them later. This is designed for callers that make a new table |
| 333 // and then copy values from the old table to the new one, then release the | 334 // and then copy values from the old table to the new one, then release the |
| 334 // old table. | 335 // old table. |
| 335 // | 336 // |
| 336 // Returns true on success. On failure, the old table will be restored. The | 337 // Returns true on success. On failure, the old table will be restored. The |
| 337 // caller should not attemp to release the pointer/handle in this case. | 338 // caller should not attemp to release the pointer/handle in this case. |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 if (hash >= table_length_ - 1) | 384 if (hash >= table_length_ - 1) |
| 384 return 0; // Wrap around. | 385 return 0; // Wrap around. |
| 385 return hash + 1; | 386 return hash + 1; |
| 386 } | 387 } |
| 387 inline Hash DecrementHash(Hash hash) { | 388 inline Hash DecrementHash(Hash hash) { |
| 388 if (hash <= 0) | 389 if (hash <= 0) |
| 389 return table_length_ - 1; // Wrap around. | 390 return table_length_ - 1; // Wrap around. |
| 390 return hash - 1; | 391 return hash - 1; |
| 391 } | 392 } |
| 392 | 393 |
| 394 // Returns a pointer to the start of the hash table, given the mapping |
| 395 // containing the hash table. |
| 396 static Fingerprint* GetHashTableFromMapping( |
| 397 const mojo::ScopedSharedBufferMapping& hash_table_mapping); |
| 398 |
| 393 // Reference to the browser context that this object belongs to | 399 // Reference to the browser context that this object belongs to |
| 394 // (it knows the path to where the data is stored) | 400 // (it knows the path to where the data is stored) |
| 395 content::BrowserContext* browser_context_; | 401 content::BrowserContext* browser_context_; |
| 396 | 402 |
| 397 // Client owns the delegate and is responsible for it being valid through | 403 // Client owns the delegate and is responsible for it being valid through |
| 398 // the life time this VisitedLinkMaster. | 404 // the life time this VisitedLinkMaster. |
| 399 VisitedLinkDelegate* delegate_; | 405 VisitedLinkDelegate* delegate_; |
| 400 | 406 |
| 401 // VisitedLinkEventListener to handle incoming events. | 407 // VisitedLinkEventListener to handle incoming events. |
| 402 std::unique_ptr<Listener> listener_; | 408 std::unique_ptr<Listener> listener_; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 428 // guaranteed to be executed after the opening. | 434 // guaranteed to be executed after the opening. |
| 429 // The class owns both the |file_| pointer and the pointer pointed | 435 // The class owns both the |file_| pointer and the pointer pointed |
| 430 // by |*file_|. | 436 // by |*file_|. |
| 431 FILE** file_; | 437 FILE** file_; |
| 432 | 438 |
| 433 // If true, will try to persist the hash table to disk. Will rebuild from | 439 // If true, will try to persist the hash table to disk. Will rebuild from |
| 434 // VisitedLinkDelegate::RebuildTable if there are disk corruptions. | 440 // VisitedLinkDelegate::RebuildTable if there are disk corruptions. |
| 435 bool persist_to_disk_; | 441 bool persist_to_disk_; |
| 436 | 442 |
| 437 // Shared memory consists of a SharedHeader followed by the table. | 443 // Shared memory consists of a SharedHeader followed by the table. |
| 438 base::SharedMemory *shared_memory_; | 444 mojo::ScopedSharedBufferHandle shared_memory_; |
| 445 |
| 446 // A mapping of the table including the SharedHeader. |
| 447 // GetHashTableFromMapping() can be used to obtain a pointer to the hash table |
| 448 // contained in this mapping. |
| 449 mojo::ScopedSharedBufferMapping hash_table_mapping_; |
| 439 | 450 |
| 440 // When we generate new tables, we increment the serial number of the | 451 // When we generate new tables, we increment the serial number of the |
| 441 // shared memory object. | 452 // shared memory object. |
| 442 int32_t shared_memory_serial_; | 453 int32_t shared_memory_serial_; |
| 443 | 454 |
| 444 // Number of non-empty items in the table, used to compute fullness. | 455 // Number of non-empty items in the table, used to compute fullness. |
| 445 int32_t used_items_; | 456 int32_t used_items_; |
| 446 | 457 |
| 447 // We set this to true to avoid writing to the database file. | 458 // We set this to true to avoid writing to the database file. |
| 448 bool table_is_loading_from_file_; | 459 bool table_is_loading_from_file_; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 485 if (hash_table_[i]) | 496 if (hash_table_[i]) |
| 486 used_count++; | 497 used_count++; |
| 487 } | 498 } |
| 488 DCHECK_EQ(used_count, used_items_); | 499 DCHECK_EQ(used_count, used_items_); |
| 489 } | 500 } |
| 490 #endif | 501 #endif |
| 491 | 502 |
| 492 } // namespace visitedlink | 503 } // namespace visitedlink |
| 493 | 504 |
| 494 #endif // COMPONENTS_VISITEDLINK_BROWSER_VISITEDLINK_MASTER_H_ | 505 #endif // COMPONENTS_VISITEDLINK_BROWSER_VISITEDLINK_MASTER_H_ |
| OLD | NEW |