Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / dbus / file_descriptor.cc
blobc740f28062244b83ddd8e0727f771a6f8b4ad050
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.
5 #include <algorithm>
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"
14 using std::swap;
16 namespace dbus {
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);
24 FileDescriptor::FileDescriptor(RValue other)
25 : value_(-1), owner_(false), valid_(false) {
26 Swap(other.object);
29 FileDescriptor::~FileDescriptor() {
30 if (owner_)
31 base::File auto_closer(value_);
34 FileDescriptor& FileDescriptor::operator=(RValue other) {
35 Swap(other.object);
36 return *this;
39 int FileDescriptor::value() const {
40 CHECK(valid_);
41 return value_;
44 int FileDescriptor::TakeValue() {
45 CHECK(valid_); // NB: check first so owner_ is unchanged if this triggers
46 owner_ = false;
47 return value_;
50 void FileDescriptor::CheckValidity() {
51 base::File file(value_);
52 if (!file.IsValid()) {
53 valid_ = false;
54 return;
57 base::File::Info info;
58 bool ok = file.GetInfo(&info);
59 file.TakePlatformFile(); // Prevent |value_| from being closed by |file|.
60 valid_ = (ok && !info.is_directory);
63 void FileDescriptor::Swap(FileDescriptor* other) {
64 swap(value_, other->value_);
65 swap(owner_, other->owner_);
66 swap(valid_, other->valid_);
69 } // namespace dbus