1e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// Copyright 2014 The Chromium Authors. All rights reserved. 2e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// Use of this source code is governed by a BSD-style license that can be 3e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// found in the LICENSE file. 4e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 55f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#ifndef SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_SERVICE_PROXY_H_ 65f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#define SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_SERVICE_PROXY_H_ 7e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 8e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "base/basictypes.h" 9e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "base/callback.h" 10e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "base/memory/scoped_ptr.h" 11e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "base/memory/weak_ptr.h" 12e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "base/sequenced_task_runner.h" 13e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "base/task_runner.h" 14e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "sync/api/attachments/attachment.h" 15e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "sync/base/sync_export.h" 165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "sync/internal_api/public/attachments/attachment_service.h" 17e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 18e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochnamespace syncer { 19e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 20e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// AttachmentServiceProxy wraps an AttachmentService allowing multiple threads 21a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// to share the wrapped AttachmentService and invoke its methods in the 22a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// appropriate thread. 23e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// 24e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// Callbacks passed to methods on this class will be invoked in the same thread 25e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// from which the method was called. 26e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// 27e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// This class does not own its wrapped AttachmentService object. This class 28e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// holds a WeakPtr to the wrapped object. Once the the wrapped object is 29e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// destroyed, method calls on this object will be no-ops. 30e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// 31a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// Users of this class should take care to destroy the wrapped object on the 32a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// correct thread (wrapped_task_runner). 33e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// 34a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch// This class is thread-safe and is designed to be passed by const-ref. 35e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochclass SYNC_EXPORT AttachmentServiceProxy : public AttachmentService { 36e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch public: 37e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch // Default copy and assignment are welcome. 38e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 39e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch // Construct an invalid AttachmentServiceProxy. 40e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch AttachmentServiceProxy(); 41e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 42e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch // Construct an AttachmentServiceProxy that forwards calls to |wrapped| on the 43e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch // |wrapped_task_runner| thread. 44a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch // 45a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch // Note, this object does not own |wrapped|. When |wrapped| is destroyed, 46a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch // calls to this object become no-ops. 47e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch AttachmentServiceProxy( 48e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch const scoped_refptr<base::SequencedTaskRunner>& wrapped_task_runner, 49a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch const base::WeakPtr<syncer::AttachmentService>& wrapped); 50e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 51e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch virtual ~AttachmentServiceProxy(); 52e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 53e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch // AttachmentService implementation. 541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // 551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci // GetStore always returns NULL. 561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci virtual AttachmentStore* GetStore() OVERRIDE; 57a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch virtual void GetOrDownloadAttachments( 58a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch const AttachmentIdList& attachment_ids, 59a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch const GetOrDownloadCallback& callback) OVERRIDE; 60e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch virtual void DropAttachments(const AttachmentIdList& attachment_ids, 61e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch const DropCallback& callback) OVERRIDE; 621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci virtual void UploadAttachments( 631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci const AttachmentIdSet& attachment_ids) OVERRIDE; 64e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 65a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch protected: 66a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch // Core does the work of proxying calls to AttachmentService methods from one 67a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch // thread to another so AttachmentServiceProxy can be an easy-to-use, 68a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch // non-ref-counted A ref-counted class. 69a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch // 70a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch // Callback from AttachmentService are proxied back using free functions 71a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch // defined in the .cc file (ProxyFooCallback functions). 72a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch // 73a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch // Core is ref-counted because we want to allow AttachmentServiceProxy to be 74a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch // copy-constructable while allowing for different implementations of Core 75a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch // (e.g. one type of core might own the wrapped AttachmentService). 76a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch // 77a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch // Calls to objects of this class become no-ops once its wrapped object is 78a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch // destroyed. 79a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch class SYNC_EXPORT Core : public AttachmentService, 80a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch public base::RefCountedThreadSafe<Core> { 81a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch public: 82a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch // Construct an AttachmentServiceProxyCore that forwards calls to |wrapped|. 83a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch Core(const base::WeakPtr<syncer::AttachmentService>& wrapped); 84a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch 85a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch // AttachmentService implementation. 861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci virtual AttachmentStore* GetStore() OVERRIDE; 87a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch virtual void GetOrDownloadAttachments( 88a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch const AttachmentIdList& attachment_ids, 89a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch const GetOrDownloadCallback& callback) OVERRIDE; 90a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch virtual void DropAttachments(const AttachmentIdList& attachment_ids, 91a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch const DropCallback& callback) OVERRIDE; 921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci virtual void UploadAttachments( 931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci const AttachmentIdSet& attachment_ids) OVERRIDE; 94a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch 95a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch protected: 96a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch friend class base::RefCountedThreadSafe<Core>; 97a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch virtual ~Core(); 98a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch 99a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch private: 100a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch base::WeakPtr<AttachmentService> wrapped_; 101a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch 102a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch DISALLOW_COPY_AND_ASSIGN(Core); 103a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch }; 104a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch 105a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch // Used in tests to create an AttachmentServiceProxy with a custom Core. 106a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch AttachmentServiceProxy( 107a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch const scoped_refptr<base::SequencedTaskRunner>& wrapped_task_runner, 108a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch const scoped_refptr<Core>& core); 109a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch 110e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch private: 111e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch scoped_refptr<base::SequencedTaskRunner> wrapped_task_runner_; 112a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch scoped_refptr<Core> core_; 113e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch}; 114e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 115e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch} // namespace syncer 116e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 1175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#endif // SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_ATTACHMENT_SERVICE_PROXY_H_ 118