Automated Commit: Committing new LKGM version 6953.0.0 for chromeos.
[chromium-blink-merge.git] / sync / syncable / on_disk_directory_backing_store.cc
blobf369e9360bc952ff835cdf3418af128c63d7aea1
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
3 // found in the LICENSE file.
5 #include "sync/syncable/on_disk_directory_backing_store.h"
7 #include "base/logging.h"
8 #include "base/stl_util.h"
9 #include "base/metrics/histogram.h"
10 #include "sync/syncable/syncable-inl.h"
12 namespace syncer {
13 namespace syncable {
15 namespace {
17 enum HistogramResultEnum {
18 FIRST_TRY_SUCCESS,
19 SECOND_TRY_SUCCESS,
20 SECOND_TRY_FAILURE,
21 RESULT_COUNT
24 } // namespace
26 OnDiskDirectoryBackingStore::OnDiskDirectoryBackingStore(
27 const std::string& dir_name, const base::FilePath& backing_filepath)
28 : DirectoryBackingStore(dir_name),
29 allow_failure_for_test_(false),
30 backing_filepath_(backing_filepath) {
33 OnDiskDirectoryBackingStore::~OnDiskDirectoryBackingStore() { }
35 DirOpenResult OnDiskDirectoryBackingStore::TryLoad(
36 Directory::MetahandlesMap* handles_map,
37 JournalIndex* delete_journals,
38 MetahandleSet* metahandles_to_purge,
39 Directory::KernelLoadInfo* kernel_load_info) {
40 DCHECK(CalledOnValidThread());
41 if (!db_->is_open()) {
42 if (!db_->Open(backing_filepath_))
43 return FAILED_OPEN_DATABASE;
46 if (!InitializeTables())
47 return FAILED_OPEN_DATABASE;
49 if (!LoadEntries(handles_map, metahandles_to_purge))
50 return FAILED_DATABASE_CORRUPT;
51 if (!LoadDeleteJournals(delete_journals))
52 return FAILED_DATABASE_CORRUPT;
53 if (!LoadInfo(kernel_load_info))
54 return FAILED_DATABASE_CORRUPT;
55 if (!VerifyReferenceIntegrity(handles_map))
56 return FAILED_DATABASE_CORRUPT;
58 return OPENED;
62 DirOpenResult OnDiskDirectoryBackingStore::Load(
63 Directory::MetahandlesMap* handles_map,
64 JournalIndex* delete_journals,
65 MetahandleSet* metahandles_to_purge,
66 Directory::KernelLoadInfo* kernel_load_info) {
67 DirOpenResult result = TryLoad(handles_map, delete_journals,
68 metahandles_to_purge, kernel_load_info);
69 if (result == OPENED) {
70 UMA_HISTOGRAM_ENUMERATION(
71 "Sync.DirectoryOpenResult", FIRST_TRY_SUCCESS, RESULT_COUNT);
72 return OPENED;
75 ReportFirstTryOpenFailure();
77 // The fallback: delete the current database and return a fresh one. We can
78 // fetch the user's data from the cloud.
79 STLDeleteValues(handles_map);
80 STLDeleteElements(delete_journals);
82 ResetAndCreateConnection();
84 base::DeleteFile(backing_filepath_, false);
86 result = TryLoad(handles_map, delete_journals, metahandles_to_purge,
87 kernel_load_info);
88 if (result == OPENED) {
89 UMA_HISTOGRAM_ENUMERATION(
90 "Sync.DirectoryOpenResult", SECOND_TRY_SUCCESS, RESULT_COUNT);
91 } else {
92 UMA_HISTOGRAM_ENUMERATION(
93 "Sync.DirectoryOpenResult", SECOND_TRY_FAILURE, RESULT_COUNT);
96 return result;
99 void OnDiskDirectoryBackingStore::ReportFirstTryOpenFailure() {
100 // In debug builds, the last thing we want is to silently clear the database.
101 // It's full of evidence that might help us determine what went wrong. It
102 // might be sqlite's fault, but it could also be a bug in sync. We crash
103 // immediately so a developer can investigate.
105 // Developers: If you're not interested in debugging this right now, just move
106 // aside the 'Sync Data' directory in your profile. This is similar to what
107 // the code would do if this DCHECK were disabled.
108 NOTREACHED() << "Crashing to preserve corrupt sync database";
111 } // namespace syncable
112 } // namespace syncer