Commit 0e6f1bdb authored by Daniel Cheng's avatar Daniel Cheng Committed by Commit Bot

Remove base::ScopedPlatformHandle.

This will be superseded by base::ScopedPlatformFile, which is strictly
target at files. base::ScopedPlatformHandle was originally going to
support other types of scoped platform handles (such as Mach ports), but
that was dropped due to the additional complexity it would incur.

Change-Id: I3ec8324ce842aca3f64670b630255717ec6bbda0
Reviewed-on: https://chromium-review.googlesource.com/570599Reviewed-by: default avatarKen Rockot <rockot@chromium.org>
Commit-Queue: Daniel Cheng <dcheng@chromium.org>
Cr-Commit-Position: refs/heads/master@{#486499}
parent 8109b5bb
......@@ -341,10 +341,6 @@ component("base") {
"files/memory_mapped_file_win.cc",
"files/scoped_file.cc",
"files/scoped_file.h",
"files/scoped_platform_handle.cc",
"files/scoped_platform_handle.h",
"files/scoped_platform_handle_posix.cc",
"files/scoped_platform_handle_win.cc",
"files/scoped_temp_dir.cc",
"files/scoped_temp_dir.h",
"format_macros.h",
......@@ -2028,7 +2024,6 @@ test("base_unittests") {
"files/file_util_unittest.cc",
"files/important_file_writer_unittest.cc",
"files/memory_mapped_file_unittest.cc",
"files/scoped_platform_handle_unittest.cc",
"files/scoped_temp_dir_unittest.cc",
"gmock_unittest.cc",
"guid_unittest.cc",
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/files/scoped_platform_handle.h"
namespace base {
ScopedPlatformHandle::ScopedPlatformHandle() : ScopedPlatformHandle(nullptr) {}
ScopedPlatformHandle::ScopedPlatformHandle(std::nullptr_t) {}
ScopedPlatformHandle::ScopedPlatformHandle(ScopedPlatformHandle&& other) =
default;
ScopedPlatformHandle::ScopedPlatformHandle(HandleType handle)
: handle_(handle) {}
ScopedPlatformHandle::ScopedPlatformHandle(ScopedHandleType handle)
: handle_(std::move(handle)) {}
ScopedPlatformHandle::~ScopedPlatformHandle() {}
ScopedPlatformHandle& ScopedPlatformHandle::operator=(
ScopedPlatformHandle&& other) = default;
ScopedPlatformHandle::ScopedHandleType ScopedPlatformHandle::Take() {
return ScopedHandleType(release());
}
} // namespace base
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_FILES_SCOPED_PLATFORM_HANDLE_H_
#define BASE_FILES_SCOPED_PLATFORM_HANDLE_H_
#include <stddef.h>
#include <stdint.h>
#include "base/base_export.h"
#include "build/build_config.h"
#if defined(OS_WIN)
#include <windows.h>
#include "base/win/scoped_handle.h"
#elif defined(OS_POSIX)
#include "base/files/scoped_file.h"
#endif
namespace base {
// A ScopedPlatformHandle encapsulates ownership of either a Windows handle or
// a POSIX file descriptor, while presenting a common interface for the sake
// of simple, consistent, and safe ownership semantics. Platform-specific usage
// details are thus relegated to code which either acquires or uses the
// underlying platform resource.
class BASE_EXPORT ScopedPlatformHandle {
public:
#if defined(OS_WIN)
using HandleType = HANDLE;
using ScopedHandleType = win::ScopedHandle;
#elif defined(OS_POSIX)
using HandleType = int;
using ScopedHandleType = ScopedFD;
#endif
// Constructors for an invalid ScopedPlatformHandle.
ScopedPlatformHandle();
ScopedPlatformHandle(std::nullptr_t);
ScopedPlatformHandle(ScopedPlatformHandle&& other);
// These constructors always take ownership of the given handle.
explicit ScopedPlatformHandle(HandleType handle);
explicit ScopedPlatformHandle(ScopedHandleType handle);
~ScopedPlatformHandle();
ScopedPlatformHandle& operator=(ScopedPlatformHandle&& other);
// Indicates whether this ScopedPlatformHandle is holding a valid handle.
bool is_valid() const;
// Closes the handle.
void reset();
// Returns the platform-specific handle value.
HandleType get() const;
// Returns the platform-specific handle value, releasing ownership of the
// handle.
HandleType release();
// Transfers ownership of the handle to a platform-specific scoper.
ScopedHandleType Take();
private:
ScopedHandleType handle_;
};
} // namespace base
#endif // BASE_FILES_SCOPED_PLATFORM_HANDLE_H_
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/files/scoped_platform_handle.h"
namespace base {
bool ScopedPlatformHandle::is_valid() const {
return handle_.is_valid();
}
void ScopedPlatformHandle::reset() {
handle_.reset();
}
ScopedPlatformHandle::HandleType ScopedPlatformHandle::get() const {
return handle_.get();
}
ScopedPlatformHandle::HandleType ScopedPlatformHandle::release() {
return handle_.release();
}
} // namespace base
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/files/scoped_platform_handle.h"
#include "base/files/file.h"
#include "base/files/scoped_temp_dir.h"
#include "base/macros.h"
#include "base/strings/stringprintf.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_WIN)
#include <windows.h>
#include "base/win/scoped_handle.h"
#elif defined(OS_POSIX)
#include "base/files/scoped_file.h"
#endif
namespace base {
namespace {
class ScopedPlatformHandleTest : public testing::Test {
public:
ScopedPlatformHandleTest() { CHECK(temp_dir_.CreateUniqueTempDir()); }
protected:
ScopedPlatformHandle CreateValidHandle() {
return ScopedPlatformHandle(OpenTempFile().TakePlatformFile());
}
private:
base::File OpenTempFile() {
base::File temp_file(temp_dir_.GetPath().AppendASCII(
base::StringPrintf("file_%d", next_file_id_)),
base::File::FLAG_CREATE | base::File::FLAG_WRITE);
++next_file_id_;
return temp_file;
}
ScopedTempDir temp_dir_;
int next_file_id_ = 1;
DISALLOW_COPY_AND_ASSIGN(ScopedPlatformHandleTest);
};
TEST_F(ScopedPlatformHandleTest, Invalid) {
ScopedPlatformHandle default_value;
EXPECT_TRUE(!default_value.is_valid());
ScopedPlatformHandle null_value(nullptr);
EXPECT_TRUE(!null_value.is_valid());
default_value.reset();
null_value.reset();
EXPECT_TRUE(!default_value.is_valid());
EXPECT_TRUE(!null_value.is_valid());
}
TEST_F(ScopedPlatformHandleTest, BasicUsage) {
ScopedPlatformHandle handle_a = CreateValidHandle();
ScopedPlatformHandle handle_b = CreateValidHandle();
EXPECT_TRUE(handle_a.is_valid());
EXPECT_TRUE(handle_b.is_valid());
ScopedPlatformHandle::HandleType handle_a_value = handle_a.get();
ScopedPlatformHandle::HandleType handle_b_value = handle_b.get();
EXPECT_TRUE(handle_a.is_valid());
EXPECT_TRUE(handle_b.is_valid());
ScopedPlatformHandle::ScopedHandleType scoped_handle = handle_a.Take();
ScopedPlatformHandle::HandleType raw_handle = handle_b.release();
EXPECT_FALSE(handle_a.is_valid());
EXPECT_FALSE(handle_b.is_valid());
handle_a = ScopedPlatformHandle(std::move(scoped_handle));
handle_b = ScopedPlatformHandle(raw_handle);
EXPECT_TRUE(handle_a.is_valid());
EXPECT_TRUE(handle_b.is_valid());
EXPECT_EQ(handle_a_value, handle_a.get());
EXPECT_EQ(handle_b_value, handle_b.get());
handle_b = std::move(handle_a);
EXPECT_FALSE(handle_a.is_valid());
EXPECT_TRUE(handle_b.is_valid());
EXPECT_EQ(handle_a_value, handle_b.get());
handle_b.reset();
EXPECT_FALSE(handle_b.is_valid());
}
} // namespace
} // namespace base
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/files/scoped_platform_handle.h"
namespace base {
bool ScopedPlatformHandle::is_valid() const {
return handle_.IsValid();
}
void ScopedPlatformHandle::reset() {
handle_.Close();
}
ScopedPlatformHandle::HandleType ScopedPlatformHandle::get() const {
return handle_.Get();
}
ScopedPlatformHandle::HandleType ScopedPlatformHandle::release() {
return handle_.Take();
}
} // namespace base
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment