Roll src/third_party/WebKit 9301d6f:4619053 (svn 201058:201059)
[chromium-blink-merge.git] / dbus / file_descriptor.h
blob8a4109789ecc48df5f4c334e53ee04d9208a5b63
1 // Copyright (c) 2012 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 DBUS_FILE_DESCRIPTOR_H_
6 #define DBUS_FILE_DESCRIPTOR_H_
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/move.h"
11 #include "dbus/dbus_export.h"
13 namespace dbus {
15 // FileDescriptor is a type used to encapsulate D-Bus file descriptors
16 // and to follow the RAII idiom appropiate for use with message operations
17 // where the descriptor might be easily leaked. To guard against this the
18 // descriptor is closed when an instance is destroyed if it is owned.
19 // Ownership is asserted only when PutValue is used and TakeValue can be
20 // used to take ownership.
22 // For example, in the following
23 // FileDescriptor fd;
24 // if (!reader->PopString(&name) ||
25 // !reader->PopFileDescriptor(&fd) ||
26 // !reader->PopUint32(&flags)) {
27 // the descriptor in fd will be closed if the PopUint32 fails. But
28 // writer.AppendFileDescriptor(dbus::FileDescriptor(1));
29 // will not automatically close "1" because it is not owned.
31 // Descriptors must be validated before marshalling in a D-Bus message
32 // or using them after unmarshalling. We disallow descriptors to a
33 // directory to reduce the security risks. Splitting out validation
34 // also allows the caller to do this work on the File thread to conform
35 // with i/o restrictions.
36 class CHROME_DBUS_EXPORT FileDescriptor {
37 MOVE_ONLY_TYPE_FOR_CPP_03(FileDescriptor, RValue);
39 public:
40 // This provides a simple way to pass around file descriptors since they must
41 // be closed on a thread that is allowed to perform I/O.
42 struct Deleter {
43 void CHROME_DBUS_EXPORT operator()(FileDescriptor* fd);
46 // Permits initialization without a value for passing to
47 // dbus::MessageReader::PopFileDescriptor to fill in and from int values.
48 FileDescriptor() : value_(-1), owner_(false), valid_(false) {}
49 explicit FileDescriptor(int value) : value_(value), owner_(false),
50 valid_(false) {}
52 // Move constructor for C++03 move emulation of this type.
53 FileDescriptor(RValue other);
55 virtual ~FileDescriptor();
57 // Move operator= for C++03 move emulation of this type.
58 FileDescriptor& operator=(RValue other);
60 // Retrieves value as an int without affecting ownership.
61 int value() const;
63 // Retrieves whether or not the descriptor is ok to send/receive.
64 int is_valid() const { return valid_; }
66 // Sets the value and assign ownership.
67 void PutValue(int value) {
68 value_ = value;
69 owner_ = true;
70 valid_ = false;
73 // Takes the value and ownership.
74 int TakeValue();
76 // Checks (and records) validity of the file descriptor.
77 // We disallow directories to avoid potential sandbox escapes.
78 // Note this call must be made on a thread where file i/o is allowed.
79 void CheckValidity();
81 private:
82 void Swap(FileDescriptor* other);
84 int value_;
85 bool owner_;
86 bool valid_;
89 using ScopedFileDescriptor =
90 scoped_ptr<FileDescriptor, FileDescriptor::Deleter>;
92 } // namespace dbus
94 #endif // DBUS_FILE_DESCRIPTOR_H_