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 "storage/browser/fileapi/local_file_stream_writer.h" | 5 #include "storage/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 "base/profiler/scoped_tracker.h" | 9 #include "base/profiler/scoped_tracker.h" |
10 #include "net/base/file_stream.h" | 10 #include "net/base/file_stream.h" |
11 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
13 | 13 |
14 namespace storage { | 14 namespace storage { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 const int kOpenFlagsForWrite = base::File::FLAG_OPEN | | 18 const int kOpenFlagsForWrite = base::File::FLAG_OPEN | |
19 base::File::FLAG_WRITE | | 19 base::File::FLAG_WRITE | |
20 base::File::FLAG_ASYNC; | 20 base::File::FLAG_ASYNC; |
21 const int kCreateFlagsForWrite = base::File::FLAG_CREATE | | |
22 base::File::FLAG_WRITE | | |
23 base::File::FLAG_ASYNC; | |
24 | 21 |
25 } // namespace | 22 } // namespace |
26 | 23 |
27 FileStreamWriter* FileStreamWriter::CreateForLocalFile( | 24 FileStreamWriter* FileStreamWriter::CreateForLocalFile( |
28 base::TaskRunner* task_runner, | 25 base::TaskRunner* task_runner, |
29 const base::FilePath& file_path, | 26 const base::FilePath& file_path, |
30 int64 initial_offset, | 27 int64 initial_offset) { |
31 OpenOrCreate open_or_create) { | 28 return new LocalFileStreamWriter(task_runner, file_path, initial_offset); |
32 return new LocalFileStreamWriter( | |
33 task_runner, file_path, initial_offset, open_or_create); | |
34 } | 29 } |
35 | 30 |
36 LocalFileStreamWriter::~LocalFileStreamWriter() { | 31 LocalFileStreamWriter::~LocalFileStreamWriter() { |
37 // Invalidate weak pointers so that we won't receive any callbacks from | 32 // Invalidate weak pointers so that we won't receive any callbacks from |
38 // in-flight stream operations, which might be triggered during the file close | 33 // in-flight stream operations, which might be triggered during the file close |
39 // in the FileStream destructor. | 34 // in the FileStream destructor. |
40 weak_factory_.InvalidateWeakPtrs(); | 35 weak_factory_.InvalidateWeakPtrs(); |
41 | 36 |
42 // FileStream's destructor closes the file safely, since we opened the file | 37 // FileStream's destructor closes the file safely, since we opened the file |
43 // by its Open() method. | 38 // by its Open() method. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 | 75 |
81 has_pending_operation_ = true; | 76 has_pending_operation_ = true; |
82 int result = InitiateFlush(callback); | 77 int result = InitiateFlush(callback); |
83 if (result != net::ERR_IO_PENDING) | 78 if (result != net::ERR_IO_PENDING) |
84 has_pending_operation_ = false; | 79 has_pending_operation_ = false; |
85 return result; | 80 return result; |
86 } | 81 } |
87 | 82 |
88 LocalFileStreamWriter::LocalFileStreamWriter(base::TaskRunner* task_runner, | 83 LocalFileStreamWriter::LocalFileStreamWriter(base::TaskRunner* task_runner, |
89 const base::FilePath& file_path, | 84 const base::FilePath& file_path, |
90 int64 initial_offset, | 85 int64 initial_offset) |
91 OpenOrCreate open_or_create) | |
92 : file_path_(file_path), | 86 : file_path_(file_path), |
93 open_or_create_(open_or_create), | |
94 initial_offset_(initial_offset), | 87 initial_offset_(initial_offset), |
95 task_runner_(task_runner), | 88 task_runner_(task_runner), |
96 has_pending_operation_(false), | 89 has_pending_operation_(false), |
97 weak_factory_(this) {} | 90 weak_factory_(this) { |
| 91 } |
98 | 92 |
99 int LocalFileStreamWriter::InitiateOpen( | 93 int LocalFileStreamWriter::InitiateOpen( |
100 const net::CompletionCallback& error_callback, | 94 const net::CompletionCallback& error_callback, |
101 const base::Closure& main_operation) { | 95 const base::Closure& main_operation) { |
102 DCHECK(has_pending_operation_); | 96 DCHECK(has_pending_operation_); |
103 DCHECK(!stream_impl_.get()); | 97 DCHECK(!stream_impl_.get()); |
104 | 98 |
105 stream_impl_.reset(new net::FileStream(task_runner_)); | 99 stream_impl_.reset(new net::FileStream(task_runner_)); |
106 | 100 |
107 int open_flags = 0; | 101 return stream_impl_->Open( |
108 switch (open_or_create_) { | 102 file_path_, kOpenFlagsForWrite, |
109 case OPEN_EXISTING_FILE: | 103 base::Bind(&LocalFileStreamWriter::DidOpen, weak_factory_.GetWeakPtr(), |
110 open_flags = kOpenFlagsForWrite; | 104 error_callback, main_operation)); |
111 break; | |
112 case CREATE_NEW_FILE: | |
113 open_flags = kCreateFlagsForWrite; | |
114 break; | |
115 } | |
116 | |
117 return stream_impl_->Open(file_path_, | |
118 open_flags, | |
119 base::Bind(&LocalFileStreamWriter::DidOpen, | |
120 weak_factory_.GetWeakPtr(), | |
121 error_callback, | |
122 main_operation)); | |
123 } | 105 } |
124 | 106 |
125 void LocalFileStreamWriter::DidOpen( | 107 void LocalFileStreamWriter::DidOpen( |
126 const net::CompletionCallback& error_callback, | 108 const net::CompletionCallback& error_callback, |
127 const base::Closure& main_operation, | 109 const base::Closure& main_operation, |
128 int result) { | 110 int result) { |
129 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. | 111 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. |
130 tracked_objects::ScopedTracker tracking_profile( | 112 tracked_objects::ScopedTracker tracking_profile( |
131 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 113 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
132 "423948 LocalFileStreamWriter::DidOpen")); | 114 "423948 LocalFileStreamWriter::DidOpen")); |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 return false; | 236 return false; |
255 | 237 |
256 net::CompletionCallback pending_cancel = cancel_callback_; | 238 net::CompletionCallback pending_cancel = cancel_callback_; |
257 has_pending_operation_ = false; | 239 has_pending_operation_ = false; |
258 cancel_callback_.Reset(); | 240 cancel_callback_.Reset(); |
259 pending_cancel.Run(net::OK); | 241 pending_cancel.Run(net::OK); |
260 return true; | 242 return true; |
261 } | 243 } |
262 | 244 |
263 } // namespace storage | 245 } // namespace storage |
OLD | NEW |