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

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

Issue 10701043: Make ShortcutsBackend a ProfileKeyedService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 5 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/guid.h" 11 #include "base/guid.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/stringprintf.h" 13 #include "base/stringprintf.h"
14 #include "base/time.h" 14 #include "base/time.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/common/chrome_constants.h"
15 #include "sql/statement.h" 17 #include "sql/statement.h"
16 18
17 namespace { 19 namespace {
18 20
19 // Using define instead of const char, so I could use ## in the statements. 21 const char* kShortcutsTableName = "omni_box_shortcuts";
20 #define kShortcutsDBName "omni_box_shortcuts"
21 22
22 void BindShortcutToStatement( 23 void BindShortcutToStatement(
23 const history::ShortcutsBackend::Shortcut& shortcut, 24 const history::ShortcutsBackend::Shortcut& shortcut,
24 sql::Statement* s) { 25 sql::Statement* s) {
25 DCHECK(base::IsValidGUID(shortcut.id)); 26 DCHECK(base::IsValidGUID(shortcut.id));
26 s->BindString(0, shortcut.id); 27 s->BindString(0, shortcut.id);
27 s->BindString16(1, shortcut.text); 28 s->BindString16(1, shortcut.text);
28 s->BindString(2, shortcut.url.spec()); 29 s->BindString(2, shortcut.url.spec());
29 s->BindString16(3, shortcut.contents); 30 s->BindString16(3, shortcut.contents);
30 s->BindString(4, 31 s->BindString(4,
31 AutocompleteMatch::ClassificationsToString(shortcut.contents_class)); 32 AutocompleteMatch::ClassificationsToString(shortcut.contents_class));
32 s->BindString16(5, shortcut.description); 33 s->BindString16(5, shortcut.description);
33 s->BindString(6, 34 s->BindString(6,
34 AutocompleteMatch::ClassificationsToString(shortcut.description_class)); 35 AutocompleteMatch::ClassificationsToString(shortcut.description_class));
35 s->BindInt64(7, shortcut.last_access_time.ToInternalValue()); 36 s->BindInt64(7, shortcut.last_access_time.ToInternalValue());
36 s->BindInt(8, shortcut.number_of_hits); 37 s->BindInt(8, shortcut.number_of_hits);
37 } 38 }
38 39
39 bool DeleteShortcut(const char* field_name, 40 bool DeleteShortcut(const char* field_name,
40 const std::string& id, 41 const std::string& id,
41 sql::Connection& db) { 42 sql::Connection& db) {
42 sql::Statement s(db.GetUniqueStatement( 43 sql::Statement s(db.GetUniqueStatement(
43 base::StringPrintf("DELETE FROM %s WHERE %s = ?", kShortcutsDBName, 44 base::StringPrintf("DELETE FROM %s WHERE %s = ?", kShortcutsTableName,
44 field_name).c_str())); 45 field_name).c_str()));
45 s.BindString(0, id); 46 s.BindString(0, id);
46 47
47 return s.Run(); 48 return s.Run();
48 } 49 }
49 50
50 } // namespace 51 } // namespace
51 52
52 namespace history { 53 namespace history {
53 54
54 const FilePath::CharType ShortcutsDatabase::kShortcutsDatabaseName[] = 55 ShortcutsDatabase::ShortcutsDatabase(Profile* profile) {
55 FILE_PATH_LITERAL("Shortcuts"); 56 database_path_ = profile->GetPath().Append(chrome::kShortcutsDatabaseName);
56
57 ShortcutsDatabase::ShortcutsDatabase(const FilePath& folder_path)
58 : database_path_(folder_path.Append(FilePath(kShortcutsDatabaseName))) {
59 } 57 }
60 58
61 bool ShortcutsDatabase::Init() { 59 bool ShortcutsDatabase::Init() {
62 // Set the database page size to something a little larger to give us 60 // Set the database page size to something a little larger to give us
63 // better performance (we're typically seek rather than bandwidth limited). 61 // better performance (we're typically seek rather than bandwidth limited).
64 // This only has an effect before any tables have been created, otherwise 62 // This only has an effect before any tables have been created, otherwise
65 // this is a NOP. Must be a power of 2 and a max of 8192. 63 // this is a NOP. Must be a power of 2 and a max of 8192.
66 db_.set_page_size(4096); 64 db_.set_page_size(4096);
67 65
68 // Run the database in exclusive mode. Nobody else should be accessing the 66 // Run the database in exclusive mode. Nobody else should be accessing the
69 // database while we're running, and this will give somewhat improved perf. 67 // database while we're running, and this will give somewhat improved perf.
70 db_.set_exclusive_locking(); 68 db_.set_exclusive_locking();
71 69
72 // Attach the database to our index file. 70 // Attach the database to our index file.
73 if (!db_.Open(database_path_)) 71 if (!db_.Open(database_path_))
74 return false; 72 return false;
75 73
76 if (!EnsureTable()) 74 if (!EnsureTable())
77 return false; 75 return false;
78 return true; 76 return true;
79 } 77 }
80 78
81 bool ShortcutsDatabase::AddShortcut( 79 bool ShortcutsDatabase::AddShortcut(
82 const ShortcutsBackend::Shortcut& shortcut) { 80 const ShortcutsBackend::Shortcut& shortcut) {
83 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, 81 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE,
84 "INSERT INTO " kShortcutsDBName 82 base::StringPrintf("INSERT INTO %s (id, text, url, contents, "
85 " (id, text, url, contents, contents_class, description," 83 "contents_class, description, description_class, last_access_time, "
86 " description_class, last_access_time, number_of_hits) " 84 "number_of_hits) VALUES (?,?,?,?,?,?,?,?,?)",
87 "VALUES (?,?,?,?,?,?,?,?,?)")); 85 kShortcutsTableName).c_str()));
88 BindShortcutToStatement(shortcut, &s); 86 BindShortcutToStatement(shortcut, &s);
89 87
90 return s.Run(); 88 return s.Run();
91 } 89 }
92 90
93 bool ShortcutsDatabase::UpdateShortcut( 91 bool ShortcutsDatabase::UpdateShortcut(
94 const ShortcutsBackend::Shortcut& shortcut) { 92 const ShortcutsBackend::Shortcut& shortcut) {
95 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, 93 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE,
96 "UPDATE " kShortcutsDBName " " 94 base::StringPrintf("UPDATE %s SET id=?, text=?, url=?, contents=?, "
97 "SET id=?, text=?, url=?, contents=?, contents_class=?," 95 "contents_class=?, description=?, description_class=?, "
98 " description=?, description_class=?, last_access_time=?," 96 "last_access_time=?, number_of_hits=? WHERE id=?",
99 " number_of_hits=? " 97 kShortcutsTableName).c_str()));
100 "WHERE id=?"));
101 BindShortcutToStatement(shortcut, &s); 98 BindShortcutToStatement(shortcut, &s);
102 s.BindString(9, shortcut.id); 99 s.BindString(9, shortcut.id);
103 100
104 bool result = s.Run(); 101 bool result = s.Run();
105 DCHECK_GT(db_.GetLastChangeCount(), 0); 102 DCHECK_GT(db_.GetLastChangeCount(), 0);
106 return result; 103 return result;
107 } 104 }
108 105
109 bool ShortcutsDatabase::DeleteShortcutsWithIds( 106 bool ShortcutsDatabase::DeleteShortcutsWithIds(
110 const std::vector<std::string>& shortcut_ids) { 107 const std::vector<std::string>& shortcut_ids) {
111 bool success = true; 108 bool success = true;
112 db_.BeginTransaction(); 109 db_.BeginTransaction();
113 for (std::vector<std::string>::const_iterator it = shortcut_ids.begin(); 110 for (std::vector<std::string>::const_iterator it = shortcut_ids.begin();
114 it != shortcut_ids.end(); ++it) { 111 it != shortcut_ids.end(); ++it) {
115 if (!DeleteShortcut("id", *it, db_)) 112 if (!DeleteShortcut("id", *it, db_))
116 success = false; 113 success = false;
117 } 114 }
118 db_.CommitTransaction(); 115 db_.CommitTransaction();
119 return success; 116 return success;
120 } 117 }
121 118
122 bool ShortcutsDatabase::DeleteShortcutsWithUrl( 119 bool ShortcutsDatabase::DeleteShortcutsWithUrl(
123 const std::string& shortcut_url_spec) { 120 const std::string& shortcut_url_spec) {
124 return DeleteShortcut("url", shortcut_url_spec, db_); 121 return DeleteShortcut("url", shortcut_url_spec, db_);
125 } 122 }
126 123
127 bool ShortcutsDatabase::DeleteAllShortcuts() { 124 bool ShortcutsDatabase::DeleteAllShortcuts() {
128 if (!db_.Execute("DELETE FROM " kShortcutsDBName)) 125 if (!db_.Execute(base::StringPrintf("DELETE FROM %s",
126 kShortcutsTableName).c_str()))
129 return false; 127 return false;
130 128
131 ignore_result(db_.Execute("VACUUM")); 129 ignore_result(db_.Execute("VACUUM"));
132 return true; 130 return true;
133 } 131 }
134 132
135 // Loads all of the shortcuts. 133 // Loads all of the shortcuts.
136 bool ShortcutsDatabase::LoadShortcuts(GuidToShortcutMap* shortcuts) { 134 bool ShortcutsDatabase::LoadShortcuts(GuidToShortcutMap* shortcuts) {
137 DCHECK(shortcuts); 135 DCHECK(shortcuts);
138 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, 136 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE,
139 "SELECT id, text, url, contents, contents_class, " 137 base::StringPrintf("SELECT id, text, url, contents, contents_class, "
140 "description, description_class, last_access_time, number_of_hits " 138 "description, description_class, last_access_time, number_of_hits "
141 "FROM " kShortcutsDBName)); 139 "FROM %s", kShortcutsTableName).c_str()));
142 140
143 if (!s.is_valid()) 141 if (!s.is_valid())
144 return false; 142 return false;
145 143
146 shortcuts->clear(); 144 shortcuts->clear();
147 while (s.Step()) { 145 while (s.Step()) {
148 shortcuts->insert(std::make_pair(s.ColumnString(0), 146 shortcuts->insert(std::make_pair(s.ColumnString(0),
149 ShortcutsBackend::Shortcut(s.ColumnString(0), s.ColumnString16(1), 147 ShortcutsBackend::Shortcut(s.ColumnString(0), s.ColumnString16(1),
150 GURL(s.ColumnString(2)), s.ColumnString16(3), 148 GURL(s.ColumnString(2)), s.ColumnString16(3),
151 AutocompleteMatch::ClassificationsFromString(s.ColumnString(4)), 149 AutocompleteMatch::ClassificationsFromString(s.ColumnString(4)),
152 s.ColumnString16(5), 150 s.ColumnString16(5),
153 AutocompleteMatch::ClassificationsFromString(s.ColumnString(6)), 151 AutocompleteMatch::ClassificationsFromString(s.ColumnString(6)),
154 base::Time::FromInternalValue(s.ColumnInt64(7)), s.ColumnInt(8)))); 152 base::Time::FromInternalValue(s.ColumnInt64(7)), s.ColumnInt(8))));
155 } 153 }
156 return true; 154 return true;
157 } 155 }
158 156
159 ShortcutsDatabase::~ShortcutsDatabase() {} 157 ShortcutsDatabase::~ShortcutsDatabase() {}
160 158
161 bool ShortcutsDatabase::EnsureTable() { 159 bool ShortcutsDatabase::EnsureTable() {
162 if (!db_.DoesTableExist(kShortcutsDBName)) { 160 if (!db_.DoesTableExist(kShortcutsTableName)) {
163 if (!db_.Execute(base::StringPrintf( 161 if (!db_.Execute(base::StringPrintf(
164 "CREATE TABLE %s ( " 162 "CREATE TABLE %s ( "
165 "id VARCHAR PRIMARY KEY, " 163 "id VARCHAR PRIMARY KEY, "
166 "text VARCHAR, " 164 "text VARCHAR, "
167 "url VARCHAR, " 165 "url VARCHAR, "
168 "contents VARCHAR, " 166 "contents VARCHAR, "
169 "contents_class VARCHAR, " 167 "contents_class VARCHAR, "
170 "description VARCHAR, " 168 "description VARCHAR, "
171 "description_class VARCHAR, " 169 "description_class VARCHAR, "
172 "last_access_time INTEGER, " 170 "last_access_time INTEGER, "
173 "number_of_hits INTEGER)", kShortcutsDBName).c_str())) { 171 "number_of_hits INTEGER)", kShortcutsTableName).c_str())) {
174 NOTREACHED(); 172 NOTREACHED();
175 return false; 173 return false;
176 } 174 }
177 } 175 }
178 return true; 176 return true;
179 } 177 }
180 178
181 } // namespace history 179 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698