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

Side by Side Diff: chrome/browser/history/shortcuts_database.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 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_database.h" 5 #include "chrome/browser/history/shortcuts_database.h"
6 6
7 #include <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/string_number_conversions.h"
13 #include "base/stringprintf.h" 12 #include "base/stringprintf.h"
14 #include "base/time.h" 13 #include "base/time.h"
15 #include "base/utf_string_conversions.h"
16 #include "chrome/browser/autocomplete/shortcuts_provider.h"
17 #include "chrome/common/guid.h" 14 #include "chrome/common/guid.h"
18 #include "sql/statement.h" 15 #include "sql/statement.h"
19 16
20 using base::Time;
21
22 namespace { 17 namespace {
23 18
24 // Using define instead of const char, so I could use ## in the statements. 19 // Using define instead of const char, so I could use ## in the statements.
25 #define kShortcutsDBName "omni_box_shortcuts" 20 #define kShortcutsDBName "omni_box_shortcuts"
26 21
27 // The maximum length allowed for form data. 22 void BindShortcutToStatement(
28 const size_t kMaxDataLength = 2048; // 2K is a hard limit on URLs URI. 23 const history::ShortcutsBackend::Shortcut& shortcut,
29 24 sql::Statement* s) {
30 string16 LimitDataSize(const string16& data) {
31 if (data.size() > kMaxDataLength)
32 return data.substr(0, kMaxDataLength);
33
34 return data;
35 }
36
37 void BindShortcutToStatement(const shortcuts_provider::Shortcut& shortcut,
38 sql::Statement* s) {
39 DCHECK(guid::IsValidGUID(shortcut.id)); 25 DCHECK(guid::IsValidGUID(shortcut.id));
40 s->BindString(0, shortcut.id); 26 s->BindString(0, shortcut.id);
41 s->BindString16(1, LimitDataSize(shortcut.text)); 27 s->BindString16(1, shortcut.text);
42 s->BindString16(2, LimitDataSize(UTF8ToUTF16(shortcut.url.spec()))); 28 s->BindString(2, shortcut.url.spec());
43 s->BindString16(3, LimitDataSize(shortcut.contents)); 29 s->BindString16(3, shortcut.contents);
44 s->BindString16(4, LimitDataSize(shortcut.contents_class_as_str())); 30 s->BindString(4,
45 s->BindString16(5, LimitDataSize(shortcut.description)); 31 AutocompleteMatch::ClassificationsToString(shortcut.contents_class));
46 s->BindString16(6, LimitDataSize(shortcut.description_class_as_str())); 32 s->BindString16(5, shortcut.description);
33 s->BindString(6,
34 AutocompleteMatch::ClassificationsToString(shortcut.description_class));
47 s->BindInt64(7, shortcut.last_access_time.ToInternalValue()); 35 s->BindInt64(7, shortcut.last_access_time.ToInternalValue());
48 s->BindInt(8, shortcut.number_of_hits); 36 s->BindInt(8, shortcut.number_of_hits);
49 } 37 }
50 38
51 shortcuts_provider::Shortcut ShortcutFromStatement(const sql::Statement& s) { 39 bool DeleteShortcut(const char* field_name,
52 return shortcuts_provider::Shortcut(s.ColumnString(0), 40 const std::string& id,
53 s.ColumnString16(1),
54 s.ColumnString16(2),
55 s.ColumnString16(3),
56 s.ColumnString16(4),
57 s.ColumnString16(5),
58 s.ColumnString16(6),
59 s.ColumnInt64(7),
60 s.ColumnInt(8));
61 }
62
63 bool DeleteShortcut(const char* field_name, const std::string& id,
64 sql::Connection& db) { 41 sql::Connection& db) {
65 sql::Statement s(db.GetUniqueStatement( 42 sql::Statement s(db.GetUniqueStatement(
66 base::StringPrintf("DELETE FROM %s WHERE %s = ?", kShortcutsDBName, 43 base::StringPrintf("DELETE FROM %s WHERE %s = ?", kShortcutsDBName,
67 field_name).c_str())); 44 field_name).c_str()));
68 s.BindString(0, id); 45 s.BindString(0, id);
69 46
70 return s.Run(); 47 return s.Run();
71 } 48 }
72 49
73 } // namespace 50 } // namespace
(...skipping 23 matching lines...) Expand all
97 // Attach the database to our index file. 74 // Attach the database to our index file.
98 if (!db_.Open(database_path_)) 75 if (!db_.Open(database_path_))
99 return false; 76 return false;
100 77
101 if (!EnsureTable()) 78 if (!EnsureTable())
102 return false; 79 return false;
103 return true; 80 return true;
104 } 81 }
105 82
106 bool ShortcutsDatabase::AddShortcut( 83 bool ShortcutsDatabase::AddShortcut(
107 const shortcuts_provider::Shortcut& shortcut) { 84 const ShortcutsBackend::Shortcut& shortcut) {
108 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, 85 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE,
109 "INSERT INTO " kShortcutsDBName 86 "INSERT INTO " kShortcutsDBName
110 " (id, text, url, contents, contents_class, description," 87 " (id, text, url, contents, contents_class, description,"
111 " description_class, last_access_time, number_of_hits) " 88 " description_class, last_access_time, number_of_hits) "
112 "VALUES (?,?,?,?,?,?,?,?,?)")); 89 "VALUES (?,?,?,?,?,?,?,?,?)"));
113 BindShortcutToStatement(shortcut, &s); 90 BindShortcutToStatement(shortcut, &s);
114 91
115 return s.Run(); 92 return s.Run();
116 } 93 }
117 94
118 bool ShortcutsDatabase::UpdateShortcut( 95 bool ShortcutsDatabase::UpdateShortcut(
119 const shortcuts_provider::Shortcut& shortcut) { 96 const ShortcutsBackend::Shortcut& shortcut) {
120 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, 97 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE,
121 "UPDATE " kShortcutsDBName " " 98 "UPDATE " kShortcutsDBName " "
122 "SET id=?, text=?, url=?, contents=?, contents_class=?," 99 "SET id=?, text=?, url=?, contents=?, contents_class=?,"
123 " description=?, description_class=?, last_access_time=?," 100 " description=?, description_class=?, last_access_time=?,"
124 " number_of_hits=? " 101 " number_of_hits=? "
125 "WHERE id=?")); 102 "WHERE id=?"));
126 BindShortcutToStatement(shortcut, &s); 103 BindShortcutToStatement(shortcut, &s);
127 s.BindString(9, shortcut.id); 104 s.BindString(9, shortcut.id);
128 105
129 bool result = s.Run(); 106 bool result = s.Run();
(...skipping 21 matching lines...) Expand all
151 128
152 bool ShortcutsDatabase::DeleteAllShortcuts() { 129 bool ShortcutsDatabase::DeleteAllShortcuts() {
153 if (!db_.Execute("DELETE FROM " kShortcutsDBName)) 130 if (!db_.Execute("DELETE FROM " kShortcutsDBName))
154 return false; 131 return false;
155 132
156 ignore_result(db_.Execute("VACUUM")); 133 ignore_result(db_.Execute("VACUUM"));
157 return true; 134 return true;
158 } 135 }
159 136
160 // Loads all of the shortcuts. 137 // Loads all of the shortcuts.
161 bool ShortcutsDatabase::LoadShortcuts( 138 bool ShortcutsDatabase::LoadShortcuts(GuidToShortcutMap* shortcuts) {
162 std::map<std::string, shortcuts_provider::Shortcut>* shortcuts) {
163 DCHECK(shortcuts); 139 DCHECK(shortcuts);
164 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, 140 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE,
165 "SELECT id, text, url, contents, contents_class, " 141 "SELECT id, text, url, contents, contents_class, "
166 "description, description_class, last_access_time, number_of_hits " 142 "description, description_class, last_access_time, number_of_hits "
167 "FROM " kShortcutsDBName)); 143 "FROM " kShortcutsDBName));
168 144
169 if (!s.is_valid()) 145 if (!s.is_valid())
170 return false; 146 return false;
171 147
172 shortcuts->clear(); 148 shortcuts->clear();
173 while (s.Step()) { 149 while (s.Step()) {
174 shortcuts->insert(std::make_pair(s.ColumnString(0), 150 shortcuts->insert(std::make_pair(s.ColumnString(0),
175 ShortcutFromStatement(s))); 151 ShortcutsBackend::Shortcut(s.ColumnString(0), s.ColumnString16(1),
152 GURL(s.ColumnString(2)), s.ColumnString16(3),
153 AutocompleteMatch::ClassificationsFromString(s.ColumnString(4)),
154 s.ColumnString16(5),
155 AutocompleteMatch::ClassificationsFromString(s.ColumnString(6)),
156 base::Time::FromInternalValue(s.ColumnInt64(7)), s.ColumnInt(8))));
176 } 157 }
177 return true; 158 return true;
178 } 159 }
179 160
180 bool ShortcutsDatabase::EnsureTable() { 161 bool ShortcutsDatabase::EnsureTable() {
181 if (!db_.DoesTableExist(kShortcutsDBName)) { 162 if (!db_.DoesTableExist(kShortcutsDBName)) {
182 if (!db_.Execute(base::StringPrintf( 163 if (!db_.Execute(base::StringPrintf(
183 "CREATE TABLE %s ( " 164 "CREATE TABLE %s ( "
184 "id VARCHAR PRIMARY KEY, " 165 "id VARCHAR PRIMARY KEY, "
185 "text VARCHAR, " 166 "text VARCHAR, "
186 "url VARCHAR, " 167 "url VARCHAR, "
187 "contents VARCHAR, " 168 "contents VARCHAR, "
188 "contents_class VARCHAR, " 169 "contents_class VARCHAR, "
189 "description VARCHAR, " 170 "description VARCHAR, "
190 "description_class VARCHAR, " 171 "description_class VARCHAR, "
191 "last_access_time INTEGER, " 172 "last_access_time INTEGER, "
192 "number_of_hits INTEGER)", kShortcutsDBName).c_str())) { 173 "number_of_hits INTEGER)", kShortcutsDBName).c_str())) {
193 NOTREACHED(); 174 NOTREACHED();
194 return false; 175 return false;
195 } 176 }
196 } 177 }
197 return true; 178 return true;
198 } 179 }
199 180
200 } // namespace history 181 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698