OLD | NEW |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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/blob/blob_data_builder.h" | 5 #include "storage/browser/blob/blob_data_builder.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <utility> |
9 | 10 |
10 #include "base/numerics/safe_conversions.h" | 11 #include "base/numerics/safe_conversions.h" |
11 #include "base/numerics/safe_math.h" | 12 #include "base/numerics/safe_math.h" |
12 #include "base/time/time.h" | 13 #include "base/time/time.h" |
13 #include "net/disk_cache/disk_cache.h" | 14 #include "net/disk_cache/disk_cache.h" |
14 #include "storage/browser/blob/shareable_file_reference.h" | 15 #include "storage/browser/blob/shareable_file_reference.h" |
15 | 16 |
16 namespace storage { | 17 namespace storage { |
17 | 18 |
18 const char BlobDataBuilder::kAppendFutureFileTemporaryFileName[] = | 19 const char BlobDataBuilder::kAppendFutureFileTemporaryFileName[] = |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 NOTREACHED(); | 51 NOTREACHED(); |
51 break; | 52 break; |
52 } | 53 } |
53 } | 54 } |
54 | 55 |
55 void BlobDataBuilder::AppendData(const char* data, size_t length) { | 56 void BlobDataBuilder::AppendData(const char* data, size_t length) { |
56 if (!length) | 57 if (!length) |
57 return; | 58 return; |
58 scoped_ptr<DataElement> element(new DataElement()); | 59 scoped_ptr<DataElement> element(new DataElement()); |
59 element->SetToBytes(data, length); | 60 element->SetToBytes(data, length); |
60 items_.push_back(new BlobDataItem(element.Pass())); | 61 items_.push_back(new BlobDataItem(std::move(element))); |
61 } | 62 } |
62 | 63 |
63 size_t BlobDataBuilder::AppendFutureData(size_t length) { | 64 size_t BlobDataBuilder::AppendFutureData(size_t length) { |
64 CHECK_NE(length, 0u); | 65 CHECK_NE(length, 0u); |
65 scoped_ptr<DataElement> element(new DataElement()); | 66 scoped_ptr<DataElement> element(new DataElement()); |
66 element->SetToBytesDescription(length); | 67 element->SetToBytesDescription(length); |
67 items_.push_back(new BlobDataItem(element.Pass())); | 68 items_.push_back(new BlobDataItem(std::move(element))); |
68 return items_.size() - 1; | 69 return items_.size() - 1; |
69 } | 70 } |
70 | 71 |
71 bool BlobDataBuilder::PopulateFutureData(size_t index, | 72 bool BlobDataBuilder::PopulateFutureData(size_t index, |
72 const char* data, | 73 const char* data, |
73 size_t offset, | 74 size_t offset, |
74 size_t length) { | 75 size_t length) { |
75 DCHECK_LT(index, items_.size()); | 76 DCHECK_LT(index, items_.size()); |
76 DCHECK(data); | 77 DCHECK(data); |
77 DataElement* element = items_.at(index)->data_element_ptr(); | 78 DataElement* element = items_.at(index)->data_element_ptr(); |
(...skipping 22 matching lines...) Expand all Loading... |
100 std::memcpy(element->mutable_bytes() + offset, data, length); | 101 std::memcpy(element->mutable_bytes() + offset, data, length); |
101 return true; | 102 return true; |
102 } | 103 } |
103 | 104 |
104 size_t BlobDataBuilder::AppendFutureFile(uint64_t offset, uint64_t length) { | 105 size_t BlobDataBuilder::AppendFutureFile(uint64_t offset, uint64_t length) { |
105 CHECK_NE(length, 0ull); | 106 CHECK_NE(length, 0ull); |
106 scoped_ptr<DataElement> element(new DataElement()); | 107 scoped_ptr<DataElement> element(new DataElement()); |
107 element->SetToFilePathRange(base::FilePath::FromUTF8Unsafe(std::string( | 108 element->SetToFilePathRange(base::FilePath::FromUTF8Unsafe(std::string( |
108 kAppendFutureFileTemporaryFileName)), | 109 kAppendFutureFileTemporaryFileName)), |
109 offset, length, base::Time()); | 110 offset, length, base::Time()); |
110 items_.push_back(new BlobDataItem(element.Pass())); | 111 items_.push_back(new BlobDataItem(std::move(element))); |
111 return items_.size() - 1; | 112 return items_.size() - 1; |
112 } | 113 } |
113 | 114 |
114 bool BlobDataBuilder::PopulateFutureFile( | 115 bool BlobDataBuilder::PopulateFutureFile( |
115 size_t index, | 116 size_t index, |
116 const scoped_refptr<ShareableFileReference>& file_reference, | 117 const scoped_refptr<ShareableFileReference>& file_reference, |
117 const base::Time& expected_modification_time) { | 118 const base::Time& expected_modification_time) { |
118 DCHECK_LT(index, items_.size()); | 119 DCHECK_LT(index, items_.size()); |
119 DataElement* old_element = items_.at(index)->data_element_ptr(); | 120 DataElement* old_element = items_.at(index)->data_element_ptr(); |
120 | 121 |
121 if (old_element->type() != DataElement::TYPE_FILE) { | 122 if (old_element->type() != DataElement::TYPE_FILE) { |
122 DVLOG(1) << "Invalid item type."; | 123 DVLOG(1) << "Invalid item type."; |
123 return false; | 124 return false; |
124 } else if (old_element->path().AsUTF8Unsafe() != | 125 } else if (old_element->path().AsUTF8Unsafe() != |
125 std::string(kAppendFutureFileTemporaryFileName)) { | 126 std::string(kAppendFutureFileTemporaryFileName)) { |
126 DVLOG(1) << "Item not created by AppendFutureFile"; | 127 DVLOG(1) << "Item not created by AppendFutureFile"; |
127 return false; | 128 return false; |
128 } | 129 } |
129 uint64_t length = old_element->length(); | 130 uint64_t length = old_element->length(); |
130 uint64_t offset = old_element->offset(); | 131 uint64_t offset = old_element->offset(); |
131 scoped_ptr<DataElement> element(new DataElement()); | 132 scoped_ptr<DataElement> element(new DataElement()); |
132 element->SetToFilePathRange(file_reference->path(), offset, length, | 133 element->SetToFilePathRange(file_reference->path(), offset, length, |
133 expected_modification_time); | 134 expected_modification_time); |
134 items_[index] = new BlobDataItem(element.Pass(), file_reference); | 135 items_[index] = new BlobDataItem(std::move(element), file_reference); |
135 return true; | 136 return true; |
136 } | 137 } |
137 | 138 |
138 void BlobDataBuilder::AppendFile(const base::FilePath& file_path, | 139 void BlobDataBuilder::AppendFile(const base::FilePath& file_path, |
139 uint64_t offset, | 140 uint64_t offset, |
140 uint64_t length, | 141 uint64_t length, |
141 const base::Time& expected_modification_time) { | 142 const base::Time& expected_modification_time) { |
142 scoped_ptr<DataElement> element(new DataElement()); | 143 scoped_ptr<DataElement> element(new DataElement()); |
143 element->SetToFilePathRange(file_path, offset, length, | 144 element->SetToFilePathRange(file_path, offset, length, |
144 expected_modification_time); | 145 expected_modification_time); |
145 items_.push_back( | 146 items_.push_back(new BlobDataItem(std::move(element), |
146 new BlobDataItem(element.Pass(), ShareableFileReference::Get(file_path))); | 147 ShareableFileReference::Get(file_path))); |
147 } | 148 } |
148 | 149 |
149 void BlobDataBuilder::AppendBlob(const std::string& uuid, | 150 void BlobDataBuilder::AppendBlob(const std::string& uuid, |
150 uint64_t offset, | 151 uint64_t offset, |
151 uint64_t length) { | 152 uint64_t length) { |
152 DCHECK_GT(length, 0ul); | 153 DCHECK_GT(length, 0ul); |
153 scoped_ptr<DataElement> element(new DataElement()); | 154 scoped_ptr<DataElement> element(new DataElement()); |
154 element->SetToBlobRange(uuid, offset, length); | 155 element->SetToBlobRange(uuid, offset, length); |
155 items_.push_back(new BlobDataItem(element.Pass())); | 156 items_.push_back(new BlobDataItem(std::move(element))); |
156 } | 157 } |
157 | 158 |
158 void BlobDataBuilder::AppendBlob(const std::string& uuid) { | 159 void BlobDataBuilder::AppendBlob(const std::string& uuid) { |
159 scoped_ptr<DataElement> element(new DataElement()); | 160 scoped_ptr<DataElement> element(new DataElement()); |
160 element->SetToBlob(uuid); | 161 element->SetToBlob(uuid); |
161 items_.push_back(new BlobDataItem(element.Pass())); | 162 items_.push_back(new BlobDataItem(std::move(element))); |
162 } | 163 } |
163 | 164 |
164 void BlobDataBuilder::AppendFileSystemFile( | 165 void BlobDataBuilder::AppendFileSystemFile( |
165 const GURL& url, | 166 const GURL& url, |
166 uint64_t offset, | 167 uint64_t offset, |
167 uint64_t length, | 168 uint64_t length, |
168 const base::Time& expected_modification_time) { | 169 const base::Time& expected_modification_time) { |
169 DCHECK_GT(length, 0ul); | 170 DCHECK_GT(length, 0ul); |
170 scoped_ptr<DataElement> element(new DataElement()); | 171 scoped_ptr<DataElement> element(new DataElement()); |
171 element->SetToFileSystemUrlRange(url, offset, length, | 172 element->SetToFileSystemUrlRange(url, offset, length, |
172 expected_modification_time); | 173 expected_modification_time); |
173 items_.push_back(new BlobDataItem(element.Pass())); | 174 items_.push_back(new BlobDataItem(std::move(element))); |
174 } | 175 } |
175 | 176 |
176 void BlobDataBuilder::AppendDiskCacheEntry( | 177 void BlobDataBuilder::AppendDiskCacheEntry( |
177 const scoped_refptr<DataHandle>& data_handle, | 178 const scoped_refptr<DataHandle>& data_handle, |
178 disk_cache::Entry* disk_cache_entry, | 179 disk_cache::Entry* disk_cache_entry, |
179 int disk_cache_stream_index) { | 180 int disk_cache_stream_index) { |
180 scoped_ptr<DataElement> element(new DataElement()); | 181 scoped_ptr<DataElement> element(new DataElement()); |
181 element->SetToDiskCacheEntryRange( | 182 element->SetToDiskCacheEntryRange( |
182 0U, disk_cache_entry->GetDataSize(disk_cache_stream_index)); | 183 0U, disk_cache_entry->GetDataSize(disk_cache_stream_index)); |
183 items_.push_back( | 184 items_.push_back(new BlobDataItem(std::move(element), data_handle, |
184 new BlobDataItem(element.Pass(), data_handle, disk_cache_entry, | 185 disk_cache_entry, disk_cache_stream_index)); |
185 disk_cache_stream_index)); | |
186 } | 186 } |
187 | 187 |
188 void BlobDataBuilder::Clear() { | 188 void BlobDataBuilder::Clear() { |
189 items_.clear(); | 189 items_.clear(); |
190 content_disposition_.clear(); | 190 content_disposition_.clear(); |
191 content_type_.clear(); | 191 content_type_.clear(); |
192 uuid_.clear(); | 192 uuid_.clear(); |
193 } | 193 } |
194 | 194 |
195 void PrintTo(const BlobDataBuilder& x, std::ostream* os) { | 195 void PrintTo(const BlobDataBuilder& x, std::ostream* os) { |
196 DCHECK(os); | 196 DCHECK(os); |
197 *os << "<BlobDataBuilder>{uuid: " << x.uuid() | 197 *os << "<BlobDataBuilder>{uuid: " << x.uuid() |
198 << ", content_type: " << x.content_type_ | 198 << ", content_type: " << x.content_type_ |
199 << ", content_disposition: " << x.content_disposition_ << ", items: ["; | 199 << ", content_disposition: " << x.content_disposition_ << ", items: ["; |
200 for (const auto& item : x.items_) { | 200 for (const auto& item : x.items_) { |
201 PrintTo(*item, os); | 201 PrintTo(*item, os); |
202 *os << ", "; | 202 *os << ", "; |
203 } | 203 } |
204 *os << "]}"; | 204 *os << "]}"; |
205 } | 205 } |
206 | 206 |
207 } // namespace storage | 207 } // namespace storage |
OLD | NEW |