summaryrefslogtreecommitdiffstats
path: root/content/child/shared_memory_received_data_factory.h
blob: 3da0926c14c3494210b867b7490573af00411061 (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
// 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_RECEIVED_DATA_FACTORY_H_
#define CONTENT_CHILD_SHARED_MEMORY_RECEIVED_DATA_FACTORY_H_

#include <vector>

#include "base/memory/linked_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/shared_memory.h"
#include "content/common/content_export.h"
#include "content/public/child/request_peer.h"

namespace IPC {
class Sender;
}  // namespace IPC

namespace content {

class CONTENT_EXPORT SharedMemoryReceivedDataFactory final
    : public base::RefCounted<SharedMemoryReceivedDataFactory> {
 public:
  SharedMemoryReceivedDataFactory(IPC::Sender* message_sender,
                                  int request_id,
                                  linked_ptr<base::SharedMemory> memory);

  scoped_ptr<RequestPeer::ReceivedData> Create(int offset,
                                               int length,
                                               int encoded_length);

  // Stops this factory. After calling this function, releasing issued data
  // won't send ack signal to the browser process.
  void Stop();

 private:
  // Here TicketId is uint32_t, but a factory may issue more data by reusing id.
  using TicketId = uint32_t;

  class SharedMemoryReceivedData;
  // Called by SharedMemoryReceivedData.
  void Reclaim(TicketId id);

  class TicketComparator;
  friend class base::RefCounted<SharedMemoryReceivedDataFactory>;
  ~SharedMemoryReceivedDataFactory();

  void SendAck(size_t count);

  TicketId id_;
  TicketId oldest_;
  std::vector<TicketId> released_tickets_;
  // We assume that |message_sender_| is valid until |Stop| is called.
  IPC::Sender* message_sender_;
  int request_id_;
  bool is_stopped_;
  // Just to keep the payload alive while issued data is alive.
  linked_ptr<base::SharedMemory> memory_;

  DISALLOW_COPY_AND_ASSIGN(SharedMemoryReceivedDataFactory);
};

}  // namespace content

#endif  // CONTENT_CHILD_SHARED_MEMORY_RECEIVED_DATA_FACTORY_H_