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