Add our own (mojo::util::)ScopedFILE and replace uses of base::ScopedFILE with it. It's entirely equivalent to base::ScopedFILE. For now, we continue to use base's scoped_ptr, but we could use std::unique_ptr instead in the future. R=vardhan@google.com Review URL: https://codereview.chromium.org/1347783002 .
diff --git a/mojo/edk/embedder/BUILD.gn b/mojo/edk/embedder/BUILD.gn index 08a42fa..1cc513c 100644 --- a/mojo/edk/embedder/BUILD.gn +++ b/mojo/edk/embedder/BUILD.gn
@@ -95,6 +95,8 @@ if (is_android) { deps += [ "//third_party/ashmem" ] } + + mojo_edk_deps = [ "mojo/edk/util" ] } mojo_edk_source_set("delegates") { @@ -139,5 +141,6 @@ "mojo/edk/test:test_support", "mojo/edk/system", "mojo/edk/system:test_utils", + "mojo/edk/util", ] }
diff --git a/mojo/edk/embedder/platform_channel_pair_posix_unittest.cc b/mojo/edk/embedder/platform_channel_pair_posix_unittest.cc index 0b20f4b..8857ecc 100644 --- a/mojo/edk/embedder/platform_channel_pair_posix_unittest.cc +++ b/mojo/edk/embedder/platform_channel_pair_posix_unittest.cc
@@ -15,7 +15,6 @@ #include <deque> -#include "base/files/scoped_file.h" #include "base/logging.h" #include "build/build_config.h" #include "mojo/edk/embedder/platform_channel_utils_posix.h" @@ -23,6 +22,7 @@ #include "mojo/edk/embedder/platform_handle_vector.h" #include "mojo/edk/embedder/scoped_platform_handle.h" #include "mojo/edk/test/test_utils.h" +#include "mojo/edk/util/scoped_file.h" #include "mojo/public/cpp/system/macros.h" #include "testing/gtest/include/gtest/gtest.h" @@ -166,7 +166,7 @@ const char c = '0' + (i % 10); ScopedPlatformHandleVectorPtr platform_handles(new PlatformHandleVector); for (size_t j = 1; j <= i; j++) { - base::ScopedFILE fp(NewTmpFile()); + util::ScopedFILE fp(NewTmpFile()); ASSERT_TRUE(fp); ASSERT_EQ(j, fwrite(std::string(j, c).data(), 1, j, fp.get())); platform_handles->push_back( @@ -194,7 +194,7 @@ EXPECT_EQ(i, received_handles.size()); for (size_t j = 0; !received_handles.empty(); j++) { - base::ScopedFILE fp(test::FILEFromPlatformHandle( + util::ScopedFILE fp(test::FILEFromPlatformHandle( ScopedPlatformHandle(received_handles.front()), "rb")); received_handles.pop_front(); ASSERT_TRUE(fp); @@ -217,7 +217,7 @@ const std::string file_contents("hello world"); { - base::ScopedFILE fp(NewTmpFile()); + util::ScopedFILE fp(NewTmpFile()); ASSERT_TRUE(fp); ASSERT_EQ(file_contents.size(), fwrite(file_contents.data(), 1, file_contents.size(), fp.get())); @@ -252,7 +252,7 @@ EXPECT_TRUE(received_handles[1].is_valid()); { - base::ScopedFILE fp(test::FILEFromPlatformHandle( + util::ScopedFILE fp(test::FILEFromPlatformHandle( ScopedPlatformHandle(received_handles[1]), "rb")); received_handles[1] = PlatformHandle(); ASSERT_TRUE(fp);
diff --git a/mojo/edk/embedder/simple_platform_shared_buffer_posix.cc b/mojo/edk/embedder/simple_platform_shared_buffer_posix.cc index 5e09e7d..613919d 100644 --- a/mojo/edk/embedder/simple_platform_shared_buffer_posix.cc +++ b/mojo/edk/embedder/simple_platform_shared_buffer_posix.cc
@@ -15,11 +15,11 @@ #include "base/files/file_path.h" #include "base/files/file_util.h" -#include "base/files/scoped_file.h" #include "base/logging.h" #include "base/posix/eintr_wrapper.h" #include "base/sys_info.h" #include "base/threading/thread_restrictions.h" +#include "mojo/edk/util/scoped_file.h" // We assume that |size_t| and |off_t| (type for |ftruncate()|) fits in a // |uint64_t|. @@ -56,7 +56,7 @@ return false; } base::FilePath shared_buffer_file; - base::ScopedFILE fp(base::CreateAndOpenTemporaryFileInDir( + util::ScopedFILE fp(base::CreateAndOpenTemporaryFileInDir( shared_buffer_dir, &shared_buffer_file)); if (!fp) { LOG(ERROR) << "Failed to create/open temporary file for shared memory";
diff --git a/mojo/edk/system/BUILD.gn b/mojo/edk/system/BUILD.gn index 7323026..b8a1d18 100644 --- a/mojo/edk/system/BUILD.gn +++ b/mojo/edk/system/BUILD.gn
@@ -219,6 +219,7 @@ ":test_utils", "../embedder:embedder_unittests", "../test:test_support", + "../util", "//base", "//base/test:test_support", "//testing/gtest",
diff --git a/mojo/edk/system/multiprocess_message_pipe_unittest.cc b/mojo/edk/system/multiprocess_message_pipe_unittest.cc index 4f28bc5..d67c1b7 100644 --- a/mojo/edk/system/multiprocess_message_pipe_unittest.cc +++ b/mojo/edk/system/multiprocess_message_pipe_unittest.cc
@@ -12,7 +12,6 @@ #include "base/bind.h" #include "base/files/file_path.h" #include "base/files/file_util.h" -#include "base/files/scoped_file.h" #include "base/files/scoped_temp_dir.h" #include "base/location.h" #include "base/logging.h" @@ -28,6 +27,7 @@ #include "mojo/edk/system/shared_buffer_dispatcher.h" #include "mojo/edk/system/test_utils.h" #include "mojo/edk/test/test_utils.h" +#include "mojo/edk/util/scoped_file.h" #include "testing/gtest/include/gtest/gtest.h" namespace mojo { @@ -431,7 +431,7 @@ CHECK(h.is_valid()); dispatcher->Close(); - base::ScopedFILE fp(mojo::test::FILEFromPlatformHandle(h.Pass(), "r")); + util::ScopedFILE fp(mojo::test::FILEFromPlatformHandle(h.Pass(), "r")); CHECK(fp); std::string fread_buffer(100, '\0'); size_t bytes_read = @@ -463,7 +463,7 @@ size_t pipe_count = GetParam(); for (size_t i = 0; i < pipe_count; ++i) { base::FilePath unused; - base::ScopedFILE fp( + util::ScopedFILE fp( CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); const std::string world("world"); CHECK_EQ(fwrite(&world[0], 1, world.size(), fp.get()), world.size());
diff --git a/mojo/edk/system/platform_handle_dispatcher_unittest.cc b/mojo/edk/system/platform_handle_dispatcher_unittest.cc index 55887a7..f3426a2 100644 --- a/mojo/edk/system/platform_handle_dispatcher_unittest.cc +++ b/mojo/edk/system/platform_handle_dispatcher_unittest.cc
@@ -8,10 +8,10 @@ #include "base/files/file_path.h" #include "base/files/file_util.h" -#include "base/files/scoped_file.h" #include "base/files/scoped_temp_dir.h" #include "base/memory/ref_counted.h" #include "mojo/edk/test/test_utils.h" +#include "mojo/edk/util/scoped_file.h" #include "testing/gtest/include/gtest/gtest.h" namespace mojo { @@ -25,7 +25,7 @@ static const char kHelloWorld[] = "hello world"; base::FilePath unused; - base::ScopedFILE fp( + util::ScopedFILE fp( CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); ASSERT_TRUE(fp); EXPECT_EQ(sizeof(kHelloWorld), @@ -68,7 +68,7 @@ static const char kFooBar[] = "foo bar"; base::FilePath unused; - base::ScopedFILE fp( + util::ScopedFILE fp( CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); EXPECT_EQ(sizeof(kFooBar), fwrite(kFooBar, 1, sizeof(kFooBar), fp.get()));
diff --git a/mojo/edk/system/raw_channel_unittest.cc b/mojo/edk/system/raw_channel_unittest.cc index 9f7838c..db11b5b 100644 --- a/mojo/edk/system/raw_channel_unittest.cc +++ b/mojo/edk/system/raw_channel_unittest.cc
@@ -12,7 +12,6 @@ #include "base/bind.h" #include "base/files/file_path.h" #include "base/files/file_util.h" -#include "base/files/scoped_file.h" #include "base/files/scoped_temp_dir.h" #include "base/location.h" #include "base/logging.h" @@ -30,6 +29,7 @@ #include "mojo/edk/system/transport_data.h" #include "mojo/edk/test/test_io_thread.h" #include "mojo/edk/test/test_utils.h" +#include "mojo/edk/util/scoped_file.h" #include "mojo/public/cpp/system/macros.h" #include "testing/gtest/include/gtest/gtest.h" @@ -763,7 +763,7 @@ { char buffer[100] = {}; - base::ScopedFILE fp(mojo::test::FILEFromPlatformHandle(h1.Pass(), "rb")); + util::ScopedFILE fp(mojo::test::FILEFromPlatformHandle(h1.Pass(), "rb")); EXPECT_TRUE(fp); rewind(fp.get()); EXPECT_EQ(1u, fread(buffer, 1, sizeof(buffer), fp.get())); @@ -772,7 +772,7 @@ { char buffer[100] = {}; - base::ScopedFILE fp(mojo::test::FILEFromPlatformHandle(h2.Pass(), "rb")); + util::ScopedFILE fp(mojo::test::FILEFromPlatformHandle(h2.Pass(), "rb")); EXPECT_TRUE(fp); rewind(fp.get()); EXPECT_EQ(1u, fread(buffer, 1, sizeof(buffer), fp.get())); @@ -817,10 +817,10 @@ base::Unretained(&read_delegate))); base::FilePath unused; - base::ScopedFILE fp1( + util::ScopedFILE fp1( base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); EXPECT_EQ(1u, fwrite("1", 1, 1, fp1.get())); - base::ScopedFILE fp2( + util::ScopedFILE fp2( base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); EXPECT_EQ(1u, fwrite("2", 1, 1, fp2.get()));
diff --git a/mojo/edk/system/remote_message_pipe_unittest.cc b/mojo/edk/system/remote_message_pipe_unittest.cc index 70d0960..25b91c0 100644 --- a/mojo/edk/system/remote_message_pipe_unittest.cc +++ b/mojo/edk/system/remote_message_pipe_unittest.cc
@@ -11,7 +11,6 @@ #include "base/bind.h" #include "base/files/file_path.h" #include "base/files/file_util.h" -#include "base/files/scoped_file.h" #include "base/files/scoped_temp_dir.h" #include "base/location.h" #include "base/logging.h" @@ -34,6 +33,7 @@ #include "mojo/edk/system/waiter.h" #include "mojo/edk/test/test_io_thread.h" #include "mojo/edk/test/test_utils.h" +#include "mojo/edk/util/scoped_file.h" #include "mojo/public/cpp/system/macros.h" #include "testing/gtest/include/gtest/gtest.h" @@ -1040,7 +1040,7 @@ BootstrapChannelEndpoints(ep0, ep1); base::FilePath unused; - base::ScopedFILE fp( + util::ScopedFILE fp( base::CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); EXPECT_EQ(sizeof(kHello), fwrite(kHello, 1, sizeof(kHello), fp.get())); // We'll try to pass this dispatcher, which will cause a |PlatformHandle| to
diff --git a/mojo/edk/test/BUILD.gn b/mojo/edk/test/BUILD.gn index e07bbae..225fc6b 100644 --- a/mojo/edk/test/BUILD.gn +++ b/mojo/edk/test/BUILD.gn
@@ -26,7 +26,10 @@ "//testing/gtest", ] - mojo_edk_deps = [ "mojo/edk/system" ] + mojo_edk_deps = [ + "mojo/edk/system", + "mojo/edk/util", + ] mojo_sdk_public_deps = [ "mojo/public/cpp/system" ] }
diff --git a/mojo/edk/test/test_utils.h b/mojo/edk/test/test_utils.h index 9c21a1a..fad57ba 100644 --- a/mojo/edk/test/test_utils.h +++ b/mojo/edk/test/test_utils.h
@@ -10,9 +10,9 @@ #include <string> -#include "base/files/scoped_file.h" #include "mojo/edk/embedder/platform_handle.h" #include "mojo/edk/embedder/scoped_platform_handle.h" +#include "mojo/edk/util/scoped_file.h" namespace mojo { namespace test { @@ -41,10 +41,10 @@ size_t* bytes_read); // Gets a (scoped) |PlatformHandle| from the given (scoped) |FILE|. -embedder::ScopedPlatformHandle PlatformHandleFromFILE(base::ScopedFILE fp); +embedder::ScopedPlatformHandle PlatformHandleFromFILE(util::ScopedFILE fp); // Gets a (scoped) |FILE| from a (scoped) |PlatformHandle|. -base::ScopedFILE FILEFromPlatformHandle(embedder::ScopedPlatformHandle h, +util::ScopedFILE FILEFromPlatformHandle(embedder::ScopedPlatformHandle h, const char* mode); } // namespace test
diff --git a/mojo/edk/test/test_utils_posix.cc b/mojo/edk/test/test_utils_posix.cc index 182aa33..46f50e2 100644 --- a/mojo/edk/test/test_utils_posix.cc +++ b/mojo/edk/test/test_utils_posix.cc
@@ -7,6 +7,7 @@ #include <fcntl.h> #include <unistd.h> +#include "base/logging.h" #include "base/posix/eintr_wrapper.h" namespace mojo { @@ -72,17 +73,17 @@ return true; } -embedder::ScopedPlatformHandle PlatformHandleFromFILE(base::ScopedFILE fp) { +embedder::ScopedPlatformHandle PlatformHandleFromFILE(util::ScopedFILE fp) { CHECK(fp); int rv = dup(fileno(fp.get())); PCHECK(rv != -1) << "dup"; return embedder::ScopedPlatformHandle(embedder::PlatformHandle(rv)); } -base::ScopedFILE FILEFromPlatformHandle(embedder::ScopedPlatformHandle h, +util::ScopedFILE FILEFromPlatformHandle(embedder::ScopedPlatformHandle h, const char* mode) { CHECK(h.is_valid()); - base::ScopedFILE rv(fdopen(h.release().fd, mode)); + util::ScopedFILE rv(fdopen(h.release().fd, mode)); PCHECK(rv) << "fdopen"; return rv.Pass(); }
diff --git a/mojo/edk/test/test_utils_win.cc b/mojo/edk/test/test_utils_win.cc index c74f009..0750cba 100644 --- a/mojo/edk/test/test_utils_win.cc +++ b/mojo/edk/test/test_utils_win.cc
@@ -77,7 +77,7 @@ return true; } -embedder::ScopedPlatformHandle PlatformHandleFromFILE(base::ScopedFILE fp) { +embedder::ScopedPlatformHandle PlatformHandleFromFILE(util::ScopedFILE fp) { CHECK(fp); HANDLE rv = INVALID_HANDLE_VALUE; @@ -89,7 +89,7 @@ return embedder::ScopedPlatformHandle(embedder::PlatformHandle(rv)); } -base::ScopedFILE FILEFromPlatformHandle(embedder::ScopedPlatformHandle h, +util::ScopedFILE FILEFromPlatformHandle(embedder::ScopedPlatformHandle h, const char* mode) { CHECK(h.is_valid()); // Microsoft's documentation for |_open_osfhandle()| only discusses these @@ -101,7 +101,7 @@ flags |= _O_RDONLY; if (strchr(mode, 't')) flags |= _O_TEXT; - base::ScopedFILE rv(_fdopen( + util::ScopedFILE rv(_fdopen( _open_osfhandle(reinterpret_cast<intptr_t>(h.release().handle), flags), mode)); PCHECK(rv) << "_fdopen";
diff --git a/mojo/edk/util/BUILD.gn b/mojo/edk/util/BUILD.gn new file mode 100644 index 0000000..455a6c6 --- /dev/null +++ b/mojo/edk/util/BUILD.gn
@@ -0,0 +1,22 @@ +# Copyright 2015 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. + +import("../mojo_edk.gni") + +mojo_edk_source_set("util") { + sources = [ + "scoped_file.h", + ] + + defines = [ + "MOJO_SYSTEM_IMPL_IMPLEMENTATION", + "MOJO_SYSTEM_IMPLEMENTATION", + ] + + mojo_edk_configs = [ "mojo/edk/system:system_config" ] + + deps = [ + "//base", + ] +}
diff --git a/mojo/edk/util/scoped_file.h b/mojo/edk/util/scoped_file.h new file mode 100644 index 0000000..7aeb868 --- /dev/null +++ b/mojo/edk/util/scoped_file.h
@@ -0,0 +1,32 @@ +// Copyright 2014 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 MOJO_EDK_UTIL_SCOPED_FILE_H_ +#define MOJO_EDK_UTIL_SCOPED_FILE_H_ + +#include <stdio.h> + +#include "base/memory/scoped_ptr.h" + +namespace mojo { +namespace util { +namespace internal { + +// Functor for |ScopedFILE| (below). +struct ScopedFILECloser { + inline void operator()(FILE* x) const { + if (x) + fclose(x); + } +}; + +} // namespace internal + +// Automatically closes |FILE*|s. +using ScopedFILE = scoped_ptr<FILE, internal::ScopedFILECloser>; + +} // namespace util +} // namespace mojo + +#endif // MOJO_EDK_UTIL_SCOPED_FILE_H_