OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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_MASTER_H__ | 5 #ifndef CHROME_BROWSER_VISITEDLINK_MASTER_H__ |
6 #define CHROME_BROWSER_VISITEDLINK_MASTER_H__ | 6 #define CHROME_BROWSER_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 <string> | 12 #include <string> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/file_path.h" | 15 #include "base/file_path.h" |
16 #include "base/ref_counted.h" | 16 #include "base/ref_counted.h" |
17 #include "base/shared_memory.h" | 17 #include "base/shared_memory.h" |
18 #include "chrome/browser/history/history.h" | 18 #include "chrome/browser/history/history.h" |
19 #include "chrome/common/visitedlink_common.h" | 19 #include "chrome/common/visitedlink_common.h" |
20 #include "testing/gtest/include/gtest/gtest_prod.h" | 20 #include "testing/gtest/include/gtest/gtest_prod.h" |
21 | 21 |
22 class GURL; | 22 class GURL; |
23 class MessageLoop; | 23 class MessageLoop; |
24 class Profile; | 24 class Profile; |
25 | 25 |
26 namespace base { | |
27 class Thread; | |
28 } // namespace base | |
29 | |
30 // Controls the link coloring database. The master controls all writing to the | 26 // Controls the link coloring database. The master controls all writing to the |
31 // database as well as disk I/O. There should be only one master. | 27 // database as well as disk I/O. There should be only one master. |
32 // | 28 // |
33 // This class will optionally defer writing operations to another thread. This | 29 // This class will defer writing operations to the file thread. This means that |
34 // means that after class destruction, the file may still be open since | 30 // class destruction, the file may still be open since operations are pending on |
35 // operations are pending on another thread. | 31 // another thread. |
36 class VisitedLinkMaster : public VisitedLinkCommon { | 32 class VisitedLinkMaster : public VisitedLinkCommon { |
37 public: | 33 public: |
38 // Listens to the link coloring database events. The master is given this | 34 // Listens to the link coloring database events. The master is given this |
39 // event as a constructor argument and dispatches events using it. | 35 // event as a constructor argument and dispatches events using it. |
40 class Listener { | 36 class Listener { |
41 public: | 37 public: |
42 virtual ~Listener() {} | 38 virtual ~Listener() {} |
43 | 39 |
44 // Called when link coloring database has been created or replaced. The | 40 // Called when link coloring database has been created or replaced. The |
45 // argument is the new table handle. | 41 // argument is the new table handle. |
46 virtual void NewTable(base::SharedMemory*) = 0; | 42 virtual void NewTable(base::SharedMemory*) = 0; |
47 | 43 |
48 // Called when new link has been added. The argument is the fingerprint | 44 // Called when new link has been added. The argument is the fingerprint |
49 // (hash) of the link. | 45 // (hash) of the link. |
50 virtual void Add(Fingerprint fingerprint) = 0; | 46 virtual void Add(Fingerprint fingerprint) = 0; |
51 | 47 |
52 // Called when link coloring state has been reset. This may occur when | 48 // Called when link coloring state has been reset. This may occur when |
53 // entire or parts of history were deleted. | 49 // entire or parts of history were deleted. |
54 virtual void Reset() = 0; | 50 virtual void Reset() = 0; |
55 }; | 51 }; |
56 | 52 |
57 // The |file_thread| may be NULL, in which case write operations will be | |
58 // synchronous. | |
59 // The |listener| may not be NULL. | 53 // The |listener| may not be NULL. |
60 VisitedLinkMaster(base::Thread* file_thread, | 54 VisitedLinkMaster(Listener* listener, Profile* profile); |
61 Listener* listener, | |
62 Profile* profile); | |
63 | 55 |
64 // In unit test mode, we allow the caller to optionally specify the database | 56 // In unit test mode, we allow the caller to optionally specify the database |
65 // filename so that it can be run from a unit test. The directory where this | 57 // filename so that it can be run from a unit test. The directory where this |
66 // file resides must exist in this mode. You can also specify the default | 58 // file resides must exist in this mode. You can also specify the default |
67 // table size to test table resizing. If this parameter is 0, we will use the | 59 // table size to test table resizing. If this parameter is 0, we will use the |
68 // defaults. | 60 // defaults. |
69 // | 61 // |
70 // In the unit test mode, we also allow the caller to provide a history | 62 // In the unit test mode, we also allow the caller to provide a history |
71 // service pointer (the history service can't be fetched from the browser | 63 // service pointer (the history service can't be fetched from the browser |
72 // process when we're in unit test mode). This can be NULL to try to access | 64 // process when we're in unit test mode). This can be NULL to try to access |
73 // the main version, which will probably fail (which can be good for testing | 65 // the main version, which will probably fail (which can be good for testing |
74 // this failure mode). | 66 // this failure mode). |
75 // | 67 // |
76 // When |suppress_rebuild| is set, we'll not attempt to load data from | 68 // When |suppress_rebuild| is set, we'll not attempt to load data from |
77 // history if the file can't be loaded. This should generally be set for | 69 // history if the file can't be loaded. This should generally be set for |
78 // testing except when you want to test the rebuild process explicitly. | 70 // testing except when you want to test the rebuild process explicitly. |
79 VisitedLinkMaster(base::Thread* file_thread, | 71 VisitedLinkMaster(Listener* listener, |
80 Listener* listener, | |
81 HistoryService* history_service, | 72 HistoryService* history_service, |
82 bool suppress_rebuild, | 73 bool suppress_rebuild, |
83 const FilePath& filename, | 74 const FilePath& filename, |
84 int32 default_table_size); | 75 int32 default_table_size); |
85 virtual ~VisitedLinkMaster(); | 76 virtual ~VisitedLinkMaster(); |
86 | 77 |
87 // Must be called immediately after object creation. Nothing else will work | 78 // Must be called immediately after object creation. Nothing else will work |
88 // until this is called. Returns true on success, false means that this | 79 // until this is called. Returns true on success, false means that this |
89 // object won't work. | 80 // object won't work. |
90 bool Init(); | 81 bool Init(); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 | 152 |
162 // When creating a fresh new table, we use this many entries. | 153 // When creating a fresh new table, we use this many entries. |
163 static const unsigned kDefaultTableSize; | 154 static const unsigned kDefaultTableSize; |
164 | 155 |
165 // When the user is deleting a boatload of URLs, we don't really want to do | 156 // When the user is deleting a boatload of URLs, we don't really want to do |
166 // individual writes for each of them. When the count exceeds this threshold, | 157 // individual writes for each of them. When the count exceeds this threshold, |
167 // we will write the whole table to disk at once instead of individual items. | 158 // we will write the whole table to disk at once instead of individual items. |
168 static const size_t kBigDeleteThreshold; | 159 static const size_t kBigDeleteThreshold; |
169 | 160 |
170 // Backend for the constructors initializing the members. | 161 // Backend for the constructors initializing the members. |
171 void InitMembers(base::Thread* file_thread, | 162 void InitMembers(Listener* listener, Profile* profile); |
172 Listener* listener, | |
173 Profile* profile); | |
174 | 163 |
175 // If a rebuild is in progress, we save the URL in the temporary list. | 164 // If a rebuild is in progress, we save the URL in the temporary list. |
176 // Otherwise, we add this to the table. Returns the index of the | 165 // Otherwise, we add this to the table. Returns the index of the |
177 // inserted fingerprint or null_hash_ on failure. | 166 // inserted fingerprint or null_hash_ on failure. |
178 Hash TryToAddURL(const GURL& url); | 167 Hash TryToAddURL(const GURL& url); |
179 | 168 |
180 // File I/O functions | 169 // File I/O functions |
181 // ------------------ | 170 // ------------------ |
182 | 171 |
183 // Writes the entire table to disk, returning true on success. It will leave | 172 // Writes the entire table to disk, returning true on success. It will leave |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 | 307 |
319 #ifndef NDEBUG | 308 #ifndef NDEBUG |
320 // Indicates whether any asynchronous operation has ever been completed. | 309 // Indicates whether any asynchronous operation has ever been completed. |
321 // We do some synchronous reads that require that no asynchronous operations | 310 // We do some synchronous reads that require that no asynchronous operations |
322 // are pending, yet we don't track whether they have been completed. This | 311 // are pending, yet we don't track whether they have been completed. This |
323 // flag is a sanity check that these reads only happen before any | 312 // flag is a sanity check that these reads only happen before any |
324 // asynchronous writes have been fired. | 313 // asynchronous writes have been fired. |
325 bool posted_asynchronous_operation_; | 314 bool posted_asynchronous_operation_; |
326 #endif | 315 #endif |
327 | 316 |
328 // The thread where we do write operations from to avoid synchronous I/O on | |
329 // the main thread. This may be NULL, which indicates synchronous I/O. | |
330 MessageLoop* file_thread_; | |
331 | |
332 // Reference to the user profile that this object belongs to | 317 // Reference to the user profile that this object belongs to |
333 // (it knows the path to where the data is stored) | 318 // (it knows the path to where the data is stored) |
334 Profile* profile_; | 319 Profile* profile_; |
335 | 320 |
336 // When non-NULL, indicates we are in database rebuild mode and points to | 321 // When non-NULL, indicates we are in database rebuild mode and points to |
337 // the class collecting fingerprint information from the history system. | 322 // the class collecting fingerprint information from the history system. |
338 // The pointer is owned by this class, but it must remain valid while the | 323 // The pointer is owned by this class, but it must remain valid while the |
339 // history query is running. We must only delete it when the query is done. | 324 // history query is running. We must only delete it when the query is done. |
340 scoped_refptr<TableBuilder> table_builder_; | 325 scoped_refptr<TableBuilder> table_builder_; |
341 | 326 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 int32 used_count = 0; | 386 int32 used_count = 0; |
402 for (int32 i = 0; i < table_length_; i++) { | 387 for (int32 i = 0; i < table_length_; i++) { |
403 if (hash_table_[i]) | 388 if (hash_table_[i]) |
404 used_count++; | 389 used_count++; |
405 } | 390 } |
406 DCHECK_EQ(used_count, used_items_); | 391 DCHECK_EQ(used_count, used_items_); |
407 } | 392 } |
408 #endif | 393 #endif |
409 | 394 |
410 #endif // CHROME_BROWSER_VISITEDLINK_MASTER_H__ | 395 #endif // CHROME_BROWSER_VISITEDLINK_MASTER_H__ |
OLD | NEW |