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; | |
23 | 20 |
24 } // namespace | 21 } // namespace |
25 | 22 |
26 FileStreamWriter* FileStreamWriter::CreateForLocalFile( | 23 FileStreamWriter* FileStreamWriter::CreateForLocalFile( |
27 base::TaskRunner* task_runner, | 24 base::TaskRunner* task_runner, |
28 const base::FilePath& file_path, | 25 const base::FilePath& file_path, |
29 int64 initial_offset, | 26 int64 initial_offset) { |
30 OpenOrCreate open_or_create) { | 27 return new LocalFileStreamWriter(task_runner, file_path, initial_offset); |
31 return new LocalFileStreamWriter( | |
32 task_runner, file_path, initial_offset, open_or_create); | |
33 } | 28 } |
34 | 29 |
35 LocalFileStreamWriter::~LocalFileStreamWriter() { | 30 LocalFileStreamWriter::~LocalFileStreamWriter() { |
36 // Invalidate weak pointers so that we won't receive any callbacks from | 31 // Invalidate weak pointers so that we won't receive any callbacks from |
37 // in-flight stream operations, which might be triggered during the file close | 32 // in-flight stream operations, which might be triggered during the file close |
38 // in the FileStream destructor. | 33 // in the FileStream destructor. |
39 weak_factory_.InvalidateWeakPtrs(); | 34 weak_factory_.InvalidateWeakPtrs(); |
40 | 35 |
41 // FileStream's destructor closes the file safely, since we opened the file | 36 // FileStream's destructor closes the file safely, since we opened the file |
42 // by its Open() method. | 37 // by its Open() method. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 | 74 |
80 has_pending_operation_ = true; | 75 has_pending_operation_ = true; |
81 int result = InitiateFlush(callback); | 76 int result = InitiateFlush(callback); |
82 if (result != net::ERR_IO_PENDING) | 77 if (result != net::ERR_IO_PENDING) |
83 has_pending_operation_ = false; | 78 has_pending_operation_ = false; |
84 return result; | 79 return result; |
85 } | 80 } |
86 | 81 |
87 LocalFileStreamWriter::LocalFileStreamWriter(base::TaskRunner* task_runner, | 82 LocalFileStreamWriter::LocalFileStreamWriter(base::TaskRunner* task_runner, |
88 const base::FilePath& file_path, | 83 const base::FilePath& file_path, |
89 int64 initial_offset, | 84 int64 initial_offset) |
90 OpenOrCreate open_or_create) | |
91 : file_path_(file_path), | 85 : file_path_(file_path), |
92 open_or_create_(open_or_create), | |
93 initial_offset_(initial_offset), | 86 initial_offset_(initial_offset), |
94 task_runner_(task_runner), | 87 task_runner_(task_runner), |
95 has_pending_operation_(false), | 88 has_pending_operation_(false), |
96 weak_factory_(this) {} | 89 weak_factory_(this) {} |
97 | 90 |
98 int LocalFileStreamWriter::InitiateOpen( | 91 int LocalFileStreamWriter::InitiateOpen( |
99 const net::CompletionCallback& error_callback, | 92 const net::CompletionCallback& error_callback, |
100 const base::Closure& main_operation) { | 93 const base::Closure& main_operation) { |
101 DCHECK(has_pending_operation_); | 94 DCHECK(has_pending_operation_); |
102 DCHECK(!stream_impl_.get()); | 95 DCHECK(!stream_impl_.get()); |
103 | 96 |
104 stream_impl_.reset(new net::FileStream(NULL, task_runner_)); | 97 stream_impl_.reset(new net::FileStream(NULL, task_runner_)); |
105 | 98 |
106 int open_flags = 0; | |
107 switch (open_or_create_) { | |
108 case OPEN_EXISTING_FILE: | |
109 open_flags = kOpenFlagsForWrite; | |
110 break; | |
111 case CREATE_NEW_FILE: | |
112 open_flags = kCreateFlagsForWrite; | |
113 break; | |
114 } | |
115 | |
116 return stream_impl_->Open(file_path_, | 99 return stream_impl_->Open(file_path_, |
117 open_flags, | 100 kOpenFlagsForWrite, |
118 base::Bind(&LocalFileStreamWriter::DidOpen, | 101 base::Bind(&LocalFileStreamWriter::DidOpen, |
119 weak_factory_.GetWeakPtr(), | 102 weak_factory_.GetWeakPtr(), |
120 error_callback, | 103 error_callback, |
121 main_operation)); | 104 main_operation)); |
122 } | 105 } |
123 | 106 |
124 void LocalFileStreamWriter::DidOpen( | 107 void LocalFileStreamWriter::DidOpen( |
125 const net::CompletionCallback& error_callback, | 108 const net::CompletionCallback& error_callback, |
126 const base::Closure& main_operation, | 109 const base::Closure& main_operation, |
127 int result) { | 110 int result) { |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 return false; | 231 return false; |
249 | 232 |
250 net::CompletionCallback pending_cancel = cancel_callback_; | 233 net::CompletionCallback pending_cancel = cancel_callback_; |
251 has_pending_operation_ = false; | 234 has_pending_operation_ = false; |
252 cancel_callback_.Reset(); | 235 cancel_callback_.Reset(); |
253 pending_cancel.Run(net::OK); | 236 pending_cancel.Run(net::OK); |
254 return true; | 237 return true; |
255 } | 238 } |
256 | 239 |
257 } // namespace fileapi | 240 } // namespace fileapi |
OLD | NEW |