OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/child/webblobregistry_impl.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/files/file_path.h" | |
9 #include "base/guid.h" | |
10 #include "base/location.h" | |
11 #include "base/memory/ptr_util.h" | |
12 #include "base/memory/ref_counted.h" | |
13 #include "base/memory/shared_memory.h" | |
14 #include "base/message_loop/message_loop.h" | |
15 #include "base/numerics/safe_conversions.h" | |
16 #include "base/trace_event/trace_event.h" | |
17 #include "content/child/blob_storage/blob_consolidation.h" | |
18 #include "content/child/blob_storage/blob_transport_controller.h" | |
19 #include "content/child/child_thread_impl.h" | |
20 #include "content/child/thread_safe_sender.h" | |
21 #include "content/common/fileapi/webblob_messages.h" | |
22 #include "storage/common/blob_storage/blob_storage_constants.h" | |
23 #include "third_party/WebKit/public/platform/FilePathConversion.h" | |
24 #include "third_party/WebKit/public/platform/WebBlobData.h" | |
25 #include "third_party/WebKit/public/platform/WebString.h" | |
26 #include "third_party/WebKit/public/platform/WebThreadSafeData.h" | |
27 #include "third_party/WebKit/public/platform/WebURL.h" | |
28 | |
29 using blink::WebBlobData; | |
30 using blink::WebString; | |
31 using blink::WebThreadSafeData; | |
32 using blink::WebURL; | |
33 using blink::WebBlobRegistry; | |
34 using storage::DataElement; | |
35 | |
36 namespace content { | |
37 | |
38 WebBlobRegistryImpl::WebBlobRegistryImpl( | |
39 scoped_refptr<base::SingleThreadTaskRunner> io_runner, | |
40 scoped_refptr<base::SingleThreadTaskRunner> main_runner, | |
41 scoped_refptr<ThreadSafeSender> sender) | |
42 : io_runner_(std::move(io_runner)), | |
43 main_runner_(std::move(main_runner)), | |
44 sender_(std::move(sender)) { | |
45 // Record a dummy trace event on startup so the 'Storage' category shows up | |
46 // in the chrome://tracing viewer. | |
47 TRACE_EVENT0("Blob", "Init"); | |
48 } | |
49 | |
50 WebBlobRegistryImpl::~WebBlobRegistryImpl() { | |
51 } | |
52 | |
53 std::unique_ptr<WebBlobRegistry::Builder> WebBlobRegistryImpl::createBuilder( | |
54 const blink::WebString& uuid, | |
55 const blink::WebString& content_type) { | |
56 return base::WrapUnique(new BuilderImpl(uuid, content_type, sender_.get(), | |
57 io_runner_, main_runner_)); | |
58 } | |
59 | |
60 void WebBlobRegistryImpl::registerBlobData(const blink::WebString& uuid, | |
61 const blink::WebBlobData& data) { | |
62 TRACE_EVENT0("Blob", "Registry::RegisterBlob"); | |
63 std::unique_ptr<Builder> builder(createBuilder(uuid, data.contentType())); | |
64 | |
65 // This is temporary until we move to createBuilder() as our blob creation | |
66 // method. | |
67 size_t i = 0; | |
68 WebBlobData::Item data_item; | |
69 while (data.itemAt(i++, data_item)) { | |
70 if (data_item.length == 0) { | |
71 continue; | |
72 } | |
73 switch (data_item.type) { | |
74 case WebBlobData::Item::TypeData: { | |
75 // WebBlobData does not allow partial data items. | |
76 DCHECK(!data_item.offset && data_item.length == -1); | |
77 builder->appendData(data_item.data); | |
78 break; | |
79 } | |
80 case WebBlobData::Item::TypeFile: | |
81 builder->appendFile(data_item.filePath, | |
82 static_cast<uint64_t>(data_item.offset), | |
83 static_cast<uint64_t>(data_item.length), | |
84 data_item.expectedModificationTime); | |
85 break; | |
86 case WebBlobData::Item::TypeBlob: | |
87 builder->appendBlob(data_item.blobUUID, data_item.offset, | |
88 data_item.length); | |
89 break; | |
90 case WebBlobData::Item::TypeFileSystemURL: | |
91 // We only support filesystem URL as of now. | |
92 DCHECK(GURL(data_item.fileSystemURL).SchemeIsFileSystem()); | |
93 builder->appendFileSystemURL(data_item.fileSystemURL, | |
94 static_cast<uint64_t>(data_item.offset), | |
95 static_cast<uint64_t>(data_item.length), | |
96 data_item.expectedModificationTime); | |
97 break; | |
98 default: | |
99 NOTREACHED(); | |
100 } | |
101 } | |
102 builder->build(); | |
103 } | |
104 | |
105 void WebBlobRegistryImpl::addBlobDataRef(const WebString& uuid) { | |
106 sender_->Send(new BlobHostMsg_IncrementRefCount(uuid.utf8())); | |
107 } | |
108 | |
109 void WebBlobRegistryImpl::removeBlobDataRef(const WebString& uuid) { | |
110 sender_->Send(new BlobHostMsg_DecrementRefCount(uuid.utf8())); | |
111 } | |
112 | |
113 void WebBlobRegistryImpl::registerPublicBlobURL(const WebURL& url, | |
114 const WebString& uuid) { | |
115 sender_->Send(new BlobHostMsg_RegisterPublicURL(url, uuid.utf8())); | |
116 } | |
117 | |
118 void WebBlobRegistryImpl::revokePublicBlobURL(const WebURL& url) { | |
119 sender_->Send(new BlobHostMsg_RevokePublicURL(url)); | |
120 } | |
121 | |
122 // ------ streams stuff ----- | |
123 | |
124 void WebBlobRegistryImpl::registerStreamURL(const WebURL& url, | |
125 const WebString& content_type) { | |
126 DCHECK(ChildThreadImpl::current()); | |
127 sender_->Send(new StreamHostMsg_StartBuilding(url, content_type.utf8())); | |
128 } | |
129 | |
130 void WebBlobRegistryImpl::registerStreamURL(const WebURL& url, | |
131 const WebURL& src_url) { | |
132 DCHECK(ChildThreadImpl::current()); | |
133 sender_->Send(new StreamHostMsg_Clone(url, src_url)); | |
134 } | |
135 | |
136 void WebBlobRegistryImpl::addDataToStream(const WebURL& url, | |
137 const char* data, | |
138 size_t length) { | |
139 DCHECK(ChildThreadImpl::current()); | |
140 if (length == 0) | |
141 return; | |
142 if (length < storage::kBlobStorageIPCThresholdBytes) { | |
143 DataElement item; | |
144 item.SetToBytes(data, length); | |
145 sender_->Send(new StreamHostMsg_AppendBlobDataItem(url, item)); | |
146 } else { | |
147 // We handle larger amounts of data via SharedMemory instead of | |
148 // writing it directly to the IPC channel. | |
149 size_t shared_memory_size = | |
150 std::min(length, storage::kBlobStorageMaxSharedMemoryBytes); | |
151 std::unique_ptr<base::SharedMemory> shared_memory( | |
152 ChildThreadImpl::AllocateSharedMemory(shared_memory_size, | |
153 sender_.get())); | |
154 CHECK(shared_memory.get()); | |
155 if (!shared_memory->Map(shared_memory_size)) | |
156 CHECK(false); | |
157 | |
158 size_t remaining_bytes = length; | |
159 const char* current_ptr = data; | |
160 while (remaining_bytes) { | |
161 size_t chunk_size = std::min(remaining_bytes, shared_memory_size); | |
162 memcpy(shared_memory->memory(), current_ptr, chunk_size); | |
163 sender_->Send(new StreamHostMsg_SyncAppendSharedMemory( | |
164 url, shared_memory->handle(), | |
165 base::checked_cast<uint32_t>(chunk_size))); | |
166 remaining_bytes -= chunk_size; | |
167 current_ptr += chunk_size; | |
168 } | |
169 } | |
170 } | |
171 | |
172 void WebBlobRegistryImpl::flushStream(const WebURL& url) { | |
173 DCHECK(ChildThreadImpl::current()); | |
174 sender_->Send(new StreamHostMsg_Flush(url)); | |
175 } | |
176 | |
177 void WebBlobRegistryImpl::finalizeStream(const WebURL& url) { | |
178 DCHECK(ChildThreadImpl::current()); | |
179 sender_->Send(new StreamHostMsg_FinishBuilding(url)); | |
180 } | |
181 | |
182 void WebBlobRegistryImpl::abortStream(const WebURL& url) { | |
183 DCHECK(ChildThreadImpl::current()); | |
184 sender_->Send(new StreamHostMsg_AbortBuilding(url)); | |
185 } | |
186 | |
187 void WebBlobRegistryImpl::unregisterStreamURL(const WebURL& url) { | |
188 DCHECK(ChildThreadImpl::current()); | |
189 sender_->Send(new StreamHostMsg_Remove(url)); | |
190 } | |
191 | |
192 WebBlobRegistryImpl::BuilderImpl::BuilderImpl( | |
193 const blink::WebString& uuid, | |
194 const blink::WebString& content_type, | |
195 ThreadSafeSender* sender, | |
196 scoped_refptr<base::SingleThreadTaskRunner> io_runner, | |
197 scoped_refptr<base::SingleThreadTaskRunner> main_runner) | |
198 : uuid_(uuid.utf8()), | |
199 content_type_(content_type.utf8()), | |
200 consolidation_(new BlobConsolidation()), | |
201 sender_(sender), | |
202 io_runner_(std::move(io_runner)), | |
203 main_runner_(std::move(main_runner)) {} | |
204 | |
205 WebBlobRegistryImpl::BuilderImpl::~BuilderImpl() { | |
206 } | |
207 | |
208 void WebBlobRegistryImpl::BuilderImpl::appendData( | |
209 const WebThreadSafeData& data) { | |
210 consolidation_->AddDataItem(data); | |
211 } | |
212 | |
213 void WebBlobRegistryImpl::BuilderImpl::appendBlob(const WebString& uuid, | |
214 uint64_t offset, | |
215 uint64_t length) { | |
216 consolidation_->AddBlobItem(uuid.utf8(), offset, length); | |
217 } | |
218 | |
219 void WebBlobRegistryImpl::BuilderImpl::appendFile( | |
220 const WebString& path, | |
221 uint64_t offset, | |
222 uint64_t length, | |
223 double expected_modification_time) { | |
224 consolidation_->AddFileItem(blink::WebStringToFilePath(path), offset, length, | |
225 expected_modification_time); | |
226 } | |
227 | |
228 void WebBlobRegistryImpl::BuilderImpl::appendFileSystemURL( | |
229 const WebURL& fileSystemURL, | |
230 uint64_t offset, | |
231 uint64_t length, | |
232 double expected_modification_time) { | |
233 DCHECK(GURL(fileSystemURL).SchemeIsFileSystem()); | |
234 consolidation_->AddFileSystemItem(GURL(fileSystemURL), offset, length, | |
235 expected_modification_time); | |
236 } | |
237 | |
238 void WebBlobRegistryImpl::BuilderImpl::build() { | |
239 BlobTransportController::InitiateBlobTransfer( | |
240 uuid_, content_type_, std::move(consolidation_), sender_, | |
241 io_runner_.get(), main_runner_); | |
242 } | |
243 | |
244 } // namespace content | |
OLD | NEW |