| 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 namespace dbus { | 9 namespace dbus { |
| 10 | 10 |
| 11 // FileDescriptor is a type used to encapsulate D-Bus file descriptors | 11 // FileDescriptor is a type used to encapsulate D-Bus file descriptors |
| 12 // and to follow the RAII idiom appropiate for use with message operations | 12 // and to follow the RAII idiom appropiate for use with message operations |
| 13 // where the descriptor might be easily leaked. To guard against this the | 13 // where the descriptor might be easily leaked. To guard against this the |
| 14 // descriptor is closed when an instance is destroyed if it is owned. | 14 // descriptor is closed when an instance is destroyed if it is owned. |
| 15 // Ownership is asserted only when PutValue is used and TakeValue can be | 15 // Ownership is asserted only when PutValue is used and TakeValue can be |
| 16 // used to take ownership. | 16 // used to take ownership. |
| 17 // | 17 // |
| 18 // For example, in the following | 18 // For example, in the following |
| 19 // FileDescriptor fd; | 19 // FileDescriptor fd; |
| 20 // if (!reader->PopString(&name) || | 20 // if (!reader->PopString(&name) || |
| 21 // !reader->PopFileDescriptor(&fd) || | 21 // !reader->PopFileDescriptor(&fd) || |
| 22 // !reader->PopUint32(&flags)) { | 22 // !reader->PopUint32(&flags)) { |
| 23 // the descriptor in fd will be closed if the PopUint32 fails. But | 23 // the descriptor in fd will be closed if the PopUint32 fails. But |
| 24 // writer.AppendFileDescriptor(dbus::FileDescriptor(1)); | 24 // writer.AppendFileDescriptor(dbus::FileDescriptor(1)); |
| 25 // will not automatically close "1" because it is not owned. | 25 // will not automatically close "1" because it is not owned. |
| 26 // |
| 27 // Descriptors must be validated before marshalling in a D-Bus message |
| 28 // or using them after unmarshalling. We disallow descriptors to a |
| 29 // directory to reduce the security risks. Splitting out validation |
| 30 // also allows the caller to do this work on the File thread to conform |
| 31 // with i/o restrictions. |
| 26 class FileDescriptor { | 32 class FileDescriptor { |
| 27 public: | 33 public: |
| 28 // Permits initialization without a value for passing to | 34 // Permits initialization without a value for passing to |
| 29 // dbus::MessageReader::PopFileDescriptor to fill in and from int values. | 35 // dbus::MessageReader::PopFileDescriptor to fill in and from int values. |
| 30 FileDescriptor() : value_(-1), owner_(false) {} | 36 FileDescriptor() : value_(-1), owner_(false), valid_(false) {} |
| 31 explicit FileDescriptor(int value) : value_(value), owner_(false) {} | 37 explicit FileDescriptor(int value) : value_(value), owner_(false), |
| 38 valid_(false) {} |
| 32 | 39 |
| 33 virtual ~FileDescriptor(); | 40 virtual ~FileDescriptor(); |
| 34 | 41 |
| 35 // Retrieves value as an int without affecting ownership. | 42 // Retrieves value as an int without affecting ownership. |
| 36 int value() const { return value_; } | 43 int value() const; |
| 44 |
| 45 // Retrieves whether or not the descriptor is ok to send/receive. |
| 46 int is_valid() const { return valid_; } |
| 37 | 47 |
| 38 // Sets the value and assign ownership. | 48 // Sets the value and assign ownership. |
| 39 void PutValue(int value) { | 49 void PutValue(int value) { |
| 40 value_ = value; | 50 value_ = value; |
| 41 owner_ = true; | 51 owner_ = true; |
| 52 valid_ = false; |
| 42 } | 53 } |
| 43 | 54 |
| 44 // Takes the value and ownership. | 55 // Takes the value and ownership. |
| 45 int TakeValue() { | 56 int TakeValue(); |
| 46 owner_ = false; | 57 |
| 47 return value_; | 58 // Checks (and records) validity of the file descriptor. |
| 48 } | 59 void CheckValidity(); |
| 49 | 60 |
| 50 private: | 61 private: |
| 51 int value_; | 62 int value_; |
| 52 bool owner_; | 63 bool owner_; |
| 64 bool valid_; |
| 53 | 65 |
| 54 DISALLOW_COPY_AND_ASSIGN(FileDescriptor); | 66 DISALLOW_COPY_AND_ASSIGN(FileDescriptor); |
| 55 }; | 67 }; |
| 56 | 68 |
| 57 } // namespace dbus | 69 } // namespace dbus |
| 58 | 70 |
| 59 #endif // DBUS_FILE_DESCRIPTOR_H_ | 71 #endif // DBUS_FILE_DESCRIPTOR_H_ |
| OLD | NEW |