Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 WEBKIT_FILEAPI_LOCAL_FILE_WRITER_H_ | |
| 6 #define WEBKIT_FILEAPI_LOCAL_FILE_WRITER_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/compiler_specific.h" | |
| 12 #include "base/callback.h" | |
| 13 #include "base/file_path.h" | |
| 14 #include "base/platform_file.h" | |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/memory/weak_ptr.h" | |
| 17 #include "webkit/fileapi/file_writer.h" | |
| 18 | |
| 19 namespace net { | |
| 20 class FileStream; | |
| 21 } | |
| 22 | |
| 23 namespace fileapi { | |
| 24 | |
| 25 // Thin wrapper around net::FileStream | |
|
kinuko
2012/04/23 09:29:57
nit: Please make comments look like sentence and e
kinaba
2012/04/23 10:55:14
Done.
| |
| 26 class LocalFileWriter : public FileWriter { | |
| 27 public: | |
| 28 enum FileOpenMode { | |
| 29 OPEN, | |
| 30 CREATE, | |
| 31 }; | |
| 32 | |
| 33 // Create a writer for the file in the path |file_path| starting from | |
| 34 // |initial_offset|. If |mode| is |CREATE|, |file_path| is set to an empty | |
|
kinuko
2012/04/23 09:29:57
This might feel a bit ambiguous-- does this mean T
kinaba
2012/04/23 10:55:14
What I intend is O_CREAT|O_WRONLY|O_TRUNC, which i
kinuko
2012/04/23 12:50:28
I can imagine the situation. I think it's ok to h
kinuko
2012/04/23 13:13:47
By the way we'll also need to check if the given p
| |
| 35 // file when opened. If |mode| is |OPEN|, |file_path| must point to an | |
| 36 // existing file, and the file content will not be changed while opening the | |
| 37 // file. | |
| 38 LocalFileWriter(const FilePath& file_path, | |
| 39 int64 initial_offset, | |
| 40 FileOpenMode mode); | |
| 41 virtual ~LocalFileWriter(); | |
| 42 | |
| 43 // FileWriter overrides. | |
| 44 virtual int Write(net::IOBuffer* buf, int buf_len, | |
| 45 const net::CompletionCallback& callback) OVERRIDE; | |
| 46 virtual void Cancel(const net::CompletionCallback& callback) OVERRIDE; | |
| 47 | |
| 48 private: | |
| 49 // Opens |file_path_| and if it succeeds, proceeds to InitiateSeek(). | |
| 50 // If failed, the error code is returned by calling |error_callback|. | |
| 51 int InitiateOpen(const net::CompletionCallback& error_callback, | |
| 52 const base::Closure& main_operation); | |
| 53 void DidOpen(const net::CompletionCallback& error_callback, | |
| 54 const base::Closure& main_operation, | |
| 55 int result); | |
| 56 | |
| 57 // Seeks to |initial_offset_| and proceeds to |main_operation| if it succeeds. | |
| 58 // If failed, the error code is returned by calling |error_callback|. | |
| 59 void InitiateSeek(const net::CompletionCallback& error_callback, | |
| 60 const base::Closure& main_operation); | |
| 61 void DidSeek(const net::CompletionCallback& error_callback, | |
| 62 const base::Closure& main_operation, | |
| 63 int64 result); | |
| 64 | |
| 65 // Passed as the |main_operation| of InitiateOpen() function. | |
| 66 void ReadyToWrite(net::IOBuffer* buf, int buf_len, | |
| 67 const net::CompletionCallback& callback); | |
| 68 | |
| 69 // Writes asynchronously to the file. | |
| 70 int InitiateWrite(net::IOBuffer* buf, int buf_len, | |
| 71 const net::CompletionCallback& callback); | |
| 72 void DidWrite(const net::CompletionCallback& callback, int result); | |
| 73 | |
| 74 // Stops the in-flight operation and calls |cancel_callback_| if it has been | |
| 75 // set by Cancel() for the current operation. | |
| 76 bool CancelIfRequested(); | |
| 77 | |
| 78 // Initialization parameters. | |
| 79 const FilePath file_path_; | |
| 80 const int file_open_flags_; | |
| 81 const int64 initial_offset_; | |
| 82 | |
| 83 // Current states of the operation. | |
| 84 bool has_pending_operation_; | |
| 85 scoped_ptr<net::FileStream> stream_impl_; | |
| 86 net::CompletionCallback cancel_callback_; | |
| 87 | |
| 88 base::WeakPtrFactory<LocalFileWriter> weak_factory_; | |
| 89 DISALLOW_COPY_AND_ASSIGN(LocalFileWriter); | |
| 90 }; | |
| 91 | |
| 92 } // namespace fileapi | |
| 93 | |
| 94 #endif // WEBKIT_FILEAPI_LOCAL_FILE_WRITER_H_ | |
| OLD | NEW |