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" |
| 9 #include "dbus/dbus_export.h" |
| 10 |
8 namespace dbus { | 11 namespace dbus { |
9 | 12 |
10 // FileDescriptor is a type used to encapsulate D-Bus file descriptors | 13 // FileDescriptor is a type used to encapsulate D-Bus file descriptors |
11 // and to follow the RAII idiom appropiate for use with message operations | 14 // and to follow the RAII idiom appropiate for use with message operations |
12 // where the descriptor might be easily leaked. To guard against this the | 15 // where the descriptor might be easily leaked. To guard against this the |
13 // descriptor is closed when an instance is destroyed if it is owned. | 16 // descriptor is closed when an instance is destroyed if it is owned. |
14 // Ownership is asserted only when PutValue is used and TakeValue can be | 17 // Ownership is asserted only when PutValue is used and TakeValue can be |
15 // used to take ownership. | 18 // used to take ownership. |
16 // | 19 // |
17 // For example, in the following | 20 // For example, in the following |
18 // FileDescriptor fd; | 21 // FileDescriptor fd; |
19 // if (!reader->PopString(&name) || | 22 // if (!reader->PopString(&name) || |
20 // !reader->PopFileDescriptor(&fd) || | 23 // !reader->PopFileDescriptor(&fd) || |
21 // !reader->PopUint32(&flags)) { | 24 // !reader->PopUint32(&flags)) { |
22 // the descriptor in fd will be closed if the PopUint32 fails. But | 25 // the descriptor in fd will be closed if the PopUint32 fails. But |
23 // writer.AppendFileDescriptor(dbus::FileDescriptor(1)); | 26 // writer.AppendFileDescriptor(dbus::FileDescriptor(1)); |
24 // will not automatically close "1" because it is not owned. | 27 // will not automatically close "1" because it is not owned. |
25 // | 28 // |
26 // Descriptors must be validated before marshalling in a D-Bus message | 29 // Descriptors must be validated before marshalling in a D-Bus message |
27 // or using them after unmarshalling. We disallow descriptors to a | 30 // or using them after unmarshalling. We disallow descriptors to a |
28 // directory to reduce the security risks. Splitting out validation | 31 // directory to reduce the security risks. Splitting out validation |
29 // also allows the caller to do this work on the File thread to conform | 32 // also allows the caller to do this work on the File thread to conform |
30 // with i/o restrictions. | 33 // with i/o restrictions. |
31 class FileDescriptor { | 34 class CHROME_DBUS_EXPORT FileDescriptor { |
32 public: | 35 public: |
33 // Permits initialization without a value for passing to | 36 // Permits initialization without a value for passing to |
34 // dbus::MessageReader::PopFileDescriptor to fill in and from int values. | 37 // dbus::MessageReader::PopFileDescriptor to fill in and from int values. |
35 FileDescriptor() : value_(-1), owner_(false), valid_(false) {} | 38 FileDescriptor() : value_(-1), owner_(false), valid_(false) {} |
36 explicit FileDescriptor(int value) : value_(value), owner_(false), | 39 explicit FileDescriptor(int value) : value_(value), owner_(false), |
37 valid_(false) {} | 40 valid_(false) {} |
38 | 41 |
39 virtual ~FileDescriptor(); | 42 virtual ~FileDescriptor(); |
40 | 43 |
41 // Retrieves value as an int without affecting ownership. | 44 // Retrieves value as an int without affecting ownership. |
(...skipping 21 matching lines...) Expand all Loading... |
63 int value_; | 66 int value_; |
64 bool owner_; | 67 bool owner_; |
65 bool valid_; | 68 bool valid_; |
66 | 69 |
67 DISALLOW_COPY_AND_ASSIGN(FileDescriptor); | 70 DISALLOW_COPY_AND_ASSIGN(FileDescriptor); |
68 }; | 71 }; |
69 | 72 |
70 } // namespace dbus | 73 } // namespace dbus |
71 | 74 |
72 #endif // DBUS_FILE_DESCRIPTOR_H_ | 75 #endif // DBUS_FILE_DESCRIPTOR_H_ |
OLD | NEW |