[email protected] | 16dd6e2 | 2012-03-01 19:08:20 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 22339b1 | 2010-08-27 18:29:24 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | b76b556d | 2013-05-29 03:09:43 | [diff] [blame] | 5 | #include "content/common_child/webblobregistry_impl.h" |
[email protected] | 22339b1 | 2010-08-27 18:29:24 | [diff] [blame] | 6 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 7 | #include "base/memory/ref_counted.h" |
[email protected] | b180a0bb | 2013-03-06 00:36:10 | [diff] [blame] | 8 | #include "base/message_loop.h" |
[email protected] | a3c71e8 | 2011-10-17 23:00:51 | [diff] [blame] | 9 | #include "base/shared_memory.h" |
| 10 | #include "content/common/child_thread.h" |
[email protected] | 16dd6e2 | 2012-03-01 19:08:20 | [diff] [blame] | 11 | #include "content/common/fileapi/webblob_messages.h" |
[email protected] | b180a0bb | 2013-03-06 00:36:10 | [diff] [blame] | 12 | #include "content/common/thread_safe_sender.h" |
[email protected] | 5c30b5e0 | 2013-05-30 03:46:08 | [diff] [blame^] | 13 | #include "third_party/WebKit/public/platform/WebBlobData.h" |
| 14 | #include "third_party/WebKit/public/platform/WebString.h" |
| 15 | #include "third_party/WebKit/public/platform/WebURL.h" |
[email protected] | bb0e7947 | 2012-10-23 04:36:34 | [diff] [blame] | 16 | #include "webkit/base/file_path_string_conversions.h" |
[email protected] | b76b556d | 2013-05-29 03:09:43 | [diff] [blame] | 17 | #include "webkit/common/blob/blob_data.h" |
[email protected] | 22339b1 | 2010-08-27 18:29:24 | [diff] [blame] | 18 | |
| 19 | using WebKit::WebBlobData; |
[email protected] | 22339b1 | 2010-08-27 18:29:24 | [diff] [blame] | 20 | using WebKit::WebString; |
| 21 | using WebKit::WebURL; |
| 22 | |
[email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 23 | namespace content { |
| 24 | |
[email protected] | b180a0bb | 2013-03-06 00:36:10 | [diff] [blame] | 25 | WebBlobRegistryImpl::WebBlobRegistryImpl(ThreadSafeSender* sender) |
| 26 | : sender_(sender) { |
[email protected] | 22339b1 | 2010-08-27 18:29:24 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | WebBlobRegistryImpl::~WebBlobRegistryImpl() { |
| 30 | } |
| 31 | |
| 32 | void WebBlobRegistryImpl::registerBlobURL( |
| 33 | const WebURL& url, WebBlobData& data) { |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 34 | DCHECK(ChildThread::current()->message_loop() == |
| 35 | base::MessageLoop::current()); |
[email protected] | a3c71e8 | 2011-10-17 23:00:51 | [diff] [blame] | 36 | const size_t kLargeThresholdBytes = 250 * 1024; |
| 37 | const size_t kMaxSharedMemoryBytes = 10 * 1024 * 1024; |
| 38 | |
[email protected] | b180a0bb | 2013-03-06 00:36:10 | [diff] [blame] | 39 | sender_->Send(new BlobHostMsg_StartBuildingBlob(url)); |
[email protected] | a3c71e8 | 2011-10-17 23:00:51 | [diff] [blame] | 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); |
[email protected] | 40c889e | 2012-05-24 15:40:12 | [diff] [blame] | 48 | if (data_item.data.size() == 0) |
| 49 | break; |
[email protected] | a3c71e8 | 2011-10-17 23:00:51 | [diff] [blame] | 50 | if (data_item.data.size() < kLargeThresholdBytes) { |
[email protected] | 9ff2b229 | 2012-08-23 14:32:43 | [diff] [blame] | 51 | item.SetToBytes(data_item.data.data(), data_item.data.size()); |
[email protected] | b180a0bb | 2013-03-06 00:36:10 | [diff] [blame] | 52 | sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); |
[email protected] | a3c71e8 | 2011-10-17 23:00:51 | [diff] [blame] | 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( |
[email protected] | b180a0bb | 2013-03-06 00:36:10 | [diff] [blame] | 61 | ChildThread::AllocateSharedMemory(shared_memory_size, sender_)); |
[email protected] | a3c71e8 | 2011-10-17 23:00:51 | [diff] [blame] | 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); |
[email protected] | b180a0bb | 2013-03-06 00:36:10 | [diff] [blame] | 66 | sender_->Send(new BlobHostMsg_SyncAppendSharedMemory( |
[email protected] | a3c71e8 | 2011-10-17 23:00:51 | [diff] [blame] | 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: |
[email protected] | 40c889e | 2012-05-24 15:40:12 | [diff] [blame] | 75 | if (data_item.length) { |
[email protected] | 9ff2b229 | 2012-08-23 14:32:43 | [diff] [blame] | 76 | item.SetToFilePathRange( |
[email protected] | bb0e7947 | 2012-10-23 04:36:34 | [diff] [blame] | 77 | webkit_base::WebStringToFilePath(data_item.filePath), |
[email protected] | 40c889e | 2012-05-24 15:40:12 | [diff] [blame] | 78 | static_cast<uint64>(data_item.offset), |
| 79 | static_cast<uint64>(data_item.length), |
| 80 | base::Time::FromDoubleT(data_item.expectedModificationTime)); |
[email protected] | b180a0bb | 2013-03-06 00:36:10 | [diff] [blame] | 81 | sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); |
[email protected] | 40c889e | 2012-05-24 15:40:12 | [diff] [blame] | 82 | } |
[email protected] | a3c71e8 | 2011-10-17 23:00:51 | [diff] [blame] | 83 | break; |
| 84 | case WebBlobData::Item::TypeBlob: |
| 85 | if (data_item.length) { |
[email protected] | 9ff2b229 | 2012-08-23 14:32:43 | [diff] [blame] | 86 | item.SetToBlobUrlRange( |
[email protected] | a3c71e8 | 2011-10-17 23:00:51 | [diff] [blame] | 87 | data_item.blobURL, |
| 88 | static_cast<uint64>(data_item.offset), |
| 89 | static_cast<uint64>(data_item.length)); |
[email protected] | b180a0bb | 2013-03-06 00:36:10 | [diff] [blame] | 90 | sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); |
[email protected] | a3c71e8 | 2011-10-17 23:00:51 | [diff] [blame] | 91 | } |
[email protected] | a3c71e8 | 2011-10-17 23:00:51 | [diff] [blame] | 92 | break; |
[email protected] | 2cd2107 | 2012-08-28 10:19:45 | [diff] [blame] | 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)); |
[email protected] | b180a0bb | 2013-03-06 00:36:10 | [diff] [blame] | 102 | sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); |
[email protected] | 2cd2107 | 2012-08-28 10:19:45 | [diff] [blame] | 103 | } |
| 104 | break; |
[email protected] | a3c71e8 | 2011-10-17 23:00:51 | [diff] [blame] | 105 | default: |
| 106 | NOTREACHED(); |
| 107 | } |
| 108 | } |
[email protected] | b180a0bb | 2013-03-06 00:36:10 | [diff] [blame] | 109 | sender_->Send(new BlobHostMsg_FinishBuildingBlob( |
[email protected] | a3c71e8 | 2011-10-17 23:00:51 | [diff] [blame] | 110 | url, data.contentType().utf8().data())); |
[email protected] | 22339b1 | 2010-08-27 18:29:24 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void WebBlobRegistryImpl::registerBlobURL( |
| 114 | const WebURL& url, const WebURL& src_url) { |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 115 | DCHECK(ChildThread::current()->message_loop() == |
| 116 | base::MessageLoop::current()); |
[email protected] | b180a0bb | 2013-03-06 00:36:10 | [diff] [blame] | 117 | sender_->Send(new BlobHostMsg_CloneBlob(url, src_url)); |
[email protected] | 22339b1 | 2010-08-27 18:29:24 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void WebBlobRegistryImpl::unregisterBlobURL(const WebURL& url) { |
[email protected] | dd32b127 | 2013-05-04 14:17:11 | [diff] [blame] | 121 | DCHECK(ChildThread::current()->message_loop() == |
| 122 | base::MessageLoop::current()); |
[email protected] | b180a0bb | 2013-03-06 00:36:10 | [diff] [blame] | 123 | sender_->Send(new BlobHostMsg_RemoveBlob(url)); |
[email protected] | 22339b1 | 2010-08-27 18:29:24 | [diff] [blame] | 124 | } |
[email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 125 | |
| 126 | } // namespace content |