Support the Service-Worker-Allowed header
[chromium-blink-merge.git] / sync / api / attachments / attachment_store.h
blobb2897231d09aaa25cd78993fef239e7760b8a9cf
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/api/attachments/attachment_metadata.h"
14 #include "sync/base/sync_export.h"
16 namespace base {
17 class FilePath;
18 class RefCountedMemory;
19 class SequencedTaskRunner;
20 } // namespace base
22 namespace syncer {
24 class Attachment;
25 class AttachmentId;
27 // A place to locally store and access Attachments.
29 // Destroying this object does not necessarily cancel outstanding async
30 // operations. If you need cancel like semantics, use WeakPtr in the callbacks.
31 class SYNC_EXPORT AttachmentStoreBase {
32 public:
33 // TODO(maniscalco): Consider udpating Read and Write methods to support
34 // resumable transfers (bug 353292).
36 // The result status of an attachment store operation.
37 // Do not re-order or delete these entries; they are used in a UMA histogram.
38 enum Result {
39 SUCCESS = 0, // No error, all completed successfully.
40 UNSPECIFIED_ERROR = 1, // An unspecified error occurred for >= 1 item.
41 STORE_INITIALIZATION_FAILED = 2, // AttachmentStore initialization failed.
42 // When adding a value here, you must increment RESULT_SIZE below.
44 const int RESULT_SIZE = 10; // Size of the Result enum; used for histograms.
46 typedef base::Callback<void(const Result&)> InitCallback;
47 typedef base::Callback<void(const Result&,
48 scoped_ptr<AttachmentMap>,
49 scoped_ptr<AttachmentIdList>)> ReadCallback;
50 typedef base::Callback<void(const Result&)> WriteCallback;
51 typedef base::Callback<void(const Result&)> DropCallback;
52 typedef base::Callback<void(const Result&,
53 scoped_ptr<AttachmentMetadataList>)>
54 ReadMetadataCallback;
56 AttachmentStoreBase();
57 virtual ~AttachmentStoreBase();
59 // Asynchronously initializes attachment store.
61 // This method should not be called by consumer of this interface. It is
62 // called by factory methods in AttachmentStore class. When initialization is
63 // complete |callback| is invoked with result, in case of failure result is
64 // UNSPECIFIED_ERROR.
65 virtual void Init(const InitCallback& callback) = 0;
67 // Asynchronously reads the attachments identified by |ids|.
69 // |callback| will be invoked when finished. AttachmentStore will attempt to
70 // read all attachments specified in ids. If any of the attachments do not
71 // exist or could not be read, |callback|'s Result will be UNSPECIFIED_ERROR.
72 // Callback's AttachmentMap will contain all attachments that were
73 // successfully read, AttachmentIdList will contain attachment ids of
74 // attachments that are unavailable in attachment store, these need to be
75 // downloaded from server.
77 // Reads on individual attachments are treated atomically; |callback| will not
78 // read only part of an attachment.
79 virtual void Read(const AttachmentIdList& ids,
80 const ReadCallback& callback) = 0;
82 // Asynchronously writes |attachments| to the store.
84 // Will not overwrite stored attachments. Attempting to overwrite an
85 // attachment that already exists is not an error.
87 // |callback| will be invoked when finished. If any of the attachments could
88 // not be written |callback|'s Result will be UNSPECIFIED_ERROR. When this
89 // happens, some or none of the attachments may have been written
90 // successfully.
91 virtual void Write(const AttachmentList& attachments,
92 const WriteCallback& callback) = 0;
94 // Asynchronously drops |attchments| from this store.
96 // This does not remove attachments from the server.
98 // |callback| will be invoked when finished. Attempting to drop an attachment
99 // that does not exist is not an error. If any of the existing attachment
100 // could not be dropped, |callback|'s Result will be UNSPECIFIED_ERROR. When
101 // this happens, some or none of the attachments may have been dropped
102 // successfully.
103 virtual void Drop(const AttachmentIdList& ids,
104 const DropCallback& callback) = 0;
106 // Asynchronously reads metadata for the attachments identified by |ids|.
108 // |callback| will be invoked when finished. AttachmentStore will attempt to
109 // 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
111 // will be UNSPECIFIED_ERROR.
112 virtual void ReadMetadata(const AttachmentIdList& ids,
113 const ReadMetadataCallback& callback) = 0;
115 // Asynchronously reads metadata for all attachments in the store.
117 // |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 virtual void ReadAllMetadata(const ReadMetadataCallback& callback) = 0;
122 // AttachmentStore is an interface exposed to data type and AttachmentService
123 // code. Also contains factory methods for default implementations.
124 class SYNC_EXPORT AttachmentStore
125 : public AttachmentStoreBase,
126 public base::RefCountedThreadSafe<AttachmentStore> {
127 public:
128 AttachmentStore();
130 // Creates an AttachmentStoreHandle backed by in-memory implementation of
131 // attachment store. For now frontend lives on the same thread as backend.
132 static scoped_refptr<AttachmentStore> CreateInMemoryStore();
134 // Creates an AttachmentStoreHandle backed by on-disk implementation of
135 // attachment store. Opens corresponding leveldb database located at |path|.
136 // All backend operations are scheduled to |backend_task_runner|. Opening
137 // attachment store is asynchronous, once it finishes |callback| will be
138 // called on the thread that called CreateOnDiskStore. Calling Read/Write/Drop
139 // before initialization completed is allowed. Later if initialization fails
140 // these operations will fail with STORE_INITIALIZATION_FAILED error.
141 static scoped_refptr<AttachmentStore> CreateOnDiskStore(
142 const base::FilePath& path,
143 const scoped_refptr<base::SequencedTaskRunner>& backend_task_runner,
144 const InitCallback& callback);
146 protected:
147 friend class base::RefCountedThreadSafe<AttachmentStore>;
148 ~AttachmentStore() override;
151 } // namespace syncer
153 #endif // SYNC_API_ATTACHMENTS_ATTACHMENT_STORE_H_