summaryrefslogtreecommitdiffstats
path: root/webkit/fileapi/local_file_writer.h
blob: a16767a29e13ca84760fb1db13c01f085a3157e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) 2012 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 WEBKIT_FILEAPI_LOCAL_FILE_WRITER_H_
#define WEBKIT_FILEAPI_LOCAL_FILE_WRITER_H_
#pragma once

#include <utility>

#include "base/compiler_specific.h"
#include "base/callback.h"
#include "base/file_path.h"
#include "base/platform_file.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "webkit/fileapi/file_writer.h"

namespace net {
class FileStream;
}

namespace fileapi {

// This class is a thin wrapper around net::FileStream for writing local files.
class LocalFileWriter : public FileWriter {
 public:
  // Create a writer for the existing file in the path |file_path| starting from
  // |initial_offset|.
  LocalFileWriter(const FilePath& file_path, int64 initial_offset);
  virtual ~LocalFileWriter();

  // FileWriter overrides.
  virtual int Write(net::IOBuffer* buf, int buf_len,
                    const net::CompletionCallback& callback) OVERRIDE;
  virtual int Cancel(const net::CompletionCallback& callback) OVERRIDE;

 private:
  // Opens |file_path_| and if it succeeds, proceeds to InitiateSeek().
  // If failed, the error code is returned by calling |error_callback|.
  int InitiateOpen(const net::CompletionCallback& error_callback,
                   const base::Closure& main_operation);
  void DidOpen(const net::CompletionCallback& error_callback,
               const base::Closure& main_operation,
               int result);

  // Seeks to |initial_offset_| and proceeds to |main_operation| if it succeeds.
  // If failed, the error code is returned by calling |error_callback|.
  void InitiateSeek(const net::CompletionCallback& error_callback,
                    const base::Closure& main_operation);
  void DidSeek(const net::CompletionCallback& error_callback,
               const base::Closure& main_operation,
               int64 result);

  // Passed as the |main_operation| of InitiateOpen() function.
  void ReadyToWrite(net::IOBuffer* buf, int buf_len,
                    const net::CompletionCallback& callback);

  // Writes asynchronously to the file.
  int InitiateWrite(net::IOBuffer* buf, int buf_len,
                    const net::CompletionCallback& callback);
  void DidWrite(const net::CompletionCallback& callback, int result);

  // Stops the in-flight operation and calls |cancel_callback_| if it has been
  // set by Cancel() for the current operation.
  bool CancelIfRequested();

  // Initialization parameters.
  const FilePath file_path_;
  const int64 initial_offset_;

  // Current states of the operation.
  bool has_pending_operation_;
  scoped_ptr<net::FileStream> stream_impl_;
  net::CompletionCallback cancel_callback_;

  base::WeakPtrFactory<LocalFileWriter> weak_factory_;
  DISALLOW_COPY_AND_ASSIGN(LocalFileWriter);
};

}  // namespace fileapi

#endif  // WEBKIT_FILEAPI_LOCAL_FILE_WRITER_H_