summaryrefslogtreecommitdiffstats
path: root/webkit/fileapi/file_system_operation.h
blob: 1107303960a3aefcaf92b4465dcc25b1ba932f71 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) 2010 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_FILE_SYSTEM_OPERATION_H_
#define WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_H_

#include <vector>

#include "base/file_path.h"
#include "base/file_util_proxy.h"
#include "base/message_loop_proxy.h"
#include "base/platform_file.h"
#include "base/ref_counted.h"
#include "base/scoped_callback_factory.h"
#include "base/scoped_ptr.h"

namespace base {
class Time;
}

class GURL;

namespace fileapi {

class FileSystemCallbackDispatcher;

// This class is designed to serve one-time file system operation per instance.
// Only one method(CreateFile, CreateDirectory, Copy, Move, DirectoryExists,
// GetMetadata, ReadDirectory and Remove) may be called during the lifetime of
// this object and it should be called no more than once.
class FileSystemOperation {
 public:
  FileSystemOperation(FileSystemCallbackDispatcher* dispatcher,
                      scoped_refptr<base::MessageLoopProxy> proxy);
  ~FileSystemOperation();

  void CreateFile(const FilePath& path,
                  bool exclusive);

  void CreateDirectory(const FilePath& path,
                       bool exclusive,
                       bool recursive);

  void Copy(const FilePath& src_path,
            const FilePath& dest_path);

  // If |dest_path| exists and is a directory, behavior is unspecified or
  // varies for different platforms. TODO(kkanetkar): Fix this as per spec
  // when it is addressed in spec.
  void Move(const FilePath& src_path,
            const FilePath& dest_path);

  void DirectoryExists(const FilePath& path);

  void FileExists(const FilePath& path);

  void GetMetadata(const FilePath& path);

  void ReadDirectory(const FilePath& path);

  void Remove(const FilePath& path);

  void Write(
      const FilePath& path, const GURL& blob_url, int64 offset);

  void Truncate(const FilePath& path, int64 length);

  void TouchFile(const FilePath& path,
                 const base::Time& last_access_time,
                 const base::Time& last_modified_time);

  // Used to attempt to cancel the current operation.  This currently does
  // nothing for any operation other than Write().
  void Cancel();

 protected:
  // Proxy for calling file_util_proxy methods.
  scoped_refptr<base::MessageLoopProxy> proxy_;

 private:
  // Callbacks for above methods.
  void DidCreateFileExclusive(
      base::PlatformFileError rv, base::PassPlatformFile file, bool created);

  // Returns success even if the file already existed.
  void DidCreateFileNonExclusive(
      base::PlatformFileError rv, base::PassPlatformFile file, bool created);

  // Generic callback that translates platform errors to WebKit error codes.
  void DidFinishFileOperation(base::PlatformFileError rv);

  void DidDirectoryExists(base::PlatformFileError rv,
                          const base::PlatformFileInfo& file_info);

  void DidFileExists(base::PlatformFileError rv,
                     const base::PlatformFileInfo& file_info);

  void DidGetMetadata(base::PlatformFileError rv,
                      const base::PlatformFileInfo& file_info);

  void DidReadDirectory(
      base::PlatformFileError rv,
      const std::vector<base::file_util_proxy::Entry>& entries);

  void DidWrite(
      base::PlatformFileError rv,
      int64 bytes,
      bool complete);

  void DidTouchFile(base::PlatformFileError rv);

  scoped_ptr<FileSystemCallbackDispatcher> dispatcher_;

  base::ScopedCallbackFactory<FileSystemOperation> callback_factory_;

#ifndef NDEBUG
  // A flag to make sure we call operation only once per instance.
  bool operation_pending_;
#endif

  DISALLOW_COPY_AND_ASSIGN(FileSystemOperation);
};

}  // namespace fileapi

#endif  // WEBKIT_FILEAPI_FILE_SYSTEM_OPERATION_H_