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. 4 5 #ifndef STORAGE_BROWSER_FILEAPI_LOCAL_FILE_STREAM_WRITER_H_ 6 #define STORAGE_BROWSER_FILEAPI_LOCAL_FILE_STREAM_WRITER_H_ 7 8 #include <utility> 9 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" 19 20 namespace content { 21 class LocalFileStreamWriterTest; 22 } 23 24 namespace net { 25 class FileStream; 26 } 27 28 namespace storage { 29 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) { 33 public: 34 virtual ~LocalFileStreamWriter(); 35 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; 41 42 private: 43 friend class content::LocalFileStreamWriterTest; 44 friend class FileStreamWriter; 45 LocalFileStreamWriter(base::TaskRunner* task_runner, 46 const base::FilePath& file_path, 47 int64 initial_offset, 48 OpenOrCreate open_or_create); 49 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, 56 int result); 57 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, 64 int64 result); 65 66 // Passed as the |main_operation| of InitiateOpen() function. 67 void ReadyToWrite(net::IOBuffer* buf, int buf_len, 68 const net::CompletionCallback& callback); 69 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); 74 75 // Flushes asynchronously to the file. 76 int InitiateFlush(const net::CompletionCallback& callback); 77 void DidFlush(const net::CompletionCallback& callback, int result); 78 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(); 82 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_; 88 89 // Current states of the operation. 90 bool has_pending_operation_; 91 scoped_ptr<net::FileStream> stream_impl_; 92 net::CompletionCallback cancel_callback_; 93 94 base::WeakPtrFactory<LocalFileStreamWriter> weak_factory_; 95 DISALLOW_COPY_AND_ASSIGN(LocalFileStreamWriter); 96 }; 97 98 } // namespace storage 99 100 #endif // STORAGE_BROWSER_FILEAPI_LOCAL_FILE_STREAM_WRITER_H_ 101