OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/browser/fileapi/local_file_stream_writer.h" | 5 #include "webkit/browser/fileapi/local_file_stream_writer.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "net/base/file_stream.h" | 9 #include "net/base/file_stream.h" |
10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
12 | 12 |
13 namespace fileapi { | 13 namespace fileapi { |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 const int kOpenFlagsForWrite = base::PLATFORM_FILE_OPEN | | 17 const int kOpenFlagsForWrite = base::PLATFORM_FILE_OPEN | |
18 base::PLATFORM_FILE_WRITE | | 18 base::PLATFORM_FILE_WRITE | |
19 base::PLATFORM_FILE_ASYNC; | 19 base::PLATFORM_FILE_ASYNC; |
| 20 const int kCreateFlagsForWrite = base::PLATFORM_FILE_CREATE | |
| 21 base::PLATFORM_FILE_WRITE | |
| 22 base::PLATFORM_FILE_ASYNC; |
20 | 23 |
21 } // namespace | 24 } // namespace |
22 | 25 |
23 FileStreamWriter* FileStreamWriter::CreateForLocalFile( | 26 FileStreamWriter* FileStreamWriter::CreateForLocalFile( |
24 base::TaskRunner* task_runner, | 27 base::TaskRunner* task_runner, |
25 const base::FilePath& file_path, | 28 const base::FilePath& file_path, |
26 int64 initial_offset) { | 29 int64 initial_offset, |
27 return new LocalFileStreamWriter(task_runner, file_path, initial_offset); | 30 OpenOrCreate open_or_create) { |
| 31 return new LocalFileStreamWriter( |
| 32 task_runner, file_path, initial_offset, open_or_create); |
28 } | 33 } |
29 | 34 |
30 LocalFileStreamWriter::~LocalFileStreamWriter() { | 35 LocalFileStreamWriter::~LocalFileStreamWriter() { |
31 // Invalidate weak pointers so that we won't receive any callbacks from | 36 // Invalidate weak pointers so that we won't receive any callbacks from |
32 // in-flight stream operations, which might be triggered during the file close | 37 // in-flight stream operations, which might be triggered during the file close |
33 // in the FileStream destructor. | 38 // in the FileStream destructor. |
34 weak_factory_.InvalidateWeakPtrs(); | 39 weak_factory_.InvalidateWeakPtrs(); |
35 | 40 |
36 // FileStream's destructor closes the file safely, since we opened the file | 41 // FileStream's destructor closes the file safely, since we opened the file |
37 // by its Open() method. | 42 // by its Open() method. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 | 79 |
75 has_pending_operation_ = true; | 80 has_pending_operation_ = true; |
76 int result = InitiateFlush(callback); | 81 int result = InitiateFlush(callback); |
77 if (result != net::ERR_IO_PENDING) | 82 if (result != net::ERR_IO_PENDING) |
78 has_pending_operation_ = false; | 83 has_pending_operation_ = false; |
79 return result; | 84 return result; |
80 } | 85 } |
81 | 86 |
82 LocalFileStreamWriter::LocalFileStreamWriter(base::TaskRunner* task_runner, | 87 LocalFileStreamWriter::LocalFileStreamWriter(base::TaskRunner* task_runner, |
83 const base::FilePath& file_path, | 88 const base::FilePath& file_path, |
84 int64 initial_offset) | 89 int64 initial_offset, |
| 90 OpenOrCreate open_or_create) |
85 : file_path_(file_path), | 91 : file_path_(file_path), |
| 92 open_or_create_(open_or_create), |
86 initial_offset_(initial_offset), | 93 initial_offset_(initial_offset), |
87 task_runner_(task_runner), | 94 task_runner_(task_runner), |
88 has_pending_operation_(false), | 95 has_pending_operation_(false), |
89 weak_factory_(this) {} | 96 weak_factory_(this) {} |
90 | 97 |
91 int LocalFileStreamWriter::InitiateOpen( | 98 int LocalFileStreamWriter::InitiateOpen( |
92 const net::CompletionCallback& error_callback, | 99 const net::CompletionCallback& error_callback, |
93 const base::Closure& main_operation) { | 100 const base::Closure& main_operation) { |
94 DCHECK(has_pending_operation_); | 101 DCHECK(has_pending_operation_); |
95 DCHECK(!stream_impl_.get()); | 102 DCHECK(!stream_impl_.get()); |
96 | 103 |
97 stream_impl_.reset(new net::FileStream(NULL, task_runner_)); | 104 stream_impl_.reset(new net::FileStream(NULL, task_runner_)); |
98 | 105 |
99 return stream_impl_->Open(file_path_, | 106 return stream_impl_->Open(file_path_, |
100 kOpenFlagsForWrite, | 107 open_or_create_ == OPEN_EXISTING |
| 108 ? kOpenFlagsForWrite |
| 109 : kCreateFlagsForWrite, |
101 base::Bind(&LocalFileStreamWriter::DidOpen, | 110 base::Bind(&LocalFileStreamWriter::DidOpen, |
102 weak_factory_.GetWeakPtr(), | 111 weak_factory_.GetWeakPtr(), |
103 error_callback, | 112 error_callback, |
104 main_operation)); | 113 main_operation)); |
105 } | 114 } |
106 | 115 |
107 void LocalFileStreamWriter::DidOpen( | 116 void LocalFileStreamWriter::DidOpen( |
108 const net::CompletionCallback& error_callback, | 117 const net::CompletionCallback& error_callback, |
109 const base::Closure& main_operation, | 118 const base::Closure& main_operation, |
110 int result) { | 119 int result) { |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 return false; | 240 return false; |
232 | 241 |
233 net::CompletionCallback pending_cancel = cancel_callback_; | 242 net::CompletionCallback pending_cancel = cancel_callback_; |
234 has_pending_operation_ = false; | 243 has_pending_operation_ = false; |
235 cancel_callback_.Reset(); | 244 cancel_callback_.Reset(); |
236 pending_cancel.Run(net::OK); | 245 pending_cancel.Run(net::OK); |
237 return true; | 246 return true; |
238 } | 247 } |
239 | 248 |
240 } // namespace fileapi | 249 } // namespace fileapi |
OLD | NEW |