1 // Copyright 2014 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 #ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_WRITER_H_
6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_WRITER_H_
7 
8 #include <stdint.h>
9 
10 #include "base/files/file.h"
11 #include "base/files/file_path.h"
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/weak_ptr.h"
15 #include "net/base/completion_once_callback.h"
16 #include "storage/browser/file_system/file_stream_writer.h"
17 #include "storage/browser/file_system/file_system_url.h"
18 
19 namespace chromeos {
20 namespace file_system_provider {
21 
22 // Implements a streamed file writer. It is lazily initialized by the first call
23 // to Write().
24 class FileStreamWriter : public storage::FileStreamWriter {
25  public:
26   FileStreamWriter(const storage::FileSystemURL& url, int64_t initial_offset);
27 
28   ~FileStreamWriter() override;
29 
30   // storage::FileStreamWriter overrides.
31   int Write(net::IOBuffer* buf,
32             int buf_len,
33             net::CompletionOnceCallback callback) override;
34   int Cancel(net::CompletionOnceCallback callback) override;
35   int Flush(net::CompletionOnceCallback callback) override;
36 
37  private:
38   // Helper class for executing operations on the provided file system. All
39   // of its methods must be called on UI thread. Callbacks are called on IO
40   // thread.
41   class OperationRunner;
42 
43   // State of the file stream writer.
44   enum State {
45     NOT_INITIALIZED,
46     INITIALIZING,
47     INITIALIZED,
48     EXECUTING,
49     FAILED,
50     CANCELLING
51   };
52 
53   // Called when OperationRunner::WriteOnUIThread is completed.
54   void OnWriteFileCompleted(int buffer_length,
55                             net::CompletionOnceCallback callback,
56                             base::File::Error result);
57 
58   // Called when Write() operation is completed with either a success or an
59   // error.
60   void OnWriteCompleted(int result);
61 
62   // Initializes the writer by opening the file. When completed with success,
63   // runs the |pending_closure|. Otherwise, calls the |error_callback|.
64   void Initialize(base::OnceClosure pending_closure,
65                   net::CompletionOnceCallback error_callback);
66 
67   // Called when opening a file is completed with either a success or an error.
68   void OnOpenFileCompleted(base::OnceClosure pending_closure,
69                            net::CompletionOnceCallback error_callback,
70                            base::File::Error result);
71 
72   // Same as Write(), but called after initializing is completed.
73   void WriteAfterInitialized(scoped_refptr<net::IOBuffer> buffer,
74                              int buffer_length,
75                              net::CompletionOnceCallback callback);
76 
77   net::CompletionOnceCallback write_callback_;
78   storage::FileSystemURL url_;
79   int64_t current_offset_;
80   scoped_refptr<OperationRunner> runner_;
81   State state_;
82 
83   base::WeakPtrFactory<FileStreamWriter> weak_ptr_factory_{this};
84   DISALLOW_COPY_AND_ASSIGN(FileStreamWriter);
85 };
86 
87 }  // namespace file_system_provider
88 }  // namespace chromeos
89 
90 #endif  // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_WRITER_H_
91