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

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

Issue 11573060: Remove VisitedLink dependency on rest of chrome (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Use delegate instead Created 8 years 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 | Annotate | Revision Log
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 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"
18 #include "base/logging.h"
17 #include "base/memory/ref_counted.h" 19 #include "base/memory/ref_counted.h"
18 #include "base/shared_memory.h" 20 #include "base/shared_memory.h"
19 #include "base/threading/sequenced_worker_pool.h" 21 #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" 22 #include "chrome/common/visitedlink_common.h"
24 23
25 class GURL; 24 class GURL;
25 class VisitedLinkDelegate;
26
27 // TODO(boliu): Remove.
26 class Profile; 28 class Profile;
27 29
30 namespace content {
31 class BrowserContext;
32 } // namespace content
33
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 class URLIterator {
101 public:
102 virtual const GURL& next() = 0;
103 virtual bool has_next() const = 0;
104 };
105
96 // Deletes the specified URLs from |rows| from the table. 106 // Deletes the specified URLs from |rows| from the table.
97 void DeleteURLs(const history::URLRows& rows); 107 void DeleteURLs(URLIterator* iterator);
98 108
99 // Clears the visited links table by deleting the file from disk. Used as 109 // Clears the visited links table by deleting the file from disk. Used as
100 // part of history clearing. 110 // part of history clearing.
101 void DeleteAllURLs(); 111 void DeleteAllURLs();
102 112
113 VisitedLinkDelegate* GetDelegate();
114
103 #if defined(UNIT_TEST) || !defined(NDEBUG) || defined(PERF_TEST) 115 #if defined(UNIT_TEST) || !defined(NDEBUG) || defined(PERF_TEST)
104 // This is a debugging function that can be called to double-check internal 116 // This is a debugging function that can be called to double-check internal
105 // data structures. It will assert if the check fails. 117 // data structures. It will assert if the check fails.
106 void DebugValidate(); 118 void DebugValidate();
107 119
108 // Sets a task to execute when the next rebuild from history is complete. 120 // 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 121 // 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. 122 // they continue. The pointer will be owned by this object after the call.
111 void set_rebuild_complete_task(const base::Closure& task) { 123 void set_rebuild_complete_task(const base::Closure& task) {
112 DCHECK(rebuild_complete_task_.is_null()); 124 DCHECK(rebuild_complete_task_.is_null());
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 return static_cast<float>(used_items_) / static_cast<float>(table_length_); 294 return static_cast<float>(used_items_) / static_cast<float>(table_length_);
283 } 295 }
284 296
285 // Initializes a rebuild of the visited link database based on the browser 297 // 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 298 // 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 299 // already be a rebuild in place when called. See the definition for more
288 // details on how this works. 300 // details on how this works.
289 // 301 //
290 // Returns true on success. Failure means we're not attempting to rebuild 302 // Returns true on success. Failure means we're not attempting to rebuild
291 // the database because something failed. 303 // the database because something failed.
292 bool RebuildTableFromHistory(); 304 bool RebuildTableFromDelegate();
293 305
294 // Callback that the table rebuilder uses when the rebuild is complete. 306 // Callback that the table rebuilder uses when the rebuild is complete.
295 // |success| is true if the fingerprint generation succeeded, in which case 307 // |success| is true if the fingerprint generation succeeded, in which case
296 // |fingerprints| will contain the computed fingerprints. On failure, there 308 // |fingerprints| will contain the computed fingerprints. On failure, there
297 // will be no fingerprints. 309 // will be no fingerprints.
298 void OnTableRebuildComplete(bool success, 310 void OnTableRebuildComplete(bool success,
299 const std::vector<Fingerprint>& fingerprints); 311 const std::vector<Fingerprint>& fingerprints);
300 312
301 // Increases or decreases the given hash value by one, wrapping around as 313 // Increases or decreases the given hash value by one, wrapping around as
302 // necessary. Used for probing. 314 // necessary. Used for probing.
(...skipping 10 matching lines...) Expand all
313 325
314 #ifndef NDEBUG 326 #ifndef NDEBUG
315 // Indicates whether any asynchronous operation has ever been completed. 327 // Indicates whether any asynchronous operation has ever been completed.
316 // We do some synchronous reads that require that no asynchronous operations 328 // 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 329 // 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 330 // flag is a sanity check that these reads only happen before any
319 // asynchronous writes have been fired. 331 // asynchronous writes have been fired.
320 bool posted_asynchronous_operation_; 332 bool posted_asynchronous_operation_;
321 #endif 333 #endif
322 334
323 // Reference to the user profile that this object belongs to 335 // Reference to the browser context that this object belongs to
324 // (it knows the path to where the data is stored) 336 // (it knows the path to where the data is stored)
325 Profile* profile_; 337 content::BrowserContext* browser_context_;
338
339 VisitedLinkDelegate* delegate_;
joth 2012/12/20 01:39:29 // weak.
boliu 2012/12/29 01:48:12 Added lengthier comment.
326 340
327 // VisitedLinkEventListener to handle incoming events. 341 // VisitedLinkEventListener to handle incoming events.
328 scoped_ptr<Listener> listener_; 342 scoped_ptr<Listener> listener_;
329 343
330 // Lazily initialized sequence token for posting file tasks. 344 // Lazily initialized sequence token for posting file tasks.
331 base::SequencedWorkerPool::SequenceToken sequence_token_; 345 base::SequencedWorkerPool::SequenceToken sequence_token_;
332 346
333 // When non-NULL, indicates we are in database rebuild mode and points to 347 // When non-NULL, indicates we are in database rebuild mode and points to
334 // the class collecting fingerprint information from the history system. 348 // the class collecting fingerprint information from the history system.
335 // The pointer is owned by this class, but it must remain valid while the 349 // 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
373 // don't want to change the signature of the object between the unit test and 387 // 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 388 // regular builds. Instead, we just have "default" values that these take
375 // in release builds that give "regular" behavior. 389 // in release builds that give "regular" behavior.
376 390
377 // Overridden database file name for testing 391 // Overridden database file name for testing
378 FilePath database_name_override_; 392 FilePath database_name_override_;
379 393
380 // When nonzero, overrides the table size for new databases for testing 394 // When nonzero, overrides the table size for new databases for testing
381 int32 table_size_override_; 395 int32 table_size_override_;
382 396
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 397 // When set, indicates the task that should be run after the next rebuild from
388 // history is complete. 398 // history is complete.
389 base::Closure rebuild_complete_task_; 399 base::Closure rebuild_complete_task_;
390 400
391 // Set to prevent us from attempting to rebuild the database from global 401 // 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, 402 // history if we have an error opening the file. This is used for testing,
393 // will be false in production. 403 // will be false in production.
394 bool suppress_rebuild_; 404 bool suppress_rebuild_;
395 405
396 DISALLOW_COPY_AND_ASSIGN(VisitedLinkMaster); 406 DISALLOW_COPY_AND_ASSIGN(VisitedLinkMaster);
397 }; 407 };
398 408
399 // NOTE: These methods are defined inline here, so we can share the compilation 409 // 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. 410 // of visitedlink_master.cc between the browser and the unit/perf tests.
401 411
402 #if defined(UNIT_TEST) || defined(PERF_TEST) || !defined(NDEBUG) 412 #if defined(UNIT_TEST) || defined(PERF_TEST) || !defined(NDEBUG)
403 inline void VisitedLinkMaster::DebugValidate() { 413 inline void VisitedLinkMaster::DebugValidate() {
404 int32 used_count = 0; 414 int32 used_count = 0;
405 for (int32 i = 0; i < table_length_; i++) { 415 for (int32 i = 0; i < table_length_; i++) {
406 if (hash_table_[i]) 416 if (hash_table_[i])
407 used_count++; 417 used_count++;
408 } 418 }
409 DCHECK_EQ(used_count, used_items_); 419 DCHECK_EQ(used_count, used_items_);
410 } 420 }
411 #endif 421 #endif
412 422
413 #endif // CHROME_BROWSER_VISITEDLINK_VISITEDLINK_MASTER_H_ 423 #endif // CHROME_BROWSER_VISITEDLINK_VISITEDLINK_MASTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698