Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: content/child/blob_storage/blob_consolidation.cc

Issue 1183713003: Blob Consolidation & Registry Hookup (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Android build fix Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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;
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) {}
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), total_memory_read_(0) {}
36
37 BlobConsolidation::~BlobConsolidation() {}
38
39 void BlobConsolidation::AddDataItem(const WebThreadSafeData& data) {
40 if (data.size() == 0) return;
41 ConsolidatedItem* item;
42 if (consolidated_items_.empty() ||
43 consolidated_items_.back().type != DataElement::TYPE_BYTES) {
44 consolidated_items_.push_back(
45 ConsolidatedItem(DataElement::TYPE_BYTES, 0, 0));
46 }
47 item = &consolidated_items_.back();
48 if (!item->data.empty()) {
49 item->offsets.push_back(static_cast<size_t>(item->length));
50 }
51 item->length += data.size();
52 total_memory_ += data.size();
53 item->data.push_back(WebThreadSafeData());
54 item->data.back().assign(data);
55 }
56
57 void BlobConsolidation::AddFileItem(const WebString& path, uint64_t offset,
58 uint64_t length,
59 double expected_modification_time) {
60 if (length == 0) return;
michaeln 2015/06/18 00:10:48 it's very unusual to see this as a one-liner in co
dmurph 2015/06/19 21:42:05 It's valid by the style guide, but I'll add it ;)
61 consolidated_items_.push_back(
62 ConsolidatedItem(DataElement::TYPE_FILE, offset, length));
63 ConsolidatedItem& item = consolidated_items_.back();
64 item.path.assign(path);
65 item.expected_modification_time = expected_modification_time;
66 }
67
68 void BlobConsolidation::AddBlobItem(const WebString& uuid, uint64_t offset,
69 uint64_t length) {
70 if (length == 0) return;
71 consolidated_items_.push_back(
72 ConsolidatedItem(DataElement::TYPE_BLOB, offset, length));
73 ConsolidatedItem& item = consolidated_items_.back();
74 item.blob_uuid.assign(uuid);
75 }
76
77 void BlobConsolidation::AddFileSystemItem(const WebURL& url, uint64_t offset,
78 uint64_t length,
79 double expected_modification_time) {
80 if (length == 0) return;
81 consolidated_items_.push_back(
82 ConsolidatedItem(DataElement::TYPE_FILE_FILESYSTEM, offset, length));
83 ConsolidatedItem& item = consolidated_items_.back();
84 item.filesystem_url = url;
85 item.expected_modification_time = expected_modification_time;
86 }
87
88 ReadStatus BlobConsolidation::ReadMemory(size_t consolidated_item_index,
89 size_t consolidated_offset,
90 size_t consolidated_size,
91 void* memory_out) {
92 if (consolidated_item_index >= consolidated_items_.size()) {
93 return ReadStatus::ERROR_OUT_OF_BOUNDS;
94 }
95 const ConsolidatedItem& item = consolidated_items_[consolidated_item_index];
96 if (item.type != DataElement::TYPE_BYTES) {
97 return ReadStatus::ERROR_WRONG_TYPE;
98 }
99 if (consolidated_size + consolidated_offset > item.length) {
100 LOG(ERROR) << "Invalid consolidated size " << consolidated_size
101 << " and offset " << consolidated_offset << " vs item length of "
102 << item.length;
103 return ReadStatus::ERROR_OUT_OF_BOUNDS;
104 }
105
106 // We do a binary search to find the correct data to start with in the data
107 // elements. This is slightly customized due to our unique storage and
108 // constraints
109 size_t mid = 0;
110 size_t offset_from_mid = consolidated_offset;
111 size_t num_items = item.data.size();
112 if (!item.offsets.empty()) {
113 size_t low = 0;
114 size_t high = num_items - 1;
115 while (true) {
116 mid = (high + low) / 2;
117 // Note: we don't include the implicit '0' for the first item in offsets.
118 size_t item_offset = (mid == 0 ? 0 : item.offsets[mid - 1]);
119 offset_from_mid = consolidated_offset - item_offset;
120 size_t next_item_offset = (mid + 1 == num_items ? 0 : item.offsets[mid]);
121 if (item_offset == consolidated_offset) {
122 // found exact match.
123 break;
124 } else if (item_offset > consolidated_offset) {
125 high = mid - 1;
126 } else if (mid + 1 == num_items ||
127 next_item_offset > consolidated_offset) {
128 // We are at the last item or the next offset it greater than the one
129 // we want, so the current item wins.
130 break;
131 } else {
132 low = mid + 1;
133 }
134 }
135 }
136
137 DCHECK_LT(offset_from_mid, item.data[mid].size());
138 // Read starting from 'mid' and 'offset_from_mid'.
139 for (size_t memory_read = 0;
140 mid < num_items && memory_read < consolidated_size; mid++) {
141 size_t read_size = std::min(item.data[mid].size() - offset_from_mid,
142 consolidated_size - memory_read);
143 memmove(static_cast<char*>(memory_out) + memory_read,
michaeln 2015/06/18 00:10:48 would memcpy() be preferable here?
dmurph 2015/06/19 21:42:05 sure.
144 item.data[mid].data() + offset_from_mid, read_size);
145 offset_from_mid = 0;
146 memory_read += read_size;
147 }
148 return ReadStatus::DONE;
149 }
150
151 ReadStatus BlobConsolidation::RecordMemoryRead(size_t consolidated_item_index,
152 size_t memory_size) {
153 if (consolidated_item_index >= consolidated_items_.size()) {
154 return ReadStatus::ERROR_OUT_OF_BOUNDS;
155 }
156 if (total_memory_read_ + memory_size > total_memory_) {
157 LOG(ERROR) << "Memory for blob marked as read more than once.";
158 return ReadStatus::ERROR;
159 }
160 const ConsolidatedItem& item = consolidated_items_[consolidated_item_index];
161 if (item.type != DataElement::TYPE_BYTES) {
162 LOG(ERROR) << "Cannot read bytes from item with type " << item.type;
163 return ReadStatus::ERROR_WRONG_TYPE;
164 }
165
166 // Keep track of memory read.
167 total_memory_read_ += memory_size;
168 return total_memory_read_ < total_memory_ ? ReadStatus::BLOB_BYTES_PENDING
169 : ReadStatus::DONE;
170 }
171
172 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698