diff options
author | rvargas@chromium.org <rvargas@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-18 17:56:38 +0000 |
---|---|---|
committer | rvargas@chromium.org <rvargas@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-18 17:56:38 +0000 |
commit | e49597018bf66ca87d3e864482f64473ea92399e (patch) | |
tree | 0139a446aad778c6c84addafa8c4ec9036d56b23 /chrome/browser/extensions/api | |
parent | bff4ff94f311adae0b622603029efdf4a8a8df2c (diff) | |
download | chromium_src-e49597018bf66ca87d3e864482f64473ea92399e.zip chromium_src-e49597018bf66ca87d3e864482f64473ea92399e.tar.gz chromium_src-e49597018bf66ca87d3e864482f64473ea92399e.tar.bz2 |
Remove CreatePlatformFile from browser/extensions
BUG=322664
Review URL: https://codereview.chromium.org/193603002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@257686 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions/api')
14 files changed, 156 insertions, 160 deletions
diff --git a/chrome/browser/extensions/api/file_handlers/app_file_handler_util.cc b/chrome/browser/extensions/api/file_handlers/app_file_handler_util.cc index 9d0a5cd..f79af9f 100644 --- a/chrome/browser/extensions/api/file_handlers/app_file_handler_util.cc +++ b/chrome/browser/extensions/api/file_handlers/app_file_handler_util.cc @@ -5,6 +5,7 @@ #include "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h" #include "base/file_util.h" +#include "base/files/file.h" #include "base/files/file_path.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/child_process_security_policy.h" @@ -73,21 +74,12 @@ bool DoCheckWritableFile(const base::FilePath& path, bool is_directory) { return base::DirectoryExists(path); // Create the file if it doesn't already exist. - base::PlatformFileError error = base::PLATFORM_FILE_OK; - int creation_flags = base::PLATFORM_FILE_CREATE | - base::PLATFORM_FILE_READ | - base::PLATFORM_FILE_WRITE; - base::PlatformFile file = base::CreatePlatformFile(path, creation_flags, - NULL, &error); - // Close the file so we don't keep a lock open. - if (file != base::kInvalidPlatformFileValue) - base::ClosePlatformFile(file); - if (error != base::PLATFORM_FILE_OK && - error != base::PLATFORM_FILE_ERROR_EXISTS) { - return false; - } - - return true; + int creation_flags = base::File::FLAG_CREATE | base::File::FLAG_READ | + base::File::FLAG_WRITE; + base::File file(path, creation_flags); + if (file.IsValid()) + return true; + return file.error_details() == base::File::FILE_ERROR_EXISTS; } // Checks whether a list of paths are all OK for writing and calls a provided diff --git a/chrome/browser/extensions/api/image_writer_private/operation.cc b/chrome/browser/extensions/api/image_writer_private/operation.cc index fdf7cf7..1ad4160 100644 --- a/chrome/browser/extensions/api/image_writer_private/operation.cc +++ b/chrome/browser/extensions/api/image_writer_private/operation.cc @@ -2,12 +2,12 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "chrome/browser/extensions/api/image_writer_private/operation.h" + #include "base/file_util.h" #include "base/files/file_enumerator.h" -#include "base/platform_file.h" #include "base/threading/worker_pool.h" #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" -#include "chrome/browser/extensions/api/image_writer_private/operation.h" #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h" #include "content/public/browser/browser_thread.h" @@ -284,18 +284,15 @@ void Operation::GetMD5SumOfFile( base::MD5Init(&md5_context_); - base::PlatformFile file = base::CreatePlatformFile( - file_path, - base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, - NULL, - NULL); - if (file == base::kInvalidPlatformFileValue) { + base::File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); + if (!file.IsValid()) { Error(error::kImageOpenError); return; } if (file_size <= 0) { - if (!base::GetFileSize(file_path, &file_size)) { + file_size = file.GetLength(); + if (file_size < 0) { Error(error::kImageOpenError); return; } @@ -305,7 +302,7 @@ void Operation::GetMD5SumOfFile( FROM_HERE, base::Bind(&Operation::MD5Chunk, this, - file, + Passed(file.Pass()), 0, file_size, progress_offset, @@ -314,16 +311,14 @@ void Operation::GetMD5SumOfFile( } void Operation::MD5Chunk( - const base::PlatformFile& file, + base::File file, int64 bytes_processed, int64 bytes_total, int progress_offset, int progress_scale, const base::Callback<void(const std::string&)>& callback) { - if (IsCancelled()) { - base::ClosePlatformFile(file); + if (IsCancelled()) return; - } CHECK_LE(bytes_processed, bytes_total); @@ -337,8 +332,7 @@ void Operation::MD5Chunk( base::MD5Final(&digest, &md5_context_); callback.Run(base::MD5DigestToBase16(digest)); } else { - int len = - base::ReadPlatformFile(file, bytes_processed, buffer.get(), read_size); + int len = file.Read(bytes_processed, buffer.get(), read_size); if (len == read_size) { // Process data. @@ -352,7 +346,7 @@ void Operation::MD5Chunk( FROM_HERE, base::Bind(&Operation::MD5Chunk, this, - file, + Passed(file.Pass()), bytes_processed + len, bytes_total, progress_offset, @@ -365,7 +359,6 @@ void Operation::MD5Chunk( Error(error::kHashReadError); } } - base::ClosePlatformFile(file); } void Operation::OnUnzipFailure() { diff --git a/chrome/browser/extensions/api/image_writer_private/operation.h b/chrome/browser/extensions/api/image_writer_private/operation.h index 1c9933f..2991cf77 100644 --- a/chrome/browser/extensions/api/image_writer_private/operation.h +++ b/chrome/browser/extensions/api/image_writer_private/operation.h @@ -6,6 +6,7 @@ #define CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_OPERATION_H_ #include "base/callback.h" +#include "base/files/file.h" #include "base/files/scoped_temp_dir.h" #include "base/md5.h" #include "base/memory/ref_counted_memory.h" @@ -176,7 +177,7 @@ class Operation : public base::RefCountedThreadSafe<Operation> { #endif // Incrementally calculates the MD5 sum of a file. - void MD5Chunk(const base::PlatformFile& file, + void MD5Chunk(base::File file, int64 bytes_processed, int64 bytes_total, int progress_offset, diff --git a/chrome/browser/extensions/api/messaging/native_message_process_host.cc b/chrome/browser/extensions/api/messaging/native_message_process_host.cc index 4e31c7c..170a25e 100644 --- a/chrome/browser/extensions/api/messaging/native_message_process_host.cc +++ b/chrome/browser/extensions/api/messaging/native_message_process_host.cc @@ -104,7 +104,9 @@ NativeMessageProcessHost::NativeMessageProcessHost( launcher_(launcher.Pass()), closed_(false), process_handle_(base::kNullProcessHandle), +#if defined(OS_POSIX) read_file_(base::kInvalidPlatformFileValue), +#endif read_pending_(false), write_pending_(false) { DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); @@ -164,8 +166,8 @@ void NativeMessageProcessHost::LaunchHostProcess() { void NativeMessageProcessHost::OnHostProcessLaunched( NativeProcessLauncher::LaunchResult result, base::ProcessHandle process_handle, - base::PlatformFile read_file, - base::PlatformFile write_file) { + base::File read_file, + base::File write_file) { DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); switch (result) { @@ -186,7 +188,10 @@ void NativeMessageProcessHost::OnHostProcessLaunched( } process_handle_ = process_handle; - read_file_ = read_file; +#if defined(OS_POSIX) + // This object is not the owner of the file so it should not keep an fd. + read_file_ = read_file.GetPlatformFile(); +#endif scoped_refptr<base::TaskRunner> task_runner( content::BrowserThread::GetBlockingPool()-> @@ -194,10 +199,12 @@ void NativeMessageProcessHost::OnHostProcessLaunched( base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)); read_stream_.reset(new net::FileStream( - read_file, base::PLATFORM_FILE_READ | base::PLATFORM_FILE_ASYNC, NULL, + read_file.TakePlatformFile(), + base::PLATFORM_FILE_READ | base::PLATFORM_FILE_ASYNC, NULL, task_runner)); write_stream_.reset(new net::FileStream( - write_file, base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_ASYNC, NULL, + write_file.TakePlatformFile(), + base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_ASYNC, NULL, task_runner)); WaitRead(); @@ -256,8 +263,8 @@ void NativeMessageProcessHost::WaitRead() { // FileStream uses overlapped IO, so that optimization isn't necessary there. #if defined(OS_POSIX) base::MessageLoopForIO::current()->WatchFileDescriptor( - read_file_, false /* persistent */, base::MessageLoopForIO::WATCH_READ, - &read_watcher_, this); + read_file_, false /* persistent */, + base::MessageLoopForIO::WATCH_READ, &read_watcher_, this); #else // defined(OS_POSIX) DoRead(); #endif // defined(!OS_POSIX) diff --git a/chrome/browser/extensions/api/messaging/native_message_process_host.h b/chrome/browser/extensions/api/messaging/native_message_process_host.h index cc67322..482d3b0 100644 --- a/chrome/browser/extensions/api/messaging/native_message_process_host.h +++ b/chrome/browser/extensions/api/messaging/native_message_process_host.h @@ -106,8 +106,8 @@ class NativeMessageProcessHost // Callback for NativeProcessLauncher::Launch(). void OnHostProcessLaunched(NativeProcessLauncher::LaunchResult result, base::ProcessHandle process_handle, - base::PlatformFile read_file, - base::PlatformFile write_file); + base::File read_file, + base::File write_file); // Helper methods to read incoming messages. void WaitRead(); @@ -147,11 +147,13 @@ class NativeMessageProcessHost base::ProcessHandle process_handle_; - // Input stream handle and reader. - base::PlatformFile read_file_; + // Input stream reader. scoped_ptr<net::FileStream> read_stream_; #if defined(OS_POSIX) + // TODO(rvargas): Remove these members, maybe merging the functionality to + // net::FileStream. + base::PlatformFile read_file_; base::MessageLoopForIO::FileDescriptorWatcher read_watcher_; #endif // !defined(OS_POSIX) diff --git a/chrome/browser/extensions/api/messaging/native_message_process_host_unittest.cc b/chrome/browser/extensions/api/messaging/native_message_process_host_unittest.cc index cf5b80b..f04fe60 100644 --- a/chrome/browser/extensions/api/messaging/native_message_process_host_unittest.cc +++ b/chrome/browser/extensions/api/messaging/native_message_process_host_unittest.cc @@ -4,6 +4,7 @@ #include "base/bind.h" #include "base/file_util.h" +#include "base/files/file.h" #include "base/files/file_path.h" #include "base/files/scoped_file.h" #include "base/files/scoped_temp_dir.h" @@ -11,7 +12,6 @@ #include "base/memory/scoped_ptr.h" #include "base/memory/weak_ptr.h" #include "base/message_loop/message_loop.h" -#include "base/platform_file.h" #include "base/rand_util.h" #include "base/run_loop.h" #include "base/strings/stringprintf.h" @@ -48,42 +48,48 @@ namespace extensions { class FakeLauncher : public NativeProcessLauncher { public: - FakeLauncher(base::PlatformFile read_file, base::PlatformFile write_file) - : read_file_(read_file), - write_file_(write_file) { + FakeLauncher(base::File read_file, base::File write_file) + : read_file_(read_file.Pass()), + write_file_(write_file.Pass()) { } static scoped_ptr<NativeProcessLauncher> Create(base::FilePath read_file, - base::FilePath write_file) { - int read_flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ | - base::PLATFORM_FILE_ASYNC; - int write_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE | - base::PLATFORM_FILE_ASYNC; + base::FilePath write_file) { + int read_flags = base::File::FLAG_OPEN | base::File::FLAG_READ; + int write_flags = base::File::FLAG_CREATE | base::File::FLAG_WRITE; +#if !defined(OS_POSIX) + read_flags |= base::File::FLAG_ASYNC; + write_flags |= base::File::FLAG_ASYNC; +#endif return scoped_ptr<NativeProcessLauncher>(new FakeLauncher( - base::CreatePlatformFile(read_file, read_flags, NULL, NULL), - base::CreatePlatformFile(write_file, write_flags, NULL, NULL))); + base::File(read_file, read_flags), + base::File(write_file, write_flags))); } static scoped_ptr<NativeProcessLauncher> CreateWithPipeInput( - base::PlatformFile read_pipe, + base::File read_pipe, base::FilePath write_file) { - int write_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE | - base::PLATFORM_FILE_ASYNC; + int write_flags = base::File::FLAG_CREATE | base::File::FLAG_WRITE; +#if !defined(OS_POSIX) + write_flags |= base::File::FLAG_ASYNC; +#endif + return scoped_ptr<NativeProcessLauncher>(new FakeLauncher( - read_pipe, - base::CreatePlatformFile(write_file, write_flags, NULL, NULL))); + read_pipe.Pass(), + base::File(write_file, write_flags))); } virtual void Launch(const GURL& origin, const std::string& native_host_name, LaunchedCallback callback) const OVERRIDE { callback.Run(NativeProcessLauncher::RESULT_SUCCESS, - base::kNullProcessHandle, read_file_, write_file_); + base::kNullProcessHandle, + read_file_.Pass(), write_file_.Pass()); } private: - base::PlatformFile read_file_; - base::PlatformFile write_file_; + mutable base::File read_file_; + mutable base::File write_file_; }; class NativeMessagingTest : public ::testing::Test, @@ -196,33 +202,34 @@ TEST_F(NativeMessagingTest, SingleSendMessageRead) { TEST_F(NativeMessagingTest, SingleSendMessageWrite) { base::FilePath temp_output_file = temp_dir_.path().AppendASCII("output"); - base::PlatformFile read_file; + base::File read_file; #if defined(OS_WIN) base::string16 pipe_name = base::StringPrintf( L"\\\\.\\pipe\\chrome.nativeMessaging.out.%llx", base::RandUint64()); - base::win::ScopedHandle write_handle( + base::File write_handle( CreateNamedPipeW(pipe_name.c_str(), PIPE_ACCESS_OUTBOUND | FILE_FLAG_OVERLAPPED | FILE_FLAG_FIRST_PIPE_INSTANCE, PIPE_TYPE_BYTE, 1, 0, 0, 5000, NULL)); - ASSERT_TRUE(write_handle); - base::win::ScopedHandle read_handle( + ASSERT_TRUE(write_handle.IsValid()); + base::File read_handle( CreateFileW(pipe_name.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL)); - ASSERT_TRUE(read_handle); + ASSERT_TRUE(read_handle.IsValid()); - read_file = read_handle.Get(); + read_file = read_handle.Pass(); #else // defined(OS_WIN) base::PlatformFile pipe_handles[2]; ASSERT_EQ(0, pipe(pipe_handles)); base::ScopedFD read_fd(pipe_handles[0]); base::ScopedFD write_fd(pipe_handles[1]); - read_file = pipe_handles[0]; + read_file = base::File(pipe_handles[0]); #endif // !defined(OS_WIN) scoped_ptr<NativeProcessLauncher> launcher = - FakeLauncher::CreateWithPipeInput(read_file, temp_output_file).Pass(); + FakeLauncher::CreateWithPipeInput(read_file.Pass(), + temp_output_file).Pass(); native_message_process_host_ = NativeMessageProcessHost::CreateWithLauncher( AsWeakPtr(), ScopedTestNativeMessagingHost::kExtensionId, "empty_app.py", 0, launcher.Pass()); diff --git a/chrome/browser/extensions/api/messaging/native_process_launcher.cc b/chrome/browser/extensions/api/messaging/native_process_launcher.cc index edf29ee..32f4741 100644 --- a/chrome/browser/extensions/api/messaging/native_process_launcher.cc +++ b/chrome/browser/extensions/api/messaging/native_process_launcher.cc @@ -87,13 +87,13 @@ class NativeProcessLauncherImpl : public NativeProcessLauncher { void PostErrorResult(const LaunchedCallback& callback, LaunchResult error); void PostResult(const LaunchedCallback& callback, base::ProcessHandle process_handle, - base::PlatformFile read_file, - base::PlatformFile write_file); + base::File read_file, + base::File write_file); void CallCallbackOnIOThread(LaunchedCallback callback, LaunchResult result, base::ProcessHandle process_handle, - base::PlatformFile read_file, - base::PlatformFile write_file); + base::File read_file, + base::File write_file); bool detached_; @@ -212,11 +212,11 @@ void NativeProcessLauncherImpl::Core::DoLaunchOnThreadPool( #endif // !defined(OS_WIN) base::ProcessHandle process_handle; - base::PlatformFile read_file; - base::PlatformFile write_file; + base::File read_file; + base::File write_file; if (NativeProcessLauncher::LaunchNativeProcess( command_line, &process_handle, &read_file, &write_file)) { - PostResult(callback, process_handle, read_file, write_file); + PostResult(callback, process_handle, read_file.Pass(), write_file.Pass()); } else { PostErrorResult(callback, RESULT_FAILED_TO_START); } @@ -226,18 +226,13 @@ void NativeProcessLauncherImpl::Core::CallCallbackOnIOThread( LaunchedCallback callback, LaunchResult result, base::ProcessHandle process_handle, - base::PlatformFile read_file, - base::PlatformFile write_file) { + base::File read_file, + base::File write_file) { DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); - if (detached_) { - if (read_file != base::kInvalidPlatformFileValue) - base::ClosePlatformFile(read_file); - if (write_file != base::kInvalidPlatformFileValue) - base::ClosePlatformFile(write_file); + if (detached_) return; - } - callback.Run(result, process_handle, read_file, write_file); + callback.Run(result, process_handle, read_file.Pass(), write_file.Pass()); } void NativeProcessLauncherImpl::Core::PostErrorResult( @@ -247,20 +242,19 @@ void NativeProcessLauncherImpl::Core::PostErrorResult( content::BrowserThread::IO, FROM_HERE, base::Bind(&NativeProcessLauncherImpl::Core::CallCallbackOnIOThread, this, callback, error, base::kNullProcessHandle, - base::kInvalidPlatformFileValue, - base::kInvalidPlatformFileValue)); + Passed(base::File()), Passed(base::File()))); } void NativeProcessLauncherImpl::Core::PostResult( const LaunchedCallback& callback, base::ProcessHandle process_handle, - base::PlatformFile read_file, - base::PlatformFile write_file) { + base::File read_file, + base::File write_file) { content::BrowserThread::PostTask( content::BrowserThread::IO, FROM_HERE, base::Bind(&NativeProcessLauncherImpl::Core::CallCallbackOnIOThread, this, - callback, RESULT_SUCCESS, process_handle, read_file, - write_file)); + callback, RESULT_SUCCESS, process_handle, + Passed(read_file.Pass()), Passed(write_file.Pass()))); } NativeProcessLauncherImpl::NativeProcessLauncherImpl( diff --git a/chrome/browser/extensions/api/messaging/native_process_launcher.h b/chrome/browser/extensions/api/messaging/native_process_launcher.h index 6694bdc..db41309 100644 --- a/chrome/browser/extensions/api/messaging/native_process_launcher.h +++ b/chrome/browser/extensions/api/messaging/native_process_launcher.h @@ -6,8 +6,8 @@ #define CHROME_BROWSER_EXTENSIONS_API_MESSAGING_NATIVE_PROCESS_LAUNCHER_H_ #include "base/callback_forward.h" +#include "base/files/file.h" #include "base/memory/scoped_ptr.h" -#include "base/platform_file.h" #include "base/process/process.h" #include "ui/gfx/native_widget_types.h" @@ -33,10 +33,10 @@ class NativeProcessLauncher { // Callback that's called after the process has been launched. |result| is set // to false in case of a failure. Handler must take ownership of the IO // handles. - typedef base::Callback<void (LaunchResult result, - base::ProcessHandle process_handle, - base::PlatformFile read_file, - base::PlatformFile write_file)> LaunchedCallback; + typedef base::Callback<void(LaunchResult result, + base::ProcessHandle process_handle, + base::File read_file, + base::File write_file)> LaunchedCallback; static scoped_ptr<NativeProcessLauncher> CreateDefault( bool allow_user_level_hosts, @@ -69,8 +69,8 @@ class NativeProcessLauncher { // Launches native messaging process. static bool LaunchNativeProcess(const base::CommandLine& command_line, base::ProcessHandle* process_handle, - base::PlatformFile* read_file, - base::PlatformFile* write_file); + base::File* read_file, + base::File* write_file); private: DISALLOW_COPY_AND_ASSIGN(NativeProcessLauncher); diff --git a/chrome/browser/extensions/api/messaging/native_process_launcher_posix.cc b/chrome/browser/extensions/api/messaging/native_process_launcher_posix.cc index 70353d3..2a75aba 100644 --- a/chrome/browser/extensions/api/messaging/native_process_launcher_posix.cc +++ b/chrome/browser/extensions/api/messaging/native_process_launcher_posix.cc @@ -50,8 +50,8 @@ base::FilePath NativeProcessLauncher::FindManifest( bool NativeProcessLauncher::LaunchNativeProcess( const CommandLine& command_line, base::ProcessHandle* process_handle, - base::PlatformFile* read_file, - base::PlatformFile* write_file) { + base::File* read_file, + base::File* write_file) { base::FileHandleMappingVector fd_map; int read_pipe_fds[2] = {0}; @@ -83,8 +83,8 @@ bool NativeProcessLauncher::LaunchNativeProcess( write_pipe_read_fd.reset(); read_pipe_write_fd.reset(); - *read_file = read_pipe_read_fd.release(); - *write_file = write_pipe_write_fd.release(); + *read_file = base::File(read_pipe_read_fd.release()); + *write_file = base::File(write_pipe_write_fd.release()); return true; } diff --git a/chrome/browser/extensions/api/messaging/native_process_launcher_win.cc b/chrome/browser/extensions/api/messaging/native_process_launcher_win.cc index 7e30427ae..59b1b39 100644 --- a/chrome/browser/extensions/api/messaging/native_process_launcher_win.cc +++ b/chrome/browser/extensions/api/messaging/native_process_launcher_win.cc @@ -90,8 +90,8 @@ base::FilePath NativeProcessLauncher::FindManifest( bool NativeProcessLauncher::LaunchNativeProcess( const CommandLine& command_line, base::ProcessHandle* process_handle, - base::PlatformFile* read_file, - base::PlatformFile* write_file) { + base::File* read_file, + base::File* write_file) { // Timeout for the IO pipes. const DWORD kTimeoutMs = 5000; @@ -170,8 +170,8 @@ bool NativeProcessLauncher::LaunchNativeProcess( } *process_handle = cmd_handle.Take(); - *read_file = stdout_pipe.Take(); - *write_file = stdin_pipe.Take(); + *read_file = base::File(stdout_pipe.Take()); + *write_file = base::File(stdin_pipe.Take()); return true; } diff --git a/chrome/browser/extensions/api/serial/serial_connection.cc b/chrome/browser/extensions/api/serial/serial_connection.cc index 281cdda..aee95a2 100644 --- a/chrome/browser/extensions/api/serial/serial_connection.cc +++ b/chrome/browser/extensions/api/serial/serial_connection.cc @@ -8,7 +8,6 @@ #include "base/files/file_path.h" #include "base/lazy_instance.h" -#include "base/platform_file.h" #include "base/strings/string_util.h" #include "chrome/common/extensions/api/serial.h" #include "extensions/browser/api/api_resource_manager.h" @@ -35,7 +34,6 @@ SerialConnection::SerialConnection(const std::string& port, const std::string& owner_extension_id) : ApiResource(owner_extension_id), port_(port), - file_(base::kInvalidPlatformFileValue), persistent_(false), buffer_size_(kDefaultBufferSize), receive_timeout_(0), @@ -86,12 +84,11 @@ void SerialConnection::Open(const OpenCompleteCallback& callback) { void SerialConnection::Close() { DCHECK(open_complete_.is_null()); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); - if (file_ != base::kInvalidPlatformFileValue) { - base::PlatformFile file = file_; - file_ = base::kInvalidPlatformFileValue; - BrowserThread::PostTask(BrowserThread::FILE, - FROM_HERE, - base::Bind(&SerialConnection::DoClose, file)); + if (file_.IsValid()) { + BrowserThread::PostTask( + BrowserThread::FILE, + FROM_HERE, + base::Bind(&SerialConnection::DoClose, Passed(file_.Pass()))); } } @@ -163,40 +160,42 @@ bool SerialConnection::GetInfo(api::serial::ConnectionInfo* info) const { void SerialConnection::StartOpen() { DCHECK(!open_complete_.is_null()); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); - DCHECK_EQ(file_, base::kInvalidPlatformFileValue); - base::PlatformFile file = base::kInvalidPlatformFileValue; + DCHECK(!file_.IsValid()); // It's the responsibility of the API wrapper around SerialConnection to // validate the supplied path against the set of valid port names, and // it is a reasonable assumption that serial port names are ASCII. DCHECK(IsStringASCII(port_)); base::FilePath path( base::FilePath::FromUTF8Unsafe(MaybeFixUpPortName(port_))); - int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ | - base::PLATFORM_FILE_EXCLUSIVE_READ | base::PLATFORM_FILE_WRITE | - base::PLATFORM_FILE_EXCLUSIVE_WRITE | base::PLATFORM_FILE_ASYNC | - base::PLATFORM_FILE_TERMINAL_DEVICE; - file = base::CreatePlatformFile(path, flags, NULL, NULL); + int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | + base::File::FLAG_EXCLUSIVE_READ | base::File::FLAG_WRITE | + base::File::FLAG_EXCLUSIVE_WRITE | base::File::FLAG_ASYNC | + base::File::FLAG_TERMINAL_DEVICE; + base::File file(path, flags); BrowserThread::PostTask( BrowserThread::IO, FROM_HERE, - base::Bind(&SerialConnection::FinishOpen, base::Unretained(this), file)); + base::Bind(&SerialConnection::FinishOpen, base::Unretained(this), + Passed(file.Pass()))); } -void SerialConnection::FinishOpen(base::PlatformFile file) { +void SerialConnection::FinishOpen(base::File file) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); DCHECK(!open_complete_.is_null()); - DCHECK_EQ(file_, base::kInvalidPlatformFileValue); + DCHECK(!file_.IsValid()); OpenCompleteCallback callback = open_complete_; open_complete_.Reset(); - if (file == base::kInvalidPlatformFileValue) { + if (!file.IsValid()) { callback.Run(false); return; } - file_ = file; + // TODO(rvargas): crbug.com/351073. This is wrong. io_handler_ keeps a copy of + // the handler that is not in sync with this one. + file_ = file.Pass(); io_handler_->Initialize( - file_, + file_.GetPlatformFile(), base::Bind(&SerialConnection::OnAsyncReadComplete, AsWeakPtr()), base::Bind(&SerialConnection::OnAsyncWriteComplete, AsWeakPtr())); @@ -209,11 +208,9 @@ void SerialConnection::FinishOpen(base::PlatformFile file) { } // static -void SerialConnection::DoClose(base::PlatformFile port) { +void SerialConnection::DoClose(base::File port) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); - if (port != base::kInvalidPlatformFileValue) { - base::ClosePlatformFile(port); - } + // port closed by destructor. } void SerialConnection::OnReceiveTimeout() { diff --git a/chrome/browser/extensions/api/serial/serial_connection.h b/chrome/browser/extensions/api/serial/serial_connection.h index 135561d..b190f6f 100644 --- a/chrome/browser/extensions/api/serial/serial_connection.h +++ b/chrome/browser/extensions/api/serial/serial_connection.h @@ -9,10 +9,10 @@ #include <string> #include "base/callback.h" +#include "base/files/file.h" #include "base/memory/ref_counted.h" #include "base/memory/weak_ptr.h" #include "base/message_loop/message_loop.h" -#include "base/platform_file.h" #include "base/time/time.h" #include "chrome/browser/extensions/api/serial/serial_io_handler.h" #include "chrome/common/extensions/api/serial.h" @@ -164,10 +164,10 @@ class SerialConnection : public ApiResource, void StartOpen(); // Finalizes an Open operation (continued from StartOpen) on the IO thread. - void FinishOpen(base::PlatformFile file); + void FinishOpen(base::File file); // Continues a Close operation on the FILE thread. - static void DoClose(base::PlatformFile port); + static void DoClose(base::File port); // Handles a receive timeout. void OnReceiveTimeout(); @@ -185,9 +185,9 @@ class SerialConnection : public ApiResource, // The pathname of the serial device. std::string port_; - // File handle for the opened serial device. This value is only modified from - // the IO thread. - base::PlatformFile file_; + // File for the opened serial device. This value is only modified from the IO + // thread. + base::File file_; // Flag indicating whether or not the connection should persist when // its host app is suspended. diff --git a/chrome/browser/extensions/api/serial/serial_connection_posix.cc b/chrome/browser/extensions/api/serial/serial_connection_posix.cc index 68b5f20..f8d2d56 100644 --- a/chrome/browser/extensions/api/serial/serial_connection_posix.cc +++ b/chrome/browser/extensions/api/serial/serial_connection_posix.cc @@ -116,7 +116,7 @@ bool SetCustomBitrate(base::PlatformFile file, bool SerialConnection::ConfigurePort( const api::serial::ConnectionOptions& options) { struct termios config; - tcgetattr(file_, &config); + tcgetattr(file_.GetPlatformFile(), &config); if (options.bitrate.get()) { if (*options.bitrate >= 0) { speed_t bitrate_opt = B0; @@ -125,7 +125,8 @@ bool SerialConnection::ConfigurePort( cfsetospeed(&config, bitrate_opt); } else { // Attempt to set a custom speed. - if (!SetCustomBitrate(file_, &config, *options.bitrate)) { + if (!SetCustomBitrate(file_.GetPlatformFile(), &config, + *options.bitrate)) { return false; } } @@ -176,12 +177,12 @@ bool SerialConnection::ConfigurePort( config.c_cflag &= ~CRTSCTS; } } - return tcsetattr(file_, TCSANOW, &config) == 0; + return tcsetattr(file_.GetPlatformFile(), TCSANOW, &config) == 0; } bool SerialConnection::PostOpen() { struct termios config; - tcgetattr(file_, &config); + tcgetattr(file_.GetPlatformFile(), &config); // Set flags for 'raw' operation config.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHONL | ISIG); @@ -193,17 +194,17 @@ bool SerialConnection::PostOpen() { // CREAD enables reading from the port. config.c_cflag |= (CLOCAL | CREAD); - return tcsetattr(file_, TCSANOW, &config) == 0; + return tcsetattr(file_.GetPlatformFile(), TCSANOW, &config) == 0; } bool SerialConnection::Flush() const { - return tcflush(file_, TCIOFLUSH) == 0; + return tcflush(file_.GetPlatformFile(), TCIOFLUSH) == 0; } bool SerialConnection::GetControlSignals( api::serial::DeviceControlSignals* signals) const { int status; - if (ioctl(file_, TIOCMGET, &status) == -1) { + if (ioctl(file_.GetPlatformFile(), TIOCMGET, &status) == -1) { return false; } @@ -218,7 +219,7 @@ bool SerialConnection::SetControlSignals( const api::serial::HostControlSignals& signals) { int status; - if (ioctl(file_, TIOCMGET, &status) == -1) { + if (ioctl(file_.GetPlatformFile(), TIOCMGET, &status) == -1) { return false; } @@ -238,12 +239,12 @@ bool SerialConnection::SetControlSignals( } } - return ioctl(file_, TIOCMSET, &status) == 0; + return ioctl(file_.GetPlatformFile(), TIOCMSET, &status) == 0; } bool SerialConnection::GetPortInfo(api::serial::ConnectionInfo* info) const { struct termios config; - if (tcgetattr(file_, &config) == -1) { + if (tcgetattr(file_.GetPlatformFile(), &config) == -1) { return false; } speed_t ispeed = cfgetispeed(&config); diff --git a/chrome/browser/extensions/api/serial/serial_connection_win.cc b/chrome/browser/extensions/api/serial/serial_connection_win.cc index e46f857..35cded8 100644 --- a/chrome/browser/extensions/api/serial/serial_connection_win.cc +++ b/chrome/browser/extensions/api/serial/serial_connection_win.cc @@ -134,7 +134,7 @@ bool SerialConnection::ConfigurePort( const api::serial::ConnectionOptions& options) { DCB config = { 0 }; config.DCBlength = sizeof(config); - if (!GetCommState(file_, &config)) { + if (!GetCommState(file_.GetPlatformFile(), &config)) { return false; } if (options.bitrate.get()) @@ -154,7 +154,7 @@ bool SerialConnection::ConfigurePort( config.fRtsControl = RTS_CONTROL_ENABLE; } } - return SetCommState(file_, &config) != 0; + return SetCommState(file_.GetPlatformFile(), &config) != 0; } bool SerialConnection::PostOpen() { @@ -164,13 +164,13 @@ bool SerialConnection::PostOpen() { // signals that data is available. COMMTIMEOUTS timeouts = { 0 }; timeouts.ReadIntervalTimeout = MAXDWORD; - if (!::SetCommTimeouts(file_, &timeouts)) { + if (!::SetCommTimeouts(file_.GetPlatformFile(), &timeouts)) { return false; } DCB config = { 0 }; config.DCBlength = sizeof(config); - if (!GetCommState(file_, &config)) { + if (!GetCommState(file_.GetPlatformFile(), &config)) { return false; } // Setup some sane default state. @@ -184,17 +184,17 @@ bool SerialConnection::PostOpen() { config.fDsrSensitivity = FALSE; config.fOutX = FALSE; config.fInX = FALSE; - return SetCommState(file_, &config) != 0; + return SetCommState(file_.GetPlatformFile(), &config) != 0; } bool SerialConnection::Flush() const { - return PurgeComm(file_, PURGE_RXCLEAR | PURGE_TXCLEAR) != 0; + return PurgeComm(file_.GetPlatformFile(), PURGE_RXCLEAR | PURGE_TXCLEAR) != 0; } bool SerialConnection::GetControlSignals( api::serial::DeviceControlSignals* signals) const { DWORD status; - if (!GetCommModemStatus(file_, &status)) { + if (!GetCommModemStatus(file_.GetPlatformFile(), &status)) { return false; } signals->dcd = (status & MS_RLSD_ON) != 0; @@ -207,12 +207,14 @@ bool SerialConnection::GetControlSignals( bool SerialConnection::SetControlSignals( const api::serial::HostControlSignals& signals) { if (signals.dtr.get()) { - if (!EscapeCommFunction(file_, *signals.dtr ? SETDTR : CLRDTR)) { + if (!EscapeCommFunction(file_.GetPlatformFile(), + *signals.dtr ? SETDTR : CLRDTR)) { return false; } } if (signals.rts.get()) { - if (!EscapeCommFunction(file_, *signals.rts ? SETRTS : CLRRTS)) { + if (!EscapeCommFunction(file_.GetPlatformFile(), + *signals.rts ? SETRTS : CLRRTS)) { return false; } } @@ -222,7 +224,7 @@ bool SerialConnection::SetControlSignals( bool SerialConnection::GetPortInfo(api::serial::ConnectionInfo* info) const { DCB config = { 0 }; config.DCBlength = sizeof(config); - if (!GetCommState(file_, &config)) { + if (!GetCommState(file_.GetPlatformFile(), &config)) { return false; } info->bitrate.reset(new int(SpeedConstantToBitrate(config.BaudRate))); |