Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: chrome/browser/history/in_memory_url_index.cc

Issue 9316109: Move Ownership of IMUI to HistoryService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_ |= 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_ |= 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_ |= DeleteURL(row->url());
155 }
156 }
157
158 bool InMemoryURLIndex::UpdateURL(const URLRow& row) {
159 return private_data_->UpdateURL(row);
160 }
161
162 bool InMemoryURLIndex::DeleteURL(const GURL& url) {
163 return private_data_->DeleteURL(url);
164 }
165
166 // Restoring from Cache --------------------------------------------------------
167
168 void InMemoryURLIndex::RestoreFromCacheFile() {
169 FilePath path;
170 if (GetCacheFilePath(&path) && !shutdown_)
171 DoRestoreFromCacheFile(path);
172 }
173
174 void InMemoryURLIndex::DoRestoreFromCacheFile(const FilePath& path) {
175 if (private_data_->RestoreFromFile(path))
176 return;
177
178 // When unable to restore from the cache file we must rebuild from the
179 // history database.
180 HistoryService* service = profile_->GetHistoryServiceWithoutCreating();
181 if (service && service->backend_loaded())
182 ScheduleRebuildFromHistory();
183 // We must wait to rebuild until the history backend has been loaded.
184 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_LOADED,
185 content::Source<Profile>(profile_));
186 }
187
188 // Restoring from the History DB -----------------------------------------------
189
190 void InMemoryURLIndex::ScheduleRebuildFromHistory() {
191 HistoryService* service =
192 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
193 service->ScheduleDBTask(
194 new InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask(this),
195 &cache_reader_consumer_);
Peter Kasting 2012/02/04 02:20:31 So much saner than the old threading model! Yay!
mrossetti 2012/02/04 04:30:53 :^)
196 }
197
198 void InMemoryURLIndex::DoneRebuidingPrivateDataFromHistoryDB(
199 URLIndexPrivateData* data) {
200 scoped_ptr<URLIndexPrivateData> private_data(data);
201 private_data_.swap(private_data);
202 // Cache the newly rebuilt index.
203 FilePath cache_file_path;
204 if (GetCacheFilePath(&cache_file_path))
205 private_data_->SaveToFile(cache_file_path);
206 }
207
208 void InMemoryURLIndex::RebuildFromHistory(HistoryDatabase* history_db) {
209 private_data_.reset(URLIndexPrivateData::RebuildFromHistory(history_db));
210 }
211
212 // Saving to Cache -------------------------------------------------------------
213
214 void InMemoryURLIndex::SaveToCacheFile() {
215 FilePath path;
216 if (GetCacheFilePath(&path))
217 DoSaveToCacheFile(path);
218 }
219
220 void InMemoryURLIndex::DoSaveToCacheFile(const FilePath& path) {
221 private_data_->SaveToFile(path);
84 } 222 }
85 223
86 } // namespace history 224 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698