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_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_SERVICE_IMPL_H_
6 #define SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_SERVICE_IMPL_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/threading/non_thread_safe.h"
13 #include "net/base/network_change_notifier.h"
14 #include "sync/api/attachments/attachment_store.h"
15 #include "sync/internal_api/public/attachments/attachment_downloader.h"
16 #include "sync/internal_api/public/attachments/attachment_service.h"
17 #include "sync/internal_api/public/attachments/attachment_service_proxy.h"
18 #include "sync/internal_api/public/attachments/attachment_uploader.h"
19 #include "sync/internal_api/public/attachments/task_queue.h"
23 // Implementation of AttachmentService.
24 class SYNC_EXPORT AttachmentServiceImpl
25 : public AttachmentService,
26 public net::NetworkChangeNotifier::NetworkChangeObserver,
27 public base::NonThreadSafe {
29 // |attachment_uploader| is optional. If null, attachments will never be
30 // uploaded to the sync server and |delegate|'s OnAttachmentUploaded will
33 // |attachment_downloader| is optional. If null, attachments will never be
34 // downloaded. Only attachments in |attachment_store| will be returned from
35 // GetOrDownloadAttachments.
37 // |delegate| is optional delegate for AttachmentService to notify about
38 // asynchronous events (AttachmentUploaded). Pass NULL if delegate is not
39 // provided. AttachmentService doesn't take ownership of delegate, the pointer
40 // must be valid throughout AttachmentService lifetime.
42 // |initial_backoff_delay| the initial delay between upload attempts. This
43 // class automatically retries failed uploads. After the first failure, it
44 // will wait this amount of time until it tries again. After each failure,
45 // the delay is doubled until the |max_backoff_delay| is reached. A
46 // successful upload clears the delay.
48 // |max_backoff_delay| the maxmium delay between upload attempts when backed
50 AttachmentServiceImpl(scoped_refptr<AttachmentStore> attachment_store,
51 scoped_ptr<AttachmentUploader> attachment_uploader,
52 scoped_ptr<AttachmentDownloader> attachment_downloader,
54 const base::TimeDelta& initial_backoff_delay,
55 const base::TimeDelta& max_backoff_delay);
56 virtual ~AttachmentServiceImpl();
58 // Create an AttachmentServiceImpl suitable for use in tests.
59 static scoped_ptr<syncer::AttachmentService> CreateForTest();
61 // AttachmentService implementation.
62 virtual AttachmentStore* GetStore() OVERRIDE;
63 virtual void GetOrDownloadAttachments(
64 const AttachmentIdList& attachment_ids,
65 const GetOrDownloadCallback& callback) OVERRIDE;
66 virtual void DropAttachments(const AttachmentIdList& attachment_ids,
67 const DropCallback& callback) OVERRIDE;
68 virtual void UploadAttachments(
69 const AttachmentIdSet& attachment_ids) OVERRIDE;
71 // NetworkChangeObserver implementation.
72 virtual void OnNetworkChanged(
73 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
75 // Use |timer| in the underlying TaskQueue.
77 // Used in tests. See also MockTimer.
78 void SetTimerForTest(scoped_ptr<base::Timer> timer);
81 class GetOrDownloadState;
83 void ReadDone(const scoped_refptr<GetOrDownloadState>& state,
84 const AttachmentStore::Result& result,
85 scoped_ptr<AttachmentMap> attachments,
86 scoped_ptr<AttachmentIdList> unavailable_attachment_ids);
87 void DropDone(const DropCallback& callback,
88 const AttachmentStore::Result& result);
89 void UploadDone(const AttachmentUploader::UploadResult& result,
90 const AttachmentId& attachment_id);
91 void DownloadDone(const scoped_refptr<GetOrDownloadState>& state,
92 const AttachmentId& attachment_id,
93 const AttachmentDownloader::DownloadResult& result,
94 scoped_ptr<Attachment> attachment);
95 void BeginUpload(const AttachmentId& attachment_id);
96 void ReadDoneNowUpload(
97 const AttachmentStore::Result& result,
98 scoped_ptr<AttachmentMap> attachments,
99 scoped_ptr<AttachmentIdList> unavailable_attachment_ids);
101 scoped_refptr<AttachmentStore> attachment_store_;
104 const scoped_ptr<AttachmentUploader> attachment_uploader_;
107 const scoped_ptr<AttachmentDownloader> attachment_downloader_;
112 scoped_ptr<TaskQueue<AttachmentId> > upload_task_queue_;
114 // Must be last data member.
115 base::WeakPtrFactory<AttachmentServiceImpl> weak_ptr_factory_;
117 DISALLOW_COPY_AND_ASSIGN(AttachmentServiceImpl);
120 } // namespace syncer
122 #endif // SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_SERVICE_IMPL_H_