| 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" |
| 8 #include "content/common/webblob_messages.h" | 9 #include "content/common/webblob_messages.h" |
| 9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBlobData.h" | 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBlobData.h" |
| 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" | 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" | 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" |
| 12 #include "webkit/blob/blob_data.h" | 13 #include "webkit/blob/blob_data.h" |
| 14 #include "webkit/glue/webkit_glue.h" |
| 13 | 15 |
| 14 using WebKit::WebBlobData; | 16 using WebKit::WebBlobData; |
| 15 using WebKit::WebString; | 17 using WebKit::WebString; |
| 16 using WebKit::WebURL; | 18 using WebKit::WebURL; |
| 17 | 19 |
| 18 WebBlobRegistryImpl::WebBlobRegistryImpl(IPC::Message::Sender* sender) | 20 WebBlobRegistryImpl::WebBlobRegistryImpl(IPC::Message::Sender* sender) |
| 19 : sender_(sender) { | 21 : sender_(sender) { |
| 20 } | 22 } |
| 21 | 23 |
| 22 WebBlobRegistryImpl::~WebBlobRegistryImpl() { | 24 WebBlobRegistryImpl::~WebBlobRegistryImpl() { |
| 23 } | 25 } |
| 24 | 26 |
| 25 void WebBlobRegistryImpl::registerBlobURL( | 27 void WebBlobRegistryImpl::registerBlobURL( |
| 26 const WebURL& url, WebBlobData& data) { | 28 const WebURL& url, WebBlobData& data) { |
| 27 scoped_refptr<webkit_blob::BlobData> blob_data( | 29 sender_->Send(new BlobHostMsg_StartBuildingBlob(url)); |
| 28 new webkit_blob::BlobData(data)); | 30 size_t i = 0; |
| 29 sender_->Send(new BlobHostMsg_RegisterBlobUrl(url, blob_data)); | 31 WebBlobData::Item data_item; |
| 32 while (data.itemAt(i++, data_item)) { |
| 33 webkit_blob::BlobData::Item item; |
| 34 switch (data_item.type) { |
| 35 case WebBlobData::Item::TypeData: { |
| 36 // WebBlobData does not allow partial data items. |
| 37 DCHECK(!data_item.offset && data_item.length == -1); |
| 38 const size_t kLargeSizeThreshold = 250 * 1024; |
| 39 if (data_item.data.size() < kLargeSizeThreshold) { |
| 40 item.SetToData(data_item.data.data(), data_item.data.size()); |
| 41 sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); |
| 42 } else { |
| 43 // We handle larger amounts of data via SharedMemory instead of |
| 44 // writing it directly to the IPC channel. |
| 45 const size_t kMaxSharedMemorySize = 10 * 1024 * 1024; |
| 46 size_t data_size = data_item.data.size(); |
| 47 const char* data_ptr = data_item.data.data(); |
| 48 size_t shared_memory_size = std::min(data_size, kMaxSharedMemorySize); |
| 49 scoped_ptr<base::SharedMemory> shared_memory( |
| 50 AllocateSharedMemory(shared_memory_size)); |
| 51 CHECK(shared_memory.get()); |
| 52 while (data_size) { |
| 53 size_t chunk_size = std::min(data_size, shared_memory_size); |
| 54 memcpy(shared_memory->memory(), data_ptr, chunk_size); |
| 55 sender_->Send(new BlobHostMsg_SyncAppendSharedMemory( |
| 56 url, shared_memory->handle(), chunk_size)); |
| 57 data_size -= chunk_size; |
| 58 data_ptr += chunk_size; |
| 59 } |
| 60 } |
| 61 break; |
| 62 } |
| 63 case WebBlobData::Item::TypeFile: |
| 64 item.SetToFile( |
| 65 webkit_glue::WebStringToFilePath(data_item.filePath), |
| 66 static_cast<uint64>(data_item.offset), |
| 67 static_cast<uint64>(data_item.length), |
| 68 base::Time::FromDoubleT(data_item.expectedModificationTime)); |
| 69 sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); |
| 70 break; |
| 71 case WebBlobData::Item::TypeBlob: |
| 72 if (data_item.length) { |
| 73 item.SetToBlob( |
| 74 data_item.blobURL, |
| 75 static_cast<uint64>(data_item.offset), |
| 76 static_cast<uint64>(data_item.length)); |
| 77 } |
| 78 sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); |
| 79 break; |
| 80 default: |
| 81 NOTREACHED(); |
| 82 } |
| 83 } |
| 84 sender_->Send(new BlobHostMsg_FinishBuildingBlob( |
| 85 url, data.contentType().utf8().data())); |
| 86 } |
| 87 |
| 88 base::SharedMemory* WebBlobRegistryImpl::AllocateSharedMemory( |
| 89 size_t buf_size) { |
| 90 scoped_ptr<base::SharedMemory> shared_buf; |
| 91 #if defined(OS_WIN) |
| 92 shared_buf.reset(new base::SharedMemory); |
| 93 if (!shared_buf->CreateAndMapAnonymous(buf_size)) { |
| 94 NOTREACHED(); |
| 95 return NULL; |
| 96 } |
| 97 #else |
| 98 // On POSIX, we need to ask the browser to create the shared memory for us, |
| 99 // since this is blocked by the sandbox. |
| 100 base::SharedMemoryHandle shared_mem_handle; |
| 101 if (sender_->Send(new BlobHostMsg_SyncAllocateSharedMemory( |
| 102 buf_size, &shared_mem_handle))) { |
| 103 if (base::SharedMemory::IsHandleValid(shared_mem_handle)) { |
| 104 shared_buf.reset(new base::SharedMemory(shared_mem_handle, false)); |
| 105 if (!shared_buf_->Map(buf_size)) { |
| 106 NOTREACHED() << "Map failed"; |
| 107 return NULL; |
| 108 } |
| 109 } else { |
| 110 NOTREACHED() << "Browser failed to allocate shared memory"; |
| 111 return NULL; |
| 112 } |
| 113 } else { |
| 114 NOTREACHED() << "Browser allocation request message failed"; |
| 115 return NULL; |
| 116 } |
| 117 #endif |
| 118 return shared_buf.release(); |
| 30 } | 119 } |
| 31 | 120 |
| 32 void WebBlobRegistryImpl::registerBlobURL( | 121 void WebBlobRegistryImpl::registerBlobURL( |
| 33 const WebURL& url, const WebURL& src_url) { | 122 const WebURL& url, const WebURL& src_url) { |
| 34 sender_->Send(new BlobHostMsg_RegisterBlobUrlFrom(url, src_url)); | 123 sender_->Send(new BlobHostMsg_CloneBlob(url, src_url)); |
| 35 } | 124 } |
| 36 | 125 |
| 37 void WebBlobRegistryImpl::unregisterBlobURL(const WebURL& url) { | 126 void WebBlobRegistryImpl::unregisterBlobURL(const WebURL& url) { |
| 38 sender_->Send(new BlobHostMsg_UnregisterBlobUrl(url)); | 127 sender_->Send(new BlobHostMsg_RemoveBlob(url)); |
| 39 } | 128 } |
| OLD | NEW |