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/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 10 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 // then insure that the cache has already been saved. | 131 // then insure that the cache has already been saved. |
132 DCHECK(history_dir_.empty() || !needs_to_be_cached_); | 132 DCHECK(history_dir_.empty() || !needs_to_be_cached_); |
133 } | 133 } |
134 | 134 |
135 void InMemoryURLIndex::Init() { | 135 void InMemoryURLIndex::Init() { |
136 PostRestoreFromCacheFileTask(); | 136 PostRestoreFromCacheFileTask(); |
137 } | 137 } |
138 | 138 |
139 void InMemoryURLIndex::ShutDown() { | 139 void InMemoryURLIndex::ShutDown() { |
140 registrar_.RemoveAll(); | 140 registrar_.RemoveAll(); |
141 cache_reader_consumer_.CancelAllRequests(); | 141 cache_reader_tracker_.TryCancelAll(); |
142 shutdown_ = true; | 142 shutdown_ = true; |
143 base::FilePath path; | 143 base::FilePath path; |
144 if (!GetCacheFilePath(&path)) | 144 if (!GetCacheFilePath(&path)) |
145 return; | 145 return; |
146 private_data_->CancelPendingUpdates(); | 146 private_data_tracker_.TryCancelAll(); |
147 URLIndexPrivateData::WritePrivateDataToCacheFileTask(private_data_, path); | 147 URLIndexPrivateData::WritePrivateDataToCacheFileTask(private_data_, path); |
148 needs_to_be_cached_ = false; | 148 needs_to_be_cached_ = false; |
149 } | 149 } |
150 | 150 |
151 void InMemoryURLIndex::ClearPrivateData() { | 151 void InMemoryURLIndex::ClearPrivateData() { |
152 private_data_->Clear(); | 152 private_data_->Clear(); |
153 } | 153 } |
154 | 154 |
155 bool InMemoryURLIndex::GetCacheFilePath(base::FilePath* file_path) { | 155 bool InMemoryURLIndex::GetCacheFilePath(base::FilePath* file_path) { |
156 if (history_dir_.empty()) | 156 if (history_dir_.empty()) |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 // For simplicity, the unit tests send us all notifications, even when | 203 // For simplicity, the unit tests send us all notifications, even when |
204 // we haven't registered for them, so don't assert here. | 204 // we haven't registered for them, so don't assert here. |
205 break; | 205 break; |
206 } | 206 } |
207 } | 207 } |
208 | 208 |
209 void InMemoryURLIndex::OnURLVisited(const URLVisitedDetails* details) { | 209 void InMemoryURLIndex::OnURLVisited(const URLVisitedDetails* details) { |
210 HistoryService* service = | 210 HistoryService* service = |
211 HistoryServiceFactory::GetForProfile(profile_, | 211 HistoryServiceFactory::GetForProfile(profile_, |
212 Profile::EXPLICIT_ACCESS); | 212 Profile::EXPLICIT_ACCESS); |
213 needs_to_be_cached_ |= private_data_->UpdateURL( | 213 needs_to_be_cached_ |= private_data_->UpdateURL(service, |
214 service, details->row, languages_, scheme_whitelist_); | 214 details->row, |
| 215 languages_, |
| 216 scheme_whitelist_, |
| 217 &private_data_tracker_); |
215 } | 218 } |
216 | 219 |
217 void InMemoryURLIndex::OnURLsModified(const URLsModifiedDetails* details) { | 220 void InMemoryURLIndex::OnURLsModified(const URLsModifiedDetails* details) { |
218 HistoryService* service = | 221 HistoryService* service = |
219 HistoryServiceFactory::GetForProfile(profile_, | 222 HistoryServiceFactory::GetForProfile(profile_, |
220 Profile::EXPLICIT_ACCESS); | 223 Profile::EXPLICIT_ACCESS); |
221 for (URLRows::const_iterator row = details->changed_urls.begin(); | 224 for (URLRows::const_iterator row = details->changed_urls.begin(); |
222 row != details->changed_urls.end(); ++row) | 225 row != details->changed_urls.end(); |
223 needs_to_be_cached_ |= | 226 ++row) { |
224 private_data_->UpdateURL(service, *row, languages_, scheme_whitelist_); | 227 needs_to_be_cached_ |= private_data_->UpdateURL( |
| 228 service, *row, languages_, scheme_whitelist_, &private_data_tracker_); |
| 229 } |
225 } | 230 } |
226 | 231 |
227 void InMemoryURLIndex::OnURLsDeleted(const URLsDeletedDetails* details) { | 232 void InMemoryURLIndex::OnURLsDeleted(const URLsDeletedDetails* details) { |
228 if (details->all_history) { | 233 if (details->all_history) { |
229 ClearPrivateData(); | 234 ClearPrivateData(); |
230 needs_to_be_cached_ = true; | 235 needs_to_be_cached_ = true; |
231 } else { | 236 } else { |
232 for (URLRows::const_iterator row = details->rows.begin(); | 237 for (URLRows::const_iterator row = details->rows.begin(); |
233 row != details->rows.end(); ++row) | 238 row != details->rows.end(); ++row) |
234 needs_to_be_cached_ |= private_data_->DeleteURL(row->url()); | 239 needs_to_be_cached_ |= private_data_->DeleteURL(row->url()); |
(...skipping 17 matching lines...) Expand all Loading... |
252 content::BrowserThread::PostTaskAndReplyWithResult | 257 content::BrowserThread::PostTaskAndReplyWithResult |
253 <scoped_refptr<URLIndexPrivateData> >( | 258 <scoped_refptr<URLIndexPrivateData> >( |
254 content::BrowserThread::FILE, FROM_HERE, | 259 content::BrowserThread::FILE, FROM_HERE, |
255 base::Bind(&URLIndexPrivateData::RestoreFromFile, path, languages_), | 260 base::Bind(&URLIndexPrivateData::RestoreFromFile, path, languages_), |
256 base::Bind(&InMemoryURLIndex::OnCacheLoadDone, AsWeakPtr())); | 261 base::Bind(&InMemoryURLIndex::OnCacheLoadDone, AsWeakPtr())); |
257 } | 262 } |
258 | 263 |
259 void InMemoryURLIndex::OnCacheLoadDone( | 264 void InMemoryURLIndex::OnCacheLoadDone( |
260 scoped_refptr<URLIndexPrivateData> private_data) { | 265 scoped_refptr<URLIndexPrivateData> private_data) { |
261 if (private_data.get() && !private_data->Empty()) { | 266 if (private_data.get() && !private_data->Empty()) { |
| 267 private_data_tracker_.TryCancelAll(); |
262 private_data_ = private_data; | 268 private_data_ = private_data; |
263 restored_ = true; | 269 restored_ = true; |
264 if (restore_cache_observer_) | 270 if (restore_cache_observer_) |
265 restore_cache_observer_->OnCacheRestoreFinished(true); | 271 restore_cache_observer_->OnCacheRestoreFinished(true); |
266 } else if (profile_) { | 272 } else if (profile_) { |
267 // When unable to restore from the cache file delete the cache file, if | 273 // When unable to restore from the cache file delete the cache file, if |
268 // it exists, and then rebuild from the history database if it's available, | 274 // it exists, and then rebuild from the history database if it's available, |
269 // otherwise wait until the history database loaded and then rebuild. | 275 // otherwise wait until the history database loaded and then rebuild. |
270 base::FilePath path; | 276 base::FilePath path; |
271 if (!GetCacheFilePath(&path) || shutdown_) | 277 if (!GetCacheFilePath(&path) || shutdown_) |
(...skipping 13 matching lines...) Expand all Loading... |
285 | 291 |
286 // Restoring from the History DB ----------------------------------------------- | 292 // Restoring from the History DB ----------------------------------------------- |
287 | 293 |
288 void InMemoryURLIndex::ScheduleRebuildFromHistory() { | 294 void InMemoryURLIndex::ScheduleRebuildFromHistory() { |
289 HistoryService* service = | 295 HistoryService* service = |
290 HistoryServiceFactory::GetForProfile(profile_, | 296 HistoryServiceFactory::GetForProfile(profile_, |
291 Profile::EXPLICIT_ACCESS); | 297 Profile::EXPLICIT_ACCESS); |
292 service->ScheduleDBTask( | 298 service->ScheduleDBTask( |
293 new InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask( | 299 new InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask( |
294 this, languages_, scheme_whitelist_), | 300 this, languages_, scheme_whitelist_), |
295 &cache_reader_consumer_); | 301 &cache_reader_tracker_); |
296 } | 302 } |
297 | 303 |
298 void InMemoryURLIndex::DoneRebuidingPrivateDataFromHistoryDB( | 304 void InMemoryURLIndex::DoneRebuidingPrivateDataFromHistoryDB( |
299 bool succeeded, | 305 bool succeeded, |
300 scoped_refptr<URLIndexPrivateData> private_data) { | 306 scoped_refptr<URLIndexPrivateData> private_data) { |
301 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 307 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
302 if (succeeded) { | 308 if (succeeded) { |
| 309 private_data_tracker_.TryCancelAll(); |
303 private_data_ = private_data; | 310 private_data_ = private_data; |
304 PostSaveToCacheFileTask(); // Cache the newly rebuilt index. | 311 PostSaveToCacheFileTask(); // Cache the newly rebuilt index. |
305 } else { | 312 } else { |
306 private_data_->Clear(); // Dump the old private data. | 313 private_data_->Clear(); // Dump the old private data. |
307 // There is no need to do anything with the cache file as it was deleted | 314 // There is no need to do anything with the cache file as it was deleted |
308 // when the rebuild from the history operation was kicked off. | 315 // when the rebuild from the history operation was kicked off. |
309 } | 316 } |
310 restored_ = true; | 317 restored_ = true; |
311 if (restore_cache_observer_) | 318 if (restore_cache_observer_) |
312 restore_cache_observer_->OnCacheRestoreFinished(succeeded); | 319 restore_cache_observer_->OnCacheRestoreFinished(succeeded); |
313 } | 320 } |
314 | 321 |
315 void InMemoryURLIndex::RebuildFromHistory(HistoryDatabase* history_db) { | 322 void InMemoryURLIndex::RebuildFromHistory(HistoryDatabase* history_db) { |
| 323 private_data_tracker_.TryCancelAll(); |
316 private_data_ = URLIndexPrivateData::RebuildFromHistory(history_db, | 324 private_data_ = URLIndexPrivateData::RebuildFromHistory(history_db, |
317 languages_, | 325 languages_, |
318 scheme_whitelist_); | 326 scheme_whitelist_); |
319 } | 327 } |
320 | 328 |
321 // Saving to Cache ------------------------------------------------------------- | 329 // Saving to Cache ------------------------------------------------------------- |
322 | 330 |
323 void InMemoryURLIndex::PostSaveToCacheFileTask() { | 331 void InMemoryURLIndex::PostSaveToCacheFileTask() { |
324 base::FilePath path; | 332 base::FilePath path; |
325 if (!GetCacheFilePath(&path)) | 333 if (!GetCacheFilePath(&path)) |
(...skipping 17 matching lines...) Expand all Loading... |
343 base::Bind(DeleteCacheFile, path)); | 351 base::Bind(DeleteCacheFile, path)); |
344 } | 352 } |
345 } | 353 } |
346 | 354 |
347 void InMemoryURLIndex::OnCacheSaveDone(bool succeeded) { | 355 void InMemoryURLIndex::OnCacheSaveDone(bool succeeded) { |
348 if (save_cache_observer_) | 356 if (save_cache_observer_) |
349 save_cache_observer_->OnCacheSaveFinished(succeeded); | 357 save_cache_observer_->OnCacheSaveFinished(succeeded); |
350 } | 358 } |
351 | 359 |
352 } // namespace history | 360 } // namespace history |
OLD | NEW |