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

Unified Diff: chrome/browser/history/shortcuts_backend.cc

Issue 9689085: Clean up ShortcutsProvider and related classes. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/history/shortcuts_backend.cc
===================================================================
--- chrome/browser/history/shortcuts_backend.cc (revision 125990)
+++ chrome/browser/history/shortcuts_backend.cc (working copy)
@@ -26,8 +26,61 @@
using content::BrowserThread;
+namespace {
+
+// Takes Match classification vector and removes all matched positions,
+// compacting repetitions if necessary.
+void StripMatchMarkersFromClassifications(ACMatchClassifications* matches) {
+ DCHECK(matches);
+ ACMatchClassifications unmatched;
+ for (ACMatchClassifications::iterator i = matches->begin();
+ i != matches->end(); ++i) {
+ AutocompleteMatch::AddLastClassificationIfNecessary(&unmatched, i->offset,
+ i->style & ~ACMatchClassification::MATCH);
+ }
+ matches->swap(unmatched);
+}
+
+} // namespace
+
namespace history {
+// ShortcutsBackend::Shortcut -------------------------------------------------
+
+ShortcutsBackend::Shortcut::Shortcut(
+ const std::string& id,
+ const string16& text,
+ const GURL& url,
+ const string16& contents,
+ const ACMatchClassifications& contents_class,
+ const string16& description,
+ const ACMatchClassifications& description_class,
+ const base::Time& last_access_time,
+ int number_of_hits)
+ : id(id),
+ text(text),
+ url(url),
+ contents(contents),
+ contents_class(contents_class),
+ description(description),
+ description_class(description_class),
+ last_access_time(last_access_time),
+ number_of_hits(number_of_hits) {
+ StripMatchMarkersFromClassifications(&this->contents_class);
+ StripMatchMarkersFromClassifications(&this->description_class);
+}
+
+ShortcutsBackend::Shortcut::Shortcut()
+ : last_access_time(base::Time::Now()),
+ number_of_hits(0) {
+}
+
+ShortcutsBackend::Shortcut::~Shortcut() {
+}
+
+
+// ShortcutsBackend -----------------------------------------------------------
+
ShortcutsBackend::ShortcutsBackend(const FilePath& db_folder_path,
Profile *profile)
: current_state_(NOT_INITIALIZED),
@@ -45,23 +98,20 @@
ShortcutsBackend::~ShortcutsBackend() {}
bool ShortcutsBackend::Init() {
- if (current_state_ == NOT_INITIALIZED) {
- current_state_ = INITIALIZING;
- if (no_db_access_) {
- current_state_ = INITIALIZED;
- return true;
- } else {
- return BrowserThread::PostTask(
- BrowserThread::DB, FROM_HERE,
- base::Bind(&ShortcutsBackend::InitInternal, this));
- }
- } else {
+ if (current_state_ != NOT_INITIALIZED)
return false;
+
+ if (no_db_access_) {
+ current_state_ = INITIALIZED;
+ return true;
}
+
+ current_state_ = INITIALIZING;
+ return BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
+ base::Bind(&ShortcutsBackend::InitInternal, this));
}
-bool ShortcutsBackend::AddShortcut(
- const shortcuts_provider::Shortcut& shortcut) {
+bool ShortcutsBackend::AddShortcut(const Shortcut& shortcut) {
if (!initialized())
return false;
DCHECK(guid_map_.find(shortcut.id) == guid_map_.end());
@@ -69,32 +119,24 @@
std::make_pair(base::i18n::ToLower(shortcut.text), shortcut));
FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
OnShortcutsChanged());
- if (no_db_access_)
- return true;
- return BrowserThread::PostTask(
- BrowserThread::DB, FROM_HERE,
+ return no_db_access_ || BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
base::Bind(base::IgnoreResult(&ShortcutsDatabase::AddShortcut),
db_.get(), shortcut));
}
-bool ShortcutsBackend::UpdateShortcut(
- const shortcuts_provider::Shortcut& shortcut) {
+bool ShortcutsBackend::UpdateShortcut(const Shortcut& shortcut) {
if (!initialized())
return false;
- shortcuts_provider::GuidToShortcutsIteratorMap::iterator it =
- guid_map_.find(shortcut.id);
+ GuidToShortcutsIteratorMap::iterator it = guid_map_.find(shortcut.id);
if (it != guid_map_.end())
shortcuts_map_.erase(it->second);
guid_map_[shortcut.id] = shortcuts_map_.insert(
std::make_pair(base::i18n::ToLower(shortcut.text), shortcut));
FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
OnShortcutsChanged());
- if (no_db_access_)
- return true;
- return BrowserThread::PostTask(
- BrowserThread::DB, FROM_HERE,
- base::Bind(base::IgnoreResult(&ShortcutsDatabase::UpdateShortcut),
- db_.get(), shortcut));
+ return no_db_access_ || BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
+ base::Bind(base::IgnoreResult(&ShortcutsDatabase::UpdateShortcut),
+ db_.get(), shortcut));
}
bool ShortcutsBackend::DeleteShortcutsWithIds(
@@ -102,8 +144,7 @@
if (!initialized())
return false;
for (size_t i = 0; i < shortcut_ids.size(); ++i) {
- shortcuts_provider::GuidToShortcutsIteratorMap::iterator it =
- guid_map_.find(shortcut_ids[i]);
+ GuidToShortcutsIteratorMap::iterator it = guid_map_.find(shortcut_ids[i]);
if (it != guid_map_.end()) {
shortcuts_map_.erase(it->second);
guid_map_.erase(it);
@@ -111,21 +152,16 @@
}
FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
OnShortcutsChanged());
- if (no_db_access_)
- return true;
- return BrowserThread::PostTask(
- BrowserThread::DB, FROM_HERE,
- base::Bind(
- base::IgnoreResult(&ShortcutsDatabase::DeleteShortcutsWithIds),
- db_.get(), shortcut_ids));
+ return no_db_access_ || BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
+ base::Bind(base::IgnoreResult(&ShortcutsDatabase::DeleteShortcutsWithIds),
+ db_.get(), shortcut_ids));
}
bool ShortcutsBackend::DeleteShortcutsWithUrl(const GURL& shortcut_url) {
if (!initialized())
return false;
std::vector<std::string> shortcut_ids;
- for (shortcuts_provider::GuidToShortcutsIteratorMap::iterator
- it = guid_map_.begin();
+ for (GuidToShortcutsIteratorMap::iterator it = guid_map_.begin();
it != guid_map_.end();) {
if (it->second->second.url == shortcut_url) {
shortcut_ids.push_back(it->first);
@@ -137,13 +173,9 @@
}
FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
OnShortcutsChanged());
- if (no_db_access_)
- return true;
- return BrowserThread::PostTask(
- BrowserThread::DB, FROM_HERE,
- base::Bind(
- base::IgnoreResult(&ShortcutsDatabase::DeleteShortcutsWithUrl),
- db_.get(), shortcut_url.spec()));
+ return no_db_access_ || BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
+ base::Bind(base::IgnoreResult(&ShortcutsDatabase::DeleteShortcutsWithUrl),
+ db_.get(), shortcut_url.spec()));
}
bool ShortcutsBackend::DeleteAllShortcuts() {
@@ -153,29 +185,24 @@
guid_map_.clear();
FOR_EACH_OBSERVER(ShortcutsBackendObserver, observer_list_,
OnShortcutsChanged());
- if (no_db_access_)
- return true;
- return BrowserThread::PostTask(
- BrowserThread::DB, FROM_HERE,
- base::Bind(
- base::IgnoreResult(&ShortcutsDatabase::DeleteAllShortcuts),
- db_.get()));
+ return no_db_access_ || BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
+ base::Bind(base::IgnoreResult(&ShortcutsDatabase::DeleteAllShortcuts),
+ db_.get()));
}
void ShortcutsBackend::InitInternal() {
DCHECK(current_state_ == INITIALIZING);
db_->Init();
- shortcuts_provider::GuidToShortcutMap shortcuts;
+ ShortcutsDatabase::GuidToShortcutMap shortcuts;
db_->LoadShortcuts(&shortcuts);
- temp_shortcuts_map_.reset(new shortcuts_provider::ShortcutMap);
- temp_guid_map_.reset(new shortcuts_provider::GuidToShortcutsIteratorMap);
- for (shortcuts_provider::GuidToShortcutMap::iterator it = shortcuts.begin();
+ temp_shortcuts_map_.reset(new ShortcutMap);
+ temp_guid_map_.reset(new GuidToShortcutsIteratorMap);
+ for (ShortcutsDatabase::GuidToShortcutMap::iterator it = shortcuts.begin();
it != shortcuts.end(); ++it) {
(*temp_guid_map_)[it->first] = temp_shortcuts_map_->insert(
std::make_pair(base::i18n::ToLower(it->second.text), it->second));
}
- BrowserThread::PostTask(
- BrowserThread::UI, FROM_HERE,
+ BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&ShortcutsBackend::InitCompleted, this));
}
@@ -204,8 +231,7 @@
content::Details<const history::URLsDeletedDetails>(details)->urls;
std::vector<std::string> shortcut_ids;
- for (shortcuts_provider::GuidToShortcutsIteratorMap::iterator
- it = guid_map_.begin();
+ for (GuidToShortcutsIteratorMap::iterator it = guid_map_.begin();
it != guid_map_.end(); ++it) {
if (urls.find(it->second->second.url) != urls.end())
shortcut_ids.push_back(it->first);
@@ -219,32 +245,21 @@
AutocompleteLog* log = content::Details<AutocompleteLog>(details).ptr();
string16 text_lowercase(base::i18n::ToLower(log->text));
- int number_of_hits = 1;
- std::string id;
const AutocompleteMatch& match(log->result.match_at(log->selected_index));
- for (shortcuts_provider::ShortcutMap::iterator it =
- shortcuts_map_.lower_bound(text_lowercase);
+ for (ShortcutMap::iterator it = shortcuts_map_.lower_bound(text_lowercase);
it != shortcuts_map_.end() &&
StartsWith(it->first, text_lowercase, true); ++it) {
if (match.destination_url == it->second.url) {
- number_of_hits = it->second.number_of_hits + 1;
- id = it->second.id;
- break;
+ UpdateShortcut(Shortcut(it->second.id, log->text, match.destination_url,
+ match.contents, match.contents_class, match.description,
+ match.description_class, base::Time::Now(),
+ it->second.number_of_hits + 1));
+ return;
}
}
- shortcuts_provider::Shortcut shortcut(log->text, match.destination_url,
+ AddShortcut(Shortcut(guid::GenerateGUID(), log->text, match.destination_url,
match.contents, match.contents_class, match.description,
- match.description_class);
- shortcut.number_of_hits = number_of_hits;
- if (number_of_hits == 1)
- shortcut.id = guid::GenerateGUID();
- else
- shortcut.id = id;
-
- if (number_of_hits == 1)
- AddShortcut(shortcut);
- else
- UpdateShortcut(shortcut);
+ match.description_class, base::Time::Now(), 1));
}
} // namespace history

Powered by Google App Engine
This is Rietveld 408576698