Chromium Code Reviews| Index: blimp/net/blob_channel/blob_channel_receiver.h |
| diff --git a/blimp/net/blob_channel/blob_channel_receiver.h b/blimp/net/blob_channel/blob_channel_receiver.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6b94cff7ff4f2c0074e99c74b722f5e23c19d667 |
| --- /dev/null |
| +++ b/blimp/net/blob_channel/blob_channel_receiver.h |
| @@ -0,0 +1,94 @@ |
| +// Copyright 2016 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 BLIMP_NET_BLOB_CHANNEL_BLOB_CHANNEL_RECEIVER_H_ |
| +#define BLIMP_NET_BLOB_CHANNEL_BLOB_CHANNEL_RECEIVER_H_ |
| + |
| +#include <map> |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/macros.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/synchronization/lock.h" |
| +#include "blimp/common/blob_cache/blob_cache.h" |
| +#include "blimp/net/blimp_message_processor.h" |
| +#include "blimp/net/blimp_net_export.h" |
| +#include "blimp/net/blob_channel/types.h" |
| + |
| +namespace blimp { |
| + |
| +class BlobCache; |
| + |
| +// Receives blobs from a remote sender. |
| +// The transport-specific details are provided by the caller using a Delegate |
| +// subclass. |
| +class BLIMP_NET_EXPORT BlobChannelReceiver { |
| + public: |
| + typedef base::Callback<void(const BlobId&, scoped_refptr<RefCountedVector>)> |
| + BlobReceivedCallback; |
| + |
| + class Delegate { |
| + public: |
| + Delegate(); |
| + ~Delegate(); |
| + |
| + // Requests the blob |id| from the sender. The delegate method |
| + // OnBlobReceived() is triggered if and when the blob arrives. |
| + virtual void Request(const BlobId& id) = 0; |
| + |
| + // Calls OnBlobReceived() on the BlobChannelReceiver. |
| + void OnBlobReceived(const BlobId& id, BlobData data); |
| + |
| + private: |
| + friend class BlobChannelReceiver; |
| + |
| + // Sets the Receiver object to receive incoming blobs. |
| + void SetReceiver(base::WeakPtr<BlobChannelReceiver> receiver); |
| + |
| + // Using a WeakPtr so the BlobChannelReceiver can be destroyed before |
| + // |this|. |
| + base::WeakPtr<BlobChannelReceiver> receiver_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(Delegate); |
| + }; |
| + |
| + // Caller must ensure that |cache| and |delegate| outlive |this|. |
| + BlobChannelReceiver(BlobCache* cache, Delegate* delegate); |
|
Wez
2016/04/18 22:40:40
Remind me why the receiver can't just "own" the ca
|
| + ~BlobChannelReceiver(); |
| + |
| + // Gets a blob from the BlobChannel, first pulling from a local cache and |
| + // falling back to requesting the data from a remote cache. |
| + // If blob |id| is already available, then it is returned synchronously and |
| + // |callback| is discarded. |
| + // If blob |id| has not yet been received, then a null refptr is returned, |
| + // and |callback| is invoked later when the blob arrives. |
| + // Can be called on any thread. Holds |lock_|. |
|
Wez
2016/04/18 22:40:40
Holds lock_ is an internal implementation detail.
|
| + BlobData Get(const BlobId& id, const BlobReceivedCallback& callback); |
|
Wez
2016/04/18 22:40:41
What is BlobData? I don't see it defined anywhere
|
| + |
| + private: |
| + friend class Delegate; |
| + |
| + // Method called by |delegate_| when a blob arrives from the transport. |
| + // Holds |lock_|. |
| + void OnBlobReceived(const BlobId& id, BlobData data); |
| + |
| + BlobCache* cache_ = nullptr; |
| + Delegate* delegate_ = nullptr; |
|
Wez
2016/04/18 22:56:11
nit: You have no default ctor for this class, so n
|
| + |
| + // Callbacks waiting to be invoked on read completion, keyed by blob ID. |
| + std::multimap<BlobId, BlobReceivedCallback> active_read_callbacks_; |
| + |
| + // Guards concurrent access to |cache_| and |active_read_callbacks_|. |
|
Wez
2016/04/18 22:40:41
Concurrent access by whom? The delegates call the
|
| + base::Lock lock_; |
| + |
| + base::WeakPtrFactory<BlobChannelReceiver> weak_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(BlobChannelReceiver); |
| +}; |
| + |
| +} // namespace blimp |
| + |
| +#endif // BLIMP_NET_BLOB_CHANNEL_BLOB_CHANNEL_RECEIVER_H_ |