summaryrefslogtreecommitdiffstats
path: root/content/child/shared_memory_data_consumer_handle.h
blob: 52a244b90a6f6d4d566782ba9bf58f23b95a12a7 (plain)
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
// Copyright 2015 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 CONTENT_CHILD_SHARED_MEMORY_DATA_CONSUMER_HANDLE_H_
#define CONTENT_CHILD_SHARED_MEMORY_DATA_CONSUMER_HANDLE_H_

#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "content/common/content_export.h"
#include "content/public/child/request_peer.h"
#include "third_party/WebKit/public/platform/WebDataConsumerHandle.h"

namespace content {

// This class is a WebDataConsumerHandle that accepts RequestPeer::ReceivedData.
class CONTENT_EXPORT SharedMemoryDataConsumerHandle final
    : public NON_EXPORTED_BASE(blink::WebDataConsumerHandle) {
 private:
  class Context;

 public:
  enum BackpressureMode {
    kApplyBackpressure,
    kDoNotApplyBackpressure,
  };

  class CONTENT_EXPORT Writer final {
   public:
    Writer(const scoped_refptr<Context>& context, BackpressureMode mode);
    ~Writer();
    // Note: Writer assumes |AddData| is not called in a client's didGetReadable
    // callback. There isn't such assumption for |Close| and |Fail|.
    void AddData(scoped_ptr<RequestPeer::ReceivedData> data);
    void Close();
    // TODO(yhirano): Consider providing error code.
    void Fail();

   private:
    scoped_refptr<Context> context_;
    BackpressureMode mode_;

    DISALLOW_COPY_AND_ASSIGN(Writer);
  };

  class ReaderImpl final : public Reader {
   public:
    ReaderImpl(scoped_refptr<Context> context, Client* client);
    ~ReaderImpl() override;
    Result read(void* data,
                size_t size,
                Flags flags,
                size_t* readSize) override;
    Result beginRead(const void** buffer,
                     Flags flags,
                     size_t* available) override;
    Result endRead(size_t readSize) override;

   private:
    scoped_refptr<Context> context_;

    DISALLOW_COPY_AND_ASSIGN(ReaderImpl);
  };

  // Creates a handle and a writer associated with the handle. The created
  // writer should be used on the calling thread.
  SharedMemoryDataConsumerHandle(BackpressureMode mode,
                                 scoped_ptr<Writer>* writer);
  // |on_reader_detached| will be called aynchronously on the calling thread
  // when the reader (including the handle) is detached (i.e. both the handle
  // and the reader are destructed). The callback will be reset in the internal
  // context when the writer is detached, i.e. |Close| or |Fail| is called,
  // and the callback will never be called.
  SharedMemoryDataConsumerHandle(BackpressureMode mode,
                                 const base::Closure& on_reader_detached,
                                 scoped_ptr<Writer>* writer);
  ~SharedMemoryDataConsumerHandle() override;

  scoped_ptr<Reader> ObtainReader(Client* client);

 private:
  ReaderImpl* obtainReaderInternal(Client* client) override;
  const char* debugName() const override;

  scoped_refptr<Context> context_;

  DISALLOW_COPY_AND_ASSIGN(SharedMemoryDataConsumerHandle);
};

}  // namespace content

#endif  // CONTENT_CHILD_SHARED_MEMORY_DATA_CONSUMER_HANDLE_H_