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 "net/base/file_stream_context.h" | 5 #include "net/base/file_stream_context.h" |
6 | 6 |
7 #include "base/location.h" | 7 #include "base/location.h" |
8 #include "base/message_loop/message_loop_proxy.h" | 8 #include "base/message_loop/message_loop_proxy.h" |
9 #include "base/task_runner_util.h" | 9 #include "base/task_runner_util.h" |
10 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
13 | 13 |
14 #if defined(OS_ANDROID) | 14 #if defined(OS_ANDROID) |
15 #include "base/android/content_uri_utils.h" | 15 #include "base/android/content_uri_utils.h" |
16 #endif | 16 #endif |
17 | 17 |
18 namespace net { | 18 namespace net { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 void CallInt64ToInt(const CompletionCallback& callback, int64 result) { | 22 void CallInt64ToInt(const CompletionCallback& callback, int64 result) { |
23 callback.Run(static_cast<int>(result)); | 23 callback.Run(static_cast<int>(result)); |
24 } | 24 } |
25 | 25 |
26 } // namespace | 26 } // namespace |
27 | 27 |
28 FileStream::Context::IOResult::IOResult() | 28 FileStream::Context::IOResult::IOResult() : result(OK), os_error(0) { |
29 : result(OK), | |
30 os_error(0) { | |
31 } | 29 } |
32 | 30 |
33 FileStream::Context::IOResult::IOResult(int64 result, int os_error) | 31 FileStream::Context::IOResult::IOResult(int64 result, int os_error) |
34 : result(result), | 32 : result(result), os_error(os_error) { |
35 os_error(os_error) { | |
36 } | 33 } |
37 | 34 |
38 // static | 35 // static |
39 FileStream::Context::IOResult FileStream::Context::IOResult::FromOSError( | 36 FileStream::Context::IOResult FileStream::Context::IOResult::FromOSError( |
40 int64 os_error) { | 37 int64 os_error) { |
41 return IOResult(MapSystemError(os_error), os_error); | 38 return IOResult(MapSystemError(os_error), os_error); |
42 } | 39 } |
43 | 40 |
44 // --------------------------------------------------------------------- | 41 // --------------------------------------------------------------------- |
45 | 42 |
46 FileStream::Context::OpenResult::OpenResult() { | 43 FileStream::Context::OpenResult::OpenResult() { |
47 } | 44 } |
48 | 45 |
49 FileStream::Context::OpenResult::OpenResult(base::File file, | 46 FileStream::Context::OpenResult::OpenResult(base::File file, |
50 IOResult error_code) | 47 IOResult error_code) |
51 : file(file.Pass()), | 48 : file(file.Pass()), error_code(error_code) { |
52 error_code(error_code) { | |
53 } | 49 } |
54 | 50 |
55 FileStream::Context::OpenResult::OpenResult(RValue other) | 51 FileStream::Context::OpenResult::OpenResult(RValue other) |
56 : file(other.object->file.Pass()), | 52 : file(other.object->file.Pass()), error_code(other.object->error_code) { |
57 error_code(other.object->error_code) { | |
58 } | 53 } |
59 | 54 |
60 FileStream::Context::OpenResult& FileStream::Context::OpenResult::operator=( | 55 FileStream::Context::OpenResult& FileStream::Context::OpenResult::operator=( |
61 RValue other) { | 56 RValue other) { |
62 if (this != other.object) { | 57 if (this != other.object) { |
63 file = other.object->file.Pass(); | 58 file = other.object->file.Pass(); |
64 error_code = other.object->error_code; | 59 error_code = other.object->error_code; |
65 } | 60 } |
66 return *this; | 61 return *this; |
67 } | 62 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 void FileStream::Context::SeekAsync(Whence whence, | 108 void FileStream::Context::SeekAsync(Whence whence, |
114 int64 offset, | 109 int64 offset, |
115 const Int64CompletionCallback& callback) { | 110 const Int64CompletionCallback& callback) { |
116 DCHECK(!async_in_progress_); | 111 DCHECK(!async_in_progress_); |
117 | 112 |
118 bool posted = base::PostTaskAndReplyWithResult( | 113 bool posted = base::PostTaskAndReplyWithResult( |
119 task_runner_.get(), | 114 task_runner_.get(), |
120 FROM_HERE, | 115 FROM_HERE, |
121 base::Bind( | 116 base::Bind( |
122 &Context::SeekFileImpl, base::Unretained(this), whence, offset), | 117 &Context::SeekFileImpl, base::Unretained(this), whence, offset), |
123 base::Bind(&Context::OnAsyncCompleted, | 118 base::Bind(&Context::OnAsyncCompleted, base::Unretained(this), callback)); |
124 base::Unretained(this), | |
125 callback)); | |
126 DCHECK(posted); | 119 DCHECK(posted); |
127 | 120 |
128 async_in_progress_ = true; | 121 async_in_progress_ = true; |
129 } | 122 } |
130 | 123 |
131 void FileStream::Context::FlushAsync(const CompletionCallback& callback) { | 124 void FileStream::Context::FlushAsync(const CompletionCallback& callback) { |
132 DCHECK(!async_in_progress_); | 125 DCHECK(!async_in_progress_); |
133 | 126 |
134 bool posted = base::PostTaskAndReplyWithResult( | 127 bool posted = base::PostTaskAndReplyWithResult( |
135 task_runner_.get(), | 128 task_runner_.get(), |
136 FROM_HERE, | 129 FROM_HERE, |
137 base::Bind(&Context::FlushFileImpl, base::Unretained(this)), | 130 base::Bind(&Context::FlushFileImpl, base::Unretained(this)), |
138 base::Bind(&Context::OnAsyncCompleted, | 131 base::Bind(&Context::OnAsyncCompleted, |
139 base::Unretained(this), | 132 base::Unretained(this), |
140 IntToInt64(callback))); | 133 IntToInt64(callback))); |
141 DCHECK(posted); | 134 DCHECK(posted); |
142 | 135 |
143 async_in_progress_ = true; | 136 async_in_progress_ = true; |
144 } | 137 } |
145 | 138 |
146 FileStream::Context::OpenResult FileStream::Context::OpenFileImpl( | 139 FileStream::Context::OpenResult FileStream::Context::OpenFileImpl( |
147 const base::FilePath& path, int open_flags) { | 140 const base::FilePath& path, |
| 141 int open_flags) { |
148 #if defined(OS_POSIX) | 142 #if defined(OS_POSIX) |
149 // Always use blocking IO. | 143 // Always use blocking IO. |
150 open_flags &= ~base::File::FLAG_ASYNC; | 144 open_flags &= ~base::File::FLAG_ASYNC; |
151 #endif | 145 #endif |
152 base::File file; | 146 base::File file; |
153 #if defined(OS_ANDROID) | 147 #if defined(OS_ANDROID) |
154 if (path.IsContentUri()) { | 148 if (path.IsContentUri()) { |
155 // Check that only Read flags are set. | 149 // Check that only Read flags are set. |
156 DCHECK_EQ(open_flags & ~base::File::FLAG_ASYNC, | 150 DCHECK_EQ(open_flags & ~base::File::FLAG_ASYNC, |
157 base::File::FLAG_OPEN | base::File::FLAG_READ); | 151 base::File::FLAG_OPEN | base::File::FLAG_READ); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 // should be reset before CloseAsync() because it shouldn't run if any async | 209 // should be reset before CloseAsync() because it shouldn't run if any async |
216 // operation is in progress. | 210 // operation is in progress. |
217 async_in_progress_ = false; | 211 async_in_progress_ = false; |
218 if (orphaned_) | 212 if (orphaned_) |
219 CloseAndDelete(); | 213 CloseAndDelete(); |
220 else | 214 else |
221 callback.Run(result.result); | 215 callback.Run(result.result); |
222 } | 216 } |
223 | 217 |
224 } // namespace net | 218 } // namespace net |
OLD | NEW |