| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 "content/common/fileapi/webblobregistry_impl.h" | |
| 6 | |
| 7 #include "base/memory/ref_counted.h" | |
| 8 #include "base/message_loop.h" | |
| 9 #include "base/shared_memory.h" | |
| 10 #include "content/common/child_thread.h" | |
| 11 #include "content/common/fileapi/webblob_messages.h" | |
| 12 #include "content/common/thread_safe_sender.h" | |
| 13 #include "third_party/WebKit/Source/Platform/chromium/public/WebBlobData.h" | |
| 14 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h" | |
| 15 #include "third_party/WebKit/Source/Platform/chromium/public/WebURL.h" | |
| 16 #include "webkit/base/file_path_string_conversions.h" | |
| 17 #include "webkit/blob/blob_data.h" | |
| 18 | |
| 19 using WebKit::WebBlobData; | |
| 20 using WebKit::WebString; | |
| 21 using WebKit::WebURL; | |
| 22 | |
| 23 namespace content { | |
| 24 | |
| 25 WebBlobRegistryImpl::WebBlobRegistryImpl(ThreadSafeSender* sender) | |
| 26 : sender_(sender) { | |
| 27 } | |
| 28 | |
| 29 WebBlobRegistryImpl::~WebBlobRegistryImpl() { | |
| 30 } | |
| 31 | |
| 32 void WebBlobRegistryImpl::registerBlobURL( | |
| 33 const WebURL& url, WebBlobData& data) { | |
| 34 DCHECK(ChildThread::current()->message_loop() == | |
| 35 base::MessageLoop::current()); | |
| 36 const size_t kLargeThresholdBytes = 250 * 1024; | |
| 37 const size_t kMaxSharedMemoryBytes = 10 * 1024 * 1024; | |
| 38 | |
| 39 sender_->Send(new BlobHostMsg_StartBuildingBlob(url)); | |
| 40 size_t i = 0; | |
| 41 WebBlobData::Item data_item; | |
| 42 while (data.itemAt(i++, data_item)) { | |
| 43 webkit_blob::BlobData::Item item; | |
| 44 switch (data_item.type) { | |
| 45 case WebBlobData::Item::TypeData: { | |
| 46 // WebBlobData does not allow partial data items. | |
| 47 DCHECK(!data_item.offset && data_item.length == -1); | |
| 48 if (data_item.data.size() == 0) | |
| 49 break; | |
| 50 if (data_item.data.size() < kLargeThresholdBytes) { | |
| 51 item.SetToBytes(data_item.data.data(), data_item.data.size()); | |
| 52 sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); | |
| 53 } else { | |
| 54 // We handle larger amounts of data via SharedMemory instead of | |
| 55 // writing it directly to the IPC channel. | |
| 56 size_t data_size = data_item.data.size(); | |
| 57 const char* data_ptr = data_item.data.data(); | |
| 58 size_t shared_memory_size = std::min( | |
| 59 data_size, kMaxSharedMemoryBytes); | |
| 60 scoped_ptr<base::SharedMemory> shared_memory( | |
| 61 ChildThread::AllocateSharedMemory(shared_memory_size, sender_)); | |
| 62 CHECK(shared_memory.get()); | |
| 63 while (data_size) { | |
| 64 size_t chunk_size = std::min(data_size, shared_memory_size); | |
| 65 memcpy(shared_memory->memory(), data_ptr, chunk_size); | |
| 66 sender_->Send(new BlobHostMsg_SyncAppendSharedMemory( | |
| 67 url, shared_memory->handle(), chunk_size)); | |
| 68 data_size -= chunk_size; | |
| 69 data_ptr += chunk_size; | |
| 70 } | |
| 71 } | |
| 72 break; | |
| 73 } | |
| 74 case WebBlobData::Item::TypeFile: | |
| 75 if (data_item.length) { | |
| 76 item.SetToFilePathRange( | |
| 77 webkit_base::WebStringToFilePath(data_item.filePath), | |
| 78 static_cast<uint64>(data_item.offset), | |
| 79 static_cast<uint64>(data_item.length), | |
| 80 base::Time::FromDoubleT(data_item.expectedModificationTime)); | |
| 81 sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); | |
| 82 } | |
| 83 break; | |
| 84 case WebBlobData::Item::TypeBlob: | |
| 85 if (data_item.length) { | |
| 86 item.SetToBlobUrlRange( | |
| 87 data_item.blobURL, | |
| 88 static_cast<uint64>(data_item.offset), | |
| 89 static_cast<uint64>(data_item.length)); | |
| 90 sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); | |
| 91 } | |
| 92 break; | |
| 93 case WebBlobData::Item::TypeURL: | |
| 94 if (data_item.length) { | |
| 95 // We only support filesystem URL as of now. | |
| 96 DCHECK(GURL(data_item.url).SchemeIsFileSystem()); | |
| 97 item.SetToFileSystemUrlRange( | |
| 98 data_item.url, | |
| 99 static_cast<uint64>(data_item.offset), | |
| 100 static_cast<uint64>(data_item.length), | |
| 101 base::Time::FromDoubleT(data_item.expectedModificationTime)); | |
| 102 sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); | |
| 103 } | |
| 104 break; | |
| 105 default: | |
| 106 NOTREACHED(); | |
| 107 } | |
| 108 } | |
| 109 sender_->Send(new BlobHostMsg_FinishBuildingBlob( | |
| 110 url, data.contentType().utf8().data())); | |
| 111 } | |
| 112 | |
| 113 void WebBlobRegistryImpl::registerBlobURL( | |
| 114 const WebURL& url, const WebURL& src_url) { | |
| 115 DCHECK(ChildThread::current()->message_loop() == | |
| 116 base::MessageLoop::current()); | |
| 117 sender_->Send(new BlobHostMsg_CloneBlob(url, src_url)); | |
| 118 } | |
| 119 | |
| 120 void WebBlobRegistryImpl::unregisterBlobURL(const WebURL& url) { | |
| 121 DCHECK(ChildThread::current()->message_loop() == | |
| 122 base::MessageLoop::current()); | |
| 123 sender_->Send(new BlobHostMsg_RemoveBlob(url)); | |
| 124 } | |
| 125 | |
| 126 } // namespace content | |
| OLD | NEW |