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 #ifndef SYNC_API_ATTACHMENTS_ATTACHMENT_STORE_H_ | 5 #ifndef SYNC_API_ATTACHMENTS_ATTACHMENT_STORE_H_ |
6 #define SYNC_API_ATTACHMENTS_ATTACHMENT_STORE_H_ | 6 #define SYNC_API_ATTACHMENTS_ATTACHMENT_STORE_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "sync/api/attachments/attachment.h" | 11 #include "sync/api/attachments/attachment.h" |
12 #include "sync/api/attachments/attachment_id.h" | 12 #include "sync/api/attachments/attachment_id.h" |
13 #include "sync/api/attachments/attachment_metadata.h" | 13 #include "sync/api/attachments/attachment_metadata.h" |
14 #include "sync/base/sync_export.h" | 14 #include "sync/base/sync_export.h" |
15 | 15 |
16 namespace base { | 16 namespace base { |
17 class FilePath; | 17 class FilePath; |
| 18 class RefCountedMemory; |
18 class SequencedTaskRunner; | 19 class SequencedTaskRunner; |
19 } // namespace base | 20 } // namespace base |
20 | 21 |
21 namespace syncer { | 22 namespace syncer { |
22 | 23 |
23 class AttachmentStoreFrontend; | 24 class Attachment; |
24 class AttachmentStoreBackend; | 25 class AttachmentId; |
25 | 26 |
26 // AttachmentStore is a place to locally store and access Attachments. | 27 // AttachmentStore is a place to locally store and access Attachments. |
27 // | 28 // |
28 // AttachmentStore class is an interface exposed to data type and | 29 // AttachmentStore class is an interface exposed to data type and |
29 // AttachmentService code. | 30 // AttachmentService code. |
30 // It also contains factory methods for default attachment store | 31 // It also contains factory methods for default attachment store |
31 // implementations. | 32 // implementations. |
32 // Destroying this object does not necessarily cancel outstanding async | 33 // Destroying this object does not necessarily cancel outstanding async |
33 // operations. If you need cancel like semantics, use WeakPtr in the callbacks. | 34 // operations. If you need cancel like semantics, use WeakPtr in the callbacks. |
34 class SYNC_EXPORT AttachmentStore { | 35 class SYNC_EXPORT AttachmentStore |
| 36 : public base::RefCountedThreadSafe<AttachmentStore> { |
35 public: | 37 public: |
36 // TODO(maniscalco): Consider udpating Read and Write methods to support | 38 // TODO(maniscalco): Consider udpating Read and Write methods to support |
37 // resumable transfers (bug 353292). | 39 // resumable transfers (bug 353292). |
38 | 40 |
39 // The result status of an attachment store operation. | 41 // The result status of an attachment store operation. |
40 // Do not re-order or delete these entries; they are used in a UMA histogram. | 42 // Do not re-order or delete these entries; they are used in a UMA histogram. |
41 enum Result { | 43 enum Result { |
42 SUCCESS = 0, // No error, all completed successfully. | 44 SUCCESS = 0, // No error, all completed successfully. |
43 UNSPECIFIED_ERROR = 1, // An unspecified error occurred for >= 1 item. | 45 UNSPECIFIED_ERROR = 1, // An unspecified error occurred for >= 1 item. |
44 STORE_INITIALIZATION_FAILED = 2, // AttachmentStore initialization failed. | 46 STORE_INITIALIZATION_FAILED = 2, // AttachmentStore initialization failed. |
45 // When adding a value here, you must increment RESULT_SIZE below. | 47 // When adding a value here, you must increment RESULT_SIZE below. |
46 }; | 48 }; |
47 static const int RESULT_SIZE = | 49 static const int RESULT_SIZE = |
48 10; // Size of the Result enum; used for histograms. | 50 10; // Size of the Result enum; used for histograms. |
49 | 51 |
50 // Each attachment can have references from sync or model type. Tracking these | |
51 // references is needed for lifetime management of attachment, it can only be | |
52 // deleted from the store when it doesn't have references. | |
53 enum AttachmentReferrer { | |
54 MODEL_TYPE, | |
55 SYNC, | |
56 }; | |
57 | |
58 typedef base::Callback<void(const Result&)> InitCallback; | 52 typedef base::Callback<void(const Result&)> InitCallback; |
59 typedef base::Callback<void(const Result&, | 53 typedef base::Callback<void(const Result&, |
60 scoped_ptr<AttachmentMap>, | 54 scoped_ptr<AttachmentMap>, |
61 scoped_ptr<AttachmentIdList>)> ReadCallback; | 55 scoped_ptr<AttachmentIdList>)> ReadCallback; |
62 typedef base::Callback<void(const Result&)> WriteCallback; | 56 typedef base::Callback<void(const Result&)> WriteCallback; |
63 typedef base::Callback<void(const Result&)> DropCallback; | 57 typedef base::Callback<void(const Result&)> DropCallback; |
64 typedef base::Callback<void(const Result&, | 58 typedef base::Callback<void(const Result&, |
65 scoped_ptr<AttachmentMetadataList>)> | 59 scoped_ptr<AttachmentMetadataList>)> |
66 ReadMetadataCallback; | 60 ReadMetadataCallback; |
67 | 61 |
68 ~AttachmentStore(); | 62 AttachmentStore(); |
| 63 |
| 64 // Asynchronously initializes attachment store. |
| 65 // |
| 66 // This method should not be called by consumer of this interface. It is |
| 67 // called by factory methods in AttachmentStore class. When initialization is |
| 68 // complete |callback| is invoked with result, in case of failure result is |
| 69 // UNSPECIFIED_ERROR. |
| 70 virtual void Init(const InitCallback& callback) = 0; |
69 | 71 |
70 // Asynchronously reads the attachments identified by |ids|. | 72 // Asynchronously reads the attachments identified by |ids|. |
71 // | 73 // |
72 // |callback| will be invoked when finished. AttachmentStore will attempt to | 74 // |callback| will be invoked when finished. AttachmentStore will attempt to |
73 // read all attachments specified in ids. If any of the attachments do not | 75 // read all attachments specified in ids. If any of the attachments do not |
74 // exist or could not be read, |callback|'s Result will be UNSPECIFIED_ERROR. | 76 // exist or could not be read, |callback|'s Result will be UNSPECIFIED_ERROR. |
75 // Callback's AttachmentMap will contain all attachments that were | 77 // Callback's AttachmentMap will contain all attachments that were |
76 // successfully read, AttachmentIdList will contain attachment ids of | 78 // successfully read, AttachmentIdList will contain attachment ids of |
77 // attachments that are unavailable in attachment store, these need to be | 79 // attachments that are unavailable in attachment store, these need to be |
78 // downloaded from server. | 80 // downloaded from server. |
79 // | 81 // |
80 // Reads on individual attachments are treated atomically; |callback| will not | 82 // Reads on individual attachments are treated atomically; |callback| will not |
81 // read only part of an attachment. | 83 // read only part of an attachment. |
82 void Read(const AttachmentIdList& ids, const ReadCallback& callback); | 84 virtual void Read(const AttachmentIdList& ids, |
| 85 const ReadCallback& callback) = 0; |
83 | 86 |
84 // Asynchronously writes |attachments| to the store. | 87 // Asynchronously writes |attachments| to the store. |
85 // | 88 // |
86 // Will not overwrite stored attachments. Attempting to overwrite an | 89 // Will not overwrite stored attachments. Attempting to overwrite an |
87 // attachment that already exists is not an error. | 90 // attachment that already exists is not an error. |
88 // | 91 // |
89 // |callback| will be invoked when finished. If any of the attachments could | 92 // |callback| will be invoked when finished. If any of the attachments could |
90 // not be written |callback|'s Result will be UNSPECIFIED_ERROR. When this | 93 // not be written |callback|'s Result will be UNSPECIFIED_ERROR. When this |
91 // happens, some or none of the attachments may have been written | 94 // happens, some or none of the attachments may have been written |
92 // successfully. | 95 // successfully. |
93 void Write(const AttachmentList& attachments, const WriteCallback& callback); | 96 virtual void Write(const AttachmentList& attachments, |
| 97 const WriteCallback& callback) = 0; |
94 | 98 |
95 // Asynchronously drops |attchments| from this store. | 99 // Asynchronously drops |attchments| from this store. |
96 // | 100 // |
97 // This does not remove attachments from the server. | 101 // This does not remove attachments from the server. |
98 // | 102 // |
99 // |callback| will be invoked when finished. Attempting to drop an attachment | 103 // |callback| will be invoked when finished. Attempting to drop an attachment |
100 // that does not exist is not an error. If any of the existing attachment | 104 // that does not exist is not an error. If any of the existing attachment |
101 // could not be dropped, |callback|'s Result will be UNSPECIFIED_ERROR. When | 105 // could not be dropped, |callback|'s Result will be UNSPECIFIED_ERROR. When |
102 // this happens, some or none of the attachments may have been dropped | 106 // this happens, some or none of the attachments may have been dropped |
103 // successfully. | 107 // successfully. |
104 void Drop(const AttachmentIdList& ids, const DropCallback& callback); | 108 virtual void Drop(const AttachmentIdList& ids, |
| 109 const DropCallback& callback) = 0; |
105 | 110 |
106 // Asynchronously reads metadata for the attachments identified by |ids|. | 111 // Asynchronously reads metadata for the attachments identified by |ids|. |
107 // | 112 // |
108 // |callback| will be invoked when finished. AttachmentStore will attempt to | 113 // |callback| will be invoked when finished. AttachmentStore will attempt to |
109 // read metadata for all attachments specified in ids. If any of the | 114 // read metadata for all attachments specified in ids. If any of the |
110 // metadata entries do not exist or could not be read, |callback|'s Result | 115 // metadata entries do not exist or could not be read, |callback|'s Result |
111 // will be UNSPECIFIED_ERROR. | 116 // will be UNSPECIFIED_ERROR. |
112 void ReadMetadata(const AttachmentIdList& ids, | 117 virtual void ReadMetadata(const AttachmentIdList& ids, |
113 const ReadMetadataCallback& callback); | 118 const ReadMetadataCallback& callback) = 0; |
114 | 119 |
115 // Asynchronously reads metadata for all attachments in the store. | 120 // Asynchronously reads metadata for all attachments in the store. |
116 // | 121 // |
117 // |callback| will be invoked when finished. If any of the metadata entries | 122 // |callback| will be invoked when finished. If any of the metadata entries |
118 // could not be read, |callback|'s Result will be UNSPECIFIED_ERROR. | 123 // could not be read, |callback|'s Result will be UNSPECIFIED_ERROR. |
119 void ReadAllMetadata(const ReadMetadataCallback& callback); | 124 virtual void ReadAllMetadata(const ReadMetadataCallback& callback) = 0; |
120 | 125 |
121 // Given current AttachmentStore (this) creates separate AttachmentStore that | 126 // Creates an AttachmentStoreHandle backed by in-memory implementation of |
122 // will be used by sync components (AttachmentService). Resulting | 127 // attachment store. For now frontend lives on the same thread as backend. |
123 // AttachmentStore is backed by the same frontend/backend. | 128 static scoped_refptr<AttachmentStore> CreateInMemoryStore(); |
124 scoped_ptr<AttachmentStore> CreateAttachmentStoreForSync() const; | |
125 | 129 |
126 // Creates an AttachmentStore backed by in-memory implementation of attachment | 130 // Creates an AttachmentStoreHandle backed by on-disk implementation of |
127 // store. For now frontend lives on the same thread as backend. | 131 // attachment store. Opens corresponding leveldb database located at |path|. |
128 static scoped_ptr<AttachmentStore> CreateInMemoryStore(); | 132 // All backend operations are scheduled to |backend_task_runner|. Opening |
129 | 133 // attachment store is asynchronous, once it finishes |callback| will be |
130 // Creates an AttachmentStore backed by on-disk implementation of attachment | 134 // called on the thread that called CreateOnDiskStore. Calling Read/Write/Drop |
131 // store. Opens corresponding leveldb database located at |path|. All backend | 135 // before initialization completed is allowed. Later if initialization fails |
132 // operations are scheduled to |backend_task_runner|. Opening attachment store | 136 // these operations will fail with STORE_INITIALIZATION_FAILED error. |
133 // is asynchronous, once it finishes |callback| will be called on the thread | 137 static scoped_refptr<AttachmentStore> CreateOnDiskStore( |
134 // that called CreateOnDiskStore. Calling Read/Write/Drop before | |
135 // initialization completed is allowed. Later if initialization fails these | |
136 // operations will fail with STORE_INITIALIZATION_FAILED error. | |
137 static scoped_ptr<AttachmentStore> CreateOnDiskStore( | |
138 const base::FilePath& path, | 138 const base::FilePath& path, |
139 const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner, | 139 const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner, |
140 const InitCallback& callback); | 140 const InitCallback& callback); |
141 | 141 |
142 // Creates set of AttachmentStore/AttachmentStoreFrontend instances for tests | 142 protected: |
143 // that provide their own implementation of AttachmentstoreBackend for | 143 friend class base::RefCountedThreadSafe<AttachmentStore>; |
144 // mocking. | 144 virtual ~AttachmentStore(); |
145 static scoped_ptr<AttachmentStore> CreateMockStoreForTest( | 145 }; |
146 scoped_ptr<AttachmentStoreBackend> backend); | 146 |
| 147 // Interface for AttachmentStore backends. |
| 148 // |
| 149 // AttachmentStoreBackend provides interface for different backends (on-disk, |
| 150 // in-memory). Factory methods in AttachmentStore create corresponding backend |
| 151 // and pass reference to AttachmentStoreHandle. |
| 152 // All functions in AttachmentStoreBackend mirror corresponding functions in |
| 153 // AttachmentStore. |
| 154 // All callbacks and result codes are used directly from AttachmentStore. |
| 155 // AttachmentStoreHandle only passes callbacks and results, there is no need to |
| 156 // declare separate set. |
| 157 class SYNC_EXPORT AttachmentStoreBackend { |
| 158 public: |
| 159 explicit AttachmentStoreBackend( |
| 160 const scoped_refptr<base::SequencedTaskRunner>& callback_task_runner); |
| 161 virtual ~AttachmentStoreBackend(); |
| 162 virtual void Init(const AttachmentStore::InitCallback& callback) = 0; |
| 163 virtual void Read(const AttachmentIdList& ids, |
| 164 const AttachmentStore::ReadCallback& callback) = 0; |
| 165 virtual void Write(const AttachmentList& attachments, |
| 166 const AttachmentStore::WriteCallback& callback) = 0; |
| 167 virtual void Drop(const AttachmentIdList& ids, |
| 168 const AttachmentStore::DropCallback& callback) = 0; |
| 169 virtual void ReadMetadata( |
| 170 const AttachmentIdList& ids, |
| 171 const AttachmentStore::ReadMetadataCallback& callback) = 0; |
| 172 virtual void ReadAllMetadata( |
| 173 const AttachmentStore::ReadMetadataCallback& callback) = 0; |
| 174 |
| 175 protected: |
| 176 // Helper function to post callback on callback_task_runner. |
| 177 void PostCallback(const base::Closure& callback); |
147 | 178 |
148 private: | 179 private: |
149 AttachmentStore(const scoped_refptr<AttachmentStoreFrontend>& frontend, | 180 scoped_refptr<base::SequencedTaskRunner> callback_task_runner_; |
150 AttachmentReferrer referrer); | |
151 | 181 |
152 scoped_refptr<AttachmentStoreFrontend> frontend_; | 182 DISALLOW_COPY_AND_ASSIGN(AttachmentStoreBackend); |
153 // Modification operations with attachment store will be performed on behalf | |
154 // of |referrer_|. | |
155 const AttachmentReferrer referrer_; | |
156 | |
157 DISALLOW_COPY_AND_ASSIGN(AttachmentStore); | |
158 }; | 183 }; |
159 | 184 |
160 } // namespace syncer | 185 } // namespace syncer |
161 | 186 |
162 #endif // SYNC_API_ATTACHMENTS_ATTACHMENT_STORE_H_ | 187 #endif // SYNC_API_ATTACHMENTS_ATTACHMENT_STORE_H_ |
OLD | NEW |