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

Side by Side Diff: components/omnibox/browser/shortcuts_database.cc

Issue 1832173002: [sql] Database recovery system for Shortcuts. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address review comments from #9 and #10. Created 4 years, 8 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
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 "components/omnibox/browser/shortcuts_database.h" 5 #include "components/omnibox/browser/shortcuts_database.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h"
9 #include "base/guid.h" 10 #include "base/guid.h"
10 #include "base/logging.h" 11 #include "base/logging.h"
11 #include "base/strings/stringprintf.h" 12 #include "base/strings/stringprintf.h"
12 #include "base/time/time.h" 13 #include "base/time/time.h"
13 #include "components/omnibox/browser/autocomplete_match_type.h" 14 #include "components/omnibox/browser/autocomplete_match_type.h"
14 #include "sql/meta_table.h" 15 #include "sql/meta_table.h"
16 #include "sql/recovery.h"
15 #include "sql/statement.h" 17 #include "sql/statement.h"
16 #include "sql/transaction.h" 18 #include "sql/transaction.h"
17 #include "ui/base/page_transition_types.h" 19 #include "ui/base/page_transition_types.h"
18 20
19 21
20 // Helpers -------------------------------------------------------------------- 22 // Helpers --------------------------------------------------------------------
21 23
22 namespace { 24 namespace {
23 25
24 // Current version number. We write databases at the "current" version number, 26 // Current version number. We write databases at the "current" version number,
(...skipping 23 matching lines...) Expand all
48 bool DeleteShortcut(const char* field_name, 50 bool DeleteShortcut(const char* field_name,
49 const std::string& id, 51 const std::string& id,
50 sql::Connection& db) { 52 sql::Connection& db) {
51 sql::Statement s(db.GetUniqueStatement( 53 sql::Statement s(db.GetUniqueStatement(
52 base::StringPrintf("DELETE FROM omni_box_shortcuts WHERE %s = ?", 54 base::StringPrintf("DELETE FROM omni_box_shortcuts WHERE %s = ?",
53 field_name).c_str())); 55 field_name).c_str()));
54 s.BindString(0, id); 56 s.BindString(0, id);
55 return s.Run(); 57 return s.Run();
56 } 58 }
57 59
60 void DatabaseErrorCallback(sql::Connection* db,
61 const base::FilePath& db_path,
62 int extended_error,
63 sql::Statement* stmt) {
64 if (sql::Recovery::ShouldRecoverOrRaze(extended_error)) {
65 // Prevent reentrant calls.
66 db->reset_error_callback();
67
68 // After this call, the |db| handle is poisoned so that future calls will
69 // return errors until the handle is re-opened.
70 sql::Recovery::RecoverDatabaseOrRaze(db, db_path);
71 }
72
73 // The default handling is to assert on debug and to ignore on release.
74 if (!sql::Connection::ShouldIgnoreSqliteError(extended_error))
Mark P 2016/04/19 23:34:27 You misunderstood my comment. I meant in the curr
Scott Hess - ex-Googler 2016/05/13 21:24:35 I'll just have the recovery path return. Connecti
75 DLOG(FATAL) << db->GetErrorMessage();
76 }
77
58 } // namespace 78 } // namespace
59 79
60 // ShortcutsDatabase::Shortcut::MatchCore ------------------------------------- 80 // ShortcutsDatabase::Shortcut::MatchCore -------------------------------------
61 81
62 ShortcutsDatabase::Shortcut::MatchCore::MatchCore( 82 ShortcutsDatabase::Shortcut::MatchCore::MatchCore(
63 const base::string16& fill_into_edit, 83 const base::string16& fill_into_edit,
64 const GURL& destination_url, 84 const GURL& destination_url,
65 const base::string16& contents, 85 const base::string16& contents,
66 const std::string& contents_class, 86 const std::string& contents_class,
67 const base::string16& description, 87 const base::string16& description,
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 136
117 // ShortcutsDatabase ---------------------------------------------------------- 137 // ShortcutsDatabase ----------------------------------------------------------
118 138
119 ShortcutsDatabase::ShortcutsDatabase(const base::FilePath& database_path) 139 ShortcutsDatabase::ShortcutsDatabase(const base::FilePath& database_path)
120 : database_path_(database_path) { 140 : database_path_(database_path) {
121 } 141 }
122 142
123 bool ShortcutsDatabase::Init() { 143 bool ShortcutsDatabase::Init() {
124 db_.set_histogram_tag("Shortcuts"); 144 db_.set_histogram_tag("Shortcuts");
125 145
146 // To recover from corruption.
147 db_.set_error_callback(
148 base::Bind(&DatabaseErrorCallback, &db_, database_path_));
149
126 // Set the database page size to something a little larger to give us 150 // Set the database page size to something a little larger to give us
127 // better performance (we're typically seek rather than bandwidth limited). 151 // better performance (we're typically seek rather than bandwidth limited).
128 // This only has an effect before any tables have been created, otherwise 152 // This only has an effect before any tables have been created, otherwise
129 // this is a NOP. Must be a power of 2 and a max of 8192. 153 // this is a NOP. Must be a power of 2 and a max of 8192.
130 db_.set_page_size(4096); 154 db_.set_page_size(4096);
131 155
132 // Run the database in exclusive mode. Nobody else should be accessing the 156 // Run the database in exclusive mode. Nobody else should be accessing the
133 // database while we're running, and this will give somewhat improved perf. 157 // database while we're running, and this will give somewhat improved perf.
134 db_.set_exclusive_locking(); 158 db_.set_exclusive_locking();
135 159
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 "UPDATE omni_box_shortcuts SET type = %d", 274 "UPDATE omni_box_shortcuts SET type = %d",
251 static_cast<int>(AutocompleteMatchType::HISTORY_TITLE)).c_str()) && 275 static_cast<int>(AutocompleteMatchType::HISTORY_TITLE)).c_str()) &&
252 db_.Execute("ALTER TABLE omni_box_shortcuts " 276 db_.Execute("ALTER TABLE omni_box_shortcuts "
253 "ADD COLUMN keyword VARCHAR") && 277 "ADD COLUMN keyword VARCHAR") &&
254 transaction.Commit())) { 278 transaction.Commit())) {
255 return false; 279 return false;
256 } 280 }
257 } 281 }
258 282
259 if (!sql::MetaTable::DoesTableExist(&db_)) { 283 if (!sql::MetaTable::DoesTableExist(&db_)) {
260 meta_table_.Init(&db_, kCurrentVersionNumber, kCompatibleVersionNumber);
261 sql::Transaction transaction(&db_); 284 sql::Transaction transaction(&db_);
262 if (!(transaction.Begin() && 285 if (!(transaction.Begin() &&
263 // Migrate old SEARCH_OTHER_ENGINE values to the new type value. 286 meta_table_.Init(
264 db_.Execute(base::StringPrintf("UPDATE omni_box_shortcuts " 287 &db_, kCurrentVersionNumber, kCompatibleVersionNumber) &&
265 "SET type = 13 WHERE type = 9").c_str()) && 288 // Migrate old SEARCH_OTHER_ENGINE values to the new type value.
266 // Migrate old EXTENSION_APP values to the new type value. 289 db_.Execute(
267 db_.Execute(base::StringPrintf("UPDATE omni_box_shortcuts " 290 "UPDATE omni_box_shortcuts SET type = 13 WHERE type = 9") &&
268 "SET type = 14 WHERE type = 10").c_str()) && 291 // Migrate old EXTENSION_APP values to the new type value.
269 // Migrate old CONTACT values to the new type value. 292 db_.Execute(
270 db_.Execute(base::StringPrintf("UPDATE omni_box_shortcuts " 293 "UPDATE omni_box_shortcuts SET type = 14 WHERE type = 10") &&
271 "SET type = 15 WHERE type = 11").c_str()) && 294 // Migrate old CONTACT values to the new type value.
272 // Migrate old BOOKMARK_TITLE values to the new type value. 295 db_.Execute(
273 db_.Execute(base::StringPrintf("UPDATE omni_box_shortcuts " 296 "UPDATE omni_box_shortcuts SET type = 15 WHERE type = 11") &&
274 "SET type = 16 WHERE type = 12").c_str()) && 297 // Migrate old BOOKMARK_TITLE values to the new type value.
275 transaction.Commit())) { 298 db_.Execute(
299 "UPDATE omni_box_shortcuts SET type = 16 WHERE type = 12") &&
300 transaction.Commit())) {
276 return false; 301 return false;
277 } 302 }
278 } 303 }
279 return true; 304 return true;
280 } 305 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698