Web MIDI: send back error information to blink on starting sessions
[chromium-blink-merge.git] / net / base / file_stream_context_posix.cc
blobecf92855be3370c207842d6b2fe9a3004f1952e9
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 // For 64-bit file access (off_t = off64_t, lseek64, etc).
6 #define _FILE_OFFSET_BITS 64
8 #include "net/base/file_stream_context.h"
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <sys/stat.h>
13 #include <sys/types.h>
14 #include <unistd.h>
16 #include "base/basictypes.h"
17 #include "base/bind.h"
18 #include "base/bind_helpers.h"
19 #include "base/callback.h"
20 #include "base/files/file_path.h"
21 #include "base/location.h"
22 #include "base/logging.h"
23 #include "base/metrics/histogram.h"
24 #include "base/posix/eintr_wrapper.h"
25 #include "base/task_runner_util.h"
26 #include "net/base/io_buffer.h"
27 #include "net/base/net_errors.h"
29 #if defined(OS_ANDROID)
30 // Android's bionic libc only supports the LFS transitional API.
31 #define off_t off64_t
32 #define lseek lseek64
33 #define stat stat64
34 #define fstat fstat64
35 #endif
37 namespace net {
39 // We cast back and forth, so make sure it's the size we're expecting.
40 COMPILE_ASSERT(sizeof(int64) == sizeof(off_t), off_t_64_bit);
42 // Make sure our Whence mappings match the system headers.
43 COMPILE_ASSERT(FROM_BEGIN == SEEK_SET &&
44 FROM_CURRENT == SEEK_CUR &&
45 FROM_END == SEEK_END, whence_matches_system);
47 FileStream::Context::Context(const scoped_refptr<base::TaskRunner>& task_runner)
48 : async_in_progress_(false),
49 orphaned_(false),
50 task_runner_(task_runner) {
53 FileStream::Context::Context(base::File file,
54 const scoped_refptr<base::TaskRunner>& task_runner)
55 : file_(file.Pass()),
56 async_in_progress_(false),
57 orphaned_(false),
58 task_runner_(task_runner) {
61 FileStream::Context::~Context() {
64 int FileStream::Context::ReadAsync(IOBuffer* in_buf,
65 int buf_len,
66 const CompletionCallback& callback) {
67 DCHECK(!async_in_progress_);
69 scoped_refptr<IOBuffer> buf = in_buf;
70 const bool posted = base::PostTaskAndReplyWithResult(
71 task_runner_.get(),
72 FROM_HERE,
73 base::Bind(&Context::ReadFileImpl, base::Unretained(this), buf, buf_len),
74 base::Bind(&Context::OnAsyncCompleted,
75 base::Unretained(this),
76 IntToInt64(callback)));
77 DCHECK(posted);
79 async_in_progress_ = true;
80 return ERR_IO_PENDING;
83 int FileStream::Context::WriteAsync(IOBuffer* in_buf,
84 int buf_len,
85 const CompletionCallback& callback) {
86 DCHECK(!async_in_progress_);
88 scoped_refptr<IOBuffer> buf = in_buf;
89 const bool posted = base::PostTaskAndReplyWithResult(
90 task_runner_.get(),
91 FROM_HERE,
92 base::Bind(&Context::WriteFileImpl, base::Unretained(this), buf, buf_len),
93 base::Bind(&Context::OnAsyncCompleted,
94 base::Unretained(this),
95 IntToInt64(callback)));
96 DCHECK(posted);
98 async_in_progress_ = true;
99 return ERR_IO_PENDING;
102 FileStream::Context::IOResult FileStream::Context::SeekFileImpl(Whence whence,
103 int64 offset) {
104 off_t res = lseek(file_.GetPlatformFile(), static_cast<off_t>(offset),
105 static_cast<int>(whence));
106 if (res == static_cast<off_t>(-1))
107 return IOResult::FromOSError(errno);
109 return IOResult(res, 0);
112 FileStream::Context::IOResult FileStream::Context::FlushFileImpl() {
113 ssize_t res = HANDLE_EINTR(fsync(file_.GetPlatformFile()));
114 if (res == -1)
115 return IOResult::FromOSError(errno);
117 return IOResult(res, 0);
120 FileStream::Context::IOResult FileStream::Context::ReadFileImpl(
121 scoped_refptr<IOBuffer> buf,
122 int buf_len) {
123 // Loop in the case of getting interrupted by a signal.
124 ssize_t res = HANDLE_EINTR(read(file_.GetPlatformFile(), buf->data(),
125 static_cast<size_t>(buf_len)));
126 if (res == -1)
127 return IOResult::FromOSError(errno);
129 return IOResult(res, 0);
132 FileStream::Context::IOResult FileStream::Context::WriteFileImpl(
133 scoped_refptr<IOBuffer> buf,
134 int buf_len) {
135 ssize_t res = HANDLE_EINTR(write(file_.GetPlatformFile(), buf->data(),
136 buf_len));
137 if (res == -1)
138 return IOResult::FromOSError(errno);
140 return IOResult(res, 0);
143 } // namespace net