OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <string> | |
6 | |
7 #include "content/child/blob_storage/blob_consolidation.h" | |
8 | |
9 using storage::DataElement; | |
10 using blink::WebBlobData; | |
11 using blink::WebString; | |
12 using blink::WebThreadSafeData; | |
13 using blink::WebURL; | |
michaeln
2015/06/22 23:20:21
i think only WebThreadSafeData is used now
dmurph
2015/06/24 00:36:16
Done.
| |
14 | |
15 namespace content { | |
16 | |
17 using ReadStatus = BlobConsolidation::ReadStatus; | |
18 | |
19 BlobConsolidation::ConsolidatedItem::ConsolidatedItem() | |
20 : type(DataElement::TYPE_UNKNOWN), | |
21 offset(0), | |
22 length(kuint64max), | |
23 expected_modification_time(0) {} | |
kinuko
2015/06/23 14:11:30
nit: please put empty line between function defini
dmurph
2015/06/24 00:36:16
Done.
| |
24 BlobConsolidation::ConsolidatedItem::~ConsolidatedItem() {} | |
25 | |
26 BlobConsolidation::ConsolidatedItem::ConsolidatedItem(DataElement::Type type, | |
27 uint64_t offset, | |
28 uint64_t length) | |
29 : type(type), | |
30 offset(offset), | |
31 length(length), | |
32 expected_modification_time(0) {} | |
33 | |
34 BlobConsolidation::BlobConsolidation() | |
35 : total_memory_(0) {} | |
36 | |
37 BlobConsolidation::~BlobConsolidation() {} | |
38 | |
39 void BlobConsolidation::AddDataItem(const WebThreadSafeData& data) { | |
40 if (data.size() == 0) | |
41 return; | |
42 ConsolidatedItem* item; | |
kinuko
2015/06/23 14:11:30
move this right before it starts to be used (i.e.
dmurph
2015/06/24 00:36:16
Done.
| |
43 if (consolidated_items_.empty() || | |
44 consolidated_items_.back().type != DataElement::TYPE_BYTES) { | |
45 consolidated_items_.push_back( | |
46 ConsolidatedItem(DataElement::TYPE_BYTES, 0, 0)); | |
47 } | |
48 item = &consolidated_items_.back(); | |
49 if (!item->data.empty()) { | |
50 item->offsets.push_back(static_cast<size_t>(item->length)); | |
51 } | |
52 item->length += data.size(); | |
53 total_memory_ += data.size(); | |
54 item->data.push_back(WebThreadSafeData()); | |
kinuko
2015/06/23 14:11:30
nit: I think just item->data.push_back(data) works
dmurph
2015/06/24 00:36:16
Done.
| |
55 item->data.back().assign(data); | |
56 } | |
57 | |
58 void BlobConsolidation::AddFileItem(const base::FilePath& path, uint64_t offset, | |
59 uint64_t length, | |
60 double expected_modification_time) { | |
61 if (length == 0) | |
62 return; | |
63 consolidated_items_.push_back( | |
64 ConsolidatedItem(DataElement::TYPE_FILE, offset, length)); | |
65 ConsolidatedItem& item = consolidated_items_.back(); | |
66 item.path = path; | |
67 item.expected_modification_time = expected_modification_time; | |
68 } | |
69 | |
70 void BlobConsolidation::AddBlobItem(const std::string& uuid, uint64_t offset, | |
71 uint64_t length) { | |
72 if (length == 0) | |
73 return; | |
74 consolidated_items_.push_back( | |
75 ConsolidatedItem(DataElement::TYPE_BLOB, offset, length)); | |
76 ConsolidatedItem& item = consolidated_items_.back(); | |
77 item.blob_uuid = uuid; | |
78 } | |
79 | |
80 void BlobConsolidation::AddFileSystemItem(const GURL& url, | |
81 uint64_t offset, uint64_t length, | |
82 double expected_modification_time) { | |
83 if (length == 0) | |
84 return; | |
85 consolidated_items_.push_back( | |
86 ConsolidatedItem(DataElement::TYPE_FILE_FILESYSTEM, offset, length)); | |
87 ConsolidatedItem& item = consolidated_items_.back(); | |
88 item.filesystem_url = url; | |
89 item.expected_modification_time = expected_modification_time; | |
90 } | |
91 | |
92 ReadStatus BlobConsolidation::ReadMemory(size_t consolidated_item_index, | |
93 size_t consolidated_offset, | |
94 size_t consolidated_size, | |
95 void* memory_out) { | |
96 if (consolidated_item_index >= consolidated_items_.size()) { | |
97 return ReadStatus::ERROR_OUT_OF_BOUNDS; | |
98 } | |
kinuko
2015/06/23 14:11:30
nit: no need of { } for single-line body in chromi
dmurph
2015/06/24 00:36:17
Done.
| |
99 const ConsolidatedItem& item = consolidated_items_[consolidated_item_index]; | |
100 if (item.type != DataElement::TYPE_BYTES) { | |
101 return ReadStatus::ERROR_WRONG_TYPE; | |
102 } | |
103 if (consolidated_size + consolidated_offset > item.length) { | |
104 LOG(ERROR) << "Invalid consolidated size " << consolidated_size | |
105 << " and offset " << consolidated_offset << " vs item length of " | |
106 << item.length; | |
107 return ReadStatus::ERROR_OUT_OF_BOUNDS; | |
108 } | |
109 | |
110 // We do a binary search to find the correct data to start with in the data | |
111 // elements. This is slightly customized due to our unique storage and | |
112 // constraints | |
kinuko
2015/06/23 14:11:30
nit: end comments with period
dmurph
2015/06/24 00:36:16
Done.
| |
113 size_t mid = 0; | |
114 size_t offset_from_mid = consolidated_offset; | |
115 size_t num_items = item.data.size(); | |
116 if (!item.offsets.empty()) { | |
117 size_t low = 0; | |
118 size_t high = num_items - 1; | |
119 while (true) { | |
120 mid = (high + low) / 2; | |
121 // Note: we don't include the implicit '0' for the first item in offsets. | |
122 size_t item_offset = (mid == 0 ? 0 : item.offsets[mid - 1]); | |
123 offset_from_mid = consolidated_offset - item_offset; | |
124 size_t next_item_offset = (mid + 1 == num_items ? 0 : item.offsets[mid]); | |
125 if (item_offset == consolidated_offset) { | |
126 // found exact match. | |
127 break; | |
128 } else if (item_offset > consolidated_offset) { | |
129 high = mid - 1; | |
130 } else if (mid + 1 == num_items || | |
131 next_item_offset > consolidated_offset) { | |
132 // We are at the last item or the next offset it greater than the one | |
kinuko
2015/06/23 14:11:30
nit: it greater -> is greater ?
dmurph
2015/06/24 00:36:17
Done.
| |
133 // we want, so the current item wins. | |
134 break; | |
135 } else { | |
136 low = mid + 1; | |
137 } | |
138 } | |
139 } | |
140 | |
141 DCHECK_LT(offset_from_mid, item.data[mid].size()); | |
142 // Read starting from 'mid' and 'offset_from_mid'. | |
143 for (size_t memory_read = 0; | |
144 mid < num_items && memory_read < consolidated_size; mid++) { | |
145 size_t read_size = std::min(item.data[mid].size() - offset_from_mid, | |
146 consolidated_size - memory_read); | |
147 memcpy(static_cast<char*>(memory_out) + memory_read, | |
148 item.data[mid].data() + offset_from_mid, read_size); | |
149 offset_from_mid = 0; | |
150 memory_read += read_size; | |
151 } | |
152 return ReadStatus::DONE; | |
153 } | |
154 | |
155 } // namespace content | |
OLD | NEW |