Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(554)

Side by Side Diff: components/visitedlink/browser/visitedlink_master.h

Issue 2048503002: Convert visitedlink to use mojo. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@message-mojom-magic
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 // |listener| may be non-null to inject a listener for tests. If non-null a
77 // default Listener will be used.
76 VisitedLinkMaster(content::BrowserContext* browser_context, 78 VisitedLinkMaster(content::BrowserContext* browser_context,
77 VisitedLinkDelegate* delegate, 79 VisitedLinkDelegate* delegate,
78 bool persist_to_disk); 80 bool persist_to_disk,
81 std::unique_ptr<Listener> listener = nullptr);
dcheng 2016/09/20 22:29:27 Nit: I think it's slightly better to just have Set
Sam McNally 2016/09/21 02:38:14 Done.
79 82
80 // In unit test mode, we allow the caller to optionally specify the database 83 // 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 84 // 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 85 // 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 86 // table size to test table resizing. If this parameter is 0, we will use the
84 // defaults. 87 // defaults.
85 // 88 //
86 // In the unit test mode, we also allow the caller to provide a history 89 // 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 90 // 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 91 // 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 92 // the main version, which will probably fail (which can be good for testing
90 // this failure mode). 93 // this failure mode).
91 // 94 //
92 // When |suppress_rebuild| is set, we'll not attempt to load data from 95 // 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 96 // 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. 97 // testing except when you want to test the rebuild process explicitly.
95 VisitedLinkMaster(Listener* listener, 98 VisitedLinkMaster(Listener* listener,
96 VisitedLinkDelegate* delegate, 99 VisitedLinkDelegate* delegate,
97 bool persist_to_disk, 100 bool persist_to_disk,
98 bool suppress_rebuild, 101 bool suppress_rebuild,
99 const base::FilePath& filename, 102 const base::FilePath& filename,
100 int32_t default_table_size); 103 int32_t default_table_size);
101 ~VisitedLinkMaster() override; 104 ~VisitedLinkMaster() override;
102 105
103 // Must be called immediately after object creation. Nothing else will work 106 // Must be called immediately after object creation. Nothing else will work
104 // until this is called. Returns true on success, false means that this 107 // until this is called. Returns true on success, false means that this
105 // object won't work. 108 // object won't work.
106 bool Init(); 109 bool Init();
107 110
108 base::SharedMemory* shared_memory() { return shared_memory_; } 111 const mojo::SharedBufferHandle& shared_memory() {
112 return shared_memory_.get();
113 }
109 114
110 // Adds a URL to the table. 115 // Adds a URL to the table.
111 void AddURL(const GURL& url); 116 void AddURL(const GURL& url);
112 117
113 // Adds a set of URLs to the table. 118 // Adds a set of URLs to the table.
114 void AddURLs(const std::vector<GURL>& urls); 119 void AddURLs(const std::vector<GURL>& urls);
115 120
116 // See DeleteURLs. 121 // See DeleteURLs.
117 class URLIterator { 122 class URLIterator {
118 public: 123 public:
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 bool InitFromScratch(bool suppress_rebuild); 318 bool InitFromScratch(bool suppress_rebuild);
314 319
315 // Allocates the Fingerprint structure and length. Structure is filled with 0s 320 // Allocates the Fingerprint structure and length. Structure is filled with 0s
316 // and shared header with salt and used_items_ is set to 0. 321 // and shared header with salt and used_items_ is set to 0.
317 bool CreateURLTable(int32_t num_entries); 322 bool CreateURLTable(int32_t num_entries);
318 323
319 // Allocates the Fingerprint structure and length. Returns true on success. 324 // Allocates the Fingerprint structure and length. Returns true on success.
320 // Structure is filled with 0s and shared header with salt. The result of 325 // 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 326 // allocation is saved into |shared_memory| and |hash_table| points to the
322 // beginning of Fingerprint table in |shared_memory|. 327 // beginning of Fingerprint table in |shared_memory|.
323 static bool CreateApartURLTable( 328 static bool CreateApartURLTable(int32_t num_entries,
324 int32_t num_entries, 329 const uint8_t salt[LINK_SALT_LENGTH],
325 const uint8_t salt[LINK_SALT_LENGTH], 330 mojo::ScopedSharedBufferHandle* shared_memory,
326 std::unique_ptr<base::SharedMemory>* shared_memory, 331 mojo::ScopedSharedBufferMapping* hash_table);
327 VisitedLinkCommon::Fingerprint** hash_table);
328 332
329 // A wrapper for CreateURLTable, this will allocate a new table, initialized 333 // A wrapper for CreateURLTable, this will allocate a new table, initialized
330 // to empty. The caller is responsible for saving the shared memory pointer 334 // 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 335 // 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 336 // 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 337 // and then copy values from the old table to the new one, then release the
334 // old table. 338 // old table.
335 // 339 //
336 // Returns true on success. On failure, the old table will be restored. The 340 // 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. 341 // caller should not attemp to release the pointer/handle in this case.
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 if (hash >= table_length_ - 1) 387 if (hash >= table_length_ - 1)
384 return 0; // Wrap around. 388 return 0; // Wrap around.
385 return hash + 1; 389 return hash + 1;
386 } 390 }
387 inline Hash DecrementHash(Hash hash) { 391 inline Hash DecrementHash(Hash hash) {
388 if (hash <= 0) 392 if (hash <= 0)
389 return table_length_ - 1; // Wrap around. 393 return table_length_ - 1; // Wrap around.
390 return hash - 1; 394 return hash - 1;
391 } 395 }
392 396
397 // Returns a pointer to the start of the hash table, given the mapping
398 // containing the hash table.
399 static Fingerprint* GetHashTableFromMapping(
400 const mojo::ScopedSharedBufferMapping& hash_table_mapping);
401
393 // Reference to the browser context that this object belongs to 402 // Reference to the browser context that this object belongs to
394 // (it knows the path to where the data is stored) 403 // (it knows the path to where the data is stored)
395 content::BrowserContext* browser_context_; 404 content::BrowserContext* browser_context_;
396 405
397 // Client owns the delegate and is responsible for it being valid through 406 // Client owns the delegate and is responsible for it being valid through
398 // the life time this VisitedLinkMaster. 407 // the life time this VisitedLinkMaster.
399 VisitedLinkDelegate* delegate_; 408 VisitedLinkDelegate* delegate_;
400 409
401 // VisitedLinkEventListener to handle incoming events. 410 // VisitedLinkEventListener to handle incoming events.
402 std::unique_ptr<Listener> listener_; 411 std::unique_ptr<Listener> listener_;
(...skipping 25 matching lines...) Expand all
428 // guaranteed to be executed after the opening. 437 // guaranteed to be executed after the opening.
429 // The class owns both the |file_| pointer and the pointer pointed 438 // The class owns both the |file_| pointer and the pointer pointed
430 // by |*file_|. 439 // by |*file_|.
431 FILE** file_; 440 FILE** file_;
432 441
433 // If true, will try to persist the hash table to disk. Will rebuild from 442 // If true, will try to persist the hash table to disk. Will rebuild from
434 // VisitedLinkDelegate::RebuildTable if there are disk corruptions. 443 // VisitedLinkDelegate::RebuildTable if there are disk corruptions.
435 bool persist_to_disk_; 444 bool persist_to_disk_;
436 445
437 // Shared memory consists of a SharedHeader followed by the table. 446 // Shared memory consists of a SharedHeader followed by the table.
438 base::SharedMemory *shared_memory_; 447 mojo::ScopedSharedBufferHandle shared_memory_;
448
449 // A mapping of the table including the SharedHeader.
450 // GetHashTableFromMapping() can be used to obtain a pointer to the hash table
451 // contained in this mapping.
452 mojo::ScopedSharedBufferMapping hash_table_mapping_;
439 453
440 // When we generate new tables, we increment the serial number of the 454 // When we generate new tables, we increment the serial number of the
441 // shared memory object. 455 // shared memory object.
442 int32_t shared_memory_serial_; 456 int32_t shared_memory_serial_;
443 457
444 // Number of non-empty items in the table, used to compute fullness. 458 // Number of non-empty items in the table, used to compute fullness.
445 int32_t used_items_; 459 int32_t used_items_;
446 460
447 // We set this to true to avoid writing to the database file. 461 // We set this to true to avoid writing to the database file.
448 bool table_is_loading_from_file_; 462 bool table_is_loading_from_file_;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 if (hash_table_[i]) 499 if (hash_table_[i])
486 used_count++; 500 used_count++;
487 } 501 }
488 DCHECK_EQ(used_count, used_items_); 502 DCHECK_EQ(used_count, used_items_);
489 } 503 }
490 #endif 504 #endif
491 505
492 } // namespace visitedlink 506 } // namespace visitedlink
493 507
494 #endif // COMPONENTS_VISITEDLINK_BROWSER_VISITEDLINK_MASTER_H_ 508 #endif // COMPONENTS_VISITEDLINK_BROWSER_VISITEDLINK_MASTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698