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

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 string16 text_lowercase(base::i18n::ToLower(text));
Bart N. 2013/10/14 21:31:54 const
Peter Kasting 2013/10/16 01:28:55 I agree, and I made the change here, but note that
195
196 for (ShortcutMap::const_iterator it(
Bart N. 2013/10/14 21:31:54 Hmmm... why don't you use GUID -> shortcut iterato
Peter Kasting 2013/10/16 01:28:55 I'm confused. How is using the GUID map useful?
Bart N. 2013/10/16 17:42:47 Sorry, my mistake. I thought that this was a URL h
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 base::Time::Now(),
Bart N. 2013/10/14 21:31:54 For consistency and performance, you should take t
Peter Kasting 2013/10/16 01:28:55 Done.
203 it->second.number_of_hits + 1));
204 return;
205 }
206 }
207 AddShortcut(Shortcut(base::GenerateGUID(), text, Shortcut::MatchCore(match),
208 base::Time::Now(), 1));
Bart N. 2013/10/14 21:31:54 Replace it with "now" introduced in previous step.
Peter Kasting 2013/10/16 01:28:55 Done.
209 }
210
194 ShortcutsBackend::~ShortcutsBackend() { 211 ShortcutsBackend::~ShortcutsBackend() {
195 } 212 }
196 213
197 void ShortcutsBackend::ShutdownOnUIThread() { 214 void ShortcutsBackend::ShutdownOnUIThread() {
198 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || 215 DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) ||
199 BrowserThread::CurrentlyOn(BrowserThread::UI)); 216 BrowserThread::CurrentlyOn(BrowserThread::UI));
200 notification_registrar_.RemoveAll(); 217 notification_registrar_.RemoveAll();
201 } 218 }
202 219
203 void ShortcutsBackend::Observe(int type, 220 void ShortcutsBackend::Observe(int type,
204 const content::NotificationSource& source, 221 const content::NotificationSource& source,
205 const content::NotificationDetails& details) { 222 const content::NotificationDetails& details) {
206 if (!initialized()) 223 if (!initialized())
207 return; 224 return;
208 225
209 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) { 226 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) {
210 // When an extension is unloaded, we want to remove any Shortcuts associated 227 // When an extension is unloaded, we want to remove any Shortcuts associated
211 // with it. 228 // with it.
212 DeleteShortcutsWithUrl(content::Details<extensions::UnloadedExtensionInfo>( 229 DeleteShortcutsWithUrl(content::Details<extensions::UnloadedExtensionInfo>(
213 details)->extension->url(), false); 230 details)->extension->url(), false);
214 return; 231 return;
215 } 232 }
216 233
217 if (type == chrome::NOTIFICATION_HISTORY_URLS_DELETED) { 234 DCHECK_EQ(chrome::NOTIFICATION_HISTORY_URLS_DELETED, type);
218 if (content::Details<const history::URLsDeletedDetails>(details)-> 235 if (content::Details<const history::URLsDeletedDetails>(details)->
Bart N. 2013/10/14 21:31:54 Style: -> should be on the second line.
Peter Kasting 2013/10/16 01:28:55 Operators go on ends of lines, not starts. But I
Bart N. 2013/10/16 17:42:47 Interesting, unlike the internal Google style.
219 all_history) { 236 all_history) {
220 DeleteAllShortcuts(); 237 DeleteAllShortcuts();
221 } 238 }
222 const URLRows& rows( 239 const URLRows& rows(
223 content::Details<const history::URLsDeletedDetails>(details)->rows); 240 content::Details<const history::URLsDeletedDetails>(details)->rows);
224 std::vector<std::string> shortcut_ids; 241 std::vector<std::string> shortcut_ids;
225 242
226 for (GuidMap::const_iterator it(guid_map_.begin()); it != guid_map_.end(); 243 for (GuidMap::const_iterator it(guid_map_.begin()); it != guid_map_.end();
227 ++it) { 244 ++it) {
228 if (std::find_if( 245 if (std::find_if(
229 rows.begin(), rows.end(), URLRow::URLRowHasURL( 246 rows.begin(), rows.end(), URLRow::URLRowHasURL(
230 it->second->second.match_core.destination_url)) != rows.end()) 247 it->second->second.match_core.destination_url)) != rows.end())
231 shortcut_ids.push_back(it->first); 248 shortcut_ids.push_back(it->first);
232 }
233 DeleteShortcutsWithIds(shortcut_ids);
234 return;
235 } 249 }
236 250 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 } 251 }
257 252
258 void ShortcutsBackend::InitInternal() { 253 void ShortcutsBackend::InitInternal() {
259 DCHECK(current_state_ == INITIALIZING); 254 DCHECK(current_state_ == INITIALIZING);
260 db_->Init(); 255 db_->Init();
261 ShortcutsDatabase::GuidToShortcutMap shortcuts; 256 ShortcutsDatabase::GuidToShortcutMap shortcuts;
262 db_->LoadShortcuts(&shortcuts); 257 db_->LoadShortcuts(&shortcuts);
263 temp_shortcuts_map_.reset(new ShortcutMap); 258 temp_shortcuts_map_.reset(new ShortcutMap);
264 temp_guid_map_.reset(new GuidMap); 259 temp_guid_map_.reset(new GuidMap);
265 for (ShortcutsDatabase::GuidToShortcutMap::const_iterator it( 260 for (ShortcutsDatabase::GuidToShortcutMap::const_iterator it(
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 shortcuts_map_.clear(); 354 shortcuts_map_.clear();
360 guid_map_.clear(); 355 guid_map_.clear();
361 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_, 356 FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
362 OnShortcutsChanged()); 357 OnShortcutsChanged());
363 return no_db_access_ || BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, 358 return no_db_access_ || BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
364 base::Bind(base::IgnoreResult(&ShortcutsDatabase::DeleteAllShortcuts), 359 base::Bind(base::IgnoreResult(&ShortcutsDatabase::DeleteAllShortcuts),
365 db_.get())); 360 db_.get()));
366 } 361 }
367 362
368 } // namespace history 363 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698