OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/upload_data.h" | 5 #include "net/base/upload_data.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/platform_file.h" |
8 #include "base/logging.h" | 9 #include "base/logging.h" |
9 | 10 |
10 namespace net { | 11 namespace net { |
11 | 12 |
12 uint64 UploadData::GetContentLength() const { | 13 uint64 UploadData::GetContentLength() const { |
13 uint64 len = 0; | 14 uint64 len = 0; |
14 std::vector<Element>::const_iterator it = elements_.begin(); | 15 std::vector<Element>::const_iterator it = elements_.begin(); |
15 for (; it != elements_.end(); ++it) | 16 for (; it != elements_.end(); ++it) |
16 len += (*it).GetContentLength(); | 17 len += (*it).GetContentLength(); |
17 return len; | 18 return len; |
18 } | 19 } |
19 | 20 |
20 uint64 UploadData::Element::GetContentLength() const { | 21 void UploadData::CloseFiles() { |
21 if (override_content_length_) | 22 std::vector<Element>::iterator it = elements_.begin(); |
22 return content_length_; | 23 for (; it != elements_.end(); ++it) { |
| 24 if (it->type() == TYPE_FILE) |
| 25 it->Close(); |
| 26 } |
| 27 } |
23 | 28 |
24 if (type_ == TYPE_BYTES) | 29 base::PlatformFile UploadData::Element::platform_file() const { |
25 return static_cast<uint64>(bytes_.size()); | 30 DCHECK(type_ == TYPE_FILE) << "platform_file on non-file Element"; |
26 | 31 |
27 DCHECK(type_ == TYPE_FILE); | 32 return file_; |
| 33 } |
28 | 34 |
29 // TODO(darin): This size calculation could be out of sync with the state of | 35 void UploadData::Element::Close() { |
30 // the file when we get around to reading it. We should probably find a way | 36 DCHECK(type_ == TYPE_FILE) << "Close on non-file Element"; |
31 // to lock the file or somehow protect against this error condition. | |
32 | 37 |
33 int64 length = 0; | 38 if (file_ != base::kInvalidPlatformFileValue) |
34 if (!file_util::GetFileSize(file_path_, &length)) | 39 base::ClosePlatformFile(file_); |
35 return 0; | 40 file_ = base::kInvalidPlatformFileValue; |
| 41 } |
36 | 42 |
37 if (file_range_offset_ >= static_cast<uint64>(length)) | 43 void UploadData::Element::SetToFilePathRange(const FilePath& path, |
38 return 0; // range is beyond eof | 44 uint64 offset, |
| 45 uint64 length) { |
| 46 type_ = TYPE_FILE; |
| 47 file_range_offset_ = 0; |
| 48 file_range_length_ = 0; |
39 | 49 |
40 // compensate for the offset and clip file_range_length_ to eof | 50 Close(); |
41 return std::min(length - file_range_offset_, file_range_length_); | 51 |
| 52 if (offset + length < offset) { |
| 53 LOG(ERROR) << "Upload file offset and length overflow 64-bits. Ignoring."; |
| 54 return; |
| 55 } |
| 56 |
| 57 base::PlatformFile file = base::CreatePlatformFile( |
| 58 path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, NULL); |
| 59 if (file == base::kInvalidPlatformFileValue) { |
| 60 // This case occurs when the user selects a file that isn't readable. |
| 61 file_path_= path; |
| 62 return; |
| 63 } |
| 64 |
| 65 uint64 file_size; |
| 66 if (!base::GetPlatformFileSize(file, &file_size)) { |
| 67 base::ClosePlatformFile(file); |
| 68 return; |
| 69 } |
| 70 |
| 71 if (offset > file_size) { |
| 72 base::ClosePlatformFile(file); |
| 73 return; |
| 74 } |
| 75 if (offset + length > file_size) |
| 76 length = file_size - offset; |
| 77 |
| 78 file_ = file; |
| 79 file_path_ = path; |
| 80 file_range_offset_ = offset; |
| 81 file_range_length_ = length; |
42 } | 82 } |
43 | 83 |
44 } // namespace net | 84 } // namespace net |
OLD | NEW |