| 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 #include <algorithm> |
| 6 |
| 5 #include "base/bind.h" | 7 #include "base/bind.h" |
| 6 #include "base/files/file.h" | 8 #include "base/files/file.h" |
| 7 #include "base/location.h" | 9 #include "base/location.h" |
| 8 #include "base/logging.h" | 10 #include "base/logging.h" |
| 9 #include "base/threading/worker_pool.h" | 11 #include "base/threading/worker_pool.h" |
| 10 #include "dbus/file_descriptor.h" | 12 #include "dbus/file_descriptor.h" |
| 11 | 13 |
| 14 using std::swap; |
| 15 |
| 12 namespace dbus { | 16 namespace dbus { |
| 13 | 17 |
| 14 void CHROME_DBUS_EXPORT FileDescriptor::Deleter::operator()( | 18 void CHROME_DBUS_EXPORT FileDescriptor::Deleter::operator()( |
| 15 FileDescriptor* fd) { | 19 FileDescriptor* fd) { |
| 16 base::WorkerPool::PostTask( | 20 base::WorkerPool::PostTask( |
| 17 FROM_HERE, base::Bind(&base::DeletePointer<FileDescriptor>, fd), false); | 21 FROM_HERE, base::Bind(&base::DeletePointer<FileDescriptor>, fd), false); |
| 18 } | 22 } |
| 19 | 23 |
| 24 FileDescriptor::FileDescriptor(RValue other) |
| 25 : value_(-1), owner_(false), valid_(false) { |
| 26 Swap(other.object); |
| 27 } |
| 28 |
| 20 FileDescriptor::~FileDescriptor() { | 29 FileDescriptor::~FileDescriptor() { |
| 21 if (owner_) | 30 if (owner_) |
| 22 base::File auto_closer(value_); | 31 base::File auto_closer(value_); |
| 23 } | 32 } |
| 24 | 33 |
| 34 FileDescriptor& FileDescriptor::operator=(RValue other) { |
| 35 Swap(other.object); |
| 36 return *this; |
| 37 } |
| 38 |
| 25 int FileDescriptor::value() const { | 39 int FileDescriptor::value() const { |
| 26 CHECK(valid_); | 40 CHECK(valid_); |
| 27 return value_; | 41 return value_; |
| 28 } | 42 } |
| 29 | 43 |
| 30 int FileDescriptor::TakeValue() { | 44 int FileDescriptor::TakeValue() { |
| 31 CHECK(valid_); // NB: check first so owner_ is unchanged if this triggers | 45 CHECK(valid_); // NB: check first so owner_ is unchanged if this triggers |
| 32 owner_ = false; | 46 owner_ = false; |
| 33 return value_; | 47 return value_; |
| 34 } | 48 } |
| 35 | 49 |
| 36 void FileDescriptor::CheckValidity() { | 50 void FileDescriptor::CheckValidity() { |
| 37 base::File file(value_); | 51 base::File file(value_); |
| 38 base::File::Info info; | 52 base::File::Info info; |
| 39 bool ok = file.GetInfo(&info); | 53 bool ok = file.GetInfo(&info); |
| 40 file.TakePlatformFile(); // Prevent |value_| from being closed by |file|. | 54 file.TakePlatformFile(); // Prevent |value_| from being closed by |file|. |
| 41 valid_ = (ok && !info.is_directory); | 55 valid_ = (ok && !info.is_directory); |
| 42 } | 56 } |
| 43 | 57 |
| 58 void FileDescriptor::Swap(FileDescriptor* other) { |
| 59 swap(value_, other->value_); |
| 60 swap(owner_, other->owner_); |
| 61 swap(valid_, other->valid_); |
| 62 } |
| 63 |
| 44 } // namespace dbus | 64 } // namespace dbus |
| OLD | NEW |