1 // Copyright (c) 2012 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 STORAGE_BROWSER_FILEAPI_LOCAL_FILE_STREAM_WRITER_H_
6 #define STORAGE_BROWSER_FILEAPI_LOCAL_FILE_STREAM_WRITER_H_
10 #include "base/callback.h"
11 #include "base/compiler_specific.h"
12 #include "base/files/file_path.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/task_runner.h"
17 #include "storage/browser/fileapi/file_stream_writer.h"
18 #include "storage/browser/storage_browser_export.h"
21 class LocalFileStreamWriterTest
;
30 // This class is a thin wrapper around net::FileStream for writing local files.
31 class STORAGE_EXPORT LocalFileStreamWriter
32 : public NON_EXPORTED_BASE(FileStreamWriter
) {
34 ~LocalFileStreamWriter() override
;
36 // FileStreamWriter overrides.
37 int Write(net::IOBuffer
* buf
,
39 const net::CompletionCallback
& callback
) override
;
40 int Cancel(const net::CompletionCallback
& callback
) override
;
41 int Flush(const net::CompletionCallback
& callback
) override
;
44 friend class content::LocalFileStreamWriterTest
;
45 friend class FileStreamWriter
;
46 LocalFileStreamWriter(base::TaskRunner
* task_runner
,
47 const base::FilePath
& file_path
,
49 OpenOrCreate open_or_create
);
51 // Opens |file_path_| and if it succeeds, proceeds to InitiateSeek().
52 // If failed, the error code is returned by calling |error_callback|.
53 int InitiateOpen(const net::CompletionCallback
& error_callback
,
54 const base::Closure
& main_operation
);
55 void DidOpen(const net::CompletionCallback
& error_callback
,
56 const base::Closure
& main_operation
,
59 // Seeks to |initial_offset_| and proceeds to |main_operation| if it succeeds.
60 // If failed, the error code is returned by calling |error_callback|.
61 void InitiateSeek(const net::CompletionCallback
& error_callback
,
62 const base::Closure
& main_operation
);
63 void DidSeek(const net::CompletionCallback
& error_callback
,
64 const base::Closure
& main_operation
,
67 // Passed as the |main_operation| of InitiateOpen() function.
68 void ReadyToWrite(net::IOBuffer
* buf
, int buf_len
,
69 const net::CompletionCallback
& callback
);
71 // Writes asynchronously to the file.
72 int InitiateWrite(net::IOBuffer
* buf
, int buf_len
,
73 const net::CompletionCallback
& callback
);
74 void DidWrite(const net::CompletionCallback
& callback
, int result
);
76 // Flushes asynchronously to the file.
77 int InitiateFlush(const net::CompletionCallback
& callback
);
78 void DidFlush(const net::CompletionCallback
& callback
, int result
);
80 // Stops the in-flight operation and calls |cancel_callback_| if it has been
81 // set by Cancel() for the current operation.
82 bool CancelIfRequested();
84 // Initialization parameters.
85 const base::FilePath file_path_
;
86 OpenOrCreate open_or_create_
;
87 const int64 initial_offset_
;
88 scoped_refptr
<base::TaskRunner
> task_runner_
;
90 // Current states of the operation.
91 bool has_pending_operation_
;
92 scoped_ptr
<net::FileStream
> stream_impl_
;
93 net::CompletionCallback cancel_callback_
;
95 base::WeakPtrFactory
<LocalFileStreamWriter
> weak_factory_
;
96 DISALLOW_COPY_AND_ASSIGN(LocalFileStreamWriter
);
99 } // namespace storage
101 #endif // STORAGE_BROWSER_FILEAPI_LOCAL_FILE_STREAM_WRITER_H_