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/in_memory_url_index.h" | 5 #include "chrome/browser/history/in_memory_url_index.h" |
6 | 6 |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/history/history_notifications.h" |
8 #include "chrome/browser/history/url_database.h" | 9 #include "chrome/browser/history/url_database.h" |
| 10 #include "chrome/browser/history/url_index_private_data.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/common/chrome_notification_types.h" |
| 13 #include "content/public/browser/notification_details.h" |
| 14 #include "content/public/browser/notification_source.h" |
9 | 15 |
10 using in_memory_url_index::InMemoryURLIndexCacheItem; | 16 using in_memory_url_index::InMemoryURLIndexCacheItem; |
11 | 17 |
12 namespace history { | 18 namespace history { |
13 | 19 |
14 InMemoryURLIndex::InMemoryURLIndex(const FilePath& history_dir) | 20 InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask:: |
15 : history_dir_(history_dir), | 21 RebuildPrivateDataFromHistoryDBTask(InMemoryURLIndex* index) |
| 22 : index_(index), |
| 23 succeeded_(false) {} |
| 24 |
| 25 InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask:: |
| 26 ~RebuildPrivateDataFromHistoryDBTask() {} |
| 27 |
| 28 bool InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask::RunOnDBThread( |
| 29 HistoryBackend* backend, |
| 30 HistoryDatabase* db) { |
| 31 data_.reset(URLIndexPrivateData::RebuildFromHistory(db)); |
| 32 succeeded_ = data_.get() && !data_->history_info_map_.empty(); |
| 33 if (!succeeded_) |
| 34 data_.reset(); |
| 35 return true; |
| 36 } |
| 37 |
| 38 void InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask:: |
| 39 DoneRunOnMainThread() { |
| 40 if (succeeded_) |
| 41 index_->DoneRebuidingPrivateDataFromHistoryDB(data_.release()); |
| 42 } |
| 43 |
| 44 InMemoryURLIndex::InMemoryURLIndex(Profile* profile, |
| 45 const FilePath& history_dir, |
| 46 const std::string& languages) |
| 47 : profile_(profile), |
| 48 history_dir_(history_dir), |
16 private_data_(new URLIndexPrivateData), | 49 private_data_(new URLIndexPrivateData), |
17 cached_at_shutdown_(false) { | 50 shutdown_(false), |
| 51 needs_to_be_cached_(false) { |
| 52 private_data_->set_languages(languages); |
| 53 if (profile) { |
| 54 // TODO(mrossetti): Register for language change notifications. |
| 55 content::Source<Profile> source(profile); |
| 56 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URL_VISITED, source); |
| 57 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_TYPED_URLS_MODIFIED, |
| 58 source); |
| 59 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, source); |
| 60 } |
18 } | 61 } |
19 | 62 |
20 // Called only by unit tests. | 63 // Called only by unit tests. |
21 InMemoryURLIndex::InMemoryURLIndex() | 64 InMemoryURLIndex::InMemoryURLIndex() |
22 : private_data_(new URLIndexPrivateData), | 65 : profile_(NULL), |
23 cached_at_shutdown_(false) { | 66 private_data_(new URLIndexPrivateData), |
| 67 shutdown_(false), |
| 68 needs_to_be_cached_(false) { |
24 } | 69 } |
25 | 70 |
26 InMemoryURLIndex::~InMemoryURLIndex() { | 71 InMemoryURLIndex::~InMemoryURLIndex() { |
27 // If there was a history directory (which there won't be for some unit tests) | 72 // If there was a history directory (which there won't be for some unit tests) |
28 // then insure that the cache has already been saved. | 73 // then insure that the cache has already been saved. |
29 DCHECK(history_dir_.empty() || cached_at_shutdown_); | 74 DCHECK(history_dir_.empty() || !needs_to_be_cached_); |
30 } | 75 } |
31 | 76 |
32 bool InMemoryURLIndex::Init(URLDatabase* history_db, | 77 void InMemoryURLIndex::Init() { |
33 const std::string& languages) { | 78 RestoreFromCacheFile(); |
34 // TODO(mrossetti): Register for profile/language change notifications. | |
35 private_data_->set_languages(languages); | |
36 FilePath cache_file_path; | |
37 return (GetCacheFilePath(&cache_file_path) && | |
38 private_data_->RestoreFromFile(cache_file_path)) || | |
39 ReloadFromHistory(history_db); | |
40 } | |
41 | |
42 bool InMemoryURLIndex::ReloadFromHistory(history::URLDatabase* history_db) { | |
43 if (!private_data_->ReloadFromHistory(history_db)) | |
44 return false; | |
45 FilePath cache_file_path; | |
46 if (GetCacheFilePath(&cache_file_path)) | |
47 private_data_->SaveToFile(cache_file_path); | |
48 return true; | |
49 } | 79 } |
50 | 80 |
51 void InMemoryURLIndex::ShutDown() { | 81 void InMemoryURLIndex::ShutDown() { |
52 // Write our cache. | 82 registrar_.RemoveAll(); |
53 FilePath cache_file_path; | 83 cache_reader_consumer_.CancelAllRequests(); |
54 if (!GetCacheFilePath(&cache_file_path)) | 84 shutdown_ = true; |
55 return; | 85 SaveToCacheFile(); |
56 private_data_->SaveToFile(cache_file_path); | 86 needs_to_be_cached_ = false; |
57 cached_at_shutdown_ = true; | |
58 } | 87 } |
59 | 88 |
60 void InMemoryURLIndex::ClearPrivateData() { | 89 void InMemoryURLIndex::ClearPrivateData() { |
61 private_data_->Clear(); | 90 private_data_->Clear(); |
62 } | 91 } |
63 | 92 |
64 bool InMemoryURLIndex::GetCacheFilePath(FilePath* file_path) { | 93 bool InMemoryURLIndex::GetCacheFilePath(FilePath* file_path) { |
65 if (history_dir_.empty()) | 94 if (history_dir_.empty()) |
66 return false; | 95 return false; |
67 *file_path = history_dir_.Append(FILE_PATH_LITERAL("History Provider Cache")); | 96 *file_path = history_dir_.Append(FILE_PATH_LITERAL("History Provider Cache")); |
68 return true; | 97 return true; |
69 } | 98 } |
70 | 99 |
71 // Querying and Updating ------------------------------------------------------- | 100 // Querying -------------------------------------------------------------------- |
72 | 101 |
73 ScoredHistoryMatches InMemoryURLIndex::HistoryItemsForTerms( | 102 ScoredHistoryMatches InMemoryURLIndex::HistoryItemsForTerms( |
74 const string16& term_string) { | 103 const string16& term_string) { |
75 return private_data_->HistoryItemsForTerms(term_string); | 104 return private_data_->HistoryItemsForTerms(term_string); |
76 } | 105 } |
77 | 106 |
78 void InMemoryURLIndex::UpdateURL(URLID row_id, const URLRow& row) { | 107 // Updating -------------------------------------------------------------------- |
79 private_data_->UpdateURL(row_id, row); | 108 |
| 109 void InMemoryURLIndex::Observe(int notification_type, |
| 110 const content::NotificationSource& source, |
| 111 const content::NotificationDetails& details) { |
| 112 switch (notification_type) { |
| 113 case chrome::NOTIFICATION_HISTORY_URL_VISITED: |
| 114 OnURLVisited(content::Details<URLVisitedDetails>(details).ptr()); |
| 115 break; |
| 116 case chrome::NOTIFICATION_HISTORY_TYPED_URLS_MODIFIED: |
| 117 OnURLsModified( |
| 118 content::Details<history::URLsModifiedDetails>(details).ptr()); |
| 119 break; |
| 120 case chrome::NOTIFICATION_HISTORY_URLS_DELETED: |
| 121 OnURLsDeleted( |
| 122 content::Details<history::URLsDeletedDetails>(details).ptr()); |
| 123 break; |
| 124 case chrome::NOTIFICATION_HISTORY_LOADED: { |
| 125 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_LOADED, |
| 126 content::Source<Profile>(profile_)); |
| 127 ScheduleRebuildFromHistory(); |
| 128 break; |
| 129 } |
| 130 default: |
| 131 // For simplicity, the unit tests send us all notifications, even when |
| 132 // we haven't registered for them, so don't assert here. |
| 133 break; |
| 134 } |
80 } | 135 } |
81 | 136 |
82 void InMemoryURLIndex::DeleteURL(URLID row_id) { | 137 void InMemoryURLIndex::OnURLVisited(const URLVisitedDetails* details) { |
83 private_data_->DeleteURL(row_id); | 138 needs_to_be_cached_ |= private_data_->UpdateURL(details->row); |
| 139 } |
| 140 |
| 141 void InMemoryURLIndex::OnURLsModified(const URLsModifiedDetails* details) { |
| 142 for (URLRows::const_iterator row = details->changed_urls.begin(); |
| 143 row != details->changed_urls.end(); ++row) |
| 144 needs_to_be_cached_ |= private_data_->UpdateURL(*row); |
| 145 } |
| 146 |
| 147 void InMemoryURLIndex::OnURLsDeleted(const URLsDeletedDetails* details) { |
| 148 if (details->all_history) { |
| 149 ClearPrivateData(); |
| 150 needs_to_be_cached_ = true; |
| 151 } else { |
| 152 for (URLRows::const_iterator row = details->rows.begin(); |
| 153 row != details->rows.end(); ++row) |
| 154 needs_to_be_cached_ |= private_data_->DeleteURL(row->url()); |
| 155 } |
| 156 } |
| 157 |
| 158 // Restoring from Cache -------------------------------------------------------- |
| 159 |
| 160 void InMemoryURLIndex::RestoreFromCacheFile() { |
| 161 FilePath path; |
| 162 if (GetCacheFilePath(&path) && !shutdown_) |
| 163 DoRestoreFromCacheFile(path); |
| 164 } |
| 165 |
| 166 void InMemoryURLIndex::DoRestoreFromCacheFile(const FilePath& path) { |
| 167 if (private_data_->RestoreFromFile(path)) |
| 168 return; |
| 169 |
| 170 // When unable to restore from the cache file we must rebuild from the |
| 171 // history database. |
| 172 HistoryService* service = profile_->GetHistoryServiceWithoutCreating(); |
| 173 if (service && service->backend_loaded()) |
| 174 ScheduleRebuildFromHistory(); |
| 175 // We must wait to rebuild until the history backend has been loaded. |
| 176 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_LOADED, |
| 177 content::Source<Profile>(profile_)); |
| 178 } |
| 179 |
| 180 // Restoring from the History DB ----------------------------------------------- |
| 181 |
| 182 void InMemoryURLIndex::ScheduleRebuildFromHistory() { |
| 183 HistoryService* service = |
| 184 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 185 service->ScheduleDBTask( |
| 186 new InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask(this), |
| 187 &cache_reader_consumer_); |
| 188 } |
| 189 |
| 190 void InMemoryURLIndex::DoneRebuidingPrivateDataFromHistoryDB( |
| 191 URLIndexPrivateData* data) { |
| 192 scoped_ptr<URLIndexPrivateData> private_data(data); |
| 193 private_data_.swap(private_data); |
| 194 // Cache the newly rebuilt index. |
| 195 FilePath cache_file_path; |
| 196 if (GetCacheFilePath(&cache_file_path)) |
| 197 private_data_->SaveToFile(cache_file_path); |
| 198 } |
| 199 |
| 200 void InMemoryURLIndex::RebuildFromHistory(HistoryDatabase* history_db) { |
| 201 private_data_.reset(URLIndexPrivateData::RebuildFromHistory(history_db)); |
| 202 } |
| 203 |
| 204 // Saving to Cache ------------------------------------------------------------- |
| 205 |
| 206 void InMemoryURLIndex::SaveToCacheFile() { |
| 207 FilePath path; |
| 208 if (GetCacheFilePath(&path)) |
| 209 DoSaveToCacheFile(path); |
| 210 } |
| 211 |
| 212 void InMemoryURLIndex::DoSaveToCacheFile(const FilePath& path) { |
| 213 private_data_->SaveToFile(path); |
84 } | 214 } |
85 | 215 |
86 } // namespace history | 216 } // namespace history |
OLD | NEW |