iOS factory for AccountFetcherService
[chromium-blink-merge.git] / content / child / shared_memory_data_consumer_handle.h
blobcd6e34ed0df562d94d38ae56bcadd16e179557fa
1 // Copyright 2015 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 CONTENT_CHILD_SHARED_MEMORY_DATA_CONSUMER_HANDLE_H_
6 #define CONTENT_CHILD_SHARED_MEMORY_DATA_CONSUMER_HANDLE_H_
8 #include "base/callback.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "content/common/content_export.h"
12 #include "content/public/child/request_peer.h"
13 #include "third_party/WebKit/public/platform/WebDataConsumerHandle.h"
15 namespace content {
17 // This class is a WebDataConsumerHandle that accepts RequestPeer::ReceivedData.
18 class CONTENT_EXPORT SharedMemoryDataConsumerHandle final
19 : public NON_EXPORTED_BASE(blink::WebDataConsumerHandle) {
20 private:
21 class Context;
23 public:
24 enum BackpressureMode {
25 kApplyBackpressure,
26 kDoNotApplyBackpressure,
29 class CONTENT_EXPORT Writer final {
30 public:
31 Writer(const scoped_refptr<Context>& context, BackpressureMode mode);
32 ~Writer();
33 // Note: Writer assumes |AddData| is not called in a client's didGetReadable
34 // callback. There isn't such assumption for |Close| and |Fail|.
35 void AddData(scoped_ptr<RequestPeer::ReceivedData> data);
36 void Close();
37 // TODO(yhirano): Consider providing error code.
38 void Fail();
40 private:
41 scoped_refptr<Context> context_;
42 BackpressureMode mode_;
44 DISALLOW_COPY_AND_ASSIGN(Writer);
47 class ReaderImpl final : public Reader {
48 public:
49 ReaderImpl(scoped_refptr<Context> context, Client* client);
50 virtual ~ReaderImpl();
51 virtual Result read(void* data, size_t size, Flags flags, size_t* readSize);
52 virtual Result beginRead(const void** buffer,
53 Flags flags,
54 size_t* available);
55 virtual Result endRead(size_t readSize);
57 private:
58 scoped_refptr<Context> context_;
60 DISALLOW_COPY_AND_ASSIGN(ReaderImpl);
63 // Creates a handle and a writer associated with the handle. The created
64 // writer should be used on the calling thread.
65 SharedMemoryDataConsumerHandle(BackpressureMode mode,
66 scoped_ptr<Writer>* writer);
67 // |on_reader_detached| will be called aynchronously on the calling thread
68 // when the reader (including the handle) is detached (i.e. both the handle
69 // and the reader are destructed). The callback will be reset in the internal
70 // context when the writer is detached, i.e. |Close| or |Fail| is called,
71 // and the callback will never be called.
72 SharedMemoryDataConsumerHandle(BackpressureMode mode,
73 const base::Closure& on_reader_detached,
74 scoped_ptr<Writer>* writer);
75 virtual ~SharedMemoryDataConsumerHandle();
77 scoped_ptr<Reader> ObtainReader(Client* client);
79 private:
80 virtual ReaderImpl* obtainReaderInternal(Client* client);
81 const char* debugName() const override;
83 scoped_refptr<Context> context_;
85 DISALLOW_COPY_AND_ASSIGN(SharedMemoryDataConsumerHandle);
88 } // namespace content
90 #endif // CONTENT_CHILD_SHARED_MEMORY_DATA_CONSUMER_HANDLE_H_