Home | History | Annotate | Download | only in attachments
      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.
      4 
      5 #ifndef SYNC_API_ATTACHMENTS_ATTACHMENT_STORE_H_
      6 #define SYNC_API_ATTACHMENTS_ATTACHMENT_STORE_H_
      7 
      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"
     14 
     15 namespace base {
     16 class RefCountedMemory;
     17 }  // namespace base
     18 
     19 namespace syncer {
     20 
     21 class Attachment;
     22 class AttachmentId;
     23 
     24 // A place to locally store and access Attachments.
     25 //
     26 // Destroying this object does not necessarily cancel outstanding async
     27 // operations. If you need cancel like semantics, use WeakPtr in the callbacks.
     28 class SYNC_EXPORT AttachmentStore {
     29  public:
     30   AttachmentStore();
     31   virtual ~AttachmentStore();
     32 
     33   // TODO(maniscalco): Consider udpating Read and Write methods to support
     34   // resumable transfers (bug 353292).
     35 
     36   enum Result {
     37     SUCCESS,            // No error, all completed successfully.
     38     UNSPECIFIED_ERROR,  // An unspecified error occurred for one or more items.
     39   };
     40 
     41   typedef base::Callback<void(const Result&,
     42                               scoped_ptr<AttachmentMap>,
     43                               scoped_ptr<AttachmentIdList>)> ReadCallback;
     44   typedef base::Callback<void(const Result&)> WriteCallback;
     45   typedef base::Callback<void(const Result&)> DropCallback;
     46 
     47   // Asynchronously reads the attachments identified by |ids|.
     48   //
     49   // |callback| will be invoked when finished. AttachmentStore will attempt to
     50   // read all attachments specified in ids. If any of the attachments do not
     51   // exist or could not be read, |callback|'s Result will be UNSPECIFIED_ERROR.
     52   // Callback's AttachmentMap will contain all attachments that were
     53   // successfully read, AttachmentIdList will contain attachment ids of
     54   // attachments that are unavailable in attachment store, these need to be
     55   // downloaded from server.
     56   //
     57   // Reads on individual attachments are treated atomically; |callback| will not
     58   // read only part of an attachment.
     59   virtual void Read(const AttachmentIdList& ids,
     60                     const ReadCallback& callback) = 0;
     61 
     62   // Asynchronously writes |attachments| to the store.
     63   //
     64   // Will not overwrite stored attachments. Attempting to overwrite an
     65   // attachment that already exists is not an error.
     66   //
     67   // |callback| will be invoked when finished. If any of the attachments could
     68   // not be written |callback|'s Result will be UNSPECIFIED_ERROR. When this
     69   // happens, some or none of the attachments may have been written
     70   // successfully.
     71   virtual void Write(const AttachmentList& attachments,
     72                      const WriteCallback& callback) = 0;
     73 
     74   // Asynchronously drops |attchments| from this store.
     75   //
     76   // This does not remove attachments from the server.
     77   //
     78   // |callback| will be invoked when finished. Attempting to drop an attachment
     79   // that does not exist is not an error. If any of the existing attachment
     80   // could not be dropped, |callback|'s Result will be UNSPECIFIED_ERROR. When
     81   // this happens, some or none of the attachments may have been dropped
     82   // successfully.
     83   virtual void Drop(const AttachmentIdList& ids,
     84                     const DropCallback& callback) = 0;
     85 };
     86 
     87 }  // namespace syncer
     88 
     89 #endif  // SYNC_API_ATTACHMENTS_ATTACHMENT_STORE_H_
     90