summaryrefslogtreecommitdiffstats
path: root/remoting/capturer/shared_buffer.h
blob: 16429b9fa19c3216c2eadc1cc1825dd0cdf673b4 (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
// Copyright (c) 2012 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 REMOTING_CAPTURER_SHARED_BUFFER_H_
#define REMOTING_CAPTURER_SHARED_BUFFER_H_

#include "base/basictypes.h"
#include "base/memory/ref_counted.h"
#include "base/process.h"
#include "base/shared_memory.h"

namespace remoting {

// Represents a memory buffer that can be shared between multiple processes.
// It is more or less a convenience wrapper around base::SharedMemory providing
// ref-counted lifetime management and unique buffer identifiers.
class SharedBuffer
    : public base::RefCountedThreadSafe<SharedBuffer> {
 public:
  // Creates a new shared memory buffer of the given size and maps it to
  // the memory of the calling process. If the operation fails for any reason,
  // ptr() method will return NULL. This constructor set the identifier of this
  // buffer to 0.
  explicit SharedBuffer(uint32 size);

  // Opens an existing shared memory buffer and maps it to the memory of
  // the calling process (in read only mode). If the operation fails for any
  // reason, ptr() method will return NULL.
  SharedBuffer(int id, base::SharedMemoryHandle handle, uint32 size);

  // Opens an existing shared memory buffer created by a different process and
  // maps it to the memory of the calling process (in read only mode). If
  // the operation fails for any reason, ptr() method will return NULL.
  SharedBuffer(int id, base::SharedMemoryHandle handle,
               base::ProcessHandle process, uint32 size);

  // Returns pointer to the beginning of the allocated data buffer. Returns NULL
  // if the object initialization failed for any reason.
  void* ptr() const { return shared_memory_.memory(); }

  // Returns handle of the shared memory section containing the allocated
  // data buffer.
  base::SharedMemoryHandle handle() const { return shared_memory_.handle(); }

  int id() const { return id_; }
  uint32 size() const { return size_; }

  void set_id(int id) { id_ = id; }

 private:
  friend class base::RefCountedThreadSafe<SharedBuffer>;
  virtual ~SharedBuffer();

  // Unique identifier of the buffer or 0 if ID hasn't been set.
  int id_;

  // Shared memory section backing up the buffer.
  base::SharedMemory shared_memory_;

  // Size of the buffer in bytes.
  uint32 size_;

  DISALLOW_COPY_AND_ASSIGN(SharedBuffer);
};

}  // namespace remoting

#endif  // REMOTING_CAPTURER_SHARED_BUFFER_H_