Fix Android sync regression as a result of https://codereview.chromium.org/1144543009
[chromium-blink-merge.git] / sync / syncable / deferred_on_disk_directory_backing_store.cc
blob5e960d2d2ac6f77403ac3e78f6c323ba1ca89a92
1 // Copyright 2014 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/deferred_on_disk_directory_backing_store.h"
7 #include "base/logging.h"
8 #include "base/metrics/histogram.h"
9 #include "base/stl_util.h"
10 #include "sync/syncable/syncable-inl.h"
12 namespace syncer {
13 namespace syncable {
15 DeferredOnDiskDirectoryBackingStore::DeferredOnDiskDirectoryBackingStore(
16 const std::string& dir_name,
17 const base::FilePath& backing_file_path)
18 : OnDiskDirectoryBackingStore(dir_name, backing_file_path),
19 created_on_disk_(false) {
22 DeferredOnDiskDirectoryBackingStore::~DeferredOnDiskDirectoryBackingStore() {}
24 bool DeferredOnDiskDirectoryBackingStore::SaveChanges(
25 const Directory::SaveChangesSnapshot& snapshot) {
26 DCHECK(CalledOnValidThread());
28 // Back out early if there is nothing to save.
29 if (!snapshot.HasUnsavedMetahandleChanges()) {
30 return true;
32 if (!created_on_disk_ && !CreateOnDisk())
33 return false;
34 return OnDiskDirectoryBackingStore::SaveChanges(snapshot);
37 bool DeferredOnDiskDirectoryBackingStore::CreateOnDisk() {
38 DCHECK(CalledOnValidThread());
39 DCHECK(!created_on_disk_);
40 ResetAndCreateConnection();
41 if (!base::DeleteFile(backing_file_path(), false))
42 return false;
43 if (!Open(backing_file_path()) || !InitializeTables())
44 return false;
45 created_on_disk_ = true;
46 return true;
49 DirOpenResult DeferredOnDiskDirectoryBackingStore::Load(
50 Directory::MetahandlesMap* handles_map,
51 JournalIndex* delete_journals,
52 MetahandleSet* metahandles_to_purge,
53 Directory::KernelLoadInfo* kernel_load_info) {
54 DCHECK(CalledOnValidThread());
55 // Open an in-memory database at first to create initial sync data needed by
56 // Directory.
57 CHECK(!IsOpen());
58 if (!OpenInMemory())
59 return FAILED_OPEN_DATABASE;
61 if (!InitializeTables())
62 return FAILED_OPEN_DATABASE;
63 if (!LoadEntries(handles_map, metahandles_to_purge))
64 return FAILED_DATABASE_CORRUPT;
65 if (!LoadInfo(kernel_load_info))
66 return FAILED_DATABASE_CORRUPT;
68 return OPENED;
71 } // namespace syncable
72 } // namespace syncer