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/bookmarks/bookmark_model_factory.h" | 10 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 index_->DoneRebuidingPrivateDataFromHistoryDB(succeeded_, data_); | 82 index_->DoneRebuidingPrivateDataFromHistoryDB(succeeded_, data_); |
83 } | 83 } |
84 | 84 |
85 InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask:: | 85 InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask:: |
86 ~RebuildPrivateDataFromHistoryDBTask() { | 86 ~RebuildPrivateDataFromHistoryDBTask() { |
87 } | 87 } |
88 | 88 |
89 // InMemoryURLIndex ------------------------------------------------------------ | 89 // InMemoryURLIndex ------------------------------------------------------------ |
90 | 90 |
91 InMemoryURLIndex::InMemoryURLIndex(Profile* profile, | 91 InMemoryURLIndex::InMemoryURLIndex(Profile* profile, |
| 92 HistoryService* history_service, |
92 const base::FilePath& history_dir, | 93 const base::FilePath& history_dir, |
93 const std::string& languages, | 94 const std::string& languages, |
94 HistoryClient* history_client) | 95 HistoryClient* history_client) |
95 : profile_(profile), | 96 : profile_(profile), |
| 97 history_service_(history_service), |
96 history_client_(history_client), | 98 history_client_(history_client), |
97 history_dir_(history_dir), | 99 history_dir_(history_dir), |
98 languages_(languages), | 100 languages_(languages), |
99 private_data_(new URLIndexPrivateData), | 101 private_data_(new URLIndexPrivateData), |
100 restore_cache_observer_(NULL), | 102 restore_cache_observer_(NULL), |
101 save_cache_observer_(NULL), | 103 save_cache_observer_(NULL), |
102 shutdown_(false), | 104 shutdown_(false), |
103 restored_(false), | 105 restored_(false), |
104 needs_to_be_cached_(false) { | 106 needs_to_be_cached_(false) { |
105 InitializeSchemeWhitelist(&scheme_whitelist_); | 107 InitializeSchemeWhitelist(&scheme_whitelist_); |
106 if (profile) { | 108 if (profile) { |
107 // TODO(mrossetti): Register for language change notifications. | 109 // TODO(mrossetti): Register for language change notifications. |
108 content::Source<Profile> source(profile); | 110 content::Source<Profile> source(profile); |
109 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URL_VISITED, source); | |
110 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_MODIFIED, | 111 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_MODIFIED, |
111 source); | 112 source); |
112 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, source); | 113 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, source); |
113 } | 114 } |
| 115 if (history_service_) |
| 116 history_service_->AddObserver(this); |
114 } | 117 } |
115 | 118 |
116 // Called only by unit tests. | 119 // Called only by unit tests. |
117 InMemoryURLIndex::InMemoryURLIndex() | 120 InMemoryURLIndex::InMemoryURLIndex() |
118 : profile_(NULL), | 121 : profile_(NULL), |
| 122 history_service_(nullptr), |
119 history_client_(NULL), | 123 history_client_(NULL), |
120 private_data_(new URLIndexPrivateData), | 124 private_data_(new URLIndexPrivateData), |
121 restore_cache_observer_(NULL), | 125 restore_cache_observer_(NULL), |
122 save_cache_observer_(NULL), | 126 save_cache_observer_(NULL), |
123 shutdown_(false), | 127 shutdown_(false), |
124 restored_(false), | 128 restored_(false), |
125 needs_to_be_cached_(false) { | 129 needs_to_be_cached_(false) { |
126 InitializeSchemeWhitelist(&scheme_whitelist_); | 130 InitializeSchemeWhitelist(&scheme_whitelist_); |
127 } | 131 } |
128 | 132 |
129 InMemoryURLIndex::~InMemoryURLIndex() { | 133 InMemoryURLIndex::~InMemoryURLIndex() { |
130 // If there was a history directory (which there won't be for some unit tests) | 134 // If there was a history directory (which there won't be for some unit tests) |
131 // then insure that the cache has already been saved. | 135 // then insure that the cache has already been saved. |
132 DCHECK(history_dir_.empty() || !needs_to_be_cached_); | 136 DCHECK(history_dir_.empty() || !needs_to_be_cached_); |
133 } | 137 } |
134 | 138 |
135 void InMemoryURLIndex::Init() { | 139 void InMemoryURLIndex::Init() { |
136 PostRestoreFromCacheFileTask(); | 140 PostRestoreFromCacheFileTask(); |
137 } | 141 } |
138 | 142 |
139 void InMemoryURLIndex::ShutDown() { | 143 void InMemoryURLIndex::ShutDown() { |
| 144 if (history_service_) |
| 145 history_service_->RemoveObserver(this); |
140 registrar_.RemoveAll(); | 146 registrar_.RemoveAll(); |
141 cache_reader_tracker_.TryCancelAll(); | 147 cache_reader_tracker_.TryCancelAll(); |
142 shutdown_ = true; | 148 shutdown_ = true; |
143 base::FilePath path; | 149 base::FilePath path; |
144 if (!GetCacheFilePath(&path)) | 150 if (!GetCacheFilePath(&path)) |
145 return; | 151 return; |
146 private_data_tracker_.TryCancelAll(); | 152 private_data_tracker_.TryCancelAll(); |
147 URLIndexPrivateData::WritePrivateDataToCacheFileTask(private_data_, path); | 153 URLIndexPrivateData::WritePrivateDataToCacheFileTask(private_data_, path); |
148 needs_to_be_cached_ = false; | 154 needs_to_be_cached_ = false; |
149 } | 155 } |
(...skipping 26 matching lines...) Expand all Loading... |
176 // Updating -------------------------------------------------------------------- | 182 // Updating -------------------------------------------------------------------- |
177 | 183 |
178 void InMemoryURLIndex::DeleteURL(const GURL& url) { | 184 void InMemoryURLIndex::DeleteURL(const GURL& url) { |
179 private_data_->DeleteURL(url); | 185 private_data_->DeleteURL(url); |
180 } | 186 } |
181 | 187 |
182 void InMemoryURLIndex::Observe(int notification_type, | 188 void InMemoryURLIndex::Observe(int notification_type, |
183 const content::NotificationSource& source, | 189 const content::NotificationSource& source, |
184 const content::NotificationDetails& details) { | 190 const content::NotificationDetails& details) { |
185 switch (notification_type) { | 191 switch (notification_type) { |
186 case chrome::NOTIFICATION_HISTORY_URL_VISITED: | |
187 OnURLVisited(content::Details<URLVisitedDetails>(details).ptr()); | |
188 break; | |
189 case chrome::NOTIFICATION_HISTORY_URLS_MODIFIED: | 192 case chrome::NOTIFICATION_HISTORY_URLS_MODIFIED: |
190 OnURLsModified( | 193 OnURLsModified( |
191 content::Details<history::URLsModifiedDetails>(details).ptr()); | 194 content::Details<history::URLsModifiedDetails>(details).ptr()); |
192 break; | 195 break; |
193 case chrome::NOTIFICATION_HISTORY_URLS_DELETED: | 196 case chrome::NOTIFICATION_HISTORY_URLS_DELETED: |
194 OnURLsDeleted( | 197 OnURLsDeleted( |
195 content::Details<history::URLsDeletedDetails>(details).ptr()); | 198 content::Details<history::URLsDeletedDetails>(details).ptr()); |
196 break; | 199 break; |
197 case chrome::NOTIFICATION_HISTORY_LOADED: | 200 case chrome::NOTIFICATION_HISTORY_LOADED: |
198 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_LOADED, | 201 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_LOADED, |
199 content::Source<Profile>(profile_)); | 202 content::Source<Profile>(profile_)); |
200 ScheduleRebuildFromHistory(); | 203 ScheduleRebuildFromHistory(); |
201 break; | 204 break; |
202 default: | 205 default: |
203 // For simplicity, the unit tests send us all notifications, even when | 206 // For simplicity, the unit tests send us all notifications, even when |
204 // we haven't registered for them, so don't assert here. | 207 // we haven't registered for them, so don't assert here. |
205 break; | 208 break; |
206 } | 209 } |
207 } | 210 } |
208 | 211 |
209 void InMemoryURLIndex::OnURLVisited(const URLVisitedDetails* details) { | 212 void InMemoryURLIndex::OnURLVisited(HistoryService* history_service, |
210 HistoryService* service = | 213 ui::PageTransition transition, |
211 HistoryServiceFactory::GetForProfile(profile_, | 214 const URLRow& row, |
212 Profile::EXPLICIT_ACCESS); | 215 const RedirectList& redirects, |
213 needs_to_be_cached_ |= private_data_->UpdateURL(service, | 216 base::Time visit_time) { |
214 details->row, | 217 DCHECK(history_service_ == history_service); |
| 218 needs_to_be_cached_ |= private_data_->UpdateURL(history_service_, |
| 219 row, |
215 languages_, | 220 languages_, |
216 scheme_whitelist_, | 221 scheme_whitelist_, |
217 &private_data_tracker_); | 222 &private_data_tracker_); |
218 } | 223 } |
219 | 224 |
220 void InMemoryURLIndex::OnURLsModified(const URLsModifiedDetails* details) { | 225 void InMemoryURLIndex::OnURLsModified(const URLsModifiedDetails* details) { |
221 HistoryService* service = | 226 HistoryService* service = |
222 HistoryServiceFactory::GetForProfile(profile_, | 227 HistoryServiceFactory::GetForProfile(profile_, |
223 Profile::EXPLICIT_ACCESS); | 228 Profile::EXPLICIT_ACCESS); |
224 for (URLRows::const_iterator row = details->changed_urls.begin(); | 229 for (URLRows::const_iterator row = details->changed_urls.begin(); |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 base::Bind(DeleteCacheFile, path)); | 376 base::Bind(DeleteCacheFile, path)); |
372 } | 377 } |
373 } | 378 } |
374 | 379 |
375 void InMemoryURLIndex::OnCacheSaveDone(bool succeeded) { | 380 void InMemoryURLIndex::OnCacheSaveDone(bool succeeded) { |
376 if (save_cache_observer_) | 381 if (save_cache_observer_) |
377 save_cache_observer_->OnCacheSaveFinished(succeeded); | 382 save_cache_observer_->OnCacheSaveFinished(succeeded); |
378 } | 383 } |
379 | 384 |
380 } // namespace history | 385 } // namespace history |
OLD | NEW |