OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sync/api/attachments/attachment_store.h" | 5 #include "sync/api/attachments/attachment_store.h" |
6 | 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback.h" |
| 9 #include "base/location.h" |
| 10 #include "base/sequenced_task_runner.h" |
7 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
8 #include "sync/internal_api/public/attachments/attachment_store_handle.h" | 12 #include "sync/internal_api/public/attachments/attachment_store_handle.h" |
9 #include "sync/internal_api/public/attachments/in_memory_attachment_store.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" |
10 | 15 |
11 namespace syncer { | 16 namespace syncer { |
12 | 17 |
13 AttachmentStoreBase::AttachmentStoreBase() {} | 18 AttachmentStoreBase::AttachmentStoreBase() {} |
14 AttachmentStoreBase::~AttachmentStoreBase() {} | 19 AttachmentStoreBase::~AttachmentStoreBase() {} |
15 | 20 |
16 AttachmentStore::AttachmentStore() {} | 21 AttachmentStore::AttachmentStore() {} |
17 AttachmentStore::~AttachmentStore() {} | 22 AttachmentStore::~AttachmentStore() {} |
18 | 23 |
19 scoped_refptr<AttachmentStore> AttachmentStore::CreateInMemoryStore() { | 24 scoped_refptr<AttachmentStore> AttachmentStore::CreateInMemoryStore() { |
20 // Both frontend and backend of attachment store will live on current thread. | 25 // Both frontend and backend of attachment store will live on current thread. |
21 scoped_ptr<AttachmentStoreBase> backend( | 26 scoped_ptr<AttachmentStoreBase> backend( |
22 new InMemoryAttachmentStore(base::ThreadTaskRunnerHandle::Get())); | 27 new InMemoryAttachmentStore(base::ThreadTaskRunnerHandle::Get())); |
23 return scoped_refptr<AttachmentStore>(new AttachmentStoreHandle( | 28 return scoped_refptr<AttachmentStore>(new AttachmentStoreHandle( |
24 backend.Pass(), base::ThreadTaskRunnerHandle::Get())); | 29 backend.Pass(), base::ThreadTaskRunnerHandle::Get())); |
25 } | 30 } |
26 | 31 |
| 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)); |
| 43 } |
| 44 |
| 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)); |
| 60 } |
| 61 |
| 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); |
| 70 } |
| 71 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 72 FROM_HERE, base::Bind(callback, result, store)); |
| 73 } |
| 74 |
27 } // namespace syncer | 75 } // namespace syncer |
OLD | NEW |