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