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