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

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: Separated out RecordMemoryRead 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 ConsolidatedItem* item;
41 if (consolidated_items_.empty() ||
42 consolidated_items_.back().type != DataElement::TYPE_BYTES) {
43 consolidated_items_.push_back(
44 ConsolidatedItem(DataElement::TYPE_BYTES, 0, 0));
45 }
46 item = &consolidated_items_.back();
47 if (!item->data.empty()) {
48 item->offsets.push_back(static_cast<size_t>(item->length));
49 }
50 item->length += data.size();
51 total_memory_ += data.size();
52 item->data.push_back(WebThreadSafeData());
53 item->data.back().assign(data);
54 }
55
56 void BlobConsolidation::AddFileItem(const WebString& path, uint64_t offset,
57 uint64_t length,
58 double expected_modification_time) {
59 consolidated_items_.push_back(
60 ConsolidatedItem(DataElement::TYPE_FILE, offset, length));
61 ConsolidatedItem& item = consolidated_items_.back();
62 item.path.assign(path);
63 item.expected_modification_time = expected_modification_time;
64 }
65
66 void BlobConsolidation::AddBlobItem(const WebString& uuid, uint64_t offset,
67 uint64_t length) {
68 consolidated_items_.push_back(
69 ConsolidatedItem(DataElement::TYPE_BLOB, offset, length));
70 ConsolidatedItem& item = consolidated_items_.back();
71 item.blob_uuid.assign(uuid);
72 }
73
74 void BlobConsolidation::AddFileSystemItem(const WebURL& url, uint64_t offset,
75 uint64_t length,
76 double expected_modification_time) {
77 consolidated_items_.push_back(
78 ConsolidatedItem(DataElement::TYPE_FILE_FILESYSTEM, offset, length));
79 ConsolidatedItem& item = consolidated_items_.back();
80 item.filesystem_url = url;
81 item.expected_modification_time = expected_modification_time;
82 }
83
84 ReadStatus BlobConsolidation::RecordMemoryRead(size_t consolidated_item_index,
michaeln 2015/06/17 03:17:55 please put the method bodies in the .cc in the sam
dmurph 2015/06/17 18:29:50 Done.
85 size_t memory_size) {
86 if (consolidated_item_index >= consolidated_items_.size()) {
87 return ReadStatus::ERROR_OUT_OF_BOUNDS;
88 }
89 if (total_memory_read_ + memory_size > total_memory_) {
90 LOG(ERROR) << "Memory for blob marked as read more than once.";
91 return ReadStatus::ERROR;
92 }
93 const ConsolidatedItem& item = consolidated_items_[consolidated_item_index];
94 if (item.type != DataElement::TYPE_BYTES) {
95 LOG(ERROR) << "Cannot read bytes from item with type " << item.type;
96 return ReadStatus::ERROR_WRONG_TYPE;
97 }
98
99 // Keep track of memory read.
100 total_memory_read_ += memory_size;
101 return total_memory_read_ < total_memory_ ? ReadStatus::BLOB_BYTES_PENDING
102 : ReadStatus::DONE;
103 }
104
105 ReadStatus BlobConsolidation::ReadMemory(size_t consolidated_item_index,
106 size_t consolidated_offset,
107 size_t consolidated_size,
108 char* memory_out) {
109 if (consolidated_item_index >= consolidated_items_.size()) {
110 return ReadStatus::ERROR_OUT_OF_BOUNDS;
111 }
112 const ConsolidatedItem& item = consolidated_items_[consolidated_item_index];
113 if (item.type != DataElement::TYPE_BYTES) {
114 return ReadStatus::ERROR_WRONG_TYPE;
115 }
116 if (consolidated_size + consolidated_offset > item.length) {
117 LOG(ERROR) << "Invalid consolidated size " << consolidated_size
118 << " and offset " << consolidated_offset << " vs item length of "
119 << item.length;
120 return ReadStatus::ERROR_OUT_OF_BOUNDS;
121 }
122
123 // We do a binary search to find the correct data to start with in the data
michaeln 2015/06/17 03:17:55 fun ;)
124 // elements. This is slightly customized due to our unique storage and
125 // constraints
126 size_t mid = 0;
127 size_t offset_from_mid = consolidated_offset;
128 size_t num_items = item.data.size();
129 if (!item.offsets.empty()) {
130 size_t low = 0;
131 size_t high = num_items - 1;
132 while (true) {
133 mid = (high + low) / 2;
134 // Note: we don't include the implicit '0' for the first item in offsets,
135 // so we do (index - 1).
michaeln 2015/06/17 03:17:55 i'd say you don't need what's after the , in the c
dmurph 2015/06/17 18:29:50 Done.
136 size_t item_offset = (mid == 0 ? 0 : item.offsets[mid - 1]);
137 offset_from_mid = consolidated_offset - item_offset;
138 size_t next_item_offset = (mid + 1 == num_items ? 0 : item.offsets[mid]);
139 if (item_offset == consolidated_offset) {
140 // found exact match.
141 break;
142 } else if (item_offset > consolidated_offset) {
143 high = mid - 1;
144 } else if (mid + 1 == num_items ||
145 next_item_offset > consolidated_offset) {
146 // We are at the last item or the next offset it greater than the one
147 // we want, so the current item wins.
148 break;
149 } else {
150 low = mid + 1;
151 }
152 }
153 }
154
155 DCHECK_LT(offset_from_mid, item.data[mid].size());
156 // read starting from 'mid' and 'offset_from_mid'
michaeln 2015/06/17 03:17:55 nit: cap "R" and a period at the end
dmurph 2015/06/17 18:29:50 Done.
157 for (size_t memory_read = 0;
158 mid < num_items && memory_read < consolidated_size; mid++) {
159 size_t read_size = std::min(item.data[mid].size() - offset_from_mid,
160 consolidated_size - memory_read);
161 memmove(memory_out + memory_read, item.data[mid].data() + offset_from_mid,
162 read_size);
163 offset_from_mid = 0;
164 memory_read += read_size;
165 }
166 return ReadStatus::DONE;
167 }
168
169 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698