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 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/numerics/safe_conversions.h" | 13 #include "base/numerics/safe_conversions.h" |
14 #include "base/numerics/safe_math.h" | 14 #include "base/numerics/safe_math.h" |
15 #include "base/strings/string_number_conversions.h" | |
16 #include "base/strings/string_util.h" | |
15 #include "base/time/time.h" | 17 #include "base/time/time.h" |
16 #include "net/disk_cache/disk_cache.h" | 18 #include "net/disk_cache/disk_cache.h" |
17 #include "storage/browser/blob/shareable_file_reference.h" | |
18 | 19 |
19 namespace storage { | 20 namespace storage { |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 const static int kInvalidDiskCacheSideStreamIndex = -1; | 24 const static int kInvalidDiskCacheSideStreamIndex = -1; |
24 | 25 |
25 } // namespace | 26 } // namespace |
26 | 27 |
27 const char BlobDataBuilder::kAppendFutureFileTemporaryFileName[] = | 28 const char kFutureFileName[] = "_future_name_"; |
28 "kFakeFilenameToBeChangedByPopulateFutureFile"; | |
29 | 29 |
30 BlobDataBuilder::BlobDataBuilder(const std::string& uuid) : uuid_(uuid) { | 30 /* static */ |
31 base::FilePath BlobDataBuilder::GetFutureFileItemPath(uint64_t file_id) { | |
32 return base::FilePath::FromUTF8Unsafe(kFutureFileName) | |
33 .AddExtension(base::Uint64ToString(file_id)); | |
31 } | 34 } |
32 BlobDataBuilder::~BlobDataBuilder() { | 35 |
36 /* static */ | |
37 bool BlobDataBuilder::IsFutureFileItem(const DataElement& element) { | |
38 const std::string prefix(kFutureFileName); | |
39 const std::string path = element.path().MaybeAsASCII(); | |
40 return path.size() >= prefix.size() && | |
michaeln
2016/09/27 00:09:29
not sure the size comparison condition needed, Sta
dmurph
2016/09/29 00:44:20
Ah, right.Done.
| |
41 base::StartsWith(path, prefix, base::CompareCase::SENSITIVE); | |
michaeln
2016/09/27 00:09:29
is there any chance that "_future_name_" is valid
dmurph
2016/09/29 00:44:20
I mean, it's technically valid. I can DCHECK in Ap
michaeln
2016/10/04 00:11:54
This seems like something we have to resolve? I do
| |
33 } | 42 } |
34 | 43 |
44 /* static */ | |
45 uint64_t BlobDataBuilder::GetFutureFileID(const DataElement& element) { | |
46 DCHECK(IsFutureFileItem(element)); | |
47 uint64_t id = 0; | |
48 bool success = | |
49 base::StringToUint64(element.path().Extension().substr(1), &id); | |
50 DCHECK(success) << element.path().Extension(); | |
51 return id; | |
52 } | |
53 | |
54 BlobDataBuilder::BlobDataBuilder(const std::string& uuid) : uuid_(uuid) {} | |
55 BlobDataBuilder::~BlobDataBuilder() {} | |
56 | |
35 void BlobDataBuilder::AppendIPCDataElement(const DataElement& ipc_data) { | 57 void BlobDataBuilder::AppendIPCDataElement(const DataElement& ipc_data) { |
36 uint64_t length = ipc_data.length(); | 58 uint64_t length = ipc_data.length(); |
37 switch (ipc_data.type()) { | 59 switch (ipc_data.type()) { |
38 case DataElement::TYPE_BYTES: | 60 case DataElement::TYPE_BYTES: |
39 DCHECK(!ipc_data.offset()); | 61 DCHECK(!ipc_data.offset()); |
40 AppendData(ipc_data.bytes(), | 62 AppendData(ipc_data.bytes(), |
41 base::checked_cast<size_t, uint64_t>(length)); | 63 base::checked_cast<size_t, uint64_t>(length)); |
42 break; | 64 break; |
43 case DataElement::TYPE_FILE: | 65 case DataElement::TYPE_FILE: |
44 AppendFile(ipc_data.path(), ipc_data.offset(), length, | 66 AppendFile(ipc_data.path(), ipc_data.offset(), length, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
76 items_.push_back(new BlobDataItem(std::move(element))); | 98 items_.push_back(new BlobDataItem(std::move(element))); |
77 return items_.size() - 1; | 99 return items_.size() - 1; |
78 } | 100 } |
79 | 101 |
80 bool BlobDataBuilder::PopulateFutureData(size_t index, | 102 bool BlobDataBuilder::PopulateFutureData(size_t index, |
81 const char* data, | 103 const char* data, |
82 size_t offset, | 104 size_t offset, |
83 size_t length) { | 105 size_t length) { |
84 DCHECK_LT(index, items_.size()); | 106 DCHECK_LT(index, items_.size()); |
85 DCHECK(data); | 107 DCHECK(data); |
86 DataElement* element = items_.at(index)->data_element_ptr(); | 108 DataElement* element = items_[index]->data_element_ptr(); |
87 | 109 |
88 // We lazily allocate our data buffer by waiting until the first | 110 // We lazily allocate our data buffer by waiting until the first |
89 // PopulateFutureData call. | 111 // PopulateFutureData call. |
90 // Why? The reason we have the AppendFutureData method is to create our Blob | 112 // Why? The reason we have the AppendFutureData method is to create our Blob |
91 // record when the Renderer tells us about the blob without actually | 113 // record when the Renderer tells us about the blob without actually |
92 // allocating the memory yet, as we might not have the quota yet. So we don't | 114 // allocating the memory yet, as we might not have the quota yet. So we don't |
93 // want to allocate the memory until we're actually receiving the data (which | 115 // want to allocate the memory until we're actually receiving the data (which |
94 // the browser process only does when it has quota). | 116 // the browser process only does when it has quota). |
95 if (element->type() == DataElement::TYPE_BYTES_DESCRIPTION) { | 117 if (element->type() == DataElement::TYPE_BYTES_DESCRIPTION) { |
96 element->SetToAllocatedBytes(element->length()); | 118 element->SetToAllocatedBytes(element->length()); |
97 // The type of the element is now TYPE_BYTES. | 119 // The type of the element is now TYPE_BYTES. |
98 } | 120 } |
99 if (element->type() != DataElement::TYPE_BYTES) { | 121 if (element->type() != DataElement::TYPE_BYTES) { |
100 DVLOG(1) << "Invalid item type."; | 122 DVLOG(1) << "Invalid item type."; |
101 return false; | 123 return false; |
102 } | 124 } |
103 base::CheckedNumeric<size_t> checked_end = offset; | 125 base::CheckedNumeric<size_t> checked_end = offset; |
104 checked_end += length; | 126 checked_end += length; |
105 if (!checked_end.IsValid() || checked_end.ValueOrDie() > element->length()) { | 127 if (!checked_end.IsValid() || checked_end.ValueOrDie() > element->length()) { |
106 DVLOG(1) << "Invalid offset or length."; | 128 DVLOG(1) << "Invalid offset or length."; |
107 return false; | 129 return false; |
108 } | 130 } |
109 std::memcpy(element->mutable_bytes() + offset, data, length); | 131 std::memcpy(element->mutable_bytes() + offset, data, length); |
110 return true; | 132 return true; |
111 } | 133 } |
112 | 134 |
113 size_t BlobDataBuilder::AppendFutureFile(uint64_t offset, uint64_t length) { | 135 size_t BlobDataBuilder::AppendFutureFile(uint64_t offset, |
136 uint64_t length, | |
137 uint64_t file_id) { | |
114 CHECK_NE(length, 0ull); | 138 CHECK_NE(length, 0ull); |
115 std::unique_ptr<DataElement> element(new DataElement()); | 139 std::unique_ptr<DataElement> element(new DataElement()); |
116 element->SetToFilePathRange(base::FilePath::FromUTF8Unsafe(std::string( | 140 element->SetToFilePathRange(GetFutureFileItemPath(file_id), offset, length, |
117 kAppendFutureFileTemporaryFileName)), | 141 base::Time()); |
118 offset, length, base::Time()); | |
119 items_.push_back(new BlobDataItem(std::move(element))); | 142 items_.push_back(new BlobDataItem(std::move(element))); |
120 return items_.size() - 1; | 143 return items_.size() - 1; |
121 } | 144 } |
122 | 145 |
123 bool BlobDataBuilder::PopulateFutureFile( | 146 bool BlobDataBuilder::PopulateFutureFile( |
124 size_t index, | 147 size_t index, |
125 const scoped_refptr<ShareableFileReference>& file_reference, | 148 const scoped_refptr<ShareableFileReference>& file_reference, |
126 const base::Time& expected_modification_time) { | 149 const base::Time& expected_modification_time) { |
127 DCHECK_LT(index, items_.size()); | 150 DCHECK_LT(index, items_.size()); |
128 DataElement* old_element = items_.at(index)->data_element_ptr(); | 151 DataElement* element = items_[index]->data_element_ptr(); |
129 | 152 |
130 if (old_element->type() != DataElement::TYPE_FILE) { | 153 if (element->type() != DataElement::TYPE_FILE) { |
131 DVLOG(1) << "Invalid item type."; | 154 DVLOG(1) << "Invalid item type."; |
132 return false; | 155 return false; |
133 } else if (old_element->path().AsUTF8Unsafe() != | 156 } else if (!IsFutureFileItem(*element)) { |
134 std::string(kAppendFutureFileTemporaryFileName)) { | |
135 DVLOG(1) << "Item not created by AppendFutureFile"; | 157 DVLOG(1) << "Item not created by AppendFutureFile"; |
136 return false; | 158 return false; |
137 } | 159 } |
138 uint64_t length = old_element->length(); | 160 uint64_t length = element->length(); |
139 uint64_t offset = old_element->offset(); | 161 uint64_t offset = element->offset(); |
140 std::unique_ptr<DataElement> element(new DataElement()); | 162 items_[index]->data_handle_ = std::move(file_reference); |
141 element->SetToFilePathRange(file_reference->path(), offset, length, | 163 element->SetToFilePathRange(file_reference->path(), offset, length, |
142 expected_modification_time); | 164 expected_modification_time); |
143 items_[index] = new BlobDataItem(std::move(element), file_reference); | |
144 return true; | 165 return true; |
145 } | 166 } |
146 | 167 |
147 void BlobDataBuilder::AppendFile(const base::FilePath& file_path, | 168 void BlobDataBuilder::AppendFile(const base::FilePath& file_path, |
148 uint64_t offset, | 169 uint64_t offset, |
149 uint64_t length, | 170 uint64_t length, |
150 const base::Time& expected_modification_time) { | 171 const base::Time& expected_modification_time) { |
151 std::unique_ptr<DataElement> element(new DataElement()); | 172 std::unique_ptr<DataElement> element(new DataElement()); |
152 element->SetToFilePathRange(file_path, offset, length, | 173 element->SetToFilePathRange(file_path, offset, length, |
153 expected_modification_time); | 174 expected_modification_time); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
220 << ", content_type: " << x.content_type_ | 241 << ", content_type: " << x.content_type_ |
221 << ", content_disposition: " << x.content_disposition_ << ", items: ["; | 242 << ", content_disposition: " << x.content_disposition_ << ", items: ["; |
222 for (const auto& item : x.items_) { | 243 for (const auto& item : x.items_) { |
223 PrintTo(*item, os); | 244 PrintTo(*item, os); |
224 *os << ", "; | 245 *os << ", "; |
225 } | 246 } |
226 *os << "]}"; | 247 *os << "]}"; |
227 } | 248 } |
228 | 249 |
229 } // namespace storage | 250 } // namespace storage |
OLD | NEW |