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