| 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 "chrome/browser/history/url_index_private_data.h" | 5 #include "chrome/browser/history/url_index_private_data.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <functional> | 8 #include <functional> |
| 9 #include <iterator> | 9 #include <iterator> |
| 10 #include <limits> | 10 #include <limits> |
| 11 #include <numeric> | 11 #include <numeric> |
| 12 | 12 |
| 13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 14 #include "base/i18n/case_conversion.h" | 14 #include "base/i18n/case_conversion.h" |
| 15 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
| 16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 17 #include "base/threading/thread_restrictions.h" | 17 #include "base/time.h" |
| 18 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
| 19 #include "chrome/browser/autocomplete/autocomplete.h" | 19 #include "chrome/browser/autocomplete/autocomplete.h" |
| 20 #include "chrome/browser/history/history_database.h" | 20 #include "chrome/browser/history/history_database.h" |
| 21 #include "chrome/browser/history/in_memory_url_index.h" |
| 21 #include "chrome/common/url_constants.h" | 22 #include "chrome/common/url_constants.h" |
| 23 #include "content/public/browser/notification_details.h" |
| 24 #include "content/public/browser/notification_service.h" |
| 25 #include "content/public/browser/notification_source.h" |
| 22 #include "net/base/net_util.h" | 26 #include "net/base/net_util.h" |
| 23 #include "third_party/protobuf/src/google/protobuf/repeated_field.h" | 27 #include "third_party/protobuf/src/google/protobuf/repeated_field.h" |
| 24 | 28 |
| 25 using google::protobuf::RepeatedField; | 29 using google::protobuf::RepeatedField; |
| 26 using google::protobuf::RepeatedPtrField; | 30 using google::protobuf::RepeatedPtrField; |
| 27 using in_memory_url_index::InMemoryURLIndexCacheItem; | 31 using in_memory_url_index::InMemoryURLIndexCacheItem; |
| 28 | 32 |
| 29 namespace history { | 33 namespace history { |
| 30 | 34 |
| 31 typedef imui::InMemoryURLIndexCacheItem_WordListItem WordListItem; | 35 typedef imui::InMemoryURLIndexCacheItem_WordListItem WordListItem; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 47 const int kMaxTotalScore = 1425; | 51 const int kMaxTotalScore = 1425; |
| 48 | 52 |
| 49 // Score ranges used to get a 'base' score for each of the scoring factors | 53 // Score ranges used to get a 'base' score for each of the scoring factors |
| 50 // (such as recency of last visit, times visited, times the URL was typed, | 54 // (such as recency of last visit, times visited, times the URL was typed, |
| 51 // and the quality of the string match). There is a matching value range for | 55 // and the quality of the string match). There is a matching value range for |
| 52 // each of these scores for each factor. Note that the top score is greater | 56 // each of these scores for each factor. Note that the top score is greater |
| 53 // than |kMaxTotalScore|. The score for each candidate will be capped in the | 57 // than |kMaxTotalScore|. The score for each candidate will be capped in the |
| 54 // final calculation. | 58 // final calculation. |
| 55 const int kScoreRank[] = { 1450, 1200, 900, 400 }; | 59 const int kScoreRank[] = { 1450, 1200, 900, 400 }; |
| 56 | 60 |
| 61 // RefCountedURLIndexPrivateDataPtr -------------------------------------------- |
| 62 |
| 63 RefCountedURLIndexPrivateDataPtr::RefCountedURLIndexPrivateDataPtr() {} |
| 64 RefCountedURLIndexPrivateDataPtr::~RefCountedURLIndexPrivateDataPtr() {} |
| 65 |
| 66 URLIndexPrivateData* RefCountedURLIndexPrivateDataPtr::get() { |
| 67 return data_.get(); |
| 68 } |
| 69 URLIndexPrivateData* RefCountedURLIndexPrivateDataPtr::release() { |
| 70 return data_.release(); |
| 71 } |
| 72 void RefCountedURLIndexPrivateDataPtr::reset(URLIndexPrivateData* data) { |
| 73 data_.reset(data); |
| 74 } |
| 75 |
| 57 // SearchTermCacheItem --------------------------------------------------------- | 76 // SearchTermCacheItem --------------------------------------------------------- |
| 58 | 77 |
| 59 URLIndexPrivateData::SearchTermCacheItem::SearchTermCacheItem( | 78 URLIndexPrivateData::SearchTermCacheItem::SearchTermCacheItem( |
| 60 const WordIDSet& word_id_set, | 79 const WordIDSet& word_id_set, |
| 61 const HistoryIDSet& history_id_set) | 80 const HistoryIDSet& history_id_set) |
| 62 : word_id_set_(word_id_set), | 81 : word_id_set_(word_id_set), |
| 63 history_id_set_(history_id_set), | 82 history_id_set_(history_id_set), |
| 64 used_(true) {} | 83 used_(true) {} |
| 65 | 84 |
| 66 URLIndexPrivateData::SearchTermCacheItem::SearchTermCacheItem() | 85 URLIndexPrivateData::SearchTermCacheItem::SearchTermCacheItem() |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 void URLIndexPrivateData::Clear() { | 145 void URLIndexPrivateData::Clear() { |
| 127 word_list_.clear(); | 146 word_list_.clear(); |
| 128 available_words_.clear(); | 147 available_words_.clear(); |
| 129 word_map_.clear(); | 148 word_map_.clear(); |
| 130 char_word_map_.clear(); | 149 char_word_map_.clear(); |
| 131 word_id_history_map_.clear(); | 150 word_id_history_map_.clear(); |
| 132 history_id_word_map_.clear(); | 151 history_id_word_map_.clear(); |
| 133 history_info_map_.clear(); | 152 history_info_map_.clear(); |
| 134 } | 153 } |
| 135 | 154 |
| 155 bool URLIndexPrivateData::Empty() const { |
| 156 return history_info_map_.empty(); |
| 157 } |
| 158 |
| 136 // Cache Updating -------------------------------------------------------------- | 159 // Cache Updating -------------------------------------------------------------- |
| 137 | 160 |
| 138 bool URLIndexPrivateData::IndexRow(const URLRow& row) { | 161 bool URLIndexPrivateData::IndexRow(const URLRow& row) { |
| 139 const GURL& gurl(row.url()); | 162 const GURL& gurl(row.url()); |
| 140 | 163 |
| 141 // Index only URLs with a whitelisted scheme. | 164 // Index only URLs with a whitelisted scheme. |
| 142 if (!URLIndexPrivateData::URLSchemeIsWhitelisted(gurl)) | 165 if (!URLIndexPrivateData::URLSchemeIsWhitelisted(gurl)) |
| 143 return false; | 166 return false; |
| 144 | 167 |
| 145 URLID row_id = row.id(); | 168 URLID row_id = row.id(); |
| (...skipping 749 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 895 whitelist->insert(std::string(chrome::kChromeUIScheme)); | 918 whitelist->insert(std::string(chrome::kChromeUIScheme)); |
| 896 whitelist->insert(std::string(chrome::kFileScheme)); | 919 whitelist->insert(std::string(chrome::kFileScheme)); |
| 897 whitelist->insert(std::string(chrome::kFtpScheme)); | 920 whitelist->insert(std::string(chrome::kFtpScheme)); |
| 898 whitelist->insert(std::string(chrome::kHttpScheme)); | 921 whitelist->insert(std::string(chrome::kHttpScheme)); |
| 899 whitelist->insert(std::string(chrome::kHttpsScheme)); | 922 whitelist->insert(std::string(chrome::kHttpsScheme)); |
| 900 whitelist->insert(std::string(chrome::kMailToScheme)); | 923 whitelist->insert(std::string(chrome::kMailToScheme)); |
| 901 } | 924 } |
| 902 | 925 |
| 903 // Cache Saving ---------------------------------------------------------------- | 926 // Cache Saving ---------------------------------------------------------------- |
| 904 | 927 |
| 928 // static |
| 929 void URLIndexPrivateData::WritePrivateDataToCacheFileTask( |
| 930 scoped_ptr<URLIndexPrivateData> private_data, |
| 931 const FilePath& file_path, |
| 932 scoped_refptr<RefCountedBool> succeeded) { |
| 933 DCHECK(private_data.get()); |
| 934 DCHECK(!file_path.empty()); |
| 935 succeeded->set_value(private_data->SaveToFile(file_path)); |
| 936 } |
| 937 |
| 905 bool URLIndexPrivateData::SaveToFile(const FilePath& file_path) { | 938 bool URLIndexPrivateData::SaveToFile(const FilePath& file_path) { |
| 906 // TODO(mrossetti): Move File IO to another thread. | |
| 907 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
| 908 base::TimeTicks beginning_time = base::TimeTicks::Now(); | 939 base::TimeTicks beginning_time = base::TimeTicks::Now(); |
| 909 InMemoryURLIndexCacheItem index_cache; | 940 InMemoryURLIndexCacheItem index_cache; |
| 910 SavePrivateData(&index_cache); | 941 SavePrivateData(&index_cache); |
| 911 std::string data; | 942 std::string data; |
| 912 if (!index_cache.SerializeToString(&data)) { | 943 if (!index_cache.SerializeToString(&data)) { |
| 913 LOG(WARNING) << "Failed to serialize the InMemoryURLIndex cache."; | 944 LOG(WARNING) << "Failed to serialize the InMemoryURLIndex cache."; |
| 914 return false; | 945 return false; |
| 915 } | 946 } |
| 916 | 947 |
| 917 int size = data.size(); | 948 int size = data.size(); |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1015 map_entry->set_visit_count(url_row.visit_count()); | 1046 map_entry->set_visit_count(url_row.visit_count()); |
| 1016 map_entry->set_typed_count(url_row.typed_count()); | 1047 map_entry->set_typed_count(url_row.typed_count()); |
| 1017 map_entry->set_last_visit(url_row.last_visit().ToInternalValue()); | 1048 map_entry->set_last_visit(url_row.last_visit().ToInternalValue()); |
| 1018 map_entry->set_url(url_row.url().spec()); | 1049 map_entry->set_url(url_row.url().spec()); |
| 1019 map_entry->set_title(UTF16ToUTF8(url_row.title())); | 1050 map_entry->set_title(UTF16ToUTF8(url_row.title())); |
| 1020 } | 1051 } |
| 1021 } | 1052 } |
| 1022 | 1053 |
| 1023 // Cache Restoring ------------------------------------------------------------- | 1054 // Cache Restoring ------------------------------------------------------------- |
| 1024 | 1055 |
| 1025 bool URLIndexPrivateData::RestoreFromFile(const FilePath& file_path) { | 1056 // static |
| 1026 // TODO(mrossetti): Figure out how to determine if the cache is up-to-date. | 1057 void URLIndexPrivateData::RestoreFromFileTask( |
| 1027 // That is: ensure that the database has not been modified since the cache | 1058 const FilePath& file_path, |
| 1028 // was last saved. DB file modification date is inadequate. There are no | 1059 scoped_refptr<RefCountedURLIndexPrivateDataPtr> private_data_ptr) { |
| 1029 // SQLite table checksums automatically stored. | 1060 private_data_ptr->reset(URLIndexPrivateData::RestoreFromFile(file_path)); |
| 1030 Clear(); // Start with a clean slate. | 1061 } |
| 1031 | 1062 |
| 1032 // FIXME(mrossetti): Move File IO to another thread. | 1063 // static |
| 1033 base::ThreadRestrictions::ScopedAllowIO allow_io; | 1064 URLIndexPrivateData* URLIndexPrivateData::RestoreFromFile( |
| 1065 const FilePath& file_path) { |
| 1034 base::TimeTicks beginning_time = base::TimeTicks::Now(); | 1066 base::TimeTicks beginning_time = base::TimeTicks::Now(); |
| 1035 if (!file_util::PathExists(file_path)) | 1067 if (!file_util::PathExists(file_path)) |
| 1036 return false; | 1068 return NULL; |
| 1037 std::string data; | 1069 std::string data; |
| 1038 // If there is no cache file then simply give up. This will cause us to | 1070 // If there is no cache file then simply give up. This will cause us to |
| 1039 // attempt to rebuild from the history database. | 1071 // attempt to rebuild from the history database. |
| 1040 if (!file_util::ReadFileToString(file_path, &data)) | 1072 if (!file_util::ReadFileToString(file_path, &data)) |
| 1041 return false; | 1073 return NULL; |
| 1042 | 1074 |
| 1075 scoped_ptr<URLIndexPrivateData> restored_data(new URLIndexPrivateData); |
| 1043 InMemoryURLIndexCacheItem index_cache; | 1076 InMemoryURLIndexCacheItem index_cache; |
| 1044 if (!index_cache.ParseFromArray(data.c_str(), data.size())) { | 1077 if (!index_cache.ParseFromArray(data.c_str(), data.size())) { |
| 1045 LOG(WARNING) << "Failed to parse InMemoryURLIndex cache data read from " | 1078 LOG(WARNING) << "Failed to parse URLIndexPrivateData cache data read from " |
| 1046 << file_path.value(); | 1079 << file_path.value(); |
| 1047 return false; | 1080 return restored_data.release(); |
| 1048 } | 1081 } |
| 1049 | 1082 |
| 1050 if (!RestorePrivateData(index_cache)) { | 1083 if (!restored_data->RestorePrivateData(index_cache)) { |
| 1051 Clear(); // Back to square one -- must build from scratch. | 1084 restored_data.reset(); // Back to square one -- must build from history DB. |
| 1052 return false; | 1085 return NULL; |
| 1053 } | 1086 } |
| 1054 | 1087 |
| 1055 UMA_HISTOGRAM_TIMES("History.InMemoryURLIndexRestoreCacheTime", | 1088 UMA_HISTOGRAM_TIMES("History.InMemoryURLIndexRestoreCacheTime", |
| 1056 base::TimeTicks::Now() - beginning_time); | 1089 base::TimeTicks::Now() - beginning_time); |
| 1057 UMA_HISTOGRAM_COUNTS("History.InMemoryURLHistoryItems", | 1090 UMA_HISTOGRAM_COUNTS("History.InMemoryURLHistoryItems", |
| 1058 history_id_word_map_.size()); | 1091 restored_data->history_id_word_map_.size()); |
| 1059 UMA_HISTOGRAM_COUNTS("History.InMemoryURLCacheSize", data.size()); | 1092 UMA_HISTOGRAM_COUNTS("History.InMemoryURLCacheSize", data.size()); |
| 1060 UMA_HISTOGRAM_COUNTS_10000("History.InMemoryURLWords", word_map_.size()); | 1093 UMA_HISTOGRAM_COUNTS_10000("History.InMemoryURLWords", |
| 1061 UMA_HISTOGRAM_COUNTS_10000("History.InMemoryURLChars", char_word_map_.size()); | 1094 restored_data->word_map_.size()); |
| 1062 return true; | 1095 UMA_HISTOGRAM_COUNTS_10000("History.InMemoryURLChars", |
| 1096 restored_data->char_word_map_.size()); |
| 1097 if (restored_data->Empty()) |
| 1098 restored_data.reset(); // 'No data' is the same as a failed reload. |
| 1099 return restored_data.release(); |
| 1063 } | 1100 } |
| 1064 | 1101 |
| 1065 // static | 1102 // static |
| 1066 URLIndexPrivateData* URLIndexPrivateData::RebuildFromHistory( | 1103 URLIndexPrivateData* URLIndexPrivateData::RebuildFromHistory( |
| 1067 HistoryDatabase* history_db) { | 1104 HistoryDatabase* history_db) { |
| 1068 if (!history_db) | 1105 if (!history_db) |
| 1069 return NULL; | 1106 return NULL; |
| 1070 | 1107 |
| 1071 base::TimeTicks beginning_time = base::TimeTicks::Now(); | 1108 base::TimeTicks beginning_time = base::TimeTicks::Now(); |
| 1072 | 1109 |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1207 if (iter->has_title()) { | 1244 if (iter->has_title()) { |
| 1208 string16 title(UTF8ToUTF16(iter->title())); | 1245 string16 title(UTF8ToUTF16(iter->title())); |
| 1209 url_row.set_title(title); | 1246 url_row.set_title(title); |
| 1210 } | 1247 } |
| 1211 history_info_map_[history_id] = url_row; | 1248 history_info_map_[history_id] = url_row; |
| 1212 } | 1249 } |
| 1213 return true; | 1250 return true; |
| 1214 } | 1251 } |
| 1215 | 1252 |
| 1216 } // namespace history | 1253 } // namespace history |
| OLD | NEW |