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 SequencedTaskRunner; | 18 class SequencedTaskRunner; |
19 } // namespace base | 19 } // namespace base |
20 | 20 |
21 namespace syncer { | 21 namespace syncer { |
22 | 22 |
| 23 class AttachmentStoreBackend; |
| 24 class AttachmentStoreForSync; |
23 class AttachmentStoreFrontend; | 25 class AttachmentStoreFrontend; |
24 class AttachmentStoreBackend; | |
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 { |
35 public: | 36 public: |
36 // TODO(maniscalco): Consider udpating Read and Write methods to support | 37 // TODO(maniscalco): Consider udpating Read and Write methods to support |
37 // resumable transfers (bug 353292). | 38 // resumable transfers (bug 353292). |
38 | 39 |
39 // The result status of an attachment store operation. | 40 // The result status of an attachment store operation. |
40 // Do not re-order or delete these entries; they are used in a UMA histogram. | 41 // Do not re-order or delete these entries; they are used in a UMA histogram. |
41 enum Result { | 42 enum Result { |
42 SUCCESS = 0, // No error, all completed successfully. | 43 SUCCESS = 0, // No error, all completed successfully. |
43 UNSPECIFIED_ERROR = 1, // An unspecified error occurred for >= 1 item. | 44 UNSPECIFIED_ERROR = 1, // An unspecified error occurred for >= 1 item. |
44 STORE_INITIALIZATION_FAILED = 2, // AttachmentStore initialization failed. | 45 STORE_INITIALIZATION_FAILED = 2, // AttachmentStore initialization failed. |
45 // When adding a value here, you must increment RESULT_SIZE below. | 46 // When adding a value here, you must increment RESULT_SIZE below. |
46 }; | 47 }; |
47 static const int RESULT_SIZE = | 48 static const int RESULT_SIZE = |
48 10; // Size of the Result enum; used for histograms. | 49 10; // Size of the Result enum; used for histograms. |
49 | 50 |
50 // Each attachment can have references from sync or model type. Tracking these | 51 // 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 // references is needed for lifetime management of attachment, it can only be |
52 // deleted from the store when it doesn't have references. | 53 // deleted from the store when it doesn't have references. |
53 enum AttachmentReferrer { | 54 enum Component { |
54 MODEL_TYPE, | 55 MODEL_TYPE, |
55 SYNC, | 56 SYNC, |
56 }; | 57 }; |
57 | 58 |
58 typedef base::Callback<void(const Result&)> InitCallback; | 59 typedef base::Callback<void(const Result&)> InitCallback; |
59 typedef base::Callback<void(const Result&, | 60 typedef base::Callback<void(const Result&, |
60 scoped_ptr<AttachmentMap>, | 61 scoped_ptr<AttachmentMap>, |
61 scoped_ptr<AttachmentIdList>)> ReadCallback; | 62 scoped_ptr<AttachmentIdList>)> ReadCallback; |
62 typedef base::Callback<void(const Result&)> WriteCallback; | 63 typedef base::Callback<void(const Result&)> WriteCallback; |
63 typedef base::Callback<void(const Result&)> DropCallback; | 64 typedef base::Callback<void(const Result&)> DropCallback; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 | 115 |
115 // Asynchronously reads metadata for all attachments in the store. | 116 // Asynchronously reads metadata for all attachments in the store. |
116 // | 117 // |
117 // |callback| will be invoked when finished. If any of the metadata entries | 118 // |callback| will be invoked when finished. If any of the metadata entries |
118 // could not be read, |callback|'s Result will be UNSPECIFIED_ERROR. | 119 // could not be read, |callback|'s Result will be UNSPECIFIED_ERROR. |
119 void ReadAllMetadata(const ReadMetadataCallback& callback); | 120 void ReadAllMetadata(const ReadMetadataCallback& callback); |
120 | 121 |
121 // Given current AttachmentStore (this) creates separate AttachmentStore that | 122 // Given current AttachmentStore (this) creates separate AttachmentStore that |
122 // will be used by sync components (AttachmentService). Resulting | 123 // will be used by sync components (AttachmentService). Resulting |
123 // AttachmentStore is backed by the same frontend/backend. | 124 // AttachmentStore is backed by the same frontend/backend. |
124 scoped_ptr<AttachmentStore> CreateAttachmentStoreForSync() const; | 125 scoped_ptr<AttachmentStoreForSync> CreateAttachmentStoreForSync() const; |
125 | 126 |
126 // Creates an AttachmentStore backed by in-memory implementation of attachment | 127 // Creates an AttachmentStore backed by in-memory implementation of attachment |
127 // store. For now frontend lives on the same thread as backend. | 128 // store. For now frontend lives on the same thread as backend. |
128 static scoped_ptr<AttachmentStore> CreateInMemoryStore(); | 129 static scoped_ptr<AttachmentStore> CreateInMemoryStore(); |
129 | 130 |
130 // Creates an AttachmentStore backed by on-disk implementation of attachment | 131 // Creates an AttachmentStore backed by on-disk implementation of attachment |
131 // store. Opens corresponding leveldb database located at |path|. All backend | 132 // store. Opens corresponding leveldb database located at |path|. All backend |
132 // operations are scheduled to |backend_task_runner|. Opening attachment store | 133 // operations are scheduled to |backend_task_runner|. Opening attachment store |
133 // is asynchronous, once it finishes |callback| will be called on the thread | 134 // is asynchronous, once it finishes |callback| will be called on the thread |
134 // that called CreateOnDiskStore. Calling Read/Write/Drop before | 135 // that called CreateOnDiskStore. Calling Read/Write/Drop before |
135 // initialization completed is allowed. Later if initialization fails these | 136 // initialization completed is allowed. Later if initialization fails these |
136 // operations will fail with STORE_INITIALIZATION_FAILED error. | 137 // operations will fail with STORE_INITIALIZATION_FAILED error. |
137 static scoped_ptr<AttachmentStore> CreateOnDiskStore( | 138 static scoped_ptr<AttachmentStore> CreateOnDiskStore( |
138 const base::FilePath& path, | 139 const base::FilePath& path, |
139 const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner, | 140 const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner, |
140 const InitCallback& callback); | 141 const InitCallback& callback); |
141 | 142 |
142 // Creates set of AttachmentStore/AttachmentStoreFrontend instances for tests | 143 // Creates set of AttachmentStore/AttachmentStoreFrontend instances for tests |
143 // that provide their own implementation of AttachmentstoreBackend for | 144 // that provide their own implementation of AttachmentstoreBackend for |
144 // mocking. | 145 // mocking. |
145 static scoped_ptr<AttachmentStore> CreateMockStoreForTest( | 146 static scoped_ptr<AttachmentStore> CreateMockStoreForTest( |
146 scoped_ptr<AttachmentStoreBackend> backend); | 147 scoped_ptr<AttachmentStoreBackend> backend); |
147 | 148 |
| 149 protected: |
| 150 AttachmentStore(const scoped_refptr<AttachmentStoreFrontend>& frontend, |
| 151 Component component); |
| 152 |
| 153 const scoped_refptr<AttachmentStoreFrontend>& frontend() { return frontend_; } |
| 154 |
148 private: | 155 private: |
149 AttachmentStore(const scoped_refptr<AttachmentStoreFrontend>& frontend, | |
150 AttachmentReferrer referrer); | |
151 | |
152 scoped_refptr<AttachmentStoreFrontend> frontend_; | 156 scoped_refptr<AttachmentStoreFrontend> frontend_; |
153 // Modification operations with attachment store will be performed on behalf | 157 // Modification operations with attachment store will be performed on behalf |
154 // of |referrer_|. | 158 // of |component_|. |
155 const AttachmentReferrer referrer_; | 159 const Component component_; |
156 | 160 |
157 DISALLOW_COPY_AND_ASSIGN(AttachmentStore); | 161 DISALLOW_COPY_AND_ASSIGN(AttachmentStore); |
158 }; | 162 }; |
159 | 163 |
| 164 // AttachmentStoreForSync extends AttachmentStore and provides additional |
| 165 // functions necessary for AttachmentService. These are needed when |
| 166 // AttachmentService writes attachment on behalf of model type after download |
| 167 // and takes reference on attachment for the duration of upload. |
| 168 // Model type implementation shouldn't use this interface. |
| 169 class AttachmentStoreForSync : public AttachmentStore { |
| 170 public: |
| 171 // Asynchronously adds reference from sync to attachments. |
| 172 void SetSyncReference(const AttachmentIdList& ids); |
| 173 |
| 174 // Asynchronously drops sync reference from attachments. |
| 175 void DropSyncReference(const AttachmentIdList& ids); |
| 176 |
| 177 private: |
| 178 friend class AttachmentStore; |
| 179 AttachmentStoreForSync(const scoped_refptr<AttachmentStoreFrontend>& frontend, |
| 180 Component consumer_component, |
| 181 Component sync_component); |
| 182 |
| 183 // |sync_component_| is passed to frontend when sync related operations are |
| 184 // perfromed. |
| 185 const Component sync_component_; |
| 186 |
| 187 DISALLOW_COPY_AND_ASSIGN(AttachmentStoreForSync); |
| 188 }; |
| 189 |
160 } // namespace syncer | 190 } // namespace syncer |
161 | 191 |
162 #endif // SYNC_API_ATTACHMENTS_ATTACHMENT_STORE_H_ | 192 #endif // SYNC_API_ATTACHMENTS_ATTACHMENT_STORE_H_ |
OLD | NEW |