Roll leveldb 3f7758:803d69 (v1.17 -> v1.18)
[chromium-blink-merge.git] / sync / api / attachments / attachment_store.cc
blobdf785bbf39f6242b391b5ab583a063b0427b6e7b
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/api/attachments/attachment_store.h"
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/location.h"
10 #include "base/sequenced_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "sync/internal_api/public/attachments/attachment_store_handle.h"
13 #include "sync/internal_api/public/attachments/in_memory_attachment_store.h"
14 #include "sync/internal_api/public/attachments/on_disk_attachment_store.h"
16 namespace syncer {
18 AttachmentStoreBase::AttachmentStoreBase() {}
19 AttachmentStoreBase::~AttachmentStoreBase() {}
21 AttachmentStore::AttachmentStore() {}
22 AttachmentStore::~AttachmentStore() {}
24 scoped_refptr<AttachmentStore> AttachmentStore::CreateInMemoryStore() {
25 // Both frontend and backend of attachment store will live on current thread.
26 scoped_ptr<AttachmentStoreBase> backend(
27 new InMemoryAttachmentStore(base::ThreadTaskRunnerHandle::Get()));
28 return scoped_refptr<AttachmentStore>(new AttachmentStoreHandle(
29 backend.Pass(), base::ThreadTaskRunnerHandle::Get()));
32 void AttachmentStore::CreateOnDiskStore(
33 const base::FilePath& path,
34 const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner,
35 const CreateCallback& callback) {
36 scoped_refptr<base::SequencedTaskRunner> frontend_task_runner =
37 base::ThreadTaskRunnerHandle::Get();
38 backend_task_runner->PostTask(FROM_HERE,
39 base::Bind(&CreateOnDiskStoreOnBackendThread,
40 path,
41 frontend_task_runner,
42 callback));
45 void AttachmentStore::CreateOnDiskStoreOnBackendThread(
46 const base::FilePath& path,
47 const scoped_refptr<base::SequencedTaskRunner>& frontend_task_runner,
48 const CreateCallback& callback) {
49 scoped_ptr<OnDiskAttachmentStore> store(
50 new OnDiskAttachmentStore(frontend_task_runner));
51 Result result = store->OpenOrCreate(path);
52 if (result != SUCCESS)
53 store.reset();
54 frontend_task_runner->PostTask(FROM_HERE,
55 base::Bind(&CreateBackendDone,
56 result,
57 base::Passed(&store),
58 base::ThreadTaskRunnerHandle::Get(),
59 callback));
62 void AttachmentStore::CreateBackendDone(
63 const Result& result,
64 scoped_ptr<AttachmentStoreBase> backend,
65 const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner,
66 const CreateCallback& callback) {
67 scoped_refptr<AttachmentStore> store;
68 if (result == SUCCESS) {
69 store = new AttachmentStoreHandle(backend.Pass(), backend_task_runner);
71 base::ThreadTaskRunnerHandle::Get()->PostTask(
72 FROM_HERE, base::Bind(callback, result, store));
75 } // namespace syncer