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

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

Issue 27057004: Make the OmniboxNavigationObserver update the ShortcutsProvider. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 2 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/shortcuts_backend.h" 5 #include "chrome/browser/history/shortcuts_backend.h"
6 6
7 #include <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 if (!suppress_db) { 153 if (!suppress_db) {
154 db_ = new ShortcutsDatabase( 154 db_ = new ShortcutsDatabase(
155 profile->GetPath().Append(chrome::kShortcutsDatabaseName)); 155 profile->GetPath().Append(chrome::kShortcutsDatabaseName));
156 } 156 }
157 // |profile| can be NULL in tests. 157 // |profile| can be NULL in tests.
158 if (profile) { 158 if (profile) {
159 notification_registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, 159 notification_registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
160 content::Source<Profile>(profile)); 160 content::Source<Profile>(profile));
161 notification_registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, 161 notification_registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED,
162 content::Source<Profile>(profile)); 162 content::Source<Profile>(profile));
163 notification_registrar_.Add(this, chrome::NOTIFICATION_OMNIBOX_OPENED_URL,
164 content::Source<Profile>(profile));
165 } 163 }
166 } 164 }
167 165
168 bool ShortcutsBackend::Init() { 166 bool ShortcutsBackend::Init() {
169 if (current_state_ != NOT_INITIALIZED) 167 if (current_state_ != NOT_INITIALIZED)
170 return false; 168 return false;
171 169
172 if (no_db_access_) { 170 if (no_db_access_) {
173 current_state_ = INITIALIZED; 171 current_state_ = INITIALIZED;
174 return true; 172 return true;
175 } 173 }
176 174
177 current_state_ = INITIALIZING; 175 current_state_ = INITIALIZING;
178 return BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, 176 return BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
179 base::Bind(&ShortcutsBackend::InitInternal, this)); 177 base::Bind(&ShortcutsBackend::InitInternal, this));
180 } 178 }
181 179
182 bool ShortcutsBackend::DeleteShortcutsWithUrl(const GURL& shortcut_url) { 180 bool ShortcutsBackend::DeleteShortcutsWithUrl(const GURL& shortcut_url) {
183 return initialized() && DeleteShortcutsWithUrl(shortcut_url, true); 181 return initialized() && DeleteShortcutsWithUrl(shortcut_url, true);
184 } 182 }
185 183
186 void ShortcutsBackend::AddObserver(ShortcutsBackendObserver* obs) { 184 void ShortcutsBackend::AddObserver(ShortcutsBackendObserver* obs) {
187 observer_list_.AddObserver(obs); 185 observer_list_.AddObserver(obs);
188 } 186 }
189 187
190 void ShortcutsBackend::RemoveObserver(ShortcutsBackendObserver* obs) { 188 void ShortcutsBackend::RemoveObserver(ShortcutsBackendObserver* obs) {
191 observer_list_.RemoveObserver(obs); 189 observer_list_.RemoveObserver(obs);
192 } 190 }
193 191
192 void ShortcutsBackend::OnOmniboxNavigation(const string16& text,
193 const AutocompleteMatch& match) {
194 const string16 text_lowercase(base::i18n::ToLower(text));
195 const base::Time now(base::Time::Now());
196 for (ShortcutMap::const_iterator it(
197 shortcuts_map_.lower_bound(text_lowercase));
198 it != shortcuts_map_.end() &&
199 StartsWith(it->first, text_lowercase, true); ++it) {
200 if (match.destination_url == it->second.match_core.destination_url) {
201 UpdateShortcut(Shortcut(it->second.id, text, Shortcut::MatchCore(match),
202 now, it->second.number_of_hits + 1));
203 return;
204 }
205 }
206 AddShortcut(Shortcut(base::GenerateGUID(), text, Shortcut::MatchCore(match),
207 now, 1));
208 }
209
194 ShortcutsBackend::~ShortcutsBackend() { 210 ShortcutsBackend::~ShortcutsBackend() {
195 } 211 }
196 212
197 void ShortcutsBackend::ShutdownOnUIThread() { 213 void ShortcutsBackend::ShutdownOnUIThread() {
198 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || 214 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
199 BrowserThread::CurrentlyOn(BrowserThread::UI)); 215 BrowserThread::CurrentlyOn(BrowserThread::UI));
200 notification_registrar_.RemoveAll(); 216 notification_registrar_.RemoveAll();
201 } 217 }
202 218
203 void ShortcutsBackend::Observe(int type, 219 void ShortcutsBackend::Observe(int type,
204 const content::NotificationSource& source, 220 const content::NotificationSource& source,
205 const content::NotificationDetails& details) { 221 const content::NotificationDetails& details) {
206 if (!initialized()) 222 if (!initialized())
207 return; 223 return;
208 224
209 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) { 225 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) {
210 // When an extension is unloaded, we want to remove any Shortcuts associated 226 // When an extension is unloaded, we want to remove any Shortcuts associated
211 // with it. 227 // with it.
212 DeleteShortcutsWithUrl(content::Details<extensions::UnloadedExtensionInfo>( 228 DeleteShortcutsWithUrl(content::Details<extensions::UnloadedExtensionInfo>(
213 details)->extension->url(), false); 229 details)->extension->url(), false);
214 return; 230 return;
215 } 231 }
216 232
217 if (type == chrome::NOTIFICATION_HISTORY_URLS_DELETED) { 233 DCHECK_EQ(chrome::NOTIFICATION_HISTORY_URLS_DELETED, type);
218 if (content::Details<const history::URLsDeletedDetails>(details)-> 234 const history::URLsDeletedDetails* deleted_details =
219 all_history) { 235 content::Details<const history::URLsDeletedDetails>(details).ptr();
220 DeleteAllShortcuts(); 236 if (deleted_details->all_history)
221 } 237 DeleteAllShortcuts();
222 const URLRows& rows( 238 const URLRows& rows(deleted_details->rows);
223 content::Details<const history::URLsDeletedDetails>(details)->rows); 239 std::vector<std::string> shortcut_ids;
224 std::vector<std::string> shortcut_ids;
225 240
226 for (GuidMap::const_iterator it(guid_map_.begin()); it != guid_map_.end(); 241 for (GuidMap::const_iterator it(guid_map_.begin()); it != guid_map_.end();
227 ++it) { 242 ++it) {
228 if (std::find_if( 243 if (std::find_if(
229 rows.begin(), rows.end(), URLRow::URLRowHasURL( 244 rows.begin(), rows.end(), URLRow::URLRowHasURL(
230 it->second->second.match_core.destination_url)) != rows.end()) 245 it->second->second.match_core.destination_url)) != rows.end())
231 shortcut_ids.push_back(it->first); 246 shortcut_ids.push_back(it->first);
232 }
233 DeleteShortcutsWithIds(shortcut_ids);
234 return;
235 } 247 }
236 248 DeleteShortcutsWithIds(shortcut_ids);
237 DCHECK(type == chrome::NOTIFICATION_OMNIBOX_OPENED_URL);
238
239 OmniboxLog* log = content::Details<OmniboxLog>(details).ptr();
240 string16 text_lowercase(base::i18n::ToLower(log->text));
241
242 const AutocompleteMatch& match(log->result.match_at(log->selected_index));
243 for (ShortcutMap::const_iterator it(
244 shortcuts_map_.lower_bound(text_lowercase));
245 it != shortcuts_map_.end() &&
246 StartsWith(it->first, text_lowercase, true); ++it) {
247 if (match.destination_url == it->second.match_core.destination_url) {
248 UpdateShortcut(Shortcut(it->second.id, log->text,
249 Shortcut::MatchCore(match), base::Time::Now(),
250 it->second.number_of_hits + 1));
251 return;
252 }
253 }
254 AddShortcut(Shortcut(base::GenerateGUID(), log->text,
255 Shortcut::MatchCore(match), base::Time::Now(), 1));
256 } 249 }
257 250
258 void ShortcutsBackend::InitInternal() { 251 void ShortcutsBackend::InitInternal() {
259 DCHECK(current_state_ == INITIALIZING); 252 DCHECK(current_state_ == INITIALIZING);
260 db_->Init(); 253 db_->Init();
261 ShortcutsDatabase::GuidToShortcutMap shortcuts; 254 ShortcutsDatabase::GuidToShortcutMap shortcuts;
262 db_->LoadShortcuts(&shortcuts); 255 db_->LoadShortcuts(&shortcuts);
263 temp_shortcuts_map_.reset(new ShortcutMap); 256 temp_shortcuts_map_.reset(new ShortcutMap);
264 temp_guid_map_.reset(new GuidMap); 257 temp_guid_map_.reset(new GuidMap);
265 for (ShortcutsDatabase::GuidToShortcutMap::const_iterator it( 258 for (ShortcutsDatabase::GuidToShortcutMap::const_iterator it(
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 shortcuts_map_.clear(); 352 shortcuts_map_.clear();
360 guid_map_.clear(); 353 guid_map_.clear();
361 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_, 354 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
362 OnShortcutsChanged()); 355 OnShortcutsChanged());
363 return no_db_access_ || BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, 356 return no_db_access_ || BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
364 base::Bind(base::IgnoreResult(&ShortcutsDatabase::DeleteAllShortcuts), 357 base::Bind(base::IgnoreResult(&ShortcutsDatabase::DeleteAllShortcuts),
365 db_.get())); 358 db_.get()));
366 } 359 }
367 360
368 } // namespace history 361 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698