Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: sync/api/attachments/attachment_store.h

Issue 986743004: [Sync] Refactor AttachmentStore classes. Introduce concept of referrer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sync/BUILD.gn ('k') | sync/api/attachments/attachment_store.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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;
19 class SequencedTaskRunner; 18 class SequencedTaskRunner;
20 } // namespace base 19 } // namespace base
21 20
22 namespace syncer { 21 namespace syncer {
23 22
24 class Attachment; 23 class AttachmentStoreFrontend;
25 class AttachmentId; 24 class AttachmentStoreBackend;
26 25
27 // AttachmentStore is a place to locally store and access Attachments. 26 // AttachmentStore is a place to locally store and access Attachments.
28 // 27 //
29 // AttachmentStore class is an interface exposed to data type and 28 // AttachmentStore class is an interface exposed to data type and
30 // AttachmentService code. 29 // AttachmentService code.
31 // It also contains factory methods for default attachment store 30 // It also contains factory methods for default attachment store
32 // implementations. 31 // implementations.
33 // Destroying this object does not necessarily cancel outstanding async 32 // Destroying this object does not necessarily cancel outstanding async
34 // operations. If you need cancel like semantics, use WeakPtr in the callbacks. 33 // operations. If you need cancel like semantics, use WeakPtr in the callbacks.
35 class SYNC_EXPORT AttachmentStore 34 class SYNC_EXPORT AttachmentStore {
36 : public base::RefCountedThreadSafe<AttachmentStore> {
37 public: 35 public:
38 // TODO(maniscalco): Consider udpating Read and Write methods to support 36 // TODO(maniscalco): Consider udpating Read and Write methods to support
39 // resumable transfers (bug 353292). 37 // resumable transfers (bug 353292).
40 38
41 // The result status of an attachment store operation. 39 // The result status of an attachment store operation.
42 // Do not re-order or delete these entries; they are used in a UMA histogram. 40 // Do not re-order or delete these entries; they are used in a UMA histogram.
43 enum Result { 41 enum Result {
44 SUCCESS = 0, // No error, all completed successfully. 42 SUCCESS = 0, // No error, all completed successfully.
45 UNSPECIFIED_ERROR = 1, // An unspecified error occurred for >= 1 item. 43 UNSPECIFIED_ERROR = 1, // An unspecified error occurred for >= 1 item.
46 STORE_INITIALIZATION_FAILED = 2, // AttachmentStore initialization failed. 44 STORE_INITIALIZATION_FAILED = 2, // AttachmentStore initialization failed.
47 // When adding a value here, you must increment RESULT_SIZE below. 45 // When adding a value here, you must increment RESULT_SIZE below.
48 }; 46 };
49 static const int RESULT_SIZE = 47 static const int RESULT_SIZE =
50 10; // Size of the Result enum; used for histograms. 48 10; // Size of the Result enum; used for histograms.
51 49
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
52 typedef base::Callback<void(const Result&)> InitCallback; 58 typedef base::Callback<void(const Result&)> InitCallback;
53 typedef base::Callback<void(const Result&, 59 typedef base::Callback<void(const Result&,
54 scoped_ptr<AttachmentMap>, 60 scoped_ptr<AttachmentMap>,
55 scoped_ptr<AttachmentIdList>)> ReadCallback; 61 scoped_ptr<AttachmentIdList>)> ReadCallback;
56 typedef base::Callback<void(const Result&)> WriteCallback; 62 typedef base::Callback<void(const Result&)> WriteCallback;
57 typedef base::Callback<void(const Result&)> DropCallback; 63 typedef base::Callback<void(const Result&)> DropCallback;
58 typedef base::Callback<void(const Result&, 64 typedef base::Callback<void(const Result&,
59 scoped_ptr<AttachmentMetadataList>)> 65 scoped_ptr<AttachmentMetadataList>)>
60 ReadMetadataCallback; 66 ReadMetadataCallback;
61 67
62 AttachmentStore(); 68 ~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;
71 69
72 // Asynchronously reads the attachments identified by |ids|. 70 // Asynchronously reads the attachments identified by |ids|.
73 // 71 //
74 // |callback| will be invoked when finished. AttachmentStore will attempt to 72 // |callback| will be invoked when finished. AttachmentStore will attempt to
75 // read all attachments specified in ids. If any of the attachments do not 73 // read all attachments specified in ids. If any of the attachments do not
76 // exist or could not be read, |callback|'s Result will be UNSPECIFIED_ERROR. 74 // exist or could not be read, |callback|'s Result will be UNSPECIFIED_ERROR.
77 // Callback's AttachmentMap will contain all attachments that were 75 // Callback's AttachmentMap will contain all attachments that were
78 // successfully read, AttachmentIdList will contain attachment ids of 76 // successfully read, AttachmentIdList will contain attachment ids of
79 // attachments that are unavailable in attachment store, these need to be 77 // attachments that are unavailable in attachment store, these need to be
80 // downloaded from server. 78 // downloaded from server.
81 // 79 //
82 // Reads on individual attachments are treated atomically; |callback| will not 80 // Reads on individual attachments are treated atomically; |callback| will not
83 // read only part of an attachment. 81 // read only part of an attachment.
84 virtual void Read(const AttachmentIdList& ids, 82 void Read(const AttachmentIdList& ids, const ReadCallback& callback);
85 const ReadCallback& callback) = 0;
86 83
87 // Asynchronously writes |attachments| to the store. 84 // Asynchronously writes |attachments| to the store.
88 // 85 //
89 // Will not overwrite stored attachments. Attempting to overwrite an 86 // Will not overwrite stored attachments. Attempting to overwrite an
90 // attachment that already exists is not an error. 87 // attachment that already exists is not an error.
91 // 88 //
92 // |callback| will be invoked when finished. If any of the attachments could 89 // |callback| will be invoked when finished. If any of the attachments could
93 // not be written |callback|'s Result will be UNSPECIFIED_ERROR. When this 90 // not be written |callback|'s Result will be UNSPECIFIED_ERROR. When this
94 // happens, some or none of the attachments may have been written 91 // happens, some or none of the attachments may have been written
95 // successfully. 92 // successfully.
96 virtual void Write(const AttachmentList& attachments, 93 void Write(const AttachmentList& attachments, const WriteCallback& callback);
97 const WriteCallback& callback) = 0;
98 94
99 // Asynchronously drops |attchments| from this store. 95 // Asynchronously drops |attchments| from this store.
100 // 96 //
101 // This does not remove attachments from the server. 97 // This does not remove attachments from the server.
102 // 98 //
103 // |callback| will be invoked when finished. Attempting to drop an attachment 99 // |callback| will be invoked when finished. Attempting to drop an attachment
104 // that does not exist is not an error. If any of the existing attachment 100 // that does not exist is not an error. If any of the existing attachment
105 // could not be dropped, |callback|'s Result will be UNSPECIFIED_ERROR. When 101 // could not be dropped, |callback|'s Result will be UNSPECIFIED_ERROR. When
106 // this happens, some or none of the attachments may have been dropped 102 // this happens, some or none of the attachments may have been dropped
107 // successfully. 103 // successfully.
108 virtual void Drop(const AttachmentIdList& ids, 104 void Drop(const AttachmentIdList& ids, const DropCallback& callback);
109 const DropCallback& callback) = 0;
110 105
111 // Asynchronously reads metadata for the attachments identified by |ids|. 106 // Asynchronously reads metadata for the attachments identified by |ids|.
112 // 107 //
113 // |callback| will be invoked when finished. AttachmentStore will attempt to 108 // |callback| will be invoked when finished. AttachmentStore will attempt to
114 // read metadata for all attachments specified in ids. If any of the 109 // read metadata for all attachments specified in ids. If any of the
115 // metadata entries do not exist or could not be read, |callback|'s Result 110 // metadata entries do not exist or could not be read, |callback|'s Result
116 // will be UNSPECIFIED_ERROR. 111 // will be UNSPECIFIED_ERROR.
117 virtual void ReadMetadata(const AttachmentIdList& ids, 112 void ReadMetadata(const AttachmentIdList& ids,
118 const ReadMetadataCallback& callback) = 0; 113 const ReadMetadataCallback& callback);
119 114
120 // Asynchronously reads metadata for all attachments in the store. 115 // Asynchronously reads metadata for all attachments in the store.
121 // 116 //
122 // |callback| will be invoked when finished. If any of the metadata entries 117 // |callback| will be invoked when finished. If any of the metadata entries
123 // could not be read, |callback|'s Result will be UNSPECIFIED_ERROR. 118 // could not be read, |callback|'s Result will be UNSPECIFIED_ERROR.
124 virtual void ReadAllMetadata(const ReadMetadataCallback& callback) = 0; 119 void ReadAllMetadata(const ReadMetadataCallback& callback);
125 120
126 // Creates an AttachmentStoreHandle backed by in-memory implementation of 121 // Given current AttachmentStore (this) creates separate AttachmentStore that
127 // attachment store. For now frontend lives on the same thread as backend. 122 // will be used by sync components (AttachmentService). Resulting
128 static scoped_refptr<AttachmentStore> CreateInMemoryStore(); 123 // AttachmentStore is backed by the same frontend/backend.
124 scoped_ptr<AttachmentStore> CreateAttachmentStoreForSync() const;
129 125
130 // Creates an AttachmentStoreHandle backed by on-disk implementation of 126 // Creates an AttachmentStore backed by in-memory implementation of attachment
131 // attachment store. Opens corresponding leveldb database located at |path|. 127 // store. For now frontend lives on the same thread as backend.
132 // All backend operations are scheduled to |backend_task_runner|. Opening 128 static scoped_ptr<AttachmentStore> CreateInMemoryStore();
133 // attachment store is asynchronous, once it finishes |callback| will be 129
134 // called on the thread that called CreateOnDiskStore. Calling Read/Write/Drop 130 // Creates an AttachmentStore backed by on-disk implementation of attachment
135 // before initialization completed is allowed. Later if initialization fails 131 // store. Opens corresponding leveldb database located at |path|. All backend
136 // these operations will fail with STORE_INITIALIZATION_FAILED error. 132 // operations are scheduled to |backend_task_runner|. Opening attachment store
137 static scoped_refptr<AttachmentStore> CreateOnDiskStore( 133 // is asynchronous, once it finishes |callback| will be called on the thread
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 protected: 142 // Creates set of AttachmentStore/AttachmentStoreFrontend instances for tests
143 friend class base::RefCountedThreadSafe<AttachmentStore>; 143 // that provide their own implementation of AttachmentstoreBackend for
144 virtual ~AttachmentStore(); 144 // mocking.
145 }; 145 static scoped_ptr<AttachmentStore> CreateMockStoreForTest(
146 146 scoped_ptr<AttachmentStoreBackend> backend);
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);
178 147
179 private: 148 private:
180 scoped_refptr<base::SequencedTaskRunner> callback_task_runner_; 149 AttachmentStore(const scoped_refptr<AttachmentStoreFrontend>& frontend,
150 AttachmentReferrer referrer);
181 151
182 DISALLOW_COPY_AND_ASSIGN(AttachmentStoreBackend); 152 scoped_refptr<AttachmentStoreFrontend> frontend_;
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);
183 }; 158 };
184 159
185 } // namespace syncer 160 } // namespace syncer
186 161
187 #endif // SYNC_API_ATTACHMENTS_ATTACHMENT_STORE_H_ 162 #endif // SYNC_API_ATTACHMENTS_ATTACHMENT_STORE_H_
OLDNEW
« no previous file with comments | « sync/BUILD.gn ('k') | sync/api/attachments/attachment_store.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698