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