Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/shared_memory.h" | 10 #include "base/memory/shared_memory.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "content/child/child_thread.h" | 12 #include "content/child/child_thread.h" |
| 13 #include "content/child/thread_safe_sender.h" | 13 #include "content/child/thread_safe_sender.h" |
| 14 #include "content/common/fileapi/webblob_messages.h" | 14 #include "content/common/fileapi/webblob_messages.h" |
| 15 #include "storage/common/blob/blob_data.h" | 15 #include "storage/common/blob/blob_data.h" |
| 16 #include "storage/common/data_element.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; |
| 25 | 26 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 36 : sender_(sender) { | 37 : sender_(sender) { |
| 37 } | 38 } |
| 38 | 39 |
| 39 WebBlobRegistryImpl::~WebBlobRegistryImpl() { | 40 WebBlobRegistryImpl::~WebBlobRegistryImpl() { |
| 40 } | 41 } |
| 41 | 42 |
| 42 void WebBlobRegistryImpl::registerBlobData( | 43 void WebBlobRegistryImpl::registerBlobData( |
| 43 const blink::WebString& uuid, const blink::WebBlobData& data) { | 44 const blink::WebString& uuid, const blink::WebBlobData& data) { |
| 44 const std::string uuid_str(uuid.utf8()); | 45 const std::string uuid_str(uuid.utf8()); |
| 45 | 46 |
| 47 storage::DataElement data_buffer; | |
| 48 data_buffer.SetToEmptyBytes(); | |
| 49 | |
| 46 sender_->Send(new BlobHostMsg_StartBuilding(uuid_str)); | 50 sender_->Send(new BlobHostMsg_StartBuilding(uuid_str)); |
| 47 size_t i = 0; | 51 size_t i = 0; |
| 48 WebBlobData::Item data_item; | 52 WebBlobData::Item data_item; |
| 49 while (data.itemAt(i++, data_item)) { | 53 while (data.itemAt(i++, data_item)) { |
| 54 // This skips empty files and blobs. We skip empty data below. | |
|
michaeln
2015/01/22 21:57:46
this comment seems unnecessary
dmurph
2015/01/23 00:42:00
Done.
| |
| 55 if (data_item.length == 0) { | |
| 56 continue; | |
| 57 } | |
| 58 if (data_item.type != WebBlobData::Item::TypeData && | |
| 59 data_buffer.length() != 0) { | |
| 60 FlushBlobItemBuffer(uuid_str, &data_buffer); | |
| 61 } | |
| 50 switch (data_item.type) { | 62 switch (data_item.type) { |
| 51 case WebBlobData::Item::TypeData: { | 63 case WebBlobData::Item::TypeData: { |
| 52 // WebBlobData does not allow partial data items. | 64 // WebBlobData does not allow partial data items. |
| 53 DCHECK(!data_item.offset && data_item.length == -1); | 65 DCHECK(!data_item.offset && data_item.length == -1); |
| 54 SendDataForBlob(uuid_str, data_item.data); | 66 if (data_item.data.size() == 0) { |
|
michaeln
2015/01/22 21:57:46
is WebBlobData::Item.data.size() ever != to WebBlo
dmurph
2015/01/23 00:42:00
It never equals the size. See the statement above
| |
| 67 continue; | |
| 68 } | |
| 69 BufferBlobData(uuid_str, data_item.data, &data_buffer); | |
| 55 break; | 70 break; |
| 56 } | 71 } |
| 57 case WebBlobData::Item::TypeFile: | 72 case WebBlobData::Item::TypeFile: |
| 58 if (data_item.length) { | |
| 59 storage::BlobData::Item item; | 73 storage::BlobData::Item item; |
|
michaeln
2015/01/22 21:57:46
there are compile errors about 'item' being define
dmurph
2015/01/23 00:42:00
Done.
| |
| 60 item.SetToFilePathRange( | 74 item.SetToFilePathRange( |
| 61 base::FilePath::FromUTF16Unsafe(data_item.filePath), | 75 base::FilePath::FromUTF16Unsafe(data_item.filePath), |
| 62 static_cast<uint64>(data_item.offset), | 76 static_cast<uint64>(data_item.offset), |
| 63 static_cast<uint64>(data_item.length), | 77 static_cast<uint64>(data_item.length), |
| 64 base::Time::FromDoubleT(data_item.expectedModificationTime)); | 78 base::Time::FromDoubleT(data_item.expectedModificationTime)); |
| 65 sender_->Send( | 79 sender_->Send( |
| 66 new BlobHostMsg_AppendBlobDataItem(uuid_str, item)); | 80 new BlobHostMsg_AppendBlobDataItem(uuid_str, item)); |
| 67 } | |
| 68 break; | 81 break; |
| 69 case WebBlobData::Item::TypeBlob: | 82 case WebBlobData::Item::TypeBlob: |
| 70 if (data_item.length) { | |
| 71 storage::BlobData::Item item; | 83 storage::BlobData::Item item; |
| 72 item.SetToBlobRange( | 84 item.SetToBlobRange( |
| 73 data_item.blobUUID.utf8(), | 85 data_item.blobUUID.utf8(), |
| 74 static_cast<uint64>(data_item.offset), | 86 static_cast<uint64>(data_item.offset), |
| 75 static_cast<uint64>(data_item.length)); | 87 static_cast<uint64>(data_item.length)); |
| 76 sender_->Send( | 88 sender_->Send( |
| 77 new BlobHostMsg_AppendBlobDataItem(uuid_str, item)); | 89 new BlobHostMsg_AppendBlobDataItem(uuid_str, item)); |
| 78 } | |
| 79 break; | 90 break; |
| 80 case WebBlobData::Item::TypeFileSystemURL: | 91 case WebBlobData::Item::TypeFileSystemURL: |
| 81 if (data_item.length) { | |
| 82 // We only support filesystem URL as of now. | 92 // We only support filesystem URL as of now. |
| 83 DCHECK(GURL(data_item.fileSystemURL).SchemeIsFileSystem()); | 93 DCHECK(GURL(data_item.fileSystemURL).SchemeIsFileSystem()); |
| 84 storage::BlobData::Item item; | 94 storage::BlobData::Item item; |
| 85 item.SetToFileSystemUrlRange( | 95 item.SetToFileSystemUrlRange( |
| 86 data_item.fileSystemURL, | 96 data_item.fileSystemURL, |
| 87 static_cast<uint64>(data_item.offset), | 97 static_cast<uint64>(data_item.offset), |
| 88 static_cast<uint64>(data_item.length), | 98 static_cast<uint64>(data_item.length), |
| 89 base::Time::FromDoubleT(data_item.expectedModificationTime)); | 99 base::Time::FromDoubleT(data_item.expectedModificationTime)); |
| 90 sender_->Send( | 100 sender_->Send( |
| 91 new BlobHostMsg_AppendBlobDataItem(uuid_str, item)); | 101 new BlobHostMsg_AppendBlobDataItem(uuid_str, item)); |
| 92 } | |
| 93 break; | 102 break; |
| 94 default: | 103 default: |
| 95 NOTREACHED(); | 104 NOTREACHED(); |
| 96 } | 105 } |
| 97 } | 106 } |
| 107 if (data_buffer.length() != 0) { | |
| 108 FlushBlobItemBuffer(uuid_str, &data_buffer); | |
| 109 } | |
| 98 sender_->Send(new BlobHostMsg_FinishBuilding( | 110 sender_->Send(new BlobHostMsg_FinishBuilding( |
| 99 uuid_str, data.contentType().utf8().data())); | 111 uuid_str, data.contentType().utf8().data())); |
| 100 } | 112 } |
| 101 | 113 |
| 102 void WebBlobRegistryImpl::addBlobDataRef(const WebString& uuid) { | 114 void WebBlobRegistryImpl::addBlobDataRef(const WebString& uuid) { |
| 103 sender_->Send(new BlobHostMsg_IncrementRefCount(uuid.utf8())); | 115 sender_->Send(new BlobHostMsg_IncrementRefCount(uuid.utf8())); |
| 104 } | 116 } |
| 105 | 117 |
| 106 void WebBlobRegistryImpl::removeBlobDataRef(const WebString& uuid) { | 118 void WebBlobRegistryImpl::removeBlobDataRef(const WebString& uuid) { |
| 107 sender_->Send(new BlobHostMsg_DecrementRefCount(uuid.utf8())); | 119 sender_->Send(new BlobHostMsg_DecrementRefCount(uuid.utf8())); |
| 108 } | 120 } |
| 109 | 121 |
| 110 void WebBlobRegistryImpl::registerPublicBlobURL( | 122 void WebBlobRegistryImpl::registerPublicBlobURL( |
| 111 const WebURL& url, const WebString& uuid) { | 123 const WebURL& url, const WebString& uuid) { |
| 112 sender_->Send(new BlobHostMsg_RegisterPublicURL(url, uuid.utf8())); | 124 sender_->Send(new BlobHostMsg_RegisterPublicURL(url, uuid.utf8())); |
| 113 } | 125 } |
| 114 | 126 |
| 115 void WebBlobRegistryImpl::revokePublicBlobURL(const WebURL& url) { | 127 void WebBlobRegistryImpl::revokePublicBlobURL(const WebURL& url) { |
| 116 sender_->Send(new BlobHostMsg_RevokePublicURL(url)); | 128 sender_->Send(new BlobHostMsg_RevokePublicURL(url)); |
| 117 } | 129 } |
| 118 | 130 |
| 119 void WebBlobRegistryImpl::SendDataForBlob(const std::string& uuid_str, | 131 void WebBlobRegistryImpl::FlushBlobItemBuffer( |
| 120 const WebThreadSafeData& data) { | 132 const std::string& uuid_str, |
| 133 storage::DataElement* data_buffer) const { | |
| 134 DCHECK_NE(data_buffer->length(), 0ul); | |
| 135 DCHECK_LT(data_buffer->length(), kLargeThresholdBytes); | |
| 136 storage::BlobData::Item item; | |
|
michaeln
2015/01/22 21:57:46
local no longer needed
dmurph
2015/01/23 00:42:00
Done.
| |
| 137 sender_->Send(new BlobHostMsg_AppendBlobDataItem(uuid_str, *data_buffer)); | |
| 138 data_buffer->SetToEmptyBytes(); | |
| 139 } | |
| 121 | 140 |
| 122 if (data.size() == 0) | 141 void WebBlobRegistryImpl::BufferBlobData(const std::string& uuid_str, |
| 123 return; | 142 const blink::WebThreadSafeData& data, |
| 124 if (data.size() < kLargeThresholdBytes) { | 143 storage::DataElement* data_buffer) { |
| 125 storage::BlobData::Item item; | 144 size_t buffer_size = data_buffer->length(); |
| 126 item.SetToBytes(data.data(), data.size()); | 145 size_t data_size = data.size(); |
| 127 sender_->Send(new BlobHostMsg_AppendBlobDataItem(uuid_str, item)); | 146 DCHECK_NE(data_size, 0ul); |
| 147 if (buffer_size != 0 && buffer_size + data_size >= kLargeThresholdBytes) { | |
| 148 FlushBlobItemBuffer(uuid_str, data_buffer); | |
| 149 buffer_size = 0; | |
| 150 } | |
| 151 if (data_size >= kLargeThresholdBytes) { | |
| 152 SendOversizedDataForBlob(uuid_str, data); | |
| 128 } else { | 153 } else { |
| 129 // We handle larger amounts of data via SharedMemory instead of | 154 DCHECK_LT(buffer_size + data_size, kLargeThresholdBytes); |
| 130 // writing it directly to the IPC channel. | 155 data_buffer->AppendBytes(data.data(), data_size); |
| 131 size_t shared_memory_size = std::min( | |
| 132 data.size(), kMaxSharedMemoryBytes); | |
| 133 scoped_ptr<base::SharedMemory> shared_memory( | |
| 134 ChildThread::AllocateSharedMemory(shared_memory_size, | |
| 135 sender_.get())); | |
| 136 CHECK(shared_memory.get()); | |
| 137 if (!shared_memory->Map(shared_memory_size)) | |
| 138 CHECK(false); | |
| 139 | |
| 140 size_t data_size = data.size(); | |
| 141 const char* data_ptr = data.data(); | |
| 142 while (data_size) { | |
| 143 size_t chunk_size = std::min(data_size, shared_memory_size); | |
| 144 memcpy(shared_memory->memory(), data_ptr, chunk_size); | |
| 145 sender_->Send(new BlobHostMsg_SyncAppendSharedMemory( | |
| 146 uuid_str, shared_memory->handle(), chunk_size)); | |
| 147 data_size -= chunk_size; | |
| 148 data_ptr += chunk_size; | |
| 149 } | |
| 150 } | 156 } |
| 151 } | 157 } |
| 152 | 158 |
| 159 void WebBlobRegistryImpl::SendOversizedDataForBlob( | |
| 160 const std::string& uuid_str, | |
| 161 const blink::WebThreadSafeData& data) { | |
| 162 DCHECK_GE(data.size(), kLargeThresholdBytes); | |
| 163 // We handle larger amounts of data via SharedMemory instead of | |
| 164 // writing it directly to the IPC channel. | |
| 165 size_t shared_memory_size = std::min(data.size(), kMaxSharedMemoryBytes); | |
| 166 scoped_ptr<base::SharedMemory> shared_memory( | |
| 167 ChildThread::AllocateSharedMemory(shared_memory_size, sender_.get())); | |
| 168 CHECK(shared_memory.get()); | |
| 169 if (!shared_memory->Map(shared_memory_size)) | |
| 170 CHECK(false); | |
| 171 | |
| 172 size_t data_size = data.size(); | |
| 173 const char* data_ptr = data.data(); | |
| 174 while (data_size) { | |
| 175 size_t chunk_size = std::min(data_size, shared_memory_size); | |
| 176 memcpy(shared_memory->memory(), data_ptr, chunk_size); | |
| 177 sender_->Send(new BlobHostMsg_SyncAppendSharedMemory( | |
| 178 uuid_str, shared_memory->handle(), chunk_size)); | |
| 179 data_size -= chunk_size; | |
| 180 data_ptr += chunk_size; | |
| 181 } | |
| 182 } | |
| 183 | |
| 153 // ------ streams stuff ----- | 184 // ------ streams stuff ----- |
| 154 | 185 |
| 155 void WebBlobRegistryImpl::registerStreamURL( | 186 void WebBlobRegistryImpl::registerStreamURL( |
| 156 const WebURL& url, const WebString& content_type) { | 187 const WebURL& url, const WebString& content_type) { |
| 157 DCHECK(ChildThread::current()); | 188 DCHECK(ChildThread::current()); |
| 158 sender_->Send(new StreamHostMsg_StartBuilding(url, content_type.utf8())); | 189 sender_->Send(new StreamHostMsg_StartBuilding(url, content_type.utf8())); |
| 159 } | 190 } |
| 160 | 191 |
| 161 void WebBlobRegistryImpl::registerStreamURL( | 192 void WebBlobRegistryImpl::registerStreamURL( |
| 162 const WebURL& url, const WebURL& src_url) { | 193 const WebURL& url, const WebURL& src_url) { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 212 DCHECK(ChildThread::current()); | 243 DCHECK(ChildThread::current()); |
| 213 sender_->Send(new StreamHostMsg_AbortBuilding(url)); | 244 sender_->Send(new StreamHostMsg_AbortBuilding(url)); |
| 214 } | 245 } |
| 215 | 246 |
| 216 void WebBlobRegistryImpl::unregisterStreamURL(const WebURL& url) { | 247 void WebBlobRegistryImpl::unregisterStreamURL(const WebURL& url) { |
| 217 DCHECK(ChildThread::current()); | 248 DCHECK(ChildThread::current()); |
| 218 sender_->Send(new StreamHostMsg_Remove(url)); | 249 sender_->Send(new StreamHostMsg_Remove(url)); |
| 219 } | 250 } |
| 220 | 251 |
| 221 } // namespace content | 252 } // namespace content |
| OLD | NEW |