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