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

Side by Side Diff: content/child/webblobregistry_impl.cc

Issue 1183713003: Blob Consolidation & Registry Hookup (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: file path fix Created 5 years, 5 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
« no previous file with comments | « content/child/webblobregistry_impl.h ('k') | content/content_child.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "content/child/webblobregistry_impl.h" 5 #include "content/child/webblobregistry_impl.h"
6 6
7 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "base/guid.h" 8 #include "base/guid.h"
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/shared_memory.h" 11 #include "base/memory/shared_memory.h"
11 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
12 #include "base/trace_event/trace_event.h" 13 #include "base/trace_event/trace_event.h"
13 #include "content/child/child_thread_impl.h" 14 #include "content/child/child_thread_impl.h"
14 #include "content/child/thread_safe_sender.h" 15 #include "content/child/thread_safe_sender.h"
15 #include "content/common/fileapi/webblob_messages.h" 16 #include "content/common/fileapi/webblob_messages.h"
16 #include "third_party/WebKit/public/platform/WebBlobData.h" 17 #include "third_party/WebKit/public/platform/WebBlobData.h"
17 #include "third_party/WebKit/public/platform/WebString.h" 18 #include "third_party/WebKit/public/platform/WebString.h"
18 #include "third_party/WebKit/public/platform/WebThreadSafeData.h" 19 #include "third_party/WebKit/public/platform/WebThreadSafeData.h"
19 #include "third_party/WebKit/public/platform/WebURL.h" 20 #include "third_party/WebKit/public/platform/WebURL.h"
20 21
21 using blink::WebBlobData; 22 using blink::WebBlobData;
22 using blink::WebString; 23 using blink::WebString;
23 using blink::WebThreadSafeData; 24 using blink::WebThreadSafeData;
24 using blink::WebURL; 25 using blink::WebURL;
26 using blink::WebBlobRegistry;
27 using storage::DataElement;
25 28
26 namespace content { 29 namespace content {
27 30
28 namespace { 31 namespace {
29 32
30 const size_t kLargeThresholdBytes = 250 * 1024; 33 const size_t kLargeThresholdBytes = 250 * 1024;
31 const size_t kMaxSharedMemoryBytes = 10 * 1024 * 1024; 34 const size_t kMaxSharedMemoryBytes = 10 * 1024 * 1024;
32 35
33 } // namespace 36 } // namespace
34 37
35 WebBlobRegistryImpl::WebBlobRegistryImpl(ThreadSafeSender* sender) 38 WebBlobRegistryImpl::WebBlobRegistryImpl(ThreadSafeSender* sender)
36 : sender_(sender) { 39 : sender_(sender) {
37 // Record a dummy trace event on startup so the 'Storage' category shows up 40 // Record a dummy trace event on startup so the 'Storage' category shows up
38 // in the chrome://tracing viewer. 41 // in the chrome://tracing viewer.
39 TRACE_EVENT0("Blob", "Init"); 42 TRACE_EVENT0("Blob", "Init");
40 } 43 }
41 44
42 WebBlobRegistryImpl::~WebBlobRegistryImpl() { 45 WebBlobRegistryImpl::~WebBlobRegistryImpl() {
43 } 46 }
44 47
45 void WebBlobRegistryImpl::registerBlobData( 48 blink::WebBlobRegistry::Builder* WebBlobRegistryImpl::createBuilder(
46 const blink::WebString& uuid, const blink::WebBlobData& data) { 49 const blink::WebString& uuid,
50 const blink::WebString& contentType) {
51 return new BuilderImpl(uuid, contentType, sender_.get());
52 }
53
54 void WebBlobRegistryImpl::registerBlobData(const blink::WebString& uuid,
55 const blink::WebBlobData& data) {
47 TRACE_EVENT0("Blob", "Registry::RegisterBlob"); 56 TRACE_EVENT0("Blob", "Registry::RegisterBlob");
48 const std::string uuid_str(uuid.utf8()); 57 scoped_ptr<Builder> builder(createBuilder(uuid, data.contentType()));
49 58
50 storage::DataElement data_buffer; 59 // This is temporary until we move to createBuilder() as our blob creation
51 data_buffer.SetToEmptyBytes(); 60 // method.
52
53 sender_->Send(new BlobHostMsg_StartBuilding(uuid_str));
54 size_t i = 0; 61 size_t i = 0;
55 WebBlobData::Item data_item; 62 WebBlobData::Item data_item;
56 while (data.itemAt(i++, data_item)) { 63 while (data.itemAt(i++, data_item)) {
57 // NOTE: data_item.length == -1 when we want to use the whole file. This
58 // only happens when we are creating a file object in Blink, and the file
59 // object is the only item in the 'blob'. If we use that file blob to
60 // create another blob, it is sent here as a 'file' item and not a blob,
61 // and the correct size is populated.
62 // static_cast<uint64>(-1) == kuint64max, which is what DataElement uses
63 // to specificy "use the whole file".
64 if (data_item.length == 0) { 64 if (data_item.length == 0) {
65 continue; 65 continue;
66 } 66 }
67 if (data_item.type != WebBlobData::Item::TypeData &&
68 data_buffer.length() != 0) {
69 FlushBlobItemBuffer(uuid_str, &data_buffer);
70 }
71 storage::DataElement item;
72 switch (data_item.type) { 67 switch (data_item.type) {
73 case WebBlobData::Item::TypeData: { 68 case WebBlobData::Item::TypeData: {
74 // WebBlobData does not allow partial data items. 69 // WebBlobData does not allow partial data items.
75 DCHECK(!data_item.offset && data_item.length == -1); 70 DCHECK(!data_item.offset && data_item.length == -1);
76 if (data_item.data.size() == 0) { 71 builder->appendData(data_item.data);
77 continue;
78 }
79 BufferBlobData(uuid_str, data_item.data, &data_buffer);
80 break; 72 break;
81 } 73 }
82 case WebBlobData::Item::TypeFile: 74 case WebBlobData::Item::TypeFile:
83 item.SetToFilePathRange( 75 builder->appendFile(data_item.filePath,
84 base::FilePath::FromUTF16Unsafe(data_item.filePath), 76 static_cast<uint64_t>(data_item.offset),
85 static_cast<uint64>(data_item.offset), 77 static_cast<uint64_t>(data_item.length),
86 static_cast<uint64>(data_item.length), 78 data_item.expectedModificationTime);
87 base::Time::FromDoubleT(data_item.expectedModificationTime));
88 sender_->Send(new BlobHostMsg_AppendBlobDataItem(uuid_str, item));
89 break; 79 break;
90 case WebBlobData::Item::TypeBlob: 80 case WebBlobData::Item::TypeBlob:
91 item.SetToBlobRange( 81 builder->appendBlob(data_item.blobUUID, data_item.offset,
92 data_item.blobUUID.utf8(), 82 data_item.length);
93 static_cast<uint64>(data_item.offset),
94 static_cast<uint64>(data_item.length));
95 sender_->Send(
96 new BlobHostMsg_AppendBlobDataItem(uuid_str, item));
97 break; 83 break;
98 case WebBlobData::Item::TypeFileSystemURL: 84 case WebBlobData::Item::TypeFileSystemURL:
99 // We only support filesystem URL as of now. 85 // We only support filesystem URL as of now.
100 DCHECK(GURL(data_item.fileSystemURL).SchemeIsFileSystem()); 86 DCHECK(GURL(data_item.fileSystemURL).SchemeIsFileSystem());
101 item.SetToFileSystemUrlRange( 87 builder->appendFileSystemURL(data_item.fileSystemURL,
102 data_item.fileSystemURL, 88 static_cast<uint64>(data_item.offset),
103 static_cast<uint64>(data_item.offset), 89 static_cast<uint64>(data_item.length),
104 static_cast<uint64>(data_item.length), 90 data_item.expectedModificationTime);
105 base::Time::FromDoubleT(data_item.expectedModificationTime));
106 sender_->Send(
107 new BlobHostMsg_AppendBlobDataItem(uuid_str, item));
108 break; 91 break;
109 default: 92 default:
110 NOTREACHED(); 93 NOTREACHED();
111 } 94 }
112 } 95 }
113 if (data_buffer.length() != 0) { 96 builder->build();
114 FlushBlobItemBuffer(uuid_str, &data_buffer);
115 }
116 sender_->Send(new BlobHostMsg_FinishBuilding(
117 uuid_str, data.contentType().utf8().data()));
118 } 97 }
119 98
120 void WebBlobRegistryImpl::addBlobDataRef(const WebString& uuid) { 99 void WebBlobRegistryImpl::addBlobDataRef(const WebString& uuid) {
121 sender_->Send(new BlobHostMsg_IncrementRefCount(uuid.utf8())); 100 sender_->Send(new BlobHostMsg_IncrementRefCount(uuid.utf8()));
122 } 101 }
123 102
124 void WebBlobRegistryImpl::removeBlobDataRef(const WebString& uuid) { 103 void WebBlobRegistryImpl::removeBlobDataRef(const WebString& uuid) {
125 sender_->Send(new BlobHostMsg_DecrementRefCount(uuid.utf8())); 104 sender_->Send(new BlobHostMsg_DecrementRefCount(uuid.utf8()));
126 } 105 }
127 106
128 void WebBlobRegistryImpl::registerPublicBlobURL( 107 void WebBlobRegistryImpl::registerPublicBlobURL(const WebURL& url,
129 const WebURL& url, const WebString& uuid) { 108 const WebString& uuid) {
130 sender_->Send(new BlobHostMsg_RegisterPublicURL(url, uuid.utf8())); 109 sender_->Send(new BlobHostMsg_RegisterPublicURL(url, uuid.utf8()));
131 } 110 }
132 111
133 void WebBlobRegistryImpl::revokePublicBlobURL(const WebURL& url) { 112 void WebBlobRegistryImpl::revokePublicBlobURL(const WebURL& url) {
134 sender_->Send(new BlobHostMsg_RevokePublicURL(url)); 113 sender_->Send(new BlobHostMsg_RevokePublicURL(url));
135 } 114 }
136 115
137 void WebBlobRegistryImpl::FlushBlobItemBuffer(
138 const std::string& uuid_str,
139 storage::DataElement* data_buffer) const {
140 DCHECK_NE(data_buffer->length(), 0ul);
141 DCHECK_LT(data_buffer->length(), kLargeThresholdBytes);
142 sender_->Send(new BlobHostMsg_AppendBlobDataItem(uuid_str, *data_buffer));
143 data_buffer->SetToEmptyBytes();
144 }
145
146 void WebBlobRegistryImpl::BufferBlobData(const std::string& uuid_str,
147 const blink::WebThreadSafeData& data,
148 storage::DataElement* data_buffer) {
149 size_t buffer_size = data_buffer->length();
150 size_t data_size = data.size();
151 DCHECK_NE(data_size, 0ul);
152 if (buffer_size != 0 && buffer_size + data_size >= kLargeThresholdBytes) {
153 FlushBlobItemBuffer(uuid_str, data_buffer);
154 buffer_size = 0;
155 }
156 if (data_size >= kLargeThresholdBytes) {
157 TRACE_EVENT0("Blob", "Registry::SendOversizedBlobData");
158 SendOversizedDataForBlob(uuid_str, data);
159 } else {
160 DCHECK_LT(buffer_size + data_size, kLargeThresholdBytes);
161 data_buffer->AppendBytes(data.data(), data_size);
162 }
163 }
164
165 void WebBlobRegistryImpl::SendOversizedDataForBlob(
166 const std::string& uuid_str,
167 const blink::WebThreadSafeData& data) {
168 DCHECK_GE(data.size(), kLargeThresholdBytes);
169 // We handle larger amounts of data via SharedMemory instead of
170 // writing it directly to the IPC channel.
171 size_t shared_memory_size = std::min(data.size(), kMaxSharedMemoryBytes);
172 scoped_ptr<base::SharedMemory> shared_memory(
173 ChildThreadImpl::AllocateSharedMemory(shared_memory_size, sender_.get()));
174 CHECK(shared_memory.get());
175 if (!shared_memory->Map(shared_memory_size))
176 CHECK(false);
177
178 size_t data_size = data.size();
179 const char* data_ptr = data.data();
180 while (data_size) {
181 TRACE_EVENT0("Blob", "Registry::SendOversizedBlobItem");
182 size_t chunk_size = std::min(data_size, shared_memory_size);
183 memcpy(shared_memory->memory(), data_ptr, chunk_size);
184 sender_->Send(new BlobHostMsg_SyncAppendSharedMemory(
185 uuid_str, shared_memory->handle(), chunk_size));
186 data_size -= chunk_size;
187 data_ptr += chunk_size;
188 }
189 }
190
191 // ------ streams stuff ----- 116 // ------ streams stuff -----
192 117
193 void WebBlobRegistryImpl::registerStreamURL( 118 void WebBlobRegistryImpl::registerStreamURL(const WebURL& url,
194 const WebURL& url, const WebString& content_type) { 119 const WebString& content_type) {
195 DCHECK(ChildThreadImpl::current()); 120 DCHECK(ChildThreadImpl::current());
196 sender_->Send(new StreamHostMsg_StartBuilding(url, content_type.utf8())); 121 sender_->Send(new StreamHostMsg_StartBuilding(url, content_type.utf8()));
197 } 122 }
198 123
199 void WebBlobRegistryImpl::registerStreamURL( 124 void WebBlobRegistryImpl::registerStreamURL(const WebURL& url,
200 const WebURL& url, const WebURL& src_url) { 125 const WebURL& src_url) {
201 DCHECK(ChildThreadImpl::current()); 126 DCHECK(ChildThreadImpl::current());
202 sender_->Send(new StreamHostMsg_Clone(url, src_url)); 127 sender_->Send(new StreamHostMsg_Clone(url, src_url));
203 } 128 }
204 129
205 void WebBlobRegistryImpl::addDataToStream(const WebURL& url, 130 void WebBlobRegistryImpl::addDataToStream(const WebURL& url,
206 const char* data, size_t length) { 131 const char* data,
132 size_t length) {
207 DCHECK(ChildThreadImpl::current()); 133 DCHECK(ChildThreadImpl::current());
208 if (length == 0) 134 if (length == 0)
209 return; 135 return;
210 if (length < kLargeThresholdBytes) { 136 if (length < kLargeThresholdBytes) {
211 storage::DataElement item; 137 DataElement item;
212 item.SetToBytes(data, length); 138 item.SetToBytes(data, length);
213 sender_->Send(new StreamHostMsg_AppendBlobDataItem(url, item)); 139 sender_->Send(new StreamHostMsg_AppendBlobDataItem(url, item));
214 } else { 140 } else {
215 // We handle larger amounts of data via SharedMemory instead of 141 // We handle larger amounts of data via SharedMemory instead of
216 // writing it directly to the IPC channel. 142 // writing it directly to the IPC channel.
217 size_t shared_memory_size = std::min( 143 size_t shared_memory_size = std::min(length, kMaxSharedMemoryBytes);
218 length, kMaxSharedMemoryBytes);
219 scoped_ptr<base::SharedMemory> shared_memory( 144 scoped_ptr<base::SharedMemory> shared_memory(
220 ChildThreadImpl::AllocateSharedMemory(shared_memory_size, 145 ChildThreadImpl::AllocateSharedMemory(shared_memory_size,
221 sender_.get())); 146 sender_.get()));
222 CHECK(shared_memory.get()); 147 CHECK(shared_memory.get());
223 if (!shared_memory->Map(shared_memory_size)) 148 if (!shared_memory->Map(shared_memory_size))
224 CHECK(false); 149 CHECK(false);
225 150
226 size_t remaining_bytes = length; 151 size_t remaining_bytes = length;
227 const char* current_ptr = data; 152 const char* current_ptr = data;
228 while (remaining_bytes) { 153 while (remaining_bytes) {
(...skipping 20 matching lines...) Expand all
249 void WebBlobRegistryImpl::abortStream(const WebURL& url) { 174 void WebBlobRegistryImpl::abortStream(const WebURL& url) {
250 DCHECK(ChildThreadImpl::current()); 175 DCHECK(ChildThreadImpl::current());
251 sender_->Send(new StreamHostMsg_AbortBuilding(url)); 176 sender_->Send(new StreamHostMsg_AbortBuilding(url));
252 } 177 }
253 178
254 void WebBlobRegistryImpl::unregisterStreamURL(const WebURL& url) { 179 void WebBlobRegistryImpl::unregisterStreamURL(const WebURL& url) {
255 DCHECK(ChildThreadImpl::current()); 180 DCHECK(ChildThreadImpl::current());
256 sender_->Send(new StreamHostMsg_Remove(url)); 181 sender_->Send(new StreamHostMsg_Remove(url));
257 } 182 }
258 183
184 WebBlobRegistryImpl::BuilderImpl::BuilderImpl(
185 const blink::WebString& uuid,
186 const blink::WebString& content_type,
187 ThreadSafeSender* sender)
188 : uuid_(uuid.utf8()), content_type_(content_type.utf8()), sender_(sender) {
189 }
190
191 WebBlobRegistryImpl::BuilderImpl::~BuilderImpl() {
192 }
193
194 void WebBlobRegistryImpl::BuilderImpl::appendData(
195 const WebThreadSafeData& data) {
196 consolidation_.AddDataItem(data);
197 }
198
199 void WebBlobRegistryImpl::BuilderImpl::appendBlob(const WebString& uuid,
200 uint64_t offset,
201 uint64_t length) {
202 consolidation_.AddBlobItem(uuid.utf8(), offset, length);
203 }
204
205 void WebBlobRegistryImpl::BuilderImpl::appendFile(
206 const WebString& path,
207 uint64_t offset,
208 uint64_t length,
209 double expected_modification_time) {
210 consolidation_.AddFileItem(
211 base::FilePath::FromUTF16Unsafe(base::string16(path)), offset, length,
212 expected_modification_time);
213 }
214
215 void WebBlobRegistryImpl::BuilderImpl::appendFileSystemURL(
216 const WebURL& fileSystemURL,
217 uint64_t offset,
218 uint64_t length,
219 double expected_modification_time) {
220 DCHECK(GURL(fileSystemURL).SchemeIsFileSystem());
221 consolidation_.AddFileSystemItem(GURL(fileSystemURL), offset, length,
222 expected_modification_time);
223 }
224
225 void WebBlobRegistryImpl::BuilderImpl::build() {
226 sender_->Send(new BlobHostMsg_StartBuilding(uuid_));
227 const auto& items = consolidation_.consolidated_items();
228
229 // We still need a buffer to hold the continuous block of data so the
230 // DataElement can hold it.
231 size_t buffer_size = 0;
232 scoped_ptr<char[]> buffer;
233 for (size_t i = 0; i < items.size(); i++) {
234 const BlobConsolidation::ConsolidatedItem& item = items[i];
235 DataElement element;
236 // NOTE: length == -1 when we want to use the whole file. This
237 // only happens when we are creating a file object in Blink, and the file
238 // object is the only item in the 'blob'. If we use that file blob to
239 // create another blob, it is sent here as a 'file' item and not a blob,
240 // and the correct size is populated.
241 // static_cast<uint64>(-1) == kuint64max, which is what DataElement uses
242 // to specificy "use the whole file".
243 switch (item.type) {
244 case DataElement::TYPE_BYTES:
245 if (item.length > kLargeThresholdBytes) {
246 SendOversizedDataForBlob(i);
247 break;
248 }
249 if (buffer_size < item.length) {
250 buffer.reset(new char[item.length]);
251 buffer_size = item.length;
252 }
253 consolidation_.ReadMemory(i, 0, item.length, buffer.get());
254 element.SetToSharedBytes(buffer.get(), item.length);
255 sender_->Send(new BlobHostMsg_AppendBlobDataItem(uuid_, element));
256 break;
257 case DataElement::TYPE_FILE:
258 element.SetToFilePathRange(
259 item.path, item.offset, item.length,
260 base::Time::FromDoubleT(item.expected_modification_time));
261 sender_->Send(new BlobHostMsg_AppendBlobDataItem(uuid_, element));
262 break;
263 case DataElement::TYPE_BLOB:
264 element.SetToBlobRange(item.blob_uuid, item.offset, item.length);
265 sender_->Send(new BlobHostMsg_AppendBlobDataItem(uuid_, element));
266 break;
267 case DataElement::TYPE_FILE_FILESYSTEM:
268 element.SetToFileSystemUrlRange(
269 item.filesystem_url, item.offset, item.length,
270 base::Time::FromDoubleT(item.expected_modification_time));
271 sender_->Send(new BlobHostMsg_AppendBlobDataItem(uuid_, element));
272 break;
273 default:
274 NOTREACHED();
275 }
276 }
277 sender_->Send(new BlobHostMsg_FinishBuilding(uuid_, content_type_));
278 }
279
280 void WebBlobRegistryImpl::BuilderImpl::SendOversizedDataForBlob(
281 size_t consolidated_item_index) {
282 TRACE_EVENT0("Blob", "Registry::SendOversizedBlobData");
283 const BlobConsolidation::ConsolidatedItem& item =
284 consolidation_.consolidated_items()[consolidated_item_index];
285 // We handle larger amounts of data via SharedMemory instead of
286 // writing it directly to the IPC channel.
287
288 size_t data_size = item.length;
289 size_t shared_memory_size = std::min(data_size, kMaxSharedMemoryBytes);
290 scoped_ptr<base::SharedMemory> shared_memory(
291 ChildThreadImpl::AllocateSharedMemory(shared_memory_size, sender_.get()));
292 CHECK(shared_memory.get());
293 const bool mapped = shared_memory->Map(shared_memory_size);
294 CHECK(mapped) << "Unable to map shared memory.";
295
296 size_t offset = 0;
297 while (data_size) {
298 TRACE_EVENT0("Blob", "Registry::SendOversizedBlobItem");
299 size_t chunk_size = std::min(data_size, shared_memory_size);
300 consolidation_.ReadMemory(consolidated_item_index, offset, chunk_size,
301 shared_memory->memory());
302 sender_->Send(new BlobHostMsg_SyncAppendSharedMemory(
303 uuid_, shared_memory->handle(), chunk_size));
304 data_size -= chunk_size;
305 offset += chunk_size;
306 }
307 }
308
259 } // namespace content 309 } // namespace content
OLDNEW
« no previous file with comments | « content/child/webblobregistry_impl.h ('k') | content/content_child.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698