| 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 |
| 72 // all cached visitedlink hashes need to be recalculated. | 72 // all cached visitedlink hashes need to be recalculated. |
| 73 virtual void Reset(bool invalidate_hashes) = 0; | 73 virtual void Reset(bool invalidate_hashes) = 0; |
| 74 }; | 74 }; |
| 75 | 75 |
| 76 VisitedLinkMaster(content::BrowserContext* browser_context, | 76 VisitedLinkMaster(content::BrowserContext* browser_context, |
| 77 VisitedLinkDelegate* delegate, | 77 VisitedLinkDelegate* delegate, |
| 78 bool persist_to_disk); | 78 bool persist_to_disk, |
| 79 std::unique_ptr<Listener> listener = nullptr); |
| 79 | 80 |
| 80 // In unit test mode, we allow the caller to optionally specify the database | 81 // In unit test mode, we allow the caller to optionally specify the database |
| 81 // filename so that it can be run from a unit test. The directory where this | 82 // filename so that it can be run from a unit test. The directory where this |
| 82 // file resides must exist in this mode. You can also specify the default | 83 // file resides must exist in this mode. You can also specify the default |
| 83 // table size to test table resizing. If this parameter is 0, we will use the | 84 // table size to test table resizing. If this parameter is 0, we will use the |
| 84 // defaults. | 85 // defaults. |
| 85 // | 86 // |
| 86 // In the unit test mode, we also allow the caller to provide a history | 87 // In the unit test mode, we also allow the caller to provide a history |
| 87 // service pointer (the history service can't be fetched from the browser | 88 // service pointer (the history service can't be fetched from the browser |
| 88 // process when we're in unit test mode). This can be NULL to try to access | 89 // process when we're in unit test mode). This can be NULL to try to access |
| 89 // the main version, which will probably fail (which can be good for testing | 90 // the main version, which will probably fail (which can be good for testing |
| 90 // this failure mode). | 91 // this failure mode). |
| 91 // | 92 // |
| 92 // When |suppress_rebuild| is set, we'll not attempt to load data from | 93 // When |suppress_rebuild| is set, we'll not attempt to load data from |
| 93 // history if the file can't be loaded. This should generally be set for | 94 // history if the file can't be loaded. This should generally be set for |
| 94 // testing except when you want to test the rebuild process explicitly. | 95 // testing except when you want to test the rebuild process explicitly. |
| 95 VisitedLinkMaster(Listener* listener, | 96 VisitedLinkMaster(Listener* listener, |
| 96 VisitedLinkDelegate* delegate, | 97 VisitedLinkDelegate* delegate, |
| 97 bool persist_to_disk, | 98 bool persist_to_disk, |
| 98 bool suppress_rebuild, | 99 bool suppress_rebuild, |
| 99 const base::FilePath& filename, | 100 const base::FilePath& filename, |
| 100 int32_t default_table_size); | 101 int32_t default_table_size); |
| 101 ~VisitedLinkMaster() override; | 102 ~VisitedLinkMaster() override; |
| 102 | 103 |
| 103 // Must be called immediately after object creation. Nothing else will work | 104 // Must be called immediately after object creation. Nothing else will work |
| 104 // until this is called. Returns true on success, false means that this | 105 // until this is called. Returns true on success, false means that this |
| 105 // object won't work. | 106 // object won't work. |
| 106 bool Init(); | 107 bool Init(); |
| 107 | 108 |
| 108 base::SharedMemory* shared_memory() { return shared_memory_; } | 109 const mojo::SharedBufferHandle& shared_memory() { |
| 110 return shared_memory_.get(); |
| 111 } |
| 109 | 112 |
| 110 // Adds a URL to the table. | 113 // Adds a URL to the table. |
| 111 void AddURL(const GURL& url); | 114 void AddURL(const GURL& url); |
| 112 | 115 |
| 113 // Adds a set of URLs to the table. | 116 // Adds a set of URLs to the table. |
| 114 void AddURLs(const std::vector<GURL>& urls); | 117 void AddURLs(const std::vector<GURL>& urls); |
| 115 | 118 |
| 116 // See DeleteURLs. | 119 // See DeleteURLs. |
| 117 class URLIterator { | 120 class URLIterator { |
| 118 public: | 121 public: |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 bool InitFromScratch(bool suppress_rebuild); | 316 bool InitFromScratch(bool suppress_rebuild); |
| 314 | 317 |
| 315 // Allocates the Fingerprint structure and length. Structure is filled with 0s | 318 // Allocates the Fingerprint structure and length. Structure is filled with 0s |
| 316 // and shared header with salt and used_items_ is set to 0. | 319 // and shared header with salt and used_items_ is set to 0. |
| 317 bool CreateURLTable(int32_t num_entries); | 320 bool CreateURLTable(int32_t num_entries); |
| 318 | 321 |
| 319 // Allocates the Fingerprint structure and length. Returns true on success. | 322 // Allocates the Fingerprint structure and length. Returns true on success. |
| 320 // Structure is filled with 0s and shared header with salt. The result of | 323 // 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 | 324 // allocation is saved into |shared_memory| and |hash_table| points to the |
| 322 // beginning of Fingerprint table in |shared_memory|. | 325 // beginning of Fingerprint table in |shared_memory|. |
| 323 static bool CreateApartURLTable( | 326 static bool CreateApartURLTable(int32_t num_entries, |
| 324 int32_t num_entries, | 327 const uint8_t salt[LINK_SALT_LENGTH], |
| 325 const uint8_t salt[LINK_SALT_LENGTH], | 328 mojo::ScopedSharedBufferHandle* shared_memory, |
| 326 std::unique_ptr<base::SharedMemory>* shared_memory, | 329 mojo::ScopedSharedBufferMapping* hash_table); |
| 327 VisitedLinkCommon::Fingerprint** hash_table); | |
| 328 | 330 |
| 329 // A wrapper for CreateURLTable, this will allocate a new table, initialized | 331 // A wrapper for CreateURLTable, this will allocate a new table, initialized |
| 330 // to empty. The caller is responsible for saving the shared memory pointer | 332 // 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 | 333 // 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 | 334 // 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 | 335 // and then copy values from the old table to the new one, then release the |
| 334 // old table. | 336 // old table. |
| 335 // | 337 // |
| 336 // Returns true on success. On failure, the old table will be restored. The | 338 // 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. | 339 // caller should not attemp to release the pointer/handle in this case. |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 // guaranteed to be executed after the opening. | 430 // guaranteed to be executed after the opening. |
| 429 // The class owns both the |file_| pointer and the pointer pointed | 431 // The class owns both the |file_| pointer and the pointer pointed |
| 430 // by |*file_|. | 432 // by |*file_|. |
| 431 FILE** file_; | 433 FILE** file_; |
| 432 | 434 |
| 433 // If true, will try to persist the hash table to disk. Will rebuild from | 435 // If true, will try to persist the hash table to disk. Will rebuild from |
| 434 // VisitedLinkDelegate::RebuildTable if there are disk corruptions. | 436 // VisitedLinkDelegate::RebuildTable if there are disk corruptions. |
| 435 bool persist_to_disk_; | 437 bool persist_to_disk_; |
| 436 | 438 |
| 437 // Shared memory consists of a SharedHeader followed by the table. | 439 // Shared memory consists of a SharedHeader followed by the table. |
| 438 base::SharedMemory *shared_memory_; | 440 mojo::ScopedSharedBufferHandle shared_memory_; |
| 441 |
| 442 // A mapping of the table without the SharedHeader. |
| 443 mojo::ScopedSharedBufferMapping hash_table_mapping_; |
| 439 | 444 |
| 440 // When we generate new tables, we increment the serial number of the | 445 // When we generate new tables, we increment the serial number of the |
| 441 // shared memory object. | 446 // shared memory object. |
| 442 int32_t shared_memory_serial_; | 447 int32_t shared_memory_serial_; |
| 443 | 448 |
| 444 // Number of non-empty items in the table, used to compute fullness. | 449 // Number of non-empty items in the table, used to compute fullness. |
| 445 int32_t used_items_; | 450 int32_t used_items_; |
| 446 | 451 |
| 447 // We set this to true to avoid writing to the database file. | 452 // We set this to true to avoid writing to the database file. |
| 448 bool table_is_loading_from_file_; | 453 bool table_is_loading_from_file_; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 485 if (hash_table_[i]) | 490 if (hash_table_[i]) |
| 486 used_count++; | 491 used_count++; |
| 487 } | 492 } |
| 488 DCHECK_EQ(used_count, used_items_); | 493 DCHECK_EQ(used_count, used_items_); |
| 489 } | 494 } |
| 490 #endif | 495 #endif |
| 491 | 496 |
| 492 } // namespace visitedlink | 497 } // namespace visitedlink |
| 493 | 498 |
| 494 #endif // COMPONENTS_VISITEDLINK_BROWSER_VISITEDLINK_MASTER_H_ | 499 #endif // COMPONENTS_VISITEDLINK_BROWSER_VISITEDLINK_MASTER_H_ |
| OLD | NEW |