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 #ifndef SYNC_API_ATTACHMENTS_ATTACHMENT_STORE_H_
6 #define SYNC_API_ATTACHMENTS_ATTACHMENT_STORE_H_
8 #include "base/callback.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "sync/api/attachments/attachment.h"
12 #include "sync/api/attachments/attachment_id.h"
13 #include "sync/base/sync_export.h"
17 class RefCountedMemory
;
18 class SequencedTaskRunner
;
26 // A place to locally store and access Attachments.
28 // Destroying this object does not necessarily cancel outstanding async
29 // operations. If you need cancel like semantics, use WeakPtr in the callbacks.
30 class SYNC_EXPORT AttachmentStoreBase
{
32 // TODO(maniscalco): Consider udpating Read and Write methods to support
33 // resumable transfers (bug 353292).
36 SUCCESS
, // No error, all completed successfully.
37 UNSPECIFIED_ERROR
, // An unspecified error occurred for one or more items.
40 typedef base::Callback
<void(const Result
&,
41 scoped_ptr
<AttachmentMap
>,
42 scoped_ptr
<AttachmentIdList
>)> ReadCallback
;
43 typedef base::Callback
<void(const Result
&)> WriteCallback
;
44 typedef base::Callback
<void(const Result
&)> DropCallback
;
46 AttachmentStoreBase();
47 virtual ~AttachmentStoreBase();
49 // Asynchronously reads the attachments identified by |ids|.
51 // |callback| will be invoked when finished. AttachmentStore will attempt to
52 // read all attachments specified in ids. If any of the attachments do not
53 // exist or could not be read, |callback|'s Result will be UNSPECIFIED_ERROR.
54 // Callback's AttachmentMap will contain all attachments that were
55 // successfully read, AttachmentIdList will contain attachment ids of
56 // attachments that are unavailable in attachment store, these need to be
57 // downloaded from server.
59 // Reads on individual attachments are treated atomically; |callback| will not
60 // read only part of an attachment.
61 virtual void Read(const AttachmentIdList
& ids
,
62 const ReadCallback
& callback
) = 0;
64 // Asynchronously writes |attachments| to the store.
66 // Will not overwrite stored attachments. Attempting to overwrite an
67 // attachment that already exists is not an error.
69 // |callback| will be invoked when finished. If any of the attachments could
70 // not be written |callback|'s Result will be UNSPECIFIED_ERROR. When this
71 // happens, some or none of the attachments may have been written
73 virtual void Write(const AttachmentList
& attachments
,
74 const WriteCallback
& callback
) = 0;
76 // Asynchronously drops |attchments| from this store.
78 // This does not remove attachments from the server.
80 // |callback| will be invoked when finished. Attempting to drop an attachment
81 // that does not exist is not an error. If any of the existing attachment
82 // could not be dropped, |callback|'s Result will be UNSPECIFIED_ERROR. When
83 // this happens, some or none of the attachments may have been dropped
85 virtual void Drop(const AttachmentIdList
& ids
,
86 const DropCallback
& callback
) = 0;
89 // AttachmentStore is an interface exposed to data type and AttachmentService
90 // code. Also contains factory methods for default implementations.
91 class SYNC_EXPORT AttachmentStore
92 : public AttachmentStoreBase
,
93 public base::RefCountedThreadSafe
<AttachmentStore
> {
95 typedef base::Callback
<void(const Result
&,
96 const scoped_refptr
<AttachmentStore
>&)>
101 // Creates an AttachmentStoreHandle backed by in-memory implementation of
102 // attachment store. For now frontend lives on the same thread as backend.
103 static scoped_refptr
<AttachmentStore
> CreateInMemoryStore();
105 // Creates an AttachmentStoreHandle backed by on-disk implementation of
106 // attachment store. Opens corresponding leveldb database located at |path|.
107 // All backend operations are scheduled to |backend_task_runner|. Opening
108 // attachment store is asynchronous, once it finishes |callback| will be
109 // called on the thread that called CreateOnDiskStore. |store| parameter of
110 // callback is only valid when |result| is SUCCESS.
111 static void CreateOnDiskStore(
112 const base::FilePath
& path
,
113 const scoped_refptr
<base::SequencedTaskRunner
>& backend_task_runner
,
114 const CreateCallback
& callback
);
117 friend class base::RefCountedThreadSafe
<AttachmentStore
>;
118 ~AttachmentStore() override
;
121 static void CreateOnDiskStoreOnBackendThread(
122 const base::FilePath
& path
,
123 const scoped_refptr
<base::SequencedTaskRunner
>& frontend_task_runner
,
124 const CreateCallback
& callback
);
126 static void CreateBackendDone(
127 const Result
& result
,
128 scoped_ptr
<AttachmentStoreBase
> backend
,
129 const scoped_refptr
<base::SequencedTaskRunner
>& backend_task_runner
,
130 const CreateCallback
& callback
);
133 } // namespace syncer
135 #endif // SYNC_API_ATTACHMENTS_ATTACHMENT_STORE_H_