Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
4 : // Use of this source code is governed by a BSD-style license that can be
5 : // found in the LICENSE file.
6 :
7 : #ifndef CHROME_COMMON_FILE_DESCRIPTOR_SET_POSIX_H_
8 : #define CHROME_COMMON_FILE_DESCRIPTOR_SET_POSIX_H_
9 :
10 : #include <vector>
11 :
12 : #include "base/basictypes.h"
13 : #include "base/file_descriptor_posix.h"
14 : #include "nsISupportsImpl.h"
15 :
16 : // -----------------------------------------------------------------------------
17 : // A FileDescriptorSet is an ordered set of POSIX file descriptors. These are
18 : // associated with IPC messages so that descriptors can be transmitted over a
19 : // UNIX domain socket.
20 : // -----------------------------------------------------------------------------
21 : class FileDescriptorSet {
22 : public:
23 1295 : NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FileDescriptorSet)
24 : FileDescriptorSet();
25 :
26 : // Mac and Linux both limit the number of file descriptors per message to
27 : // slightly more than 250.
28 : enum {
29 : MAX_DESCRIPTORS_PER_MESSAGE = 250
30 : };
31 :
32 : // ---------------------------------------------------------------------------
33 : // Interfaces for building during message serialisation...
34 :
35 : // Add a descriptor to the end of the set. Returns false iff the set is full.
36 : bool Add(int fd);
37 : // Add a descriptor to the end of the set and automatically close it after
38 : // transmission. Returns false iff the set is full.
39 : bool AddAndAutoClose(int fd);
40 :
41 : // ---------------------------------------------------------------------------
42 :
43 :
44 : // ---------------------------------------------------------------------------
45 : // Interfaces for accessing during message deserialisation...
46 :
47 : // Return the number of descriptors
48 74 : unsigned size() const { return descriptors_.size(); }
49 : // Return true if no unconsumed descriptors remain
50 392 : bool empty() const { return descriptors_.empty(); }
51 : // Fetch the nth descriptor from the beginning of the set. Code using this
52 : // /must/ access the descriptors in order, except that it may wrap from the
53 : // end to index 0 again.
54 : //
55 : // This interface is designed for the deserialising code as it doesn't
56 : // support close flags.
57 : // returns: file descriptor, or -1 on error
58 : int GetDescriptorAt(unsigned n) const;
59 :
60 : // ---------------------------------------------------------------------------
61 :
62 :
63 : // ---------------------------------------------------------------------------
64 : // Interfaces for transmission...
65 :
66 : // Fill an array with file descriptors without 'consuming' them. CommitAll
67 : // must be called after these descriptors have been transmitted.
68 : // buffer: (output) a buffer of, at least, size() integers.
69 : void GetDescriptors(int* buffer) const;
70 : // This must be called after transmitting the descriptors returned by
71 : // GetDescriptors. It marks all the descriptors as consumed and closes those
72 : // which are auto-close.
73 : void CommitAll();
74 :
75 : // ---------------------------------------------------------------------------
76 :
77 :
78 : // ---------------------------------------------------------------------------
79 : // Interfaces for receiving...
80 :
81 : // Set the contents of the set from the given buffer. This set must be empty
82 : // before calling. The auto-close flag is set on all the descriptors so that
83 : // unconsumed descriptors are closed on destruction.
84 : void SetDescriptors(const int* buffer, unsigned count);
85 :
86 : // ---------------------------------------------------------------------------
87 :
88 : private:
89 : ~FileDescriptorSet();
90 :
91 : // A vector of descriptors and close flags. If this message is sent, then
92 : // these descriptors are sent as control data. After sending, any descriptors
93 : // with a true flag are closed. If this message has been received, then these
94 : // are the descriptors which were received and all close flags are true.
95 : std::vector<base::FileDescriptor> descriptors_;
96 :
97 : // This contains the index of the next descriptor which should be consumed.
98 : // It's used in a couple of ways. Firstly, at destruction we can check that
99 : // all the descriptors have been read (with GetNthDescriptor). Secondly, we
100 : // can check that they are read in order.
101 : mutable unsigned consumed_descriptor_highwater_;
102 :
103 : DISALLOW_COPY_AND_ASSIGN(FileDescriptorSet);
104 : };
105 :
106 : #endif // CHROME_COMMON_FILE_DESCRIPTOR_SET_POSIX_H_
|