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

Side by Side Diff: sync/syncable/directory_backing_store.cc

Issue 1072093002: [Sync] Update DirectoryBackingStore to detect Sync DB corruption (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Merge with master. Created 5 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 2012 The Chromium Authors. All rights reserved. 1 // Copyright 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 "sync/syncable/directory_backing_store.h" 5 #include "sync/syncable/directory_backing_store.h"
6 6
7 #include "build/build_config.h" 7 #include "build/build_config.h"
8 8
9 #include <limits> 9 #include <limits>
10 10
11 #include "base/base64.h" 11 #include "base/base64.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/metrics/field_trial.h" 13 #include "base/metrics/field_trial.h"
14 #include "base/rand_util.h" 14 #include "base/rand_util.h"
15 #include "base/strings/stringprintf.h" 15 #include "base/strings/stringprintf.h"
16 #include "base/thread_task_runner_handle.h"
16 #include "base/time/time.h" 17 #include "base/time/time.h"
17 #include "base/trace_event/trace_event.h" 18 #include "base/trace_event/trace_event.h"
18 #include "sql/connection.h" 19 #include "sql/connection.h"
20 #include "sql/error_delegate_util.h"
19 #include "sql/statement.h" 21 #include "sql/statement.h"
20 #include "sql/transaction.h" 22 #include "sql/transaction.h"
21 #include "sync/internal_api/public/base/node_ordinal.h" 23 #include "sync/internal_api/public/base/node_ordinal.h"
22 #include "sync/protocol/bookmark_specifics.pb.h" 24 #include "sync/protocol/bookmark_specifics.pb.h"
23 #include "sync/protocol/sync.pb.h" 25 #include "sync/protocol/sync.pb.h"
24 #include "sync/syncable/syncable-inl.h" 26 #include "sync/syncable/syncable-inl.h"
25 #include "sync/syncable/syncable_columns.h" 27 #include "sync/syncable/syncable_columns.h"
26 #include "sync/syncable/syncable_util.h" 28 #include "sync/syncable/syncable_util.h"
27 #include "sync/util/time.h" 29 #include "sync/util/time.h"
28 30
29 using std::string; 31 using std::string;
30 32
31 namespace { 33 namespace {
32 34
33 bool IsSyncBackingDatabase32KEnabled() { 35 bool IsSyncBackingDatabase32KEnabled() {
34 const std::string group_name = 36 const std::string group_name =
35 base::FieldTrialList::FindFullName("SyncBackingDatabase32K"); 37 base::FieldTrialList::FindFullName("SyncBackingDatabase32K");
36 return group_name == "Enabled"; 38 return group_name == "Enabled";
37 } 39 }
38 40
41 void OnSqliteError(
42 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
43 const base::Closure& catastrophic_error_handler,
44 int err,
45 sql::Statement* statement) {
46 // An error has been detected. Ignore unless it is catastrophic.
47 if (sql::IsErrorCatastrophic(err)) {
48 // At this point sql::* and DirectoryBackingStore may be on the callstack so
49 // don't invoke the error handler directly. Instead, PostTask to this thread
50 // to avoid potential reentrancy issues.
51 task_runner->PostTask(FROM_HERE, catastrophic_error_handler);
Nicolas Zea 2015/04/13 20:16:33 nit: do you need the task runner? Can you just pos
maniscalco 2015/04/14 17:44:14 Good idea. Done.
52 }
53 }
54
39 } // namespace 55 } // namespace
40 56
41 namespace syncer { 57 namespace syncer {
42 namespace syncable { 58 namespace syncable {
43 59
44 // This just has to be big enough to hold an UPDATE or INSERT statement that 60 // This just has to be big enough to hold an UPDATE or INSERT statement that
45 // modifies all the columns in the entry table. 61 // modifies all the columns in the entry table.
46 static const string::size_type kUpdateStatementBufferSize = 2048; 62 static const string::size_type kUpdateStatementBufferSize = 2048;
47 63
48 // Increment this version whenever updating DB tables. 64 // Increment this version whenever updating DB tables.
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 194
179 } // namespace 195 } // namespace
180 196
181 /////////////////////////////////////////////////////////////////////////////// 197 ///////////////////////////////////////////////////////////////////////////////
182 // DirectoryBackingStore implementation. 198 // DirectoryBackingStore implementation.
183 199
184 DirectoryBackingStore::DirectoryBackingStore(const string& dir_name) 200 DirectoryBackingStore::DirectoryBackingStore(const string& dir_name)
185 : dir_name_(dir_name), 201 : dir_name_(dir_name),
186 database_page_size_(IsSyncBackingDatabase32KEnabled() ? 32768 : 4096), 202 database_page_size_(IsSyncBackingDatabase32KEnabled() ? 32768 : 4096),
187 needs_column_refresh_(false) { 203 needs_column_refresh_(false) {
204 DCHECK(base::ThreadTaskRunnerHandle::IsSet());
188 ResetAndCreateConnection(); 205 ResetAndCreateConnection();
189 } 206 }
190 207
191 DirectoryBackingStore::DirectoryBackingStore(const string& dir_name, 208 DirectoryBackingStore::DirectoryBackingStore(const string& dir_name,
192 sql::Connection* db) 209 sql::Connection* db)
193 : db_(db), 210 : db_(db),
194 dir_name_(dir_name), 211 dir_name_(dir_name),
195 database_page_size_(IsSyncBackingDatabase32KEnabled() ? 32768 : 4096), 212 database_page_size_(IsSyncBackingDatabase32KEnabled() ? 32768 : 4096),
196 needs_column_refresh_(false) { 213 needs_column_refresh_(false) {
214 DCHECK(base::ThreadTaskRunnerHandle::IsSet());
197 } 215 }
198 216
199 DirectoryBackingStore::~DirectoryBackingStore() { 217 DirectoryBackingStore::~DirectoryBackingStore() {
200 } 218 }
201 219
202 bool DirectoryBackingStore::DeleteEntries(EntryTable from, 220 bool DirectoryBackingStore::DeleteEntries(EntryTable from,
203 const MetahandleSet& handles) { 221 const MetahandleSet& handles) {
204 if (handles.empty()) 222 if (handles.empty())
205 return true; 223 return true;
206 224
(...skipping 1427 matching lines...) Expand 10 before | Expand all | Expand 10 after
1634 bool DirectoryBackingStore::needs_column_refresh() const { 1652 bool DirectoryBackingStore::needs_column_refresh() const {
1635 return needs_column_refresh_; 1653 return needs_column_refresh_;
1636 } 1654 }
1637 1655
1638 void DirectoryBackingStore::ResetAndCreateConnection() { 1656 void DirectoryBackingStore::ResetAndCreateConnection() {
1639 db_.reset(new sql::Connection()); 1657 db_.reset(new sql::Connection());
1640 db_->set_histogram_tag("SyncDirectory"); 1658 db_->set_histogram_tag("SyncDirectory");
1641 db_->set_exclusive_locking(); 1659 db_->set_exclusive_locking();
1642 db_->set_cache_size(32); 1660 db_->set_cache_size(32);
1643 db_->set_page_size(database_page_size_); 1661 db_->set_page_size(database_page_size_);
1662 if (!catastrophic_error_handler_.is_null())
1663 SetCatastrophicErrorHandler(catastrophic_error_handler_);
1664 }
1665
1666 void DirectoryBackingStore::SetCatastrophicErrorHandler(
1667 const base::Closure& catastrophic_error_handler) {
1668 DCHECK(CalledOnValidThread());
1669 DCHECK(!catastrophic_error_handler.is_null());
1670 catastrophic_error_handler_ = catastrophic_error_handler;
1671 sql::Connection::ErrorCallback error_callback =
1672 base::Bind(&OnSqliteError, base::ThreadTaskRunnerHandle::Get(),
1673 catastrophic_error_handler_);
1674 db_->set_error_callback(error_callback);
1644 } 1675 }
1645 1676
1646 } // namespace syncable 1677 } // namespace syncable
1647 } // namespace syncer 1678 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698