Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / chromeos / file_system_provider / fileapi / file_stream_writer.h
blobb898f2bd371514bd363345d4f1b53af5b69ee6da
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.
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_
8 #include "base/basictypes.h"
9 #include "base/files/file.h"
10 #include "base/files/file_path.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h"
13 #include "storage/browser/fileapi/file_stream_writer.h"
14 #include "storage/browser/fileapi/file_system_url.h"
16 namespace chromeos {
17 namespace file_system_provider {
19 class ProvidedFileSystemInterface;
21 // Implements a streamed file writer. It is lazily initialized by the first call
22 // to Write().
23 class FileStreamWriter : public storage::FileStreamWriter {
24 public:
25 FileStreamWriter(const storage::FileSystemURL& url, int64 initial_offset);
27 ~FileStreamWriter() override;
29 // storage::FileStreamWriter overrides.
30 int Write(net::IOBuffer* buf,
31 int buf_len,
32 const net::CompletionCallback& callback) override;
33 int Cancel(const net::CompletionCallback& callback) override;
34 int Flush(const net::CompletionCallback& callback) override;
36 private:
37 // Helper class for executing operations on the provided file system. All
38 // of its methods must be called on UI thread. Callbacks are called on IO
39 // thread.
40 class OperationRunner;
42 // State of the file stream writer.
43 enum State {
44 NOT_INITIALIZED,
45 INITIALIZING,
46 INITIALIZED,
47 EXECUTING,
48 FAILED,
49 CANCELLING
52 // Called when OperationRunner::WriteOnUIThread is completed.
53 void OnWriteFileCompleted(int buffer_length,
54 const net::CompletionCallback& callback,
55 base::File::Error result);
57 // Called when Write() operation is completed with either a success or an
58 // error.
59 void OnWriteCompleted(net::CompletionCallback callback, int result);
61 // Initializes the writer by opening the file. When completed with success,
62 // runs the |pending_closure|. Otherwise, calls the |error_callback|.
63 void Initialize(const base::Closure& pending_closure,
64 const net::CompletionCallback& error_callback);
66 // Called when opening a file is completed with either a success or an error.
67 void OnOpenFileCompleted(
68 const base::Closure& pending_closure,
69 const net::CompletionCallback& error_callback,
70 base::File::Error result);
72 // Same as Write(), but called after initializing is completed.
73 void WriteAfterInitialized(scoped_refptr<net::IOBuffer> buffer,
74 int buffer_length,
75 const net::CompletionCallback& callback);
77 storage::FileSystemURL url_;
78 int64 current_offset_;
79 scoped_refptr<OperationRunner> runner_;
80 State state_;
82 base::WeakPtrFactory<FileStreamWriter> weak_ptr_factory_;
83 DISALLOW_COPY_AND_ASSIGN(FileStreamWriter);
86 } // namespace file_system_provider
87 } // namespace chromeos
89 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_WRITER_H_