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/upload_data.h" | 5 #include "net/base/upload_data.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/threading/worker_pool.h" | 10 #include "base/threading/worker_pool.h" |
11 | 11 |
12 namespace net { | 12 namespace net { |
13 | 13 |
14 namespace { | |
15 | |
16 // Helper function for GetContentLength(). | |
17 void OnGetContentLengthComplete( | |
18 const UploadData::ContentLengthCallback& callback, | |
19 uint64* content_length) { | |
20 callback.Run(*content_length); | |
21 } | |
22 | |
23 } // namespace | |
24 | |
25 UploadData::UploadData() | 14 UploadData::UploadData() |
26 : identifier_(0), | 15 : identifier_(0), |
27 chunk_callback_(NULL), | 16 chunk_callback_(NULL), |
28 is_chunked_(false), | 17 is_chunked_(false), |
29 last_chunk_appended_(false) { | 18 last_chunk_appended_(false) { |
30 } | 19 } |
31 | 20 |
32 void UploadData::AppendBytes(const char* bytes, int bytes_len) { | 21 void UploadData::AppendBytes(const char* bytes, int bytes_len) { |
33 DCHECK(!is_chunked_); | 22 DCHECK(!is_chunked_); |
34 if (bytes_len > 0) { | 23 if (bytes_len > 0) { |
(...skipping 20 matching lines...) Expand all Loading... |
55 elements_.back().SetToBytes(bytes, bytes_len); | 44 elements_.back().SetToBytes(bytes, bytes_len); |
56 last_chunk_appended_ = is_last_chunk; | 45 last_chunk_appended_ = is_last_chunk; |
57 if (chunk_callback_) | 46 if (chunk_callback_) |
58 chunk_callback_->OnChunkAvailable(); | 47 chunk_callback_->OnChunkAvailable(); |
59 } | 48 } |
60 | 49 |
61 void UploadData::set_chunk_callback(ChunkCallback* callback) { | 50 void UploadData::set_chunk_callback(ChunkCallback* callback) { |
62 chunk_callback_ = callback; | 51 chunk_callback_ = callback; |
63 } | 52 } |
64 | 53 |
65 void UploadData::GetContentLength(const ContentLengthCallback& callback) { | |
66 uint64* result = new uint64(0); | |
67 const bool task_is_slow = true; | |
68 const bool posted = base::WorkerPool::PostTaskAndReply( | |
69 FROM_HERE, | |
70 base::Bind(&UploadData::DoGetContentLength, this, result), | |
71 base::Bind(&OnGetContentLengthComplete, callback, base::Owned(result)), | |
72 task_is_slow); | |
73 DCHECK(posted); | |
74 } | |
75 | |
76 uint64 UploadData::GetContentLengthSync() { | |
77 uint64 content_length = 0; | |
78 DoGetContentLength(&content_length); | |
79 return content_length; | |
80 } | |
81 | |
82 bool UploadData::IsInMemory() const { | |
83 // Chunks are in memory, but UploadData does not have all the chunks at | |
84 // once. Chunks are provided progressively with AppendChunk() as chunks | |
85 // are ready. Check is_chunked_ here, rather than relying on the loop | |
86 // below, as there is a case that is_chunked_ is set to true, but the | |
87 // first chunk is not yet delivered. | |
88 if (is_chunked_) | |
89 return false; | |
90 | |
91 for (size_t i = 0; i < elements_.size(); ++i) { | |
92 if (elements_[i].type() != UploadElement::TYPE_BYTES) | |
93 return false; | |
94 } | |
95 return true; | |
96 } | |
97 | |
98 void UploadData::ResetOffset() { | |
99 for (size_t i = 0; i < elements_.size(); ++i) | |
100 elements_[i].ResetOffset(); | |
101 } | |
102 | |
103 void UploadData::SetElements(const std::vector<UploadElement>& elements) { | 54 void UploadData::SetElements(const std::vector<UploadElement>& elements) { |
104 elements_ = elements; | 55 elements_ = elements; |
105 } | 56 } |
106 | 57 |
107 void UploadData::DoGetContentLength(uint64* content_length) { | |
108 *content_length = 0; | |
109 | |
110 if (is_chunked_) | |
111 return; | |
112 | |
113 for (size_t i = 0; i < elements_.size(); ++i) | |
114 *content_length += elements_[i].GetContentLength(); | |
115 } | |
116 | |
117 UploadData::~UploadData() { | 58 UploadData::~UploadData() { |
118 } | 59 } |
119 | 60 |
120 } // namespace net | 61 } // namespace net |
OLD | NEW |