1116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
2116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// found in the LICENSE file.
4116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "sync/internal_api/public/util/sync_db_util.h"
6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
7116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/files/file_path.h"
8116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/single_thread_task_runner.h"
9116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "sql/connection.h"
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "sync/syncable/directory.h"
11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace syncer {
13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
14116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid CheckSyncDbLastModifiedTime(
15116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const base::FilePath& sync_dir,
16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    scoped_refptr<base::SingleThreadTaskRunner> callback_runner,
17116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    base::Callback<void(base::Time)> callback) {
18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const base::FilePath sync_db =
19116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      sync_dir.Append(syncable::Directory::kSyncDatabaseFilename);
20116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  base::File f(sync_db, base::File::FLAG_OPEN | base::File::FLAG_READ);
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  base::File::Info info;
23116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (!f.IsValid() || !f.GetInfo(&info)) {
24116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    callback_runner->PostTask(FROM_HERE, base::Bind(callback, base::Time()));
25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return;
26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  f.Close();
28116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
29116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  sql::Connection db;
30116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (!db.Open(sync_db) || !db.QuickIntegrityCheck()) {
31116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    callback_runner->PostTask(FROM_HERE, base::Bind(callback, base::Time()));
32116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  } else {
33116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    callback_runner->PostTask(FROM_HERE,
34116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                              base::Bind(callback, info.last_modified));
35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
36116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}  // namespace syncer
39