Home | History | Annotate | Download | only in base
      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.
      4 
      5 #include "net/base/file_stream_context.h"
      6 
      7 #include <errno.h>
      8 
      9 #include "base/basictypes.h"
     10 #include "base/bind.h"
     11 #include "base/bind_helpers.h"
     12 #include "base/callback.h"
     13 #include "base/files/file_path.h"
     14 #include "base/location.h"
     15 #include "base/logging.h"
     16 #include "base/metrics/histogram.h"
     17 #include "base/posix/eintr_wrapper.h"
     18 #include "base/task_runner.h"
     19 #include "base/task_runner_util.h"
     20 #include "net/base/io_buffer.h"
     21 #include "net/base/net_errors.h"
     22 
     23 namespace net {
     24 
     25 FileStream::Context::Context(const scoped_refptr<base::TaskRunner>& task_runner)
     26     : async_in_progress_(false),
     27       orphaned_(false),
     28       task_runner_(task_runner) {
     29 }
     30 
     31 FileStream::Context::Context(base::File file,
     32                              const scoped_refptr<base::TaskRunner>& task_runner)
     33     : file_(file.Pass()),
     34       async_in_progress_(false),
     35       orphaned_(false),
     36       task_runner_(task_runner) {
     37 }
     38 
     39 FileStream::Context::~Context() {
     40 }
     41 
     42 int FileStream::Context::Read(IOBuffer* in_buf,
     43                               int buf_len,
     44                               const CompletionCallback& callback) {
     45   DCHECK(!async_in_progress_);
     46 
     47   scoped_refptr<IOBuffer> buf = in_buf;
     48   const bool posted = base::PostTaskAndReplyWithResult(
     49       task_runner_.get(),
     50       FROM_HERE,
     51       base::Bind(&Context::ReadFileImpl, base::Unretained(this), buf, buf_len),
     52       base::Bind(&Context::OnAsyncCompleted,
     53                  base::Unretained(this),
     54                  IntToInt64(callback)));
     55   DCHECK(posted);
     56 
     57   async_in_progress_ = true;
     58   return ERR_IO_PENDING;
     59 }
     60 
     61 int FileStream::Context::Write(IOBuffer* in_buf,
     62                                int buf_len,
     63                                const CompletionCallback& callback) {
     64   DCHECK(!async_in_progress_);
     65 
     66   scoped_refptr<IOBuffer> buf = in_buf;
     67   const bool posted = base::PostTaskAndReplyWithResult(
     68       task_runner_.get(),
     69       FROM_HERE,
     70       base::Bind(&Context::WriteFileImpl, base::Unretained(this), buf, buf_len),
     71       base::Bind(&Context::OnAsyncCompleted,
     72                  base::Unretained(this),
     73                  IntToInt64(callback)));
     74   DCHECK(posted);
     75 
     76   async_in_progress_ = true;
     77   return ERR_IO_PENDING;
     78 }
     79 
     80 FileStream::Context::IOResult FileStream::Context::SeekFileImpl(
     81     base::File::Whence whence,
     82     int64 offset) {
     83   int64 res = file_.Seek(whence, offset);
     84   if (res == -1)
     85     return IOResult::FromOSError(errno);
     86 
     87   return IOResult(res, 0);
     88 }
     89 
     90 void FileStream::Context::OnFileOpened() {
     91 }
     92 
     93 FileStream::Context::IOResult FileStream::Context::ReadFileImpl(
     94     scoped_refptr<IOBuffer> buf,
     95     int buf_len) {
     96   int res = file_.ReadAtCurrentPosNoBestEffort(buf->data(), buf_len);
     97   if (res == -1)
     98     return IOResult::FromOSError(errno);
     99 
    100   return IOResult(res, 0);
    101 }
    102 
    103 FileStream::Context::IOResult FileStream::Context::WriteFileImpl(
    104     scoped_refptr<IOBuffer> buf,
    105     int buf_len) {
    106   int res = file_.WriteAtCurrentPosNoBestEffort(buf->data(), buf_len);
    107   if (res == -1)
    108     return IOResult::FromOSError(errno);
    109 
    110   return IOResult(res, 0);
    111 }
    112 
    113 }  // namespace net
    114