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/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/history/history_service.h" | 10 #include "chrome/browser/history/history_service.h" |
11 #include "chrome/browser/history/history_service_factory.h" | |
12 #include "chrome/browser/history/url_index_private_data.h" | 11 #include "chrome/browser/history/url_index_private_data.h" |
13 #include "chrome/browser/profiles/profile.h" | |
14 #include "chrome/common/url_constants.h" | 12 #include "chrome/common/url_constants.h" |
15 #include "components/history/core/browser/url_database.h" | 13 #include "components/history/core/browser/url_database.h" |
16 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
17 | 15 |
18 using in_memory_url_index::InMemoryURLIndexCacheItem; | 16 using in_memory_url_index::InMemoryURLIndexCacheItem; |
19 | 17 |
20 namespace history { | 18 namespace history { |
21 | 19 |
22 // Called by DoSaveToCacheFile to delete any old cache file at |path| when | 20 // Called by DoSaveToCacheFile to delete any old cache file at |path| when |
23 // there is no private data to save. Runs on the FILE thread. | 21 // there is no private data to save. Runs on the FILE thread. |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 DoneRunOnMainThread() { | 72 DoneRunOnMainThread() { |
75 index_->DoneRebuidingPrivateDataFromHistoryDB(succeeded_, data_); | 73 index_->DoneRebuidingPrivateDataFromHistoryDB(succeeded_, data_); |
76 } | 74 } |
77 | 75 |
78 InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask:: | 76 InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask:: |
79 ~RebuildPrivateDataFromHistoryDBTask() { | 77 ~RebuildPrivateDataFromHistoryDBTask() { |
80 } | 78 } |
81 | 79 |
82 // InMemoryURLIndex ------------------------------------------------------------ | 80 // InMemoryURLIndex ------------------------------------------------------------ |
83 | 81 |
84 InMemoryURLIndex::InMemoryURLIndex(Profile* profile, | 82 InMemoryURLIndex::InMemoryURLIndex(HistoryService* history_service, |
85 HistoryService* history_service, | |
86 const base::FilePath& history_dir, | 83 const base::FilePath& history_dir, |
87 const std::string& languages, | 84 const std::string& languages, |
88 HistoryClient* history_client) | 85 HistoryClient* history_client) |
89 : profile_(profile), | 86 : history_service_(history_service), |
90 history_service_(history_service), | |
91 history_client_(history_client), | 87 history_client_(history_client), |
92 history_dir_(history_dir), | 88 history_dir_(history_dir), |
93 languages_(languages), | 89 languages_(languages), |
94 private_data_(new URLIndexPrivateData), | 90 private_data_(new URLIndexPrivateData), |
95 restore_cache_observer_(NULL), | 91 restore_cache_observer_(NULL), |
96 save_cache_observer_(NULL), | 92 save_cache_observer_(NULL), |
97 shutdown_(false), | 93 shutdown_(false), |
98 restored_(false), | 94 restored_(false), |
99 needs_to_be_cached_(false), | 95 needs_to_be_cached_(false) { |
100 history_service_observer_(this) { | |
101 InitializeSchemeWhitelist(&scheme_whitelist_); | 96 InitializeSchemeWhitelist(&scheme_whitelist_); |
102 // TODO(mrossetti): Register for language change notifications. | 97 // TODO(mrossetti): Register for language change notifications. |
103 if (history_service_) | 98 if (history_service_) |
104 history_service_observer_.Add(history_service_); | 99 history_service_->AddObserver(this); |
105 } | |
106 | |
107 // Called only by unit tests. | |
108 InMemoryURLIndex::InMemoryURLIndex() | |
109 : profile_(NULL), | |
110 history_service_(nullptr), | |
111 history_client_(NULL), | |
112 private_data_(new URLIndexPrivateData), | |
113 restore_cache_observer_(NULL), | |
114 save_cache_observer_(NULL), | |
115 shutdown_(false), | |
116 restored_(false), | |
117 needs_to_be_cached_(false), | |
118 history_service_observer_(this) { | |
119 InitializeSchemeWhitelist(&scheme_whitelist_); | |
120 } | 100 } |
121 | 101 |
122 InMemoryURLIndex::~InMemoryURLIndex() { | 102 InMemoryURLIndex::~InMemoryURLIndex() { |
123 // If there was a history directory (which there won't be for some unit tests) | 103 // If there was a history directory (which there won't be for some unit tests) |
124 // then insure that the cache has already been saved. | 104 // then insure that the cache has already been saved. |
125 DCHECK(history_dir_.empty() || !needs_to_be_cached_); | 105 DCHECK(history_dir_.empty() || !needs_to_be_cached_); |
| 106 DCHECK(!history_service_); |
| 107 DCHECK(shutdown_); |
126 } | 108 } |
127 | 109 |
128 void InMemoryURLIndex::Init() { | 110 void InMemoryURLIndex::Init() { |
129 PostRestoreFromCacheFileTask(); | 111 PostRestoreFromCacheFileTask(); |
130 } | 112 } |
131 | 113 |
132 void InMemoryURLIndex::ShutDown() { | 114 void InMemoryURLIndex::ShutDown() { |
133 history_service_observer_.RemoveAll(); | 115 if (history_service_) { |
| 116 history_service_->RemoveObserver(this); |
| 117 history_service_ = nullptr; |
| 118 } |
134 cache_reader_tracker_.TryCancelAll(); | 119 cache_reader_tracker_.TryCancelAll(); |
135 shutdown_ = true; | 120 shutdown_ = true; |
136 base::FilePath path; | 121 base::FilePath path; |
137 if (!GetCacheFilePath(&path)) | 122 if (!GetCacheFilePath(&path)) |
138 return; | 123 return; |
139 private_data_tracker_.TryCancelAll(); | 124 private_data_tracker_.TryCancelAll(); |
140 URLIndexPrivateData::WritePrivateDataToCacheFileTask(private_data_, path); | 125 URLIndexPrivateData::WritePrivateDataToCacheFileTask(private_data_, path); |
141 needs_to_be_cached_ = false; | 126 needs_to_be_cached_ = false; |
142 } | 127 } |
143 | 128 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 } | 241 } |
257 | 242 |
258 void InMemoryURLIndex::OnCacheLoadDone( | 243 void InMemoryURLIndex::OnCacheLoadDone( |
259 scoped_refptr<URLIndexPrivateData> private_data) { | 244 scoped_refptr<URLIndexPrivateData> private_data) { |
260 if (private_data.get() && !private_data->Empty()) { | 245 if (private_data.get() && !private_data->Empty()) { |
261 private_data_tracker_.TryCancelAll(); | 246 private_data_tracker_.TryCancelAll(); |
262 private_data_ = private_data; | 247 private_data_ = private_data; |
263 restored_ = true; | 248 restored_ = true; |
264 if (restore_cache_observer_) | 249 if (restore_cache_observer_) |
265 restore_cache_observer_->OnCacheRestoreFinished(true); | 250 restore_cache_observer_->OnCacheRestoreFinished(true); |
266 } else if (profile_) { | 251 } else if (history_service_) { |
267 // When unable to restore from the cache file delete the cache file, if | 252 // 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, | 253 // it exists, and then rebuild from the history database if it's available, |
269 // otherwise wait until the history database loaded and then rebuild. | 254 // otherwise wait until the history database loaded and then rebuild. |
270 base::FilePath path; | 255 base::FilePath path; |
271 if (!GetCacheFilePath(&path) || shutdown_) | 256 if (!GetCacheFilePath(&path) || shutdown_) |
272 return; | 257 return; |
273 content::BrowserThread::PostBlockingPoolTask( | 258 content::BrowserThread::PostBlockingPoolTask( |
274 FROM_HERE, base::Bind(DeleteCacheFile, path)); | 259 FROM_HERE, base::Bind(DeleteCacheFile, path)); |
275 HistoryService* service = | 260 if (history_service_->backend_loaded()) { |
276 HistoryServiceFactory::GetForProfileWithoutCreating(profile_); | 261 ScheduleRebuildFromHistory(); |
277 if (service) { | |
278 if (!service->backend_loaded()) { | |
279 if (!history_service_observer_.IsObserving(service)) | |
280 history_service_observer_.Add(service); | |
281 } else { | |
282 ScheduleRebuildFromHistory(); | |
283 } | |
284 } | 262 } |
285 } | 263 } |
286 } | 264 } |
287 | 265 |
288 // Restoring from the History DB ----------------------------------------------- | 266 // Restoring from the History DB ----------------------------------------------- |
289 | 267 |
290 void InMemoryURLIndex::ScheduleRebuildFromHistory() { | 268 void InMemoryURLIndex::ScheduleRebuildFromHistory() { |
291 HistoryService* service = HistoryServiceFactory::GetForProfile( | 269 DCHECK(history_service_); |
292 profile_, ServiceAccessType::EXPLICIT_ACCESS); | 270 history_service_->ScheduleDBTask( |
293 service->ScheduleDBTask( | |
294 scoped_ptr<history::HistoryDBTask>( | 271 scoped_ptr<history::HistoryDBTask>( |
295 new InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask( | 272 new InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask( |
296 this, languages_, scheme_whitelist_)), | 273 this, languages_, scheme_whitelist_)), |
297 &cache_reader_tracker_); | 274 &cache_reader_tracker_); |
298 } | 275 } |
299 | 276 |
300 void InMemoryURLIndex::DoneRebuidingPrivateDataFromHistoryDB( | 277 void InMemoryURLIndex::DoneRebuidingPrivateDataFromHistoryDB( |
301 bool succeeded, | 278 bool succeeded, |
302 scoped_refptr<URLIndexPrivateData> private_data) { | 279 scoped_refptr<URLIndexPrivateData> private_data) { |
303 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 280 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 base::Bind(DeleteCacheFile, path)); | 324 base::Bind(DeleteCacheFile, path)); |
348 } | 325 } |
349 } | 326 } |
350 | 327 |
351 void InMemoryURLIndex::OnCacheSaveDone(bool succeeded) { | 328 void InMemoryURLIndex::OnCacheSaveDone(bool succeeded) { |
352 if (save_cache_observer_) | 329 if (save_cache_observer_) |
353 save_cache_observer_->OnCacheSaveFinished(succeeded); | 330 save_cache_observer_->OnCacheSaveFinished(succeeded); |
354 } | 331 } |
355 | 332 |
356 } // namespace history | 333 } // namespace history |
OLD | NEW |