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_SERVICE_H_
      6 #define SYNC_API_ATTACHMENTS_ATTACHMENT_SERVICE_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/callback.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/memory/weak_ptr.h"
     12 #include "sync/api/attachments/attachment.h"
     13 #include "sync/base/sync_export.h"
     14 
     15 namespace syncer {
     16 
     17 class SyncData;
     18 
     19 // AttachmentService is responsible for managing a model type's attachments.
     20 //
     21 // Outside of sync code, AttachmentService shouldn't be used directly. Instead
     22 // use the functionality provided by SyncData and SyncChangeProcessor.
     23 //
     24 // Destroying this object does not necessarily cancel outstanding async
     25 // operations. If you need cancel like semantics, use WeakPtr in the callbacks.
     26 class SYNC_EXPORT AttachmentService {
     27  public:
     28   // The result of a GetOrDownloadAttachments operation.
     29   enum GetOrDownloadResult {
     30     GET_SUCCESS,            // No error, all attachments returned.
     31     GET_UNSPECIFIED_ERROR,  // An unspecified error occurred.
     32   };
     33 
     34   typedef base::Callback<
     35       void(const GetOrDownloadResult&, scoped_ptr<AttachmentMap> attachments)>
     36       GetOrDownloadCallback;
     37 
     38   // The result of a DropAttachments operation.
     39   enum DropResult {
     40     DROP_SUCCESS,            // No error, all attachments dropped.
     41     DROP_UNSPECIFIED_ERROR,  // An unspecified error occurred. Some or all
     42                              // attachments may not have been dropped.
     43   };
     44 
     45   typedef base::Callback<void(const DropResult&)> DropCallback;
     46 
     47   // The result of a StoreAttachments operation.
     48   enum StoreResult {
     49     STORE_SUCCESS,            // No error, all attachments stored (at least
     50                               // locally).
     51     STORE_UNSPECIFIED_ERROR,  // An unspecified error occurred. Some or all
     52                               // attachments may not have been stored.
     53   };
     54 
     55   typedef base::Callback<void(const StoreResult&)> StoreCallback;
     56 
     57   // An interface that embedder code implements to be notified about different
     58   // events that originate from AttachmentService.
     59   // This interface will be called from the same thread AttachmentService was
     60   // created and called.
     61   class Delegate {
     62    public:
     63     virtual ~Delegate() {}
     64 
     65     // Attachment is uploaded to server and attachment_id is updated with server
     66     // url.
     67     virtual void OnAttachmentUploaded(const AttachmentId& attachment_id) = 0;
     68   };
     69 
     70   AttachmentService();
     71   virtual ~AttachmentService();
     72 
     73   // See SyncData::GetOrDownloadAttachments.
     74   virtual void GetOrDownloadAttachments(
     75       const AttachmentIdList& attachment_ids,
     76       const GetOrDownloadCallback& callback) = 0;
     77 
     78   // See SyncData::DropAttachments.
     79   virtual void DropAttachments(const AttachmentIdList& attachment_ids,
     80                                const DropCallback& callback) = 0;
     81 
     82   // Store |attachments| on device and (eventually) upload them to the server.
     83   //
     84   // Invokes |callback| once the attachments have been written to device
     85   // storage.
     86   virtual void StoreAttachments(const AttachmentList& attachments,
     87                                 const StoreCallback& callback) = 0;
     88 
     89   // This method should be called when a SyncData is about to be deleted from
     90   // the sync database so we can remove any unreferenced attachments from local
     91   // storage.
     92   virtual void OnSyncDataDelete(const SyncData& sync_data) = 0;
     93 
     94   // This method should be called when a SyncData is about to be updated so we
     95   // can remove unreferenced attachments from local storage and ensure new
     96   // attachments are persisted and uploaded to the sync server.
     97   virtual void OnSyncDataUpdate(const AttachmentIdList& old_attachment_ids,
     98                                 const SyncData& updated_sync_data) = 0;
     99 };
    100 
    101 }  // namespace syncer
    102 
    103 #endif  // SYNC_API_ATTACHMENTS_ATTACHMENT_SERVICE_H_
    104