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) |
| 9 #include <windows.h> |
| 10 #endif |
8 #include <set> | 11 #include <set> |
9 #include <string> | 12 #include <string> |
10 #include <vector> | 13 #include <vector> |
11 | 14 |
| 15 #include "base/file_path.h" |
12 #include "base/ref_counted.h" | 16 #include "base/ref_counted.h" |
13 #include "base/shared_memory.h" | 17 #include "base/shared_memory.h" |
| 18 #if defined(OS_WIN) |
14 #include "chrome/browser/history/history.h" | 19 #include "chrome/browser/history/history.h" |
| 20 #else |
| 21 // TODO(port): remove scaffolding, use history.h for both POSIX and WIN. |
| 22 #include "chrome/common/temp_scaffolding_stubs.h" |
| 23 #endif // !defined(OS_WIN) |
15 #include "chrome/common/visitedlink_common.h" | 24 #include "chrome/common/visitedlink_common.h" |
16 #include "testing/gtest/include/gtest/gtest_prod.h" | 25 #include "testing/gtest/include/gtest/gtest_prod.h" |
17 | 26 |
18 class GURL; | 27 class GURL; |
19 class Profile; | 28 class Profile; |
20 | 29 |
21 // Controls the link coloring database. The master controls all writing to the | 30 // Controls the link coloring database. The master controls all writing to the |
22 // database as well as disk I/O. There should be only one master. | 31 // database as well as disk I/O. There should be only one master. |
23 // | 32 // |
24 // This class will optionally defer writing operations to another thread. This | 33 // This class will optionally defer writing operations to another thread. This |
(...skipping 21 matching lines...) Expand all Loading... |
46 // the main version, which will probably fail (which can be good for testing | 55 // the main version, which will probably fail (which can be good for testing |
47 // this failure mode). | 56 // this failure mode). |
48 // | 57 // |
49 // When |suppress_rebuild| is set, we'll not attempt to load data from | 58 // When |suppress_rebuild| is set, we'll not attempt to load data from |
50 // history if the file can't be loaded. This should generally be set for | 59 // history if the file can't be loaded. This should generally be set for |
51 // testing except when you want to test the rebuild process explicitly. | 60 // testing except when you want to test the rebuild process explicitly. |
52 VisitedLinkMaster(base::Thread* file_thread, | 61 VisitedLinkMaster(base::Thread* file_thread, |
53 PostNewTableEvent* poster, | 62 PostNewTableEvent* poster, |
54 HistoryService* history_service, | 63 HistoryService* history_service, |
55 bool suppress_rebuild, | 64 bool suppress_rebuild, |
56 const std::wstring& filename, | 65 const FilePath& filename, |
57 int32 default_table_size); | 66 int32 default_table_size); |
58 virtual ~VisitedLinkMaster(); | 67 virtual ~VisitedLinkMaster(); |
59 | 68 |
60 // Must be called immediately after object creation. Nothing else will work | 69 // Must be called immediately after object creation. Nothing else will work |
61 // until this is called. Returns true on success, false means that this | 70 // until this is called. Returns true on success, false means that this |
62 // object won't work. | 71 // object won't work. |
63 bool Init(); | 72 bool Init(); |
64 | 73 |
65 // Duplicates the handle to the shared memory to another process. | 74 // Duplicates the handle to the shared memory to another process. |
66 // Returns true on success. | 75 // Returns true on success. |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 | 143 |
135 // Bytes in the file header, including the salt. | 144 // Bytes in the file header, including the salt. |
136 static const size_t kFileHeaderSize; | 145 static const size_t kFileHeaderSize; |
137 | 146 |
138 // When creating a fresh new table, we use this many entries. | 147 // When creating a fresh new table, we use this many entries. |
139 static const unsigned kDefaultTableSize; | 148 static const unsigned kDefaultTableSize; |
140 | 149 |
141 // When the user is deleting a boatload of URLs, we don't really want to do | 150 // When the user is deleting a boatload of URLs, we don't really want to do |
142 // individual writes for each of them. When the count exceeds this threshold, | 151 // individual writes for each of them. When the count exceeds this threshold, |
143 // we will write the whole table to disk at once instead of individual items. | 152 // we will write the whole table to disk at once instead of individual items. |
144 static const int32 kBigDeleteThreshold; | 153 static const size_t kBigDeleteThreshold; |
145 | 154 |
146 // Backend for the constructors initializing the members. | 155 // Backend for the constructors initializing the members. |
147 void InitMembers(base::Thread* file_thread, | 156 void InitMembers(base::Thread* file_thread, |
148 PostNewTableEvent* poster, | 157 PostNewTableEvent* poster, |
149 Profile* profile); | 158 Profile* profile); |
150 | 159 |
151 // If a rebuild is in progress, we save the URL in the temporary list. | 160 // If a rebuild is in progress, we save the URL in the temporary list. |
152 // Otherwise, we add this to the table. Returns the index of the | 161 // Otherwise, we add this to the table. Returns the index of the |
153 // inserted fingerprint or null_hash_ on failure. | 162 // inserted fingerprint or null_hash_ on failure. |
154 Hash TryToAddURL(const GURL& url); | 163 Hash TryToAddURL(const GURL& url); |
155 | 164 |
156 // File I/O functions | 165 // File I/O functions |
157 // ------------------ | 166 // ------------------ |
158 | 167 |
159 // Writes the entire table to disk, returning true on success. It will leave | 168 // Writes the entire table to disk, returning true on success. It will leave |
160 // the table file open and the handle to it in file_ | 169 // the table file open and the handle to it in file_ |
161 bool WriteFullTable(); | 170 bool WriteFullTable(); |
162 | 171 |
163 // Try to load the table from the database file. If the file doesn't exist or | 172 // Try to load the table from the database file. If the file doesn't exist or |
164 // is corrupt, this will return failure. | 173 // is corrupt, this will return failure. |
165 bool InitFromFile(); | 174 bool InitFromFile(); |
166 | 175 |
167 // Reads the header of the link coloring database from disk. Assumes the | 176 // Reads the header of the link coloring database from disk. Assumes the |
168 // file pointer is at the beginning of the file and that there are no pending | 177 // file pointer is at the beginning of the file and that there are no pending |
169 // asynchronous I/O operations. | 178 // asynchronous I/O operations. |
170 // | 179 // |
171 // Returns true on success and places the size of the table in num_entries | 180 // Returns true on success and places the size of the table in num_entries |
172 // and the number of nonzero fingerprints in used_count. This will fail if | 181 // and the number of nonzero fingerprints in used_count. This will fail if |
173 // the version of the file is not the current version of the database. | 182 // the version of the file is not the current version of the database. |
174 bool ReadFileHeader(HANDLE hfile, int32* num_entries, int32* used_count, | 183 bool ReadFileHeader(FILE* hfile, int32* num_entries, int32* used_count, |
175 uint8 salt[LINK_SALT_LENGTH]); | 184 uint8 salt[LINK_SALT_LENGTH]); |
176 | 185 |
177 // Fills *filename with the name of the link database filename | 186 // Fills *filename with the name of the link database filename |
178 bool GetDatabaseFileName(std::wstring* filename); | 187 bool GetDatabaseFileName(FilePath* filename); |
179 | 188 |
180 // Wrapper around Window's WriteFile using asynchronous I/O. This will proxy | 189 // Wrapper around Window's WriteFile using asynchronous I/O. This will proxy |
181 // the write to a background thread. | 190 // the write to a background thread. |
182 void WriteToFile(HANDLE hfile, int32 offset, void* data, int32 data_size); | 191 void WriteToFile(FILE* hfile, off_t offset, void* data, int32 data_size); |
183 | 192 |
184 // Helper function to schedule and asynchronous write of the used count to | 193 // Helper function to schedule and asynchronous write of the used count to |
185 // disk (this is a common operation). | 194 // disk (this is a common operation). |
186 void WriteUsedItemCountToFile(); | 195 void WriteUsedItemCountToFile(); |
187 | 196 |
188 // Helper function to schedule an asynchronous write of the given range of | 197 // Helper function to schedule an asynchronous write of the given range of |
189 // hash functions to disk. The range is inclusive on both ends. The range can | 198 // hash functions to disk. The range is inclusive on both ends. The range can |
190 // wrap around at 0 and this function will handle it. | 199 // wrap around at 0 and this function will handle it. |
191 void WriteHashRangeToFile(Hash first_hash, Hash last_hash); | 200 void WriteHashRangeToFile(Hash first_hash, Hash last_hash); |
192 | 201 |
193 // Synchronous read from the file. Assumes there are no pending asynchronous | 202 // Synchronous read from the file. Assumes there are no pending asynchronous |
194 // I/O functions. Returns true if the entire buffer was successfully filled. | 203 // I/O functions. Returns true if the entire buffer was successfully filled. |
195 bool ReadFromFile(HANDLE hfile, int32 offset, void* data, int32 data_size); | 204 bool ReadFromFile(FILE* hfile, off_t offset, void* data, size_t data_size); |
196 | 205 |
197 // General table handling | 206 // General table handling |
198 // ---------------------- | 207 // ---------------------- |
199 | 208 |
200 // Called to add a fingerprint to the table. Returns the index of the | 209 // Called to add a fingerprint to the table. Returns the index of the |
201 // inserted fingerprint or null_hash_ if there was a duplicate and this item | 210 // inserted fingerprint or null_hash_ if there was a duplicate and this item |
202 // was skippped. | 211 // was skippped. |
203 Hash AddFingerprint(Fingerprint fingerprint); | 212 Hash AddFingerprint(Fingerprint fingerprint); |
204 | 213 |
205 // Deletes all fingerprints from the given vector from the current hash table | 214 // Deletes all fingerprints from the given vector from the current hash table |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 std::set<Fingerprint> deleted_since_rebuild_; | 328 std::set<Fingerprint> deleted_since_rebuild_; |
320 | 329 |
321 // TODO(brettw) Support deletion, we need to track whether anything was | 330 // TODO(brettw) Support deletion, we need to track whether anything was |
322 // deleted during the rebuild here. Then we should delete any of these | 331 // deleted during the rebuild here. Then we should delete any of these |
323 // entries from the complete table later. | 332 // entries from the complete table later. |
324 //std::vector<Fingerprint> removed_since_rebuild_; | 333 //std::vector<Fingerprint> removed_since_rebuild_; |
325 | 334 |
326 // The currently open file with the table in it. This may be NULL if we're | 335 // The currently open file with the table in it. This may be NULL if we're |
327 // rebuilding and haven't written a new version yet. Writing to the file may | 336 // rebuilding and haven't written a new version yet. Writing to the file may |
328 // be safely ignored in this case. | 337 // be safely ignored in this case. |
329 HANDLE file_; | 338 FILE* file_; |
330 | 339 |
331 // Shared memory consists of a SharedHeader followed by the table. | 340 // Shared memory consists of a SharedHeader followed by the table. |
332 base::SharedMemory *shared_memory_; | 341 base::SharedMemory *shared_memory_; |
333 | 342 |
334 // When we generate new tables, we increment the serial number of the | 343 // When we generate new tables, we increment the serial number of the |
335 // shared memory object. | 344 // shared memory object. |
336 int32 shared_memory_serial_; | 345 int32 shared_memory_serial_; |
337 | 346 |
338 // Number of non-empty items in the table, used to compute fullness. | 347 // Number of non-empty items in the table, used to compute fullness. |
339 int32 used_items_; | 348 int32 used_items_; |
340 | 349 |
341 // Testing values ----------------------------------------------------------- | 350 // Testing values ----------------------------------------------------------- |
342 // | 351 // |
343 // The following fields exist for testing purposes. They are not used in | 352 // The following fields exist for testing purposes. They are not used in |
344 // release builds. It'd be nice to eliminate them in release builds, but we | 353 // release builds. It'd be nice to eliminate them in release builds, but we |
345 // don't want to change the signature of the object between the unit test and | 354 // don't want to change the signature of the object between the unit test and |
346 // regular builds. Instead, we just have "default" values that these take | 355 // regular builds. Instead, we just have "default" values that these take |
347 // in release builds that give "regular" behavior. | 356 // in release builds that give "regular" behavior. |
348 | 357 |
349 // Overridden database file name for testing | 358 // Overridden database file name for testing |
350 std::wstring database_name_override_; | 359 FilePath database_name_override_; |
351 | 360 |
352 // When nonzero, overrides the table size for new databases for testing | 361 // When nonzero, overrides the table size for new databases for testing |
353 int32 table_size_override_; | 362 int32 table_size_override_; |
354 | 363 |
355 // When non-NULL, overrides the history service to use rather than as the | 364 // When non-NULL, overrides the history service to use rather than as the |
356 // BrowserProcess. This is provided for unit tests. | 365 // BrowserProcess. This is provided for unit tests. |
357 HistoryService* history_service_override_; | 366 HistoryService* history_service_override_; |
358 | 367 |
359 // When non-NULL, indicates the task that should be run after the next | 368 // When non-NULL, indicates the task that should be run after the next |
360 // rebuild from history is complete. | 369 // rebuild from history is complete. |
(...skipping 16 matching lines...) Expand all Loading... |
377 for (int32 i = 0; i < table_length_; i++) { | 386 for (int32 i = 0; i < table_length_; i++) { |
378 if (hash_table_[i]) | 387 if (hash_table_[i]) |
379 used_count++; | 388 used_count++; |
380 } | 389 } |
381 DCHECK(used_count == used_items_); | 390 DCHECK(used_count == used_items_); |
382 } | 391 } |
383 #endif | 392 #endif |
384 | 393 |
385 #endif // CHROME_BROWSER_VISITEDLINK_MASTER_H__ | 394 #endif // CHROME_BROWSER_VISITEDLINK_MASTER_H__ |
386 | 395 |
OLD | NEW |