| 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 CHROME_BROWSER_VISITEDLINK_VISITEDLINK_MASTER_H_ | 5 #ifndef CHROME_BROWSER_VISITEDLINK_VISITEDLINK_MASTER_H_ |
| 6 #define CHROME_BROWSER_VISITEDLINK_VISITEDLINK_MASTER_H_ | 6 #define CHROME_BROWSER_VISITEDLINK_VISITEDLINK_MASTER_H_ |
| 7 | 7 |
| 8 #if defined(OS_WIN) | 8 #if defined(OS_WIN) |
| 9 #include <windows.h> | 9 #include <windows.h> |
| 10 #endif | 10 #endif |
| 11 #include <set> | 11 #include <set> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/callback.h" | |
| 15 #include "base/callback_forward.h" | 14 #include "base/callback_forward.h" |
| 16 #include "base/file_path.h" | 15 #include "base/file_path.h" |
| 17 #include "base/gtest_prod_util.h" | 16 #include "base/gtest_prod_util.h" |
| 18 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 19 #include "base/shared_memory.h" | 18 #include "base/shared_memory.h" |
| 20 #include "base/threading/sequenced_worker_pool.h" | 19 #include "base/threading/sequenced_worker_pool.h" |
| 20 #include "chrome/browser/history/history.h" |
| 21 #include "chrome/browser/history/history_types.h" |
| 22 #include "chrome/browser/profiles/profile_keyed_service.h" |
| 21 #include "chrome/common/visitedlink_common.h" | 23 #include "chrome/common/visitedlink_common.h" |
| 22 | 24 |
| 23 #if defined(UNIT_TEST) || defined(PERF_TEST) || !defined(NDEBUG) | |
| 24 #include "base/logging.h" | |
| 25 #endif | |
| 26 | |
| 27 class GURL; | 25 class GURL; |
| 28 class VisitedLinkDelegate; | 26 class Profile; |
| 29 | |
| 30 namespace content { | |
| 31 class BrowserContext; | |
| 32 } // namespace content | |
| 33 | |
| 34 | 27 |
| 35 // Controls the link coloring database. The master controls all writing to the | 28 // Controls the link coloring database. The master controls all writing to the |
| 36 // database as well as disk I/O. There should be only one master. | 29 // database as well as disk I/O. There should be only one master. |
| 37 // | 30 // |
| 38 // This class will defer writing operations to the file thread. This means that | 31 // This class will defer writing operations to the file thread. This means that |
| 39 // class destruction, the file may still be open since operations are pending on | 32 // class destruction, the file may still be open since operations are pending on |
| 40 // another thread. | 33 // another thread. |
| 41 class VisitedLinkMaster : public VisitedLinkCommon { | 34 class VisitedLinkMaster : public VisitedLinkCommon, |
| 35 public ProfileKeyedService { |
| 42 public: | 36 public: |
| 43 // Listens to the link coloring database events. The master is given this | 37 // Listens to the link coloring database events. The master is given this |
| 44 // event as a constructor argument and dispatches events using it. | 38 // event as a constructor argument and dispatches events using it. |
| 45 class Listener { | 39 class Listener { |
| 46 public: | 40 public: |
| 47 virtual ~Listener() {} | 41 virtual ~Listener() {} |
| 48 | 42 |
| 49 // Called when link coloring database has been created or replaced. The | 43 // Called when link coloring database has been created or replaced. The |
| 50 // argument is the new table handle. | 44 // argument is the new table handle. |
| 51 virtual void NewTable(base::SharedMemory*) = 0; | 45 virtual void NewTable(base::SharedMemory*) = 0; |
| 52 | 46 |
| 53 // Called when new link has been added. The argument is the fingerprint | 47 // Called when new link has been added. The argument is the fingerprint |
| 54 // (hash) of the link. | 48 // (hash) of the link. |
| 55 virtual void Add(Fingerprint fingerprint) = 0; | 49 virtual void Add(Fingerprint fingerprint) = 0; |
| 56 | 50 |
| 57 // Called when link coloring state has been reset. This may occur when | 51 // Called when link coloring state has been reset. This may occur when |
| 58 // entire or parts of history were deleted. | 52 // entire or parts of history were deleted. |
| 59 virtual void Reset() = 0; | 53 virtual void Reset() = 0; |
| 60 }; | 54 }; |
| 61 | 55 |
| 62 VisitedLinkMaster(content::BrowserContext* browser_context, | 56 explicit VisitedLinkMaster(Profile* profile); |
| 63 VisitedLinkDelegate* delegate); | |
| 64 | 57 |
| 65 // In unit test mode, we allow the caller to optionally specify the database | 58 // In unit test mode, we allow the caller to optionally specify the database |
| 66 // filename so that it can be run from a unit test. The directory where this | 59 // filename so that it can be run from a unit test. The directory where this |
| 67 // file resides must exist in this mode. You can also specify the default | 60 // file resides must exist in this mode. You can also specify the default |
| 68 // table size to test table resizing. If this parameter is 0, we will use the | 61 // table size to test table resizing. If this parameter is 0, we will use the |
| 69 // defaults. | 62 // defaults. |
| 70 // | 63 // |
| 71 // In the unit test mode, we also allow the caller to provide a history | 64 // In the unit test mode, we also allow the caller to provide a history |
| 72 // service pointer (the history service can't be fetched from the browser | 65 // service pointer (the history service can't be fetched from the browser |
| 73 // process when we're in unit test mode). This can be NULL to try to access | 66 // process when we're in unit test mode). This can be NULL to try to access |
| 74 // the main version, which will probably fail (which can be good for testing | 67 // the main version, which will probably fail (which can be good for testing |
| 75 // this failure mode). | 68 // this failure mode). |
| 76 // | 69 // |
| 77 // When |suppress_rebuild| is set, we'll not attempt to load data from | 70 // When |suppress_rebuild| is set, we'll not attempt to load data from |
| 78 // history if the file can't be loaded. This should generally be set for | 71 // history if the file can't be loaded. This should generally be set for |
| 79 // testing except when you want to test the rebuild process explicitly. | 72 // testing except when you want to test the rebuild process explicitly. |
| 80 VisitedLinkMaster(Listener* listener, | 73 VisitedLinkMaster(Listener* listener, |
| 81 VisitedLinkDelegate* delegate, | 74 HistoryService* history_service, |
| 82 bool suppress_rebuild, | 75 bool suppress_rebuild, |
| 83 const FilePath& filename, | 76 const FilePath& filename, |
| 84 int32 default_table_size); | 77 int32 default_table_size); |
| 85 virtual ~VisitedLinkMaster(); | 78 virtual ~VisitedLinkMaster(); |
| 86 | 79 |
| 80 // Return the VisitedLinkMaster instance for a profile. |
| 81 static VisitedLinkMaster* FromProfile(Profile* profile); |
| 82 |
| 87 // Must be called immediately after object creation. Nothing else will work | 83 // Must be called immediately after object creation. Nothing else will work |
| 88 // until this is called. Returns true on success, false means that this | 84 // until this is called. Returns true on success, false means that this |
| 89 // object won't work. | 85 // object won't work. |
| 90 bool Init(); | 86 bool Init(); |
| 91 | 87 |
| 92 base::SharedMemory* shared_memory() { return shared_memory_; } | 88 base::SharedMemory* shared_memory() { return shared_memory_; } |
| 93 | 89 |
| 94 // Adds a URL to the table. | 90 // Adds a URL to the table. |
| 95 void AddURL(const GURL& url); | 91 void AddURL(const GURL& url); |
| 96 | 92 |
| 97 // Adds a set of URLs to the table. | 93 // Adds a set of URLs to the table. |
| 98 void AddURLs(const std::vector<GURL>& url); | 94 void AddURLs(const std::vector<GURL>& url); |
| 99 | 95 |
| 100 // See DeleteURLs. | |
| 101 class URLIterator { | |
| 102 public: | |
| 103 // HasNextURL must return true when this is called. Returns the next URL | |
| 104 // then advances the iterator. Note that the returned reference is only | |
| 105 // valid until the next call of NextURL. | |
| 106 virtual const GURL& NextURL() = 0; | |
| 107 | |
| 108 // Returns true if still has URLs to be iterated. | |
| 109 virtual bool HasNextURL() const = 0; | |
| 110 | |
| 111 protected: | |
| 112 virtual ~URLIterator() {} | |
| 113 }; | |
| 114 | |
| 115 // Deletes the specified URLs from |rows| from the table. | 96 // Deletes the specified URLs from |rows| from the table. |
| 116 void DeleteURLs(URLIterator* iterator); | 97 void DeleteURLs(const history::URLRows& rows); |
| 117 | 98 |
| 118 // Clears the visited links table by deleting the file from disk. Used as | 99 // Clears the visited links table by deleting the file from disk. Used as |
| 119 // part of history clearing. | 100 // part of history clearing. |
| 120 void DeleteAllURLs(); | 101 void DeleteAllURLs(); |
| 121 | 102 |
| 122 // Returns the Delegate of this Master. | |
| 123 VisitedLinkDelegate* GetDelegate(); | |
| 124 | |
| 125 #if defined(UNIT_TEST) || !defined(NDEBUG) || defined(PERF_TEST) | 103 #if defined(UNIT_TEST) || !defined(NDEBUG) || defined(PERF_TEST) |
| 126 // This is a debugging function that can be called to double-check internal | 104 // This is a debugging function that can be called to double-check internal |
| 127 // data structures. It will assert if the check fails. | 105 // data structures. It will assert if the check fails. |
| 128 void DebugValidate(); | 106 void DebugValidate(); |
| 129 | 107 |
| 130 // Sets a task to execute when the next rebuild from history is complete. | 108 // Sets a task to execute when the next rebuild from history is complete. |
| 131 // This is used by unit tests to wait for the rebuild to complete before | 109 // This is used by unit tests to wait for the rebuild to complete before |
| 132 // they continue. The pointer will be owned by this object after the call. | 110 // they continue. The pointer will be owned by this object after the call. |
| 133 void set_rebuild_complete_task(const base::Closure& task) { | 111 void set_rebuild_complete_task(const base::Closure& task) { |
| 134 DCHECK(rebuild_complete_task_.is_null()); | 112 DCHECK(rebuild_complete_task_.is_null()); |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 return static_cast<float>(used_items_) / static_cast<float>(table_length_); | 282 return static_cast<float>(used_items_) / static_cast<float>(table_length_); |
| 305 } | 283 } |
| 306 | 284 |
| 307 // Initializes a rebuild of the visited link database based on the browser | 285 // Initializes a rebuild of the visited link database based on the browser |
| 308 // history. This will set table_builder_ while working, and there should not | 286 // history. This will set table_builder_ while working, and there should not |
| 309 // already be a rebuild in place when called. See the definition for more | 287 // already be a rebuild in place when called. See the definition for more |
| 310 // details on how this works. | 288 // details on how this works. |
| 311 // | 289 // |
| 312 // Returns true on success. Failure means we're not attempting to rebuild | 290 // Returns true on success. Failure means we're not attempting to rebuild |
| 313 // the database because something failed. | 291 // the database because something failed. |
| 314 bool RebuildTableFromDelegate(); | 292 bool RebuildTableFromHistory(); |
| 315 | 293 |
| 316 // Callback that the table rebuilder uses when the rebuild is complete. | 294 // Callback that the table rebuilder uses when the rebuild is complete. |
| 317 // |success| is true if the fingerprint generation succeeded, in which case | 295 // |success| is true if the fingerprint generation succeeded, in which case |
| 318 // |fingerprints| will contain the computed fingerprints. On failure, there | 296 // |fingerprints| will contain the computed fingerprints. On failure, there |
| 319 // will be no fingerprints. | 297 // will be no fingerprints. |
| 320 void OnTableRebuildComplete(bool success, | 298 void OnTableRebuildComplete(bool success, |
| 321 const std::vector<Fingerprint>& fingerprints); | 299 const std::vector<Fingerprint>& fingerprints); |
| 322 | 300 |
| 323 // Increases or decreases the given hash value by one, wrapping around as | 301 // Increases or decreases the given hash value by one, wrapping around as |
| 324 // necessary. Used for probing. | 302 // necessary. Used for probing. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 335 | 313 |
| 336 #ifndef NDEBUG | 314 #ifndef NDEBUG |
| 337 // Indicates whether any asynchronous operation has ever been completed. | 315 // Indicates whether any asynchronous operation has ever been completed. |
| 338 // We do some synchronous reads that require that no asynchronous operations | 316 // We do some synchronous reads that require that no asynchronous operations |
| 339 // are pending, yet we don't track whether they have been completed. This | 317 // are pending, yet we don't track whether they have been completed. This |
| 340 // flag is a sanity check that these reads only happen before any | 318 // flag is a sanity check that these reads only happen before any |
| 341 // asynchronous writes have been fired. | 319 // asynchronous writes have been fired. |
| 342 bool posted_asynchronous_operation_; | 320 bool posted_asynchronous_operation_; |
| 343 #endif | 321 #endif |
| 344 | 322 |
| 345 // Reference to the browser context that this object belongs to | 323 // Reference to the user profile that this object belongs to |
| 346 // (it knows the path to where the data is stored) | 324 // (it knows the path to where the data is stored) |
| 347 content::BrowserContext* browser_context_; | 325 Profile* profile_; |
| 348 | |
| 349 // Client owns the delegate and is responsible for it being valid through | |
| 350 // the life time this VisitedLinkMaster. | |
| 351 VisitedLinkDelegate* delegate_; | |
| 352 | 326 |
| 353 // VisitedLinkEventListener to handle incoming events. | 327 // VisitedLinkEventListener to handle incoming events. |
| 354 scoped_ptr<Listener> listener_; | 328 scoped_ptr<Listener> listener_; |
| 355 | 329 |
| 356 // Lazily initialized sequence token for posting file tasks. | 330 // Lazily initialized sequence token for posting file tasks. |
| 357 base::SequencedWorkerPool::SequenceToken sequence_token_; | 331 base::SequencedWorkerPool::SequenceToken sequence_token_; |
| 358 | 332 |
| 359 // When non-NULL, indicates we are in database rebuild mode and points to | 333 // When non-NULL, indicates we are in database rebuild mode and points to |
| 360 // the class collecting fingerprint information from the history system. | 334 // the class collecting fingerprint information from the history system. |
| 361 // The pointer is owned by this class, but it must remain valid while the | 335 // The pointer is owned by this class, but it must remain valid while the |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 399 // don't want to change the signature of the object between the unit test and | 373 // don't want to change the signature of the object between the unit test and |
| 400 // regular builds. Instead, we just have "default" values that these take | 374 // regular builds. Instead, we just have "default" values that these take |
| 401 // in release builds that give "regular" behavior. | 375 // in release builds that give "regular" behavior. |
| 402 | 376 |
| 403 // Overridden database file name for testing | 377 // Overridden database file name for testing |
| 404 FilePath database_name_override_; | 378 FilePath database_name_override_; |
| 405 | 379 |
| 406 // When nonzero, overrides the table size for new databases for testing | 380 // When nonzero, overrides the table size for new databases for testing |
| 407 int32 table_size_override_; | 381 int32 table_size_override_; |
| 408 | 382 |
| 383 // When non-NULL, overrides the history service to use rather than as the |
| 384 // BrowserProcess. This is provided for unit tests. |
| 385 HistoryService* history_service_override_; |
| 386 |
| 409 // When set, indicates the task that should be run after the next rebuild from | 387 // When set, indicates the task that should be run after the next rebuild from |
| 410 // history is complete. | 388 // history is complete. |
| 411 base::Closure rebuild_complete_task_; | 389 base::Closure rebuild_complete_task_; |
| 412 | 390 |
| 413 // Set to prevent us from attempting to rebuild the database from global | 391 // Set to prevent us from attempting to rebuild the database from global |
| 414 // history if we have an error opening the file. This is used for testing, | 392 // history if we have an error opening the file. This is used for testing, |
| 415 // will be false in production. | 393 // will be false in production. |
| 416 bool suppress_rebuild_; | 394 bool suppress_rebuild_; |
| 417 | 395 |
| 418 DISALLOW_COPY_AND_ASSIGN(VisitedLinkMaster); | 396 DISALLOW_COPY_AND_ASSIGN(VisitedLinkMaster); |
| 419 }; | 397 }; |
| 420 | 398 |
| 421 // NOTE: These methods are defined inline here, so we can share the compilation | 399 // NOTE: These methods are defined inline here, so we can share the compilation |
| 422 // of visitedlink_master.cc between the browser and the unit/perf tests. | 400 // of visitedlink_master.cc between the browser and the unit/perf tests. |
| 423 | 401 |
| 424 #if defined(UNIT_TEST) || defined(PERF_TEST) || !defined(NDEBUG) | 402 #if defined(UNIT_TEST) || defined(PERF_TEST) || !defined(NDEBUG) |
| 425 inline void VisitedLinkMaster::DebugValidate() { | 403 inline void VisitedLinkMaster::DebugValidate() { |
| 426 int32 used_count = 0; | 404 int32 used_count = 0; |
| 427 for (int32 i = 0; i < table_length_; i++) { | 405 for (int32 i = 0; i < table_length_; i++) { |
| 428 if (hash_table_[i]) | 406 if (hash_table_[i]) |
| 429 used_count++; | 407 used_count++; |
| 430 } | 408 } |
| 431 DCHECK_EQ(used_count, used_items_); | 409 DCHECK_EQ(used_count, used_items_); |
| 432 } | 410 } |
| 433 #endif | 411 #endif |
| 434 | 412 |
| 435 #endif // CHROME_BROWSER_VISITEDLINK_VISITEDLINK_MASTER_H_ | 413 #endif // CHROME_BROWSER_VISITEDLINK_VISITEDLINK_MASTER_H_ |
| OLD | NEW |