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