| 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/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/shared_memory.h" | 10 #include "base/memory/shared_memory.h" |
| 10 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 11 #include "content/child/child_thread.h" | 12 #include "content/child/child_thread.h" |
| 12 #include "content/child/thread_safe_sender.h" | 13 #include "content/child/thread_safe_sender.h" |
| 13 #include "content/common/fileapi/webblob_messages.h" | 14 #include "content/common/fileapi/webblob_messages.h" |
| 14 #include "third_party/WebKit/public/platform/WebBlobData.h" | 15 #include "third_party/WebKit/public/platform/WebBlobData.h" |
| 15 #include "third_party/WebKit/public/platform/WebString.h" | 16 #include "third_party/WebKit/public/platform/WebString.h" |
| 16 #include "third_party/WebKit/public/platform/WebThreadSafeData.h" | 17 #include "third_party/WebKit/public/platform/WebThreadSafeData.h" |
| 17 #include "third_party/WebKit/public/platform/WebURL.h" | 18 #include "third_party/WebKit/public/platform/WebURL.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 31 | 32 |
| 32 } // namespace | 33 } // namespace |
| 33 | 34 |
| 34 WebBlobRegistryImpl::WebBlobRegistryImpl(ThreadSafeSender* sender) | 35 WebBlobRegistryImpl::WebBlobRegistryImpl(ThreadSafeSender* sender) |
| 35 : sender_(sender) { | 36 : sender_(sender) { |
| 36 } | 37 } |
| 37 | 38 |
| 38 WebBlobRegistryImpl::~WebBlobRegistryImpl() { | 39 WebBlobRegistryImpl::~WebBlobRegistryImpl() { |
| 39 } | 40 } |
| 40 | 41 |
| 41 void WebBlobRegistryImpl::SendDataForBlob(const WebURL& url, | 42 void WebBlobRegistryImpl::registerBlobData( |
| 42 const WebThreadSafeData& data) { | 43 const WebKit::WebString& uuid, const WebKit::WebBlobData& data) { |
| 44 const std::string uuid_str(uuid.utf8()); |
| 43 | 45 |
| 44 if (data.size() == 0) | 46 sender_->Send(new BlobHostMsg_StartBuilding(uuid_str)); |
| 45 return; | |
| 46 if (data.size() < kLargeThresholdBytes) { | |
| 47 webkit_blob::BlobData::Item item; | |
| 48 item.SetToBytes(data.data(), data.size()); | |
| 49 sender_->Send(new BlobHostMsg_AppendBlobDataItem(url, item)); | |
| 50 } else { | |
| 51 // We handle larger amounts of data via SharedMemory instead of | |
| 52 // writing it directly to the IPC channel. | |
| 53 size_t shared_memory_size = std::min( | |
| 54 data.size(), kMaxSharedMemoryBytes); | |
| 55 scoped_ptr<base::SharedMemory> shared_memory( | |
| 56 ChildThread::AllocateSharedMemory(shared_memory_size, | |
| 57 sender_.get())); | |
| 58 CHECK(shared_memory.get()); | |
| 59 | |
| 60 size_t data_size = data.size(); | |
| 61 const char* data_ptr = data.data(); | |
| 62 while (data_size) { | |
| 63 size_t chunk_size = std::min(data_size, shared_memory_size); | |
| 64 memcpy(shared_memory->memory(), data_ptr, chunk_size); | |
| 65 sender_->Send(new BlobHostMsg_SyncAppendSharedMemory( | |
| 66 url, shared_memory->handle(), chunk_size)); | |
| 67 data_size -= chunk_size; | |
| 68 data_ptr += chunk_size; | |
| 69 } | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 void WebBlobRegistryImpl::registerBlobURL( | |
| 74 const WebURL& url, WebBlobData& data) { | |
| 75 DCHECK(ChildThread::current()); | |
| 76 sender_->Send(new BlobHostMsg_StartBuilding(url)); | |
| 77 size_t i = 0; | 47 size_t i = 0; |
| 78 WebBlobData::Item data_item; | 48 WebBlobData::Item data_item; |
| 79 while (data.itemAt(i++, data_item)) { | 49 while (data.itemAt(i++, data_item)) { |
| 80 switch (data_item.type) { | 50 switch (data_item.type) { |
| 81 case WebBlobData::Item::TypeData: { | 51 case WebBlobData::Item::TypeData: { |
| 82 // WebBlobData does not allow partial data items. | 52 // WebBlobData does not allow partial data items. |
| 83 DCHECK(!data_item.offset && data_item.length == -1); | 53 DCHECK(!data_item.offset && data_item.length == -1); |
| 84 SendDataForBlob(url, data_item.data); | 54 SendDataForBlob(uuid_str, data_item.data); |
| 85 break; | 55 break; |
| 86 } | 56 } |
| 87 case WebBlobData::Item::TypeFile: | 57 case WebBlobData::Item::TypeFile: |
| 88 if (data_item.length) { | 58 if (data_item.length) { |
| 89 webkit_blob::BlobData::Item item; | 59 webkit_blob::BlobData::Item item; |
| 90 item.SetToFilePathRange( | 60 item.SetToFilePathRange( |
| 91 base::FilePath::FromUTF16Unsafe(data_item.filePath), | 61 base::FilePath::FromUTF16Unsafe(data_item.filePath), |
| 92 static_cast<uint64>(data_item.offset), | 62 static_cast<uint64>(data_item.offset), |
| 93 static_cast<uint64>(data_item.length), | 63 static_cast<uint64>(data_item.length), |
| 94 base::Time::FromDoubleT(data_item.expectedModificationTime)); | 64 base::Time::FromDoubleT(data_item.expectedModificationTime)); |
| 95 sender_->Send( | 65 sender_->Send( |
| 96 new BlobHostMsg_AppendBlobDataItem(url, item)); | 66 new BlobHostMsg_AppendBlobDataItem(uuid_str, item)); |
| 97 } | 67 } |
| 98 break; | 68 break; |
| 99 case WebBlobData::Item::TypeBlob: | 69 case WebBlobData::Item::TypeBlob: |
| 100 if (data_item.length) { | 70 if (data_item.length) { |
| 101 webkit_blob::BlobData::Item item; | 71 webkit_blob::BlobData::Item item; |
| 102 item.SetToBlobUrlRange( | 72 item.SetToBlobUrlRange( |
| 103 data_item.blobURL, | 73 data_item.blobURL, |
| 104 static_cast<uint64>(data_item.offset), | 74 static_cast<uint64>(data_item.offset), |
| 105 static_cast<uint64>(data_item.length)); | 75 static_cast<uint64>(data_item.length)); |
| 106 sender_->Send( | 76 sender_->Send( |
| 107 new BlobHostMsg_AppendBlobDataItem(url, item)); | 77 new BlobHostMsg_AppendBlobDataItem(uuid_str, item)); |
| 108 } | 78 } |
| 109 break; | 79 break; |
| 110 case WebBlobData::Item::TypeURL: | 80 case WebBlobData::Item::TypeURL: |
| 111 if (data_item.length) { | 81 if (data_item.length) { |
| 112 // We only support filesystem URL as of now. | 82 // We only support filesystem URL as of now. |
| 113 DCHECK(GURL(data_item.url).SchemeIsFileSystem()); | 83 DCHECK(GURL(data_item.url).SchemeIsFileSystem()); |
| 114 webkit_blob::BlobData::Item item; | 84 webkit_blob::BlobData::Item item; |
| 115 item.SetToFileSystemUrlRange( | 85 item.SetToFileSystemUrlRange( |
| 116 data_item.url, | 86 data_item.url, |
| 117 static_cast<uint64>(data_item.offset), | 87 static_cast<uint64>(data_item.offset), |
| 118 static_cast<uint64>(data_item.length), | 88 static_cast<uint64>(data_item.length), |
| 119 base::Time::FromDoubleT(data_item.expectedModificationTime)); | 89 base::Time::FromDoubleT(data_item.expectedModificationTime)); |
| 120 sender_->Send( | 90 sender_->Send( |
| 121 new BlobHostMsg_AppendBlobDataItem(url, item)); | 91 new BlobHostMsg_AppendBlobDataItem(uuid_str, item)); |
| 122 } | 92 } |
| 123 break; | 93 break; |
| 124 default: | 94 default: |
| 125 NOTREACHED(); | 95 NOTREACHED(); |
| 126 } | 96 } |
| 127 } | 97 } |
| 128 sender_->Send(new BlobHostMsg_FinishBuilding( | 98 sender_->Send(new BlobHostMsg_FinishBuilding( |
| 129 url, data.contentType().utf8().data())); | 99 uuid_str, data.contentType().utf8().data())); |
| 100 } |
| 101 |
| 102 void WebBlobRegistryImpl::addBlobDataRef(const WebString& uuid) { |
| 103 sender_->Send(new BlobHostMsg_IncrementRefCount(uuid.utf8())); |
| 104 } |
| 105 |
| 106 void WebBlobRegistryImpl::removeBlobDataRef(const WebString& uuid) { |
| 107 sender_->Send(new BlobHostMsg_DecrementRefCount(uuid.utf8())); |
| 108 } |
| 109 |
| 110 void WebBlobRegistryImpl::registerPublicBlobURL( |
| 111 const WebURL& url, const WebString& uuid) { |
| 112 sender_->Send(new BlobHostMsg_RegisterPublicURL(url, uuid.utf8())); |
| 113 } |
| 114 |
| 115 void WebBlobRegistryImpl::revokePublicBlobURL(const WebURL& url) { |
| 116 sender_->Send(new BlobHostMsg_RevokePublicURL(url)); |
| 117 } |
| 118 |
| 119 void WebBlobRegistryImpl::SendDataForBlob(const std::string& uuid_str, |
| 120 const WebThreadSafeData& data) { |
| 121 |
| 122 if (data.size() == 0) |
| 123 return; |
| 124 if (data.size() < kLargeThresholdBytes) { |
| 125 webkit_blob::BlobData::Item item; |
| 126 item.SetToBytes(data.data(), data.size()); |
| 127 sender_->Send(new BlobHostMsg_AppendBlobDataItem(uuid_str, item)); |
| 128 } else { |
| 129 // We handle larger amounts of data via SharedMemory instead of |
| 130 // writing it directly to the IPC channel. |
| 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 |
| 138 size_t data_size = data.size(); |
| 139 const char* data_ptr = data.data(); |
| 140 while (data_size) { |
| 141 size_t chunk_size = std::min(data_size, shared_memory_size); |
| 142 memcpy(shared_memory->memory(), data_ptr, chunk_size); |
| 143 sender_->Send(new BlobHostMsg_SyncAppendSharedMemory( |
| 144 uuid_str, shared_memory->handle(), chunk_size)); |
| 145 data_size -= chunk_size; |
| 146 data_ptr += chunk_size; |
| 147 } |
| 148 } |
| 149 } |
| 150 |
| 151 // DEPRECATED, almost. Until blink is updated, we implement these older methods |
| 152 // in terms of our newer blob storage system. We create a uuid for each 'data' |
| 153 // we see and construct a mapping from the private blob urls we're given to |
| 154 // that uuid. The mapping is maintained in the browser process. |
| 155 // |
| 156 // Chromium is setup to speak in terms of old-style private blob urls or |
| 157 // new-style uuid identifiers. Once blink has been migrated support for |
| 158 // the old-style will be deleted. Search for the term deprecated. |
| 159 |
| 160 void WebBlobRegistryImpl::registerBlobURL( |
| 161 const WebURL& url, WebBlobData& data) { |
| 162 std::string uuid = base::GenerateGUID(); |
| 163 registerBlobData(WebKit::WebString::fromUTF8(uuid), data); |
| 164 sender_->Send(new BlobHostMsg_DeprecatedRegisterBlobURL(url, uuid)); |
| 165 sender_->Send(new BlobHostMsg_DecrementRefCount(uuid)); |
| 130 } | 166 } |
| 131 | 167 |
| 132 void WebBlobRegistryImpl::registerBlobURL( | 168 void WebBlobRegistryImpl::registerBlobURL( |
| 133 const WebURL& url, const WebURL& src_url) { | 169 const WebURL& url, const WebURL& src_url) { |
| 134 DCHECK(ChildThread::current()); | 170 sender_->Send(new BlobHostMsg_DeprecatedCloneBlobURL(url, src_url)); |
| 135 sender_->Send(new BlobHostMsg_Clone(url, src_url)); | |
| 136 } | 171 } |
| 137 | 172 |
| 138 void WebBlobRegistryImpl::unregisterBlobURL(const WebURL& url) { | 173 void WebBlobRegistryImpl::unregisterBlobURL(const WebURL& url) { |
| 139 DCHECK(ChildThread::current()); | 174 sender_->Send(new BlobHostMsg_DeprecatedRevokeBlobURL(url)); |
| 140 sender_->Send(new BlobHostMsg_Remove(url)); | |
| 141 } | 175 } |
| 142 | 176 |
| 177 |
| 178 // ------ streams stuff ----- |
| 179 |
| 143 void WebBlobRegistryImpl::registerStreamURL( | 180 void WebBlobRegistryImpl::registerStreamURL( |
| 144 const WebURL& url, const WebString& content_type) { | 181 const WebURL& url, const WebString& content_type) { |
| 145 DCHECK(ChildThread::current()); | 182 DCHECK(ChildThread::current()); |
| 146 sender_->Send(new StreamHostMsg_StartBuilding(url, content_type.utf8())); | 183 sender_->Send(new StreamHostMsg_StartBuilding(url, content_type.utf8())); |
| 147 } | 184 } |
| 148 | 185 |
| 149 void WebBlobRegistryImpl::registerStreamURL( | 186 void WebBlobRegistryImpl::registerStreamURL( |
| 150 const WebURL& url, const WebURL& src_url) { | 187 const WebURL& url, const WebURL& src_url) { |
| 151 DCHECK(ChildThread::current()); | 188 DCHECK(ChildThread::current()); |
| 152 sender_->Send(new StreamHostMsg_Clone(url, src_url)); | 189 sender_->Send(new StreamHostMsg_Clone(url, src_url)); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 DCHECK(ChildThread::current()); | 230 DCHECK(ChildThread::current()); |
| 194 sender_->Send(new StreamHostMsg_AbortBuilding(url)); | 231 sender_->Send(new StreamHostMsg_AbortBuilding(url)); |
| 195 } | 232 } |
| 196 | 233 |
| 197 void WebBlobRegistryImpl::unregisterStreamURL(const WebURL& url) { | 234 void WebBlobRegistryImpl::unregisterStreamURL(const WebURL& url) { |
| 198 DCHECK(ChildThread::current()); | 235 DCHECK(ChildThread::current()); |
| 199 sender_->Send(new StreamHostMsg_Remove(url)); | 236 sender_->Send(new StreamHostMsg_Remove(url)); |
| 200 } | 237 } |
| 201 | 238 |
| 202 } // namespace content | 239 } // namespace content |
| OLD | NEW |