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 WEBKIT_BROWSER_FILEAPI_LOCAL_FILE_STREAM_WRITER_H_
6 #define WEBKIT_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 "webkit/browser/fileapi/file_stream_writer.h"
18 #include "webkit/browser/webkit_storage_browser_export.h"
21 class LocalFileStreamWriterTest
;
30 // This class is a thin wrapper around net::FileStream for writing local files.
31 class WEBKIT_STORAGE_BROWSER_EXPORT LocalFileStreamWriter
32 : public NON_EXPORTED_BASE(FileStreamWriter
) {
34 virtual ~LocalFileStreamWriter();
36 // FileStreamWriter overrides.
37 virtual int Write(net::IOBuffer
* buf
, int buf_len
,
38 const net::CompletionCallback
& callback
) OVERRIDE
;
39 virtual int Cancel(const net::CompletionCallback
& callback
) OVERRIDE
;
40 virtual int Flush(const net::CompletionCallback
& callback
) OVERRIDE
;
43 friend class content::LocalFileStreamWriterTest
;
44 friend class FileStreamWriter
;
45 LocalFileStreamWriter(base::TaskRunner
* task_runner
,
46 const base::FilePath
& file_path
,
48 OpenOrCreate open_or_create
);
50 // Opens |file_path_| and if it succeeds, proceeds to InitiateSeek().
51 // If failed, the error code is returned by calling |error_callback|.
52 int InitiateOpen(const net::CompletionCallback
& error_callback
,
53 const base::Closure
& main_operation
);
54 void DidOpen(const net::CompletionCallback
& error_callback
,
55 const base::Closure
& main_operation
,
58 // Seeks to |initial_offset_| and proceeds to |main_operation| if it succeeds.
59 // If failed, the error code is returned by calling |error_callback|.
60 void InitiateSeek(const net::CompletionCallback
& error_callback
,
61 const base::Closure
& main_operation
);
62 void DidSeek(const net::CompletionCallback
& error_callback
,
63 const base::Closure
& main_operation
,
66 // Passed as the |main_operation| of InitiateOpen() function.
67 void ReadyToWrite(net::IOBuffer
* buf
, int buf_len
,
68 const net::CompletionCallback
& callback
);
70 // Writes asynchronously to the file.
71 int InitiateWrite(net::IOBuffer
* buf
, int buf_len
,
72 const net::CompletionCallback
& callback
);
73 void DidWrite(const net::CompletionCallback
& callback
, int result
);
75 // Flushes asynchronously to the file.
76 int InitiateFlush(const net::CompletionCallback
& callback
);
77 void DidFlush(const net::CompletionCallback
& callback
, int result
);
79 // Stops the in-flight operation and calls |cancel_callback_| if it has been
80 // set by Cancel() for the current operation.
81 bool CancelIfRequested();
83 // Initialization parameters.
84 const base::FilePath file_path_
;
85 OpenOrCreate open_or_create_
;
86 const int64 initial_offset_
;
87 scoped_refptr
<base::TaskRunner
> task_runner_
;
89 // Current states of the operation.
90 bool has_pending_operation_
;
91 scoped_ptr
<net::FileStream
> stream_impl_
;
92 net::CompletionCallback cancel_callback_
;
94 base::WeakPtrFactory
<LocalFileStreamWriter
> weak_factory_
;
95 DISALLOW_COPY_AND_ASSIGN(LocalFileStreamWriter
);
98 } // namespace fileapi
100 #endif // WEBKIT_BROWSER_FILEAPI_LOCAL_FILE_STREAM_WRITER_H_