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 "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" 15 16namespace chromeos { 17namespace file_system_provider { 18 19class ProvidedFileSystemInterface; 20 21// Implements a streamed file writer. It is lazily initialized by the first call 22// to Write(). 23class FileStreamWriter : public storage::FileStreamWriter { 24 public: 25 FileStreamWriter(const storage::FileSystemURL& url, int64 initial_offset); 26 27 virtual ~FileStreamWriter(); 28 29 // storage::FileStreamWriter overrides. 30 virtual int Write(net::IOBuffer* buf, 31 int buf_len, 32 const net::CompletionCallback& callback) OVERRIDE; 33 virtual int Cancel(const net::CompletionCallback& callback) OVERRIDE; 34 virtual int Flush(const net::CompletionCallback& callback) OVERRIDE; 35 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; 41 42 // State of the file stream writer. 43 enum State { NOT_INITIALIZED, INITIALIZING, INITIALIZED, FAILED }; 44 45 // Called when OperationRunner::WriteOnUIThread is completed. 46 void OnWriteFileCompleted(int buffer_length, 47 const net::CompletionCallback& callback, 48 base::File::Error result); 49 50 // Called when Write() operation is completed with either a success or an 51 // error. 52 void OnWriteCompleted(net::CompletionCallback callback, int result); 53 54 // Called when Abort() operation is completed with either a success or an 55 // error. 56 void OnAbortCompleted(const net::CompletionCallback& callback, 57 base::File::Error result); 58 59 // Initializes the writer by opening the file. When completed with success, 60 // runs the |pending_closure|. Otherwise, calls the |error_callback|. 61 void Initialize(const base::Closure& pending_closure, 62 const net::CompletionCallback& error_callback); 63 64 // Called when opening a file is completed with either a success or an error. 65 void OnOpenFileCompleted( 66 const base::Closure& pending_closure, 67 const net::CompletionCallback& error_callback, 68 base::File::Error result); 69 70 // Same as Write(), but called after initializing is completed. 71 void WriteAfterInitialized(scoped_refptr<net::IOBuffer> buffer, 72 int buffer_length, 73 const net::CompletionCallback& callback); 74 75 storage::FileSystemURL url_; 76 int64 current_offset_; 77 scoped_refptr<OperationRunner> runner_; 78 State state_; 79 80 base::WeakPtrFactory<FileStreamWriter> weak_ptr_factory_; 81 DISALLOW_COPY_AND_ASSIGN(FileStreamWriter); 82}; 83 84} // namespace file_system_provider 85} // namespace chromeos 86 87#endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_WRITER_H_ 88