Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / sync / internal_api / public / attachments / attachment_service_proxy.h
blobc62a206ba7fb441556b998071bdd7ad934b4254a
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_PROXY_H_
6 #define SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_SERVICE_PROXY_H_
8 #include "base/basictypes.h"
9 #include "base/callback.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/sequenced_task_runner.h"
13 #include "base/task_runner.h"
14 #include "sync/api/attachments/attachment.h"
15 #include "sync/base/sync_export.h"
16 #include "sync/internal_api/public/attachments/attachment_service.h"
18 namespace syncer {
20 // AttachmentServiceProxy wraps an AttachmentService allowing multiple threads
21 // to share the wrapped AttachmentService and invoke its methods in the
22 // appropriate thread.
24 // Callbacks passed to methods on this class will be invoked in the same thread
25 // from which the method was called.
27 // This class does not own its wrapped AttachmentService object. This class
28 // holds a WeakPtr to the wrapped object. Once the the wrapped object is
29 // destroyed, method calls on this object will be no-ops.
31 // Users of this class should take care to destroy the wrapped object on the
32 // correct thread (wrapped_task_runner).
34 // This class is thread-safe and is designed to be passed by const-ref.
35 class SYNC_EXPORT AttachmentServiceProxy : public AttachmentService {
36 public:
37 // Default copy and assignment are welcome.
39 // Construct an invalid AttachmentServiceProxy.
40 AttachmentServiceProxy();
42 // Construct an AttachmentServiceProxy that forwards calls to |wrapped| on the
43 // |wrapped_task_runner| thread.
45 // Note, this object does not own |wrapped|. When |wrapped| is destroyed,
46 // calls to this object become no-ops.
47 AttachmentServiceProxy(
48 const scoped_refptr<base::SequencedTaskRunner>& wrapped_task_runner,
49 const base::WeakPtr<syncer::AttachmentService>& wrapped);
51 ~AttachmentServiceProxy() override;
53 void GetOrDownloadAttachments(const AttachmentIdList& attachment_ids,
54 const GetOrDownloadCallback& callback) override;
55 void UploadAttachments(const AttachmentIdList& attachment_ids) override;
57 protected:
58 // Core does the work of proxying calls to AttachmentService methods from one
59 // thread to another so AttachmentServiceProxy can be an easy-to-use,
60 // non-ref-counted A ref-counted class.
62 // Callback from AttachmentService are proxied back using free functions
63 // defined in the .cc file (ProxyFooCallback functions).
65 // Core is ref-counted because we want to allow AttachmentServiceProxy to be
66 // copy-constructable while allowing for different implementations of Core
67 // (e.g. one type of core might own the wrapped AttachmentService).
69 // Calls to objects of this class become no-ops once its wrapped object is
70 // destroyed.
71 class SYNC_EXPORT Core : public AttachmentService,
72 public base::RefCountedThreadSafe<Core> {
73 public:
74 // Construct an AttachmentServiceProxyCore that forwards calls to |wrapped|.
75 Core(const base::WeakPtr<syncer::AttachmentService>& wrapped);
77 // AttachmentService implementation.
78 void GetOrDownloadAttachments(
79 const AttachmentIdList& attachment_ids,
80 const GetOrDownloadCallback& callback) override;
81 void UploadAttachments(const AttachmentIdList& attachment_ids) override;
83 protected:
84 friend class base::RefCountedThreadSafe<Core>;
85 ~Core() override;
87 private:
88 base::WeakPtr<AttachmentService> wrapped_;
90 DISALLOW_COPY_AND_ASSIGN(Core);
93 // Used in tests to create an AttachmentServiceProxy with a custom Core.
94 AttachmentServiceProxy(
95 const scoped_refptr<base::SequencedTaskRunner>& wrapped_task_runner,
96 const scoped_refptr<Core>& core);
98 private:
99 scoped_refptr<base::SequencedTaskRunner> wrapped_task_runner_;
100 scoped_refptr<Core> core_;
103 } // namespace syncer
105 #endif // SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_SERVICE_PROXY_H_