Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / sync / internal_api / public / attachments / attachment_service_impl.h
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/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"
20
21 namespace syncer {
22
23 // Implementation of AttachmentService.
24 class SYNC_EXPORT AttachmentServiceImpl
25     : public AttachmentService,
26       public net::NetworkChangeNotifier::NetworkChangeObserver,
27       public base::NonThreadSafe {
28  public:
29   // |attachment_uploader| is optional. If null, attachments will never be
30   // uploaded to the sync server and |delegate|'s OnAttachmentUploaded will
31   // never be invoked.
32   //
33   // |attachment_downloader| is optional. If null, attachments will never be
34   // downloaded. Only attachments in |attachment_store| will be returned from
35   // GetOrDownloadAttachments.
36   //
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.
41   //
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.
47   //
48   // |max_backoff_delay| the maxmium delay between upload attempts when backed
49   // off.
50   AttachmentServiceImpl(scoped_refptr<AttachmentStore> attachment_store,
51                         scoped_ptr<AttachmentUploader> attachment_uploader,
52                         scoped_ptr<AttachmentDownloader> attachment_downloader,
53                         Delegate* delegate,
54                         const base::TimeDelta& initial_backoff_delay,
55                         const base::TimeDelta& max_backoff_delay);
56   ~AttachmentServiceImpl() override;
57
58   // Create an AttachmentServiceImpl suitable for use in tests.
59   static scoped_ptr<syncer::AttachmentService> CreateForTest();
60
61   // AttachmentService implementation.
62   AttachmentStore* GetStore() override;
63   void GetOrDownloadAttachments(const AttachmentIdList& attachment_ids,
64                                 const GetOrDownloadCallback& callback) override;
65   void DropAttachments(const AttachmentIdList& attachment_ids,
66                        const DropCallback& callback) override;
67   void UploadAttachments(const AttachmentIdSet& attachment_ids) override;
68
69   // NetworkChangeObserver implementation.
70   void OnNetworkChanged(
71       net::NetworkChangeNotifier::ConnectionType type) override;
72
73   // Use |timer| in the underlying TaskQueue.
74   //
75   // Used in tests.  See also MockTimer.
76   void SetTimerForTest(scoped_ptr<base::Timer> timer);
77
78  private:
79   class GetOrDownloadState;
80
81   void ReadDone(const scoped_refptr<GetOrDownloadState>& state,
82                 const AttachmentStore::Result& result,
83                 scoped_ptr<AttachmentMap> attachments,
84                 scoped_ptr<AttachmentIdList> unavailable_attachment_ids);
85   void DropDone(const DropCallback& callback,
86                 const AttachmentStore::Result& result);
87   void UploadDone(const AttachmentUploader::UploadResult& result,
88                   const AttachmentId& attachment_id);
89   void DownloadDone(const scoped_refptr<GetOrDownloadState>& state,
90                     const AttachmentId& attachment_id,
91                     const AttachmentDownloader::DownloadResult& result,
92                     scoped_ptr<Attachment> attachment);
93   void BeginUpload(const AttachmentId& attachment_id);
94   void ReadDoneNowUpload(
95       const AttachmentStore::Result& result,
96       scoped_ptr<AttachmentMap> attachments,
97       scoped_ptr<AttachmentIdList> unavailable_attachment_ids);
98
99   scoped_refptr<AttachmentStore> attachment_store_;
100
101   // May be null.
102   const scoped_ptr<AttachmentUploader> attachment_uploader_;
103
104   // May be null.
105   const scoped_ptr<AttachmentDownloader> attachment_downloader_;
106
107   // May be null.
108   Delegate* delegate_;
109
110   scoped_ptr<TaskQueue<AttachmentId> > upload_task_queue_;
111
112   // Must be last data member.
113   base::WeakPtrFactory<AttachmentServiceImpl> weak_ptr_factory_;
114
115   DISALLOW_COPY_AND_ASSIGN(AttachmentServiceImpl);
116 };
117
118 }  // namespace syncer
119
120 #endif  // SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_SERVICE_IMPL_H_