| 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/shortcuts_database.h" | 5 #include "chrome/browser/history/shortcuts_database.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | |
| 9 | 8 |
| 10 #include "base/guid.h" | 9 #include "base/guid.h" |
| 11 #include "base/logging.h" | 10 #include "base/logging.h" |
| 12 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 13 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "chrome/common/autocomplete_match_type.h" |
| 14 #include "content/public/common/page_transition_types.h" |
| 14 #include "sql/statement.h" | 15 #include "sql/statement.h" |
| 15 #include "sql/transaction.h" | 16 #include "sql/transaction.h" |
| 16 | 17 |
| 18 |
| 19 // Helpers -------------------------------------------------------------------- |
| 20 |
| 17 namespace { | 21 namespace { |
| 18 | 22 |
| 19 void BindShortcutToStatement( | 23 void BindShortcutToStatement( |
| 20 const history::ShortcutsBackend::Shortcut& shortcut, | 24 const history::ShortcutsDatabase::Shortcut& shortcut, |
| 21 sql::Statement* s) { | 25 sql::Statement* s) { |
| 22 DCHECK(base::IsValidGUID(shortcut.id)); | 26 DCHECK(base::IsValidGUID(shortcut.id)); |
| 23 s->BindString(0, shortcut.id); | 27 s->BindString(0, shortcut.id); |
| 24 s->BindString16(1, shortcut.text); | 28 s->BindString16(1, shortcut.text); |
| 25 s->BindString16(2, shortcut.match_core.fill_into_edit); | 29 s->BindString16(2, shortcut.match_core.fill_into_edit); |
| 26 s->BindString(3, shortcut.match_core.destination_url.spec()); | 30 s->BindString(3, shortcut.match_core.destination_url.spec()); |
| 27 s->BindString16(4, shortcut.match_core.contents); | 31 s->BindString16(4, shortcut.match_core.contents); |
| 28 s->BindString(5, AutocompleteMatch::ClassificationsToString( | 32 s->BindString(5, shortcut.match_core.contents_class); |
| 29 shortcut.match_core.contents_class)); | |
| 30 s->BindString16(6, shortcut.match_core.description); | 33 s->BindString16(6, shortcut.match_core.description); |
| 31 s->BindString(7, AutocompleteMatch::ClassificationsToString( | 34 s->BindString(7, shortcut.match_core.description_class); |
| 32 shortcut.match_core.description_class)); | |
| 33 s->BindInt(8, shortcut.match_core.transition); | 35 s->BindInt(8, shortcut.match_core.transition); |
| 34 s->BindInt(9, shortcut.match_core.type); | 36 s->BindInt(9, shortcut.match_core.type); |
| 35 s->BindString16(10, shortcut.match_core.keyword); | 37 s->BindString16(10, shortcut.match_core.keyword); |
| 36 s->BindInt64(11, shortcut.last_access_time.ToInternalValue()); | 38 s->BindInt64(11, shortcut.last_access_time.ToInternalValue()); |
| 37 s->BindInt(12, shortcut.number_of_hits); | 39 s->BindInt(12, shortcut.number_of_hits); |
| 38 } | 40 } |
| 39 | 41 |
| 40 bool DeleteShortcut(const char* field_name, | 42 bool DeleteShortcut(const char* field_name, |
| 41 const std::string& id, | 43 const std::string& id, |
| 42 sql::Connection& db) { | 44 sql::Connection& db) { |
| 43 sql::Statement s(db.GetUniqueStatement( | 45 sql::Statement s(db.GetUniqueStatement( |
| 44 base::StringPrintf("DELETE FROM omni_box_shortcuts WHERE %s = ?", | 46 base::StringPrintf("DELETE FROM omni_box_shortcuts WHERE %s = ?", |
| 45 field_name).c_str())); | 47 field_name).c_str())); |
| 46 s.BindString(0, id); | 48 s.BindString(0, id); |
| 47 return s.Run(); | 49 return s.Run(); |
| 48 } | 50 } |
| 49 | 51 |
| 50 } // namespace | 52 } // namespace |
| 51 | 53 |
| 54 |
| 52 namespace history { | 55 namespace history { |
| 53 | 56 |
| 57 // ShortcutsDatabase::Shortcut::MatchCore ------------------------------------- |
| 58 |
| 59 ShortcutsDatabase::Shortcut::MatchCore::MatchCore( |
| 60 const base::string16& fill_into_edit, |
| 61 const GURL& destination_url, |
| 62 const base::string16& contents, |
| 63 const std::string& contents_class, |
| 64 const base::string16& description, |
| 65 const std::string& description_class, |
| 66 int transition, |
| 67 int type, |
| 68 const base::string16& keyword) |
| 69 : fill_into_edit(fill_into_edit), |
| 70 destination_url(destination_url), |
| 71 contents(contents), |
| 72 contents_class(contents_class), |
| 73 description(description), |
| 74 description_class(description_class), |
| 75 transition(transition), |
| 76 type(type), |
| 77 keyword(keyword) { |
| 78 } |
| 79 |
| 80 ShortcutsDatabase::Shortcut::MatchCore::~MatchCore() { |
| 81 } |
| 82 |
| 83 // ShortcutsDatabase::Shortcut ------------------------------------------------ |
| 84 |
| 85 ShortcutsDatabase::Shortcut::Shortcut( |
| 86 const std::string& id, |
| 87 const base::string16& text, |
| 88 const MatchCore& match_core, |
| 89 const base::Time& last_access_time, |
| 90 int number_of_hits) |
| 91 : id(id), |
| 92 text(text), |
| 93 match_core(match_core), |
| 94 last_access_time(last_access_time), |
| 95 number_of_hits(number_of_hits) { |
| 96 } |
| 97 |
| 98 ShortcutsDatabase::Shortcut::Shortcut() |
| 99 : match_core(base::string16(), GURL(), base::string16(), std::string(), |
| 100 base::string16(), std::string(), 0, 0, base::string16()), |
| 101 last_access_time(base::Time::Now()), |
| 102 number_of_hits(0) { |
| 103 } |
| 104 |
| 105 ShortcutsDatabase::Shortcut::~Shortcut() { |
| 106 } |
| 107 |
| 108 |
| 109 // ShortcutsDatabase ---------------------------------------------------------- |
| 110 |
| 54 ShortcutsDatabase::ShortcutsDatabase(const base::FilePath& database_path) | 111 ShortcutsDatabase::ShortcutsDatabase(const base::FilePath& database_path) |
| 55 : database_path_(database_path) { | 112 : database_path_(database_path) { |
| 56 } | 113 } |
| 57 | 114 |
| 58 bool ShortcutsDatabase::Init() { | 115 bool ShortcutsDatabase::Init() { |
| 59 db_.set_histogram_tag("Shortcuts"); | 116 db_.set_histogram_tag("Shortcuts"); |
| 60 | 117 |
| 61 // Set the database page size to something a little larger to give us | 118 // Set the database page size to something a little larger to give us |
| 62 // better performance (we're typically seek rather than bandwidth limited). | 119 // better performance (we're typically seek rather than bandwidth limited). |
| 63 // This only has an effect before any tables have been created, otherwise | 120 // This only has an effect before any tables have been created, otherwise |
| 64 // this is a NOP. Must be a power of 2 and a max of 8192. | 121 // this is a NOP. Must be a power of 2 and a max of 8192. |
| 65 db_.set_page_size(4096); | 122 db_.set_page_size(4096); |
| 66 | 123 |
| 67 // Run the database in exclusive mode. Nobody else should be accessing the | 124 // Run the database in exclusive mode. Nobody else should be accessing the |
| 68 // database while we're running, and this will give somewhat improved perf. | 125 // database while we're running, and this will give somewhat improved perf. |
| 69 db_.set_exclusive_locking(); | 126 db_.set_exclusive_locking(); |
| 70 | 127 |
| 71 // Attach the database to our index file. | 128 // Attach the database to our index file. |
| 72 return db_.Open(database_path_) && EnsureTable(); | 129 return db_.Open(database_path_) && EnsureTable(); |
| 73 } | 130 } |
| 74 | 131 |
| 75 bool ShortcutsDatabase::AddShortcut( | 132 bool ShortcutsDatabase::AddShortcut(const Shortcut& shortcut) { |
| 76 const ShortcutsBackend::Shortcut& shortcut) { | |
| 77 sql::Statement s(db_.GetCachedStatement( | 133 sql::Statement s(db_.GetCachedStatement( |
| 78 SQL_FROM_HERE, | 134 SQL_FROM_HERE, |
| 79 "INSERT INTO omni_box_shortcuts (id, text, fill_into_edit, url, " | 135 "INSERT INTO omni_box_shortcuts (id, text, fill_into_edit, url, " |
| 80 "contents, contents_class, description, description_class, " | 136 "contents, contents_class, description, description_class, " |
| 81 "transition, type, keyword, last_access_time, number_of_hits) " | 137 "transition, type, keyword, last_access_time, number_of_hits) " |
| 82 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)")); | 138 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)")); |
| 83 BindShortcutToStatement(shortcut, &s); | 139 BindShortcutToStatement(shortcut, &s); |
| 84 return s.Run(); | 140 return s.Run(); |
| 85 } | 141 } |
| 86 | 142 |
| 87 bool ShortcutsDatabase::UpdateShortcut( | 143 bool ShortcutsDatabase::UpdateShortcut(const Shortcut& shortcut) { |
| 88 const ShortcutsBackend::Shortcut& shortcut) { | |
| 89 sql::Statement s(db_.GetCachedStatement( | 144 sql::Statement s(db_.GetCachedStatement( |
| 90 SQL_FROM_HERE, | 145 SQL_FROM_HERE, |
| 91 "UPDATE omni_box_shortcuts SET id=?, text=?, fill_into_edit=?, url=?, " | 146 "UPDATE omni_box_shortcuts SET id=?, text=?, fill_into_edit=?, url=?, " |
| 92 "contents=?, contents_class=?, description=?, description_class=?, " | 147 "contents=?, contents_class=?, description=?, description_class=?, " |
| 93 "transition=?, type=?, keyword=?, last_access_time=?, " | 148 "transition=?, type=?, keyword=?, last_access_time=?, " |
| 94 "number_of_hits=? WHERE id=?")); | 149 "number_of_hits=? WHERE id=?")); |
| 95 BindShortcutToStatement(shortcut, &s); | 150 BindShortcutToStatement(shortcut, &s); |
| 96 s.BindString(13, shortcut.id); | 151 s.BindString(13, shortcut.id); |
| 97 return s.Run(); | 152 return s.Run(); |
| 98 } | 153 } |
| 99 | 154 |
| 100 bool ShortcutsDatabase::DeleteShortcutsWithIds( | 155 bool ShortcutsDatabase::DeleteShortcutsWithIDs( |
| 101 const std::vector<std::string>& shortcut_ids) { | 156 const ShortcutIDs& shortcut_ids) { |
| 102 bool success = true; | 157 bool success = true; |
| 103 db_.BeginTransaction(); | 158 db_.BeginTransaction(); |
| 104 for (std::vector<std::string>::const_iterator it(shortcut_ids.begin()); | 159 for (ShortcutIDs::const_iterator it(shortcut_ids.begin()); |
| 105 it != shortcut_ids.end(); ++it) { | 160 it != shortcut_ids.end(); ++it) { |
| 106 success &= DeleteShortcut("id", *it, db_); | 161 success &= DeleteShortcut("id", *it, db_); |
| 107 } | 162 } |
| 108 db_.CommitTransaction(); | 163 db_.CommitTransaction(); |
| 109 return success; | 164 return success; |
| 110 } | 165 } |
| 111 | 166 |
| 112 bool ShortcutsDatabase::DeleteShortcutsWithUrl( | 167 bool ShortcutsDatabase::DeleteShortcutsWithURL( |
| 113 const std::string& shortcut_url_spec) { | 168 const std::string& shortcut_url_spec) { |
| 114 return DeleteShortcut("url", shortcut_url_spec, db_); | 169 return DeleteShortcut("url", shortcut_url_spec, db_); |
| 115 } | 170 } |
| 116 | 171 |
| 117 bool ShortcutsDatabase::DeleteAllShortcuts() { | 172 bool ShortcutsDatabase::DeleteAllShortcuts() { |
| 118 if (!db_.Execute("DELETE FROM omni_box_shortcuts")) | 173 if (!db_.Execute("DELETE FROM omni_box_shortcuts")) |
| 119 return false; | 174 return false; |
| 120 | 175 |
| 121 ignore_result(db_.Execute("VACUUM")); | 176 ignore_result(db_.Execute("VACUUM")); |
| 122 return true; | 177 return true; |
| 123 } | 178 } |
| 124 | 179 |
| 125 void ShortcutsDatabase::LoadShortcuts(GuidToShortcutMap* shortcuts) { | 180 void ShortcutsDatabase::LoadShortcuts(GuidToShortcutMap* shortcuts) { |
| 126 DCHECK(shortcuts); | 181 DCHECK(shortcuts); |
| 127 sql::Statement s(db_.GetCachedStatement( | 182 sql::Statement s(db_.GetCachedStatement( |
| 128 SQL_FROM_HERE, | 183 SQL_FROM_HERE, |
| 129 "SELECT id, text, fill_into_edit, url, contents, contents_class, " | 184 "SELECT id, text, fill_into_edit, url, contents, contents_class, " |
| 130 "description, description_class, transition, type, keyword, " | 185 "description, description_class, transition, type, keyword, " |
| 131 "last_access_time, number_of_hits FROM omni_box_shortcuts")); | 186 "last_access_time, number_of_hits FROM omni_box_shortcuts")); |
| 132 | 187 |
| 133 shortcuts->clear(); | 188 shortcuts->clear(); |
| 134 while (s.Step()) { | 189 while (s.Step()) { |
| 135 shortcuts->insert(std::make_pair( | 190 shortcuts->insert(std::make_pair( |
| 136 s.ColumnString(0), | 191 s.ColumnString(0), |
| 137 ShortcutsBackend::Shortcut( | 192 Shortcut( |
| 138 s.ColumnString(0), // id | 193 s.ColumnString(0), // id |
| 139 s.ColumnString16(1), // text | 194 s.ColumnString16(1), // text |
| 140 ShortcutsBackend::Shortcut::MatchCore( | 195 Shortcut::MatchCore( |
| 141 s.ColumnString16(2), // fill_into_edit | 196 s.ColumnString16(2), // fill_into_edit |
| 142 GURL(s.ColumnString(3)), // destination_url | 197 GURL(s.ColumnString(3)), // destination_url |
| 143 s.ColumnString16(4), // contents | 198 s.ColumnString16(4), // contents |
| 144 AutocompleteMatch::ClassificationsFromString(s.ColumnString(5)), | 199 s.ColumnString(5), // contents_class |
| 145 // contents_class | |
| 146 s.ColumnString16(6), // description | 200 s.ColumnString16(6), // description |
| 147 AutocompleteMatch::ClassificationsFromString(s.ColumnString(7)), | 201 s.ColumnString(7), // description_class |
| 148 // description_class | 202 s.ColumnInt(8), // transition |
| 149 static_cast<content::PageTransition>(s.ColumnInt(8)), | 203 s.ColumnInt(9), // type |
| 150 // transition | |
| 151 static_cast<AutocompleteMatch::Type>(s.ColumnInt(9)), | |
| 152 // type | |
| 153 s.ColumnString16(10)), // keyword | 204 s.ColumnString16(10)), // keyword |
| 154 base::Time::FromInternalValue(s.ColumnInt64(11)), | 205 base::Time::FromInternalValue(s.ColumnInt64(11)), |
| 155 // last_access_time | 206 // last_access_time |
| 156 s.ColumnInt(12)))); // number_of_hits | 207 s.ColumnInt(12)))); // number_of_hits |
| 157 } | 208 } |
| 158 } | 209 } |
| 159 | 210 |
| 160 ShortcutsDatabase::~ShortcutsDatabase() { | 211 ShortcutsDatabase::~ShortcutsDatabase() { |
| 161 } | 212 } |
| 162 | 213 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 193 static_cast<int>(AutocompleteMatchType::HISTORY_TITLE)).c_str()) && | 244 static_cast<int>(AutocompleteMatchType::HISTORY_TITLE)).c_str()) && |
| 194 db_.Execute("ALTER TABLE omni_box_shortcuts " | 245 db_.Execute("ALTER TABLE omni_box_shortcuts " |
| 195 "ADD COLUMN keyword VARCHAR") && | 246 "ADD COLUMN keyword VARCHAR") && |
| 196 transaction.Commit(); | 247 transaction.Commit(); |
| 197 } | 248 } |
| 198 | 249 |
| 199 return true; | 250 return true; |
| 200 } | 251 } |
| 201 | 252 |
| 202 } // namespace history | 253 } // namespace history |
| OLD | NEW |