File: attachment_service_impl.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (121 lines) | stat: -rw-r--r-- 5,040 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_SYNC_MODEL_IMPL_ATTACHMENTS_ATTACHMENT_SERVICE_IMPL_H_
#define COMPONENTS_SYNC_MODEL_IMPL_ATTACHMENTS_ATTACHMENT_SERVICE_IMPL_H_

#include <deque>
#include <memory>

#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/non_thread_safe.h"
#include "components/sync/engine/attachments/attachment_downloader.h"
#include "components/sync/engine/attachments/attachment_uploader.h"
#include "components/sync/model/attachments/attachment_service.h"
#include "components/sync/model/attachments/attachment_service_proxy.h"
#include "components/sync/model/attachments/attachment_store.h"
#include "components/sync/model_impl/attachments/task_queue.h"
#include "net/base/network_change_notifier.h"

namespace syncer {

// Implementation of AttachmentService.
class AttachmentServiceImpl
    : public AttachmentService,
      public net::NetworkChangeNotifier::NetworkChangeObserver,
      public base::NonThreadSafe {
 public:
  // |attachment_store| is required. UploadAttachments reads attachment data
  // from it. Downloaded attachments will be written into it.
  //
  // |attachment_uploader| is optional. If null, attachments will never be
  // uploaded to the sync server and |delegate|'s OnAttachmentUploaded will
  // never be invoked.
  //
  // |attachment_downloader| is optional. If null, attachments will never be
  // downloaded. Only attachments in |attachment_store| will be returned from
  // GetOrDownloadAttachments.
  //
  // |delegate| is optional delegate for AttachmentService to notify about
  // asynchronous events (AttachmentUploaded). Pass null if delegate is not
  // provided. AttachmentService doesn't take ownership of delegate, the pointer
  // must be valid throughout AttachmentService lifetime.
  //
  // |initial_backoff_delay| the initial delay between upload attempts.  This
  // class automatically retries failed uploads.  After the first failure, it
  // will wait this amount of time until it tries again.  After each failure,
  // the delay is doubled until the |max_backoff_delay| is reached.  A
  // successful upload clears the delay.
  //
  // |max_backoff_delay| the maxmium delay between upload attempts when backed
  // off.
  AttachmentServiceImpl(
      std::unique_ptr<AttachmentStoreForSync> attachment_store,
      std::unique_ptr<AttachmentUploader> attachment_uploader,
      std::unique_ptr<AttachmentDownloader> attachment_downloader,
      Delegate* delegate,
      const base::TimeDelta& initial_backoff_delay,
      const base::TimeDelta& max_backoff_delay);
  ~AttachmentServiceImpl() override;

  // AttachmentService implementation.
  void GetOrDownloadAttachments(const AttachmentIdList& attachment_ids,
                                const GetOrDownloadCallback& callback) override;
  void UploadAttachments(const AttachmentIdList& attachment_ids) override;

  // NetworkChangeObserver implementation.
  void OnNetworkChanged(
      net::NetworkChangeNotifier::ConnectionType type) override;

  // Use |timer| in the underlying TaskQueue.
  //
  // Used in tests.  See also MockTimer.
  void SetTimerForTest(std::unique_ptr<base::Timer> timer);

 private:
  class GetOrDownloadState;

  void ReadDone(const scoped_refptr<GetOrDownloadState>& state,
                const AttachmentStore::Result& result,
                std::unique_ptr<AttachmentMap> attachments,
                std::unique_ptr<AttachmentIdList> unavailable_attachment_ids);
  void WriteDone(const scoped_refptr<GetOrDownloadState>& state,
                 const Attachment& attachment,
                 const AttachmentStore::Result& result);
  void UploadDone(const AttachmentUploader::UploadResult& result,
                  const AttachmentId& attachment_id);
  void DownloadDone(const scoped_refptr<GetOrDownloadState>& state,
                    const AttachmentId& attachment_id,
                    const AttachmentDownloader::DownloadResult& result,
                    std::unique_ptr<Attachment> attachment);
  void BeginUpload(const AttachmentId& attachment_id);
  void ReadDoneNowUpload(
      const AttachmentStore::Result& result,
      std::unique_ptr<AttachmentMap> attachments,
      std::unique_ptr<AttachmentIdList> unavailable_attachment_ids);

  std::unique_ptr<AttachmentStoreForSync> attachment_store_;

  // May be null.
  const std::unique_ptr<AttachmentUploader> attachment_uploader_;

  // May be null.
  const std::unique_ptr<AttachmentDownloader> attachment_downloader_;

  // May be null.
  Delegate* delegate_;

  std::unique_ptr<TaskQueue<AttachmentId>> upload_task_queue_;

  // Must be last data member.
  base::WeakPtrFactory<AttachmentServiceImpl> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(AttachmentServiceImpl);
};

}  // namespace syncer

#endif  // COMPONENTS_SYNC_MODEL_IMPL_ATTACHMENTS_ATTACHMENT_SERVICE_IMPL_H_