OLD | NEW |
---|---|
1 // Copyright (c) 2011 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.h" | 5 #include "net/base/file_stream.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 | 8 |
9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
13 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
14 #include "net/base/file_stream_metrics.h" | 14 #include "net/base/file_stream_metrics.h" |
15 #include "net/base/file_stream_net_log_parameters.h" | |
15 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
16 | 17 |
17 namespace net { | 18 namespace net { |
18 | 19 |
19 // Ensure that we can just use our Whence values directly. | 20 // Ensure that we can just use our Whence values directly. |
20 COMPILE_ASSERT(FROM_BEGIN == FILE_BEGIN, bad_whence_begin); | 21 COMPILE_ASSERT(FROM_BEGIN == FILE_BEGIN, bad_whence_begin); |
21 COMPILE_ASSERT(FROM_CURRENT == FILE_CURRENT, bad_whence_current); | 22 COMPILE_ASSERT(FROM_CURRENT == FILE_CURRENT, bad_whence_current); |
22 COMPILE_ASSERT(FROM_END == FILE_END, bad_whence_end); | 23 COMPILE_ASSERT(FROM_END == FILE_END, bad_whence_end); |
23 | 24 |
24 static void SetOffset(OVERLAPPED* overlapped, const LARGE_INTEGER& offset) { | 25 static void SetOffset(OVERLAPPED* overlapped, const LARGE_INTEGER& offset) { |
25 overlapped->Offset = offset.LowPart; | 26 overlapped->Offset = offset.LowPart; |
26 overlapped->OffsetHigh = offset.HighPart; | 27 overlapped->OffsetHigh = offset.HighPart; |
27 } | 28 } |
28 | 29 |
29 static void IncrementOffset(OVERLAPPED* overlapped, DWORD count) { | 30 static void IncrementOffset(OVERLAPPED* overlapped, DWORD count) { |
30 LARGE_INTEGER offset; | 31 LARGE_INTEGER offset; |
31 offset.LowPart = overlapped->Offset; | 32 offset.LowPart = overlapped->Offset; |
32 offset.HighPart = overlapped->OffsetHigh; | 33 offset.HighPart = overlapped->OffsetHigh; |
33 offset.QuadPart += static_cast<LONGLONG>(count); | 34 offset.QuadPart += static_cast<LONGLONG>(count); |
34 SetOffset(overlapped, offset); | 35 SetOffset(overlapped, offset); |
35 } | 36 } |
36 | 37 |
37 namespace { | 38 namespace { |
38 | 39 |
39 int RecordAndMapError(int error, FileErrorSource source, bool record_uma) { | 40 int RecordAndMapError(int error, |
41 FileErrorSource source, | |
42 bool record_uma, | |
43 const net::BoundNetLog& bound_net_log) { | |
44 bound_net_log.AddEvent( | |
45 net::NetLog::TYPE_FILE_STREAM_ERROR, | |
46 make_scoped_refptr( | |
47 new FileStreamErrorParameters( | |
48 GetFileErrorSourceName(source), | |
49 error, | |
50 MapSystemError(error)))); | |
51 | |
40 RecordFileError(error, source, record_uma); | 52 RecordFileError(error, source, record_uma); |
41 return MapSystemError(error); | 53 return MapSystemError(error); |
42 } | 54 } |
43 | 55 |
44 } // namespace | 56 } // namespace |
45 | 57 |
46 // FileStream::AsyncContext ---------------------------------------------- | 58 // FileStream::AsyncContext ---------------------------------------------- |
47 | 59 |
48 class FileStream::AsyncContext : public MessageLoopForIO::IOHandler { | 60 class FileStream::AsyncContext : public MessageLoopForIO::IOHandler { |
49 public: | 61 public: |
50 AsyncContext(FileStream* owner) | 62 explicit AsyncContext(const net::BoundNetLog& bound_net_log) |
51 : owner_(owner), context_(), is_closing_(false), | 63 : context_(), is_closing_(false), |
52 record_uma_(false), error_source_(FILE_ERROR_SOURCE_COUNT) { | 64 record_uma_(false), bound_net_log_(bound_net_log), |
65 error_source_(FILE_ERROR_SOURCE_COUNT) { | |
53 context_.handler = this; | 66 context_.handler = this; |
54 } | 67 } |
55 ~AsyncContext(); | 68 ~AsyncContext(); |
56 | 69 |
57 void IOCompletionIsPending(const CompletionCallback& callback); | 70 void IOCompletionIsPending(const CompletionCallback& callback); |
58 | 71 |
59 OVERLAPPED* overlapped() { return &context_.overlapped; } | 72 OVERLAPPED* overlapped() { return &context_.overlapped; } |
60 const CompletionCallback& callback() const { return callback_; } | 73 const CompletionCallback& callback() const { return callback_; } |
61 | 74 |
62 void set_error_source(FileErrorSource source) { error_source_ = source; } | 75 void set_error_source(FileErrorSource source) { error_source_ = source; } |
63 | 76 |
64 void EnableErrorStatistics() { | 77 void EnableErrorStatistics() { |
65 record_uma_ = true; | 78 record_uma_ = true; |
66 } | 79 } |
67 | 80 |
68 private: | 81 private: |
69 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, | 82 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, |
70 DWORD bytes_read, DWORD error); | 83 DWORD bytes_read, DWORD error); |
71 | 84 |
72 FileStream* owner_; | |
73 MessageLoopForIO::IOContext context_; | 85 MessageLoopForIO::IOContext context_; |
74 CompletionCallback callback_; | 86 CompletionCallback callback_; |
75 bool is_closing_; | 87 bool is_closing_; |
76 bool record_uma_; | 88 bool record_uma_; |
89 net::BoundNetLog bound_net_log_; | |
77 FileErrorSource error_source_; | 90 FileErrorSource error_source_; |
78 }; | 91 }; |
79 | 92 |
80 FileStream::AsyncContext::~AsyncContext() { | 93 FileStream::AsyncContext::~AsyncContext() { |
81 is_closing_ = true; | 94 is_closing_ = true; |
82 bool waited = false; | 95 bool waited = false; |
83 base::TimeTicks start = base::TimeTicks::Now(); | 96 base::TimeTicks start = base::TimeTicks::Now(); |
84 while (!callback_.is_null()) { | 97 while (!callback_.is_null()) { |
85 waited = true; | 98 waited = true; |
86 MessageLoopForIO::current()->WaitForIOCompletion(INFINITE, this); | 99 MessageLoopForIO::current()->WaitForIOCompletion(INFINITE, this); |
(...skipping 15 matching lines...) Expand all Loading... | |
102 MessageLoopForIO::IOContext* context, DWORD bytes_read, DWORD error) { | 115 MessageLoopForIO::IOContext* context, DWORD bytes_read, DWORD error) { |
103 DCHECK_EQ(&context_, context); | 116 DCHECK_EQ(&context_, context); |
104 DCHECK(!callback_.is_null()); | 117 DCHECK(!callback_.is_null()); |
105 | 118 |
106 if (is_closing_) { | 119 if (is_closing_) { |
107 callback_.Reset(); | 120 callback_.Reset(); |
108 return; | 121 return; |
109 } | 122 } |
110 | 123 |
111 int result = static_cast<int>(bytes_read); | 124 int result = static_cast<int>(bytes_read); |
112 if (error && error != ERROR_HANDLE_EOF) | 125 if (error && error != ERROR_HANDLE_EOF) { |
113 result = RecordAndMapError(error, error_source_, record_uma_); | 126 result = RecordAndMapError(error, error_source_, record_uma_, |
127 bound_net_log_); | |
128 } | |
114 | 129 |
115 if (bytes_read) | 130 if (bytes_read) |
116 IncrementOffset(&context->overlapped, bytes_read); | 131 IncrementOffset(&context->overlapped, bytes_read); |
117 | 132 |
118 CompletionCallback temp; | 133 CompletionCallback temp; |
119 std::swap(temp, callback_); | 134 std::swap(temp, callback_); |
120 temp.Run(result); | 135 temp.Run(result); |
121 } | 136 } |
122 | 137 |
123 // FileStream ------------------------------------------------------------ | 138 // FileStream ------------------------------------------------------------ |
124 | 139 |
125 FileStream::FileStream() | 140 FileStream::FileStream(net::NetLog* net_log) |
126 : file_(INVALID_HANDLE_VALUE), | 141 : file_(base::kInvalidPlatformFileValue), |
127 open_flags_(0), | 142 open_flags_(0), |
128 auto_closed_(true), | 143 auto_closed_(true), |
129 record_uma_(false) { | 144 record_uma_(false), |
145 bound_net_log_(net::BoundNetLog::Make(net_log, | |
146 net::NetLog::SOURCE_FILESTREAM)) { | |
147 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE, NULL); | |
130 } | 148 } |
131 | 149 |
132 FileStream::FileStream(base::PlatformFile file, int flags) | 150 FileStream::FileStream(base::PlatformFile file, int flags, net::NetLog* net_log) |
133 : file_(file), | 151 : file_(file), |
134 open_flags_(flags), | 152 open_flags_(flags), |
135 auto_closed_(false), | 153 auto_closed_(false), |
136 record_uma_(false) { | 154 record_uma_(false), |
155 bound_net_log_(net::BoundNetLog::Make(net_log, | |
156 net::NetLog::SOURCE_FILESTREAM)) { | |
157 bound_net_log_.BeginEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE, NULL); | |
158 | |
137 // If the file handle is opened with base::PLATFORM_FILE_ASYNC, we need to | 159 // If the file handle is opened with base::PLATFORM_FILE_ASYNC, we need to |
138 // make sure we will perform asynchronous File IO to it. | 160 // make sure we will perform asynchronous File IO to it. |
139 if (flags & base::PLATFORM_FILE_ASYNC) { | 161 if (flags & base::PLATFORM_FILE_ASYNC) { |
140 async_context_.reset(new AsyncContext(this)); | 162 async_context_.reset(new AsyncContext(bound_net_log_)); |
141 MessageLoopForIO::current()->RegisterIOHandler(file_, | 163 MessageLoopForIO::current()->RegisterIOHandler(file_, |
142 async_context_.get()); | 164 async_context_.get()); |
143 } | 165 } |
144 } | 166 } |
145 | 167 |
146 FileStream::~FileStream() { | 168 FileStream::~FileStream() { |
147 if (auto_closed_) | 169 if (auto_closed_) |
148 Close(); | 170 Close(); |
171 | |
172 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_ALIVE, NULL); | |
149 } | 173 } |
150 | 174 |
151 void FileStream::Close() { | 175 void FileStream::Close() { |
176 bound_net_log_.AddEvent(net::NetLog::TYPE_FILE_STREAM_CLOSE, NULL); | |
152 if (file_ != INVALID_HANDLE_VALUE) | 177 if (file_ != INVALID_HANDLE_VALUE) |
153 CancelIo(file_); | 178 CancelIo(file_); |
154 | 179 |
155 async_context_.reset(); | 180 async_context_.reset(); |
156 if (file_ != INVALID_HANDLE_VALUE) { | 181 if (file_ != INVALID_HANDLE_VALUE) { |
157 CloseHandle(file_); | 182 CloseHandle(file_); |
158 file_ = INVALID_HANDLE_VALUE; | 183 file_ = INVALID_HANDLE_VALUE; |
184 | |
185 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN, NULL); | |
159 } | 186 } |
160 } | 187 } |
161 | 188 |
162 int FileStream::Open(const FilePath& path, int open_flags) { | 189 int FileStream::Open(const FilePath& path, int open_flags) { |
163 if (IsOpen()) { | 190 if (IsOpen()) { |
164 DLOG(FATAL) << "File is already open!"; | 191 DLOG(FATAL) << "File is already open!"; |
165 return ERR_UNEXPECTED; | 192 return ERR_UNEXPECTED; |
166 } | 193 } |
167 | 194 |
195 bound_net_log_.BeginEvent( | |
196 net::NetLog::TYPE_FILE_STREAM_OPEN, | |
197 make_scoped_refptr( | |
198 new net::NetLogStringParameter("file_name", | |
199 path.AsUTF8Unsafe()))); | |
200 | |
168 open_flags_ = open_flags; | 201 open_flags_ = open_flags; |
169 file_ = base::CreatePlatformFile(path, open_flags_, NULL, NULL); | 202 file_ = base::CreatePlatformFile(path, open_flags_, NULL, NULL); |
170 if (file_ == INVALID_HANDLE_VALUE) { | 203 if (file_ == INVALID_HANDLE_VALUE) { |
171 DWORD error = GetLastError(); | 204 DWORD error = GetLastError(); |
172 LOG(WARNING) << "Failed to open file: " << error; | 205 LOG(WARNING) << "Failed to open file: " << error; |
173 return RecordAndMapError(error, FILE_ERROR_SOURCE_OPEN, record_uma_); | 206 int net_error = RecordAndMapError(error, |
207 FILE_ERROR_SOURCE_OPEN, | |
208 record_uma_, | |
209 bound_net_log_); | |
210 bound_net_log_.EndEvent(net::NetLog::TYPE_FILE_STREAM_OPEN, NULL); | |
211 return net_error; | |
174 } | 212 } |
175 | 213 |
176 if (open_flags_ & base::PLATFORM_FILE_ASYNC) { | 214 if (open_flags_ & base::PLATFORM_FILE_ASYNC) { |
177 async_context_.reset(new AsyncContext(this)); | 215 async_context_.reset(new AsyncContext(bound_net_log_)); |
178 if (record_uma_) | 216 if (record_uma_) |
179 async_context_->EnableErrorStatistics(); | 217 async_context_->EnableErrorStatistics(); |
180 MessageLoopForIO::current()->RegisterIOHandler(file_, | 218 MessageLoopForIO::current()->RegisterIOHandler(file_, |
181 async_context_.get()); | 219 async_context_.get()); |
182 } | 220 } |
183 | 221 |
184 return OK; | 222 return OK; |
185 } | 223 } |
186 | 224 |
187 bool FileStream::IsOpen() const { | 225 bool FileStream::IsOpen() const { |
188 return file_ != INVALID_HANDLE_VALUE; | 226 return file_ != INVALID_HANDLE_VALUE; |
189 } | 227 } |
190 | 228 |
191 int64 FileStream::Seek(Whence whence, int64 offset) { | 229 int64 FileStream::Seek(Whence whence, int64 offset) { |
192 if (!IsOpen()) | 230 if (!IsOpen()) |
193 return ERR_UNEXPECTED; | 231 return ERR_UNEXPECTED; |
194 | 232 |
195 DCHECK(!async_context_.get() || async_context_->callback().is_null()); | 233 DCHECK(!async_context_.get() || async_context_->callback().is_null()); |
196 | 234 |
197 LARGE_INTEGER distance, result; | 235 LARGE_INTEGER distance, result; |
198 distance.QuadPart = offset; | 236 distance.QuadPart = offset; |
199 DWORD move_method = static_cast<DWORD>(whence); | 237 DWORD move_method = static_cast<DWORD>(whence); |
200 if (!SetFilePointerEx(file_, distance, &result, move_method)) { | 238 if (!SetFilePointerEx(file_, distance, &result, move_method)) { |
201 DWORD error = GetLastError(); | 239 DWORD error = GetLastError(); |
202 LOG(WARNING) << "SetFilePointerEx failed: " << error; | 240 LOG(WARNING) << "SetFilePointerEx failed: " << error; |
203 return RecordAndMapError(error, FILE_ERROR_SOURCE_SEEK, record_uma_); | 241 return RecordAndMapError(error, |
242 FILE_ERROR_SOURCE_SEEK, | |
243 record_uma_, | |
244 bound_net_log_); | |
204 } | 245 } |
205 if (async_context_.get()) { | 246 if (async_context_.get()) { |
206 async_context_->set_error_source(FILE_ERROR_SOURCE_SEEK); | 247 async_context_->set_error_source(FILE_ERROR_SOURCE_SEEK); |
207 SetOffset(async_context_->overlapped(), result); | 248 SetOffset(async_context_->overlapped(), result); |
208 } | 249 } |
209 return result.QuadPart; | 250 return result.QuadPart; |
210 } | 251 } |
211 | 252 |
212 int64 FileStream::Available() { | 253 int64 FileStream::Available() { |
213 base::ThreadRestrictions::AssertIOAllowed(); | 254 base::ThreadRestrictions::AssertIOAllowed(); |
214 | 255 |
215 if (!IsOpen()) | 256 if (!IsOpen()) |
216 return ERR_UNEXPECTED; | 257 return ERR_UNEXPECTED; |
217 | 258 |
218 int64 cur_pos = Seek(FROM_CURRENT, 0); | 259 int64 cur_pos = Seek(FROM_CURRENT, 0); |
219 if (cur_pos < 0) | 260 if (cur_pos < 0) |
220 return cur_pos; | 261 return cur_pos; |
221 | 262 |
222 LARGE_INTEGER file_size; | 263 LARGE_INTEGER file_size; |
223 if (!GetFileSizeEx(file_, &file_size)) { | 264 if (!GetFileSizeEx(file_, &file_size)) { |
224 DWORD error = GetLastError(); | 265 DWORD error = GetLastError(); |
225 LOG(WARNING) << "GetFileSizeEx failed: " << error; | 266 LOG(WARNING) << "GetFileSizeEx failed: " << error; |
226 return RecordAndMapError(error, FILE_ERROR_SOURCE_GET_SIZE, record_uma_); | 267 return RecordAndMapError(error, |
268 FILE_ERROR_SOURCE_GET_SIZE, | |
269 record_uma_, | |
270 bound_net_log_); | |
227 } | 271 } |
228 | 272 |
229 return file_size.QuadPart - cur_pos; | 273 return file_size.QuadPart - cur_pos; |
230 } | 274 } |
231 | 275 |
232 int FileStream::Read( | 276 int FileStream::Read( |
233 char* buf, int buf_len, const CompletionCallback& callback) { | 277 char* buf, int buf_len, const CompletionCallback& callback) { |
234 if (!IsOpen()) | 278 if (!IsOpen()) |
235 return ERR_UNEXPECTED; | 279 return ERR_UNEXPECTED; |
236 | 280 |
(...skipping 15 matching lines...) Expand all Loading... | |
252 DWORD bytes_read; | 296 DWORD bytes_read; |
253 if (!ReadFile(file_, buf, buf_len, &bytes_read, overlapped)) { | 297 if (!ReadFile(file_, buf, buf_len, &bytes_read, overlapped)) { |
254 DWORD error = GetLastError(); | 298 DWORD error = GetLastError(); |
255 if (async_context_.get() && error == ERROR_IO_PENDING) { | 299 if (async_context_.get() && error == ERROR_IO_PENDING) { |
256 async_context_->IOCompletionIsPending(callback); | 300 async_context_->IOCompletionIsPending(callback); |
257 rv = ERR_IO_PENDING; | 301 rv = ERR_IO_PENDING; |
258 } else if (error == ERROR_HANDLE_EOF) { | 302 } else if (error == ERROR_HANDLE_EOF) { |
259 rv = 0; // Report EOF by returning 0 bytes read. | 303 rv = 0; // Report EOF by returning 0 bytes read. |
260 } else { | 304 } else { |
261 LOG(WARNING) << "ReadFile failed: " << error; | 305 LOG(WARNING) << "ReadFile failed: " << error; |
262 rv = RecordAndMapError(error, FILE_ERROR_SOURCE_READ, record_uma_); | 306 rv = RecordAndMapError(error, |
307 FILE_ERROR_SOURCE_READ, | |
308 record_uma_, | |
309 bound_net_log_); | |
263 } | 310 } |
264 } else if (overlapped) { | 311 } else if (overlapped) { |
265 async_context_->IOCompletionIsPending(callback); | 312 async_context_->IOCompletionIsPending(callback); |
266 rv = ERR_IO_PENDING; | 313 rv = ERR_IO_PENDING; |
267 } else { | 314 } else { |
268 rv = static_cast<int>(bytes_read); | 315 rv = static_cast<int>(bytes_read); |
269 } | 316 } |
270 return rv; | 317 return rv; |
271 } | 318 } |
272 | 319 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
311 | 358 |
312 int rv; | 359 int rv; |
313 DWORD bytes_written; | 360 DWORD bytes_written; |
314 if (!WriteFile(file_, buf, buf_len, &bytes_written, overlapped)) { | 361 if (!WriteFile(file_, buf, buf_len, &bytes_written, overlapped)) { |
315 DWORD error = GetLastError(); | 362 DWORD error = GetLastError(); |
316 if (async_context_.get() && error == ERROR_IO_PENDING) { | 363 if (async_context_.get() && error == ERROR_IO_PENDING) { |
317 async_context_->IOCompletionIsPending(callback); | 364 async_context_->IOCompletionIsPending(callback); |
318 rv = ERR_IO_PENDING; | 365 rv = ERR_IO_PENDING; |
319 } else { | 366 } else { |
320 LOG(WARNING) << "WriteFile failed: " << error; | 367 LOG(WARNING) << "WriteFile failed: " << error; |
321 rv = RecordAndMapError(error, FILE_ERROR_SOURCE_WRITE, record_uma_); | 368 rv = RecordAndMapError(error, |
369 FILE_ERROR_SOURCE_WRITE, | |
370 record_uma_, | |
371 bound_net_log_); | |
322 } | 372 } |
323 } else if (overlapped) { | 373 } else if (overlapped) { |
324 async_context_->IOCompletionIsPending(callback); | 374 async_context_->IOCompletionIsPending(callback); |
325 rv = ERR_IO_PENDING; | 375 rv = ERR_IO_PENDING; |
326 } else { | 376 } else { |
327 rv = static_cast<int>(bytes_written); | 377 rv = static_cast<int>(bytes_written); |
328 } | 378 } |
329 return rv; | 379 return rv; |
330 } | 380 } |
331 | 381 |
332 int FileStream::Flush() { | 382 int FileStream::Flush() { |
333 base::ThreadRestrictions::AssertIOAllowed(); | 383 base::ThreadRestrictions::AssertIOAllowed(); |
334 | 384 |
335 if (!IsOpen()) | 385 if (!IsOpen()) |
336 return ERR_UNEXPECTED; | 386 return ERR_UNEXPECTED; |
337 | 387 |
338 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | 388 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); |
339 if (FlushFileBuffers(file_)) { | 389 if (FlushFileBuffers(file_)) { |
340 return OK; | 390 return OK; |
341 } | 391 } |
342 | 392 |
343 return RecordAndMapError(GetLastError(), | 393 return RecordAndMapError(GetLastError(), |
344 FILE_ERROR_SOURCE_FLUSH, | 394 FILE_ERROR_SOURCE_FLUSH, |
345 record_uma_); | 395 record_uma_, |
396 bound_net_log_); | |
346 } | 397 } |
347 | 398 |
348 int64 FileStream::Truncate(int64 bytes) { | 399 int64 FileStream::Truncate(int64 bytes) { |
349 base::ThreadRestrictions::AssertIOAllowed(); | 400 base::ThreadRestrictions::AssertIOAllowed(); |
350 | 401 |
351 if (!IsOpen()) | 402 if (!IsOpen()) |
352 return ERR_UNEXPECTED; | 403 return ERR_UNEXPECTED; |
353 | 404 |
354 // We better be open for reading. | 405 // We'd better be open for writing. |
355 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); | 406 DCHECK(open_flags_ & base::PLATFORM_FILE_WRITE); |
356 | 407 |
357 // Seek to the position to truncate from. | 408 // Seek to the position to truncate from. |
358 int64 seek_position = Seek(FROM_BEGIN, bytes); | 409 int64 seek_position = Seek(FROM_BEGIN, bytes); |
359 if (seek_position != bytes) | 410 if (seek_position != bytes) |
360 return ERR_UNEXPECTED; | 411 return ERR_UNEXPECTED; |
361 | 412 |
362 // And truncate the file. | 413 // And truncate the file. |
363 BOOL result = SetEndOfFile(file_); | 414 BOOL result = SetEndOfFile(file_); |
364 if (!result) { | 415 if (!result) { |
365 DWORD error = GetLastError(); | 416 DWORD error = GetLastError(); |
366 LOG(WARNING) << "SetEndOfFile failed: " << error; | 417 LOG(WARNING) << "SetEndOfFile failed: " << error; |
367 return RecordAndMapError(error, FILE_ERROR_SOURCE_SET_EOF, record_uma_); | 418 return RecordAndMapError(error, |
419 FILE_ERROR_SOURCE_SET_EOF, | |
420 record_uma_, | |
421 bound_net_log_); | |
368 } | 422 } |
369 | 423 |
370 // Success. | 424 // Success. |
371 return seek_position; | 425 return seek_position; |
372 } | 426 } |
373 | 427 |
374 void FileStream::EnableErrorStatistics() { | 428 void FileStream::EnableErrorStatistics() { |
375 record_uma_ = true; | 429 record_uma_ = true; |
376 | 430 |
377 if (async_context_.get()) | 431 if (async_context_.get()) |
378 async_context_->EnableErrorStatistics(); | 432 async_context_->EnableErrorStatistics(); |
379 } | 433 } |
380 | 434 |
435 void FileStream::SetBoundNetLogSource( | |
436 const net::BoundNetLog& owner_bound_net_log) { | |
437 if ((owner_bound_net_log.source().id == net::NetLog::Source::kInvalidId) && | |
438 (bound_net_log_.source().id == net::NetLog::Source::kInvalidId)) { | |
439 // Both |BoundNetLog|s are invalid. | |
440 return; | |
441 } | |
442 | |
443 // Should never connect to itself. | |
444 DCHECK_NE(bound_net_log_.source().id, owner_bound_net_log.source().id); | |
445 | |
446 if ((owner_bound_net_log.source().id == net::NetLog::Source::kInvalidId) && | |
447 (bound_net_log_.source().id == net::NetLog::Source::kInvalidId)) { | |
mmenke
2012/01/31 23:20:58
Think you want != here.
ahendrickson
2012/02/01 00:13:29
Removed this portion of the code . . .
| |
448 // Both |BoundNetLog|s are valid. Cross-link the sources. | |
449 bound_net_log_.AddEvent( | |
450 net::NetLog::TYPE_FILE_STREAM_BOUND_TO_OWNER, | |
451 make_scoped_refptr( | |
452 new net::NetLogSourceParameter("source_dependency", | |
453 owner_bound_net_log.source()))); | |
454 | |
455 owner_bound_net_log.AddEvent( | |
456 net::NetLog::TYPE_FILE_STREAM_SOURCE, | |
457 make_scoped_refptr( | |
458 new net::NetLogSourceParameter("source_dependency", | |
459 bound_net_log_.source()))); | |
460 | |
461 return; | |
462 } | |
463 | |
464 if (bound_net_log_.source().id == net::NetLog::Source::kInvalidId) { | |
465 // |bound_net_log_| is invalid. Add an event to the owner. | |
466 owner_bound_net_log.AddEvent( | |
467 net::NetLog::TYPE_FILE_STREAM_SOURCE, | |
468 make_scoped_refptr( | |
469 new net::NetLogStringParameter("source", "Unknown"))); | |
mmenke
2012/01/31 23:20:58
Problem with this is you'd need to update the even
ahendrickson
2012/02/01 00:13:29
OK, I thought it might be a problem. Going back t
| |
470 } else { | |
471 // |owner_bound_net_log| is invalid. Add an event indicating transfer of | |
472 // ownership. | |
473 bound_net_log_.AddEvent( | |
474 net::NetLog::TYPE_FILE_STREAM_BOUND_TO_OWNER, | |
475 make_scoped_refptr(new net::NetLogStringParameter("owner", "Unknown"))); | |
476 } | |
477 } | |
478 | |
381 } // namespace net | 479 } // namespace net |
OLD | NEW |