sleffler@chromium.org | 22fab40 | 2012-03-30 15:46:20 +0900 | [diff] [blame] | 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. |
| 4 | |
reillyg | 41176c5 | 2015-06-11 15:19:11 +0900 | [diff] [blame] | 5 | #include <algorithm> |
| 6 | |
cmasone | 54c7c08 | 2015-04-28 06:41:34 +0900 | [diff] [blame] | 7 | #include "base/bind.h" |
rvargas@chromium.org | 966a0bb | 2014-06-07 09:02:22 +0900 | [diff] [blame] | 8 | #include "base/files/file.h" |
cmasone | 54c7c08 | 2015-04-28 06:41:34 +0900 | [diff] [blame] | 9 | #include "base/location.h" |
sleffler@chromium.org | 48d575f | 2012-05-11 02:41:10 +0900 | [diff] [blame] | 10 | #include "base/logging.h" |
cmasone | 54c7c08 | 2015-04-28 06:41:34 +0900 | [diff] [blame] | 11 | #include "base/threading/worker_pool.h" |
sleffler@chromium.org | 22fab40 | 2012-03-30 15:46:20 +0900 | [diff] [blame] | 12 | #include "dbus/file_descriptor.h" |
| 13 | |
reillyg | 41176c5 | 2015-06-11 15:19:11 +0900 | [diff] [blame] | 14 | using std::swap; |
| 15 | |
sleffler@chromium.org | 22fab40 | 2012-03-30 15:46:20 +0900 | [diff] [blame] | 16 | namespace dbus { |
| 17 | |
cmasone | 54c7c08 | 2015-04-28 06:41:34 +0900 | [diff] [blame] | 18 | void CHROME_DBUS_EXPORT FileDescriptor::Deleter::operator()( |
| 19 | FileDescriptor* fd) { |
| 20 | base::WorkerPool::PostTask( |
| 21 | FROM_HERE, base::Bind(&base::DeletePointer<FileDescriptor>, fd), false); |
| 22 | } |
| 23 | |
reillyg | 41176c5 | 2015-06-11 15:19:11 +0900 | [diff] [blame] | 24 | FileDescriptor::FileDescriptor(RValue other) |
| 25 | : value_(-1), owner_(false), valid_(false) { |
| 26 | Swap(other.object); |
| 27 | } |
| 28 | |
sleffler@chromium.org | 22fab40 | 2012-03-30 15:46:20 +0900 | [diff] [blame] | 29 | FileDescriptor::~FileDescriptor() { |
| 30 | if (owner_) |
rvargas@chromium.org | 966a0bb | 2014-06-07 09:02:22 +0900 | [diff] [blame] | 31 | base::File auto_closer(value_); |
sleffler@chromium.org | 22fab40 | 2012-03-30 15:46:20 +0900 | [diff] [blame] | 32 | } |
| 33 | |
reillyg | 41176c5 | 2015-06-11 15:19:11 +0900 | [diff] [blame] | 34 | FileDescriptor& FileDescriptor::operator=(RValue other) { |
| 35 | Swap(other.object); |
| 36 | return *this; |
| 37 | } |
| 38 | |
sleffler@chromium.org | 48d575f | 2012-05-11 02:41:10 +0900 | [diff] [blame] | 39 | int FileDescriptor::value() const { |
| 40 | CHECK(valid_); |
| 41 | return value_; |
| 42 | } |
| 43 | |
| 44 | int FileDescriptor::TakeValue() { |
| 45 | CHECK(valid_); // NB: check first so owner_ is unchanged if this triggers |
| 46 | owner_ = false; |
| 47 | return value_; |
| 48 | } |
| 49 | |
| 50 | void FileDescriptor::CheckValidity() { |
rvargas@chromium.org | 966a0bb | 2014-06-07 09:02:22 +0900 | [diff] [blame] | 51 | base::File file(value_); |
reillyg | 0a19be3 | 2015-06-26 07:27:57 +0900 | [diff] [blame] | 52 | if (!file.IsValid()) { |
| 53 | valid_ = false; |
| 54 | return; |
| 55 | } |
| 56 | |
rvargas@chromium.org | 966a0bb | 2014-06-07 09:02:22 +0900 | [diff] [blame] | 57 | base::File::Info info; |
| 58 | bool ok = file.GetInfo(&info); |
| 59 | file.TakePlatformFile(); // Prevent |value_| from being closed by |file|. |
sleffler@chromium.org | 48d575f | 2012-05-11 02:41:10 +0900 | [diff] [blame] | 60 | valid_ = (ok && !info.is_directory); |
| 61 | } |
| 62 | |
reillyg | 41176c5 | 2015-06-11 15:19:11 +0900 | [diff] [blame] | 63 | void FileDescriptor::Swap(FileDescriptor* other) { |
| 64 | swap(value_, other->value_); |
| 65 | swap(owner_, other->owner_); |
| 66 | swap(valid_, other->valid_); |
| 67 | } |
| 68 | |
sleffler@chromium.org | 22fab40 | 2012-03-30 15:46:20 +0900 | [diff] [blame] | 69 | } // namespace dbus |