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"
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"
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
,
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
)
54 frontend_task_runner
->PostTask(FROM_HERE
,
55 base::Bind(&CreateBackendDone
,
58 base::ThreadTaskRunnerHandle::Get(),
62 void AttachmentStore::CreateBackendDone(
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
));