OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/common/webblobregistry_impl.h" | 5 #include "content/common/webblobregistry_impl.h" |
6 | 6 |
7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/shared_memory.h" |
| 9 #include "content/common/child_thread.h" |
8 #include "content/common/webblob_messages.h" | 10 #include "content/common/webblob_messages.h" |
9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBlobData.h" | 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBlobData.h" |
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" | 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" |
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" |
12 #include "webkit/blob/blob_data.h" | 14 #include "webkit/blob/blob_data.h" |
| 15 #include "webkit/glue/webkit_glue.h" |
13 | 16 |
14 using WebKit::WebBlobData; | 17 using WebKit::WebBlobData; |
15 using WebKit::WebString; | 18 using WebKit::WebString; |
16 using WebKit::WebURL; | 19 using WebKit::WebURL; |
17 | 20 |
18 WebBlobRegistryImpl::WebBlobRegistryImpl(IPC::Message::Sender* sender) | 21 WebBlobRegistryImpl::WebBlobRegistryImpl(ChildThread* child_thread) |
19 : sender_(sender) { | 22 : child_thread_(child_thread) { |
20 } | 23 } |
21 | 24 |
22 WebBlobRegistryImpl::~WebBlobRegistryImpl() { | 25 WebBlobRegistryImpl::~WebBlobRegistryImpl() { |
23 } | 26 } |
24 | 27 |
25 void WebBlobRegistryImpl::registerBlobURL( | 28 void WebBlobRegistryImpl::registerBlobURL( |
26 const WebURL& url, WebBlobData& data) { | 29 const WebURL& url, WebBlobData& data) { |
27 scoped_refptr<webkit_blob::BlobData> blob_data( | 30 const size_t kLargeThresholdBytes = 250 * 1024; |
28 new webkit_blob::BlobData(data)); | 31 const size_t kMaxSharedMemoryBytes = 10 * 1024 * 1024; |
29 sender_->Send(new BlobHostMsg_RegisterBlobUrl(url, blob_data)); | 32 |
| 33 child_thread_->Send(new BlobHostMsg_StartBuildingBlob(url)); |
| 34 size_t i = 0; |
| 35 WebBlobData::Item data_item; |
| 36 while (data.itemAt(i++, data_item)) { |
| 37 webkit_blob::BlobData::Item item; |
| 38 switch (data_item.type) { |
| 39 case WebBlobData::Item::TypeData: { |
| 40 // WebBlobData does not allow partial data items. |
| 41 DCHECK(!data_item.offset && data_item.length == -1); |
| 42 if (data_item.data.size() < kLargeThresholdBytes) { |
| 43 item.SetToData(data_item.data.data(), data_item.data.size()); |
| 44 child_thread_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); |
| 45 } else { |
| 46 // We handle larger amounts of data via SharedMemory instead of |
| 47 // writing it directly to the IPC channel. |
| 48 size_t data_size = data_item.data.size(); |
| 49 const char* data_ptr = data_item.data.data(); |
| 50 size_t shared_memory_size = std::min( |
| 51 data_size, kMaxSharedMemoryBytes); |
| 52 scoped_ptr<base::SharedMemory> shared_memory( |
| 53 child_thread_->AllocateSharedMemory(shared_memory_size)); |
| 54 CHECK(shared_memory.get()); |
| 55 while (data_size) { |
| 56 size_t chunk_size = std::min(data_size, shared_memory_size); |
| 57 memcpy(shared_memory->memory(), data_ptr, chunk_size); |
| 58 child_thread_->Send(new BlobHostMsg_SyncAppendSharedMemory( |
| 59 url, shared_memory->handle(), chunk_size)); |
| 60 data_size -= chunk_size; |
| 61 data_ptr += chunk_size; |
| 62 } |
| 63 } |
| 64 break; |
| 65 } |
| 66 case WebBlobData::Item::TypeFile: |
| 67 item.SetToFile( |
| 68 webkit_glue::WebStringToFilePath(data_item.filePath), |
| 69 static_cast<uint64>(data_item.offset), |
| 70 static_cast<uint64>(data_item.length), |
| 71 base::Time::FromDoubleT(data_item.expectedModificationTime)); |
| 72 child_thread_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); |
| 73 break; |
| 74 case WebBlobData::Item::TypeBlob: |
| 75 if (data_item.length) { |
| 76 item.SetToBlob( |
| 77 data_item.blobURL, |
| 78 static_cast<uint64>(data_item.offset), |
| 79 static_cast<uint64>(data_item.length)); |
| 80 } |
| 81 child_thread_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); |
| 82 break; |
| 83 default: |
| 84 NOTREACHED(); |
| 85 } |
| 86 } |
| 87 child_thread_->Send(new BlobHostMsg_FinishBuildingBlob( |
| 88 url, data.contentType().utf8().data())); |
30 } | 89 } |
31 | 90 |
32 void WebBlobRegistryImpl::registerBlobURL( | 91 void WebBlobRegistryImpl::registerBlobURL( |
33 const WebURL& url, const WebURL& src_url) { | 92 const WebURL& url, const WebURL& src_url) { |
34 sender_->Send(new BlobHostMsg_RegisterBlobUrlFrom(url, src_url)); | 93 child_thread_->Send(new BlobHostMsg_CloneBlob(url, src_url)); |
35 } | 94 } |
36 | 95 |
37 void WebBlobRegistryImpl::unregisterBlobURL(const WebURL& url) { | 96 void WebBlobRegistryImpl::unregisterBlobURL(const WebURL& url) { |
38 sender_->Send(new BlobHostMsg_UnregisterBlobUrl(url)); | 97 child_thread_->Send(new BlobHostMsg_RemoveBlob(url)); |
39 } | 98 } |
OLD | NEW |