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 #include "components/visitedlink/browser/visitedlink_master.h" | 5 #include "components/visitedlink/browser/visitedlink_master.h" |
6 | 6 |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <windows.h> | 8 #include <windows.h> |
9 #include <io.h> | 9 #include <io.h> |
10 #include <shlobj.h> | 10 #include <shlobj.h> |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include "base/strings/string_util.h" | 24 #include "base/strings/string_util.h" |
25 #include "base/threading/thread_restrictions.h" | 25 #include "base/threading/thread_restrictions.h" |
26 #include "components/visitedlink/browser/visitedlink_delegate.h" | 26 #include "components/visitedlink/browser/visitedlink_delegate.h" |
27 #include "components/visitedlink/browser/visitedlink_event_listener.h" | 27 #include "components/visitedlink/browser/visitedlink_event_listener.h" |
28 #include "content/public/browser/browser_context.h" | 28 #include "content/public/browser/browser_context.h" |
29 #include "content/public/browser/browser_thread.h" | 29 #include "content/public/browser/browser_thread.h" |
30 #include "url/gurl.h" | 30 #include "url/gurl.h" |
31 | 31 |
32 using content::BrowserThread; | 32 using content::BrowserThread; |
33 using file_util::ScopedFILE; | 33 using file_util::ScopedFILE; |
| 34 using file_util::OpenFile; |
| 35 using file_util::TruncateFile; |
34 | 36 |
35 namespace visitedlink { | 37 namespace visitedlink { |
36 | 38 |
37 const int32 VisitedLinkMaster::kFileHeaderSignatureOffset = 0; | 39 const int32 VisitedLinkMaster::kFileHeaderSignatureOffset = 0; |
38 const int32 VisitedLinkMaster::kFileHeaderVersionOffset = 4; | 40 const int32 VisitedLinkMaster::kFileHeaderVersionOffset = 4; |
39 const int32 VisitedLinkMaster::kFileHeaderLengthOffset = 8; | 41 const int32 VisitedLinkMaster::kFileHeaderLengthOffset = 8; |
40 const int32 VisitedLinkMaster::kFileHeaderUsedOffset = 12; | 42 const int32 VisitedLinkMaster::kFileHeaderUsedOffset = 12; |
41 const int32 VisitedLinkMaster::kFileHeaderSaltOffset = 16; | 43 const int32 VisitedLinkMaster::kFileHeaderSaltOffset = 16; |
42 | 44 |
43 const int32 VisitedLinkMaster::kFileCurrentVersion = 3; | 45 const int32 VisitedLinkMaster::kFileCurrentVersion = 3; |
(...skipping 15 matching lines...) Expand all Loading... |
59 // It is not necessary to generate a cryptographically strong random string, | 61 // It is not necessary to generate a cryptographically strong random string, |
60 // only that it be reasonably different for different users. | 62 // only that it be reasonably different for different users. |
61 void GenerateSalt(uint8 salt[LINK_SALT_LENGTH]) { | 63 void GenerateSalt(uint8 salt[LINK_SALT_LENGTH]) { |
62 DCHECK_EQ(LINK_SALT_LENGTH, 8) << "This code assumes the length of the salt"; | 64 DCHECK_EQ(LINK_SALT_LENGTH, 8) << "This code assumes the length of the salt"; |
63 uint64 randval = base::RandUint64(); | 65 uint64 randval = base::RandUint64(); |
64 memcpy(salt, &randval, 8); | 66 memcpy(salt, &randval, 8); |
65 } | 67 } |
66 | 68 |
67 // Opens file on a background thread to not block UI thread. | 69 // Opens file on a background thread to not block UI thread. |
68 void AsyncOpen(FILE** file, const base::FilePath& filename) { | 70 void AsyncOpen(FILE** file, const base::FilePath& filename) { |
69 *file = base::OpenFile(filename, "wb+"); | 71 *file = OpenFile(filename, "wb+"); |
70 DLOG_IF(ERROR, !(*file)) << "Failed to open file " << filename.value(); | 72 DLOG_IF(ERROR, !(*file)) << "Failed to open file " << filename.value(); |
71 } | 73 } |
72 | 74 |
73 // Returns true if the write was complete. | 75 // Returns true if the write was complete. |
74 static bool WriteToFile(FILE* file, | 76 static bool WriteToFile(FILE* file, |
75 off_t offset, | 77 off_t offset, |
76 const void* data, | 78 const void* data, |
77 size_t data_len) { | 79 size_t data_len) { |
78 if (fseek(file, offset, SEEK_SET) != 0) | 80 if (fseek(file, offset, SEEK_SET) != 0) |
79 return false; // Don't write to an invalid part of the file. | 81 return false; // Don't write to an invalid part of the file. |
(...skipping 16 matching lines...) Expand all Loading... |
96 void AsyncWrite(FILE** file, int32 offset, const std::string& data) { | 98 void AsyncWrite(FILE** file, int32 offset, const std::string& data) { |
97 if (*file) | 99 if (*file) |
98 WriteToFile(*file, offset, data.data(), data.size()); | 100 WriteToFile(*file, offset, data.data(), data.size()); |
99 } | 101 } |
100 | 102 |
101 // Truncates the file to the current position asynchronously on a background | 103 // Truncates the file to the current position asynchronously on a background |
102 // thread. Double pointer to FILE is used because file may still not be opened | 104 // thread. Double pointer to FILE is used because file may still not be opened |
103 // by the time of scheduling the task for execution. | 105 // by the time of scheduling the task for execution. |
104 void AsyncTruncate(FILE** file) { | 106 void AsyncTruncate(FILE** file) { |
105 if (*file) | 107 if (*file) |
106 base::IgnoreResult(base::TruncateFile(*file)); | 108 base::IgnoreResult(TruncateFile(*file)); |
107 } | 109 } |
108 | 110 |
109 // Closes the file on a background thread and releases memory used for storage | 111 // Closes the file on a background thread and releases memory used for storage |
110 // of FILE* value. Double pointer to FILE is used because file may still not | 112 // of FILE* value. Double pointer to FILE is used because file may still not |
111 // be opened by the time of scheduling the task for execution. | 113 // be opened by the time of scheduling the task for execution. |
112 void AsyncClose(FILE** file) { | 114 void AsyncClose(FILE** file) { |
113 if (*file) | 115 if (*file) |
114 base::IgnoreResult(fclose(*file)); | 116 base::IgnoreResult(fclose(*file)); |
115 free(file); | 117 free(file); |
116 } | 118 } |
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
533 // The hash table may have shrunk, so make sure this is the end. | 535 // The hash table may have shrunk, so make sure this is the end. |
534 PostIOTask(FROM_HERE, base::Bind(&AsyncTruncate, file_)); | 536 PostIOTask(FROM_HERE, base::Bind(&AsyncTruncate, file_)); |
535 } | 537 } |
536 | 538 |
537 bool VisitedLinkMaster::InitFromFile() { | 539 bool VisitedLinkMaster::InitFromFile() { |
538 DCHECK(file_ == NULL); | 540 DCHECK(file_ == NULL); |
539 DCHECK(persist_to_disk_); | 541 DCHECK(persist_to_disk_); |
540 | 542 |
541 base::FilePath filename; | 543 base::FilePath filename; |
542 GetDatabaseFileName(&filename); | 544 GetDatabaseFileName(&filename); |
543 ScopedFILE file_closer(base::OpenFile(filename, "rb+")); | 545 ScopedFILE file_closer(OpenFile(filename, "rb+")); |
544 if (!file_closer.get()) | 546 if (!file_closer.get()) |
545 return false; | 547 return false; |
546 | 548 |
547 int32 num_entries, used_count; | 549 int32 num_entries, used_count; |
548 if (!ReadFileHeader(file_closer.get(), &num_entries, &used_count, salt_)) | 550 if (!ReadFileHeader(file_closer.get(), &num_entries, &used_count, salt_)) |
549 return false; // Header isn't valid. | 551 return false; // Header isn't valid. |
550 | 552 |
551 // Allocate and read the table. | 553 // Allocate and read the table. |
552 if (!CreateURLTable(num_entries, false)) | 554 if (!CreateURLTable(num_entries, false)) |
553 return false; | 555 return false; |
(...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
978 BrowserThread::UI, FROM_HERE, | 980 BrowserThread::UI, FROM_HERE, |
979 base::Bind(&TableBuilder::OnCompleteMainThread, this)); | 981 base::Bind(&TableBuilder::OnCompleteMainThread, this)); |
980 } | 982 } |
981 | 983 |
982 void VisitedLinkMaster::TableBuilder::OnCompleteMainThread() { | 984 void VisitedLinkMaster::TableBuilder::OnCompleteMainThread() { |
983 if (master_) | 985 if (master_) |
984 master_->OnTableRebuildComplete(success_, fingerprints_); | 986 master_->OnTableRebuildComplete(success_, fingerprints_); |
985 } | 987 } |
986 | 988 |
987 } // namespace visitedlink | 989 } // namespace visitedlink |
OLD | NEW |