File: file_system_operation_impl.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (220 lines) | stat: -rw-r--r-- 9,788 bytes parent folder | download | duplicates (10)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef STORAGE_BROWSER_FILE_SYSTEM_FILE_SYSTEM_OPERATION_IMPL_H_
#define STORAGE_BROWSER_FILE_SYSTEM_FILE_SYSTEM_OPERATION_IMPL_H_

#include <stdint.h>

#include <memory>
#include <vector>

#include "base/component_export.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/types/pass_key.h"
#include "components/services/storage/public/cpp/quota_error_or.h"
#include "storage/browser/blob/scoped_file.h"
#include "storage/browser/file_system/file_system_operation.h"
#include "storage/browser/file_system/file_system_operation_context.h"
#include "storage/browser/file_system/file_system_url.h"
#include "storage/browser/file_system/file_writer_delegate.h"
#include "third_party/blink/public/mojom/quota/quota_types.mojom.h"

namespace storage {

class AsyncFileUtil;
class FileSystemContext;
class FileSystemOperation;
class RecursiveOperationDelegate;

// The default implementation of FileSystemOperation for file systems.
class COMPONENT_EXPORT(STORAGE_BROWSER) FileSystemOperationImpl
    : public FileSystemOperation {
 public:
  // Exposed for use with std::make_unique. Instances should be obtained from
  // the factory method FileSystemOperation::Create().
  FileSystemOperationImpl(
      OperationType type,
      const FileSystemURL& url,
      FileSystemContext* file_system_context,
      std::unique_ptr<FileSystemOperationContext> operation_context,
      base::PassKey<FileSystemOperation>);

  FileSystemOperationImpl(const FileSystemOperationImpl&) = delete;
  FileSystemOperationImpl& operator=(const FileSystemOperationImpl&) = delete;

  ~FileSystemOperationImpl() override;

  // FileSystemOperation overrides.
  void CreateFile(const FileSystemURL& url,
                  bool exclusive,
                  StatusCallback callback) override;
  void CreateDirectory(const FileSystemURL& url,
                       bool exclusive,
                       bool recursive,
                       StatusCallback callback) override;
  void Copy(const FileSystemURL& src_url,
            const FileSystemURL& dest_url,
            CopyOrMoveOptionSet options,
            ErrorBehavior error_behavior,
            std::unique_ptr<CopyOrMoveHookDelegate> copy_or_move_hook_delegate,
            StatusCallback callback) override;
  void Move(const FileSystemURL& src_url,
            const FileSystemURL& dest_url,
            CopyOrMoveOptionSet options,
            ErrorBehavior error_behavior,
            std::unique_ptr<CopyOrMoveHookDelegate> copy_or_move_hook_delegate,
            StatusCallback callback) override;
  void DirectoryExists(const FileSystemURL& url,
                       StatusCallback callback) override;
  void FileExists(const FileSystemURL& url, StatusCallback callback) override;
  void GetMetadata(const FileSystemURL& url,
                   GetMetadataFieldSet fields,
                   GetMetadataCallback callback) override;
  void ReadDirectory(const FileSystemURL& url,
                     const ReadDirectoryCallback& callback) override;
  void Remove(const FileSystemURL& url,
              bool recursive,
              StatusCallback callback) override;
  void WriteBlob(const FileSystemURL& url,
                 std::unique_ptr<FileWriterDelegate> writer_delegate,
                 std::unique_ptr<BlobReader> blob_reader,
                 const WriteCallback& callback) override;
  void Write(const FileSystemURL& url,
             std::unique_ptr<FileWriterDelegate> writer_delegate,
             mojo::ScopedDataPipeConsumerHandle data_pipe,
             const WriteCallback& callback) override;
  void Truncate(const FileSystemURL& url,
                int64_t length,
                StatusCallback callback) override;
  void TouchFile(const FileSystemURL& url,
                 const base::Time& last_access_time,
                 const base::Time& last_modified_time,
                 StatusCallback callback) override;
  void OpenFile(const FileSystemURL& url,
                uint32_t file_flags,
                OpenFileCallback callback) override;
  void Cancel(StatusCallback cancel_callback) override;
  void CreateSnapshotFile(const FileSystemURL& path,
                          SnapshotFileCallback callback) override;
  void CopyInForeignFile(const base::FilePath& src_local_disk_path,
                         const FileSystemURL& dest_url,
                         StatusCallback callback) override;
  void RemoveFile(const FileSystemURL& url, StatusCallback callback) override;
  void RemoveDirectory(const FileSystemURL& url,
                       StatusCallback callback) override;
  void CopyFileLocal(const FileSystemURL& src_url,
                     const FileSystemURL& dest_url,
                     CopyOrMoveOptionSet options,
                     const CopyFileProgressCallback& progress_callback,
                     StatusCallback callback) override;
  void MoveFileLocal(const FileSystemURL& src_url,
                     const FileSystemURL& dest_url,
                     CopyOrMoveOptionSet options,
                     StatusCallback callback) override;
  base::File::Error SyncGetPlatformPath(const FileSystemURL& url,
                                        base::FilePath* platform_path) override;

  FileSystemContext* file_system_context() const {
    return file_system_context_.get();
  }

 private:
  friend class FileSystemOperation;

  // Queries the quota and usage and then runs the given |task|.
  // If an error occurs during the quota query it runs |error_callback| instead.
  void GetBucketSpaceRemainingAndRunTask(const FileSystemURL& url,
                                         base::OnceClosure task,
                                         base::OnceClosure error_callback);

  // Called after the quota info is obtained from the quota manager
  // (which is triggered by GetBucketSpaceRemainingAndRunTask).
  // Sets the quota info in the operation_context_ and then runs the given
  // |task| if the returned quota status is successful, otherwise runs
  // |error_callback|.
  void DidGetBucketSpaceRemaining(base::OnceClosure task,
                                  base::OnceClosure error_callback,
                                  QuotaErrorOr<int64_t> space_left);

  // The 'body' methods that perform the actual work (i.e. posting the
  // file task on proxy_) after the quota check.
  void DoCreateFile(const FileSystemURL& url,
                    StatusCallback callback,
                    bool exclusive);
  void DoCreateDirectory(const FileSystemURL& url,
                         StatusCallback callback,
                         bool exclusive,
                         bool recursive);
  void DoCopyFileLocal(const FileSystemURL& src,
                       const FileSystemURL& dest,
                       CopyOrMoveOptionSet options,
                       const CopyFileProgressCallback& progress_callback,
                       StatusCallback callback);
  void DoMoveFileLocal(const FileSystemURL& src,
                       const FileSystemURL& dest,
                       CopyOrMoveOptionSet options,
                       StatusCallback callback);
  void DoCopyInForeignFile(const base::FilePath& src_local_disk_file_path,
                           const FileSystemURL& dest,
                           StatusCallback callback);
  void DoTruncate(const FileSystemURL& url,
                  StatusCallback callback,
                  int64_t length);
  void DoOpenFile(const FileSystemURL& url,
                  OpenFileCallback callback,
                  uint32_t file_flags);

  // Callback for CreateFile for |exclusive|=true cases.
  void DidEnsureFileExistsExclusive(StatusCallback callback,
                                    base::File::Error rv,
                                    bool created);

  // Callback for CreateFile for |exclusive|=false cases.
  void DidEnsureFileExistsNonExclusive(StatusCallback callback,
                                       base::File::Error rv,
                                       bool created);

  void DidFinishOperation(StatusCallback callback, base::File::Error rv);
  void DidDirectoryExists(StatusCallback callback,
                          base::File::Error rv,
                          const base::File::Info& file_info);
  void DidFileExists(StatusCallback callback,
                     base::File::Error rv,
                     const base::File::Info& file_info);
  void DidDeleteRecursively(const FileSystemURL& url,
                            StatusCallback callback,
                            base::File::Error rv);
  void DidWrite(const FileSystemURL& url,
                const WriteCallback& callback,
                base::File::Error rv,
                int64_t bytes,
                FileWriterDelegate::WriteProgressStatus write_status);

  // Used only for internal assertions.
  void CheckOperationType(OperationType type);

  const OperationType type_;
  scoped_refptr<FileSystemContext> file_system_context_;

  std::unique_ptr<FileSystemOperationContext> operation_context_;
  raw_ptr<AsyncFileUtil> async_file_util_;  // Not owned.

  std::unique_ptr<FileWriterDelegate> file_writer_delegate_;
  std::unique_ptr<RecursiveOperationDelegate> recursive_operation_delegate_;

  StatusCallback cancel_callback_;

  // A flag to make sure we call operation only once per instance.
  bool operation_called_ = false;

  base::WeakPtr<FileSystemOperationImpl> weak_ptr_;
  base::WeakPtrFactory<FileSystemOperationImpl> weak_factory_{this};
};

}  // namespace storage

#endif  // STORAGE_BROWSER_FILE_SYSTEM_FILE_SYSTEM_OPERATION_IMPL_H_