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_); |
126 } | 106 } |
127 | 107 |
128 void InMemoryURLIndex::Init() { | 108 void InMemoryURLIndex::Init() { |
129 PostRestoreFromCacheFileTask(); | 109 PostRestoreFromCacheFileTask(); |
130 } | 110 } |
131 | 111 |
132 void InMemoryURLIndex::ShutDown() { | 112 void InMemoryURLIndex::ShutDown() { |
133 history_service_observer_.RemoveAll(); | 113 if (history_service_) { |
114 history_service_->RemoveObserver(this); | |
droger
2015/01/27 09:32:32
What happens if ShutDown is never called, is the o
sdefresne
2015/01/27 10:13:56
Yes, the observer would have been leaked. I added
| |
115 history_service_ = nullptr; | |
116 } | |
134 cache_reader_tracker_.TryCancelAll(); | 117 cache_reader_tracker_.TryCancelAll(); |
135 shutdown_ = true; | 118 shutdown_ = true; |
136 base::FilePath path; | 119 base::FilePath path; |
137 if (!GetCacheFilePath(&path)) | 120 if (!GetCacheFilePath(&path)) |
138 return; | 121 return; |
139 private_data_tracker_.TryCancelAll(); | 122 private_data_tracker_.TryCancelAll(); |
140 URLIndexPrivateData::WritePrivateDataToCacheFileTask(private_data_, path); | 123 URLIndexPrivateData::WritePrivateDataToCacheFileTask(private_data_, path); |
141 needs_to_be_cached_ = false; | 124 needs_to_be_cached_ = false; |
142 } | 125 } |
143 | 126 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
256 } | 239 } |
257 | 240 |
258 void InMemoryURLIndex::OnCacheLoadDone( | 241 void InMemoryURLIndex::OnCacheLoadDone( |
259 scoped_refptr<URLIndexPrivateData> private_data) { | 242 scoped_refptr<URLIndexPrivateData> private_data) { |
260 if (private_data.get() && !private_data->Empty()) { | 243 if (private_data.get() && !private_data->Empty()) { |
261 private_data_tracker_.TryCancelAll(); | 244 private_data_tracker_.TryCancelAll(); |
262 private_data_ = private_data; | 245 private_data_ = private_data; |
263 restored_ = true; | 246 restored_ = true; |
264 if (restore_cache_observer_) | 247 if (restore_cache_observer_) |
265 restore_cache_observer_->OnCacheRestoreFinished(true); | 248 restore_cache_observer_->OnCacheRestoreFinished(true); |
266 } else if (profile_) { | 249 } else if (history_service_) { |
267 // When unable to restore from the cache file delete the cache file, if | 250 // 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, | 251 // it exists, and then rebuild from the history database if it's available, |
269 // otherwise wait until the history database loaded and then rebuild. | 252 // otherwise wait until the history database loaded and then rebuild. |
270 base::FilePath path; | 253 base::FilePath path; |
271 if (!GetCacheFilePath(&path) || shutdown_) | 254 if (!GetCacheFilePath(&path) || shutdown_) |
272 return; | 255 return; |
273 content::BrowserThread::PostBlockingPoolTask( | 256 content::BrowserThread::PostBlockingPoolTask( |
274 FROM_HERE, base::Bind(DeleteCacheFile, path)); | 257 FROM_HERE, base::Bind(DeleteCacheFile, path)); |
275 HistoryService* service = | 258 if (history_service_->backend_loaded()) { |
276 HistoryServiceFactory::GetForProfileWithoutCreating(profile_); | 259 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 } | 260 } |
285 } | 261 } |
286 } | 262 } |
287 | 263 |
288 // Restoring from the History DB ----------------------------------------------- | 264 // Restoring from the History DB ----------------------------------------------- |
289 | 265 |
290 void InMemoryURLIndex::ScheduleRebuildFromHistory() { | 266 void InMemoryURLIndex::ScheduleRebuildFromHistory() { |
291 HistoryService* service = HistoryServiceFactory::GetForProfile( | 267 DCHECK(history_service_); |
292 profile_, ServiceAccessType::EXPLICIT_ACCESS); | 268 history_service_->ScheduleDBTask( |
293 service->ScheduleDBTask( | |
294 scoped_ptr<history::HistoryDBTask>( | 269 scoped_ptr<history::HistoryDBTask>( |
295 new InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask( | 270 new InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask( |
296 this, languages_, scheme_whitelist_)), | 271 this, languages_, scheme_whitelist_)), |
297 &cache_reader_tracker_); | 272 &cache_reader_tracker_); |
298 } | 273 } |
299 | 274 |
300 void InMemoryURLIndex::DoneRebuidingPrivateDataFromHistoryDB( | 275 void InMemoryURLIndex::DoneRebuidingPrivateDataFromHistoryDB( |
301 bool succeeded, | 276 bool succeeded, |
302 scoped_refptr<URLIndexPrivateData> private_data) { | 277 scoped_refptr<URLIndexPrivateData> private_data) { |
303 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 278 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)); | 322 base::Bind(DeleteCacheFile, path)); |
348 } | 323 } |
349 } | 324 } |
350 | 325 |
351 void InMemoryURLIndex::OnCacheSaveDone(bool succeeded) { | 326 void InMemoryURLIndex::OnCacheSaveDone(bool succeeded) { |
352 if (save_cache_observer_) | 327 if (save_cache_observer_) |
353 save_cache_observer_->OnCacheSaveFinished(succeeded); | 328 save_cache_observer_->OnCacheSaveFinished(succeeded); |
354 } | 329 } |
355 | 330 |
356 } // namespace history | 331 } // namespace history |
OLD | NEW |